aboutsummarylogtreecommitdiffstats
path: root/PKGBUILD
blob: 55aa107e970b3ddb420a53714b382868ff029c7d (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
# Maintainer: wuxxin <wuxxin@gmail.com>

# env vars for build steering
# eg. to only build for cpu, set _ENABLE_CUDA and _ENABLE_ROCM to 0
_ENABLE_CPU=${_ENABLE_CPU:-1}
_ENABLE_CUDA=${_ENABLE_CUDA:-1}
_ENABLE_ROCM=${_ENABLE_ROCM:-1}

# if set to 1 make is called with -j <physical cores> for paralell building, set to 0 for debug
_ENABLE_PARALLEL=${_ENABLE_PARALLEL:-1}

# additional backends
# piper (text2speech) backend
_ENABLE_PIPER=${_ENABLE_PIPER:-1}
# whisper (speech2text) backend
_ENABLE_WHISPER=${_ENABLE_WHISPER:-1}
# python backends, enables "-python" package flavors
_ENABLE_PYTHON=${_ENABLE_PYTHON:-1}
# will be automatically set on "-python" package flavors
_IS_PYTHON_FLAVOR=0

# if GPU_TARGETS and AMDGPU_TARGETS are not set, mirror architecture list from arch:python-pytorch@2.3.0-2
_AMDGPU_TARGETS="gfx906;gfx908;gfx90a;gfx940;gfx941;gfx942;gfx1010;gfx1012;gfx1030;gfx1100;gfx1101;gfx1102"
if test -n "$GPU_TARGETS"; then _AMDGPU_TARGETS="$GPU_TARGETS"; fi
if test -n "$AMDGPU_TARGETS"; then _AMDGPU_TARGETS="$AMDGPU_TARGETS"; fi

# additional optional grpc backends
_OPTIONAL_GRPC="${_OPTIONAL_GRPC:-}"
# additional optional args for main Makefile calling
_OPTIONAL_MAKE_ARGS="${_OPTIONAL_MAKE_ARGS:-}"

# limit pulling external sources
_EXTERNAL_SOURCES="backend/cpp/llama/llama.cpp"
# disabled: go-llama.cpp gpt4all go-rwkv.cpp go-stable-diffusion go-tiny-dream go-bert go-piper whisper
_DISABLED_MOD_EDIT="nomic-ai/gpt4all/gpt4all mudler/go-stable-diffusion \
  go-skynet/go-llama.cpp go-skynet/go-bert.cpp donomii/go-rwkv.cpp M0Rf30/go-tiny-dream"

if [[ $_ENABLE_WHISPER = 1 ]]; then
  _EXTERNAL_SOURCES="$_EXTERNAL_SOURCES sources/whisper.cpp"
  _OPTIONAL_GRPC="backend-assets/grpc/whisper $_OPTIONAL_GRPC"
else
  _DISABLED_MOD_EDIT="$_DISABLED_MOD_EDIT ggerganov/whisper.cpp"
fi

if [[ $_ENABLE_PIPER = 1 ]]; then
  _EXTERNAL_SOURCES="$_EXTERNAL_SOURCES sources/go-piper"
  _OPTIONAL_GRPC="backend-assets/grpc/piper $_OPTIONAL_GRPC"
  _GO_TAGS="tts"
else
  _DISABLED_MOD_EDIT="$_DISABLED_MOD_EDIT mudler/go-piper"
  _GO_TAGS=""
fi

# disabled backends: backend-assets/util/llama-cpp-rpc-server llama-cpp-grpc
#    llama-ggml gpt4all rwkv tinydream bert-embeddings huggingface stablediffusion
# enabled backends
_GRPC_BACKENDS="backend-assets/grpc/local-store \
$_OPTIONAL_GRPC"

_pkgbase="localai"
pkgbase="${_pkgbase}-git"
pkgname=()
pkgver=2.23.0.136.g9c9359fc
pkgrel=1
pkgdesc="Self-hosted OpenAI API alternative - Open Source, community-driven and local-first."
url="https://github.com/mudler/LocalAI"
license=('MIT')
arch=('x86_64')
provides=('localai' "local-ai=${pkgver}")
conflicts=('localai' 'local-ai')
backup=("etc/${_pkgbase}/${_pkgbase}.conf")

source=(
  "${_pkgbase}"::"git+https://github.com/mudler/LocalAI"
  "README.md"
  "${_pkgbase}.conf"
  "${_pkgbase}.service"
  "${_pkgbase}.tmpfiles"
  "${_pkgbase}.sysusers"
)

sha256sums=(
  'SKIP'
  'SKIP'
  'SKIP'
  'SKIP'
  'SKIP'
  'SKIP'
)

depends=(
  'protobuf'
  'grpc'
)

# system wide dependencies for python backends
_python_depends=(
    'python-protobuf'
    'python-grpcio'
    'python-certifi'
    'python-pillow'
    'python-opencv'
    'python-numpy'
    'python-pytorch'
    'python-torchaudio'
    'python-torchvision'
    'python-transformers'
    'python-sentencepiece'
    'python-peft'
    'python-accelerate'
  )

# python backends and their local dependencies
_python_backends=(
  "bark bark"
  "coqui coqui-tts"
  "diffusers diffusers compel optimum-quanto"
  "parler-tts llvmlite"
  "rerankers rerankers[transformers]"
  "sentencetransformers sentence-transformers"
  "transformers"
  "transformers-musicgen"
  "vall-e-x"
)

makedepends=(
  'go'
  'git'
  'cmake'
  'opencv'
  'openmpi'
  'blas-openblas'
  'sdl2'
  'ffmpeg'
  'upx'
  'protoc-gen-go'
  'protoc-gen-go-grpc'
  'python-protobuf'
  'python-grpcio'
  'python-grpcio-tools'
)

if [[ $_ENABLE_PIPER = 1 ]]; then
  depends+=(
    'espeak-ng'
  )
  makedepends+=(
    'onnxruntime'
    'libucd-git'
  )
fi

if [[ $_ENABLE_CPU = 1 ]]; then
  pkgname+=("${pkgbase}")
  if [[ $_ENABLE_PYTHON = 1 ]]; then
    pkgname+=("${pkgbase}-python")
  fi
fi

if [[ $_ENABLE_CUDA = 1 ]]; then
  pkgname+=("${pkgbase}-cuda")
  if [[ $_ENABLE_PYTHON = 1 ]]; then
    pkgname+=("${pkgbase}-cuda-python")
  fi
  makedepends+=(
    'cuda'
    'cudnn'
    'nccl'
    'magma-cuda'
  )
fi

if [[ $_ENABLE_ROCM = 1 ]]; then
  pkgname+=("${pkgbase}-rocm")
  if [[ $_ENABLE_PYTHON = 1 ]]; then
    pkgname+=("${pkgbase}-rocm-python")
  fi
  makedepends+=(
    'rocm-hip-sdk'
    'miopen-hip'
    'rccl'
    'magma-hip'
  )
fi


pkgver() {
  cd "${srcdir}/${_pkgbase}"
  (git describe --always --tags | tr "-" "." | tail -c +2)
}

prepare() {
  cd "${srcdir}/${_pkgbase}"

  # display config
  cat - << EOF

prepare():

Build Options:

_ENABLE_CPU=$_ENABLE_CPU
_ENABLE_CUDA=$_ENABLE_CUDA
_ENABLE_ROCM=$_ENABLE_ROCM
_ENABLE_PYTHON=$_ENABLE_PYTHON

_ENABLE_PARALLEL=$_ENABLE_PARALLEL
_ENABLE_PIPER=$_ENABLE_PIPER
_ENABLE_WHISPER=$_ENABLE_WHISPER

_OPTIONAL_MAKE_ARGS=$_OPTIONAL_MAKE_ARGS
_EXTERNAL_SOURCES=$_EXTERNAL_SOURCES
_DISABLED_MOD_EDIT=$_DISABLED_MOD_EDIT
_OPTIONAL_GRPC=$_OPTIONAL_GRPC
_GRPC_BACKENDS=$_GRPC_BACKENDS


EOF

  # ### modify Makefile
  # remove unused sources from get-sources
  sed -ri "s#get-sources: .*#get-sources: $_EXTERNAL_SOURCES#g" Makefile
  # remove go mod edits for inactive backend sources
  for i in $_DISABLED_MOD_EDIT; do
    sed -ri 's#.+\-replace github.com/'$i'.+##g' Makefile
  done

  # fetch sources of backends to be recursive git checked out before build()
  mkdir -p "sources"
  make $_OPTIONAL_MAKE_ARGS $_EXTERNAL_SOURCES

  # patch -N -i "${srcdir}/libbackend.patch" -p1

  if [[ $_ENABLE_PIPER = 1 ]]; then
    # fix piper build
    true
    # mkdir -p "sources/go-piper/piper-phonemize/pi/lib"
    # touch "sources/go-piper/piper-phonemize/pi/lib/keep"
    # sed -ri 's#(\$\(MAKE\) -C sources/go-piper libpiper_binding.a) example/main#\1#g' Makefile
  fi

  # copy for different build types
  cd "${srcdir}"
  for n in "${_pkgbase}-cpu" "${_pkgbase}-cuda" "${_pkgbase}-rocm"; do
    if test -d "$n"; then rm -rf "$n"; fi
    cp -r "${_pkgbase}" "$n"
  done

  # ROCM fixes
  cd "${srcdir}/${_pkgbase}-rocm"
  # fix llama and whisper build: --offload-arch, is deprecated, replace it with -DGPU_TARGETS
  # for i in \
  #   backend/cpp/llama/llama.cpp/Makefile \
  #   sources/whisper.cpp/Makefile; do
  #     mkdir -p $(dirname $i); touch $i; 
  #     sed -ri 's/^(.+HIPFLAGS.+\+=).+offload-arch=.+$/\1 -DGPU_TARGETS="$(GPU_TARGETS)"/g' "$i"
  # done
}

_build() {
  # generate grpc protobuf files for python and copy to backend-assets
  make BUILD_TYPE="$1" protogen-python
  mkdir -p backend-assets/grpc
  cp -a backend/python backend-assets/grpc/python

  if test "$1" = "cublas"; then
    _LLAMA_CPP_BACKEND="backend-assets/grpc/llama-cpp-cuda"
  elif test "$1" = "hipblas"; then
    _LLAMA_CPP_BACKEND="backend-assets/grpc/llama-cpp-hipblas"
  else
    _LLAMA_CPP_BACKEND="backend-assets/grpc/llama-cpp-avx2"
  fi

  cat - << EOF


_build($1):
GO_TAGS=$_GO_TAGS
OPTIONAL_MAKE_ARGS=$_OPTIONAL_MAKE_ARGS
LLAMA_BACKEND=$_LLAMA_CPP_BACKEND
OTHER_GRPC_BACKENDS=$_GRPC_BACKENDS

EOF

  _nproc=1
  if [[ $_ENABLE_PARALLEL = 1 ]]; then
    # use number of physical cores for parallel build
    _nproc=$(grep  "^core id" /proc/cpuinfo | sort -n | uniq | wc -l)
  fi

  make -j"$_nproc" \
    BUILD_TYPE="$1" \
    GRPC_BACKENDS="$_LLAMA_CPP_BACKEND $_GRPC_BACKENDS" \
    GO_TAGS="$_GO_TAGS" $_OPTIONAL_MAKE_ARGS build
}

build() {
  if [[ $_ENABLE_CPU = 1 ]]; then
    cd "${srcdir}/${_pkgbase}-cpu"
    _build openblas
  fi

  if [[ $_ENABLE_CUDA = 1 ]]; then
    cd "${srcdir}/${_pkgbase}-cuda"
    export CUDA_HOME="${CUDA_HOME:-/opt/cuda}"
    export PATH="$CUDA_HOME/bin:$PATH"
    MAGMA_HOME="$CUDA_HOME/targets/x86_64-linux" CUDA_LIBPATH="$CUDA_HOME/lib64/" \
      _build cublas
  fi

  if [[ $_ENABLE_ROCM = 1 ]]; then
    cd "${srcdir}/${_pkgbase}-rocm"
    export ROCM_HOME="${ROCM_HOME:-/opt/rocm}"
    export ROCM_VERSION="$(cat $ROCM_HOME/.info/version)"
    export PATH="$ROC_HOME/bin:$PATH"
    echo "CXX=$CXX , CC=$CC"

    # fix build error on ROCM by removing unsupported cf-protection from CMAKE_CXX_FLAGS
    CXXFLAGS="$CXXFLAGS -fcf-protection=none" MAGMA_HOME="$ROCM_HOME" \
      AMDGPU_TARGETS="$_AMDGPU_TARGETS" GPU_TARGETS="$_AMDGPU_TARGETS" \
        _build hipblas
  fi
}

_package_install() {
  install -Dm755 "local-ai" "${pkgdir}/usr/bin/localai"
  ln -s "/usr/bin/localai" "${pkgdir}/usr/bin/local-ai"
  install -Dm644 LICENSE -t "${pkgdir}/usr/share/licenses/${_pkgbase}"
  install -Dm644 README.md -t "${pkgdir}/usr/share/doc/${_pkgbase}"
  install -Dm644 "${srcdir}/README.md" "${pkgdir}/usr/share/doc/${_pkgbase}/README-build.md"
  install -Dm644 ${srcdir}/${_pkgbase}.conf -t "${pkgdir}/etc/${_pkgbase}"
  _python_backends_str=""
  if [[ $_IS_PYTHON_FLAVOR = 1 ]]; then
    _python_backends_str=$(printf "%s\n" "${_python_backends[@]}")
  fi
  echo "ARCH_LOCALAI_PYTHON_BACKENDS=\"${_python_backends_str}\"" \
      > "${pkgdir}/etc/${_pkgbase}/python_backends.conf"
  install -Dm644 ${srcdir}/${_pkgbase}.service -t "${pkgdir}/usr/lib/systemd/system"
  install -Dm644 ${srcdir}/${_pkgbase}.sysusers "${pkgdir}/usr/lib/sysusers.d/${_pkgbase}.conf"
  install -Dm644 ${srcdir}/${_pkgbase}.tmpfiles "${pkgdir}/usr/lib/tmpfiles.d/${_pkgbase}.conf"
}

package_localai-git() {
  cd "${srcdir}/${_pkgbase}-cpu"
  depends+=('openblas')
  if [[ $_ENABLE_PIPER = 1 ]]; then depends+=('onnxruntime'); fi
  if [[ $_IS_PYTHON_FLAVOR = 1 ]]; then depends+=("${_python_depends[@]}"); fi
  _package_install
}

package_localai-git-cuda() {
  cd "${srcdir}/${_pkgbase}-cuda"
  pkgdesc+=' (with CUDA support)'
  depends+=('cuda')
  if [[ $_ENABLE_PIPER = 1 ]]; then depends+=('onnxruntime'); fi
  if [[ $_IS_PYTHON_FLAVOR = 1 ]]; then depends+=("${_python_depends[@]}"); depends+=('python-pytorch-cuda'); fi
  _package_install
}

package_localai-git-rocm() {
  cd "${srcdir}/${_pkgbase}-rocm"
  pkgdesc+=' (with ROCM support)'
  depends+=('rocm-hip-runtime' 'hipblas' 'rocblas')
  if [[ $_ENABLE_PIPER = 1 ]]; then depends+=('onnxruntime'); fi
  if [[ $_IS_PYTHON_FLAVOR = 1 ]]; then depends+=("${_python_depends[@]}"); depends+=('python-pytorch-rocm'); fi
  _package_install
}

package_localai-git-python() {
  _IS_PYTHON_FLAVOR=1
  package_localai-git "$@"
}

package_localai-git-cuda-python() {
  _IS_PYTHON_FLAVOR=1
  package_localai-git-cuda "$@"
}

package_localai-git-rocm-python() {
  _IS_PYTHON_FLAVOR=1
  package_localai-git-rocm "$@"
}