gen_darwin.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/bin/bash
  2. # This script is intended to run inside the go generate
  3. # working directory must be ./llm/generate/
  4. # TODO - add hardening to detect missing tools (cmake, etc.)
  5. set -ex
  6. set -o pipefail
  7. compress_pids=""
  8. echo "Starting darwin generate script"
  9. source $(dirname $0)/gen_common.sh
  10. init_vars
  11. git_module_setup
  12. apply_patches
  13. sign() {
  14. if [ -n "$APPLE_IDENTITY" ]; then
  15. codesign -f --timestamp --deep --options=runtime --sign "$APPLE_IDENTITY" --identifier ai.ollama.ollama $1
  16. fi
  17. }
  18. COMMON_DARWIN_DEFS="-DBUILD_SHARED_LIBS=off -DCMAKE_OSX_DEPLOYMENT_TARGET=11.3 -DGGML_METAL_MACOSX_VERSION_MIN=11.3 -DCMAKE_SYSTEM_NAME=Darwin -DGGML_METAL_EMBED_LIBRARY=on -DGGML_OPENMP=off"
  19. case "${GOARCH}" in
  20. "amd64")
  21. COMMON_CPU_DEFS="${COMMON_DARWIN_DEFS} -DCMAKE_SYSTEM_PROCESSOR=${ARCH} -DCMAKE_OSX_ARCHITECTURES=${ARCH} -DGGML_METAL=off -DGGML_NATIVE=off"
  22. # Static build for linking into the Go binary
  23. init_vars
  24. CMAKE_TARGETS="--target llama --target ggml"
  25. CMAKE_DEFS="${COMMON_CPU_DEFS} -DGGML_BLAS=off -DGGML_ACCELERATE=off -DGGML_AVX=off -DGGML_AVX2=off -DGGML_AVX512=off -DGGML_FMA=off -DGGML_F16C=off ${CMAKE_DEFS}"
  26. BUILD_DIR="../build/darwin/${ARCH}_static"
  27. echo "Building static library"
  28. build
  29. if [ -z "$OLLAMA_SKIP_CPU_GENERATE" ]; then
  30. #
  31. # CPU first for the default library, set up as lowest common denominator for maximum compatibility (including Rosetta)
  32. #
  33. init_vars
  34. CMAKE_DEFS="${COMMON_CPU_DEFS} -DGGML_ACCELERATE=off -DGGML_BLAS=off -DGGML_AVX=off -DGGML_AVX2=off -DGGML_AVX512=off -DGGML_FMA=off -DGGML_F16C=off ${CMAKE_DEFS}"
  35. BUILD_DIR="../build/darwin/${ARCH}/cpu"
  36. echo "Building LCD CPU"
  37. build
  38. sign ${BUILD_DIR}/bin/ollama_llama_server
  39. compress
  40. #
  41. # ~2011 CPU Dynamic library with more capabilities turned on to optimize performance
  42. # Approximately 400% faster than LCD on same CPU
  43. #
  44. init_vars
  45. CMAKE_DEFS="${COMMON_CPU_DEFS} -DGGML_ACCELERATE=off -DGGML_BLAS=off -DGGML_AVX=on -DGGML_AVX2=off -DGGML_AVX512=off -DGGML_FMA=off -DGGML_F16C=off ${CMAKE_DEFS}"
  46. BUILD_DIR="../build/darwin/${ARCH}/cpu_avx"
  47. echo "Building AVX CPU"
  48. build
  49. sign ${BUILD_DIR}/bin/ollama_llama_server
  50. compress
  51. #
  52. # ~2013 CPU Dynamic library
  53. # Approximately 10% faster than AVX on same CPU
  54. #
  55. init_vars
  56. CMAKE_DEFS="${COMMON_CPU_DEFS} -DGGML_ACCELERATE=on -DGGML_BLAS=off -DGGML_AVX=on -DGGML_AVX2=on -DGGML_AVX512=off -DGGML_FMA=on -DGGML_F16C=on ${CMAKE_DEFS}"
  57. BUILD_DIR="../build/darwin/${ARCH}/cpu_avx2"
  58. echo "Building AVX2 CPU"
  59. EXTRA_LIBS="${EXTRA_LIBS} -framework Accelerate -framework Foundation"
  60. build
  61. sign ${BUILD_DIR}/bin/ollama_llama_server
  62. compress
  63. fi
  64. ;;
  65. "arm64")
  66. # Static build for linking into the Go binary
  67. init_vars
  68. CMAKE_TARGETS="--target llama --target ggml"
  69. CMAKE_DEFS="${COMMON_DARWIN_DEFS} -DCMAKE_OSX_DEPLOYMENT_TARGET=11.3 -DCMAKE_SYSTEM_NAME=Darwin -DCMAKE_SYSTEM_PROCESSOR=${ARCH} -DCMAKE_OSX_ARCHITECTURES=${ARCH} ${CMAKE_DEFS}"
  70. BUILD_DIR="../build/darwin/${ARCH}_static"
  71. echo "Building static library"
  72. build
  73. if [ -z "$OLLAMA_SKIP_METAL_GENERATE" ]; then
  74. init_vars
  75. CMAKE_DEFS="${COMMON_DARWIN_DEFS} -DCMAKE_SYSTEM_PROCESSOR=${ARCH} -DCMAKE_OSX_ARCHITECTURES=${ARCH} ${CMAKE_DEFS}"
  76. BUILD_DIR="../build/darwin/${ARCH}/metal"
  77. EXTRA_LIBS="${EXTRA_LIBS} -framework Accelerate -framework Foundation -framework Metal -framework MetalKit -framework MetalPerformanceShaders"
  78. build
  79. sign ${BUILD_DIR}/bin/ollama_llama_server
  80. compress
  81. fi
  82. ;;
  83. *)
  84. echo "GOARCH must be set"
  85. echo "this script is meant to be run from within go generate"
  86. exit 1
  87. ;;
  88. esac
  89. cleanup
  90. wait_for_compress
  91. echo "go generate completed. LLM runners: $(cd ${BUILD_DIR}/..; echo *)"