gen_common.sh 3.93 KB
Newer Older
John's avatar
John committed
1
# common logic across linux and darwin
2
3

init_vars() {
4
5
6
7
8
9
10
11
    case "${GOARCH}" in
    "amd64")
        ARCH="x86_64"
        ;;
    "arm64")
        ARCH="arm64"
        ;;
    *)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
12
13
14
15
        echo "GOARCH must be set"
        echo "this script is meant to be run from within go generate"
        exit 1
        ;;
16
17
    esac

18
    LLAMACPP_DIR=../llama.cpp
Daniel Hiltgen's avatar
Daniel Hiltgen committed
19
    CMAKE_DEFS="-DCMAKE_SKIP_RPATH=on"
20
    CMAKE_TARGETS="--target ollama_llama_server"
21
    if echo "${CGO_CFLAGS}" | grep -- '-g' >/dev/null; then
22
        CMAKE_DEFS="-DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_VERBOSE_MAKEFILE=on -DLLAMA_GPROF=on -DLLAMA_SERVER_VERBOSE=on ${CMAKE_DEFS}"
23
24
    else
        # TODO - add additional optimization flags...
25
        CMAKE_DEFS="-DCMAKE_BUILD_TYPE=Release -DLLAMA_SERVER_VERBOSE=off ${CMAKE_DEFS}"
26
    fi
Roy Yang's avatar
Roy Yang committed
27
    case $(uname -s) in
28
29
30
31
32
    "Darwin")
        LIB_EXT="dylib"
        WHOLE_ARCHIVE="-Wl,-force_load"
        NO_WHOLE_ARCHIVE=""
        GCC_ARCH="-arch ${ARCH}"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
33
        DIST_BASE=../../dist/darwin-${GOARCH}/
34
        PAYLOAD_BASE=../../build/darwin/${GOARCH}
35
36
37
38
39
40
41
42
        ;;
    "Linux")
        LIB_EXT="so"
        WHOLE_ARCHIVE="-Wl,--whole-archive"
        NO_WHOLE_ARCHIVE="-Wl,--no-whole-archive"

        # Cross compiling not supported on linux - Use docker
        GCC_ARCH=""
Daniel Hiltgen's avatar
Daniel Hiltgen committed
43
        DIST_BASE=../../dist/linux-${GOARCH}/
44
        PAYLOAD_BASE=../../build/linux/${GOARCH}
45
46
47
48
        ;;
    *)
        ;;
    esac
49
    if [ -z "${CMAKE_CUDA_ARCHITECTURES}" ] ; then
50
51
        CMAKE_CUDA_ARCHITECTURES="50;52;61;70;75;80"
    fi
52
53
    GZIP=$(command -v pigz 2>/dev/null || echo "gzip")
    RUNNER_BASE="${DIST_BASE}/lib/ollama/runners"
54
55
56
}

git_module_setup() {
57
    if [ -n "${OLLAMA_SKIP_PATCHING}" ]; then
58
59
60
        echo "Skipping submodule initialization"
        return
    fi
Daniel Hiltgen's avatar
Daniel Hiltgen committed
61
62
63
64
65
    # Make sure the tree is clean after the directory moves
    if [ -d "${LLAMACPP_DIR}/gguf" ]; then
        echo "Cleaning up old submodule"
        rm -rf ${LLAMACPP_DIR}
    fi
66
    git submodule init
67
    git submodule update --force ${LLAMACPP_DIR}
68
69
70
71

}

apply_patches() {
Michael Yang's avatar
Michael Yang committed
72
73
74
75
    # apply temporary patches until fix is upstream
    for patch in ../patches/*.patch; do
        git -c 'user.name=nobody' -c 'user.email=<>' -C ${LLAMACPP_DIR} am ${patch}
    done
76
77
78
}

build() {
79
80
    cmake -S ${LLAMACPP_DIR} -B ${BUILD_DIR} ${CMAKE_DEFS}
    cmake --build ${BUILD_DIR} ${CMAKE_TARGETS} -j8
81
82
    # remove unnecessary build artifacts
    rm -f ${BUILD_DIR}/bin/ggml-common.h ${BUILD_DIR}/bin/ggml-metal.metal
83
}
84

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
dist() {
    [ -z "${RUNNER}" ] && exit 1
    mkdir -p ${RUNNER_BASE}/${RUNNER}/
    for f in ${BUILD_DIR}/bin/* ; do
        cp ${f} ${RUNNER_BASE}/${RUNNER}/
    done
    # check for lib directory
    if [ -d ${BUILD_DIR}/lib ]; then
        for f in ${BUILD_DIR}/lib/* ; do
            cp ${f} ${RUNNER_BASE}/${RUNNER}/
        done
    fi
}

# Compress from the build $BUILD_DIR into the $PAYLOAD_BASE/$RUNNER dir
100
compress() {
101
102
103
104
    [ -z "${RUNNER}" ] && exit 1
    echo "Compressing payloads with ${GZIP} to reduce overall binary size..."
    rm -rf "${PAYLOAD_BASE}/${RUNNER}/"
    mkdir -p "${PAYLOAD_BASE}/${RUNNER}/"
105
    for f in ${BUILD_DIR}/bin/* ; do
106
        ${GZIP} -c --best ${f} > "${PAYLOAD_BASE}/${RUNNER}/$(basename ${f}).gz" &
107
        compress_pids+=" $!"
108
    done
109
110
111
    # check for lib directory
    if [ -d ${BUILD_DIR}/lib ]; then
        for f in ${BUILD_DIR}/lib/* ; do
112
            ${GZIP} -c --best ${f} > "${PAYLOAD_BASE}/${RUNNER}/$(basename ${f}).gz" &
113
            compress_pids+=" $!"
114
115
116
        done
    fi
    echo
117
118
119
120
}

wait_for_compress() {
    for pid in ${compress_pids}; do
121
122
123
        wait $pid
    done
    echo "Finished compression"
124
125
}

Daniel Hiltgen's avatar
Daniel Hiltgen committed
126
127
install() {
    echo "Installing libraries to bin dir ${BUILD_DIR}/bin/"
128
    for lib in $(find ${BUILD_DIR} -name \*.${LIB_EXT} | grep -v "${BUILD_DIR}/bin/" ); do
Daniel Hiltgen's avatar
Daniel Hiltgen committed
129
130
131
132
133
        rm -f "${BUILD_DIR}/bin/$(basename ${lib})"
        cp -af "${lib}" "${BUILD_DIR}/bin/"
    done
}

134
135
# Keep the local tree clean after we're done with the build
cleanup() {
Daniel Hiltgen's avatar
Daniel Hiltgen committed
136
    git submodule update --force ${LLAMACPP_DIR}
137
}