Skip to content

onnx: ONEBIT_EP_QDQ_PROF prices each leg of the fused QDQ path (the profile #1023 asks for) - #1032

Merged
bong-water-water-bong merged 1 commit into
mainfrom
1bit/onnx-qdq-prof
Nov 5, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
1bit/onnx-qdq-prof

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Root cause of the missing number. engine#1023 (merged) measured the device leg 1.13-1.16x slower than
the host kernel at model level — 1073.7/1105.1 against 947.4/950.6 ms/token, same ids, same KL — while the
per-design probe, at the same shapes, had the device kernels 1.7x ahead (712 against 1209 ms/pass). Both
cannot describe the same pass, and #1023 states the cause as open rather than guessing, because neither
explanation could be separated from outside the process:

  1. dispatch — 196 calls per pass, two launches each, each paying the A upload, two syncs and a wait;
  2. the probe's host column is unrepresentative — it is measured on synthetic int8 operands, and the host
    loop skips aik == 0, so the graph's sparser activations may make the real host loop much faster. The
    probe's own host column sums to 1209 ms/pass, above the whole 947 ms/token host arm, so it cannot be
    subtracted from anything; the rigorous bound is only that the device's kernel advantage is at most 1.33x.

#1023 ends with "the next step is a profile", and this is that instrument.

What this adds. ONEBIT_EP_QDQ_PROF=1: gemm_q8 accumulates its own wall time per leg and writes one
line per process at exit.

qdq_prof calls_dev=… calls_host=… rows_dev=… rows_host=… dev_call_ms=… dev_epi_ms=… host_ms=…

dev_call_ms is the device call (BO writes, launch, both syncs, the wait), dev_epi_ms the epilogue both
legs share, host_ms the whole host loop including its own epilogue. Two runs that differ only in the leg
then price that leg on the graph's own int8 bytes — which is the one thing a probe timing synthetic
operands cannot do. A running total per process rather than per call, so nothing is sampled and one line
answers the question.

trace_fallback is split into trace_line (the writer — unchanged open(0600) + O_NOFOLLOW from the
CodeQL fix) plus the formatting wrapper, so every note this file emits has one writer, and the prof line goes
to stderr when ONEBIT_EP_TRACE is off. A knob whose only output can silently vanish is how a debugging
session gets lost; this file already paid for that once with ONEBIT_EP_TRACE read as a path.

Measured, without a device (so the PR stands on its own before any hardware run):

  • unoptimized host build (the stricter correctness build): 6/6 green, and 6/6 green with the knob on,
    with the fused proof's gate unchanged — worst |d| = 0 LSB per-tensor / per-axis / asymmetric /
    asymmetric-per-axis, per-K scale still refused, qdq_prof line written once per process;
  • the knob's three cases run rather than assumed: off → zero qdq_prof lines; on with a trace → one line in
    /tmp/onebit_ep_trace.<pid>.log; on without a trace → the line on stderr;
  • Release ONEBIT_EP_NPU=ON build: onebit_ep_qdq_proof passes (1.0 s); ctest there is 8 tests of which
    onebit_ep_proof fails and the two NPU probes skip, exactly as main already does for an NPU build with no
    ELF configured (pre-existing, not this PR);
  • the same 17 calls cost 79.8 ms at -O3 and ~2000 ms at -O0 (25x) — onnx: the QDQ device leg is 1.13x slower than the host kernel at model level (measured; cause not isolated) #1023's -O0 trap seen from
    the other side, and a reminder that the arms must be a Release build.

Not in this PR. The measurement itself. It is armed behind ~/wt/qdq-prof-watch.sh (waits for a quiet
box, then one short npu-hold -l qdq ticket, sized to finish inside its hold) and its numbers will be
reported on #1023 and in ~/briefs/TQDQ-STATE.md, not invented here. No device hardware was touched to
write, gate or document this.

tools/copyright.py --check rc 0. Nothing pushed to main.

#1023 measured the device leg 1.13-1.16x slower than the host kernel at model level
(1073.7/1105.1 vs 947.4/950.6 ms/token, same ids, same KL) while the per-design probe, at
the same shapes, had the device kernels 1.7x ahead (device 712 ms/pass vs host 1209).
Both readings cannot be the whole story, and nothing separated the two candidates: per-call
dispatch, or the probe's host column being measured on synthetic int8 operands while the
graph's own activations are not (its host column sums to 1209 ms/pass, above the entire
947 ms/token host arm, so it cannot be subtracted from anything).

