gen_linux.sh 3.8 KB

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