Repository navigation
onnx EP: gemm_q8 routes by rows, so a decode step stops paying for a dispatch-bound device call - #862
Conversation
PR Reviewer Guide 🔍(Review updated until commit bd0d1bb)Here are some key observations to aid the review process:
|
2a15e21 to
71d94dc
Compare
00a271c to
8c23028
Compare
|
Rebased onto its new base ( The rebase was a content conflict, and worth flagging
Gates on
|
|
Persistent review updated to latest commit 8c23028 |
…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.
8c23028 to
dd875de
Compare
|
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
Ticket
The crossover is still between 8 and 12 rows and 16 is still the first round row count at which nothing is slower — so What the re-sweep also caught: the array ramps, and it cost me a wrong first readingMy first pass through the output looked like a contradiction — device ms/call at rows 1–4 was 1.4–4.3 ms, while Two consequences, both already reflected upstream:
This PR is now |
|
Persistent review updated to latest commit dd875de |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
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 neitherONEBIT_EP_NPUnor 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
left a comment
There was a problem hiding this comment.
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:
- the body's 45-configuration table was taken with the pre-fix probe (the branch carried
r = 1at 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 tablerepeats-exact, anddocs/onnx.md's "crosses over at ~8-16 rows" would agree with #853's own
row; - 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.
|
Persistent review updated to latest commit c590f1f |
… 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]>
|
Status: cannot be merged in this shape — it needs a rebase, and the rebase is mechanical. #853 landed (squash What conflicts, and what fixes it, measured in a clone at So Everything I owe at this PR is already measured, and it carries: at One caveat that is not this PR's defect: the device leg cannot be rebuilt on current 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 |
…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.
c590f1f to
7fff297
Compare
|
Persistent review updated to latest commit 7fff297 |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
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.
|
Persistent review updated to latest commit bd0d1bb |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
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.
…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]>
onnx EP:
gemm_q8routes 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
-O2host loop and only wins from~8-16 rows up.
gemm_q8sent every call to the array anyway wheneverONEBIT_EP_NPU_ELFSis configured, so adecode 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): belowkDeviceMinRowsthe host loop runs and the call neverreaches 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 devicerun is still "neither
npu_qdq_fallbacknornpu_qdq_host_rowswas written for this shape".onnx/ep/onebit_ep_qdq_proof.cpp: the proof readsnpu_qdq_host_rowsout of each case's own trace slice andasserts the routing of every fused case against the same
kDeviceMinRows(the header is included, so theconstant 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,
-O2build, onenpu-holdticket on strixhalo (load 14.4 at grant, so the host column is the conservative one — a busy boxinflates 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.
(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-1calls anddivided 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 = 0inboth legs,
repeats=20), because a corrected divisor has to be shown measuring. All 45 configurations ran onthe array with exact int32 accumulators equal to the host loop's, one
npu-holdticket, exit 0: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.87ms are both "the device at one row".
Measured gate
Routing, read out of the EP's trace of the proof run (
npu_qdq_host_rowsis written by the kernel,qdq rows=by the shared epilogue on both paths, so only the former says which leg was chosen):
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):ctest6/6,worst |d| = 0/0/0/0LSB, per-K scalestill 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
gemm_f32leg is untouched. It has the same dispatch-bound shape, but no design exists on thisbox 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.
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.