gen_linux.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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="-DBUILD_SHARED_LIBS=off -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"
  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_CPU_GENERATE}" ]; then
  57. # Users building from source can tune the exact flags we pass to cmake for configuring
  58. # llama.cpp, and we'll build only 1 CPU variant in that case as the default.
  59. if [ -n "${OLLAMA_CUSTOM_CPU_DEFS}" ]; then
  60. init_vars
  61. echo "OLLAMA_CUSTOM_CPU_DEFS=\"${OLLAMA_CUSTOM_CPU_DEFS}\""
  62. CMAKE_DEFS="${OLLAMA_CUSTOM_CPU_DEFS} -DBUILD_SHARED_LIBS=off -DCMAKE_POSITION_INDEPENDENT_CODE=on ${CMAKE_DEFS}"
  63. BUILD_DIR="../build/linux/${ARCH}/cpu"
  64. echo "Building custom CPU"
  65. build
  66. compress
  67. else
  68. # Darwin Rosetta x86 emulation does NOT support AVX, AVX2, AVX512
  69. # -DGGML_AVX -- 2011 Intel Sandy Bridge & AMD Bulldozer
  70. # -DGGML_F16C -- 2012 Intel Ivy Bridge & AMD 2011 Bulldozer (No significant improvement over just AVX)
  71. # -DGGML_AVX2 -- 2013 Intel Haswell & 2015 AMD Excavator / 2017 AMD Zen
  72. # -DGGML_FMA (FMA3) -- 2013 Intel Haswell & 2012 AMD Piledriver
  73. # Note: the following seem to yield slower results than AVX2 - ymmv
  74. # -DGGML_AVX512 -- 2017 Intel Skylake and High End DeskTop (HEDT)
  75. # -DGGML_AVX512_VBMI -- 2018 Intel Cannon Lake
  76. # -DGGML_AVX512_VNNI -- 2021 Intel Alder Lake
  77. COMMON_CPU_DEFS="-DBUILD_SHARED_LIBS=off -DCMAKE_POSITION_INDEPENDENT_CODE=on -DGGML_NATIVE=off -DGGML_OPENMP=off"
  78. if [ -z "${OLLAMA_CPU_TARGET}" -o "${OLLAMA_CPU_TARGET}" = "cpu" ]; then
  79. #
  80. # CPU first for the default library, set up as lowest common denominator for maximum compatibility (including Rosetta)
  81. #
  82. init_vars
  83. CMAKE_DEFS="${COMMON_CPU_DEFS} -DGGML_AVX=off -DGGML_AVX2=off -DGGML_AVX512=off -DGGML_FMA=off -DGGML_F16C=off ${CMAKE_DEFS}"
  84. BUILD_DIR="../build/linux/${ARCH}/cpu"
  85. echo "Building LCD CPU"
  86. build
  87. compress
  88. fi
  89. if [ "${ARCH}" == "x86_64" ]; then
  90. #
  91. # ARM chips in M1/M2/M3-based MACs and NVidia Tegra devices do not currently support avx extensions.
  92. #
  93. if [ -z "${OLLAMA_CPU_TARGET}" -o "${OLLAMA_CPU_TARGET}" = "cpu_avx" ]; then
  94. #
  95. # ~2011 CPU Dynamic library with more capabilities turned on to optimize performance
  96. # Approximately 400% faster than LCD on same CPU
  97. #
  98. init_vars
  99. CMAKE_DEFS="${COMMON_CPU_DEFS} -DGGML_AVX=on -DGGML_AVX2=off -DGGML_AVX512=off -DGGML_FMA=off -DGGML_F16C=off ${CMAKE_DEFS}"
  100. BUILD_DIR="../build/linux/${ARCH}/cpu_avx"
  101. echo "Building AVX CPU"
  102. build
  103. compress
  104. fi
  105. if [ -z "${OLLAMA_CPU_TARGET}" -o "${OLLAMA_CPU_TARGET}" = "cpu_avx2" ]; then
  106. #
  107. # ~2013 CPU Dynamic library
  108. # Approximately 10% faster than AVX on same CPU
  109. #
  110. init_vars
  111. CMAKE_DEFS="${COMMON_CPU_DEFS} -DGGML_AVX=on -DGGML_AVX2=on -DGGML_AVX512=off -DGGML_FMA=on -DGGML_F16C=on ${CMAKE_DEFS}"
  112. BUILD_DIR="../build/linux/${ARCH}/cpu_avx2"
  113. echo "Building AVX2 CPU"
  114. build
  115. compress
  116. fi
  117. fi
  118. fi
  119. else
  120. echo "Skipping CPU generation step as requested"
  121. fi
  122. # If needed, look for the default CUDA toolkit location
  123. if [ -z "${CUDA_LIB_DIR}" ] && [ -d /usr/local/cuda/lib64 ]; then
  124. CUDA_LIB_DIR=/usr/local/cuda/lib64
  125. fi
  126. # If needed, look for CUDA on Arch Linux
  127. if [ -z "${CUDA_LIB_DIR}" ] && [ -d /opt/cuda/targets/x86_64-linux/lib ]; then
  128. CUDA_LIB_DIR=/opt/cuda/targets/x86_64-linux/lib
  129. fi
  130. # Allow override in case libcudart is in the wrong place
  131. if [ -z "${CUDART_LIB_DIR}" ]; then
  132. CUDART_LIB_DIR="${CUDA_LIB_DIR}"
  133. fi
  134. if [ -z "${OLLAMA_SKIP_CUDA_GENERATE}" -a -d "${CUDA_LIB_DIR}" ]; then
  135. echo "CUDA libraries detected - building dynamic CUDA library"
  136. init_vars
  137. CUDA_MAJOR=$(ls "${CUDA_LIB_DIR}"/libcudart.so.* | head -1 | cut -f3 -d. || true)
  138. if [ -n "${CUDA_MAJOR}" ]; then
  139. CUDA_VARIANT=_v${CUDA_MAJOR}
  140. fi
  141. if [ "${ARCH}" == "arm64" ]; then
  142. echo "ARM CPU detected - disabling unsupported AVX instructions"
  143. # ARM-based CPUs such as M1 and Tegra do not support AVX extensions.
  144. #
  145. # CUDA compute < 6.0 lacks proper FP16 support on ARM.
  146. # Disabling has minimal performance effect while maintaining compatibility.
  147. ARM64_DEFS="-DGGML_AVX=off -DGGML_AVX2=off -DGGML_AVX512=off -DGGML_CUDA_F16=off"
  148. fi
  149. # Users building from source can tune the exact flags we pass to cmake for configuring llama.cpp
  150. if [ -n "${OLLAMA_CUSTOM_CUDA_DEFS}" ]; then
  151. echo "OLLAMA_CUSTOM_CUDA_DEFS=\"${OLLAMA_CUSTOM_CUDA_DEFS}\""
  152. CMAKE_CUDA_DEFS="-DGGML_CUDA=on -DCMAKE_CUDA_ARCHITECTURES=${CMAKE_CUDA_ARCHITECTURES} ${OLLAMA_CUSTOM_CUDA_DEFS}"
  153. echo "Building custom CUDA GPU"
  154. else
  155. CMAKE_CUDA_DEFS="-DGGML_CUDA=on -DCMAKE_CUDA_FLAGS=-t8 -DCMAKE_CUDA_ARCHITECTURES=${CMAKE_CUDA_ARCHITECTURES}"
  156. fi
  157. CMAKE_DEFS="${COMMON_CMAKE_DEFS} ${CMAKE_DEFS} ${ARM64_DEFS} ${CMAKE_CUDA_DEFS}"
  158. BUILD_DIR="../build/linux/${ARCH}/cuda${CUDA_VARIANT}"
  159. EXTRA_LIBS="-L${CUDA_LIB_DIR} -lcudart -lcublas -lcublasLt -lcuda"
  160. build
  161. # Carry the CUDA libs as payloads to help reduce dependency burden on users
  162. #
  163. # TODO - in the future we may shift to packaging these separately and conditionally
  164. # downloading them in the install script.
  165. DEPS="$(ldd ${BUILD_DIR}/bin/ollama_llama_server )"
  166. for lib in libcudart.so libcublas.so libcublasLt.so ; do
  167. DEP=$(echo "${DEPS}" | grep ${lib} | cut -f1 -d' ' | xargs || true)
  168. if [ -n "${DEP}" -a -e "${CUDA_LIB_DIR}/${DEP}" ]; then
  169. cp "${CUDA_LIB_DIR}/${DEP}" "${BUILD_DIR}/bin/"
  170. elif [ -e "${CUDA_LIB_DIR}/${lib}.${CUDA_MAJOR}" ]; then
  171. cp "${CUDA_LIB_DIR}/${lib}.${CUDA_MAJOR}" "${BUILD_DIR}/bin/"
  172. elif [ -e "${CUDART_LIB_DIR}/${lib}" ]; then
  173. cp -d ${CUDART_LIB_DIR}/${lib}* "${BUILD_DIR}/bin/"
  174. else
  175. cp -d "${CUDA_LIB_DIR}/${lib}*" "${BUILD_DIR}/bin/"
  176. fi
  177. done
  178. compress
  179. fi
  180. if [ -z "${ONEAPI_ROOT}" ]; then
  181. # Try the default location in case it exists
  182. ONEAPI_ROOT=/opt/intel/oneapi
  183. fi
  184. if [ -z "${OLLAMA_SKIP_ONEAPI_GENERATE}" -a -d "${ONEAPI_ROOT}" ]; then
  185. echo "OneAPI libraries detected - building dynamic OneAPI library"
  186. init_vars
  187. source ${ONEAPI_ROOT}/setvars.sh --force # set up environment variables for oneAPI
  188. CC=icx
  189. CMAKE_DEFS="${COMMON_CMAKE_DEFS} ${CMAKE_DEFS} -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx -DGGML_SYCL=ON -DGGML_SYCL_F16=OFF"
  190. BUILD_DIR="../build/linux/${ARCH}/oneapi"
  191. EXTRA_LIBS="-fsycl -Wl,-rpath,${ONEAPI_ROOT}/compiler/latest/lib,-rpath,${ONEAPI_ROOT}/mkl/latest/lib,-rpath,${ONEAPI_ROOT}/tbb/latest/lib,-rpath,${ONEAPI_ROOT}/compiler/latest/opt/oclfpga/linux64/lib -lOpenCL -lmkl_core -lmkl_sycl_blas -lmkl_intel_ilp64 -lmkl_tbb_thread -ltbb"
  192. DEBUG_FLAGS="" # icx compiles with -O0 if we pass -g, so we must remove it
  193. build
  194. # copy oneAPI dependencies
  195. 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
  196. cp "${dep}" "${BUILD_DIR}/bin/"
  197. done
  198. cp "${ONEAPI_ROOT}/compiler/latest/lib/libOpenCL.so" "${BUILD_DIR}/bin/"
  199. cp "${ONEAPI_ROOT}/compiler/latest/lib/libimf.so" "${BUILD_DIR}/bin/"
  200. cp "${ONEAPI_ROOT}/compiler/latest/lib/libintlc.so.5" "${BUILD_DIR}/bin/"
  201. cp "${ONEAPI_ROOT}/compiler/latest/lib/libirng.so" "${BUILD_DIR}/bin/"
  202. cp "${ONEAPI_ROOT}/compiler/latest/lib/libpi_level_zero.so" "${BUILD_DIR}/bin/"
  203. cp "${ONEAPI_ROOT}/compiler/latest/lib/libsvml.so" "${BUILD_DIR}/bin/"
  204. cp "${ONEAPI_ROOT}/compiler/latest/lib/libur_loader.so.0" "${BUILD_DIR}/bin/"
  205. compress
  206. fi
  207. if [ -z "${ROCM_PATH}" ]; then
  208. # Try the default location in case it exists
  209. ROCM_PATH=/opt/rocm
  210. fi
  211. if [ -z "${CLBlast_DIR}" ]; then
  212. # Try the default location in case it exists
  213. if [ -d /usr/lib/cmake/CLBlast ]; then
  214. export CLBlast_DIR=/usr/lib/cmake/CLBlast
  215. fi
  216. fi
  217. if [ -z "${OLLAMA_SKIP_ROCM_GENERATE}" -a -d "${ROCM_PATH}" ]; then
  218. echo "ROCm libraries detected - building dynamic ROCm library"
  219. if [ -f ${ROCM_PATH}/lib/librocblas.so.*.*.????? ]; then
  220. ROCM_VARIANT=_v$(ls ${ROCM_PATH}/lib/librocblas.so.*.*.????? | cut -f5 -d. || true)
  221. fi
  222. init_vars
  223. CMAKE_DEFS="${COMMON_CMAKE_DEFS} ${CMAKE_DEFS} -DGGML_HIPBLAS=on -DLLAMA_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)"
  224. # Users building from source can tune the exact flags we pass to cmake for configuring llama.cpp
  225. if [ -n "${OLLAMA_CUSTOM_ROCM_DEFS}" ]; then
  226. echo "OLLAMA_CUSTOM_ROCM_DEFS=\"${OLLAMA_CUSTOM_ROCM_DEFS}\""
  227. CMAKE_DEFS="${CMAKE_DEFS} ${OLLAMA_CUSTOM_ROCM_DEFS}"
  228. echo "Building custom ROCM GPU"
  229. fi
  230. BUILD_DIR="../build/linux/${ARCH}/rocm${ROCM_VARIANT}"
  231. 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"
  232. build
  233. # Record the ROCM dependencies
  234. rm -f "${BUILD_DIR}/bin/deps.txt"
  235. touch "${BUILD_DIR}/bin/deps.txt"
  236. 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
  237. echo "${dep}" >> "${BUILD_DIR}/bin/deps.txt"
  238. done
  239. # bomb out if for some reason we didn't get a few deps
  240. if [ $(cat "${BUILD_DIR}/bin/deps.txt" | wc -l ) -lt 8 ] ; then
  241. cat "${BUILD_DIR}/bin/deps.txt"
  242. echo "ERROR: deps file short"
  243. exit 1
  244. fi
  245. compress
  246. fi
  247. cleanup
  248. echo "go generate completed. LLM runners: $(cd ${BUILD_DIR}/..; echo *)"