gen_linux.sh 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!/bin/bash
  2. # This script is intended to run inside the go generate
  3. # working directory must be llm/generate/
  4. # First we build one or more CPU based LLM libraries
  5. #
  6. # Then if we detect CUDA, we build a CUDA dynamic library, and carry the required
  7. # library dependencies
  8. #
  9. # Then if we detect ROCm, we build a dynamically loaded ROCm lib. The ROCM
  10. # libraries are quite large, and also dynamically load data files at runtime
  11. # which in turn are large, so we don't attempt to cary them as payload
  12. set -ex
  13. set -o pipefail
  14. # See https://llvm.org/docs/AMDGPUUsage.html#processors for reference
  15. amdGPUs() {
  16. if [ -n "${AMDGPU_TARGETS}" ]; then
  17. echo "${AMDGPU_TARGETS}"
  18. return
  19. fi
  20. GPU_LIST=(
  21. "gfx900"
  22. "gfx906:xnack-"
  23. "gfx908:xnack-"
  24. "gfx90a:xnack+"
  25. "gfx90a:xnack-"
  26. "gfx940"
  27. "gfx941"
  28. "gfx942"
  29. "gfx1010"
  30. "gfx1012"
  31. "gfx1030"
  32. "gfx1100"
  33. "gfx1101"
  34. "gfx1102"
  35. )
  36. (
  37. IFS=$';'
  38. echo "'${GPU_LIST[*]}'"
  39. )
  40. }
  41. echo "Starting linux generate script"
  42. if [ -z "${CUDACXX}" ]; then
  43. if [ -x /usr/local/cuda/bin/nvcc ]; then
  44. export CUDACXX=/usr/local/cuda/bin/nvcc
  45. else
  46. # Try the default location in case it exists
  47. export CUDACXX=$(command -v nvcc)
  48. fi
  49. fi
  50. COMMON_CMAKE_DEFS="-DCMAKE_POSITION_INDEPENDENT_CODE=on -DLLAMA_NATIVE=off -DLLAMA_AVX=on -DLLAMA_AVX2=off -DLLAMA_AVX512=off -DLLAMA_FMA=off -DLLAMA_F16C=off"
  51. source $(dirname $0)/gen_common.sh
  52. init_vars
  53. git_module_setup
  54. apply_patches
  55. init_vars
  56. if [ -z "${OLLAMA_SKIP_STATIC_GENERATE}" -o "${OLLAMA_CPU_TARGET}" = "static" ]; then
  57. # Builds by default, allows skipping, forces build if OLLAMA_CPU_TARGET="static"
  58. # Enables optimized Dockerfile builds using a blanket skip and targeted overrides
  59. # Static build for linking into the Go binary
  60. init_vars
  61. CMAKE_TARGETS="--target llama --target ggml"
  62. CMAKE_DEFS="-DBUILD_SHARED_LIBS=off -DLLAMA_NATIVE=off -DLLAMA_AVX=off -DLLAMA_AVX2=off -DLLAMA_AVX512=off -DLLAMA_FMA=off -DLLAMA_F16C=off ${CMAKE_DEFS}"
  63. BUILD_DIR="../build/linux/${ARCH}_static"
  64. echo "Building static library"
  65. build
  66. fi
  67. init_vars
  68. if [ -z "${OLLAMA_SKIP_CPU_GENERATE}" ]; then
  69. # Users building from source can tune the exact flags we pass to cmake for configuring
  70. # llama.cpp, and we'll build only 1 CPU variant in that case as the default.
  71. if [ -n "${OLLAMA_CUSTOM_CPU_DEFS}" ]; then
  72. init_vars
  73. echo "OLLAMA_CUSTOM_CPU_DEFS=\"${OLLAMA_CUSTOM_CPU_DEFS}\""
  74. CMAKE_DEFS="${OLLAMA_CUSTOM_CPU_DEFS} -DCMAKE_POSITION_INDEPENDENT_CODE=on ${CMAKE_DEFS}"
  75. BUILD_DIR="../build/linux/${ARCH}/cpu"
  76. echo "Building custom CPU"
  77. build
  78. compress
  79. else
  80. # Darwin Rosetta x86 emulation does NOT support AVX, AVX2, AVX512
  81. # -DLLAMA_AVX -- 2011 Intel Sandy Bridge & AMD Bulldozer
  82. # -DLLAMA_F16C -- 2012 Intel Ivy Bridge & AMD 2011 Bulldozer (No significant improvement over just AVX)
  83. # -DLLAMA_AVX2 -- 2013 Intel Haswell & 2015 AMD Excavator / 2017 AMD Zen
  84. # -DLLAMA_FMA (FMA3) -- 2013 Intel Haswell & 2012 AMD Piledriver
  85. # Note: the following seem to yield slower results than AVX2 - ymmv
  86. # -DLLAMA_AVX512 -- 2017 Intel Skylake and High End DeskTop (HEDT)
  87. # -DLLAMA_AVX512_VBMI -- 2018 Intel Cannon Lake
  88. # -DLLAMA_AVX512_VNNI -- 2021 Intel Alder Lake
  89. COMMON_CPU_DEFS="-DCMAKE_POSITION_INDEPENDENT_CODE=on -DLLAMA_NATIVE=off"
  90. if [ -z "${OLLAMA_CPU_TARGET}" -o "${OLLAMA_CPU_TARGET}" = "cpu" ]; then
  91. #
  92. # CPU first for the default library, set up as lowest common denominator for maximum compatibility (including Rosetta)
  93. #
  94. init_vars
  95. CMAKE_DEFS="${COMMON_CPU_DEFS} -DLLAMA_AVX=off -DLLAMA_AVX2=off -DLLAMA_AVX512=off -DLLAMA_FMA=off -DLLAMA_F16C=off ${CMAKE_DEFS}"
  96. BUILD_DIR="../build/linux/${ARCH}/cpu"
  97. echo "Building LCD CPU"
  98. build
  99. compress
  100. fi
  101. if [ "${ARCH}" == "x86_64" ]; then
  102. #
  103. # ARM chips in M1/M2/M3-based MACs and NVidia Tegra devices do not currently support avx extensions.
  104. #
  105. if [ -z "${OLLAMA_CPU_TARGET}" -o "${OLLAMA_CPU_TARGET}" = "cpu_avx" ]; then
  106. #
  107. # ~2011 CPU Dynamic library with more capabilities turned on to optimize performance
  108. # Approximately 400% faster than LCD on same CPU
  109. #
  110. init_vars
  111. CMAKE_DEFS="${COMMON_CPU_DEFS} -DLLAMA_AVX=on -DLLAMA_AVX2=off -DLLAMA_AVX512=off -DLLAMA_FMA=off -DLLAMA_F16C=off ${CMAKE_DEFS}"
  112. BUILD_DIR="../build/linux/${ARCH}/cpu_avx"
  113. echo "Building AVX CPU"
  114. build
  115. compress
  116. fi
  117. if [ -z "${OLLAMA_CPU_TARGET}" -o "${OLLAMA_CPU_TARGET}" = "cpu_avx2" ]; then
  118. #
  119. # ~2013 CPU Dynamic library
  120. # Approximately 10% faster than AVX on same CPU
  121. #
  122. init_vars
  123. CMAKE_DEFS="${COMMON_CPU_DEFS} -DLLAMA_AVX=on -DLLAMA_AVX2=on -DLLAMA_AVX512=off -DLLAMA_FMA=on -DLLAMA_F16C=on ${CMAKE_DEFS}"
  124. BUILD_DIR="../build/linux/${ARCH}/cpu_avx2"
  125. echo "Building AVX2 CPU"
  126. build
  127. compress
  128. fi
  129. fi
  130. fi
  131. else
  132. echo "Skipping CPU generation step as requested"
  133. fi
  134. # If needed, look for the default CUDA toolkit location
  135. if [ -z "${CUDA_LIB_DIR}" ] && [ -d /usr/local/cuda/lib64 ]; then
  136. CUDA_LIB_DIR=/usr/local/cuda/lib64
  137. fi
  138. # If needed, look for CUDA on Arch Linux
  139. if [ -z "${CUDA_LIB_DIR}" ] && [ -d /opt/cuda/targets/x86_64-linux/lib ]; then
  140. CUDA_LIB_DIR=/opt/cuda/targets/x86_64-linux/lib
  141. fi
  142. # Allow override in case libcudart is in the wrong place
  143. if [ -z "${CUDART_LIB_DIR}" ]; then
  144. CUDART_LIB_DIR="${CUDA_LIB_DIR}"
  145. fi
  146. if [ -d "${CUDA_LIB_DIR}" ]; then
  147. echo "CUDA libraries detected - building dynamic CUDA library"
  148. init_vars
  149. CUDA_MAJOR=$(ls "${CUDA_LIB_DIR}"/libcudart.so.* | head -1 | cut -f3 -d. || true)
  150. if [ -n "${CUDA_MAJOR}" ]; then
  151. CUDA_VARIANT=_v${CUDA_MAJOR}
  152. fi
  153. if [ "${ARCH}" == "arm64" ]; then
  154. echo "ARM CPU detected - disabling unsupported AVX instructions"
  155. # ARM-based CPUs such as M1 and Tegra do not support AVX extensions.
  156. #
  157. # CUDA compute < 6.0 lacks proper FP16 support on ARM.
  158. # Disabling has minimal performance effect while maintaining compatibility.
  159. ARM64_DEFS="-DLLAMA_AVX=off -DLLAMA_AVX2=off -DLLAMA_AVX512=off -DLLAMA_CUDA_F16=off"
  160. fi
  161. CMAKE_DEFS="-DLLAMA_CUDA=on -DLLAMA_CUDA_FORCE_MMQ=on -DCMAKE_CUDA_ARCHITECTURES=${CMAKE_CUDA_ARCHITECTURES} ${COMMON_CMAKE_DEFS} ${CMAKE_DEFS} ${ARM64_DEFS}"
  162. BUILD_DIR="../build/linux/${ARCH}/cuda${CUDA_VARIANT}"
  163. EXTRA_LIBS="-L${CUDA_LIB_DIR} -lcudart -lcublas -lcublasLt -lcuda"
  164. build
  165. # Carry the CUDA libs as payloads to help reduce dependency burden on users
  166. #
  167. # TODO - in the future we may shift to packaging these separately and conditionally
  168. # downloading them in the install script.
  169. DEPS="$(ldd ${BUILD_DIR}/bin/ollama_llama_server )"
  170. for lib in libcudart.so libcublas.so libcublasLt.so ; do
  171. DEP=$(echo "${DEPS}" | grep ${lib} | cut -f1 -d' ' | xargs || true)
  172. if [ -n "${DEP}" -a -e "${CUDA_LIB_DIR}/${DEP}" ]; then
  173. cp "${CUDA_LIB_DIR}/${DEP}" "${BUILD_DIR}/bin/"
  174. elif [ -e "${CUDA_LIB_DIR}/${lib}.${CUDA_MAJOR}" ]; then
  175. cp "${CUDA_LIB_DIR}/${lib}.${CUDA_MAJOR}" "${BUILD_DIR}/bin/"
  176. elif [ -e "${CUDART_LIB_DIR}/${lib}" ]; then
  177. cp -d ${CUDART_LIB_DIR}/${lib}* "${BUILD_DIR}/bin/"
  178. else
  179. cp -d "${CUDA_LIB_DIR}/${lib}*" "${BUILD_DIR}/bin/"
  180. fi
  181. done
  182. compress
  183. fi
  184. if [ -z "${ROCM_PATH}" ]; then
  185. # Try the default location in case it exists
  186. ROCM_PATH=/opt/rocm
  187. fi
  188. if [ -z "${CLBlast_DIR}" ]; then
  189. # Try the default location in case it exists
  190. if [ -d /usr/lib/cmake/CLBlast ]; then
  191. export CLBlast_DIR=/usr/lib/cmake/CLBlast
  192. fi
  193. fi
  194. if [ -d "${ROCM_PATH}" ]; then
  195. echo "ROCm libraries detected - building dynamic ROCm library"
  196. if [ -f ${ROCM_PATH}/lib/librocblas.so.*.*.????? ]; then
  197. ROCM_VARIANT=_v$(ls ${ROCM_PATH}/lib/librocblas.so.*.*.????? | cut -f5 -d. || true)
  198. fi
  199. init_vars
  200. CMAKE_DEFS="${COMMON_CMAKE_DEFS} ${CMAKE_DEFS} -DLLAMA_HIPBLAS=on -DCMAKE_C_COMPILER=$ROCM_PATH/llvm/bin/clang -DCMAKE_CXX_COMPILER=$ROCM_PATH/llvm/bin/clang++ -DAMDGPU_TARGETS=$(amdGPUs) -DGPU_TARGETS=$(amdGPUs)"
  201. BUILD_DIR="../build/linux/${ARCH}/rocm${ROCM_VARIANT}"
  202. EXTRA_LIBS="-L${ROCM_PATH}/lib -L/opt/amdgpu/lib/x86_64-linux-gnu/ -Wl,-rpath,\$ORIGIN/../../rocm/ -lhipblas -lrocblas -lamdhip64 -lrocsolver -lamd_comgr -lhsa-runtime64 -lrocsparse -ldrm -ldrm_amdgpu"
  203. build
  204. # Record the ROCM dependencies
  205. rm -f "${BUILD_DIR}/bin/deps.txt"
  206. touch "${BUILD_DIR}/bin/deps.txt"
  207. for dep in $(ldd "${BUILD_DIR}/bin/ollama_llama_server" | grep "=>" | cut -f2 -d= | cut -f2 -d' ' | grep -e rocm -e amdgpu -e libtinfo ); do
  208. echo "${dep}" >> "${BUILD_DIR}/bin/deps.txt"
  209. done
  210. # bomb out if for some reason we didn't get a few deps
  211. if [ $(cat "${BUILD_DIR}/bin/deps.txt" | wc -l ) -lt 8 ] ; then
  212. cat "${BUILD_DIR}/bin/deps.txt"
  213. echo "ERROR: deps file short"
  214. exit 1
  215. fi
  216. compress
  217. fi
  218. cleanup
  219. echo "go generate completed. LLM runners: $(cd ${BUILD_DIR}/..; echo *)"