gen_linux.sh 7.9 KB

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