gen_linux.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 our default built-in library which will be linked into the CGO
  5. # binary as a normal dependency. This default build is CPU based.
  6. #
  7. # Then we build a CUDA dynamic library (although statically linked with the CUDA
  8. # library dependencies for maximum portability)
  9. #
  10. # Then if we detect ROCm, we build a dynamically loaded ROCm lib. ROCm is particularly
  11. # important to be a dynamic lib even if it's the only GPU library detected because
  12. # we can't redistribute the objectfiles but must rely on dynamic libraries at
  13. # runtime, which could lead the server not to start if not present.
  14. set -ex
  15. set -o pipefail
  16. # See https://llvm.org/docs/AMDGPUUsage.html#processors for reference
  17. amdGPUs() {
  18. GPU_LIST=(
  19. "gfx803"
  20. "gfx900"
  21. "gfx906:xnack-"
  22. "gfx908:xnack-"
  23. "gfx90a:xnack+"
  24. "gfx90a:xnack-"
  25. "gfx1010"
  26. "gfx1012"
  27. "gfx1030"
  28. "gfx1100"
  29. "gfx1101"
  30. "gfx1102"
  31. )
  32. (
  33. IFS=$';'
  34. echo "'${GPU_LIST[*]}'"
  35. )
  36. }
  37. echo "Starting linux generate script"
  38. if [ -z "${CUDACXX}" -a -x /usr/local/cuda/bin/nvcc ]; then
  39. export CUDACXX=/usr/local/cuda/bin/nvcc
  40. fi
  41. 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"
  42. source $(dirname $0)/gen_common.sh
  43. init_vars
  44. git_module_setup
  45. apply_patches
  46. if [ -z "${OLLAMA_SKIP_CPU_GENERATE}" ]; then
  47. # Users building from source can tune the exact flags we pass to cmake for configuring
  48. # llama.cpp, and we'll build only 1 CPU variant in that case as the default.
  49. if [ -n "${OLLAMA_CUSTOM_CPU_DEFS}" ]; then
  50. echo "OLLAMA_CUSTOM_CPU_DEFS=\"${OLLAMA_CUSTOM_CPU_DEFS}\""
  51. CMAKE_DEFS="${OLLAMA_CUSTOM_CPU_DEFS} -DCMAKE_POSITION_INDEPENDENT_CODE=on ${CMAKE_DEFS}"
  52. BUILD_DIR="${LLAMACPP_DIR}/build/linux/cpu"
  53. echo "Building custom CPU"
  54. build
  55. install
  56. link_server_lib
  57. else
  58. # Darwin Rosetta x86 emulation does NOT support AVX, AVX2, AVX512
  59. # -DLLAMA_AVX -- 2011 Intel Sandy Bridge & AMD Bulldozer
  60. # -DLLAMA_F16C -- 2012 Intel Ivy Bridge & AMD 2011 Bulldozer (No significant improvement over just AVX)
  61. # -DLLAMA_AVX2 -- 2013 Intel Haswell & 2015 AMD Excavator / 2017 AMD Zen
  62. # -DLLAMA_FMA (FMA3) -- 2013 Intel Haswell & 2012 AMD Piledriver
  63. # Note: the following seem to yield slower results than AVX2 - ymmv
  64. # -DLLAMA_AVX512 -- 2017 Intel Skylake and High End DeskTop (HEDT)
  65. # -DLLAMA_AVX512_VBMI -- 2018 Intel Cannon Lake
  66. # -DLLAMA_AVX512_VNNI -- 2021 Intel Alder Lake
  67. COMMON_CPU_DEFS="-DCMAKE_POSITION_INDEPENDENT_CODE=on -DLLAMA_NATIVE=off"
  68. #
  69. # CPU first for the default library, set up as lowest common denominator for maximum compatibility (including Rosetta)
  70. #
  71. CMAKE_DEFS="${COMMON_CPU_DEFS} -DLLAMA_AVX=off -DLLAMA_AVX2=off -DLLAMA_AVX512=off -DLLAMA_FMA=off -DLLAMA_F16C=off ${CMAKE_DEFS}"
  72. BUILD_DIR="${LLAMACPP_DIR}/build/linux/cpu"
  73. echo "Building LCD CPU"
  74. build
  75. install
  76. link_server_lib
  77. #
  78. # ~2011 CPU Dynamic library with more capabilities turned on to optimize performance
  79. # Approximately 400% faster than LCD on same CPU
  80. #
  81. init_vars
  82. CMAKE_DEFS="${COMMON_CPU_DEFS} -DLLAMA_AVX=on -DLLAMA_AVX2=off -DLLAMA_AVX512=off -DLLAMA_FMA=off -DLLAMA_F16C=off ${CMAKE_DEFS}"
  83. BUILD_DIR="${LLAMACPP_DIR}/build/linux/cpu_avx"
  84. echo "Building AVX CPU"
  85. build
  86. install
  87. link_server_lib
  88. #
  89. # ~2013 CPU Dynamic library
  90. # Approximately 10% faster than AVX on same CPU
  91. #
  92. init_vars
  93. CMAKE_DEFS="${COMMON_CPU_DEFS} -DLLAMA_AVX=on -DLLAMA_AVX2=on -DLLAMA_AVX512=off -DLLAMA_FMA=on -DLLAMA_F16C=on ${CMAKE_DEFS}"
  94. BUILD_DIR="${LLAMACPP_DIR}/build/linux/cpu_avx2"
  95. echo "Building AVX2 CPU"
  96. build
  97. install
  98. link_server_lib
  99. fi
  100. else
  101. echo "Skipping CPU generation step as requested"
  102. fi
  103. if [ -d /usr/local/cuda/lib64/ ]; then
  104. echo "CUDA libraries detected - building dynamic CUDA library"
  105. init_vars
  106. CUDA_MAJOR=$(ls /usr/local/cuda/lib64/libcudart.so.* | head -1 | cut -f3 -d. || true)
  107. if [ -n "${CUDA_MAJOR}" ]; then
  108. CUDA_VARIANT=_v${CUDA_MAJOR}
  109. fi
  110. CMAKE_DEFS="-DLLAMA_CUBLAS=on ${COMMON_CMAKE_DEFS} ${CMAKE_DEFS}"
  111. BUILD_DIR="${LLAMACPP_DIR}/build/linux/cuda${CUDA_VARIANT}"
  112. CUDA_LIB_DIR=/usr/local/cuda/lib64
  113. build
  114. install
  115. gcc -fPIC -g -shared -o ${BUILD_DIR}/lib/libext_server.so \
  116. -Wl,--whole-archive \
  117. ${BUILD_DIR}/lib/libext_server.a \
  118. ${BUILD_DIR}/lib/libcommon.a \
  119. ${BUILD_DIR}/lib/libllama.a \
  120. -Wl,--no-whole-archive \
  121. ${CUDA_LIB_DIR}/libcudart_static.a \
  122. ${CUDA_LIB_DIR}/libcublas_static.a \
  123. ${CUDA_LIB_DIR}/libcublasLt_static.a \
  124. ${CUDA_LIB_DIR}/libcudadevrt.a \
  125. ${CUDA_LIB_DIR}/libculibos.a \
  126. -lrt -lpthread -ldl -lstdc++ -lm
  127. fi
  128. if [ -z "${ROCM_PATH}" ]; then
  129. # Try the default location in case it exists
  130. ROCM_PATH=/opt/rocm
  131. fi
  132. if [ -z "${CLBlast_DIR}" ]; then
  133. # Try the default location in case it exists
  134. if [ -d /usr/lib/cmake/CLBlast ]; then
  135. export CLBlast_DIR=/usr/lib/cmake/CLBlast
  136. fi
  137. fi
  138. if [ -d "${ROCM_PATH}" ]; then
  139. echo "ROCm libraries detected - building dynamic ROCm library"
  140. if [ -f ${ROCM_PATH}/lib/librocm_smi64.so.? ]; then
  141. ROCM_VARIANT=_v$(ls ${ROCM_PATH}/lib/librocm_smi64.so.? | cut -f3 -d. || true)
  142. fi
  143. init_vars
  144. 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)"
  145. BUILD_DIR="${LLAMACPP_DIR}/build/linux/rocm${ROCM_VARIANT}"
  146. build
  147. install
  148. gcc -fPIC -g -shared -o ${BUILD_DIR}/lib/libext_server.so \
  149. -Wl,--whole-archive \
  150. ${BUILD_DIR}/lib/libext_server.a \
  151. ${BUILD_DIR}/lib/libcommon.a \
  152. ${BUILD_DIR}/lib/libllama.a \
  153. -Wl,--no-whole-archive \
  154. -lrt -lpthread -ldl -lstdc++ -lm \
  155. -L/opt/rocm/lib -L/opt/amdgpu/lib/x86_64-linux-gnu/ \
  156. -Wl,-rpath,/opt/rocm/lib,-rpath,/opt/amdgpu/lib/x86_64-linux-gnu/ \
  157. -lhipblas -lrocblas -lamdhip64 -lrocsolver -lamd_comgr -lhsa-runtime64 -lrocsparse -ldrm -ldrm_amdgpu
  158. fi
  159. cleanup