From ac3c3936edcf363eb09ca63bcdc2a2b78d9d175c Mon Sep 17 00:00:00 2001 From: IsuraManchanayake Date: Fri, 20 Nov 2026 05:22:29 +1100 Subject: [PATCH] install.sh, README: say what the XDNA2 NPU still needs on AMD From 0.5.3 the Linux/x86_64 engine uses the machine's XDNA2 NPU beside the GPU while it reads a prompt, when the NPU's driver and firmware (/dev/accel/accel0), XRT with its NPU plugin and a locked-memory limit of 64 MiB or more are there. Without them it runs on the GPU alone. On a machine with an NPU (the device node, or PCI 1022:17f0 without one) the installer now names each missing piece in its closing notes, with the command or file that adds it. It says nothing on a machine without an NPU, with everything there, or for a release whose bundle has no NPU module, and it never stops because of the NPU. The checks look under BASERT_INSTALL_TEST_ROOT, as the ROCm ones do. --- README.md | 6 + docs/getting-started/installation.md | 22 +++ install.sh | 106 ++++++++++++- tests/scripts/test_packaging.py | 213 ++++++++++++++++++++++++++- 4 files changed, 345 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 16ce422..19c385e 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,12 @@ sudo apt install amdrocm-blas10.0-gfx1151 sudo usermod -aG render "$USER" # access to the GPU; log in again afterwards ``` +From 0.5.3 the engine also uses the XDNA2 NPU beside the GPU to read prompts +faster, when the machine has the NPU's driver (`/dev/accel/accel0`), AMD's XRT +(`sudo apt install libxrt2 libxrt-npu2` on Ubuntu 26.04) and an unlimited +locked-memory limit. Without them it runs on the GPU alone; the installer says +which are missing ([details](docs/getting-started/installation.md#the-npu-on-amd)). + ## Quickstart ### 1. Install diff --git a/docs/getting-started/installation.md b/docs/getting-started/installation.md index b34222e..8894a4b 100644 --- a/docs/getting-started/installation.md +++ b/docs/getting-started/installation.md @@ -44,6 +44,28 @@ The install script checks for the runtime, the BLAS kernels and access to `/dev/kfd`, and prints the commands for your machine when something is missing. It does not run `apt` itself. +### The NPU on AMD + +From 0.5.3 the engine also uses the XDNA2 NPU of these machines beside the GPU +while it reads a prompt. This is optional: without any of the following the +engine prints one `[npu_ffn] the prefill stays on the GPU alone: ...` line when +it loads a model and runs on the GPU as before. + +- **The NPU's kernel driver and firmware**, which make `/dev/accel/accel0`: + the `amdxdna` driver (in Linux 6.14 and later; Ubuntu 26.04's kernel has it) + and the `amdnpu` firmware from `linux-firmware`. The device belongs to the + `render` group, like the GPU. +- **XRT, AMD's runtime for the NPU, with its NPU plugin.** On Ubuntu 26.04: + `sudo apt install libxrt2 libxrt-npu2`. +- **An unlimited locked-memory limit**, because XRT locks a 64 MiB heap. Check + with `ulimit -l`. For logins, put `* - memlock unlimited` in a file under + `/etc/security/limits.d/` and log in again; for a systemd service, set + `LimitMEMLOCK=infinity`. A session that does not pass through PAM keeps the + limit of the service that started it. + +On a machine with an NPU the install script checks these three and prints what +is missing, with the command or file for each. It changes none of them itself. + ## One-shot install (recommended) ```sh diff --git a/install.sh b/install.sh index 41ee2da..b6b578a 100755 --- a/install.sh +++ b/install.sh @@ -24,7 +24,7 @@ # BASERT_NO_MODIFY_PATH set to 1 to skip editing shell profiles # # For the installer's tests only: BASERT_INSTALL_TEST_ROOT, a folder that -# stands in for / where the AMD GPU and ROCm checks look. +# stands in for / where the AMD GPU, ROCm and NPU checks look. set -eu REPO="basecompute/baseRT" @@ -79,6 +79,105 @@ rocm_steps() { echo "sudo apt install amdrocm-blas10.0-$1" } +# From 0.5.3 the Linux/x86_64 engine also uses the machine's XDNA2 NPU beside +# the GPU while it reads a prompt; a release that does has the module +# $NPU_MODULE in its bundle. The NPU is optional. It needs the kernel's amdxdna +# driver with the NPU's firmware (they make /dev/accel/accel0), AMD's XRT +# runtime with its NPU plugin, and a locked-memory limit that lets XRT lock +# its 64 MiB heap; without any of them the engine runs on the GPU alone. This +# installer says which are missing and how to add them, and changes none. +NPU_MODULE="baseRT_rocm_npu.so" +NPU_NODE="/dev/accel/accel0" +npu_missing="" +npu_engine=0 + +# The PCI revision of this machine's XDNA2 NPU (1022:17f0), which also names +# its firmware folder: 10 on Strix Point, 11 on Strix Halo. Nothing without +# one. +npu_pci_revision() { + for dev in "$sysroot"/sys/bus/pci/devices/*; do + [ "$(cat "$dev/vendor" 2>/dev/null)" = 0x1022 ] || continue + [ "$(cat "$dev/device" 2>/dev/null)" = 0x17f0 ] || continue + rev="$(sed 's/^0x//' "$dev/revision" 2>/dev/null || true)" + echo "${rev:-00}" + return 0 + done +} + +# "yes" when the dynamic loader finds a library ($1): in LD_LIBRARY_PATH, in +# the folders /etc/ld.so.conf and ld.so.conf.d name, or in its own. +loader_finds() { + { + printf '%s\n' "${LD_LIBRARY_PATH:-}" | tr ':' '\n' + cat "$sysroot/etc/ld.so.conf" "$sysroot"/etc/ld.so.conf.d/*.conf 2>/dev/null \ + | sed -n 's|^\(/[^[:space:]#]*\).*|\1|p' + printf '%s\n' /lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu /lib64 /usr/lib64 /lib /usr/lib + } | while IFS= read -r dir; do + if [ -n "$dir" ] && [ -e "$sysroot$dir/$1" ]; then echo yes; break; fi + done +} + +# What this machine still needs before the engine can use its NPU: a "- " line +# for each missing piece, and under it the command or file that adds it. +# Nothing on a machine without an NPU, or with everything there. +npu_steps() { + rev="$(npu_pci_revision)" || true + if [ ! -e "$sysroot$NPU_NODE" ]; then + [ -n "$rev" ] || return 0 # no NPU in this machine + fw="amdnpu/17f0_$rev" + if [ ! -d "$sysroot/sys/bus/pci/drivers/amdxdna" ]; then + echo "- the NPU's kernel driver, amdxdna, is not loaded, so there is no $NPU_NODE. Linux 6.14" + echo " and later have the driver (Ubuntu 26.04's kernel does); where the kernel has it:" + echo " sudo modprobe amdxdna" + elif [ ! -d "$sysroot/lib/firmware/$fw" ] && [ ! -d "$sysroot/usr/lib/firmware/$fw" ]; then + echo "- the NPU's firmware is not installed, so its driver made no $NPU_NODE. A current" + echo " linux-firmware package has it (Ubuntu 26.04's does), in:" + echo " /lib/firmware/$fw" + else + echo "- the NPU's driver, amdxdna, is loaded but made no $NPU_NODE. The kernel log says why:" + echo " sudo dmesg | grep amdxdna" + fi + elif [ ! -r "$sysroot$NPU_NODE" ] || [ ! -w "$sysroot$NPU_NODE" ]; then + echo "- you cannot open $NPU_NODE, the NPU. Add yourself to the render group and log in again:" + echo " sudo usermod -aG render \"\$USER\"" + fi + # The engine's NPU module loads libxrt_coreutil.so.2, and XRT its own NPU + # plugin. Ubuntu 26.04 packages both; an older release has neither. + case "$(os_release ID) $(os_release VERSION_ID)" in + "ubuntu 26.04") from="" ;; + *) from=" Ubuntu 26.04 has it as:" ;; + esac + if [ -z "$(loader_finds libxrt_coreutil.so.2)" ]; then + echo "- XRT, the runtime the engine reaches the NPU through, is not installed (libxrt_coreutil.so.2).$from" + echo " sudo apt install libxrt2 libxrt-npu2" + elif [ -z "$(loader_finds libxrt_driver_xdna.so.2)" ]; then + echo "- XRT is installed without its NPU plugin (libxrt_driver_xdna.so.2).$from" + echo " sudo apt install libxrt-npu2" + fi + # In bytes, or "unlimited"; what a program started from this shell gets. + limit="$(sed -n 's/^Max locked memory *\([^ ]*\) .*/\1/p' "$sysroot/proc/self/limits" 2>/dev/null || true)" + case "$limit" in + "" | *[!0-9]*) ;; + *) + if [ "${#limit}" -le 9 ] && [ "$limit" -lt 67108864 ]; then + echo "- the locked-memory limit here is $((limit / 1024)) KiB, and XRT locks a 64 MiB heap for the NPU. For" + echo " logins, add this file and log in again:" + echo " echo '* - memlock unlimited' | sudo tee /etc/security/limits.d/99-basert-npu.conf" + echo " For a systemd service, in its [Service] section:" + echo " LimitMEMLOCK=infinity" + fi ;; + esac +} + +# The steps above, for a release whose engine uses the NPU. +npu_report() { + [ -n "$npu_missing" ] || return 0 + echo + echo " Optional: this machine has an XDNA2 NPU, which the engine uses beside the GPU to read" + echo " prompts faster. BaseRT works without it, on the GPU alone. To use the NPU:" + printf '%s\n' "$npu_missing" | sed 's/^/ /' +} + device_hint="" # The AMD GPUs' LLVM targets (gfx1151, ...), one per line, from the amdgpu @@ -250,6 +349,8 @@ case "$os $arch" in warn "are not there. Install them from AMD's ROCm 10 apt repository:" printf '%s\n' "$rocm_missing" | while IFS= read -r line; do warn " $line"; done fi + # Never a reason to stop: the engine runs without the NPU. + npu_missing="$(npu_steps 2>/dev/null)" || true ASSET_PREFIX="basert-engine-linux-x64-rocm" ;; *) err "unsupported platform: $os/$arch. BaseRT ships macOS/arm64 (Metal), Linux/arm64 (NVIDIA CUDA) and Linux/x86_64 (AMD ROCm)." ;; @@ -274,6 +375,7 @@ tag="$(printf '%s' "$json" | grep -o '"tag_name"[^,]*' | grep -o '"[^"]*"$' | tr if [ "${BASERT_FORCE:-0}" != "1" ] && [ -x "$INSTALL_DIR/basert" ] \ && [ -f "$STAMP" ] && [ "$(cat "$STAMP" 2>/dev/null || true)" = "$tag" ]; then say "already on $tag at $INSTALL_DIR — nothing to do (set BASERT_FORCE=1 to reinstall)." + if [ -e "$INSTALL_DIR/$NPU_MODULE" ]; then npu_report; fi exit 0 fi @@ -299,6 +401,7 @@ mkdir "$stage" trap 'rm -rf "$tmp" "$stage"' EXIT tar -xzf "$tmp/bundle.tar.gz" -C "$stage" chmod +x "$stage"/basert* 2>/dev/null || true +if [ -e "$stage/$NPU_MODULE" ]; then npu_engine=1; fi # From 0.4.3 the launcher places a release itself: under versions/, the links in # $INSTALL_DIR turned to it in one step, the copy there before kept. A release @@ -394,6 +497,7 @@ if [ -n "$device_hint" ]; then echo " The engine runs on HIP device 0, which is not your $gpu; make the $gpu device 0:" echo " $device_hint" fi +if [ "$npu_engine" = 1 ]; then npu_report; fi echo echo " Try it:" echo " basert pull basecompute/Qwen3-0.6B" diff --git a/tests/scripts/test_packaging.py b/tests/scripts/test_packaging.py index 9ee0873..bead62d 100644 --- a/tests/scripts/test_packaging.py +++ b/tests/scripts/test_packaging.py @@ -685,7 +685,8 @@ def test_each_bundle_makes_the_wheel_for_its_platform(self) -> None: @unittest.skipUnless(INSTALL_SH.is_file(), "install.sh is in the open repo only") class InstallScriptTests(unittest.TestCase): """install.sh, run with stand-ins for uname and curl and a folder standing - in for / (BASERT_INSTALL_TEST_ROOT) where the AMD GPU and ROCm checks look.""" + in for / (BASERT_INSTALL_TEST_ROOT) where the AMD GPU, ROCm and NPU checks + look.""" V = "1.2.3" UNAME = '#!/bin/sh\ncase "$1" in -s) echo "$STUB_OS" ;; -m) echo "$STUB_ARCH" ;; *) exit 1 ;; esac\n' @@ -965,6 +966,216 @@ def test_a_kfd_this_user_cannot_open_is_pointed_out(self) -> None: self.assertEqual(run.returncode, 0, run.stderr) self.assertIn("render", run.stderr) + # --- the XDNA2 NPU: optional, so every run below installs and exits 0 --- + + NPU_INTRO = "Optional: this machine has an XDNA2 NPU" + XRT_LIBS = "usr/lib/x86_64-linux-gnu" + + def npu_release(self) -> None: + """The ROCm bundle of a release whose engine uses the NPU: it has the NPU module.""" + name = "basert-engine-linux-x64-rocm" + refuse = b"#!/bin/sh\nexit 1\n" + bundle( + self.assets / f"{name}-{self.V}.tar.gz", + {"basert": refuse, "basert-chat": refuse, f"lib-{name}": b"engine", "baseRT_rocm_npu.so": b"npu module"}, + {}, + ) + + def npu(self, node: bool = True, pci: bool = True, driver: bool = True, firmware: bool = True, rev: str = "0x11") -> None: + """A Strix machine's NPU as the kernel shows it: the PCI device (beside + another AMD one of the same class), the amdxdna driver, the firmware + folder for that revision and /dev/accel/accel0.""" + devices = self.root / "sys/bus/pci/devices" + ids = [("0000:c4:00.7", "0x1022", "0x164a", "0x00")] + if pci: + ids.append(("0000:c5:00.1", "0x1022", "0x17f0", rev)) + for slot, vendor, device, revision in ids: + (devices / slot).mkdir(parents=True) + for name, value in (("vendor", vendor), ("device", device), ("revision", revision), ("class", "0x118000")): + (devices / slot / name).write_text(value + "\n") + if driver: + (self.root / "sys/bus/pci/drivers/amdxdna").mkdir(parents=True) + if firmware: + (self.root / "lib/firmware/amdnpu" / f"17f0_{rev[2:]}").mkdir(parents=True) + if node: + (self.root / "dev/accel").mkdir(parents=True, exist_ok=True) + (self.root / "dev/accel/accel0").write_text("") + + def xrt(self, plugin: bool = True, where: str = XRT_LIBS) -> None: + """What `apt install libxrt2` (and libxrt-npu2, the plugin) put where the loader looks.""" + lib = self.root / where + lib.mkdir(parents=True, exist_ok=True) + for name in ["libxrt_coreutil"] + (["libxrt_driver_xdna"] if plugin else []): + (lib / f"{name}.so.2.21.75").write_text("xrt") + os.symlink(f"{name}.so.2.21.75", lib / f"{name}.so.2") + + def memlock(self, soft: str) -> None: + """/proc/self/limits with this soft locked-memory limit (bytes, or "unlimited").""" + rows = (("Limit", "Soft Limit", "Hard Limit", "Units"), ("Max cpu time", "unlimited", "unlimited", "seconds"), + ("Max locked memory", soft, "unlimited", "bytes"), ("Max address space", "unlimited", "unlimited", "bytes")) + (self.root / "proc/self").mkdir(parents=True, exist_ok=True) + (self.root / "proc/self/limits").write_text("".join(f"{a:<26}{b:<21}{c:<21}{d:<10}\n" for a, b, c, d in rows)) + + def strix(self, release: tuple[str, str] | None = ("ubuntu", "26.04")) -> None: + """A new Strix Halo with ROCm, to which a test adds the NPU's pieces, and a release that uses the NPU.""" + self.fresh(110501) + if release: + self.os_release(*release) + self.npu_release() + + def npu_lines(self, run: subprocess.CompletedProcess) -> list[str]: + """The NPU part of the closing notes: from its first line to the next blank one, or the end.""" + self.assertEqual(run.returncode, 0, run.stderr) + self.assertTrue((self.dest / "lib-basert-engine-linux-x64-rocm").is_file()) + self.assertNotIn("NPU", run.stderr) + lines = run.stdout.splitlines() + at = next((i for i, line in enumerate(lines) if self.NPU_INTRO in line), None) + if at is None: + self.assertNotIn("NPU", run.stdout) + self.assertNotIn("XRT", run.stdout) + return [] + end = lines.index("", at) if "" in lines[at:] else len(lines) + self.assertIn("BaseRT works without it, on the GPU alone.", " ".join(lines[at:end])) + return [line.strip() for line in lines[at + 2:end]] + + def test_a_machine_without_an_npu_is_told_nothing_about_one(self) -> None: + # No XRT and a low limit: only the missing NPU keeps the installer quiet. + self.strix() + self.npu(node=False, pci=False, driver=False, firmware=False) + self.memlock("8388608") + self.assertEqual(self.npu_lines(self.install()), []) + + def test_an_npu_with_everything_it_needs_is_told_nothing(self) -> None: + for rev, limit in (("0x11", "unlimited"), ("0x10", str(64 << 20))): + with self.subTest(rev=rev, limit=limit): + self.strix() + self.npu(rev=rev) + self.xrt() + self.memlock(limit) + self.assertEqual(self.npu_lines(self.install()), []) + + def test_an_npu_without_xrt_is_told_its_apt_line(self) -> None: + self.strix() + self.npu() + self.memlock("unlimited") + lines = self.npu_lines(self.install()) + self.assertEqual(len(lines), 2, lines) + self.assertIn("XRT, the runtime the engine reaches the NPU through, is not installed (libxrt_coreutil.so.2).", lines[0]) + self.assertEqual(lines[1], "sudo apt install libxrt2 libxrt-npu2") + + def test_xrt_without_its_npu_plugin_is_told_the_plugins_package(self) -> None: + self.strix() + self.npu() + self.xrt(plugin=False) + self.memlock("unlimited") + lines = self.npu_lines(self.install()) + self.assertEqual(len(lines), 2, lines) + self.assertIn("without its NPU plugin (libxrt_driver_xdna.so.2)", lines[0]) + self.assertEqual(lines[1], "sudo apt install libxrt-npu2") + + def test_xrt_is_looked_for_where_the_loader_looks(self) -> None: + # Outside the loader's own folders XRT counts only when ld.so.conf.d + # or LD_LIBRARY_PATH names its folder. + self.strix() + self.npu() + self.xrt(where="opt/xrt/lib") + self.memlock("unlimited") + self.assertIn("sudo apt install libxrt2 libxrt-npu2", self.npu_lines(self.install())) + self.assertEqual(self.npu_lines(self.install(BASERT_FORCE="1", LD_LIBRARY_PATH="/nowhere:/opt/xrt/lib")), []) + conf = self.root / "etc/ld.so.conf.d" + conf.mkdir(parents=True) + (conf / "xrt.conf").write_text("# XRT\n/opt/xrt/lib\n") + self.assertEqual(self.npu_lines(self.install(BASERT_FORCE="1")), []) + + def test_another_release_is_told_where_xrt_is_packaged(self) -> None: + for release in (("ubuntu", "24.04"), None): + with self.subTest(release=release): + self.strix(release) + self.npu() + self.memlock("unlimited") + lines = self.npu_lines(self.install()) + self.assertEqual(len(lines), 2, lines) + self.assertTrue(lines[0].endswith("(libxrt_coreutil.so.2). Ubuntu 26.04 has it as:"), lines[0]) + self.assertEqual(lines[1], "sudo apt install libxrt2 libxrt-npu2") + + def test_a_low_locked_memory_limit_is_told_the_limits_file_and_the_service_line(self) -> None: + self.strix() + self.npu() + self.xrt() + self.memlock("8388608") + lines = self.npu_lines(self.install()) + self.assertIn("the locked-memory limit here is 8192 KiB, and XRT locks a 64 MiB heap", lines[0]) + self.assertEqual( + lines[2:], + ["echo '* - memlock unlimited' | sudo tee /etc/security/limits.d/99-basert-npu.conf", + "For a systemd service, in its [Service] section:", "LimitMEMLOCK=infinity"], + ) + + def test_npu_hardware_without_its_device_node_is_told_what_is_missing(self) -> None: + cases = ( + ({"driver": False, "firmware": False}, "kernel driver, amdxdna, is not loaded", "sudo modprobe amdxdna"), + ({"firmware": False}, "the NPU's firmware is not installed", "/lib/firmware/amdnpu/17f0_11"), + ({}, "is loaded but made no /dev/accel/accel0", "sudo dmesg | grep amdxdna"), + ) + for missing, what, fix in cases: + with self.subTest(missing=missing): + self.strix() + self.npu(node=False, **missing) + self.xrt() + self.memlock("unlimited") + lines = self.npu_lines(self.install()) + self.assertIn(what, lines[0]) + self.assertEqual(lines[-1], fix) + self.assertEqual(sum(line.startswith("- ") for line in lines), 1, lines) + + def test_every_missing_piece_gets_its_own_line(self) -> None: + self.strix() + self.npu(node=False, driver=False, firmware=False) + self.memlock("65536") + lines = self.npu_lines(self.install()) + self.assertEqual([line.split(",")[0].split(" is ")[0] for line in lines if line.startswith("- ")], + ["- the NPU's kernel driver", "- XRT", "- the locked-memory limit here"]) + + def test_a_release_without_the_npu_module_is_told_nothing(self) -> None: + # An engine before 0.5.3 does not use the NPU, whatever the machine has. + self.amd(0, 110501) + self.rocm("gfx1151") + self.npu() + self.memlock("8388608") + self.assertEqual(self.npu_lines(self.install()), []) + + def test_a_run_that_installs_nothing_still_names_the_npu_steps(self) -> None: + self.strix() + self.npu() + self.memlock("unlimited") + first = self.npu_lines(self.install()) + self.log.write_text("") + again = self.install() + self.assertIn("nothing to do", again.stdout) + self.assertEqual(self.downloaded(), []) + self.assertEqual(self.npu_lines(again), first) + self.assertIn("sudo apt install libxrt2 libxrt-npu2", first) + + @unittest.skipIf(os.geteuid() == 0, "root opens any file") + def test_an_npu_this_user_cannot_open_is_pointed_out(self) -> None: + self.strix() + self.npu() + self.xrt() + self.memlock("unlimited") + os.chmod(self.root / "dev/accel/accel0", 0) + lines = self.npu_lines(self.install()) + self.assertIn("you cannot open /dev/accel/accel0", lines[0]) + self.assertEqual(lines[1], 'sudo usermod -aG render "$USER"') + + def test_the_npu_is_not_looked_for_on_the_other_platforms(self) -> None: + self.npu() + self.memlock("8388608") + refuse = b"#!/bin/sh\nexit 1\n" + bundle(self.assets / f"basert-engine-macos-arm64-{self.V}.tar.gz", {"basert": refuse, "baseRT_rocm_npu.so": b"x"}, {}) + run = self.install("Darwin", "arm64") + self.assertEqual(run.returncode, 0, run.stderr) + self.assertNotIn("NPU", run.stdout + run.stderr) + def test_the_other_platforms_are_unchanged(self) -> None: run = self.install("Darwin", "arm64") self.assertEqual(run.returncode, 0, run.stderr)