gen_linux.sh 7.7 KB

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