This is the instrument that separates them, without a profiler: one running total per
process of where gemm_q8's wall time went, split as the device call (BO writes, launch,
both syncs, the wait), the epilogue both legs share, and the host loop. Two arms that
differ only in the leg then price that leg on the graph's own int8 bytes.

  ONEBIT_EP_QDQ_PROF=1 -> qdq_prof calls_dev=... rows_dev=... dev_call_ms=... dev_epi_ms=...
                          host_ms=... (one line, at exit)

Verified without a device:
- unoptimized host build 6/6 green, and 6/6 green with the knob on, with the fused proof's
  gate unchanged (worst |d| = 0 LSB per-tensor / per-axis / asymmetric / asymmetric-per-axis,
  per-K scale still refused);
- the knob's three cases run: off -> zero qdq_prof lines; on with a trace -> one line in
  /tmp/onebit_ep_trace.<pid>.log; on without a trace -> the line on stderr;
- Release (ONEBIT_EP_NPU=ON) build: onebit_ep_qdq_proof passes; ctest there is 8 tests of
  which onebit_ep_proof fails and the two NPU probes skip, exactly as main already does for
  an NPU build with no ELF configured;
- the same 17 calls cost 79.8 ms at -O3 and ~2000 ms at -O0 (25x), the -O0 trap from
  2026-11-04 15:45 seen from the other side.

trace_fallback is split into trace_line (the writer, unchanged 0600 + O_NOFOLLOW) plus the
formatting wrapper, so every note this file emits has one writer and the prof line cannot go
silent when ONEBIT_EP_TRACE is off -- the same footgun as this file's old path-vs-flag bug.

The measurement this is for is armed on the NPU queue (one short npu-hold ticket, sized to
finish inside its hold); its numbers will be reported on #1023 and in
~/briefs/TQDQ-STATE.md, not in this diff. No device hardware was touched to write or gate
this.
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

#1032 ready for review at 7718955. Rebased onto current main 9bb1216 (it was cut from 434db3d, before #1023 landed), so the diff is the two files only: onnx/ep/onebit_ep_kernel.cpp and docs/onnx.md. This is the profile #1023 ends by asking for; the measurement it exists for runs on the NPU queue and its numbers will go into #1023 and ~/briefs/TQDQ-STATE.md rather than into this diff.

@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 7718955a

