Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 59 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -362,23 +362,69 @@ target_link_libraries(npu_private_route_test PRIVATE onebit_npu)
add_test(NAME npu_private_route COMMAND npu_private_route_test)
endif() # ONEBIT_NPU_HOST

# The lane itself: needs XRT (the NPU runtime) and an XDNA NPU to run.
option(ONEBIT_NPU "Build the NPU fast lane (needs XRT)" OFF)
# The lane itself: needs an NPU runtime and an XDNA NPU to run.
option(ONEBIT_NPU "Build the NPU fast lane (needs an NPU runtime: XRT by default)" OFF)
set(ONEBIT_XRT_ROOT "/opt/xilinx/xrt" CACHE PATH "XRT install prefix")
# Which runtime the NPU code compiles against (npu/rt.h, docs/npu.md "The runtime"):
# xrt XRT from ONEBIT_XRT_ROOT (the default; unchanged behaviour)
# own the XRT-compatible runtime of a private add-on: ONEBIT_NPU_RT_DIR holds npu_rt.h and
# npu_rt.cpp, built here into onebit_npu_rt. No XRT is needed or linked.
set(ONEBIT_NPU_RUNTIME "xrt" CACHE STRING "NPU runtime: xrt or own")
set_property(CACHE ONEBIT_NPU_RUNTIME PROPERTY STRINGS xrt own)
set(ONEBIT_NPU_RT_DIR "" CACHE PATH "Directory with npu_rt.h and npu_rt.cpp (ONEBIT_NPU_RUNTIME=own)")
if(NOT ONEBIT_NPU_RUNTIME STREQUAL "xrt" AND NOT ONEBIT_NPU_RUNTIME STREQUAL "own")
message(FATAL_ERROR "ONEBIT_NPU_RUNTIME must be xrt or own, not '${ONEBIT_NPU_RUNTIME}'")
endif()
if(ONEBIT_NPU AND NOT ONEBIT_NPU_HOST)
message(FATAL_ERROR "ONEBIT_NPU needs ONEBIT_NPU_HOST=ON")
endif()
# The option's refusals (bad value, own without a runtime directory); needs no XRT, runs in CI.
if(ONEBIT_NPU_HOST AND NOT WIN32)
add_test(NAME npu_runtime_option
COMMAND ${CMAKE_SOURCE_DIR}/tests/npu_runtime_option.sh ${CMAKE_SOURCE_DIR} ${FETCHCONTENT_BASE_DIR})
set_tests_properties(npu_runtime_option PROPERTIES TIMEOUT 300)
endif()
if(ONEBIT_NPU)
if(NOT EXISTS "${ONEBIT_XRT_ROOT}/include/xrt/xrt_device.h")
message(FATAL_ERROR "ONEBIT_NPU needs XRT; none at ONEBIT_XRT_ROOT=${ONEBIT_XRT_ROOT}")
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()
add_library(onebit_npu_rt STATIC ${ONEBIT_NPU_RT_DIR}/npu_rt.cpp)
target_include_directories(onebit_npu_rt SYSTEM PUBLIC ${ONEBIT_NPU_RT_DIR})
target_compile_definitions(onebit_npu_rt PUBLIC ONEBIT_NPU_RUNTIME_OWN)
find_package(Threads REQUIRED)
target_link_libraries(onebit_npu_rt PUBLIC Threads::Threads)
set(_onebit_npu_rt onebit_npu_rt)
set(_onebit_npu_rt_inc "")
set(_onebit_npu_rt_extra "")
else()
if(NOT EXISTS "${ONEBIT_XRT_ROOT}/include/xrt/xrt_device.h")
message(FATAL_ERROR "ONEBIT_NPU needs XRT; none at ONEBIT_XRT_ROOT=${ONEBIT_XRT_ROOT}")
endif()
set(_onebit_npu_rt ${ONEBIT_XRT_ROOT}/lib/libxrt_coreutil.so)
set(_onebit_npu_rt_inc ${ONEBIT_XRT_ROOT}/include)
set(_onebit_npu_rt_extra aiebu uuid)
endif()
add_library(onebit_npu_lane STATIC npu/lane.cpp npu/generate.cpp)
target_include_directories(onebit_npu_lane SYSTEM PUBLIC ${ONEBIT_XRT_ROOT}/include)
target_link_libraries(onebit_npu_lane PUBLIC onebit_npu ${ONEBIT_XRT_ROOT}/lib/libxrt_coreutil.so)
if(_onebit_npu_rt_inc)
target_include_directories(onebit_npu_lane SYSTEM PUBLIC ${_onebit_npu_rt_inc})
endif()
target_link_libraries(onebit_npu_lane PUBLIC onebit_npu ${_onebit_npu_rt})
target_sources(onebit PRIVATE app/unified.cpp)
target_link_libraries(onebit PRIVATE onebit_npu_lane)
target_compile_definitions(onebit PRIVATE ONEBIT_NPU)
set_target_properties(onebit PROPERTIES BUILD_RPATH ${ONEBIT_XRT_ROOT}/lib INSTALL_RPATH ${ONEBIT_XRT_ROOT}/lib)
if(ONEBIT_NPU_RUNTIME STREQUAL "xrt")
set_target_properties(onebit PROPERTIES BUILD_RPATH ${ONEBIT_XRT_ROOT}/lib INSTALL_RPATH ${ONEBIT_XRT_ROOT}/lib)
endif()
# The shim compiles against the selected runtime (and runs without an NPU: it only builds
# objects and checks the API surface the engine uses).
add_executable(npu_rt_shim_test tests/npu_rt_shim_test.cpp)
target_link_libraries(npu_rt_shim_test PRIVATE onebit_npu_lane)
add_test(NAME npu_rt_shim COMMAND npu_rt_shim_test)
if(ONEBIT_NPU_RUNTIME STREQUAL "xrt")
set_target_properties(npu_rt_shim_test PROPERTIES BUILD_RPATH ${ONEBIT_XRT_ROOT}/lib)
endif()

