summarylogtreecommitdiffstats
path: root/PKGBUILD
blob: 54aff627013661d563c4dc8624f2e736a57250b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# Maintainer:  dreieck
# Contributor: Alexander F. Rødseth <xyproto@archlinux.org>
# Contributor: Matt Harrison <matt@harrison.us.com>

## `PKGBUILD` based on the `PKGBUILD` for the package `ollama-rocm-git`.

_build_generic=true
_build_openmpi=true
_build_openblas=true
_build_clblas=false # 2024-04-17: Fails to link with
                    # ```
                    # /usr/bin/ld: /var/cache/makepkg/build/ollama-git/src/ollama-clblas/llm/build/linux/x86_64_static/libllama.a(ggml-opencl.cpp.o): in function `ggml_backend_opencl_buffer_get_tensor(ggml_backend_buffer*, ggml_tensor const*, void*, unsigned long, unsigned long)':
                    # ggml-opencl.cpp:(.text+0x46): undefined reference to `clEnqueueReadBuffer'
                    # ```
                    # and more errors.
_build_vulkan=true

_name="ollama"
_pkgbase="${_name}-nogpu"
pkgbase="${_pkgbase}-git"
pkgname=()
if "${_build_generic}"; then
  pkgname+=("${_name}-generic-git")
fi
if "${_build_openmpi}"; then
  pkgname+=("${_name}-openmpi-git")
fi
if "${_build_openblas}"; then
  pkgname+=("${_name}-openblas-git")
fi
if "${_build_clblas}"; then
  pkgname+=("${_name}-clblas-git")
fi
if "${_build_vulkan}"; then
  pkgname+=("${_name}-vulkan-git")
fi
pkgdesc='Create, run and share large language models (LLMs). Package(s) without dedicated GPU offloading (no CUDA, no ROCm, no SYCL).'
pkgver=0.1.37+3.r2707.20240511.4ec7445a
pkgrel=1
arch=(
  'armv7h'
  'aarch64'
  'i686'
  'x86_64'
)
url='https://github.com/jmorganca/ollama'
license=(
  "MIT"
)
depends=(
  'gcc-libs'
  'glibc'
  'openssl'
)
makedepends=(
  "bash"
  "cmake"
  "git"
  "go"
)
if "${_build_openmpi}"; then
  makedepends+=("openmpi")
fi
if "${_build_openblas}"; then
  makedepends+=("openblas")
fi
if "${_build_clblas}"; then
  makedepends+=("clblast")
fi
if "${_build_vulkan}"; then
  makedepends+=()
fi
source=(
  "${_name}::git+${url}.git"
  "llama.cpp::git+https://github.com/ggerganov/llama.cpp.git" # Submodule
  'disable-rocm-cuda.gen_linux.sh.patch'
  "ollama.service"
  "sysusers.conf"
  "tmpfiles.d"
)
b2sums=(
  'SKIP'  # ollama (git)
  'SKIP'  # llama.cpp (git)
  '02d92a87554f2842b100908395a2599a843d21b95bf2c1961c2d862c35dda906893af107460054bf02340c52e4b4e3baa80cf95d071968fb9e158e697edaf0c6'  # disable-rocm-cuda.gen_linux.sh.patch
  'a773bbf16cf5ccc2ee505ad77c3f9275346ddf412be283cfeaee7c2e4c41b8637a31aaff8766ed769524ebddc0c03cf924724452639b62208e578d98b9176124'  # ollama.service
  '3aabf135c4f18e1ad745ae8800db782b25b15305dfeaaa031b4501408ab7e7d01f66e8ebb5be59fc813cfbff6788d08d2e48dcf24ecc480a40ec9db8dbce9fec'  # sysusers.conf
  'e8f2b19e2474f30a4f984b45787950012668bf0acb5ad1ebb25cd9776925ab4a6aa927f8131ed53e35b1c71b32c504c700fe5b5145ecd25c7a8284373bb951ed'  # tmpfiles.d
)
options+=('emptydirs')
#options+=('!lto') # openmpi variant fails to link _without_ LTO.