Reviewed the knob as instrumentation rather than as a result, because that is what it is: it arms a measurement and says so (the body's "Not in this PR. The measurement itself" is the right call — #1023 asked for a profile, and inventing the numbers here would have been the thing not to do).

What I checked in the code:

  • it prices exactly the two explanations #1023 could not separate — dev_call_ms is the device call (BO writes, launch, both syncs, the wait) and host_ms is the host accumulation loop, both on the graph's own int8 operands, both from the same clock, in the same process. The shared epi_ms is separated out for the same reason the docs said the probe's host column was a lower bound: the epilogue is common to both legs and would otherwise be counted as host.
  • off costs nothing measurable: the three steady_clock::now() calls are behind p.on ? ... : {}, so an unarmed run pays one getenv at first use and a branch per call.
  • its output cannot vanish silently, which is the bug this file already paid for: trace_line() is now the single writer (open 0600 | O_NOFOLLOW | O_CLOEXEC, then fdopen), and when ONEBIT_EP_TRACE is off the prof line still goes to stderr. Folding trace_fallback() onto the same helper keeps the two writers agreeing on mode and on the "ONEBIT_EP_TRACE is a flag, not a path" fix.
  • docs: the two docs/onnx.md insertions describe the field names it actually prints (calls_dev calls_host rows_dev rows_host dev_call_ms dev_epi_ms host_ms) and where the line goes.

One nit, and it is about the numbers rather than the mechanism: Q8Prof's accumulators are plain double/long members written from gemm_q8 with no lock. ORT runs a session's nodes on its intra-op pool, so if anyone measures with -i > 1 the totals can lose updates and the leg split would be quietly wrong rather than obviously broken. The #1023 method is -i 1, so the recorded runs are fine — but either say "one thread only" in the docs line, or guard the accumulation with the same mutex the trace does not need.

Not re-run here: the EP proof. Its device-free evidence (6/6 with the knob on, the three cases off/trace/stderr, the -O3 vs -O0 25x trap seen from the other side) is yours and is the kind of thing I would re-run if the box were not carrying four lanes; CI is green on the head, and nothing here changes a decision the EP makes.

@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 5, 2026
@bong-water-water-bong
bong-water-water-bong merged commit 59724bb into main Nov 5, 2026
10 of 11 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/onnx-qdq-prof branch November 5, 2026 00:22
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

First result from this knob, and it changes the question #1023 left open. CPU-only — no device, no ticket, no lock (a Release build with ONEBIT_EP_NPU=OFF, so the binary has no device code in it at all), load 39–41 the whole time, so only shares are quotable, not absolute times.

1. Where a host-arm token goes. Same export, 32-token prompt, 8 tokens, 1 thread, two runs:
host_ms 18767.6 / 10379.3 over 1764 calls = 196×9 passes, rows_host 63504 → 36.0 rows/call, against decode walls of 2208.43 / 1290.44 ms/token. So the QDQ host loop is 0.94 / 0.89 of a decode token — everything else in the graph (attention, norms, softmax, embed, ORT) is only 53–101 ms/pass. That was the large unknown #1023 hedged about.

2. What that does to the candidates. With #1023's quiet arms (host 947, device 1074 ms/token):

quantity value
host loop on the graph's real operands 846–894 ms/pass (vs the probe's 1209 on synthetic → 1.35–1.43×, consistent with the ≥1.28× bound #1023 could prove)
the device leg at model level 973–1021 ms/pass (1074 − 53..101)
the probe's device column 712 → the model's calls cost +261–309 ms/pass, +1.33–1.58 ms/call

So candidate 2 is confirmed but not sufficient, and the residual is not dispatch-as-such: the probe pays the same dispatch and is 40 % cheaper per call.

3. The mechanism, costed. npu/ort/onebit_npu_gemm.cpp:47 already says it — one slot per kernel; two different weights of the same shape repack per call — and q8_kernel() is keyed by (K, N): 5 kernels for 196 groups, so all 28 weights of a shape alternate through one slot and every call in a run re-packs (memset + blocked byte scatter), while the probe repeats one pointer and packs once. Measured that exact loop with the real q4nx_layout::blocked_index, the real shapes and the real group counts (~/wt/qdq-repack-cost.cpp, host heap, -O3, 20 reps): 295.2 ms/pass = 1.51 ms/call.

712 + 295 = 1007 ms/pass against the 973–1021 the arms imply — the whole model-level deficit, inside 1–5 %.

Consequence: remove the re-pack and the device is ~712 against the host's 846–894, i.e. 1.19–1.26× faster at model level instead of 1.13–1.16× slower. The remaining direct confirmation is this same PR run on the device arm (dev_call_ms/calls_dev ≈ 5.0 against the probe's 3.63); it is queued behind a quiet box, and nothing was taken from the NPU FIFO for any of the above.

bong-water-water-bong pushed a commit that referenced this pull request Nov 5, 2026
…ot a build delta

