2023-11-14 01:20:34 +00:00
|
|
|
# common logic accross linux and darwin
|
|
|
|
|
|
|
|
init_vars() {
|
2024-01-13 00:28:00 +00:00
|
|
|
case "${GOARCH}" in
|
|
|
|
"amd64")
|
|
|
|
ARCH="x86_64"
|
|
|
|
;;
|
|
|
|
"arm64")
|
|
|
|
ARCH="arm64"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
ARCH=$(uname -m | sed -e "s/aarch64/arm64/g")
|
|
|
|
esac
|
|
|
|
|
2024-01-04 17:40:15 +00:00
|
|
|
LLAMACPP_DIR=../llama.cpp
|
2024-01-04 00:22:15 +00:00
|
|
|
CMAKE_DEFS=""
|
2024-01-13 00:28:00 +00:00
|
|
|
CMAKE_TARGETS="--target ext_server"
|
2023-12-20 18:36:01 +00:00
|
|
|
if echo "${CGO_CFLAGS}" | grep -- '-g' >/dev/null; then
|
2024-01-13 00:28:00 +00:00
|
|
|
CMAKE_DEFS="-DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_VERBOSE_MAKEFILE=on -DLLAMA_GPROF=on -DLLAMA_SERVER_VERBOSE=on ${CMAKE_DEFS}"
|
2023-11-14 01:20:34 +00:00
|
|
|
else
|
|
|
|
# TODO - add additional optimization flags...
|
2024-01-13 00:28:00 +00:00
|
|
|
CMAKE_DEFS="-DCMAKE_BUILD_TYPE=Release -DLLAMA_SERVER_VERBOSE=off ${CMAKE_DEFS}"
|
2023-11-14 01:20:34 +00:00
|
|
|
fi
|
2024-01-13 00:28:00 +00:00
|
|
|
case $(uname -s) in
|
|
|
|
"Darwin")
|
|
|
|
LIB_EXT="dylib"
|
|
|
|
WHOLE_ARCHIVE="-Wl,-force_load"
|
|
|
|
NO_WHOLE_ARCHIVE=""
|
|
|
|
GCC_ARCH="-arch ${ARCH}"
|
|
|
|
;;
|
|
|
|
"Linux")
|
|
|
|
LIB_EXT="so"
|
|
|
|
WHOLE_ARCHIVE="-Wl,--whole-archive"
|
|
|
|
NO_WHOLE_ARCHIVE="-Wl,--no-whole-archive"
|
|
|
|
|
|
|
|
# Cross compiling not supported on linux - Use docker
|
|
|
|
GCC_ARCH=""
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
;;
|
|
|
|
esac
|
2024-01-20 20:15:50 +00:00
|
|
|
if [ -z "${CMAKE_CUDA_ARCHITECTURES}" ] ; then
|
|
|
|
CMAKE_CUDA_ARCHITECTURES="50;52;61;70;75;80"
|
|
|
|
fi
|
2023-11-14 01:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
git_module_setup() {
|
2023-12-20 18:36:01 +00:00
|
|
|
if [ -n "${OLLAMA_SKIP_PATCHING}" ]; then
|
2023-12-18 20:05:59 +00:00
|
|
|
echo "Skipping submodule initialization"
|
|
|
|
return
|
|
|
|
fi
|
2024-01-04 21:40:16 +00:00
|
|
|
# Make sure the tree is clean after the directory moves
|
|
|
|
if [ -d "${LLAMACPP_DIR}/gguf" ]; then
|
|
|
|
echo "Cleaning up old submodule"
|
|
|
|
rm -rf ${LLAMACPP_DIR}
|
|
|
|
fi
|
2023-11-14 01:20:34 +00:00
|
|
|
git submodule init
|
2024-01-04 17:40:15 +00:00
|
|
|
git submodule update --force ${LLAMACPP_DIR}
|
2023-11-14 01:20:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
apply_patches() {
|
2023-12-22 17:51:53 +00:00
|
|
|
# Wire up our CMakefile
|
2024-01-04 17:40:15 +00:00
|
|
|
if ! grep ollama ${LLAMACPP_DIR}/examples/server/CMakeLists.txt; then
|
|
|
|
echo 'include (../../../ext_server/CMakeLists.txt) # ollama' >>${LLAMACPP_DIR}/examples/server/CMakeLists.txt
|
2023-12-18 20:05:59 +00:00
|
|
|
fi
|
2024-01-25 21:46:20 +00:00
|
|
|
|
2024-02-02 03:34:36 +00:00
|
|
|
if [ -n "$(ls -A ../patches/*.diff)" ]; then
|
|
|
|
# apply temporary patches until fix is upstream
|
|
|
|
for patch in ../patches/*.diff; do
|
|
|
|
for file in $(grep "^+++ " ${patch} | cut -f2 -d' ' | cut -f2- -d/); do
|
|
|
|
(cd ${LLAMACPP_DIR}; git checkout ${file})
|
|
|
|
done
|
2024-01-25 21:46:20 +00:00
|
|
|
done
|
2024-02-02 03:34:36 +00:00
|
|
|
for patch in ../patches/*.diff; do
|
|
|
|
(cd ${LLAMACPP_DIR} && git apply ${patch})
|
|
|
|
done
|
|
|
|
fi
|
2024-01-25 21:46:20 +00:00
|
|
|
|
2023-12-22 17:51:53 +00:00
|
|
|
# Avoid duplicate main symbols when we link into the cgo binary
|
2024-01-04 17:40:15 +00:00
|
|
|
sed -e 's/int main(/int __main(/g' <${LLAMACPP_DIR}/examples/server/server.cpp >${LLAMACPP_DIR}/examples/server/server.cpp.tmp &&
|
|
|
|
mv ${LLAMACPP_DIR}/examples/server/server.cpp.tmp ${LLAMACPP_DIR}/examples/server/server.cpp
|
2023-11-14 01:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
build() {
|
2023-11-29 19:00:37 +00:00
|
|
|
cmake -S ${LLAMACPP_DIR} -B ${BUILD_DIR} ${CMAKE_DEFS}
|
|
|
|
cmake --build ${BUILD_DIR} ${CMAKE_TARGETS} -j8
|
2024-01-13 00:28:00 +00:00
|
|
|
mkdir -p ${BUILD_DIR}/lib/
|
|
|
|
g++ -fPIC -g -shared -o ${BUILD_DIR}/lib/libext_server.${LIB_EXT} \
|
|
|
|
${GCC_ARCH} \
|
|
|
|
${WHOLE_ARCHIVE} ${BUILD_DIR}/examples/server/libext_server.a ${NO_WHOLE_ARCHIVE} \
|
|
|
|
${BUILD_DIR}/common/libcommon.a \
|
|
|
|
${BUILD_DIR}/libllama.a \
|
|
|
|
-Wl,-rpath,\$ORIGIN \
|
|
|
|
-lpthread -ldl -lm \
|
|
|
|
${EXTRA_LIBS}
|
2023-12-20 18:36:01 +00:00
|
|
|
}
|
2023-12-22 17:51:53 +00:00
|
|
|
|
2024-01-13 00:28:00 +00:00
|
|
|
compress_libs() {
|
|
|
|
echo "Compressing payloads to reduce overall binary size..."
|
|
|
|
pids=""
|
2024-01-20 00:24:11 +00:00
|
|
|
rm -rf ${BUILD_DIR}/lib/*.${LIB_EXT}*.gz
|
2024-01-13 00:28:00 +00:00
|
|
|
for lib in ${BUILD_DIR}/lib/*.${LIB_EXT}* ; do
|
2024-01-20 00:24:11 +00:00
|
|
|
gzip --best -f ${lib} &
|
2024-01-13 00:28:00 +00:00
|
|
|
pids+=" $!"
|
|
|
|
done
|
|
|
|
echo
|
|
|
|
for pid in ${pids}; do
|
|
|
|
wait $pid
|
|
|
|
done
|
|
|
|
echo "Finished compression"
|
2024-01-07 23:48:05 +00:00
|
|
|
}
|
|
|
|
|
2023-12-22 17:51:53 +00:00
|
|
|
# Keep the local tree clean after we're done with the build
|
|
|
|
cleanup() {
|
2024-01-04 17:40:15 +00:00
|
|
|
(cd ${LLAMACPP_DIR}/examples/server/ && git checkout CMakeLists.txt server.cpp)
|
2024-02-02 03:34:36 +00:00
|
|
|
|
|
|
|
if [ -n "$(ls -A ../patches/*.diff)" ]; then
|
|
|
|
for patch in ../patches/*.diff; do
|
|
|
|
for file in $(grep "^+++ " ${patch} | cut -f2 -d' ' | cut -f2- -d/); do
|
|
|
|
(cd ${LLAMACPP_DIR}; git checkout ${file})
|
|
|
|
done
|
|
|
|
done
|
|
|
|
fi
|
2023-12-22 17:51:53 +00:00
|
|
|
}
|