gen_linux.sh 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. OLLAMA_DYN_LIB_DIR="gguf/build/lib"
  22. mkdir -p ${OLLAMA_DYN_LIB_DIR}
  23. touch ${OLLAMA_DYN_LIB_DIR}/.generated
  24. source $(dirname $0)/gen_common.sh
  25. init_vars
  26. git_module_setup
  27. apply_patches
  28. #
  29. # CPU first for the default library
  30. #
  31. CMAKE_DEFS="${COMMON_CMAKE_DEFS} ${CMAKE_DEFS}"
  32. BUILD_DIR="gguf/build/cpu"
  33. build
  34. if [ -d /usr/local/cuda/lib64/ ]; then
  35. echo "CUDA libraries detected - building dynamic CUDA library"
  36. init_vars
  37. CMAKE_DEFS="-DLLAMA_CUBLAS=on ${COMMON_CMAKE_DEFS} ${CMAKE_DEFS}"
  38. BUILD_DIR="gguf/build/cuda"
  39. CUDA_LIB_DIR=/usr/local/cuda/lib64
  40. build
  41. gcc -fPIC -g -shared -o ${OLLAMA_DYN_LIB_DIR}/libcuda_server.so \
  42. -Wl,--whole-archive \
  43. ${BUILD_DIR}/examples/server/libext_server.a \
  44. ${BUILD_DIR}/common/libcommon.a \
  45. ${BUILD_DIR}/libllama.a \
  46. -Wl,--no-whole-archive \
  47. ${CUDA_LIB_DIR}/libcudart_static.a \
  48. ${CUDA_LIB_DIR}/libcublas_static.a \
  49. ${CUDA_LIB_DIR}/libcublasLt_static.a \
  50. ${CUDA_LIB_DIR}/libcudadevrt.a \
  51. ${CUDA_LIB_DIR}/libculibos.a \
  52. -lrt -lpthread -ldl -lstdc++ -lm
  53. fi
  54. if [ -z "${ROCM_PATH}" ]; then
  55. # Try the default location in case it exists
  56. ROCM_PATH=/opt/rocm
  57. fi
  58. if [ -z "${CLBlast_DIR}" ]; then
  59. # Try the default location in case it exists
  60. if [ -d /usr/lib/cmake/CLBlast ]; then
  61. export CLBlast_DIR=/usr/lib/cmake/CLBlast
  62. fi
  63. fi
  64. if [ -d "${ROCM_PATH}" ]; then
  65. echo "ROCm libraries detected - building dynamic ROCm library"
  66. init_vars
  67. 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'"
  68. BUILD_DIR="gguf/build/rocm"
  69. build
  70. gcc -fPIC -g -shared -o ${OLLAMA_DYN_LIB_DIR}/librocm_server.so \
  71. -Wl,--whole-archive \
  72. ${BUILD_DIR}/examples/server/libext_server.a \
  73. ${BUILD_DIR}/common/libcommon.a \
  74. ${BUILD_DIR}/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