gen_darwin.sh 4 KB
Newer Older
1
#!/bin/bash
2
3
# This script is intended to run inside the go generate
# working directory must be ./llm/generate/
4
5
6
7
8

# TODO - add hardening to detect missing tools (cmake, etc.)

set -ex
set -o pipefail
9
compress_pids=""
10
11
12
echo "Starting darwin generate script"
source $(dirname $0)/gen_common.sh
init_vars
13
14
15
git_module_setup
apply_patches

Jeffrey Morgan's avatar
Jeffrey Morgan committed
16
17
18
19
20
21
sign() {
    if [ -n "$APPLE_IDENTITY" ]; then
        codesign -f --timestamp --deep --options=runtime --sign "$APPLE_IDENTITY" --identifier ai.ollama.ollama $1
    fi
}

22
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"
23

24
case "${GOARCH}" in
25
"amd64")
26
    COMMON_CPU_DEFS="${COMMON_DARWIN_DEFS} -DCMAKE_SYSTEM_PROCESSOR=${ARCH} -DCMAKE_OSX_ARCHITECTURES=${ARCH} -DGGML_METAL=off -DGGML_NATIVE=off"
27

28
29
30
    # Static build for linking into the Go binary
    init_vars
    CMAKE_TARGETS="--target llama --target ggml"
31
    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}"
32
33
34
35
    BUILD_DIR="../build/darwin/${ARCH}_static"
    echo "Building static library"
    build

36
37
38
39
40
    if [ -z "$OLLAMA_SKIP_CPU_GENERATE" ]; then
        #
        # CPU first for the default library, set up as lowest common denominator for maximum compatibility (including Rosetta)
        #
        init_vars
41
        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}"
42
        RUNNER=cpu
43
        BUILD_DIR="../build/darwin/${GOARCH}/${RUNNER}"
44
45
46
47
        echo "Building LCD CPU"
        build
        sign ${BUILD_DIR}/bin/ollama_llama_server
        compress
48

49
50
51
52
53
        #
        # ~2011 CPU Dynamic library with more capabilities turned on to optimize performance
        # Approximately 400% faster than LCD on same CPU
        #
        init_vars
54
        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}"
55
        RUNNER=cpu_avx
56
        BUILD_DIR="../build/darwin/${GOARCH}/${RUNNER}"
57
58
59
60
        echo "Building AVX CPU"
        build
        sign ${BUILD_DIR}/bin/ollama_llama_server
        compress
61

62
63
64
65
66
        #
        # ~2013 CPU Dynamic library
        # Approximately 10% faster than AVX on same CPU
        #
        init_vars
67
        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}"
68
        RUNNER=cpu_avx2
69
        BUILD_DIR="../build/darwin/${GOARCH}/${RUNNER}"
70
71
72
73
74
75
        echo "Building AVX2 CPU"
        EXTRA_LIBS="${EXTRA_LIBS} -framework Accelerate -framework Foundation"
        build
        sign ${BUILD_DIR}/bin/ollama_llama_server
        compress
    fi
76
77
    ;;
"arm64")
78
79
80
81

    # Static build for linking into the Go binary
    init_vars
    CMAKE_TARGETS="--target llama --target ggml"
82
    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}"
83
84
85
86
    BUILD_DIR="../build/darwin/${ARCH}_static"
    echo "Building static library"
    build

87
88
    if [ -z "$OLLAMA_SKIP_METAL_GENERATE" ]; then
        init_vars
89
        CMAKE_DEFS="${COMMON_DARWIN_DEFS} -DCMAKE_SYSTEM_PROCESSOR=${ARCH} -DCMAKE_OSX_ARCHITECTURES=${ARCH} ${CMAKE_DEFS}"
90
        RUNNER="metal"
91
        BUILD_DIR="../build/darwin/${GOARCH}/${RUNNER}"
92
93
94
95
96
        EXTRA_LIBS="${EXTRA_LIBS} -framework Accelerate -framework Foundation -framework Metal -framework MetalKit -framework MetalPerformanceShaders"
        build
        sign ${BUILD_DIR}/bin/ollama_llama_server
        compress
    fi
97
98
99
    ;;
*)
    echo "GOARCH must be set"
100
    echo "this script is meant to be run from within go generate"
101
102
    exit 1
    ;;
103
104
esac

105
cleanup
106
wait_for_compress
107
echo "go generate completed.  LLM runners: $(cd ${BUILD_DIR}/..; echo *)"