_check_cpufeature() {
  ## Checks if the host CPU supports the feature passed as argument "$1".
  #  If yes, return "ON" to stdout.
  #  If not yes, return "OFF" to stdout.

  if grep -qE "\<$1" /proc/cpuinfo; then
    printf '%s' 'ON'
  else
    printf '%s' 'OFF'
  fi
}

_check_makepkgpotion() {
  ## Checks if the given makepkg option is set in `options`.
  #  If yes, return "ON" to stdout.
  #  If not yes, return "OFF" to stdout.

  _checkfor="$1"
  _result='OFF'
  for _option in "${options[@]}"; do
    if grep -qw "${_checkfor}" <<<"${_option}"; then
      _result="ON"
    fi
    if grep -qw "\!${_checkfor}" <<<"${_option}" 2>/dev/null; then
      _result="OFF"
    fi
  done
  echo "${_result}"
}

#-DBUILD_SHARED_LIBS=ON
#-DLLAMA_STATIC=ON
#-DLLAMA_QKK_64=ON
_cmake_options_common="
  -DBUILD_TESTING=ON
  -DCMAKE_BUILD_TYPE=Release
  -DCMAKE_INSTALL_PREFIX=/usr
  -DLLAMA_ACCELERATE=ON
  -DLLAMA_ALL_WARNINGS=OFF
  -DLLAMA_ALL_WARNINGS_3RD_PARTY=OFF
  -DLLAMA_FATAL_WARNINGS=OFF
  -DLLAMA_AVX="$(_check_cpufeature avx)" -DLLAMA_AVX2="$(_check_cpufeature avx2)" -DLLAMA_AVX512="$(_check_cpufeature avx512)" -DLLAMA_AVX512_VBMI="$(_check_cpufeature avx512vbmi)" -DLLAMA_AVX512_VNNI="$(_check_cpufeature avx512_vnni)" -DLLAMA_F16C="$(_check_cpufeature f16c)" -DLLAMA_FMA="$(_check_cpufeature fma)"
  -DLLAMA_BUILD_EXAMPLES=ON -DLLAMA_BUILD_SERVER=ON -DLLAMA_BUILD_TESTS=ON
  -DLLAMA_CPU_HBM=OFF -DLLAMA_CUBLAS=OFF -DLLAMA_CUDA=OFF -DLLAMA_HIPBLAS=OFF -DLLAMA_HIP_UMA=OFF -DLLAMA_METAL=OFF -DLLAMA_SYCL=OFF -DLLAMA_KOMPUTE=OFF
  -DLLAMA_LTO="$(_check_makepkgpotion lto)"
  -DLLAMA_GPROF=OFF -DLLAMA_PERF=OFF -DLLAMA_SANITIZE_ADDRESS=OFF -DLLAMA_SANITIZE_THREAD=OFF -DLLAMA_SANITIZE_UNDEFINED=OFF 
  -DLLAMA_SERVER_SSL=ON -DLLAMA_SERVER_VERBOSE=ON
"
_cmake_options_blas="-DLLAMA_BLAS=ON -DLLAMA_BLAS_VENDOR=openblas"
_cmake_options_mpi="-DLLAMA_MPI=ON"
_cmake_options_clblas="-DLLAMA_CLBLAST=ON"
_cmake_options_vulkan="-DLLAMA_VULKAN=ON -DLLAMA_VULKAN_CHECK_RESULTS=ON -DLLAMA_VULKAN_DEBUG=OFF -DLLAMA_VULKAN_RUN_TESTS=ON -DLLAMA_VULKAN_VALIDATE=OFF"

