2024-03-07 06:50:11 +00:00
|
|
|
# common logic across linux and darwin
|
2023-11-14 01:20:34 +00:00
|
|
|
|
|
|
|
init_vars() {
|
2024-01-13 00:28:00 +00:00
|
|
|
case "${GOARCH}" in
|
|
|
|
"amd64")
|
|
|
|
ARCH="x86_64"
|
|
|
|
;;
|
|
|
|
"arm64")
|
|
|
|
ARCH="arm64"
|
|
|
|
;;
|
|
|
|
*)
|
2024-07-08 19:50:11 +00:00
|
|
|
echo "GOARCH must be set"
|
|
|
|
echo "this script is meant to be run from within go generate"
|
|
|
|
exit 1
|
|
|
|
;;
|
2024-01-13 00:28:00 +00:00
|
|
|
esac
|
|
|
|
|
2024-01-04 17:40:15 +00:00
|
|
|
LLAMACPP_DIR=../llama.cpp
|
2024-07-08 19:50:11 +00:00
|
|
|
CMAKE_DEFS="-DCMAKE_SKIP_RPATH=on"
|
2024-03-14 17:24:13 +00:00
|
|
|
CMAKE_TARGETS="--target ollama_llama_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-04-25 18:32:26 +00:00
|
|
|
case $(uname -s) in
|
2024-01-13 00:28:00 +00:00
|
|
|
"Darwin")
|
|
|
|
LIB_EXT="dylib"
|
|
|
|
WHOLE_ARCHIVE="-Wl,-force_load"
|
|
|
|
NO_WHOLE_ARCHIVE=""
|
|
|
|
GCC_ARCH="-arch ${ARCH}"
|
2024-07-08 19:50:11 +00:00
|
|
|
DIST_BASE=../../dist/darwin-${GOARCH}/
|
2024-09-12 19:10:30 +00:00
|
|
|
PAYLOAD_BASE=../../build/darwin/${GOARCH}
|
2024-01-13 00:28:00 +00:00
|
|
|
;;
|
|
|
|
"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=""
|
2024-07-08 19:50:11 +00:00
|
|
|
DIST_BASE=../../dist/linux-${GOARCH}/
|
2024-09-12 19:10:30 +00:00
|
|
|
PAYLOAD_BASE=../../build/linux/${GOARCH}
|
2024-01-13 00:28:00 +00:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
;;
|
|
|
|
esac
|
2024-03-25 15:07:44 +00:00
|
|
|
if [ -z "${CMAKE_CUDA_ARCHITECTURES}" ] ; then
|
2024-01-20 20:15:50 +00:00
|
|
|
CMAKE_CUDA_ARCHITECTURES="50;52;61;70;75;80"
|
|
|
|
fi
|
2024-09-12 19:10:30 +00:00
|
|
|
GZIP=$(command -v pigz 2>/dev/null || echo "gzip")
|
|
|
|
RUNNER_BASE="${DIST_BASE}/lib/ollama/runners"
|
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() {
|
2024-09-16 22:58:55 +00:00
|
|
|
# apply temporary patches until fix is upstream
|
|
|
|
for patch in ../patches/*.patch; do
|
|
|
|
git -c 'user.name=nobody' -c 'user.email=<>' -C ${LLAMACPP_DIR} am ${patch}
|
|
|
|
done
|
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-08-29 20:40:43 +00:00
|
|
|
# remove unnecessary build artifacts
|
|
|
|
rm -f ${BUILD_DIR}/bin/ggml-common.h ${BUILD_DIR}/bin/ggml-metal.metal
|
2023-12-20 18:36:01 +00:00
|
|
|
}
|
2023-12-22 17:51:53 +00:00
|
|
|
|
2024-09-12 19:10:30 +00:00
|
|
|
dist() {
|
|
|
|
[ -z "${RUNNER}" ] && exit 1
|
|
|
|
mkdir -p ${RUNNER_BASE}/${RUNNER}/
|
|
|
|
for f in ${BUILD_DIR}/bin/* ; do
|
|
|
|
cp ${f} ${RUNNER_BASE}/${RUNNER}/
|
|
|
|
done
|
|
|
|
# check for lib directory
|
|
|
|
if [ -d ${BUILD_DIR}/lib ]; then
|
|
|
|
for f in ${BUILD_DIR}/lib/* ; do
|
|
|
|
cp ${f} ${RUNNER_BASE}/${RUNNER}/
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Compress from the build $BUILD_DIR into the $PAYLOAD_BASE/$RUNNER dir
|
2024-03-14 17:24:13 +00:00
|
|
|
compress() {
|
2024-09-12 19:10:30 +00:00
|
|
|
[ -z "${RUNNER}" ] && exit 1
|
|
|
|
echo "Compressing payloads with ${GZIP} to reduce overall binary size..."
|
|
|
|
rm -rf "${PAYLOAD_BASE}/${RUNNER}/"
|
|
|
|
mkdir -p "${PAYLOAD_BASE}/${RUNNER}/"
|
2024-03-14 17:24:13 +00:00
|
|
|
for f in ${BUILD_DIR}/bin/* ; do
|
2024-09-12 19:10:30 +00:00
|
|
|
${GZIP} -c --best ${f} > "${PAYLOAD_BASE}/${RUNNER}/$(basename ${f}).gz" &
|
2024-08-09 14:21:40 +00:00
|
|
|
compress_pids+=" $!"
|
2024-01-13 00:28:00 +00:00
|
|
|
done
|
2024-03-14 17:24:13 +00:00
|
|
|
# check for lib directory
|
|
|
|
if [ -d ${BUILD_DIR}/lib ]; then
|
|
|
|
for f in ${BUILD_DIR}/lib/* ; do
|
2024-09-12 19:10:30 +00:00
|
|
|
${GZIP} -c --best ${f} > "${PAYLOAD_BASE}/${RUNNER}/$(basename ${f}).gz" &
|
2024-08-09 14:21:40 +00:00
|
|
|
compress_pids+=" $!"
|
2024-03-14 17:24:13 +00:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
echo
|
2024-08-09 14:21:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wait_for_compress() {
|
|
|
|
for pid in ${compress_pids}; do
|
2024-01-13 00:28:00 +00:00
|
|
|
wait $pid
|
|
|
|
done
|
|
|
|
echo "Finished compression"
|
2024-01-07 23:48:05 +00:00
|
|
|
}
|
|
|
|
|
2024-07-08 19:50:11 +00:00
|
|
|
install() {
|
|
|
|
echo "Installing libraries to bin dir ${BUILD_DIR}/bin/"
|
2024-09-13 15:24:08 +00:00
|
|
|
for lib in $(find ${BUILD_DIR} -name \*.${LIB_EXT} | grep -v "${BUILD_DIR}/bin/" ); do
|
2024-07-08 19:50:11 +00:00
|
|
|
rm -f "${BUILD_DIR}/bin/$(basename ${lib})"
|
|
|
|
cp -af "${lib}" "${BUILD_DIR}/bin/"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2023-12-22 17:51:53 +00:00
|
|
|
# Keep the local tree clean after we're done with the build
|
|
|
|
cleanup() {
|
2024-03-12 20:51:44 +00:00
|
|
|
(cd ${LLAMACPP_DIR}/ && git checkout CMakeLists.txt)
|
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
|
|
|
}
|