gen_linux.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #
  48. # CPU first for the default library
  49. #
  50. CMAKE_DEFS="${COMMON_CMAKE_DEFS} ${CMAKE_DEFS}"
  51. BUILD_DIR="${LLAMACPP_DIR}/build/linux/cpu"
  52. build
  53. install
  54. # Placeholder to keep go embed happy until we start building dynamic CPU lib variants
  55. touch ${BUILD_DIR}/lib/dummy.so
  56. else
  57. echo "Skipping CPU generation step as requested"
  58. fi
  59. if [ -d /usr/local/cuda/lib64/ ]; then
  60. echo "CUDA libraries detected - building dynamic CUDA library"
  61. init_vars
  62. CUDA_MAJOR=$(ls /usr/local/cuda/lib64/libcudart.so.* | head -1 | cut -f3 -d. || true)
  63. if [ -n "${CUDA_MAJOR}" ]; then
  64. CUDA_VARIANT=_v${CUDA_MAJOR}
  65. fi
  66. CMAKE_DEFS="-DLLAMA_CUBLAS=on ${COMMON_CMAKE_DEFS} ${CMAKE_DEFS}"
  67. BUILD_DIR="${LLAMACPP_DIR}/build/linux/cuda${CUDA_VARIANT}"
  68. CUDA_LIB_DIR=/usr/local/cuda/lib64
  69. build
  70. install
  71. gcc -fPIC -g -shared -o ${BUILD_DIR}/lib/libext_server.so \
  72. -Wl,--whole-archive \
  73. ${BUILD_DIR}/lib/libext_server.a \
  74. ${BUILD_DIR}/lib/libcommon.a \
  75. ${BUILD_DIR}/lib/libllama.a \
  76. -Wl,--no-whole-archive \
  77. ${CUDA_LIB_DIR}/libcudart_static.a \
  78. ${CUDA_LIB_DIR}/libcublas_static.a \
  79. ${CUDA_LIB_DIR}/libcublasLt_static.a \
  80. ${CUDA_LIB_DIR}/libcudadevrt.a \
  81. ${CUDA_LIB_DIR}/libculibos.a \
  82. -lrt -lpthread -ldl -lstdc++ -lm
  83. fi
  84. if [ -z "${ROCM_PATH}" ]; then
  85. # Try the default location in case it exists
  86. ROCM_PATH=/opt/rocm
  87. fi
  88. if [ -z "${CLBlast_DIR}" ]; then
  89. # Try the default location in case it exists
  90. if [ -d /usr/lib/cmake/CLBlast ]; then
  91. export CLBlast_DIR=/usr/lib/cmake/CLBlast
  92. fi
  93. fi
  94. if [ -d "${ROCM_PATH}" ]; then
  95. echo "ROCm libraries detected - building dynamic ROCm library"
  96. if [ -f ${ROCM_PATH}/lib/librocm_smi64.so.? ]; then
  97. ROCM_VARIANT=_v$(ls ${ROCM_PATH}/lib/librocm_smi64.so.? | cut -f3 -d. || true)
  98. fi
  99. init_vars
  100. 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)"
  101. BUILD_DIR="${LLAMACPP_DIR}/build/linux/rocm${ROCM_VARIANT}"
  102. build
  103. install
  104. gcc -fPIC -g -shared -o ${BUILD_DIR}/lib/libext_server.so \
  105. -Wl,--whole-archive \
  106. ${BUILD_DIR}/lib/libext_server.a \
  107. ${BUILD_DIR}/lib/libcommon.a \
  108. ${BUILD_DIR}/lib/libllama.a \
  109. -Wl,--no-whole-archive \
  110. -lrt -lpthread -ldl -lstdc++ -lm \
  111. -L/opt/rocm/lib -L/opt/amdgpu/lib/x86_64-linux-gnu/ \
  112. -Wl,-rpath,/opt/rocm/lib,-rpath,/opt/amdgpu/lib/x86_64-linux-gnu/ \
  113. -lhipblas -lrocblas -lamdhip64 -lrocsolver -lamd_comgr -lhsa-runtime64 -lrocsparse -ldrm -ldrm_amdgpu
  114. fi
  115. cleanup