prepare() {
  export GOPATH="${srcdir}/go"
  cd "${srcdir}/${_name}"

  # Add submodules
  git submodule init
  git config --local submodule.llama.cpp.url "${srcdir}/llama.cpp"
  git -c protocol.file.allow=always submodule update

  for _patch in "${srcdir}/disable-rocm-cuda.gen_linux.sh.patch"; do
    printf '%s\n' "   > Applying patch $(basename "${_patch}") ..."
    patch -Np1 --follow-symlinks -i "${_patch}"
  done

  # Generate git logfile for later installation into the documentation directory
  git log > git.log

  # Turn LTO on and set the build type to Release
  #sed -i 's,T_CODE=on,T_CODE=on -D LLAMA_LTO=on -D CMAKE_BUILD_TYPE=Release,g' llm/generate/gen_linux.sh

  # Display a more helpful error message
  sed -i "s|could not connect to ollama server, run 'ollama serve' to start it|ollama is not running, try 'systemctl start ollama' or 'ollama serve'|g" cmd/cmd.go

  printf '%s\n' "   > Downloading go dependencies ..."
  go get

  for _variant in "${pkgname[@]}"; do
    _variant="${_variant%-git}"
    printf '%s\n' "   > Creating source directory for ${_variant} ..."
    cp -r "${srcdir}/${_name}" "${srcdir}/${_variant}"
  done
}

pkgver() {
  cd "${srcdir}/${_name}"

  _ver="$(git describe --tags | sed -E -e 's|^[vV]||' -e 's|\-g[0-9a-f]*$||' | tr '-' '+')"
  _rev="$(git rev-list --count HEAD)"
  _date="$(git log -1 --date=format:"%Y%m%d" --format="%ad")"
  _hash="$(git rev-parse --short HEAD)"

  if [ -z "${_ver}" ]; then
    error "Could not determine version."
    return 1
  else
    printf '%s' "${_ver}.r${_rev}.${_date}.${_hash}"
  fi
}

build() {
  export GOPATH="${srcdir}/go"
  #export CFLAGS="-march=native -mtune=generic -O2 -pipe -fno-plt"
  #export CXXFLAGS="$CFLAGS"
  #export CGO_CFLAGS="$CFLAGS" CGO_CPPFLAGS="$CPPFLAGS" CGO_CXXFLAGS="$CXXFLAGS" CGO_LDFLAGS="$LDFLAGS"


  if "${_build_generic}"; then
    cd "${srcdir}/ollama-generic"

    export OLLAMA_CUSTOM_CPU_DEFS="${_cmake_options_common}"

    printf '\n'
    printf '%s\n' "   > Compiling generic variant ..."
    printf '\n'
    go generate ./...
    cp llm/build/linux/x86_64/cpu/libllama.a llm/build/linux/x86_64_static/
    cp llm/build/linux/x86_64_static/libllama.so llm/build/linux/x86_64/cpu/
    go build -buildmode=pie -trimpath -mod=readonly -modcacherw -ldflags=-linkmode=external -ldflags=-buildid='' -ldflags="-X=github.com/jmorganca/ollama/version.Version=${pkgver}"
  fi


  if "${_build_openmpi}"; then
    cd "${srcdir}/ollama-openmpi"

    export OLLAMA_CUSTOM_CPU_DEFS="${_cmake_options_common} ${_cmake_options_mpi}"

    printf '\n'
    printf '%s\n' "   > Compiling OpenMPI variant ..."
    printf '\n'
    go generate ./...
    cp llm/build/linux/x86_64/cpu/libllama.a llm/build/linux/x86_64_static/
    cp llm/build/linux/x86_64_static/libllama.so llm/build/linux/x86_64/cpu/
    go build -buildmode=pie -trimpath -mod=readonly -modcacherw -ldflags=-linkmode=external -ldflags=-buildid='' -ldflags="-X=github.com/jmorganca/ollama/version.Version=${pkgver}"
  fi


  if "${_build_openblas}"; then
    cd "${srcdir}/ollama-openblas"

    export OLLAMA_CUSTOM_CPU_DEFS="${_cmake_options_common} ${_cmake_options_blas}"

    printf '\n'
    printf '%s\n' "   > Compiling openblas variant ..."
    printf '\n'
    go generate ./...
    cp llm/build/linux/x86_64/cpu/libllama.a llm/build/linux/x86_64_static/
    cp llm/build/linux/x86_64_static/libllama.so llm/build/linux/x86_64/cpu/
    go build -buildmode=pie -trimpath -mod=readonly -modcacherw -ldflags=-linkmode=external -ldflags=-buildid='' -ldflags="-X=github.com/jmorganca/ollama/version.Version=${pkgver}"
  fi


  if "${_build_clblas}"; then
    cd "${srcdir}/ollama-clblas"

    export OLLAMA_CUSTOM_CPU_DEFS="${_cmake_options_common} ${_cmake_options_clblas}"

    printf '\n'
    printf '%s\n' "   > Compiling clblas variant ..."
    printf '\n'
    go generate ./...
    cp llm/build/linux/x86_64/cpu/libllama.a llm/build/linux/x86_64_static/
    cp llm/build/linux/x86_64_static/libllama.so llm/build/linux/x86_64/cpu/
    go build -buildmode=pie -trimpath -mod=readonly -modcacherw -ldflags=-linkmode=external -ldflags=-buildid='' -ldflags="-X=github.com/jmorganca/ollama/version.Version=${pkgver}"
  fi


  if "${_build_vulkan}"; then
    cd "${srcdir}/ollama-vulkan"

    export OLLAMA_CUSTOM_CPU_DEFS="${_cmake_options_common} ${_cmake_options_vulkan}"

    printf '\n'
    printf '%s\n' "   > Compiling vulkan variant ..."
    printf '\n'
    go generate ./...
    cp llm/build/linux/x86_64/cpu/libllama.a llm/build/linux/x86_64_static/
    cp llm/build/linux/x86_64_static/libllama.so llm/build/linux/x86_64/cpu/
    go build -buildmode=pie -trimpath -mod=readonly -modcacherw -ldflags=-linkmode=external -ldflags=-buildid='' -ldflags="-X=github.com/jmorganca/ollama/version.Version=${pkgver}"
  fi
}

