gen_linux.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. if [ -z "${OLLAMA_SKIP_CPU_GENERATE}" ]; then
  56. # Users building from source can tune the exact flags we pass to cmake for configuring
  57. # llama.cpp, and we'll build only 1 CPU variant in that case as the default.
  58. if [ -n "${OLLAMA_CUSTOM_CPU_DEFS}" ]; then
  59. echo "OLLAMA_CUSTOM_CPU_DEFS=\"${OLLAMA_CUSTOM_CPU_DEFS}\""
  60. CMAKE_DEFS="${OLLAMA_CUSTOM_CPU_DEFS} -DCMAKE_POSITION_INDEPENDENT_CODE=on ${CMAKE_DEFS}"
  61. BUILD_DIR="${LLAMACPP_DIR}/build/linux/${ARCH}/cpu"
  62. echo "Building custom CPU"
  63. build
  64. compress_libs
  65. else
  66. # Darwin Rosetta x86 emulation does NOT support AVX, AVX2, AVX512
  67. # -DLLAMA_AVX -- 2011 Intel Sandy Bridge & AMD Bulldozer
  68. # -DLLAMA_F16C -- 2012 Intel Ivy Bridge & AMD 2011 Bulldozer (No significant improvement over just AVX)
  69. # -DLLAMA_AVX2 -- 2013 Intel Haswell & 2015 AMD Excavator / 2017 AMD Zen
  70. # -DLLAMA_FMA (FMA3) -- 2013 Intel Haswell & 2012 AMD Piledriver
  71. # Note: the following seem to yield slower results than AVX2 - ymmv
  72. # -DLLAMA_AVX512 -- 2017 Intel Skylake and High End DeskTop (HEDT)
  73. # -DLLAMA_AVX512_VBMI -- 2018 Intel Cannon Lake
  74. # -DLLAMA_AVX512_VNNI -- 2021 Intel Alder Lake
  75. COMMON_CPU_DEFS="-DCMAKE_POSITION_INDEPENDENT_CODE=on -DLLAMA_NATIVE=off"
  76. if [ -z "${OLLAMA_CPU_TARGET}" -o "${OLLAMA_CPU_TARGET}" = "cpu" ]; then
  77. #
  78. # CPU first for the default library, set up as lowest common denominator for maximum compatibility (including Rosetta)
  79. #
  80. CMAKE_DEFS="${COMMON_CPU_DEFS} -DLLAMA_AVX=off -DLLAMA_AVX2=off -DLLAMA_AVX512=off -DLLAMA_FMA=off -DLLAMA_F16C=off ${CMAKE_DEFS}"
  81. BUILD_DIR="${LLAMACPP_DIR}/build/linux/${ARCH}/cpu"
  82. echo "Building LCD CPU"
  83. build
  84. compress_libs
  85. fi
  86. if [ -z "${OLLAMA_CPU_TARGET}" -o "${OLLAMA_CPU_TARGET}" = "cpu_avx" ]; then
  87. #
  88. # ~2011 CPU Dynamic library with more capabilities turned on to optimize performance
  89. # Approximately 400% faster than LCD on same CPU
  90. #
  91. init_vars
  92. CMAKE_DEFS="${COMMON_CPU_DEFS} -DLLAMA_AVX=on -DLLAMA_AVX2=off -DLLAMA_AVX512=off -DLLAMA_FMA=off -DLLAMA_F16C=off ${CMAKE_DEFS}"
  93. BUILD_DIR="${LLAMACPP_DIR}/build/linux/${ARCH}/cpu_avx"
  94. echo "Building AVX CPU"
  95. build
  96. compress_libs
  97. fi
  98. if [ -z "${OLLAMA_CPU_TARGET}" -o "${OLLAMA_CPU_TARGET}" = "cpu_avx2" ]; then
  99. #
  100. # ~2013 CPU Dynamic library
  101. # Approximately 10% faster than AVX on same CPU
  102. #
  103. init_vars
  104. CMAKE_DEFS="${COMMON_CPU_DEFS} -DLLAMA_AVX=on -DLLAMA_AVX2=on -DLLAMA_AVX512=off -DLLAMA_FMA=on -DLLAMA_F16C=on ${CMAKE_DEFS}"
  105. BUILD_DIR="${LLAMACPP_DIR}/build/linux/${ARCH}/cpu_avx2"
  106. echo "Building AVX2 CPU"
  107. build
  108. compress_libs
  109. fi
  110. fi
  111. else
  112. echo "Skipping CPU generation step as requested"
  113. fi
  114. # If needed, look for the default CUDA toolkit location
  115. if [ -z "${CUDA_LIB_DIR}" ] && [ -d /usr/local/cuda/lib64 ]; then
  116. CUDA_LIB_DIR=/usr/local/cuda/lib64
  117. fi
  118. # If needed, look for CUDA on Arch Linux
  119. if [ -z "${CUDA_LIB_DIR}" ] && [ -d /opt/cuda/targets/x86_64-linux/lib ]; then
  120. CUDA_LIB_DIR=/opt/cuda/targets/x86_64-linux/lib
  121. fi
  122. # Allow override in case libcudart is in the wrong place
  123. if [ -z "${CUDART_LIB_DIR}" ]; then
  124. CUDART_LIB_DIR="${CUDA_LIB_DIR}"
  125. fi
  126. if [ -d "${CUDA_LIB_DIR}" ]; then
  127. echo "CUDA libraries detected - building dynamic CUDA library"
  128. init_vars
  129. CUDA_MAJOR=$(ls "${CUDA_LIB_DIR}"/libcudart.so.* | head -1 | cut -f3 -d. || true)
  130. if [ -n "${CUDA_MAJOR}" ]; then
  131. CUDA_VARIANT=_v${CUDA_MAJOR}
  132. fi
  133. CMAKE_DEFS="-DLLAMA_CUBLAS=on -DLLAMA_CUDA_FORCE_MMQ=on -DCMAKE_CUDA_ARCHITECTURES=${CMAKE_CUDA_ARCHITECTURES} ${COMMON_CMAKE_DEFS} ${CMAKE_DEFS}"
  134. BUILD_DIR="${LLAMACPP_DIR}/build/linux/${ARCH}/cuda${CUDA_VARIANT}"
  135. EXTRA_LIBS="-L${CUDA_LIB_DIR} -lcudart -lcublas -lcublasLt -lcuda"
  136. build
  137. # Cary the CUDA libs as payloads to help reduce dependency burden on users
  138. #
  139. # TODO - in the future we may shift to packaging these separately and conditionally
  140. # downloading them in the install script.
  141. DEPS="$(ldd ${BUILD_DIR}/lib/libext_server.so )"
  142. for lib in libcudart.so libcublas.so libcublasLt.so ; do
  143. DEP=$(echo "${DEPS}" | grep ${lib} | cut -f1 -d' ' | xargs || true)
  144. if [ -n "${DEP}" -a -e "${CUDA_LIB_DIR}/${DEP}" ]; then
  145. cp "${CUDA_LIB_DIR}/${DEP}" "${BUILD_DIR}/lib/"
  146. elif [ -e "${CUDA_LIB_DIR}/${lib}.${CUDA_MAJOR}" ]; then
  147. cp "${CUDA_LIB_DIR}/${lib}.${CUDA_MAJOR}" "${BUILD_DIR}/lib/"
  148. elif [ -e "${CUDART_LIB_DIR}/${lib}" ]; then
  149. cp -d ${CUDART_LIB_DIR}/${lib}* "${BUILD_DIR}/lib/"
  150. else
  151. cp -d "${CUDA_LIB_DIR}/${lib}*" "${BUILD_DIR}/lib/"
  152. fi
  153. done
  154. compress_libs
  155. fi
  156. if [ -z "${ROCM_PATH}" ]; then
  157. # Try the default location in case it exists
  158. ROCM_PATH=/opt/rocm
  159. fi
  160. if [ -z "${CLBlast_DIR}" ]; then
  161. # Try the default location in case it exists
  162. if [ -d /usr/lib/cmake/CLBlast ]; then
  163. export CLBlast_DIR=/usr/lib/cmake/CLBlast
  164. fi
  165. fi
  166. if [ -d "${ROCM_PATH}" ]; then
  167. echo "ROCm libraries detected - building dynamic ROCm library"
  168. if [ -f ${ROCM_PATH}/lib/librocblas.so.*.*.????? ]; then
  169. ROCM_VARIANT=_v$(ls ${ROCM_PATH}/lib/librocblas.so.*.*.????? | cut -f5 -d. || true)
  170. fi
  171. init_vars
  172. 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)"
  173. BUILD_DIR="${LLAMACPP_DIR}/build/linux/${ARCH}/rocm${ROCM_VARIANT}"
  174. 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"
  175. build
  176. # Record the ROCM dependencies
  177. rm -f "${BUILD_DIR}/lib/deps.txt"
  178. touch "${BUILD_DIR}/lib/deps.txt"
  179. for dep in $(ldd "${BUILD_DIR}/lib/libext_server.so" | grep "=>" | cut -f2 -d= | cut -f2 -d' ' | grep -e rocm -e amdgpu -e libtinfo ); do
  180. echo "${dep}" >> "${BUILD_DIR}/lib/deps.txt"
  181. done
  182. # bomb out if for some reason we didn't get a few deps
  183. if [ $(cat "${BUILD_DIR}/lib/deps.txt" | wc -l ) -lt 8 ] ; then
  184. cat "${BUILD_DIR}/lib/deps.txt"
  185. echo "ERROR: deps file short"
  186. exit 1
  187. fi
  188. compress_libs
  189. fi
  190. cleanup