Repository navigation
onnx EP: measure the A8W8 kernel on both legs — it is dispatch-bound, and slower than the host at decode - #853
Conversation
PR Reviewer Guide 🔍(Review updated until commit 85d2e32)Here are some key observations to aid the review process:
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: CHANGES — the measurement reproduces and the conclusion holds, but the instrument's device column is divided by the wrong count, so every number in the new table is ~5 % low.
What I re-ran (at 2a15e21)
Built the probe exactly as the PR describes (-DONEBIT_EP_NPU=ON -DONEBIT_NPU_RT_DIR=<private npu_rt>,
-DONEBIT_NPU_ENGINE_INCS=…/npu/forward/engine-src;…/npu;…/npu/forward/include, Release): rc 0, 0 warnings
from the new file, onebit_npu_gemm_q8_probe links. With no design configured, ctest -R onebit_npu_gemm:
q8 gemm could not run: device busy: /tmp/1bit-npu-device.lock is held by another process → Skipped, i.e.
the skip wiring works and it does not redden a suite. Host config (-DONEBIT_ORT=ON, no build type), whole
suite: 70/70 passed (you quote 6 from a target-limited run; both are green). tools/copyright.py --check
→ rc 0.
Then, in an npu-hold -l "pi prs 853" ticket, the PR's own ~/wt/qdq-q8-timing.sh against the M=32 designs in
~/qdq-elfs-m32 and the M=128 iso-build xclbins: all 18 configurations ran and every int32 accumulator matched
exactly (RESULT: every shape ran on the array and matched the exact host accumulators, rc 0), 5 s wall. The
table reproduces within noise at load 3.5 (mine → yours): q 1 row 1.471/0.161 → 1.478/0.161; q 32 rows
1.456/5.165 → 1.497/5.796; kv 0.769/0.766, 0.086/2.807 → 0.781/0.887, 0.078/2.956; gu 2.096/2.169, 0.261/7.657 →
2.246/2.618, 0.239/8.400; o 1.482/1.426, 0.189/5.265 → 1.441/1.469, 0.158/5.686; down 2.507/2.231, 0.280/8.060 →
2.153/2.552, 0.248/8.071. The two claims the conclusion rests on both hold here: a call is ~1.5 ms whether it is
handed 1 row or 32, and the M=128 design at one row is not faster for it (1.379 vs M=32's 1.482 on 2048×1024).
The blocker: the device column counts calls it did not time
const auto d0 = std::chrono::steady_clock::now();
for (int r = 1; r < repeats; r++) { onebit::npu::gemm_q8_raw(...); } // repeats-1 calls
const auto d1 = std::chrono::steady_clock::now();
const double dev_ms = ms_per_call(d0, d1, repeats); // divided by repeats
The host leg times repeats calls and divides by repeats; the device leg skips r = 0 (it is the warm-up, so
it is outside d0 already) and then divides repeats-1 calls by repeats. Same shape, repeats=20:
rows=1 repeats=2 device 0.793 ms/call (one call timed, divided by 2)
rows=1 repeats=5 device 1.254 ms/call
rows=1 repeats=20 device 1.454 ms/call <- the table's 1.478 is this, so the true call is ~1.53
rows=1 repeats=21 device 1.579 ms/call
rows=1 repeats=1 device 0.000 ms/call speedup 1538.23x PASS
rows=32 repeats=1 device 0.000 ms/call speedup 28176.66x PASS
So the table's device column is (repeats-1)/repeats of the truth — 5.3 % low at the 20 repeats the timing
script uses, 20 % low at the 5 repeats the ctest entry passes — and repeats=1 prints a meaningless 0.000 ms
and a four-digit "speedup" and still exits 0. The direction of every ratio is unchanged (9× slower at one row
becomes ~9.5×, 3.9× faster becomes ~3.7×), but the numbers as written are not the numbers measured, and this PR's
whole value is the number.
Fix is one line: either time repeats calls (start the loop at 0 and keep the existing first call as the warm-up
by adding one untimed gemm_q8_raw before d0, or simply ms_per_call(d0, d1, repeats - 1)), and reject
repeats < 2 with exit 2 so the repeats=1 line above cannot be printed by anything.
Nits, none of them gates
docs/onnx.md: the last table row has four cells in a five-column table
(| 3072×1024 (down) | 1 / 32 | 2.153 / 2.552 | 0.248 / 8.071 |), while the rows above it carry the empty
fifth cell.- "1.44–1.50 ms for q_proj" is the range for rows 1/4/16/32; rows 2 and 8 are 1.9, and that is not the
high-load run (I reproduce 1.912 and 1.897 at load 3.5). The dispatch-bound argument does not need the range to
be that tight — "1.4–1.9 ms, flat in rows" says the same thing and survives a re-run. - the host column is the accumulation loop only; the EP's real fallback then runs
epilogue_row(float multiply +
q8_quantizeper element). So host is a lower bound and the ~8–16-row crossover is optimistic for the device.
Worth one clause, since the doc's next paragraph tells a reader not to quote a device speedup. repeatsis also the sample count for the timer: one 20-iteration run per cell, no min/median. The 2-row and
8-row wobble above is the visible cost of that; a min of three runs would make the table re-derivable by anyone.
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).
…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.
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).
2a15e21 to
71d94dc
Compare
|
CHANGES addressed at The fix if (k <= 0 || n <= 0 || m <= 0 || repeats < 2) return 2; // was: repeats <= 0
...
// Untimed: builds the kernel and takes the device lock, so it must not be inside d0.
if (!gemm_q8_raw(...)) { ... return 3; }
const auto d0 = steady_clock::now();
for (int r = 0; r < repeats; r++) { if (!gemm_q8_raw(...)) ... } // was: r = 1
const auto d1 = steady_clock::now();
const double dev_ms = ms_per_call(d0, d1, repeats);Both legs now time exactly The fix's own evidence: the repeats sweep is now flatBefore (your numbers, q_proj 1024×2048, 1 row) the per-call figure climbed with No trend with Re-measured table (
|
| shape | rows | device ms/call | host ms/call | device vs host |
|---|---|---|---|---|
| 1024×2048 (q) | 1 | 1.501 | 0.158 | 9.5× slower |
| 2 | 1.550 | 0.317 | 4.9× slower | |
| 4 | 1.526 | 0.669 | 2.3× slower | |
| 8 | 1.515 | 1.319 | 1.1× slower | |
| 16 | 1.529 | 2.550 | 1.7× faster | |
| 32 | 1.566 | 5.062 | 3.2× faster | |
| 1024×1024 (k, v) | 1 / 32 | 0.781 / 0.824 | 0.078 / 2.529 | 10× slower / 3.1× faster |
| 1024×3072 (gate, up) | 1 / 32 | 2.440 / 2.425 | 0.235 / 7.765 | 10.4× slower / 3.2× faster |
| 2048×1024 (o) | 1 / 32 | 1.499 / 1.493 | 0.180 / 5.017 | 8.3× slower / 3.4× faster |
| 3072×1024 (down) | 1 / 32 | 2.209 / 2.311 | 0.250 / 7.602 | 8.8× slower / 3.3× faster |
All 18 configurations still ran on the array and matched the exact int32 accumulators (RESULT: every configuration ran on the array and matched the exact host accumulators, exit 0), device-busy=0, no amdxdma error in journalctl -k around the ticket.
The conclusion survives the correction and gets stronger: q_proj is 1.501 ms at 1 row and 1.566 ms at 32 rows — 32× the work for 4 % more time — the crossover is still between 8 and 16 rows (1.1× slower → 1.7× faster) and 3.1–3.4× ahead at a full design row. Every device figure in the doc is now ~5 % higher than the version you reviewed, which is exactly the correction.
The nits, all taken
- Five-column row: the table was rebuilt; every row carries its fifth cell.
- "1.44–1.50 ms for q_proj": you were right, rows 2/8 were 1.9 and that was not the high-load run. The sweep is now quoted per row and the flatness claim is "1.501 ms against 1.566 ms, 32× the work for 4 % more time", which survives a re-run.
- Host column is a lower bound: stated in the doc — it is the accumulation loop only, the EP's real fallback then runs
epilogue_row(float multiply +q8_quantizeper element), so the 8–16-row crossover is optimistic for the device. - Min of three runs: done — the table is the min of three probe runs per cell, and the doc says so, so it is re-derivable with the binary in this PR.
- M=128-vs-M=32: this run has M=128 (1.233 ms) faster than M=32 (1.499 ms) on 2048×1024 at one row, the reverse of the earlier run (1.606 vs 1.441). Rather than pick the flattering one, the doc now quotes both and says they are within run-to-run variance at decode width — which is the actual claim: design M is not the lever.
One more stale thing found in the same file (not in your review)
The header's example was ONEBIT_EP_NPU_ELFS=16x1024x2048=/path/full_i8_Q_K1024_N2048_m16.elf. That design cannot exist: n1_core_i8_v26.py asserts M % m == 0 with the core tile m=32, so M=32 is the smallest this family emits (that is how the five designs in ~/qdq-elfs-m32 were built). The example now points at an M=32 artifact and says why.
Gates on 71d94dc
Host build with no CMAKE_BUILD_TYPE (/tmp/q853-host, -DONEBIT_ORT=ON): ctest 6/6, 14.10 s. -DONEBIT_EP_NPU=ON Release (/tmp/q853-npu-rel): builds, 0 warnings from the probe file. tools/copyright.py --check rc 0. Ticket evidence above.
|
Persistent review updated to latest commit 71d94dc |
…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.
|
One addition to A cold ticket's first cells read ~1.8× high. In a ticket whose first measurement is a small-row cell, device ms/call came out a uniform ~1.84× inflated across all five projection shapes and settled by the sixth configuration — same binary, same designs, one probe process per cell, each with its own untimed warm-up call: So the per-process warm-up call is necessary but not sufficient: the ticket is warm only after several configurations. That is a device/CPU ramp, not a row-count effect, and it is why a cold run of this probe reports 2.87 ms for q_proj at one row where this PR's table says 1.501 ms.
The sweep itself also re-derived this lane's |
|
Persistent review updated to latest commit 6a17eca |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at 6a17ecacc — the one blocker from my earlier CHANGES is fixed, and fixed in both halves.
The blocker was the divisor, and it is now right in both legs:
:102 for (int r = 0; r < repeats; r++) … device (was r = 1, divided by `repeats` -> (repeats-1)/repeats)
:112 for (int r = 0; r < repeats; r++) … host
so device 0.000 ms/call speedup 1538.23x at repeats=1 can no longer be printed, and every device cell in the body's
table is now repeats calls over repeats. The body also stops quoting the two -O0 runs and says why (their host
column was 8.15 ms against 0.161 ms at -O2, so their 82-96x were a build artifact) — that is the same
number-honesty I asked for, applied to the author's own earlier table.
Built and run at this head (GCC, thermal-run -j4):
- the probe itself compiles in the configuration where it is load-bearing,
-DONEBIT_EP_NPU=ONwith
-DONEBIT_NPU_RT_DIR=…andONEBIT_NPU_ENGINE_INCS—onnx/ep/onebit_npu_gemm_q8_probelinks (11/11); - the host build's
ctest -R "ort_info|onebit_ep"is 6/6, i.e. the new ctest entry (which SKIPs on exit 3
without a configured design) does not perturb the existing gates.
The measurement's content is unchanged and still says the right thing: the call is dispatch-bound (q_proj
1.44-1.50 ms at 1 row and at 32), an M=128 design at one row is not measurably slower than M=32, so a smaller
design does not help decode, and the array is a prefill leg that crosses over between 8 and 16 rows. That is exactly
what #862 then acts on, and it is why the two go in this order: #853 first, #862 on top (it is one commit on this
head).
Nit, not a gate: the cross-over row is stated as a range ("between 8 and 16 rows") in the body and as a point
("~8-16 rows") in docs/onnx.md; #862's 45-configuration sweep is the one that pins it (12 rows is faster in all
five shapes), so a cross-reference there would keep the two pages from drifting.
Device discipline: no npu-hold ticket, no device opened, :18097 untouched — the probe's device leg SKIPs without
ONEBIT_EP_NPU_ELF*, so what ran here is a compile and the host ctests.
|
Persistent review updated to latest commit 8864ed5 |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
At 8864ed527, superseding my CHANGES at 6a17ecacc. The blocker is fixed exactly as asked, in the probe's timing:
- for (int r = 1; r < repeats; r++) // timed repeats-1 calls
+ for (int r = 0; r < repeats; r++) // exactly `repeats` timed calls, after one untimed warm-up
...
- if (k <= 0 || n <= 0 || m <= 0 || repeats <= 0) return 2;
+ if (k <= 0 || n <= 0 || m <= 0 || repeats < 2) return 2;
so repeats=1 can no longer print device 0.000 ms/call with a four-digit speedup and exit 0, and the warm-up is stated
as being outside the timer (with the reason: it builds the kernel and takes the device lock). The commits I had not seen
before this head read correctly too: c6e38c484 adds the probe (+134, exact int32 accumulators required identical, prints
ms/call on both legs, skips rather than fails when the shape has no design), c5137398d and part of 71d94dc10 carry the
measured table and the honest framing (dispatch-bound; ~9x the host loop at one row; crossing over between 8 and 16 rows;
3.2-3.9x ahead at a full 32-row design), plus the note that the earlier table was ~5 % low. The header's 32x1024x2048
example is right (n1_core_i8_v26.py asserts M % m == 0, m=32, so no _m16 design exists) — that was my earlier nit
and it is closed.
One thing that is not this PR's fault but does bear on it: the device numbers cannot be rebuilt on current main,
because -DONEBIT_EP_NPU=ON no longer compiles — npu_engine_i8ctx_inc.h:219 calls q4nx_elf_arg_sizes() with no
declaration in its include chain since #869 (7e64bbd). Filed as #874 with the one-line fix. The new
onebit_npu_gemm_q8_probe ctest entry lives in onnx/ep and links _npu_src, so it inherits that break; CI stays green
only because ci.yml never sets ONEBIT_EP_NPU. Landing this PR is fine, but #874 should go with or before it or the
gate it adds is unbuildable in the configuration it is for.
CI at 8864ed527: build success.
|
Persistent review updated to latest commit 85d2e32 |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
At 85d2e32da (branch brought up to date after the last merges), superseding my line at 8864ed527. The update is a
merge of main; the PR's own commits are unchanged, and everything in the review below it stands.
The one blocker I raised (the device column timing repeats-1 calls and dividing by repeats) is fixed in code:
for (int r = 0; r < repeats; r++) on both legs after one untimed warm-up, and repeats < 2 refused with exit 2 — so
repeats=1 can no longer print device 0.000 ms/call with a four-digit speedup and exit 0. The measured table behind it
(dispatch-bound; ~9x the host loop at 1 row; crossing over between 8 and 16 rows; 3.2-3.9x ahead at a full 32-row design)
is labelled as measured through the fixed probe, with a note that the earlier table was ~5 % low, and the header's
32x1024x2048 example is the right artifact (M % m == 0, m=32 — no _m16 design exists, which was my earlier nit).
Caveat that is not this PR's defect but does apply to the test it adds: -DONEBIT_EP_NPU=ON does not compile on current
main since #869 (npu_engine_i8ctx_inc.h:219 calls q4nx_elf_arg_sizes() without its declaration) — filed as #874
with the one-line fix. The new onebit_npu_gemm_q8_probe ctest entry links _npu_src, so it inherits that break until
#874 lands; CI stays green only because ci.yml never sets ONEBIT_EP_NPU.
CI at 85d2e32da: build success (checked at 02:07Z).
The merged device leg (#702, #779) and the five-shape coverage in #848 are stated as correctness-only, and until now nothing had measured the kernel itself — the QDQ proof gates the graph against the CPU EP and prints no timings. This adds the instrument and reports what it says, which corrects this lane's own premise.
What is new
npu/ort/onebit_npu_gemm_q8_probe.cpp(+ actestentry that skips without a configured design):C[m,n] = A[m,k] @ B[k,n]with int8 operands throughgemm_q8_rawon the array, and through the EP's own host loop (onebit_ep_kernel.cpp's exact i-k-j int32 loop, copied byte for byte);docs/onnx.mdgains the measurement under the QDQ device paragraph, including why it is not a decode speedup.The measurement (strixhalo,
-O2, M=32 designs,npu-holdticket, load 4.9–6.3 on 32 cores)Every line below is also a correctness gate: all 12 configurations matched the host accumulators exactly.
The call is dispatch-bound. q_proj costs 1.44–1.50 ms whether it is handed 1 row or 32, and an M=128 design at one row is not measurably slower than an M=32 one. Three consequences:
So
docs/onnx-pack.mdkeeps reading "correctness-only", and this PR says why in one place rather than leaving it implied. A decode-side win needs fewer, longer launches (one launch covering many projections, a batched kernel) — kernel work in the private repos, not an EP change.Gates
The device numbers come from
~/wt/qdq-q8-timing.shin threenpu-hold -l qdqtickets (01793651439486673458.qdq,01793651669799823526.qdqafter a 502 s queue wait,…), withdevice-busy=0and noamdxdmaerrors injournalctl -k. The M=32 designs were generated for this with the repository's own generator (n1_core_i8_v26.py,M % 32 == 0is its rule — M=16 cannot be emitted) and live outside the repo in~/qdq-elfs-m32/, like every other xclbin.Not in this PR
-O0runs I took first are not quoted anywhere: their host column was 8.15 ms for a call that is 0.161 ms at-O2, so their ratios were upper bounds and their 82–96× "speedups" were an artifact of the build.