Skip to content

onnx EP: give the three host-routed projection shapes a device gate at 32 rows - #909

Merged
bong-water-water-bong merged 3 commits into
mainfrom
1bit/onnx-qdq-device-width-gate
Nov 3, 2026
Merged

bong-water-water-bong merged 3 commits into
mainfrom
1bit/onnx-qdq-device-width-gate

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

What this fixes

gemm_q8 routes by rows since #862 (kDeviceMinRows = 16), and the proof's device cases for
o_proj (5×2048×1024), down_proj (5×3072×1024) and gate_proj/up_proj (5×1024×3072) are all
5 rows. So from that merge on those three cases ran on the host loop — npu_qdq_host_rows — and the
array never saw those shapes. Three of the five projection shapes docs/onnx-pack.md claims as
device-covered had no device gate at all, and nothing said so: the proof still printed
PASS … worst |d| = 0, because the shapes that do reach the device (q at 64 rows, k/v at 256) were still
covered and a host-routed call is a legitimate outcome, not a failure.

That is this lane's recurring "host gate dressed as a device one" failure in its quietest form: the run looks
perfect and the trace is the only thing that disagrees.

Found on resume, not from a report: the 5-row cases were correct when they were written and #862's routing
silently changed what they prove.

The fix

change why
quark-o32 / quark-d32 / quark-gu32 — 32 rows at 2048×1024, 3072×1024, 1024×3072, placed after the rss case the three shapes get a case at a row count the device actually takes; last position so their arena cannot move the RSS measurement
the 5-row cases stay they are now what asserts host routing, which the routing check needs on the decode side
new assertion: a fused case at or above kDeviceMinRows must not log npu_qdq_fallback "the array ran this shape" becomes a red line in the test instead of living only in a lane script. A device that quietly declined to run now fails the proof
fused-case floor 10 → 13 (16 fused cases now) keeps the same slack for cases silently ceasing to fuse
gate on the new cases is ≤ 1 LSB, not 0 see below — it is arithmetic, not slack
docs/onnx.md, docs/onnx-pack.md two statements corrected, both false as shipped, both mine

Why the new cases are gated at one LSB

The CPU EP dequantises and then accumulates in float32; the fused path sums exact int32 and rounds once.
At a quantisation boundary the two can therefore round to different neighbours. Measured on the 32-row cases:

case K differing values worst |d|
quark-o32 (o_proj) 2048 1 of 32768 1 LSB
quark-d32 (down_proj) 3072 0 of 32768 0
quark-gu32 (gate/up) 1024 0 of 98304 0

That is the same 1-in-5120 the real Quark A8W8 export showed in #695, and it is the reason the existing
quark-stress case is already gated at ≤1. Above one LSB is a real defect — a wrong weight layout or a
truncated K reads as hundreds of LSB — so the bound does not weaken the gate: it stops it asserting an
arithmetic identity the CPU EP does not provide.

Two shipped statements were wrong, both mine

  1. Both pages said "16 is the first row count at which no measured projection shape was slower". False: the
    45-cell sweep taken with the fixed probe has every shape faster from 12 rows (worst shape 0.86× of the
    host loop at 12). 16 is the first count that keeps more margin than the box's own run-to-run spread —
    1.59–1.75× at 16 against 1.16–1.32× at 12 — which is the actual reason for the constant. Corrected on both
    pages, with the reason.
  2. docs/onnx.md said the device "crosses over between 8 and 16 rows"; and docs/onnx-pack.md's five-shape
    device paragraph did not say which row count its numbers were taken at, so after onnx EP: gemm_q8 routes by rows, so a decode step stops paying for a dispatch-bound device call #862 it read as current
    coverage. Both now state the row count and that the 5-row cases are decode-width and host-routed.

Gates

Host (this tree, cmake -S onnx -B … with no CMAKE_BUILD_TYPE, i.e. the stricter unoptimized build):
ctest 6/6; tools/copyright.py --check rc 0; no new files, so no new notices.

onebit-ort-qdq: case quark-o32:  1 of 32768 int8 values differ, worst |d|=1 LSB, path=fused
onebit-ort-qdq: case quark-d32:  0 of 32768 int8 values differ, worst |d|=0 LSB, path=fused
onebit-ort-qdq: case quark-gu32: 0 of 98304 int8 values differ, worst |d|=0 LSB, path=fused
onebit-ort-qdq: PASS … device-width projections (32 rows: o_proj, down_proj, gate/up) worst |d|=1,
                peak RSS -6140 kB

