gen_darwin.sh 3.9 KB

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