check() {
  export GOPATH="${srcdir}/go"
  for _variant in "${pkgname[@]}"; do
    _variant="${_variant%-git}"
    printf '\n'
    printf '%s\n' "   > Running tests for ${_variant} ..."
    printf '\n'
    cd "${srcdir}/${_variant}"
    go test ./api ./format
    ./ollama --version > /dev/null
  done
}

package_ollama-generic-git() {
  pkgdesc="Create, run and share large language models (LLMs). CPU optimisation only."
  depends+=(
  )
  optdepends=()
  provides=(
    "${_name}=${pkgver}"
    "${_name}-git=${pkgver}"
  )
  conflicts=(
    "${_name}"
  )
  _variant="ollama-generic"
  cd "${srcdir}/${_variant}"

  install -Dvm755 -t "${pkgdir}/usr/bin" 'ollama'
  install -dvm755 "${pkgdir}/var/lib/ollama"
  install -Dvm644 -t "${pkgdir}/usr/lib/systemd/system" "${srcdir}/ollama.service"
  install -Dvm644 "${srcdir}/sysusers.conf" "${pkgdir}/usr/lib/sysusers.d/ollama.conf"
  install -Dvm644 "${srcdir}/tmpfiles.d" "${pkgdir}/usr/lib/tmpfiles.d/ollama.conf"

  install -Dvm644 -t "${pkgdir}/usr/share/doc/${_name}" git.log README.md
  install -Dvm644 -t "${pkgdir}/usr/share/licenses/${pkgname}" "LICENSE"
  ln -svr "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE" "${pkgdir}/usr/share/doc/${_name}/LICENSE"
}

package_ollama-openmpi-git() {
  pkgdesc="Create, run and share large language models (LLMs). CPU optimisation with openMPI."
  depends+=(
  )
  optdepends=()
  provides=(
    "${_name}=${pkgver}"
    "${_name}-git=${pkgver}"
  )
  conflicts=(
    "${_name}"
  )
  _variant="ollama-openmpi"
  cd "${srcdir}/${_variant}"

  install -Dvm755 -t "${pkgdir}/usr/bin" 'ollama'
  install -dvm755 "${pkgdir}/var/lib/ollama"
  install -Dvm644 -t "${pkgdir}/usr/lib/systemd/system" "${srcdir}/ollama.service"
  install -Dvm644 "${srcdir}/sysusers.conf" "${pkgdir}/usr/lib/sysusers.d/ollama.conf"
  install -Dvm644 "${srcdir}/tmpfiles.d" "${pkgdir}/usr/lib/tmpfiles.d/ollama.conf"

  install -Dvm644 -t "${pkgdir}/usr/share/doc/${_name}" git.log README.md
  install -Dvm644 -t "${pkgdir}/usr/share/licenses/${pkgname}" "LICENSE"
  ln -svr "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE" "${pkgdir}/usr/share/doc/${_name}/LICENSE"
}

