gen_linux.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. compress_pids=""
  15. # See https://llvm.org/docs/AMDGPUUsage.html#processors for reference
  16. amdGPUs() {
  17. if [ -n "${AMDGPU_TARGETS}" ]; then
  18. echo "${AMDGPU_TARGETS}"
  19. return
  20. fi
  21. GPU_LIST=(
  22. "gfx900"
  23. "gfx906:xnack-"
  24. "gfx908:xnack-"
  25. "gfx90a:xnack+"
  26. "gfx90a:xnack-"
  27. "gfx940"
  28. "gfx941"
  29. "gfx942"
  30. "gfx1010"
  31. "gfx1012"
  32. "gfx1030"
  33. "gfx1100"
  34. "gfx1101"
  35. "gfx1102"
  36. )
  37. (
  38. IFS=$';'
  39. echo "'${GPU_LIST[*]}'"
  40. )
  41. }
  42. echo "Starting linux generate script"
  43. if [ -z "${CUDACXX}" ]; then
  44. if [ -x /usr/local/cuda/bin/nvcc ]; then
  45. export CUDACXX=/usr/local/cuda/bin/nvcc
  46. else
  47. # Try the default location in case it exists
  48. export CUDACXX=$(command -v nvcc)
  49. fi
  50. fi
  51. COMMON_CMAKE_DEFS="-DCMAKE_SKIP_RPATH=on -DBUILD_SHARED_LIBS=on -DCMAKE_POSITION_INDEPENDENT_CODE=on -DGGML_NATIVE=off -DGGML_AVX=on -DGGML_AVX2=off -DGGML_AVX512=off -DGGML_FMA=off -DGGML_F16C=off -DGGML_OPENMP=off"
  52. source $(dirname $0)/gen_common.sh
  53. init_vars
  54. git_module_setup
  55. apply_patches
  56. init_vars
  57. if [ -z "${OLLAMA_SKIP_CPU_GENERATE}" ]; then
  58. # Users building from source can tune the exact flags we pass to cmake for configuring
  59. # llama.cpp, and we'll build only 1 CPU variant in that case as the default.
  60. if [ -n "${OLLAMA_CUSTOM_CPU_DEFS}" ]; then
  61. init_vars
  62. echo "OLLAMA_CUSTOM_CPU_DEFS=\"${OLLAMA_CUSTOM_CPU_DEFS}\""
  63. CMAKE_DEFS="${OLLAMA_CUSTOM_CPU_DEFS} -DBUILD_SHARED_LIBS=on -DCMAKE_POSITION_INDEPENDENT_CODE=on ${CMAKE_DEFS}"
  64. BUILD_DIR="../build/linux/${ARCH}/cpu"
  65. echo "Building custom CPU"
  66. build
  67. install
  68. compress
  69. else
  70. # Darwin Rosetta x86 emulation does NOT support AVX, AVX2, AVX512
  71. # -DGGML_AVX -- 2011 Intel Sandy Bridge & AMD Bulldozer
  72. # -DGGML_F16C -- 2012 Intel Ivy Bridge & AMD 2011 Bulldozer (No significant improvement over just AVX)
  73. # -DGGML_AVX2 -- 2013 Intel Haswell & 2015 AMD Excavator / 2017 AMD Zen
  74. # -DGGML_FMA (FMA3) -- 2013 Intel Haswell & 2012 AMD Piledriver
  75. # Note: the following seem to yield slower results than AVX2 - ymmv
  76. # -DGGML_AVX512 -- 2017 Intel Skylake and High End DeskTop (HEDT)
  77. # -DGGML_AVX512_VBMI -- 2018 Intel Cannon Lake
  78. # -DGGML_AVX512_VNNI -- 2021 Intel Alder Lake
  79. COMMON_CPU_DEFS="-DBUILD_SHARED_LIBS=on -DCMAKE_POSITION_INDEPENDENT_CODE=on -DGGML_NATIVE=off -DGGML_OPENMP=off"
  80. if [ -z "${OLLAMA_CPU_TARGET}" -o "${OLLAMA_CPU_TARGET}" = "cpu" ]; then
  81. #
  82. # CPU first for the default library, set up as lowest common denominator for maximum compatibility (including Rosetta)
  83. #
  84. init_vars
  85. CMAKE_DEFS="${COMMON_CPU_DEFS} -DGGML_AVX=off -DGGML_AVX2=off -DGGML_AVX512=off -DGGML_FMA=off -DGGML_F16C=off ${CMAKE_DEFS}"
  86. BUILD_DIR="../build/linux/${ARCH}/cpu"
  87. echo "Building LCD CPU"
  88. build
  89. install
  90. compress
  91. fi
  92. if [ "${ARCH}" == "x86_64" ]; then
  93. #
  94. # ARM chips in M1/M2/M3-based MACs and NVidia Tegra devices do not currently support avx extensions.
  95. #
  96. if [ -z "${OLLAMA_CPU_TARGET}" -o "${OLLAMA_CPU_TARGET}" = "cpu_avx" ]; then
  97. #
  98. # ~2011 CPU Dynamic library with more capabilities turned on to optimize performance
  99. # Approximately 400% faster than LCD on same CPU
  100. #
  101. init_vars
  102. CMAKE_DEFS="${COMMON_CPU_DEFS} -DGGML_AVX=on -DGGML_AVX2=off -DGGML_AVX512=off -DGGML_FMA=off -DGGML_F16C=off ${CMAKE_DEFS}"
  103. BUILD_DIR="../build/linux/${ARCH}/cpu_avx"
  104. echo "Building AVX CPU"
  105. build
  106. install
  107. compress
  108. fi
  109. if [ -z "${OLLAMA_CPU_TARGET}" -o "${OLLAMA_CPU_TARGET}" = "cpu_avx2" ]; then
  110. #
  111. # ~2013 CPU Dynamic library
  112. # Approximately 10% faster than AVX on same CPU
  113. #
  114. init_vars
  115. CMAKE_DEFS="${COMMON_CPU_DEFS} -DGGML_AVX=on -DGGML_AVX2=on -DGGML_AVX512=off -DGGML_FMA=on -DGGML_F16C=on ${CMAKE_DEFS}"
  116. BUILD_DIR="../build/linux/${ARCH}/cpu_avx2"
  117. echo "Building AVX2 CPU"
  118. build
  119. install
  120. compress
  121. fi
  122. fi
  123. fi
  124. else
  125. echo "Skipping CPU generation step as requested"
  126. fi
  127. # If needed, look for the default CUDA toolkit location
  128. if [ -z "${CUDA_LIB_DIR}" ] && [ -d /usr/local/cuda/lib64 ]; then
  129. CUDA_LIB_DIR=/usr/local/cuda/lib64
  130. fi
  131. # If needed, look for CUDA on Arch Linux
  132. if [ -z "${CUDA_LIB_DIR}" ] && [ -d /opt/cuda/targets/x86_64-linux/lib ]; then
  133. CUDA_LIB_DIR=/opt/cuda/targets/x86_64-linux/lib
  134. fi
  135. # Allow override in case libcudart is in the wrong place
  136. if [ -z "${CUDART_LIB_DIR}" ]; then
  137. CUDART_LIB_DIR="${CUDA_LIB_DIR}"
  138. fi
  139. if [ -z "${OLLAMA_SKIP_CUDA_GENERATE}" -a -d "${CUDA_LIB_DIR}" ]; then
  140. echo "CUDA libraries detected - building dynamic CUDA library"
  141. init_vars
  142. CUDA_MAJOR=$(ls "${CUDA_LIB_DIR}"/libcudart.so.* | head -1 | cut -f3 -d. || true)
  143. if [ -n "${CUDA_MAJOR}" -a -z "${CUDA_VARIANT}" ]; then
  144. CUDA_VARIANT=_v${CUDA_MAJOR}
  145. fi
  146. if [ "${ARCH}" == "arm64" ]; then
  147. echo "ARM CPU detected - disabling unsupported AVX instructions"
  148. # ARM-based CPUs such as M1 and Tegra do not support AVX extensions.
  149. #
  150. # CUDA compute < 6.0 lacks proper FP16 support on ARM.
  151. # Disabling has minimal performance effect while maintaining compatibility.
  152. ARM64_DEFS="-DGGML_AVX=off -DGGML_AVX2=off -DGGML_AVX512=off -DGGML_CUDA_F16=off"
  153. fi
  154. # Users building from source can tune the exact flags we pass to cmake for configuring llama.cpp
  155. if [ -n "${OLLAMA_CUSTOM_CUDA_DEFS}" ]; then
  156. echo "OLLAMA_CUSTOM_CUDA_DEFS=\"${OLLAMA_CUSTOM_CUDA_DEFS}\""
  157. CMAKE_CUDA_DEFS="-DGGML_CUDA=on -DCMAKE_CUDA_ARCHITECTURES=${CMAKE_CUDA_ARCHITECTURES} ${OLLAMA_CUSTOM_CUDA_DEFS}"
  158. echo "Building custom CUDA GPU"
  159. else
  160. CMAKE_CUDA_DEFS="-DGGML_CUDA=on -DCMAKE_CUDA_ARCHITECTURES=${CMAKE_CUDA_ARCHITECTURES}"
  161. fi
  162. export CUDAFLAGS="-t8"
  163. CMAKE_DEFS="${COMMON_CMAKE_DEFS} ${CMAKE_DEFS} ${ARM64_DEFS} ${CMAKE_CUDA_DEFS} -DGGML_STATIC=off"
  164. BUILD_DIR="../build/linux/${ARCH}/cuda${CUDA_VARIANT}"
  165. export LLAMA_SERVER_LDFLAGS="-L${CUDA_LIB_DIR} -lcudart -lcublas -lcublasLt -lcuda"
  166. CUDA_DIST_DIR="${CUDA_DIST_DIR:-${DIST_BASE}/lib/ollama}"
  167. build
  168. install
  169. echo "Installing CUDA dependencies in ${CUDA_DIST_DIR}"
  170. mkdir -p "${CUDA_DIST_DIR}"
  171. for lib in ${CUDA_LIB_DIR}/libcudart.so* ${CUDA_LIB_DIR}/libcublas.so* ${CUDA_LIB_DIR}/libcublasLt.so* ; do
  172. cp -a "${lib}" "${CUDA_DIST_DIR}"
  173. done
  174. compress
  175. fi
  176. if [ -z "${ONEAPI_ROOT}" ]; then
  177. # Try the default location in case it exists
  178. ONEAPI_ROOT=/opt/intel/oneapi
  179. fi
  180. if [ -z "${OLLAMA_SKIP_ONEAPI_GENERATE}" -a -d "${ONEAPI_ROOT}" ]; then
  181. echo "OneAPI libraries detected - building dynamic OneAPI library"
  182. init_vars
  183. source ${ONEAPI_ROOT}/setvars.sh --force # set up environment variables for oneAPI
  184. CC=icx
  185. CMAKE_DEFS="${COMMON_CMAKE_DEFS} ${CMAKE_DEFS} -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx -DGGML_SYCL=ON -DGGML_SYCL_F16=OFF"
  186. BUILD_DIR="../build/linux/${ARCH}/oneapi"
  187. ONEAPI_DIST_DIR="${DIST_BASE}/lib/ollama"
  188. export LLAMA_SERVER_LDFLAGS="-fsycl -lOpenCL -lmkl_core -lmkl_sycl_blas -lmkl_intel_ilp64 -lmkl_tbb_thread -ltbb"
  189. DEBUG_FLAGS="" # icx compiles with -O0 if we pass -g, so we must remove it
  190. build
  191. # copy oneAPI dependencies
  192. mkdir -p "${ONEAPI_DIST_DIR}"
  193. for dep in $(ldd "${BUILD_DIR}/bin/ollama_llama_server" | grep "=>" | cut -f2 -d= | cut -f2 -d' ' | grep -e sycl -e mkl -e tbb); do
  194. cp -a "${dep}" "${ONEAPI_DIST_DIR}"
  195. done
  196. cp "${ONEAPI_ROOT}/compiler/latest/lib/libOpenCL.so" "${ONEAPI_DIST_DIR}"
  197. cp "${ONEAPI_ROOT}/compiler/latest/lib/libimf.so" "${ONEAPI_DIST_DIR}"
  198. cp "${ONEAPI_ROOT}/compiler/latest/lib/libintlc.so.5" "${ONEAPI_DIST_DIR}"
  199. cp "${ONEAPI_ROOT}/compiler/latest/lib/libirng.so" "${ONEAPI_DIST_DIR}"
  200. cp "${ONEAPI_ROOT}/compiler/latest/lib/libpi_level_zero.so" "${ONEAPI_DIST_DIR}"
  201. cp "${ONEAPI_ROOT}/compiler/latest/lib/libsvml.so" "${ONEAPI_DIST_DIR}"
  202. cp "${ONEAPI_ROOT}/compiler/latest/lib/libur_loader.so.0" "${ONEAPI_DIST_DIR}"
  203. install
  204. compress
  205. fi
  206. if [ -z "${ROCM_PATH}" ]; then
  207. # Try the default location in case it exists
  208. ROCM_PATH=/opt/rocm
  209. fi
  210. if [ -z "${CLBlast_DIR}" ]; then
  211. # Try the default location in case it exists
  212. if [ -d /usr/lib/cmake/CLBlast ]; then
  213. export CLBlast_DIR=/usr/lib/cmake/CLBlast
  214. fi
  215. fi
  216. if [ -z "${OLLAMA_SKIP_ROCM_GENERATE}" -a -d "${ROCM_PATH}" ]; then
  217. echo "ROCm libraries detected - building dynamic ROCm library"
  218. if [ -f ${ROCM_PATH}/lib/librocblas.so.*.*.????? ]; then
  219. ROCM_VARIANT=_v$(ls ${ROCM_PATH}/lib/librocblas.so.*.*.????? | cut -f5 -d. || true)
  220. fi
  221. init_vars
  222. CMAKE_DEFS="${COMMON_CMAKE_DEFS} ${CMAKE_DEFS} -DGGML_HIPBLAS=on -DGGML_CUDA_NO_PEER_COPY=on -DCMAKE_C_COMPILER=$ROCM_PATH/llvm/bin/clang -DCMAKE_CXX_COMPILER=$ROCM_PATH/llvm/bin/clang++ -DAMDGPU_TARGETS=$(amdGPUs) -DGPU_TARGETS=$(amdGPUs)"
  223. # Users building from source can tune the exact flags we pass to cmake for configuring llama.cpp
  224. if [ -n "${OLLAMA_CUSTOM_ROCM_DEFS}" ]; then
  225. echo "OLLAMA_CUSTOM_ROCM_DEFS=\"${OLLAMA_CUSTOM_ROCM_DEFS}\""
  226. CMAKE_DEFS="${CMAKE_DEFS} ${OLLAMA_CUSTOM_ROCM_DEFS}"
  227. echo "Building custom ROCM GPU"
  228. fi
  229. BUILD_DIR="../build/linux/${ARCH}/rocm${ROCM_VARIANT}"
  230. # ROCm dependencies are too large to fit into a unified bundle
  231. ROCM_DIST_DIR="${DIST_BASE}/../linux-${GOARCH}-rocm/lib/ollama"
  232. # TODO figure out how to disable runpath (rpath)
  233. # export CMAKE_HIP_FLAGS="-fno-rtlib-add-rpath" # doesn't work
  234. export LLAMA_SERVER_LDFLAGS="-L${ROCM_PATH}/lib -L/opt/amdgpu/lib/x86_64-linux-gnu/ -lhipblas -lrocblas -lamdhip64 -lrocsolver -lamd_comgr -lhsa-runtime64 -lrocsparse -ldrm -ldrm_amdgpu"
  235. build
  236. # copy the ROCM dependencies
  237. mkdir -p "${ROCM_DIST_DIR}"
  238. for dep in $(ldd "${BUILD_DIR}/bin/ollama_llama_server" | grep "=>" | cut -f2 -d= | cut -f2 -d' ' | grep -v "${ARCH}/rocm${ROCM_VARIANT}" | grep -e rocm -e amdgpu -e libtinfo ); do
  239. cp -a "${dep}"* "${ROCM_DIST_DIR}"
  240. done
  241. install
  242. compress
  243. fi
  244. cleanup
  245. wait_for_compress
  246. echo "go generate completed. LLM runners: $(cd ${BUILD_DIR}/..; echo *)"