gen_darwin.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. RUNNER=cpu
  36. BUILD_DIR="../build/darwin/${ARCH}/${RUNNER}"
  37. echo "Building LCD CPU"
  38. build
  39. sign ${BUILD_DIR}/bin/ollama_llama_server
  40. compress
  41. #
  42. # ~2011 CPU Dynamic library with more capabilities turned on to optimize performance
  43. # Approximately 400% faster than LCD on same CPU
  44. #
  45. init_vars
  46. 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}"
  47. RUNNER=cpu_avx
  48. BUILD_DIR="../build/darwin/${ARCH}/${RUNNER}"
  49. echo "Building AVX CPU"
  50. build
  51. sign ${BUILD_DIR}/bin/ollama_llama_server
  52. compress
  53. #
  54. # ~2013 CPU Dynamic library
  55. # Approximately 10% faster than AVX on same CPU
  56. #
  57. init_vars
  58. 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}"
  59. RUNNER=cpu_avx2
  60. BUILD_DIR="../build/darwin/${ARCH}/${RUNNER}"
  61. echo "Building AVX2 CPU"
  62. EXTRA_LIBS="${EXTRA_LIBS} -framework Accelerate -framework Foundation"
  63. build
  64. sign ${BUILD_DIR}/bin/ollama_llama_server
  65. compress
  66. fi
  67. ;;
  68. "arm64")
  69. # Static build for linking into the Go binary
  70. init_vars
  71. CMAKE_TARGETS="--target llama --target ggml"
  72. 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}"
  73. BUILD_DIR="../build/darwin/${ARCH}_static"
  74. echo "Building static library"
  75. build
  76. if [ -z "$OLLAMA_SKIP_METAL_GENERATE" ]; then
  77. init_vars
  78. CMAKE_DEFS="${COMMON_DARWIN_DEFS} -DCMAKE_SYSTEM_PROCESSOR=${ARCH} -DCMAKE_OSX_ARCHITECTURES=${ARCH} ${CMAKE_DEFS}"
  79. RUNNER="metal"
  80. BUILD_DIR="../build/darwin/${ARCH}/${RUNNER}"
  81. EXTRA_LIBS="${EXTRA_LIBS} -framework Accelerate -framework Foundation -framework Metal -framework MetalKit -framework MetalPerformanceShaders"
  82. build
  83. sign ${BUILD_DIR}/bin/ollama_llama_server
  84. compress
  85. fi
  86. ;;
  87. *)
  88. echo "GOARCH must be set"
  89. echo "this script is meant to be run from within go generate"
  90. exit 1
  91. ;;
  92. esac
  93. cleanup
  94. wait_for_compress
  95. echo "go generate completed. LLM runners: $(cd ${BUILD_DIR}/..; echo *)"