gen_common.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. # Wire up our CMakefile
  67. if ! grep ollama ${LLAMACPP_DIR}/CMakeLists.txt; then
  68. echo 'add_subdirectory(../ext_server ext_server) # ollama' >>${LLAMACPP_DIR}/CMakeLists.txt
  69. fi
  70. if [ -n "$(ls -A ../patches/*.diff)" ]; then
  71. # apply temporary patches until fix is upstream
  72. for patch in ../patches/*.diff; do
  73. for file in $(grep "^+++ " ${patch} | cut -f2 -d' ' | cut -f2- -d/); do
  74. (cd ${LLAMACPP_DIR}; git checkout ${file})
  75. done
  76. done
  77. for patch in ../patches/*.diff; do
  78. (cd ${LLAMACPP_DIR} && git apply ${patch})
  79. done
  80. fi
  81. }
  82. build() {
  83. cmake -S ${LLAMACPP_DIR} -B ${BUILD_DIR} ${CMAKE_DEFS}
  84. cmake --build ${BUILD_DIR} ${CMAKE_TARGETS} -j8
  85. # remove unnecessary build artifacts
  86. rm -f ${BUILD_DIR}/bin/ggml-common.h ${BUILD_DIR}/bin/ggml-metal.metal
  87. }
  88. dist() {
  89. [ -z "${RUNNER}" ] && exit 1
  90. mkdir -p ${RUNNER_BASE}/${RUNNER}/
  91. for f in ${BUILD_DIR}/bin/* ; do
  92. cp ${f} ${RUNNER_BASE}/${RUNNER}/
  93. done
  94. # check for lib directory
  95. if [ -d ${BUILD_DIR}/lib ]; then
  96. for f in ${BUILD_DIR}/lib/* ; do
  97. cp ${f} ${RUNNER_BASE}/${RUNNER}/
  98. done
  99. fi
  100. }
  101. # Compress from the build $BUILD_DIR into the $PAYLOAD_BASE/$RUNNER dir
  102. compress() {
  103. [ -z "${RUNNER}" ] && exit 1
  104. echo "Compressing payloads with ${GZIP} to reduce overall binary size..."
  105. rm -rf "${PAYLOAD_BASE}/${RUNNER}/"
  106. mkdir -p "${PAYLOAD_BASE}/${RUNNER}/"
  107. for f in ${BUILD_DIR}/bin/* ; do
  108. ${GZIP} -c --best ${f} > "${PAYLOAD_BASE}/${RUNNER}/$(basename ${f}).gz" &
  109. compress_pids+=" $!"
  110. done
  111. # check for lib directory
  112. if [ -d ${BUILD_DIR}/lib ]; then
  113. for f in ${BUILD_DIR}/lib/* ; do
  114. ${GZIP} -c --best ${f} > "${PAYLOAD_BASE}/${RUNNER}/$(basename ${f}).gz" &
  115. compress_pids+=" $!"
  116. done
  117. fi
  118. echo
  119. }
  120. wait_for_compress() {
  121. for pid in ${compress_pids}; do
  122. wait $pid
  123. done
  124. echo "Finished compression"
  125. }
  126. install() {
  127. echo "Installing libraries to bin dir ${BUILD_DIR}/bin/"
  128. for lib in $(find ${BUILD_DIR} -name \*.${LIB_EXT}); do
  129. rm -f "${BUILD_DIR}/bin/$(basename ${lib})"
  130. cp -af "${lib}" "${BUILD_DIR}/bin/"
  131. done
  132. }
  133. # Keep the local tree clean after we're done with the build
  134. cleanup() {
  135. (cd ${LLAMACPP_DIR}/ && git checkout CMakeLists.txt)
  136. if [ -n "$(ls -A ../patches/*.diff)" ]; then
  137. for patch in ../patches/*.diff; do
  138. for file in $(grep "^+++ " ${patch} | cut -f2 -d' ' | cut -f2- -d/); do
  139. (cd ${LLAMACPP_DIR}; git checkout ${file})
  140. done
  141. done
  142. fi
  143. }