Skip to content

Commit cedcbf9

Browse files
fix(llama-cpp): retain CPU variants in GPU builds (#11255)
Build the runtime CPU variant set alongside x86 GPU backends so partial offload uses the host's SIMD kernels instead of the scalar fallback. Keep arm64 GPU images on the portable binary until their builders consistently provide gcc-14. Assisted-by: Codex:gpt-5 Co-authored-by: localai-org-maint-bot <306269227+localai-org-maint-bot@users.noreply.github.com>
1 parent 7e4a60c commit cedcbf9

4 files changed

Lines changed: 51 additions & 15 deletions

File tree

.docker/llama-cpp-build-target.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
arch=${1:?target architecture is required}
5+
build_type=${2-}
6+
7+
# GPU arm64 base images do not consistently provide the gcc-14 toolchain needed
8+
# to compile ggml's armv9.2 CPU variants. Keep their portable fallback until the
9+
# builder images can supply that compiler.
10+
if [ "$arch" = "arm64" ] && [ -n "$build_type" ]; then
11+
echo llama-cpp-fallback
12+
else
13+
echo llama-cpp-cpu-all
14+
fi

.docker/llama-cpp-compile.sh

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ if [[ -n "${CUDA_DOCKER_ARCH:-}" ]]; then
1818
fi
1919

2020
cd /LocalAI/backend/cpp/llama-cpp
21-
if [ -z "${BUILD_TYPE:-}" ]; then
22-
# Pure CPU image (BUILD_TYPE empty): one build with ggml CPU_ALL_VARIANTS replaces the
23-
# per-microarch binaries (x86: avx/avx2/avx512/fallback; arm64: armv8.x/armv9.x). ggml
24-
# dlopens the best libggml-cpu-*.so at runtime by probing host CPU features.
21+
BUILD_TARGET=$(/LocalAI/.docker/llama-cpp-build-target.sh "${TARGETARCH}" "${BUILD_TYPE:-}")
22+
if [ "$BUILD_TARGET" = "llama-cpp-cpu-all" ]; then
23+
# One build with ggml CPU_ALL_VARIANTS replaces the per-microarch binaries (x86:
24+
# avx/avx2/avx512/fallback; arm64: armv8.x/armv9.x). BUILD_TYPE remains in the
25+
# environment, so GPU builds retain their accelerator backend while ggml dlopens the
26+
# best CPU library when work is offloaded to the host.
2527
#
2628
# arm64: the CPU_ALL_VARIANTS table includes armv9.2 SME variants whose -march=...+sme is
2729
# rejected by the Ubuntu 24.04 default gcc-13. gcc-14 accepts it, so build the arm64
@@ -35,14 +37,8 @@ if [ -z "${BUILD_TYPE:-}" ]; then
3537
apt-get update -qq && apt-get install -y -qq gcc-14 g++-14
3638
export CC=gcc-14 CXX=g++-14
3739
fi
38-
make llama-cpp-cpu-all
39-
else
40-
# GPU build (cublas/hipblas/sycl/vulkan/...): the accelerator does the compute, so a
41-
# single fallback CPU build is enough - no per-microarch CPU variants needed. (This also
42-
# keeps the heavy GPU backend compile from also building the whole CPU variant matrix,
43-
# and avoids the gcc-14 apt step on GPU base images such as nvidia l4t.)
44-
make llama-cpp-fallback
4540
fi
41+
make "$BUILD_TARGET"
4642
make llama-cpp-grpc
4743
make llama-cpp-rpc-server
4844

backend/cpp/llama-cpp/run.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ grep -e "flags" /proc/cpuinfo | head -1
1212

1313
BINARY=llama-cpp-fallback
1414

15-
# CPU images (x86, arm64, darwin) ship a single llama-cpp-cpu-all built with ggml
15+
# CPU images and x86 GPU images ship a single llama-cpp-cpu-all built with ggml
1616
# CPU_ALL_VARIANTS: ggml's backend registry dlopens the best libggml-cpu-*.so for this
17-
# host, so no shell-side AVX probing. GPU images (cublas/sycl/vulkan/hipblas) ship only
18-
# llama-cpp-fallback (the accelerator does the compute), so fall back to it when absent.
17+
# host, so no shell-side AVX probing. GPU arm64 images still ship llama-cpp-fallback
18+
# until their builder toolchains support ggml's complete arm variant matrix.
1919
if [ -e "$CURDIR"/llama-cpp-cpu-all ]; then
2020
BINARY=llama-cpp-cpu-all
2121
fi
@@ -76,4 +76,4 @@ echo "Using binary: $BINARY"
7676
exec "$CURDIR"/$BINARY "$@"
7777

7878
# We should never reach this point, however just in case we do, run fallback
79-
exec "$CURDIR"/llama-cpp-fallback "$@"
79+
exec "$CURDIR"/llama-cpp-fallback "$@"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
CURDIR=$(dirname "$(realpath "$0")")
5+
SELECTOR="$CURDIR/../../.docker/llama-cpp-build-target.sh"
6+
7+
assert_target() {
8+
local arch=$1
9+
local build_type=$2
10+
local expected=$3
11+
local actual
12+
13+
actual=$("$SELECTOR" "$arch" "$build_type")
14+
if [ "$actual" != "$expected" ]; then
15+
echo "FAIL: $arch/$build_type selected $actual, expected $expected"
16+
exit 1
17+
fi
18+
}
19+
20+
assert_target amd64 cublas llama-cpp-cpu-all
21+
assert_target amd64 vulkan llama-cpp-cpu-all
22+
assert_target amd64 "" llama-cpp-cpu-all
23+
assert_target arm64 cublas llama-cpp-fallback
24+
assert_target arm64 "" llama-cpp-cpu-all
25+
26+
echo "PASS: llama.cpp build target preserves CPU variants where supported"

0 commit comments

Comments
 (0)