Files
LocalAI/scripts/build/backend-run-intel-env_test.sh
Dimitris Karakasilis c089caf320 feat(sycl): make the intel llama.cpp backend self-contained on any host (#10991)
* feat(sycl): make the intel llama.cpp backend self-contained on any host

The SYCL backend shipped an incomplete oneAPI runtime AND relied on a
host-provided GPU driver, so it only ran inside the build container. On a
bare host it died with "libze_loader.so.1 / libdnnl.so.3: cannot open
shared object file", and even with the host's Intel driver installed it
SIGSEGV'd during SYCL init when the host driver was built against a newer
glibc than the backend's bundled loader (rolling-release distros).

package_intel_libs now bundles the complete, coherent oneAPI runtime
(the missing MKL ILP64 / sycl_blas / tbb_thread + oneDNN + the dlopen'd
UR adapters, plus a sweep of the backend binaries' own direct deps) and
the Intel GPU userspace driver (libze_intel_gpu + libigdrcl + IGC + gmm)
with its OpenCL ICD manifest, mirroring how package_vulkan_libs bundles
Mesa. run.sh points the Level Zero and OpenCL loaders at the bundled
driver, and install-base-deps.sh installs it in the SYCL build image.
Bundling the driver is safe across kernels because it talks to the host
i915/xe via the stable DRM UAPI (unlike NVIDIA's kernel-locked
userspace).

Validated on Arch (glibc 2.43, i915): the backend loads and runs on an
Iris Xe with no host Intel packages installed.

Assisted-by: Claude:claude-opus-4-8

Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>

* fix(sycl): install a driver that exists, and let the user choose their own

The driver install added earlier in this branch asked apt for
intel-level-zero-gpu, which is not a package in Ubuntu 24.04. apt fails
outright on an unknown name, so neither driver was installed, nothing was there
to copy, and the images carried no driver at all.

It now comes from Intel's own repository, which has 25.18 for this Ubuntu
release, against 23.43 from late 2023 in the Ubuntu archive. The archive driver
does not know any card released since, so a machine with a recent Intel GPU
would end up carrying a driver that cannot drive it. Anything that goes wrong
during that install fails the build on purpose: an unreachable repository is a
passing problem that a retry fixes, while quietly carrying a different driver,
or none, is a difference nobody would notice until a user reports an idle GPU.

run.sh used to overwrite whatever driver the user had chosen. Level Zero uses
only the driver it is given, so on a machine with a card too new for the
carried driver, the GPU would go unused with no way back. Both that setting and
the OpenCL one are now left alone when already set, and the docs say how to
point a backend at the machine's own driver.

The OpenCL setting also used to be applied whenever the backend held a driver
list, even when the driver it named had not been copied, which leaves OpenCL
with nothing instead of falling back to the machine's own driver. It now
requires the copied driver to be present, and the packaging leaves out the list
entry of any driver it did not copy. The oneAPI images list a processor-only
OpenCL library, which was being carried with nothing behind it.

Two more corrections in the packaging. The scan for libraries a program is
linked against only looked at files named llama-cpp-*, so turboquant and bonsai,
which are also built for Intel GPUs, were left with the incomplete set of
libraries this branch set out to fix; it now looks at every program in the
directory. And a build that should carry a driver but ends up without one now
says so, which is what a stale prebuilt base image looks like: such a backend
still runs on a machine that has its own driver, so nothing fails and the only
other symptom is a user reporting an idle GPU.

Backends now also ask the driver to report how much graphics memory is free,
without which llama.cpp reads zero on an integrated GPU, since such a chip
shares the system memory instead of having its own. turboquant and bonsai get
the same run.sh handling as llama.cpp.

The driver is only carried by the builds that start through run.sh, because
run.sh is what points Level Zero and OpenCL at it. The Python backends for
Intel GPUs start differently and would never load it, so they keep using the
machine's own driver rather than carrying several hundred megabytes they cannot
use.

Checked in a container on Ubuntu 24.04: the install brings driver 25.18 with
the files where the packaging expects them, an unreachable repository fails the
build, and the copied set resolves on its own once the machine's Intel packages
are moved away.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>

* fix(ci): rebuild every Linux backend when the GPU packaging script changes

scripts/build/package-gpu-libs.sh decides which GPU libraries end up inside an
image. The filter that builds the backend matrix listed it as an input of the
Python images only, so changing it rebuilt no Go and no C++ backend, even
though those run it from their own package.sh. A packaging fix aimed at the
Intel llama.cpp backend could merge and reach no image, which is the same
failure this rule was written to prevent.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>

* fix(sycl): carry only the driver Level Zero uses, not the OpenCL one

llama.cpp reaches an Intel GPU through Level Zero, which hands the driver
programs that are already compiled and so needs only the back end of the
graphics compiler. The OpenCL driver can be handed source code instead, so it
needs the compiler's front end as well, and that arrives with its own copy of
clang. Carrying it cost about 139 MB in every backend built for Intel GPUs, and
took the carried set from 123 MB to 261 MB.

Nothing here takes that path. No LocalAI code selects an OpenCL device, each
backend image holds one backend, and the documentation never described OpenCL
as a way to run models: the only mentions are a stale clblas row in the
BUILD_TYPE table, for a llama.cpp backend that no longer exists and that no
build matrix entry uses, and the sycl-ls troubleshooting hint. Before this
branch the packaging carried the OpenCL loader and adapter but no driver, so
the path could not work in a released image either. There is nobody to keep
working.

The driver list that OpenCL reads is no longer carried, and run.sh no longer
sets OCL_ICD_VENDORS, so OpenCL inside a container keeps using whatever the
image provides rather than being pointed at a directory with no driver in it.

Checked in a container against the real 25.18 driver: the carried set is 123 MB
with nothing unresolved, and Level Zero still reports the GPU with the
machine's own Intel packages moved out of the way. Neither the Level Zero
driver nor the compiler back end names the front end or clang among the
libraries it opens by name, so the leaner set is complete for this path.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>

---------

Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
Co-authored-by: localai-org-maint-bot <bot-opensource@localaisrl.com>
2026-07-31 23:39:53 +02:00

140 lines
4.7 KiB
Bash
Executable File

#!/bin/bash
# Checks how the run.sh of each C++ backend sets up the Intel graphics driver.
#
# A backend built for Intel GPUs carries its own copy of the Intel graphics
# driver. run.sh has to tell Level Zero, which is how llama.cpp reaches the
# card, to use that copy. Three things must hold, and all three have broken in
# the past:
#
# 1. If the user already chose a driver, keep the user's choice. Otherwise a
# machine with a graphics card too new for the carried driver stops
# working, with no way to get back to the driver that did work.
# 2. Say nothing about OpenCL. No OpenCL driver is carried, so pointing
# OpenCL at the backend's own directory would leave it with no driver at
# all, where saying nothing leaves it the machine's own.
# 3. Ask the driver for the amount of free memory. Without this, llama.cpp
# reads zero free memory on an integrated graphics chip, because such a
# chip has no memory of its own and shares the system's.
#
# The test builds a fake backend directory for each run.sh, runs it, and reads
# back the variables it exported.
set -euo pipefail
WORK=$(mktemp -d)
trap 'rm -rf "$WORK"' EXIT
REPO_ROOT=$(dirname "$(dirname "$(dirname "$(realpath "$0")")")")
RUN_SCRIPTS=(
"backend/cpp/llama-cpp/run.sh llama-cpp"
"backend/cpp/turboquant/run.sh turboquant"
"backend/cpp/bonsai/run.sh bonsai"
)
failures=0
fail() {
echo "FAIL: $*"
failures=$((failures + 1))
}
# Builds a fake backend directory: the real run.sh, a stand-in for the backend
# program that prints the variables we care about, and whichever libraries the
# caller asked for.
#
# Usage: make_backend <dir> <program-prefix> [library ...]
make_backend() {
local dir="$1" prefix="$2"
shift 2
mkdir -p "$dir/lib"
cp "$RUN_SH" "$dir/run.sh"
chmod +x "$dir/run.sh"
local lib
for lib in "$@"; do
: > "$dir/lib/$lib"
done
cat > "$dir/${prefix}-fallback" <<'PROGRAM'
#!/bin/bash
echo "level_zero_driver=${ZE_ENABLE_ALT_DRIVERS:-}"
echo "opencl_driver_list=${OCL_ICD_VENDORS:-}"
echo "report_free_memory=${ZES_ENABLE_SYSMAN:-}"
PROGRAM
chmod +x "$dir/${prefix}-fallback"
}
# Runs a fake backend and prints the one variable asked for.
# Usage: read_variable <dir> <name>
read_variable() {
local dir="$1" name="$2"
bash "$dir/run.sh" 2>/dev/null | sed -n "s/^${name}=//p"
}
for entry in "${RUN_SCRIPTS[@]}"; do
read -r script prefix <<< "$entry"
RUN_SH="$REPO_ROOT/$script"
if [ ! -f "$RUN_SH" ]; then
fail "$script does not exist"
continue
fi
# An Intel build with its own graphics driver: point Level Zero and OpenCL
# at the bundled copies and ask for the free memory reading.
bundled="$WORK/$prefix-bundled"
make_backend "$bundled" "$prefix" \
libze_loader.so.1 libze_intel_gpu.so.1 libigdrcl.so
mkdir -p "$bundled/etc/OpenCL/vendors"
echo "libigdrcl.so" > "$bundled/etc/OpenCL/vendors/intel.icd"
got=$(read_variable "$bundled" level_zero_driver)
if [ "$got" != "$bundled/lib/libze_intel_gpu.so.1" ]; then
fail "$script: expected Level Zero to use the bundled driver, got '$got'"
fi
# Even with an OpenCL driver and a driver list sitting in the backend, which
# is what an older packaging left behind, OpenCL must be left alone.
got=$(read_variable "$bundled" opencl_driver_list)
if [ -n "$got" ]; then
fail "$script: OpenCL was pointed at the backend's own directory ('$got')"
fi
got=$(read_variable "$bundled" report_free_memory)
if [ "$got" != "1" ]; then
fail "$script: expected the free memory reading to be turned on, got '$got'"
fi
# The user picked a driver already. Both choices must survive.
got=$(ZE_ENABLE_ALT_DRIVERS=/usr/lib/host-driver.so \
read_variable "$bundled" level_zero_driver)
if [ "$got" != "/usr/lib/host-driver.so" ]; then
fail "$script: the user's Level Zero driver was overwritten with '$got'"
fi
got=$(ZES_ENABLE_SYSMAN=0 read_variable "$bundled" report_free_memory)
if [ "$got" != "0" ]; then
fail "$script: the user's free memory setting was overwritten with '$got'"
fi
# A build for some other kind of graphics card. None of the Intel
# variables belong here.
other="$WORK/$prefix-other"
make_backend "$other" "$prefix" libcublas.so.12
for name in level_zero_driver opencl_driver_list report_free_memory; do
got=$(read_variable "$other" "$name")
if [ -n "$got" ]; then
fail "$script: $name was set on a build with no Intel libraries ('$got')"
fi
done
done
if [ "$failures" -gt 0 ]; then
echo "$failures check(s) failed"
exit 1
fi
echo "PASS: every run.sh sets up the Intel graphics driver correctly"