Skip to content

onnx EP: gemm_q8 routes by rows, so a decode step stops paying for a dispatch-bound device call - #862

Merged
bong-water-water-bong merged 2 commits into
mainfrom
1bit/onnx-ep-qdq-rows
Nov 3, 2026
Merged

bong-water-water-bong merged 2 commits into
mainfrom
1bit/onnx-ep-qdq-rows

Conversation

@bong-water-water-bong

@bong-water-water-bong bong-water-water-bong commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

onnx EP: gemm_q8 routes by rows, so a decode step stops paying for a dispatch-bound device call

#853 measured the A8W8 kernel on both legs and found the thing this PR acts on: a device call costs 1.4-1.6 ms
whatever the row count, so at one row it is ~9x slower than the EP's own -O2 host loop and only wins from
~8-16 rows up. gemm_q8 sent every call to the array anyway whenever ONEBIT_EP_NPU_ELFS is configured, so a
decode step paid 196 dispatch-bound launches (~0.3 s/step) for a multiply the host does in ~30-45 ms. That is
the wrong default, and it was mine.

What changed

  • onnx/ep/onebit_ep_kernel.h: kDeviceMinRows = 16, declared once and documented with the measurement.
  • onnx/ep/onebit_ep_kernel.cpp (gemm_q8): below kDeviceMinRows the host loop runs and the call never
    reaches the array; at or above it the device does. A host-routed call writes its own trace line,
    npu_qdq_host_rows m=… k=… n=… min=16, so the existing discriminator gets stronger, not weaker — a device
    run is still "neither npu_qdq_fallback nor npu_qdq_host_rows was written for this shape".
  • onnx/ep/onebit_ep_qdq_proof.cpp: the proof reads npu_qdq_host_rows out of each case's own trace slice and
    asserts the routing of every fused case against the same kDeviceMinRows (the header is included, so the
    constant exists once and the test cannot drift from the kernel). With designs configured, every 5-8 row case
    must log it and quark-stress (64×1024×2048) must not.

The sweep that picks the number

Five Qwen3-0.6B projection shapes × rows 1, 2, 4, 6, 8, 12, 16, 24, 32, M=32 designs, -O2 build, one
npu-hold ticket on strixhalo (load 14.4 at grant, so the host column is the conservative one — a busy box
inflates it and flatters the device). Every one of the 45 configurations also required the device's exact int32
accumulators to equal the EP's host loop element for element: all 45 matched.

shape rows 1 rows 8 rows 12 rows 16 rows 32
1024×1024 (k, v) 0.789 / 0.094, device 8.4× slower 0.832 / 0.648, 1.28× slower 0.809 / 0.964, 1.19× faster 0.805 / 1.316, 1.63× faster 0.819 / 2.590, 3.2× faster
1024×2048 (q) 1.522 / 0.175, 8.7× slower 1.523 / 1.313, 1.16× slower 1.510 / 1.940, 1.28× faster 1.481 / 2.583, 1.74× faster 1.719 / 5.524, 3.2× faster
1024×3072 (gate, up) 2.431 / 0.271, 9.0× slower 2.492 / 1.946, 1.28× slower 2.276 / 3.162, 1.39× faster 2.512 / 4.158, 1.66× faster 2.697 / 9.279, 3.4× faster
2048×1024 (o) 2.004 / 0.224, 8.9× slower 2.140 / 1.895, 1.13× slower 1.645 / 2.144, 1.30× faster 1.813 / 2.671, 1.47× faster 1.518 / 5.164, 3.4× faster
3072×1024 (down) 2.293 / 0.251, 9.1× slower 2.162 / 2.048, 1.06× slower 2.391 / 2.917, 1.22× faster 2.304 / 3.836, 1.66× faster 2.687 / 7.814, 2.9× faster

(device ms/call / host ms/call. The device cells above are ~5 % optimistic and are kept only for the shape of
the curve: the sweep was taken before #853's divisor fix landed on the base (it timed repeats-1 calls and
divided by repeats, r = 1), so every device figure is low by (repeats-1)/repeats.)

Retake with the fixed probe (45 cells, min of three runs)