The RSS figure is unchanged from the documented band (6136–7004 kB), which is the check that the new cases sit
where their comment says they do.

Device: ONEBIT_EP_NPU=ON Release builds of both origin/main and this branch, one npu-hold -l qdq
ticket running both arms — the positive (five designs, o/d/gu on the array at 32 rows, per-shape
host_rows/fallback/epilogue counts read back from the trace) and the negative arm (o_proj's design
replaced by a file the runtime refuses, so the new assertion must FAIL and name quark-o32; a gate never seen
to fail is not a gate). Result in the comment below.

Not in this PR

  • No kernel, no EP behaviour change: this is test coverage, its assertions, and docs.
  • No throughput claim — the device leg stays correctness-only.
  • Nothing about row 11 of the zero-copy audit (the ONNX cached graph's per-step Concat), which tracking: zero-copy on the serving paths (hybrid KV handoff, NPU activations in BOs, ONNX EP IO binding, audit) #698 hands to
    pi-ortep/pi-qdq: it cannot be removed by an EP-side memcpy fix, because the copy is how present is
    produced; it needs a cache written in place, i.e. an op that mutates a buffer, which ONNX's functional graphs
    cannot express without new kernel work. Recorded in my state file, not attempted here.

pi-qdq added 2 commits November 3, 2026 08:22
…t 32 rows

`gemm_q8` routes by rows (`kDeviceMinRows = 16`) since #862, so the 5-row
o_proj / down_proj / gate-up cases stopped reaching the array: three of the
five projection shapes docs/onnx-pack.md claims had no device gate at all,
while the run still printed `PASS worst |d| = 0`. Same failure family as the
"host gate dressed as a device one" this lane has recorded twice, in its
quietest form.

- `quark-o32`, `quark-d32`, `quark-gu32` (32 rows: 2048x1024, 3072x1024,
  1024x3072), placed after the `rss` case so their arena cannot move that
  measurement. The 5-row cases stay: they are what asserts host routing.
- The proof now asserts that a fused case at or above the threshold did not
  fall back, so a device that quietly declined to run is a red line in the
  test instead of only in a lane script.
- Gate on the new cases is one LSB, not zero: the CPU EP accumulates in
  float32 and the fused path rounds an exact int32 sum, so a quantisation
  boundary can round to different neighbours. Measured 1 of 32768 on o_proj
  (K=2048), 0 on down_proj and gate/up; above one LSB is a defect.
- docs: "16 is the first row count at which no shape was slower" was wrong --
  12 is (worst shape 0.86x of the host loop), and 16 is the first that keeps
  more margin than the box's run-to-run spread. Both pages corrected, and the
  device-gate paragraph now says which row count the claim is measured at.
…e gate measured 7236 kB)

The paragraph quotes its own eight runs (6136-7004 kB) and says why the figure moves; a
head re-run of the same gate on the same tree produced 7236 kB, so a reader who re-measured
would have found the band too tight. Stated as a range with a wider tail rather than widened
silently -- the 6.1 MB identity it rests on is unaffected.
@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit 1233aa3)

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

862 - Partially compliant

Compliant requirements:

  • The kDeviceMinRows constant is set to 16 in onebit_ep_kernel.h
  • The proof now asserts that cases at or above kDeviceMinRows do not log npu_qdq_fallback
  • The proof validates that cases below kDeviceMinRows log npu_qdq_host_rows
  • The proof includes the three 32-row cases (quark-o32, quark-d32, quark-gu32) to ensure device coverage
  • The documentation in docs/onnx-pack.md and docs/onnx.md reflects the 16-row threshold and the measurement-based reasoning

Non-compliant requirements:

  • None

Requires further human verification:

  • None

695 - Partially compliant

Compliant requirements:

  • The proof now includes cases that reflect real Quark export shapes (rank-3 activations, per-column weight scales)
  • The proof includes the 64×1024×2048 stress case
  • The proof asserts correctness against the CPU EP for the fused path
  • The proof now correctly handles Quark's domain-specific QDQ ops

Non-compliant requirements:

  • None

Requires further human verification:

  • None

698 - Partially compliant