# The model-generic forward (npu/forward): the GGUF-on-NPU serve route
# (docs/npu.md, "Open"). q4nx_forward.cpp + its Q4NX dequant/pack/semantics
Expand All @@ -403,9 +449,9 @@ if(ONEBIT_NPU)
target_compile_definitions(onebit_npu_forward PRIVATE NPU_INFER_DEBUG_DUMPS)
endif()
target_include_directories(onebit_npu_forward SYSTEM PUBLIC
npu/forward/include npu/forward/engine-src ${ONEBIT_XRT_ROOT}/include)
npu/forward/include npu/forward/engine-src npu ${_onebit_npu_rt_inc})
target_link_libraries(onebit_npu_forward PUBLIC
${ONEBIT_XRT_ROOT}/lib/libxrt_coreutil.so aiebu uuid dl pthread m)
${_onebit_npu_rt} ${_onebit_npu_rt_extra} dl pthread m)
target_sources(onebit PRIVATE app/forward_serve.cpp)
target_link_libraries(onebit PRIVATE onebit_npu_forward)
set(ONEBIT_NPU_KERNELS "" CACHE PATH "Kernel directory (layer_ctx{1,2,17}.elf, lmhead.elf, layer.pdi)")
Expand All @@ -426,6 +472,10 @@ if(ONEBIT_NPU_PRIVATE)
if(NOT EXISTS "${ONEBIT_NPU_PRIVATE}/addons/CMakeLists.txt")
message(FATAL_ERROR "ONEBIT_NPU_PRIVATE=${ONEBIT_NPU_PRIVATE} has no addons/CMakeLists.txt")
endif()
if(ONEBIT_NPU_RUNTIME STREQUAL "own")
message(WARNING "ONEBIT_NPU_PRIVATE with ONEBIT_NPU_RUNTIME=own: an add-on that links XRT itself "
"still needs XRT (docs/npu.md, \"XRT or the own runtime\")")
endif()
add_subdirectory(${ONEBIT_NPU_PRIVATE}/addons ${CMAKE_BINARY_DIR}/npu_private)
if(NOT TARGET onebit_npu_private)
message(FATAL_ERROR "${ONEBIT_NPU_PRIVATE}/addons defines no onebit_npu_private target")
Expand Down
2 changes: 1 addition & 1 deletion app/forward_serve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

#include "../npu/forward/include/q4nx_forward.h"
#include "tokenizer.h"
#include <xrt/xrt_device.h>
#include "rt.h" // XRT, or the own runtime (-DONEBIT_NPU_RUNTIME=own)

#include <httplib.h>
#include "http_guard.h"
Expand Down
37 changes: 36 additions & 1 deletion docs/npu.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ Every packed buffer is checked against the reference lane's own dump (`npu_pack_
| decode speed, same run | 11.0 ms/token (91 tok/s); the reference lane binary: 15.1 |
| load (weights packed, kernels built) | 380 ms |

The `1bit` binary links only `libxrt_coreutil`: no xclbin, no FastFlowLM library, no Python.
The `1bit` binary links only `libxrt_coreutil` (none with `-DONEBIT_NPU_RUNTIME=own`, below): no
xclbin, no FastFlowLM library, no Python.

## Serving (3c)

Expand Down Expand Up @@ -380,6 +381,40 @@ XRT 2.21.75. Both `libxrt_core` and `libxrt_driver_xdna` were loaded from the pi
bit-identical to the reference lane on 24/24 steps, at 10.8 ms/token (92.6 tok/s), with a 327 ms
warm load.

### XRT or the own runtime (`ONEBIT_NPU_RUNTIME`)

The NPU sources include `npu/rt.h`, never XRT headers directly. With
`-DONEBIT_NPU_RUNTIME=xrt` (the default) that header includes XRT, and nothing else changes. With
`-DONEBIT_NPU_RUNTIME=own -DONEBIT_NPU_RT_DIR=<dir>` it includes `<dir>/npu_rt.h` instead: a
private add-on runtime that opens `/dev/accel/accel0` and issues the amdxdna ioctls itself, with
XRT's class names, so `namespace xrt = npu_rt;` lets the same sources build unchanged.
`<dir>/npu_rt.cpp` is built into `onebit_npu_rt`, and `1bit` links no XRT library. This repository
holds no runtime code, only the option, the shim header and two tests: `npu_runtime_option`
(CI: 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).

```
cmake -B build-own -G Ninja -DONEBIT_NPU=ON -DONEBIT_NPU_RUNTIME=own \
-DONEBIT_NPU_RT_DIR=<1bit-MONSTER checkout>/engine/npu/npu_rt
```

Measured on Strix Halo on 2026-10-09 (fast lane, Qwen3-0.6B, power mode `performance`, both builds
in the same session, NPU runs alternating):

| Check | `xrt` | `own` |
|---|---|---|
| `ldd 1bit` | `libxrt_coreutil.so.2` | no `libxrt*` |
| `1bit npu-run`, anchor prompt, 32 greedy steps: logits against the `xrt` build | (reference) | bit-identical, 32/32 steps, 3 of 3 runs |
| `1bit npu-run` decode, median of 3 | 86.2 tok/s | 83.7 tok/s |
| `1bit serve --device npu`, 3 prompts, 32 greedy tokens each | (reference) | 32/32 tokens identical for each prompt |
| `1bit serve` decode (`timings`), median of the 3 prompts | 80.3 tok/s | 82.7 tok/s |
| `tests/serve_e2e.sh` | PASS | PASS |

Scope: `own` is checked on the fast lane's full-ELF path (`npu/lane.cpp`). The per-op forward
(`npu/forward`) builds against it but has not been run on it. A private route add-on
(`ONEBIT_NPU_PRIVATE`) that links XRT itself still needs XRT.

### Checking the NPU

`tests/npu_lane_e2e.sh` is retired (2026-10-03): the fast-lane kernels it ran were derived from
Expand Down
7 changes: 1 addition & 6 deletions npu/forward/engine-src/npu_engine_i8ctx_inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@
#include <memory>
#include <string>
#include <algorithm>
#include <xrt/xrt_device.h>
#include <xrt/xrt_bo.h>
#include <xrt/xrt_kernel.h>
#include <xrt/experimental/xrt_elf.h>
#include <xrt/experimental/xrt_ext.h>
#include <xrt/experimental/xrt_module.h>
#include "rt.h" // XRT, or the own runtime (-DONEBIT_NPU_RUNTIME=own)


struct I8Ctx {
Expand Down
2 changes: 1 addition & 1 deletion npu/forward/include/q4nx_forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

struct I8Ctx;

namespace xrt { class device; class bo; }
#include "rt.h" // xrt::device, xrt::bo: XRT, or the own runtime (-DONEBIT_NPU_RUNTIME=own)

struct Q4nxForwardConfig {
int H = 1024, NH = 16, NKV = 8, HD = 128, IM = 3072, NL = 28, NV = 151936;
Expand Down
2 changes: 1 addition & 1 deletion npu/forward/src/q4nx_forward.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include <cstring>
#include <fstream>

#include <xrt/xrt_device.h>
#include "rt.h" // XRT, or the own runtime (-DONEBIT_NPU_RUNTIME=own)

#include "npu_engine_i8ctx_inc.h" // I8Ctx
#include "q4nx_dequant.h"
Expand Down
8 changes: 1 addition & 7 deletions npu/lane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@
#include "full_elf.h"
#include "pack.h"

#include <xrt/experimental/xrt_elf.h>
#include <xrt/experimental/xrt_ext.h>
#include <xrt/experimental/xrt_kernel.h>
#include <xrt/xrt_bo.h>
#include <xrt/xrt_device.h>
#include <xrt/xrt_hw_context.h>
#include <xrt/xrt_kernel.h>
#include "rt.h" // XRT, or the own runtime (-DONEBIT_NPU_RUNTIME=own)

#include <chrono>
#include <cstdio>
Expand Down
38 changes: 38 additions & 0 deletions npu/rt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2026 bong-water-water-bong
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// The NPU runtime the engine's NPU code compiles against (docs/npu.md, "The runtime").
//
// -DONEBIT_NPU_RUNTIME=xrt (the default): XRT, from ONEBIT_XRT_ROOT.
// -DONEBIT_NPU_RUNTIME=own: an XRT-compatible runtime from a private add-on directory
// (ONEBIT_NPU_RT_DIR, holding npu_rt.h and npu_rt.cpp). Its classes mirror XRT's names, so the
// alias below lets the same sources build against it unchanged. This repository holds no
// runtime code: only this header and the CMake option.
#pragma once

#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
64 changes: 64 additions & 0 deletions tests/npu_rt_shim_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2026 bong-water-water-bong
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// npu/rt.h against the selected runtime (-DONEBIT_NPU_RUNTIME=xrt|own): every runtime call the
// engine's NPU code makes (npu/lane.cpp, npu/forward) appears below, so a runtime that lacks one
// fails to build this test. Nothing here opens the device: `uses` is compiled, never called.
#include "rt.h"

#include <chrono>
#include <cstdio>
#include <memory>
#include <string>
#include <vector>

[[maybe_unused]] static void uses(const std::vector<char>& image) {
xrt::device dev{0};
auto ctx = std::make_unique<xrt::hw_context>(dev, xrt::elf(image.data(), image.size()));
ctx->add_config(xrt::elf(image.data(), image.size()));
xrt::elf from_file(std::string("kernel.elf"));
if (from_file.is_full_elf()) std::puts(from_file.get_cfg_uuid().to_string().c_str());
xrt::ext::kernel k(*ctx, std::string("kernel"));
auto act = std::make_unique<xrt::ext::bo>(dev, size_t(4096));
xrt::ext::bo e{dev, size_t(4096)};
auto plain = std::make_unique<xrt::bo>(e);
static_cast<char*>(act->map())[0] = 0;
act->sync(XCL_BO_SYNC_BO_TO_DEVICE);
act->sync(XCL_BO_SYNC_BO_FROM_DEVICE, size_t(256), size_t(0));
std::printf("%zu\n", plain->size());
xrt::run r(k);
r.set_arg(0, *act);
r.start();
if (r.wait(std::chrono::milliseconds(5000)) != ERT_CMD_STATE_COMPLETED) std::puts("not completed");
std::vector<xrt::run> runs;
runs.reserve(2);
xrt::run& r2 = runs.emplace_back(k);
r2.set_arg(0, *plain);
auto rl = std::make_unique<xrt::runlist>(*ctx);
rl->add(r2);
rl->execute();
rl->wait();
xrt::run r3 = k(*act, *plain, e);
r3.wait();
}

int main() {
#if defined(ONEBIT_NPU_RUNTIME_OWN)
std::puts("npu/rt.h: own runtime (ONEBIT_NPU_RT_DIR)");
#else
std::puts("npu/rt.h: XRT");
#endif
return 0;
}
41 changes: 41 additions & 0 deletions tests/npu_runtime_option.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash
# Copyright 2026 bong-water-water-bong
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ONEBIT_NPU_RUNTIME (docs/npu.md, "The runtime"): configuring with a value other than xrt|own, or
# with own and no runtime directory, must stop with a message that says so. Needs neither XRT nor
# an NPU, so it runs in CI; reuses the parent build's fetched dependencies (offline).
#
# usage: tests/npu_runtime_option.sh <source dir> <FetchContent base dir>
set -uo pipefail
src=${1:?usage: npu_runtime_option.sh <source dir> <fetchcontent dir>}
deps=${2:?}
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
fail=0
expect() { # expect <name> <message fragment> <cmake args...>
local name=$1 msg=$2
shift 2
local out
out=$(cmake -S "$src" -B "$tmp/$name" -DFETCHCONTENT_BASE_DIR="$deps" -DFETCHCONTENT_FULLY_DISCONNECTED=ON \
-DONEBIT_NPU=ON "$@" 2>&1)
local rc=$?
if [ $rc -ne 0 ] && [[ "$out" == *"$msg"* ]]; then echo "ok $name"; else
echo "FAIL $name (rc $rc)"; echo "$out" | tail -15; fail=1; fi
}
expect bad_value "ONEBIT_NPU_RUNTIME must be xrt or own" -DONEBIT_NPU_RUNTIME=bogus
expect own_without_dir "ONEBIT_NPU_RUNTIME=own needs ONEBIT_NPU_RT_DIR" -DONEBIT_NPU_RUNTIME=own -DONEBIT_NPU_RT_DIR="$tmp/none"
[ $fail -eq 0 ] && echo PASS
exit $fail
Loading