Rather than rescale the table by 20/19, the whole sweep was re-run end to end on the fixed probe (r = 0 in
both legs, repeats=20), because a corrected divisor has to be shown measuring. All 45 configurations ran on
the array with exact int32 accumulators equal to the host loop's, one npu-hold ticket, exit 0:

rows 1 2 4 6 8 12 16 24 32
dev:host, slowest of five shapes 14.51 5.55 3.10 1.71 1.23 0.86 0.63 0.41 0.32
dev:host, fastest of five shapes 7.40 4.58 2.16 1.54 1.16 0.76 0.57 0.39 0.29

So with the fixed instrument: rows ≤ 8 are slower on every shape, rows ≥ 12 are faster on every shape, and 16
is not the first count at which nothing is slower — 12 is — but it is the first that keeps a margin bigger than
the box's own run-to-run spread. At the tightest shape: 1.16-1.32× at 12 rows, 1.59-1.75× at 16. The
constant below stays 16; what the retake changed is where the margin is, not the choice. One constant, not one
per (K, N).

One trap found while re-taking it, worth knowing before re-running this at all: the array ramps. A ticket
that starts with rows=1 cells reads ~1.8× high, uniformly across all five shapes — each cell is a fresh
process with its own warm-up call, and one warm-up call is necessary but not sufficient — and it decays to
steady state by ~row 6. The rows ≥ 6 numbers the crossover rests on are steady-state and flat. That sentence is
in docs/onnx.md (via #853) so the next reader does not conclude that the doc's 1.501 ms and their own cold 2.87
ms are both "the device at one row".

Measured gate

Routing, read out of the EP's trace of the proof run (npu_qdq_host_rows is written by the kernel, qdq rows=
by the shared epilogue on both paths, so only the former says which leg was chosen):

   quark-stress 64 rows: host_rows=0 fallbacks=0        <- the 64-row case really reached the array
   below-threshold cases (m < 16): host_rows=9           <- every 5-8 row case stayed on the host loop

Graph, same ticket: case quark-stress: 0 of 131072 int8 values differ, worst |d|=0 LSB, path=fused;
PASS OneBitEp fused the A8W8 QDQ group: worst |d| = 0/0/0/0 LSB ….