package_ollama-openblas-git() {
  pkgdesc="Create, run and share large language models (LLMs). CPU optimisation with openblas."
  depends+=(
  )
  optdepends=()
  provides=(
    "${_name}=${pkgver}"
    "${_name}-git=${pkgver}"
  )
  conflicts=(
    "${_name}"
  )
  _variant="ollama-openblas"
  cd "${srcdir}/${_variant}"

  install -Dvm755 -t "${pkgdir}/usr/bin" 'ollama'
  install -dvm755 "${pkgdir}/var/lib/ollama"
  install -Dvm644 -t "${pkgdir}/usr/lib/systemd/system" "${srcdir}/ollama.service"
  install -Dvm644 "${srcdir}/sysusers.conf" "${pkgdir}/usr/lib/sysusers.d/ollama.conf"
  install -Dvm644 "${srcdir}/tmpfiles.d" "${pkgdir}/usr/lib/tmpfiles.d/ollama.conf"

  install -Dvm644 -t "${pkgdir}/usr/share/doc/${_name}" git.log README.md
  install -Dvm644 -t "${pkgdir}/usr/share/licenses/${pkgname}" "LICENSE"
  ln -svr "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE" "${pkgdir}/usr/share/doc/${_name}/LICENSE"
}

package_ollama-clblas-git() {
  pkgdesc="Create, run and share large language models (LLMs). With clblast backend."
  depends+=(
    'clblast'
  )
  optdepends=()
  provides=(
    "${_name}=${pkgver}"
    "${_name}-git=${pkgver}"
  )
  conflicts=(
    "${_name}"
  )
  _variant="ollama-clblas"
  cd "${srcdir}/${_variant}"

  install -Dvm755 -t "${pkgdir}/usr/bin" 'ollama'
  install -dvm755 "${pkgdir}/var/lib/ollama"
  install -Dvm644 -t "${pkgdir}/usr/lib/systemd/system" "${srcdir}/ollama.service"
  install -Dvm644 "${srcdir}/sysusers.conf" "${pkgdir}/usr/lib/sysusers.d/ollama.conf"
  install -Dvm644 "${srcdir}/tmpfiles.d" "${pkgdir}/usr/lib/tmpfiles.d/ollama.conf"

  install -Dvm644 -t "${pkgdir}/usr/share/doc/${_name}" git.log README.md
  install -Dvm644 -t "${pkgdir}/usr/share/licenses/${pkgname}" "LICENSE"
  ln -svr "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE" "${pkgdir}/usr/share/doc/${_name}/LICENSE"
}

package_ollama-vulkan-git() {
  pkgdesc="Create, run and share large language models (LLMs). With vulkan backend."
  depends+=(
  )
  optdepends=()
  provides=(
    "${_name}=${pkgver}"
    "${_name}-git=${pkgver}"
  )
  conflicts=(
    "${_name}"
  )
  _variant="ollama-vulkan"
  cd "${srcdir}/${_variant}"

  install -Dvm755 -t "${pkgdir}/usr/bin" 'ollama'
  install -dvm755 "${pkgdir}/var/lib/ollama"
  install -Dvm644 -t "${pkgdir}/usr/lib/systemd/system" "${srcdir}/ollama.service"
  install -Dvm644 "${srcdir}/sysusers.conf" "${pkgdir}/usr/lib/sysusers.d/ollama.conf"
  install -Dvm644 "${srcdir}/tmpfiles.d" "${pkgdir}/usr/lib/tmpfiles.d/ollama.conf"

  install -Dvm644 -t "${pkgdir}/usr/share/doc/${_name}" git.log README.md
  install -Dvm644 -t "${pkgdir}/usr/share/licenses/${pkgname}" "LICENSE"
  ln -svr "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE" "${pkgdir}/usr/share/doc/${_name}/LICENSE"
}