gen_common.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # common logic across linux and darwin
  2. init_vars() {
  3. case "${GOARCH}" in
  4. "amd64")
  5. ARCH="x86_64"
  6. ;;
  7. "arm64")
  8. ARCH="arm64"
  9. ;;
  10. *)
  11. echo "GOARCH must be set"
  12. echo "this script is meant to be run from within go generate"
  13. exit 1
  14. ;;
  15. esac
  16. LLAMACPP_DIR=../llama.cpp
  17. CMAKE_DEFS="-DCMAKE_SKIP_RPATH=on"
  18. CMAKE_TARGETS="--target ollama_llama_server"
  19. if echo "${CGO_CFLAGS}" | grep -- '-g' >/dev/null; then
  20. CMAKE_DEFS="-DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_VERBOSE_MAKEFILE=on -DLLAMA_GPROF=on -DLLAMA_SERVER_VERBOSE=on ${CMAKE_DEFS}"
  21. else
  22. # TODO - add additional optimization flags...
  23. CMAKE_DEFS="-DCMAKE_BUILD_TYPE=Release -DLLAMA_SERVER_VERBOSE=off ${CMAKE_DEFS}"
  24. fi
  25. case $(uname -s) in
  26. "Darwin")
  27. LIB_EXT="dylib"
  28. WHOLE_ARCHIVE="-Wl,-force_load"
  29. NO_WHOLE_ARCHIVE=""
  30. GCC_ARCH="-arch ${ARCH}"
  31. DIST_BASE=../../dist/darwin-${GOARCH}/
  32. PAYLOAD_BASE=../../build/darwin/${GOARCH}
  33. ;;
  34. "Linux")
  35. LIB_EXT="so"
  36. WHOLE_ARCHIVE="-Wl,--whole-archive"
  37. NO_WHOLE_ARCHIVE="-Wl,--no-whole-archive"
  38. # Cross compiling not supported on linux - Use docker
  39. GCC_ARCH=""
  40. DIST_BASE=../../dist/linux-${GOARCH}/
  41. PAYLOAD_BASE=../../build/linux/${GOARCH}
  42. ;;
  43. *)
  44. ;;
  45. esac
  46. if [ -z "${CMAKE_CUDA_ARCHITECTURES}" ] ; then
  47. CMAKE_CUDA_ARCHITECTURES="50;52;61;70;75;80"
  48. fi
  49. GZIP=$(command -v pigz 2>/dev/null || echo "gzip")
  50. RUNNER_BASE="${DIST_BASE}/lib/ollama/runners"
  51. }
  52. git_module_setup() {
  53. if [ -n "${OLLAMA_SKIP_PATCHING}" ]; then
  54. echo "Skipping submodule initialization"
  55. return
  56. fi
  57. # Make sure the tree is clean after the directory moves
  58. if [ -d "${LLAMACPP_DIR}/gguf" ]; then
  59. echo "Cleaning up old submodule"
  60. rm -rf ${LLAMACPP_DIR}
  61. fi
  62. git submodule init
  63. git submodule update --force ${LLAMACPP_DIR}
  64. }
  65. apply_patches() {
  66. # apply temporary patches until fix is upstream
  67. for patch in ../patches/*.patch; do
  68. git -c 'user.name=nobody' -c 'user.email=<>' -C ${LLAMACPP_DIR} am ${patch}
  69. done
  70. }
  71. build() {
  72. cmake -S ${LLAMACPP_DIR} -B ${BUILD_DIR} ${CMAKE_DEFS}
  73. cmake --build ${BUILD_DIR} ${CMAKE_TARGETS} -j8
  74. # remove unnecessary build artifacts
  75. rm -f ${BUILD_DIR}/bin/ggml-common.h ${BUILD_DIR}/bin/ggml-metal.metal
  76. }
  77. dist() {
  78. [ -z "${RUNNER}" ] && exit 1
  79. mkdir -p ${RUNNER_BASE}/${RUNNER}/
  80. for f in ${BUILD_DIR}/bin/* ; do
  81. cp ${f} ${RUNNER_BASE}/${RUNNER}/
  82. done
  83. # check for lib directory
  84. if [ -d ${BUILD_DIR}/lib ]; then
  85. for f in ${BUILD_DIR}/lib/* ; do
  86. cp ${f} ${RUNNER_BASE}/${RUNNER}/
  87. done
  88. fi
  89. }
  90. # Compress from the build $BUILD_DIR into the $PAYLOAD_BASE/$RUNNER dir
  91. compress() {
  92. [ -z "${RUNNER}" ] && exit 1
  93. echo "Compressing payloads with ${GZIP} to reduce overall binary size..."
  94. rm -rf "${PAYLOAD_BASE}/${RUNNER}/"
  95. mkdir -p "${PAYLOAD_BASE}/${RUNNER}/"
  96. for f in ${BUILD_DIR}/bin/* ; do
  97. ${GZIP} -c --best ${f} > "${PAYLOAD_BASE}/${RUNNER}/$(basename ${f}).gz" &
  98. compress_pids+=" $!"
  99. done
  100. # check for lib directory
  101. if [ -d ${BUILD_DIR}/lib ]; then
  102. for f in ${BUILD_DIR}/lib/* ; do
  103. ${GZIP} -c --best ${f} > "${PAYLOAD_BASE}/${RUNNER}/$(basename ${f}).gz" &
  104. compress_pids+=" $!"
  105. done
  106. fi
  107. echo
  108. }
  109. wait_for_compress() {
  110. for pid in ${compress_pids}; do
  111. wait $pid
  112. done
  113. echo "Finished compression"
  114. }
  115. install() {
  116. echo "Installing libraries to bin dir ${BUILD_DIR}/bin/"
  117. for lib in $(find ${BUILD_DIR} -name \*.${LIB_EXT} | grep -v "${BUILD_DIR}/bin/" ); do
  118. rm -f "${BUILD_DIR}/bin/$(basename ${lib})"
  119. cp -af "${lib}" "${BUILD_DIR}/bin/"
  120. done
  121. }
  122. # Keep the local tree clean after we're done with the build
  123. cleanup() {
  124. (cd ${LLAMACPP_DIR}/ && git checkout CMakeLists.txt)
  125. if [ -n "$(ls -A ../patches/*.diff)" ]; then
  126. for patch in ../patches/*.diff; do
  127. for file in $(grep "^+++ " ${patch} | cut -f2 -d' ' | cut -f2- -d/); do
  128. (cd ${LLAMACPP_DIR}; git checkout ${file})
  129. done
  130. done
  131. fi
  132. }