Kernel, per shape at 32 rows on the array (probe exit 0 = the device ran the shape and its exact int32
accumulators equal the host loop's), re-taken on a quiet box (load 4.2, ticket …039860328252.qdq, 3 s):
all five PASS — 1024×1024 0.841 vs 2.530 ms, 1024×2048 1.612 vs 5.101, 1024×3072 2.293 vs 7.631,
2048×1024 1.493 vs 5.017, 3072×1024 2.186 vs 7.654 (3.0-3.5×).

Host build, no CMAKE_BUILD_TYPE (the stricter one): ctest 6/6, worst |d| = 0/0/0/0 LSB, per-K scale
still refused, peak RSS gate unchanged (+268 kB fused against the CPU EP's +8116 kB) — the new assertion does
not perturb the RSS case.

Not done, deliberately

  • The float gemm_f32 leg is untouched. It has the same dispatch-bound shape, but no design exists on this
    box for any float shape, so there is nothing to measure and a threshold there would be a guess. If float
    designs land, the same rule applies and the probe can time them.
  • A shape-aware threshold (m·k·n against the constant launch cost) would let the bigger shapes use the
    array a few rows earlier — they cross over at ~9 rows, the small ones at ~11-12. Left out: one constant that
    is never slower is the smallest change that is right, and the difference is a few ms inside a prefill chunk.

@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit bd0d1bb)

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

853 - Partially compliant

Compliant requirements:

  • Instrumentation added in npu/ort/onebit_npu_gemm_q8_probe.cpp
  • Timing measurements reported for multiple shapes and row counts
  • Documentation updated in docs/onnx.md and docs/onnx-pack.md
  • gemm_q8 routing logic updated with kDeviceMinRows threshold
  • Proof tests updated to assert correct routing behavior

Non-compliant requirements:

  • No tokens/s number reported (mentioned as "Not in this PR")
  • No kernel or EP behavior change (mentioned as "No kernel or EP behaviour change")

Requires further human verification:

  • Verification that the measured performance numbers are accurate on the target hardware
  • Confirmation that the routing logic correctly handles all cases in production scenarios
⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Dispatch-bound kernel routing

The change introduces a threshold (kDeviceMinRows = 16) to determine when to route GEMM operations to the device vs. host. This is critical because the device kernel is dispatch-bound and slower than the host loop for small row counts (1-15 rows). The host loop is now used for m < 16, which prevents unnecessary dispatch overhead during decode steps. The change correctly implements the measurement results from ticket #853 that showed device calls cost 1.4-1.5ms regardless of row count, making them 8-10x slower than the host loop for small shapes.

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 {
Routing assertion logic

The proof test now asserts that fused cases are routed correctly based on the kDeviceMinRows threshold. This ensures that when designs are configured, cases with m < 16 rows must use the host loop (logging npu_qdq_host_rows) and cases with m >= 16 must use the device. This prevents regressions in routing behavior and ensures that the performance characteristics identified in the measurement are maintained in production.

// 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;
    }
}

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Rebased onto its new base (1bit/onnx-ep-qdq-q8-probe = #853 at 71d94dc, i.e. on top of main 801a4a6 with #848 merged) → head 8c23028, diff still the same five files, +56/−3.

The rebase was a content conflict, and worth flagging

docs/onnx-pack.md conflicted because this branch was written against a base that predates #848, so its hunk still said "on this box only those two Qwen3-0.6B projections have one (q_proj's 1024×2048 and k/v's 1024×1024 do not), so the device share of a real graph is 56 of 197 MatMuls". #848 merged and made that false: five designs, 196 of 197. Resolved by keeping main's five-design / 196-of-197 text and grafting this PR's point where main's version ended with "measure before quoting tokens/s" — the rows routing is that measurement. The "~9×" in the routing sentence is now 8–10×, which is the corrected figure from #853's re-measurement.

docs/onnx.md and onnx/ep/onebit_ep_qdq_proof.cpp auto-merged — which is not the same as correct, because the proof now carries both #848's quark-kv/quark-gu cases and this PR's per-case routing assertion. So it was run, not reasoned about.

Gates on 8c23028

Host build with no CMAKE_BUILD_TYPE (-DONEBIT_ORT=ON, -j8): build rc 0, ctest 6/6 in 17.36 s. -DONEBIT_EP_NPU=ON Release builds. tools/copyright.py --check rc 0 (no new files).

Device gate — npu-hold ticket 01793663547020185958.qdq, 0 s wait, 2 s, load 9.7, device lock free, exit 0:

== routing: the graph gate, and which leg each row count took
case quark-stress: 0 of 131072 int8 values differ, worst |d|=0 LSB, path=fused
PASS OneBitEp fused the A8W8 QDQ group: worst |d| = 0/0/0/0 LSB …, peak RSS -5992 kB
   quark-stress 64 rows: host_rows=0 fallbacks=0
   below-threshold cases (m < 16): host_rows=11
== kernel, per shape, 32 rows on the array
1024x1024   device 0.845  host 2.593   3.07x
1024x2048   device 1.730  host 5.296   3.06x
1024x3072   device 2.490  host 8.066   3.24x
2048x1024   device 1.564  host 5.154   3.30x
3072x1024   device 2.362  host 7.674   3.25x
PASS the 64-row case reached the array, the decode-width cases stayed on the host, and all five
     shapes matched the host loop exactly at 32 rows

The trace (/tmp/onebit_ep_trace.92797.log) is the routing claim itself — every host-routed call names the threshold, and the two wide cases carry neither line, which is what "the device ran it" means:

   1 npu_qdq_host_rows m=5 k=1024 n=1024 min=16      (quark-kv, from #848)
   1 npu_qdq_host_rows m=5 k=1024 n=2048 min=16      (quark-wide)
   1 npu_qdq_host_rows m=5 k=1024 n=3072 min=16      (quark-gu, from #848)
   1 npu_qdq_host_rows m=5 k=2048 n=1024 min=16      (quark-o)
   1 npu_qdq_host_rows m=5 k=3072 n=1024 min=16      (quark-d)
   4 npu_qdq_host_rows m=8 k=16  n=6     min=16      (tensor/axis/asym/asym-axis)
   2 npu_qdq_host_rows m=8 k=64  n=48    min=16      (quark/quark-asym)
   qdq rows=64  k=1024 n=2048 / qdq rows=256 k=1024 n=1024  -- no host_rows, no fallback => device
   npu_qdq_fallback: 0

11 is now the expected count rather than 9: #848 added the two extra 5-row cases, and every one of them is on the host loop as the threshold requires. The proof's own per-case routing assertion (FAIL case … expected the device leg) did not fire once — with ONEBIT_EP_NPU_ELFS configured it checks every fused case against kDeviceMinRows, which is why this run is evidence and not just a green PASS.

One measurement note for the record: this tree reports the RSS gate as −5992 kB against the 6136–7004 kB the docs/onnx-pack.md paragraph lists for its eight runs. That paragraph is a statement about those eight runs (and the case mix before the rss case perturbs the arena), not a universal, so I have not edited it here — flagging it rather than quietly widening a range that belongs to #700.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 8c23028

bong-water-water-bong pushed a commit that referenced this pull request Nov 2, 2026
…re ~1.8x high

Re-sweeping the rows table with the fixed probe (#862's threshold evidence) exposed a measurement trap:
in a ticket whose first measurement is a small-row cell, device ms/call comes out a uniform ~1.84x
inflated across all five shapes and settles by the sixth configuration -- 1024x1024 1.57 -> 0.81 ms,
1024x2048 2.87 -> 1.56, 1024x3072 4.24 -> 2.31, 2048x1024 2.74 -> 1.49, 3072x1024 4.13 -> 2.26, same
binary, same designs, one process per cell with its own untimed warm-up call. A per-process warm-up is
therefore not enough to make the first cells quotable; the ticket is warm only after several
configurations.

One sentence in the table's preamble, no number changed: the table above it was already taken after nine
warm-up configurations of the same shape, which is why its 1-row figure is 1.501 ms and not ~2.9.
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

One more piece of evidence, and it corrects the instrument behind this PR's threshold rather than its conclusion.

The threshold's sweep was taken with the probe that had #853's divisor bug

kDeviceMinRows = 16 was picked from a sweep measured before #853's CHANGES was fixed: the probe then timed repeats-1 calls and divided by repeats, understating device ms/call by 5.3 % at repeats=20. That bias flatters the device, which is exactly the wrong direction for a threshold, so I re-ran the whole sweep with the fixed probe (6a17eca) instead of rescaling the old table — five shapes × rows 1,2,4,6,8,12,16,24,32, min of three runs per cell, repeats=20.

Ticket 01793663547020185958.qdq's follow-up, npu-hold -l qdq -t 8, 0 s wait, exit 0, all 45 cells ran on the array with exact int32 accumulators (RESULT: all five shapes swept, every run matched the exact host accumulators):

rows worst dev:host best dev:host verdict
1 14.51 7.40 all slower
2 5.55 4.58 all slower
4 3.10 2.16 all slower
6 1.71 1.54 all slower
8 1.23 1.16 all slower
12 0.86 0.76 all faster
16 0.63 0.57 all faster — device 1.59–1.75× ahead
24 0.41 0.39 all faster
32 0.32 0.29 all faster

The crossover is still between 8 and 12 rows and 16 is still the first round row count at which nothing is slower — so kDeviceMinRows = 16 stands, with more margin than the original (1.59–1.75× at 16 rows here, against the 1.47× the buggy sweep reported). Nothing in this PR's code or docs needs to change; this is the evidence being re-taken with a sound instrument.

What the re-sweep also caught: the array ramps, and it cost me a wrong first reading

My first pass through the output looked like a contradiction — device ms/call at rows 1–4 was 1.4–4.3 ms, while #853's table had measured 1.501 ms for the same shape at 1 row. It is not a shape effect: the inflation is a uniform ~1.84× across all five shapes at rows=1 (kv 1.57 vs 0.81, q 2.87 vs 1.56, gu 4.24 vs 2.31, o 2.74 vs 1.49, down 4.13 vs 2.26), decaying to steady state by ~row 6, same binary, same designs, one process per cell with its own untimed warm-up call. That is a device/CPU clock ramp at the start of a cold ticket — this ticket began with the rows=1 cells, while #853's table began after nine warm-up configurations of the same shape.

Two consequences, both already reflected upstream:

  • the rows≥6 numbers — the ones the crossover rests on — are steady-state and flat in rows (0.80, 1.51, 2.3, 1.5, 2.25 ms at every row count from 6 to 32), so the threshold's evidence is sound;
  • #853 now carries the caveat in docs/onnx.md (6a17eca): a cold ticket's first cells are ~1.8× high and are not quotable. Worth knowing before anyone re-derives those numbers.

This PR is now dd875de, rebased onto 6a17eca; my five files are untouched by that hop (the only delta from 8c23028 is the inherited base sentence). Host ctest 6/6 and the device gate above both still describe this tree, since the base change is documentation only.

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit dd875de

@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: CHANGES at 00a271c2e — one structural item, no tprs-ready label. The code itself verifies; the branch cannot land where it sits.

1. The blocker: this PR is stacked on #853's branch, and #853 is CHANGES and CONFLICTING. baseRefName is
1bit/onnx-ep-qdq-q8-probe = #853 (2a15e2157). #853 is blocked on a measurement bug — its probe runs
for (int r = 1; r < repeats; r++) and then divides by repeats
(npu/ort/onebit_npu_gemm_q8_probe.cpp:94 vs :61), so the device column is (repeats-1)/repeats of the truth and
repeats=1 prints device 0.000 ms/call speedup 1538.23x and PASSes — and that probe is still on this branch.
Merging #862 in its position would put the buggy tool on main. Two ways out, both cheap:

  • fix #853 (one line, r = 0), land it, then this branch rebases onto it; or
  • retarget #862 to main — its own files (onnx/ep/onebit_ep_kernel.{h,cpp}, onebit_ep_qdq_proof.cpp,
    docs/onnx.md, docs/onnx-pack.md) do not overlap #853's (npu/ort/onebit_npu_gemm_q8_probe.cpp,
    onnx/ep/CMakeLists.txt), so the retarget is mechanical and it merges on its own.

2. The threshold survives that bug, and I re-derived it. #853's divisor makes the device look 5.3 % faster than it
is at repeats=20; dividing every device cell of your table by 0.95 leaves the crossover where you say it is:
rows 8 still slower in all five shapes (1.06-1.28x -> 1.01-1.22x), rows 12 still faster in all five
(1.19-1.39x -> 1.13-1.32x), rows 16 faster by 1.40x at worst. So kDeviceMinRows = 16 is not just safe, it is
conservative — the honest "nothing is slower" row count is 12. Say that in the body (or retake the column with the
fixed probe) so the table is not read as repeats-exact.

3. What I built and ran at this head (GCC, thermal-run -j4, -DONEBIT_ORT=ON, ORT 1.23.2 from pkg-config):

ctest -R "ort_info|onebit_ep"   6/6 passed   (ort_info, onebit_ep_proof, …_cpu_fallback,
                                              onebit_ep_qdq_proof, …_unfused, …_qdq_only)

— the same 6/6 you claim, including the three qdq-proof arms the new assertion sits in. And the seven new lines do
compile where they are load-bearing: ONEBIT_EP_NPU is a separate option (the EP's own option(), not the engine's
ONEBIT_NPU), so I reconfigured with -DONEBIT_EP_NPU=ON -DONEBIT_NPU_RT_DIR=… and onebit_ep_kernel.cpp — the
file holding if (m < kDeviceMinRows) trace_fallback(...) — builds clean with that define; the target only fails
later, on npu/ort/onebit_npu_gemm.cpp needing q4nx_weight_layout.h, which is the documented
ONEBIT_NPU_ENGINE_INCS knob and not this PR.

4. Read and agreed, with one consequence worth stating. trace_fallback for a choice is the right call —
"neither npu_qdq_host_rows nor npu_qdq_fallback" stays exactly "the device ran this shape", and the header
comment says why. Two consequences:

  • the routing assertion never runs in CI: it is gated on ONEBIT_EP_NPU_ELFS (onebit_ep_qdq_proof.cpp), and CI
    builds neither ONEBIT_EP_NPU nor the ELFs, so a regression in this routing is caught only by a hand run with
    designs configured. That is what makes #853's probe worth fixing before this merges — it is the instrument.
  • rows 12 stay on the host at ~1.2-1.4x their device speed. That is a few ms in a prefill chunk and you say so; the
    shape-aware threshold you left out is the right thing to leave out.

Once it is on main (retargeted, or after #853 with its probe fixed) I re-verdict in minutes: this is a 7-line
change in a file I have already built both ways.

@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 dd875dee2 — supersedes my CHANGES of 00a271c2e. Both halves of that verdict are now answered:
the branch is a single clean commit on 6a17ecacc, and #853's probe divisor is fixed on that base
(for (int r = 0; r < repeats; r++) in the device leg, so the repeats=1 -> 1538x artifact is gone). Nothing in this
PR reintroduces it.

Order it goes in: #853 -> #862. dd875dee2's parent is 6a17ecacc = #853's head, so #862 carries #853 with it;
that is now fine because #853 is READY and mergeable (BEHIND, no conflicts).

Built and run at this head (GCC, thermal-run -j4, ORT 1.23.2 from pkg-config):

-PDONEBIT_ORT=ON                          ctest -R "ort_info|onebit_ep"  ->  6/6 passed
                                          (incl. onebit_ep_qdq_proof + its unfused and qdq_only arms)
-DONEBIT_EP_NPU=ON -DONEBIT_NPU_RT_DIR=…  onebit_ep links, so the 7 new lines compile where they exist

The two new proof cases are the right addition: quark-kv (1024x1024) and quark-gu (1024x3072) now go through the
same |d| = 0 gate, and they are the shapes a decode step actually spends its 196 launches on.

The arithmetic, re-derived once more against the fixed divisor (which moves the device column by +5.3 %, so this
subsumes my earlier note): rows 8 still slower on all five shapes, rows 12 still faster on all five (1.13-1.32x),
rows 16 faster by >= 1.40x. kDeviceMinRows = 16 is therefore conservative, not tight — the honest "nothing is
slower" count is 12. Two things follow, neither a gate:

  1. the body's 45-configuration table was taken with the pre-fix probe (the branch carried r = 1 at the time), so its
    device cells are ~5 % optimistic. The conclusion is unchanged (see above), but a retake from the now-fixed probe
    would make the table repeats-exact, and docs/onnx.md's "crosses over at ~8-16 rows" would agree with #853's own
    row;
  2. rows 12 stay on the host at ~1.2-1.4x their device speed. A few ms per prefill chunk; you said so and left the
    shape-aware threshold out on purpose, which I agree with.

The routing assertion is still CI-invisible (ONEBIT_EP_NPU_ELFS unset in CI, and CI does not build ONEBIT_EP_NPU), so
the evidence is the author's run plus my compile. That is the one thing I would keep on the lane's list rather than a
blocker: the instrument exists now, and it is fixed.

@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
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit c590f1f

bong-water-water-bong added a commit that referenced this pull request Nov 3, 2026
… and slower than the host at decode (#853)

* onnx EP: measure the A8W8 kernel on both legs, and it is dispatch-bound

onebit_npu_gemm_q8_probe runs C[m,n] = A[m,k] @ B[k,n] with int8 operands through gemm_q8_raw on the NPU
and through the EP's own host loop, requires the exact int32 accumulators to be identical (an exact
integer sum cannot differ, so a mismatch is a layout or a truncated K, not rounding), and prints ms/call
for both. It is a ctest entry that skips when the shape has no design, because it measures the kernel
rather than gating the graph (the QDQ proof does that).

* docs/onnx: the A8W8 device leg is dispatch-bound, and is slower than the host loop at decode widths

Measured with the new probe on strixhalo (-O2, M=32 designs, npu-hold ticket): a call costs 1.44-1.50 ms
for q_proj at 1 row and at 32, and an M=128 design at one row (1.606 ms) is no slower than M=32 (1.441),
so the launch dominates. At one row that is ~9x the EP's own host loop for the same projection; it crosses
over between 8 and 16 rows and is 3.2-3.9x ahead at a full 32-row design. Stated so nobody reads the
196-of-197 coverage in docs/onnx-pack.md as a decode speedup.

* onnx EP: time the q8 probe's calls, don't divide the warm-up by repeats

The device column timed `repeats-1` calls (the first call is the untimed warm-up) and divided by
`repeats`, so every figure was (repeats-1)/repeats of the truth: 5.3 % low at the timing script's
repeats=20, 20 % low at the ctest entry's 5, and repeats=1 printed `device 0.000 ms/call` with a
four-digit speedup and still exited 0. Both legs now time exactly `repeats` calls after one untimed
device call, and repeats < 2 is refused with exit 2.

Re-measured through the fixed probe (npu-hold ticket 01793663361223459193.qdq, 10 s, load 9-10, min of
three runs per cell, 18/18 configurations matching the exact int32 accumulators): q_proj is 1.501 ms at
1 row and 1.566 ms at 32 rows -- 32x the work for 4 % more time -- crossing over between 8 and 16 rows and
3.1-3.4x ahead at a full 32-row design, so the dispatch-bound conclusion is unchanged and its numbers are
now the measured ones. The repeats sweep is flat (rows=1: 1.643 / 1.705 / 1.542 / 1.515 ms at repeats
2 / 5 / 20 / 21, previously 0.793 / 1.254 / 1.454 / 1.579 -- it was measuring the divisor).

docs/onnx.md: the corrected table, the host column stated as a lower bound (accumulation loop only, the
EP's real fallback then runs epilogue_row), the M=128-vs-M=32 comparison stated as within run-to-run
variance (1.233 vs 1.499 here, 1.606 vs 1.441 earlier) instead of a too-tight range, and a note that the
earlier table was ~5 % low. The probe's header example design is now an M=32 artifact: the _m16 ELF it
named cannot be emitted (n1_core_i8_v26.py asserts M % m == 0, m=32).

* docs/onnx: say that the array ramps, so a cold ticket's first cells are ~1.8x high

Re-sweeping the rows table with the fixed probe (#862's threshold evidence) exposed a measurement trap:
in a ticket whose first measurement is a small-row cell, device ms/call comes out a uniform ~1.84x
inflated across all five shapes and settles by the sixth configuration -- 1024x1024 1.57 -> 0.81 ms,
1024x2048 2.87 -> 1.56, 1024x3072 4.24 -> 2.31, 2048x1024 2.74 -> 1.49, 3072x1024 4.13 -> 2.26, same
binary, same designs, one process per cell with its own untimed warm-up call. A per-process warm-up is
therefore not enough to make the first cells quotable; the ticket is warm only after several
configurations.

One sentence in the table's preamble, no number changed: the table above it was already taken after nine
warm-up configurations of the same shape, which is why its 1-row figure is 1.501 ms and not ~2.9.

---------

Co-authored-by: bong-water-water-bong <[email protected]>
Base automatically changed from 1bit/onnx-ep-qdq-q8-probe to main November 3, 2026 02:10
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Status: cannot be merged in this shape — it needs a rebase, and the rebase is mechanical. #853 landed (squash
bc291cd15), GitHub retargeted this PR to main, and the branch still carries #853's pre-squash commits, so
update-branch is refused:

$ gh api -X PUT .../pulls/862/update-branch
{"message":"merge conflict between base and head"}   (422)   state=dirty

What conflicts, and what fixes it, measured in a clone at origin/main = bc291cd15:

$ git merge-tree --write-tree origin/main pr862
CONFLICT (content): Merge conflict in docs/onnx.md          <- the only conflict

$ git checkout -B t862 origin/main && git cherry-pick dd875dee2
[t862 f1f4628] onnx EP: gemm_q8 routes by rows, …
 5 files changed, 56 insertions(+), 3 deletions(-)          <- clean, no conflict

So git rebase origin/main on 1bit/onnx-ep-qdq-rows (letting it skip the #853 commits it already contains, which are on
main as the squash) puts the PR's own commit on main with no hand resolution. Please do it on the lane side; I do not push
to other branches.

Everything I owe at this PR is already measured, and it carries: at c590f1fad I built this tree with
-DONEBIT_ORT=ON and ran ctest -R "ort_info|onebit_ep" -> 6/6 passed (onebit_ep, onebit_ep_proof,
onebit_ep_proof_cpu_fallback, onebit_ep_qdq_proof, onebit_ep_qdq_proof_unfused, onebit_ep_qdq_proof_qdq_only,
2.16 s total), and the rows threshold re-derives as conservative: rows 12 is faster in all five shapes (1.13-1.32x), rows 16
by >= 1.40x, so 16 is a floor rather than a tuning.

One caveat that is not this PR's defect: the device leg cannot be rebuilt on current main because -DONEBIT_EP_NPU=ON
stopped compiling with #869 — filed as #874 with the one-line fix (q4nx_elf_arg_sizes() is called from
npu_engine_i8ctx_inc.h:219 with no declaration in its include chain).

When it is rebased, ping me and I re-post the verdict at the new head — the check is the same six ctests plus the
onebit_ep link in the ONEBIT_EP_NPU configuration.

…dispatch-bound device call

so at one row it is ~9x slower than the EP's own -O2 host loop and it only wins from ~8-16 rows up.
gemm_q8 sent every call to the array anyway, which made a decode step pay 196 dispatch-bound
launches for a multiply the host does in tens of milliseconds.

kDeviceMinRows = 16 (one constant, in onebit_ep_kernel.h, no duplicated literal in the test): below
it the host loop runs and the call never reaches the array; at or above it the device does. A
host-routed call is a choice, not a failure, and says so in the trace (npu_qdq_host_rows), so the
device discriminator gets stronger: a device run is still 'neither npu_qdq_host_rows nor
npu_qdq_fallback was written for this shape'. The proof reads that line out of each case's own trace
slice and asserts the routing of every fused case against the same constant.
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 7fff297

@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 7fff297f9. The rebase did exactly what was needed: it is now a single commit on main (8bb…-free), five files
+56/-3 — which is precisely the cherry-pick dd875dee2 result I measured when I reported the conflict — and the #853
commits it used to carry are gone.

Re-ran the gate at this head rather than carrying it, in the ORT build tree:

cmake --build . --target onebit_ep_qdq_proof onebit_ep_proof ort_info      rc 0
ctest -R "ort_info|onebit_ep"  ->  6/6 passed  (onebit_ep, onebit_ep_proof, onebit_ep_proof_cpu_fallback,
                                   onebit_ep_qdq_proof, onebit_ep_qdq_proof_unfused, onebit_ep_qdq_proof_qdq_only)

The threshold is conservative in the direction that matters: rows 12 is faster in all five shapes (1.13-1.32x) and rows 16
by >= 1.40x, so the switch at 16 is a floor rather than a tuned constant — a decode step stops paying the ~1.4-1.5 ms
device call and a prefill-width step keeps the kernel.

CI at 7fff297f9: build, CodeQL, Analyze x4 and pr_agent all success.

Unchanged caveat, not this PR's: the device leg still cannot be rebuilt on main because -DONEBIT_EP_NPU=ON fails to
compile since #869 — issue #874, one-line fix. The six tests above are the host configuration, which is what CI runs.

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit bd0d1bb

@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 bd0d1bb25 (merge of eec6d2231 into 7fff297f9), superseding my line at 7fff297f9. All five of this PR's files
are blob-identical at both heads, so the merge added nothing; the ctest -R "ort_info|onebit_ep" 6/6 I ran at
7fff297f9 is the result for this head too, and CI at bd0d1bb25 is build success.

Unchanged caveat, not this PR's: the device leg cannot be rebuilt on main until #874 (one #include) lands — the six
tests above are the host configuration, which is what CI runs.

@bong-water-water-bong
bong-water-water-bong merged commit aa6db8d into main Nov 3, 2026
8 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/onnx-ep-qdq-rows branch November 3, 2026 03:22
bong-water-water-bong added a commit that referenced this pull request Nov 3, 2026
…t 32 rows (#909)

* onnx EP: give the three host-routed projection shapes a device gate at 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.

* docs: the RSS band is indicative, not a bound (a later run of the same 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.

---------

Co-authored-by: pi-qdq <[email protected]>
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