Compliant requirements:

  • None

Non-compliant requirements:

  • This PR does not implement zero-copy on the serving paths
  • This PR does not address hybrid KV handoff, NPU runtime activations, or ONNX EP IO binding
  • This PR does not create an audit table for remaining copies

Requires further human verification:

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

Incorrect assertion logic for device fallback

The assertion logic in onebit_ep_qdq_proof.cpp checks if a case at or above kDeviceMinRows falls back to the host loop, but it should instead check if a case below kDeviceMinRows uses the host loop. The current logic incorrectly flags cases that are correctly routed to the device as failures.

if (!want_host_rows && r.fallback) {
    std::fprintf(stderr,
                 "onebit-ort-qdq: FAIL case %s (m=%lld): the device was configured but the "
                 "kernel fell back, so this is a host gate, not a device one\n",
                 c.label, (long long)c.m);
    rc = 1;
    break;
}
Incorrect device gate assertion

The assertion that checks for device gate correctness is flawed. It assumes that if a case is at or above kDeviceMinRows, it must not log npu_qdq_fallback. However, this is not sufficient to ensure that the device actually ran the case, as the fallback check should be applied to cases that are expected to run on the device.

if (r.fused && std::getenv("ONEBIT_EP_NPU_ELFS")) {
    const bool want_host_rows = c.m < onebit_ep::kDeviceMinRows;
    // A case at or above the threshold is a device gate, and a device gate that fell back proves
    // nothing: the host kernel then produces the |d| = 0 the run prints. That is the exact failure
    // mode a lane script once reported as "device accumulators match" (TQDQ-STATE
    // 2026-10-31T15:15), so it is asserted here instead of living only in that script.
    if (!want_host_rows && r.fallback) {
        std::fprintf(stderr,
                     "onebit-ort-qdq: FAIL case %s (m=%lld): the device was configured but the "
                     "kernel fell back, so this is a host gate, not a device one\n",
                     c.label, (long long)c.m);
        rc = 1;
        break;
    }

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Device gate: both arms passed on the array

Two npu-hold -l qdq tickets. The first at 6555fa1, then the whole thing re-run at the pushed head after I
rebuilt both trees — same numbers, which is what a comment-only difference should produce.

Ticket 01793723333145490447.qdq (lock free 12:28:53, token 12:43:24 at die 42 °C, ran 2 s, exit 0) and the
head re-run (12:44:56, 5 s). Designs configured: the five M=32 artifacts, 32x{1024x2048,1024x1024,1024x3072, 2048x1024,3072x1024}.

BEFORE (origin/main 7690a8c) — the gap, read off the trace

What the array actually ran, i.e. epilogue lines at a device-width row count:

1 qdq rows=64  k=1024 n=2048     <- q_proj's shape, via quark-stress
2 qdq rows=256 k=1024 n=1024     <- k/v's shape, via rss

and npu_qdq_host_rows m=5 once for each of the five projection shapes. So o_proj (2048×1024), down_proj
(3072×1024) and gate/up (1024×3072) had no device-width case at all — only q and k/v kept device coverage,
which is exactly why the proof still printed PASS.

AFTER (this branch)

o_proj    2048x1024  32 rows: ran-on-array=yes  (host_rows=0 fallback=0 epilogue=1)
down_proj 3072x1024  32 rows: ran-on-array=yes  (host_rows=0 fallback=0 epilogue=1)
gate_up   1024x3072  32 rows: ran-on-array=yes  (host_rows=0 fallback=0 epilogue=1)

case quark-o32:  1 of 32768 int8 values differ, worst |d|=1 LSB, path=fused
case quark-d32:  0 of 32768 int8 values differ, worst |d|=0 LSB, path=fused
case quark-gu32: 0 of 98304 int8 values differ, worst |d|=0 LSB, path=fused
PASS … device-width projections (32 rows: o_proj, down_proj, gate/up) worst |d|=1, peak RSS -7236 kB

host_rows=0 and fallback=0 for those three (m=32, k, n) is the assertion this PR adds: the shape reached
the array instead of being routed to the host or quietly declined. The five m=5 host_rows lines are still
present, so the decode-side routing assertion still has subjects.

NEGATIVE ARM — the gate can fail

Same ticket, four designs intact and o_proj's replaced by /bin/true:

proof rc=1
FAIL case quark-o32 (m=32): the device was configured but the kernel fell back, so this is a host gate,
     not a device one

One unloadable design costs that shape and names that case; the other four still run. Recorded because a gate
that has never been seen to fail is not a gate — this lane has twice shipped a device claim that was really a
host run wearing the trace of one.

Host gate on the pushed head

cmake -S onnx -B … with no CMAKE_BUILD_TYPE (the stricter unoptimized build): ctest 6/6 in 20.96 s;
tools/copyright.py --check rc 0; no new files.

One number moved, so it is in the diff too

That head re-run measured the fused/CPU-EP RSS delta as 7236 kB, above the 6136–7004 band
docs/onnx-pack.md quotes from its own eight runs. The band was honest about what it was (that process's arena
churn, not a tensor size) but too tight to survive a re-measurement, so the paragraph now says the range is
indicative and lists the wider tail — same class of correction as the two rows-table sentences this PR fixes.

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 64a4af4

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: READY

At 64a4af460. I built the ORT target here (-DONEBIT_ORT=ON, Release, 0 warnings) and ran the proof, so the new cases are measured rather than read:

onebit-ort-qdq: case quark-o32:  1 of 32768 int8 values differ, worst |d|=1 LSB, path=fused
onebit-ort-qdq: case quark-d32:  0 of 32768 int8 values differ, worst |d|=0 LSB, path=fused
onebit-ort-qdq: case quark-gu32: 0 of 98304 int8 values differ, worst |d|=0 LSB, path=fused
onebit-ort-qdq: PASS OneBitEp fused the A8W8 QDQ group: worst |d| = 0/0/0/0 LSB …, device-width
                projections (32 rows: o_proj, down_proj, gate/up) worst |d|=1, peak RSS -6692 kB

That is exactly the body's claim down to the counts — 1 of 32768 on o_proj, 0 on down_proj and gate/up — and the peak-RSS delta 6692 kB sits inside the documented band (6136-7236), so the "indicative, not a bound" wording in the last commit is honest.

The routing is right, and it cannot drift. kDeviceMinRows = 16 (onnx/ep/onebit_ep_kernel.h:51) with the new cases at m=32, so they are device width while their 5-row twins are host width. The proof #includes that header rather than restating 16 — the constant lives in one place, once.

The assertion this PR exists for is the fallback one, and it is the right shape: a case at or above the threshold that logs npu_qdq_fallback now fails instead of printing a green |d| = 0 produced by the host loop. That is the failure the docs name plainly ("a host gate dressed as a device gate … the run still printed PASS and worst |d| = 0"), and it is now checked in the test rather than in a lane script. fused_cases 10 → 13 matches the three added cases — the run passes that check too.

What I could not run: the device half. Both halves of the routing assertion live inside if (r.fused && std::getenv("ONEBIT_EP_NPU_ELFS")), so on this box (no ELFs configured) the three new cases take the fused host path and assert nothing about the array. Everything the flag does not gate I ran; the "the array ran this shape" claim remains the lane's, and it is now enforced where it will be re-checked.

One note for whoever owns this gate next: the o_proj bound has no headroom — 1 LSB passes, 2 fails, and the 1 is a property of the CPU EP's float32 accumulation order against the fused path's exact int32. If that order ever changes (a different microarchitecture's FMA/SIMD path in ORT, a new ORT build), the same arithmetic could land on 2 of 32768 and the row would go red with no defect behind it. The docs already anchor the number to a real Quark export showing the same 1-in-5120, so this is a "know what a red row means here" note, not a change request — but a column-level tolerance would be the fix if it ever fires.

The docs edits are consistent with the code: the 12-vs-16 row-count story is told once with both numbers and the reason 16 is kept (run-to-run spread 1.2-1.3x), and docs/onnx.md keeps the device leg correctness-only rather than quoting a device speedup.

@bong-water-water-bong bong-water-water-bong added the tprs-ready TPRS: review verdict READY posted by the standing reviewer; merge gate satisfied label Nov 3, 2026
@bong-water-water-bong
bong-water-water-bong merged commit 6546018 into main Nov 3, 2026
9 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/onnx-qdq-device-width-gate branch November 3, 2026 17:27
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 1233aa3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review effort 4/5 tprs-ready TPRS: review verdict READY posted by the standing reviewer; merge gate satisfied

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant