gen_linux.sh 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/bin/bash
  2. # This script is intended to run inside the go generate
  3. # working directory must be llm/llama.cpp
  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. echo "Starting linux generate script"
  17. if [ -z "${CUDACXX}" -a -x /usr/local/cuda/bin/nvcc ]; then
  18. export CUDACXX=/usr/local/cuda/bin/nvcc
  19. fi
  20. COMMON_CMAKE_DEFS="-DCMAKE_POSITION_INDEPENDENT_CODE=on -DLLAMA_ACCELERATE=on -DLLAMA_NATIVE=off -DLLAMA_AVX=on -DLLAMA_AVX2=off -DLLAMA_AVX512=off -DLLAMA_FMA=off -DLLAMA_F16C=off"
  21. source $(dirname $0)/gen_common.sh
  22. init_vars
  23. git_module_setup
  24. apply_patches
  25. #
  26. # CPU first for the default library
  27. #
  28. CMAKE_DEFS="${COMMON_CMAKE_DEFS} ${CMAKE_DEFS}"
  29. BUILD_DIR="gguf/build/linux/cpu"
  30. build
  31. install
  32. if [ -d /usr/local/cuda/lib64/ ]; then
  33. echo "CUDA libraries detected - building dynamic CUDA library"
  34. init_vars
  35. CMAKE_DEFS="-DLLAMA_CUBLAS=on ${COMMON_CMAKE_DEFS} ${CMAKE_DEFS}"
  36. BUILD_DIR="gguf/build/linux/cuda"
  37. CUDA_LIB_DIR=/usr/local/cuda/lib64
  38. build
  39. install
  40. gcc -fPIC -g -shared -o ${BUILD_DIR}/lib/libext_server.so \
  41. -Wl,--whole-archive \
  42. ${BUILD_DIR}/lib/libext_server.a \
  43. ${BUILD_DIR}/lib/libcommon.a \
  44. ${BUILD_DIR}/lib/libllama.a \
  45. -Wl,--no-whole-archive \
  46. ${CUDA_LIB_DIR}/libcudart_static.a \
  47. ${CUDA_LIB_DIR}/libcublas_static.a \
  48. ${CUDA_LIB_DIR}/libcublasLt_static.a \
  49. ${CUDA_LIB_DIR}/libcudadevrt.a \
  50. ${CUDA_LIB_DIR}/libculibos.a \
  51. -lrt -lpthread -ldl -lstdc++ -lm
  52. fi
  53. if [ -z "${ROCM_PATH}" ]; then
  54. # Try the default location in case it exists
  55. ROCM_PATH=/opt/rocm
  56. fi
  57. if [ -z "${CLBlast_DIR}" ]; then
  58. # Try the default location in case it exists
  59. if [ -d /usr/lib/cmake/CLBlast ]; then
  60. export CLBlast_DIR=/usr/lib/cmake/CLBlast
  61. fi
  62. fi
  63. if [ -d "${ROCM_PATH}" ]; then
  64. echo "ROCm libraries detected - building dynamic ROCm library"
  65. init_vars
  66. 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='gfx803;gfx900;gfx906:xnack-;gfx908:xnack-;gfx90a:xnack+;gfx90a:xnack-;gfx1010;gfx1012;gfx1030;gfx1100;gfx1101;gfx1102' -DGPU_TARGETS='gfx803;gfx900;gfx906:xnack-;gfx908:xnack-;gfx90a:xnack+;gfx90a:xnack-;gfx1010;gfx1012;gfx1030;gfx1100;gfx1101;gfx1102'"
  67. BUILD_DIR="gguf/build/linux/rocm"
  68. build
  69. install
  70. gcc -fPIC -g -shared -o ${BUILD_DIR}/lib/libext_server.so \
  71. -Wl,--whole-archive \
  72. ${BUILD_DIR}/lib/libext_server.a \
  73. ${BUILD_DIR}/lib/libcommon.a \
  74. ${BUILD_DIR}/lib/libllama.a \
  75. -Wl,--no-whole-archive \
  76. -lrt -lpthread -ldl -lstdc++ -lm \
  77. -L/opt/rocm/lib -L/opt/amdgpu/lib/x86_64-linux-gnu/ \
  78. -Wl,-rpath,/opt/rocm/lib,-rpath,/opt/amdgpu/lib/x86_64-linux-gnu/ \
  79. -lhipblas -lrocblas -lamdhip64 -lrocsolver -lamd_comgr -lhsa-runtime64 -lrocsparse -ldrm -ldrm_amdgpu
  80. fi
  81. cleanup