gen_linux.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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_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 -DGGML_NATIVE=off -DGGML_AVX=off -DGGML_AVX2=off -DGGML_AVX512=off -DGGML_FMA=off -DGGML_F16C=off -DGGML_OPENMP=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} -DBUILD_SHARED_LIBS=off -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. # -DGGML_AVX -- 2011 Intel Sandy Bridge & AMD Bulldozer
  82. # -DGGML_F16C -- 2012 Intel Ivy Bridge & AMD 2011 Bulldozer (No significant improvement over just AVX)
  83. # -DGGML_AVX2 -- 2013 Intel Haswell & 2015 AMD Excavator / 2017 AMD Zen
  84. # -DGGML_FMA (FMA3) -- 2013 Intel Haswell & 2012 AMD Piledriver
  85. # Note: the following seem to yield slower results than AVX2 - ymmv
  86. # -DGGML_AVX512 -- 2017 Intel Skylake and High End DeskTop (HEDT)
  87. # -DGGML_AVX512_VBMI -- 2018 Intel Cannon Lake
  88. # -DGGML_AVX512_VNNI -- 2021 Intel Alder Lake
  89. COMMON_CPU_DEFS="-DBUILD_SHARED_LIBS=off -DCMAKE_POSITION_INDEPENDENT_CODE=on -DGGML_NATIVE=off -DGGML_OPENMP=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} -DGGML_AVX=off -DGGML_AVX2=off -DGGML_AVX512=off -DGGML_FMA=off -DGGML_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} -DGGML_AVX=on -DGGML_AVX2=off -DGGML_AVX512=off -DGGML_FMA=off -DGGML_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} -DGGML_AVX=on -DGGML_AVX2=on -DGGML_AVX512=off -DGGML_FMA=on -DGGML_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 [ -z "${OLLAMA_SKIP_CUDA_GENERATE}" -a -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="-DGGML_AVX=off -DGGML_AVX2=off -DGGML_AVX512=off -DGGML_CUDA_F16=off"
  160. fi
  161. # Users building from source can tune the exact flags we pass to cmake for configuring llama.cpp
  162. if [ -n "${OLLAMA_CUSTOM_CUDA_DEFS}" ]; then
  163. echo "OLLAMA_CUSTOM_CUDA_DEFS=\"${OLLAMA_CUSTOM_CUDA_DEFS}\""
  164. CMAKE_CUDA_DEFS="-DGGML_CUDA=on -DCMAKE_CUDA_ARCHITECTURES=${CMAKE_CUDA_ARCHITECTURES} ${OLLAMA_CUSTOM_CUDA_DEFS}"
  165. echo "Building custom CUDA GPU"
  166. else
  167. CMAKE_CUDA_DEFS="-DGGML_CUDA=on -DCMAKE_CUDA_FLAGS=-t8 -DCMAKE_CUDA_ARCHITECTURES=${CMAKE_CUDA_ARCHITECTURES}"
  168. fi
  169. CMAKE_DEFS="${COMMON_CMAKE_DEFS} ${CMAKE_DEFS} ${ARM64_DEFS} ${CMAKE_CUDA_DEFS}"
  170. BUILD_DIR="../build/linux/${ARCH}/cuda${CUDA_VARIANT}"
  171. EXTRA_LIBS="-L${CUDA_LIB_DIR} -lcudart -lcublas -lcublasLt -lcuda"
  172. build
  173. # Carry the CUDA libs as payloads to help reduce dependency burden on users
  174. #
  175. # TODO - in the future we may shift to packaging these separately and conditionally
  176. # downloading them in the install script.
  177. DEPS="$(ldd ${BUILD_DIR}/bin/ollama_llama_server )"
  178. for lib in libcudart.so libcublas.so libcublasLt.so ; do
  179. DEP=$(echo "${DEPS}" | grep ${lib} | cut -f1 -d' ' | xargs || true)
  180. if [ -n "${DEP}" -a -e "${CUDA_LIB_DIR}/${DEP}" ]; then
  181. cp "${CUDA_LIB_DIR}/${DEP}" "${BUILD_DIR}/bin/"
  182. elif [ -e "${CUDA_LIB_DIR}/${lib}.${CUDA_MAJOR}" ]; then
  183. cp "${CUDA_LIB_DIR}/${lib}.${CUDA_MAJOR}" "${BUILD_DIR}/bin/"
  184. elif [ -e "${CUDART_LIB_DIR}/${lib}" ]; then
  185. cp -d ${CUDART_LIB_DIR}/${lib}* "${BUILD_DIR}/bin/"
  186. else
  187. cp -d "${CUDA_LIB_DIR}/${lib}*" "${BUILD_DIR}/bin/"
  188. fi
  189. done
  190. compress
  191. fi
  192. if [ -z "${ONEAPI_ROOT}" ]; then
  193. # Try the default location in case it exists
  194. ONEAPI_ROOT=/opt/intel/oneapi
  195. fi
  196. if [ -z "${OLLAMA_SKIP_ONEAPI_GENERATE}" -a -d "${ONEAPI_ROOT}" ]; then
  197. echo "OneAPI libraries detected - building dynamic OneAPI library"
  198. init_vars
  199. source ${ONEAPI_ROOT}/setvars.sh --force # set up environment variables for oneAPI
  200. CC=icx
  201. CMAKE_DEFS="${COMMON_CMAKE_DEFS} ${CMAKE_DEFS} -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx -DGGML_SYCL=ON -DGGML_SYCL_F16=OFF"
  202. BUILD_DIR="../build/linux/${ARCH}/oneapi"
  203. 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"
  204. DEBUG_FLAGS="" # icx compiles with -O0 if we pass -g, so we must remove it
  205. build
  206. # copy oneAPI dependencies
  207. 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
  208. cp "${dep}" "${BUILD_DIR}/bin/"
  209. done
  210. cp "${ONEAPI_ROOT}/compiler/latest/lib/libOpenCL.so" "${BUILD_DIR}/bin/"
  211. cp "${ONEAPI_ROOT}/compiler/latest/lib/libimf.so" "${BUILD_DIR}/bin/"
  212. cp "${ONEAPI_ROOT}/compiler/latest/lib/libintlc.so.5" "${BUILD_DIR}/bin/"
  213. cp "${ONEAPI_ROOT}/compiler/latest/lib/libirng.so" "${BUILD_DIR}/bin/"
  214. cp "${ONEAPI_ROOT}/compiler/latest/lib/libpi_level_zero.so" "${BUILD_DIR}/bin/"
  215. cp "${ONEAPI_ROOT}/compiler/latest/lib/libsvml.so" "${BUILD_DIR}/bin/"
  216. cp "${ONEAPI_ROOT}/compiler/latest/lib/libur_loader.so.0" "${BUILD_DIR}/bin/"
  217. compress
  218. fi
  219. if [ -z "${ROCM_PATH}" ]; then
  220. # Try the default location in case it exists
  221. ROCM_PATH=/opt/rocm
  222. fi
  223. if [ -z "${CLBlast_DIR}" ]; then
  224. # Try the default location in case it exists
  225. if [ -d /usr/lib/cmake/CLBlast ]; then
  226. export CLBlast_DIR=/usr/lib/cmake/CLBlast
  227. fi
  228. fi
  229. if [ -z "${OLLAMA_SKIP_ROCM_GENERATE}" -a -d "${ROCM_PATH}" ]; then
  230. echo "ROCm libraries detected - building dynamic ROCm library"
  231. if [ -f ${ROCM_PATH}/lib/librocblas.so.*.*.????? ]; then
  232. ROCM_VARIANT=_v$(ls ${ROCM_PATH}/lib/librocblas.so.*.*.????? | cut -f5 -d. || true)
  233. fi
  234. init_vars
  235. 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)"
  236. # Users building from source can tune the exact flags we pass to cmake for configuring llama.cpp
  237. if [ -n "${OLLAMA_CUSTOM_ROCM_DEFS}" ]; then
  238. echo "OLLAMA_CUSTOM_ROCM_DEFS=\"${OLLAMA_CUSTOM_ROCM_DEFS}\""
  239. CMAKE_DEFS="${CMAKE_DEFS} ${OLLAMA_CUSTOM_ROCM_DEFS}"
  240. echo "Building custom ROCM GPU"
  241. fi
  242. BUILD_DIR="../build/linux/${ARCH}/rocm${ROCM_VARIANT}"
  243. 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"
  244. build
  245. # Record the ROCM dependencies
  246. rm -f "${BUILD_DIR}/bin/deps.txt"
  247. touch "${BUILD_DIR}/bin/deps.txt"
  248. 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
  249. echo "${dep}" >> "${BUILD_DIR}/bin/deps.txt"
  250. done
  251. # bomb out if for some reason we didn't get a few deps
  252. if [ $(cat "${BUILD_DIR}/bin/deps.txt" | wc -l ) -lt 8 ] ; then
  253. cat "${BUILD_DIR}/bin/deps.txt"
  254. echo "ERROR: deps file short"
  255. exit 1
  256. fi
  257. compress
  258. fi
  259. cleanup
  260. echo "go generate completed. LLM runners: $(cd ${BUILD_DIR}/..; echo *)"