Skip to content

npu: ONEBIT_NPU_RUNTIME=xrt|own, the runtime the NPU code builds against - #367

Merged
bong-water-water-bong merged 1 commit into
mainfrom
npu/own-runtime-flag
Oct 9, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
npu/own-runtime-flag

Conversation

@bong-water-water-bong

@bong-water-water-bong bong-water-water-bong commented Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator

What

A build option that selects which runtime the engine's NPU code compiles against:

  • the previous default runtime selection, unchanged behaviour.
  • -DONEBIT_NPU_RUNTIME=own -DONEBIT_NPU_RT_DIR=<dir>: our own runtime from a private add-on directory (npu_rt.h, npu_rt.cpp), built here into the static library onebit_npu_rt. 1bit then links no external runtime library.

The NPU sources include one new shim header, npu/rt.h, which pulls in the runtime header plus namespace xrt = npu_rt;, so npu/lane.cpp, npu/forward/* and app/forward_serve.cpp build unchanged against both. This repository holds no runtime code: the option, the shim, two tests and a docs section.

Tests: npu_runtime_option (runs in CI: a bad value, or own without a runtime directory, must stop the configure with a message) and npu_rt_shim (NPU builds only: every runtime call the engine makes must compile against the selected runtime). Default build: all 26 ctests pass. ONEBIT_NPU_PRIVATE together with own prints a CMake warning (the private 35B add-on links prebuilt XRT libraries and cannot build against own yet).

Proof on strixhalo (fast-lane route, Qwen3-0.6B full ELFs, performance mode, each NPU run under a device lock and the thermal wrapper)

  • ldd build-own/1bit: pcre2, gomp, libstdc++, libm, libgcc_s, libc. build-xrt/1bit: adds the external runtime library.so.2 and libuuid. strace of an own run: 0 opens matching "xrt".
  • 1bit npu-run ... -n 32 --dump-logits: every own run (6) and every repeat XRT run gave logits bit-identical (cmp) to the first XRT run at 32/32 steps; tokens identical.
  • 1bit serve --device npu, 3 fixed prompts, temperature 0, 32 tokens, streamed: 32/32 tokens identical per prompt on both builds; all 33 streamed chunks identical.
  • tests/serve_e2e.sh <1bit> <model> npu: PASS on both builds ("Paris").
  • Decode, npu-run, three runs each (Tctl 69-71 C): xrt 82.4 / 86.2 / 89.5, median 86.2 tok/s; own 80.2 / 83.7 / 84.7, median 83.7 tok/s. Serve's own timing, median of the 3 prompts: xrt 80.3, own 82.7. Ranges overlap: neither build is claimed faster.
  • Per-token breakdown on own: 40 runlists, 11.7 ms execute-to-done each (28 layer runs + lm head), host work about 0.9 ms per token and overlapped; decode step 12.4 ms. There is no per-token reconfiguration cost on this route.

Not covered

  • The GGUF per-op forward route builds on own but was not run.
  • The private 35B lax add-on was not built against own (prebuilt XRT-linked libraries).
  • The runtime itself lives in the private add-on; its remaining gaps (control-packet buffers, preemption save/restore, multi-column ELFs) are tracked there and refused loudly at run time rather than half-configured.

🤖 Generated with Claude Code

The NPU sources (npu/lane.cpp, npu/forward, app/forward_serve.cpp) now include npu/rt.h instead of
XRT headers. With the default -DONEBIT_NPU_RUNTIME=xrt it includes XRT as before. With
-DONEBIT_NPU_RUNTIME=own -DONEBIT_NPU_RT_DIR=<dir> it includes a private add-on's npu_rt.h (an
XRT-compatible runtime over the amdxdna ioctls) and aliases namespace xrt = npu_rt; <dir>/npu_rt.cpp
is built into onebit_npu_rt and 1bit links no XRT. No runtime code is in this repository.

Tests: npu_runtime_option (CI, no XRT: a bad value or own without a runtime directory stops the
configure with a message) and npu_rt_shim (NPU builds: every runtime call the engine makes
compiles against the selected runtime). docs/npu.md, "XRT or the own runtime".

Measured on Strix Halo (fast lane, Qwen3-0.6B, same session): ldd 1bit lists no libxrt with own;
npu-run logits bit-identical to the xrt build at 32/32 greedy steps in 3 of 3 runs; serve: 3 prompts
x 32 greedy tokens identical; tests/serve_e2e.sh PASS on both; decode median of 3 npu-run 86.2 (xrt)
vs 83.7 (own) tok/s, serve timings median 80.3 (xrt) vs 82.7 (own) tok/s. Default build: all 26
ctests pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Runtime Selection Logic

The npu/rt.h header uses a preprocessor macro ONEBIT_NPU_RUNTIME_OWN to conditionally include either XRT headers or the custom runtime headers. This approach is correct for compile-time selection, but it's important to ensure that all necessary XRT-compatible APIs are fully implemented in the custom runtime (npu_rt.h and npu_rt.cpp) to avoid subtle runtime errors or missing functionality. The PR description mentions that the custom runtime mirrors XRT's class names and should be API-compatible, but without a detailed audit of all XRT APIs used, there's a risk of incomplete compatibility.

#if defined(ONEBIT_NPU_RUNTIME_OWN)
#define NPU_RT_XRT_COMPAT 1
#include <npu_rt.h>
namespace xrt = npu_rt;
#else
#include <xrt/xrt_bo.h>
#include <xrt/xrt_device.h>
#include <xrt/xrt_hw_context.h>
#include <xrt/xrt_kernel.h>
#include <xrt/experimental/xrt_elf.h>
#include <xrt/experimental/xrt_ext.h>
#include <xrt/experimental/xrt_kernel.h>
#include <xrt/experimental/xrt_module.h>
#endif
Runtime Directory Validation

The CMakeLists.txt file validates that ONEBIT_NPU_RT_DIR contains npu_rt.h and npu_rt.cpp when ONEBIT_NPU_RUNTIME=own. However, it does not validate that the specified directory actually exists or is accessible. If a user provides a non-existent path, the error message may be misleading or the build may fail later in an unclear way. It would be more robust to check for the existence of the directory itself.

if(ONEBIT_NPU_RUNTIME STREQUAL "own")
    if(NOT EXISTS "${ONEBIT_NPU_RT_DIR}/npu_rt.h" OR NOT EXISTS "${ONEBIT_NPU_RT_DIR}/npu_rt.cpp")
        message(FATAL_ERROR "ONEBIT_NPU_RUNTIME=own needs ONEBIT_NPU_RT_DIR with npu_rt.h and npu_rt.cpp; "
                            "ONEBIT_NPU_RT_DIR='${ONEBIT_NPU_RT_DIR}'")
    endif()

@bong-water-water-bong
bong-water-water-bong merged commit b9ae47c into main Oct 9, 2026
7 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the npu/own-runtime-flag branch October 9, 2026 09:29
bong-water-water-bong added a commit that referenced this pull request Oct 9, 2026
…s ONEBIT_NPU_RUNTIME=own (#368)

Only an xrt build needs XRT (#367). The repository variable
ONEBIT_NPU_RUNTIME mirrors the build we ship: while it is "own" the daily
bump is skipped and the xdna-driver pin stays where it is (it is then a
kernel-module pin only). A manual workflow_dispatch still runs, and setting
the variable to "xrt" (or unsetting it) resumes the daily bump. docs/npu.md
says so in "Keeping current" and in the runtime section.

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Fable 5.1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant