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
10 changes: 8 additions & 2 deletions docs/onnx-pack.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,14 @@ intermediate float tensor then never exists.
down_proj's ship beside the other xclbins, and q/k/v's and gate/up's came out of one `-M 128 -K <K>
-N <N> -c 4` run of the iso-build generator each (`n1_core_i8_v26.py` through the IRON/MLIR-AIE
`aiecc`), the artifacts living beside the other xclbins and never in this repository. And it is a
**correctness** result so far, not a speed one: these are M=128 designs, so a decode step hands the
kernel one real row and 127 zeroed ones — measure before quoting tokens/s.
**correctness** result so far; it is *not* a decode-side speed one, because a device call is
dispatch-bound. So `gemm_q8` routes by rows: below `kDeviceMinRows` — 16, one constant in
`onnx/ep/onebit_ep_kernel.h` — the host loop runs the multiply and the call never reaches the array, and at
or above it the device does. A host-routed call logs `npu_qdq_host_rows m=… k=… n=… min=16`, so "the device
ran this shape" stays "neither `npu_qdq_host_rows` nor `npu_qdq_fallback` was written for it". 16 is the
first row count at which no measured projection shape was slower on the array; at one row the launch costs
8–10× the host loop it replaces (`docs/onnx.md` carries the measurement). A decode-side win needs fewer,
longer launches rather than a smaller design.
- **Different kernels, not a configuration**: A16W8 needs int16 activations, MX/BFP16 need block
scales, and Int4 weight-only is the format our *own* forward route already stores (Q4NX), so the
natural split afterwards is our Q4NX pack for weight-only and the Quark QDQ path for A8W8.
Expand Down
12 changes: 12 additions & 0 deletions docs/onnx.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ smaller design.
divided by `repeats`. The figures above are the corrected ones, and `repeats < 2` — which used to print
`device 0.000 ms/call` and a four-digit speedup — is now refused with exit 2.)*

**What `gemm_q8` does with that number.** It routes by rows: below `kDeviceMinRows` — 16, declared once in
`onnx/ep/onebit_ep_kernel.h` — the host loop runs the multiply and the call never reaches the array; at or above
it the device does. 16 is the first row count at which **no** measured projection shape was slower on the
array, so the threshold is one constant rather than one per (K, N). A host-routed call is a choice, not a
failure, and it does not look like one: it writes
`npu_qdq_host_rows m=… k=… n=… min=16` to the trace. "The device ran this shape" therefore stays exactly
"neither `npu_qdq_host_rows` nor `npu_qdq_fallback` was written for it" — an assertion a green `|d| = 0` line
alone has never been (see `docs/onnx-pack.md`). `onebit_ep_qdq_proof` asserts the routing of *every* fused case
against that same constant, so a decode-width routing regression and a prefill-width one fail in different
places: with designs configured, `quark-stress` (64×1024×2048) must reach the device and every 5–8 row case must
log `npu_qdq_host_rows`.

**The device lock.** The NPU is shared, and the rule is that a plugin never evicts the owner's
serve. The EP takes `/tmp/1bit-npu-device.lock` with `flock(2)` **non-blocking** and holds it for the
life of the process; when the lock is held it does not queue — it reports `device busy` and runs the
Expand Down
8 changes: 7 additions & 1 deletion onnx/ep/onebit_ep_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,13 @@ void gemm_q8(const int8_t* a, const int8_t* b, int8_t* y, int64_t m, int64_t k,
#ifdef ONEBIT_EP_NPU
// The device leg: int8 in, exact int32 accumulators out, the epilogue above on the way back. A
// graph whose (K, N) has no ELF, or a busy device, leaves the row blocks to the host loop below.
{
// Below kDeviceMinRows the host loop is the faster leg -- the call is dispatch-bound, not work-bound --
// so that case is a policy choice, logged with its own name: a run is a device run only when neither
// this line nor npu_qdq_fallback is written for the shape.
if (m < kDeviceMinRows) {
trace_fallback("npu_qdq_host_rows m=%lld k=%lld n=%lld min=%lld", (long long)m, (long long)k,
(long long)n, (long long)kDeviceMinRows);
} else {
std::vector<int32_t> acc((size_t)m * (size_t)n);
std::string why;
if (onebit::npu::gemm_q8_raw(a, m, k, n, b, acc.data(), &why)) {
Expand Down
8 changes: 8 additions & 0 deletions onnx/ep/onebit_ep_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ bool gemm_f32(const float* a, const float* b, float* c, int64_t m, int64_t k, in
// from the same int8 bytes): npu/ort's gemm_q8_raw writes the int8 activations and weights straight
// into the kernel's BOs and hands back int32, and the epilogue below is shared by both paths.
//
// A device call is dispatch-bound: 1.4-1.5 ms whatever the row count (docs/onnx.md), so below a few rows the
// launch costs more than the host loop it replaces. `kDeviceMinRows` is the first row count at which no
// measured Qwen3-0.6B projection shape was slower on the array, and gemm_q8 uses the host loop below it
// whatever designs are configured. It applies only with ONEBIT_EP_NPU. A host-routed call is not a
// fallback: it logs `npu_qdq_host_rows`, so the absence of both that line and `npu_qdq_fallback` is what
// still means "the device ran this shape".
inline constexpr int64_t kDeviceMinRows = 16;

// Asymmetric zero points do not cost a second pass. Writing the shifted product out,
//
// Sum_k (a-zx)(w-zw) = Sum_k a*w - zx*Sum_k w - zw*Sum_k a + K*zx*zw
Expand Down
21 changes: 21 additions & 0 deletions onnx/ep/onebit_ep_qdq_proof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@

#include <onnxruntime_c_api.h>

#include "onebit_ep_kernel.h" // kDeviceMinRows: the routing the proof asserts against, in one place

#include <unistd.h>

#include <malloc.h>
Expand Down Expand Up @@ -604,6 +606,7 @@ struct Result {
size_t differing = 0;
bool fused = false; // the fused kernel ran (trace: a qdq line)
bool float_mm = false; // the float MatMul kernel ran (trace: a matmul line)
bool host_rows = false; // gemm_q8 chose the host loop on rows (trace: npu_qdq_host_rows)
long peak_cpu = -1;
long peak_fused = -1;
int rc = 0;
Expand Down Expand Up @@ -663,6 +666,7 @@ Result run_case(const Case& c, const char* ep_lib, const char* trace_path) {
const std::string slice = trace_mark < trace.size() ? trace.substr(trace_mark) : std::string();
r.fused = slice.find("qdq rows=") != std::string::npos;
r.float_mm = slice.find("matmul batch=") != std::string::npos;
r.host_rows = slice.find("npu_qdq_host_rows") != std::string::npos;
std::printf("onebit-ort-qdq: case %s: %zu of %zu int8 values differ, worst |d|=%d LSB, "
"path=%s\n",
c.label, r.differing, ref_out.size(), r.worst, r.fused ? "fused" : "per-node");
Expand Down Expand Up @@ -778,6 +782,23 @@ int main(int argc, char** argv) {
rc = 1;
break;
}
// With designs configured, every fused case must take the leg the threshold names: the host loop
// below kDeviceMinRows (a decode step), the device at or above it (a prefill chunk). Without ELFs the
// device is unused either way, so the rule is not asserted there.
if (r.fused && std::getenv("ONEBIT_EP_NPU_ELFS")) {
const bool want_host_rows = c.m < onebit_ep::kDeviceMinRows;
if (r.host_rows != want_host_rows) {
std::fprintf(stderr,
"onebit-ort-qdq: FAIL case %s (m=%lld): %s, expected %s "
"(kDeviceMinRows=%lld)\n",
c.label, (long long)c.m,
r.host_rows ? "the host loop on rows" : "the device leg",
want_host_rows ? "the host loop on rows" : "the device leg",
(long long)onebit_ep::kDeviceMinRows);
rc = 1;
break;
}
}
const bool want_fused = c.expect_fused && !fusion_off;
if (r.fused != want_fused) {
std::fprintf(stderr, "onebit-ort-qdq: FAIL case %s: %s, expected %s\n", c.label,
Expand Down
Loading