docs/onnx.md and docs/onnx-pack.md both close the model-level section with a
promise -- the cause of the 1.13-1.16x loss is a sign without a cause, and the
next step is a profile. That profile has been run, on both arms, in one window:

  1. it prices the per-call costs on the array: the one-slot weight cache is
     0.24 ms/call (the whole-image BO writeback), the shared epilogue 0.134,
     the pack loop 0.06 -- 0.43 of the ~1.3 ms/call the candidate needs;
  2. it measures the other candidate without the array at all: dumped from one
     prefill, the graph's real int8 activations are 62.1 % nonzero against the
     probe's ~99 %, so its host loop is 1.42x faster per pass (1098 -> 770.6 ms)
     and the array's kernel advantage is ~1.12x at model level, not 1.7x;
  3. it re-measures both arms on the current tree: 930.5 ms/token on the array
     against 972.9 on the host kernel, i.e. 4.4 % in the array's favour, from
     the same window with two runs each agreeing on the sign. The split closes
     on both sides -- device calls 802.5 + epilogue 19.0 + 109 of graph work
     against the host loop's 879.0 + 94 -- and the host loop's 879 ms/pass
     validates the CPU-only estimate of 848 to within 4 %.

The 1.13x loss the pages report is kept, dated to the session it came from, with
a pointer to the re-measurement rather than deleted. Attribution is stated the
way it was checked, not the way it was first guessed: exactly two commits touch
this path since those figures (#1032 profiling, #1047 the re-pack at ~5 % of a
token), the private NPU runtime the EP links against is unchanged (2026-10-31),
the five design ELFs are byte-identical, and load cannot explain the direction
(the device arm is slower under load: 5.65 ms/call at load 11-14 against 4.09
here). So ~5 % of the 13 % is code and the rest is unestablished -- most
plausibly CPU-side contention in the earlier session, since half of a device
call is host work -- and the pages say the magnitude is the finding, not the
sign, and that it is small in both directions.

Also recorded because it is a measured negative: keeping every weight resident
removes the writeback (delta 0.054/0.067) for 568 MiB of images on the real
export's 197 projected MatMuls, which is 3 % of a token and was not taken.
bong-water-water-bong added a commit that referenced this pull request Nov 5, 2026
…ot a build delta (#1076)

docs/onnx.md and docs/onnx-pack.md both close the model-level section with a
promise -- the cause of the 1.13-1.16x loss is a sign without a cause, and the
next step is a profile. That profile has been run, on both arms, in one window:

  1. it prices the per-call costs on the array: the one-slot weight cache is
     0.24 ms/call (the whole-image BO writeback), the shared epilogue 0.134,
     the pack loop 0.06 -- 0.43 of the ~1.3 ms/call the candidate needs;
  2. it measures the other candidate without the array at all: dumped from one
     prefill, the graph's real int8 activations are 62.1 % nonzero against the
     probe's ~99 %, so its host loop is 1.42x faster per pass (1098 -> 770.6 ms)
     and the array's kernel advantage is ~1.12x at model level, not 1.7x;
  3. it re-measures both arms on the current tree: 930.5 ms/token on the array
     against 972.9 on the host kernel, i.e. 4.4 % in the array's favour, from
     the same window with two runs each agreeing on the sign. The split closes
     on both sides -- device calls 802.5 + epilogue 19.0 + 109 of graph work
     against the host loop's 879.0 + 94 -- and the host loop's 879 ms/pass
     validates the CPU-only estimate of 848 to within 4 %.

The 1.13x loss the pages report is kept, dated to the session it came from, with
a pointer to the re-measurement rather than deleted. Attribution is stated the
way it was checked, not the way it was first guessed: exactly two commits touch
this path since those figures (#1032 profiling, #1047 the re-pack at ~5 % of a
token), the private NPU runtime the EP links against is unchanged (2026-10-31),
the five design ELFs are byte-identical, and load cannot explain the direction
(the device arm is slower under load: 5.65 ms/call at load 11-14 against 4.09
here). So ~5 % of the 13 % is code and the rest is unestablished -- most
plausibly CPU-side contention in the earlier session, since half of a device
call is host work -- and the pages say the magnitude is the finding, not the
sign, and that it is small in both directions.

Also recorded because it is a measured negative: keeping every weight resident
removes the writeback (delta 0.054/0.067) for 568 MiB of images on the real
export's 197 projected MatMuls, which is 3 % of a token and was not taken.

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

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