Repository navigation
onnx EP: fold asymmetric QDQ zero points into the int8 epilogue (TQDQ task 3) - #693
Conversation
738ec9a to
ca49da2
Compare
e5133c9 to
628d142
Compare
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY on this head for content (628d142), but no tprs-ready yet: the PR is based on 1bit/onnx-ep-qdq-per-axis, not main. That is the trap that made #690 CONFLICTING for two rounds — a squash-merge of #690 puts different commits on main, the base branch stays at ca49da2, and a retarget then carries #690's commit again. One command fixes it after #690 lands, and I verified it is clean (details at the bottom).
What I verified at 628d142 (parent ca49da2, so this is #690 plus one commit; tools/copyright.py --check rc 0, incremental ninja rc 0, ctest -R "onebit_ep|ort_info" 5/5 passed, 0.81 s):
case tensor 0 of 48 int8 values differ, worst |d|=0 LSB, path=fused
case axis 0 of 48 int8 values differ, worst |d|=0 LSB, path=fused
case asym 0 of 48 int8 values differ, worst |d|=0 LSB, path=fused
case asym-axis 0 of 48 int8 values differ, worst |d|=0 LSB, path=fused
case xaxis-K 0 of 48 int8 values differ, worst |d|=0 LSB, path=per-node
case rss 0 of 262144 int8 values differ, worst |d|=0 LSB, path=fused
case rss: peak RSS growth: CPU EP +6960 kB, fused +260 kB (delta 6700 kB)
PASS OneBitEp fused the A8W8 QDQ group: worst |d| = 0/0/0/0 LSB (per-tensor, per-axis,
asymmetric, asymmetric per-axis), per-K scale refused (worst |d|=0), peak RSS -6700 kB
Six cases, and the two new ones do run the new code rather than falling back — the EP's own trace, read back per case:
case axis qdq rows=8 k=16 n=6 sx=8 sw=6 zp_x=0 zp_w=0 zp_y=0 (no qdq_colsum)
case asym qdq_colsum k=16 n=6
qdq rows=8 k=16 n=6 sx=1 sw=1 zp_x=1 zp_w=1 zp_y=-7
case asym-axis qdq_colsum k=16 n=6
qdq rows=8 k=16 n=6 sx=8 sw=6 zp_x=8 zp_w=6 zp_y=-7
case xaxis-K capability nodes=4 claimed=0 qdq_groups=0 -> per-node path
That is also the doc claim checked rather than taken: docs/onnx-pack.md says the column sums are computed lazily, and a symmetric graph never asks for them — the per-axis symmetric case above writes no qdq_colsum line, the two asymmetric ones write it once.
The arithmetic, re-derived independently. The identity in the header, Σ_k (a−zx)(w−zw) = Σ_k a·w − zx·Σ_k w − zw·Σ_k a + K·zx·zw, is the whole PR, and so is the rounding order (round first, then + zp_y). I compiled onnx/ep/onebit_ep_kernel.cpp standalone and drove gemm_q8 with hand-computed cases, including a real tie:
2.5 exactly, zp_y = 0 -> 2 (nearbyint half-to-even; 3 would be half-away)
2.5 exactly, zp_y = 1 -> 3 (round then add; rounding after the shift gives 4)
a=[2], w=[5], zp_x=1 zp_w=2, k=1 -> 3 (10 − 5 − 4 + 2)
a=[2,3], w=[5,7], zp_x=1 zp_w=2, k=2 -> 13 (31 − 12 − 10 + 4)
130 with zp_y = 1 -> 127 (saturates after the zero point)
tie_check: ok
So the three things that could silently be wrong — the expansion of the shift, the order of the output zero point, and saturation — are right in the kernel, not just in the graph comparison.
Reading the diff: zp_count() refuses a zero point it cannot fold cheaply (any axis but the epilogue's own, and any rank-3 activation zero point, which is conservative on purpose), a 1-element zero point is treated as scalar (correct: the value is the same everywhere), the output zero point is accepted only as a scalar, ensure_colsum is keyed on the weight pointer so a symmetric graph pays nothing, and every correction is int64 before the single float multiply. The per-node fallback is still the answer for a per-K scale or zero point, and xaxis-K above shows it is still taken.
One nit, not a blocker: epilogue.colsum_w = plan->colsum_w.data() is read outside colsum_mu, so a second session that shares the plan object with a different weight pointer could reallocate the vector mid-GEMM. It cannot happen for two sessions of the same graph (same initializer, same pointer), and the fix is one line if it ever does — hold the lock across the call or copy the sums per run.
Merge order: #690 → rebase this onto main → then I put the label on. After #690 is squash-merged:
git rebase --onto origin/main ca49da2a9
git push --force-with-lease
(ca49da2a9 is this PR's base, i.e. #690's head.) The result is one commit on top of main with this diff unchanged — the same shape that cleared #690.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
Correction to my last comment, before anyone runs that command: git rebase --onto origin/main ca49da2a9 does not work here and I should not have written it. --onto origin/main ca49da2a9 moves only the commits after ca49da2a9, and this PR's own commit is a delta on #690's changes, so replaying it alone onto main conflicts in onebit_ep.cpp / onebit_ep_kernel.cpp (I ran it in a scratch worktree: Could not apply 628d142).
What works, verified in the same worktree by simulating the squash (fakeMain = ca49da2's tree as a single commit on 7d39a53):
$ git rebase origin/main
warning: skipped previously applied commit ca49da2 # #690, already in main by patch
Successfully rebased and updated detached HEAD.
$ git log --oneline -2
b860e8d onnx EP: fold asymmetric QDQ zero points into the int8 epilogue
95c05f6 squashed #690 (simulated main)
$ git diff --stat origin/main HEAD
6 files changed, 463 insertions(+), 146 deletions(-) # this PR's diff, unchanged
So: plain git rebase origin/main after #690 merges — git drops the #690 commit because main already has its patch, and re-applies this one cleanly. Then tprs-ready goes on. Everything else in my verdict stands.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
Reading my verdict against the PR as it now stands: the base has been retargeted to main (it was 1bit/onnx-ep-qdq-per-axis when I reviewed it), so GitHub now reports CONFLICTING/DIRTY. That is the expected consequence and it does not change anything I verified — the head is still 628d142 with parent ca49da2, i.e. the branch still carries #690's commit.
Two ways forward, and only the first one is safe:
- Merge #690 first, then
git rebase origin/mainhere (git skipsca49da2as already applied and replays this PR's one commit). Mergeable, one commit, label goes on. This is what the review above asks for. - Rebasing onto main now also succeeds, but it produces a two-commit branch (#690's commit + this one) that GitHub would call mergeable — merging that would land #690's payload through this PR as well as through #690. Don't, unless #690 is being closed instead.
Whichever happens, the head's content is verified: six proof cases at worst |d| = 0 LSB, the epilogue's correction terms and rounding order checked by hand, copyright rc 0, ctest -R "onebit_ep|ort_info" 5/5.
Task 3 of the A8W8 QDQ fast path. Until now a non-zero zero point kept the
group on the per-node path. Writing the shifted product out,
Sum_k (a-zx)(w-zw) = Sum_k a*w - zx*Sum_k w - zw*Sum_k a + K*zx*zw
the GEMM already computes the first term, the second is a column sum of the
weight, the third a row sum of the activation, so the corrections are exact int64
arithmetic in the epilogue rather than a second pass:
* colsum_w = Sum_k w_q[k][n] is computed once on the first run that needs it and
kept for the session (a mutex guards two concurrent first runs). A symmetric
graph never computes it, and no decode step pays O(K*N) for it.
* the row sum is one extra add per k, inside the loop that already reads the row.
* the output zero point is added *after* round-half-to-even -- rounding after an
integer shift would move exact ties to the wrong neighbour.
Scales and zero points each take the per-tensor or the two per-axis forms
(rows for the activation, output columns for the weight), so the fully per-axis
asymmetric case is one code path. A zero point along K is still refused, for the
same reason a scale along K is: it needs a column-weighted vector of the weight.
gemm_q8 now takes one Q8Epilogue instead of eight positional arguments (scales,
zero points, colsum, 1/s_y), and the kernel finally includes its own header --
it never did, so the signature was only kept in sync by hand.
Proof (ctest -R onebit_ep_qdq_proof), strixhalo, ORT 1.23.2, optimized and a
fresh no-optimization build, 5/5 tests each:
* per-tensor / per-axis / asymmetric zp_x=-40, zp_w=-25, zp_y=-7 /
asymmetric per-axis (8 row + 6 column scales and zero points): worst |d| = 0
LSB each against the CPU EP;
* the trace shows zp_x=8 zp_w=6 zp_y=-7 and one qdq_colsum for the asymmetric
cases, and zp_x=1 zp_w=1 zp_y=0 with no colsum for the symmetric ones;
* per-K scale still refused (per-node path, 0 LSB);
* peak RSS on 256x1024x1024 unchanged: ~6.4 MB (CPU EP) vs 268 kB (fused);
* control ONEBIT_EP_QDQ=0: every case on the per-node path.
Also removes a use-after-move the first version of the plan log had (dereferencing
the unique_ptr after moving it into storage) -- it segfaulted in the optimized
build before it segfaulted anywhere else.
Docs: onnx-pack.md moves asymmetric zero points from "Not done" to "Works" with
these numbers and keeps the per-K refusal documented; onnx.md says the same.
628d142 to
4ae48e6
Compare
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY — tprs-ready applied. Re-pushed onto main and it is a pure rebase: head 4ae48e6a, parent 3b950ac (#690's squash), origin/main an ancestor, GitHub MERGEABLE, and the tree is 01578bb7… — identical to the 628d142 tree I verified, so git diff 628d142 4ae48e6a is empty and every number below belongs to this head rather than being carried over on trust. The base is now main, so the stacked-base problem is gone.
Rebuilt and re-ran at this head anyway:
$ python3 tools/copyright.py --check rc=0
$ ninja onebit_ep_qdq_proof onebit_ep_proof ort_info rc=0
$ ctest -R "onebit_ep|ort_info" 5/5 passed (0.75 s)
$ ./onnx/ep/onebit_ep_qdq_proof ./libonebit_ep.so
case tensor 0 of 48 int8 values differ, worst |d|=0 LSB, path=fused
case axis 0 of 48 int8 values differ, worst |d|=0 LSB, path=fused
case asym 0 of 48 int8 values differ, worst |d|=0 LSB, path=fused
case asym-axis 0 of 48 int8 values differ, worst |d|=0 LSB, path=fused
case xaxis-K 0 of 48 int8 values differ, worst |d|=0 LSB, path=per-node
case rss 0 of 262144 int8 values differ, worst |d|=0 LSB, path=fused
PASS OneBitEp fused the A8W8 QDQ group: worst |d| = 0/0/0/0 LSB (per-tensor, per-axis,
asymmetric, asymmetric per-axis), per-K scale refused (worst |d|=0), peak RSS -6620 kB
And the two new cases still take the new code, from the trace (two qdq_colsum lines for the two asymmetric cases, none for the symmetric per-axis case; xaxis-K still claimed=0).
Content, from the review above and re-checked here: the shift expansion Σ(a−zx)(w−zw) = Σaw − zx·Σw − zw·Σa + K·zx·zw, the output zero point added after round-half-to-even, and saturation are all exercised by hand against gemm_q8 directly (2.5 tie → 2, 2.5 + zp_y=1 → 3 not 4, k=2 case → 13, 130 + zp_y=1 → 127), and the column-sum lazy path is confirmed by the trace rather than by the doc. The one nit I raised (colsum data() read outside the mutex) stays a nit — unreachable for two sessions of one graph.
CI at this head: Analyze green, build still running when I posted this; pr_agent red is the expected coder-llm-off noise. If build comes back red on this tree I pull the label and say so here.
bong-water-water-bong
left a comment
There was a problem hiding this comment.
build finished green at 4ae48e6a (3m40s), so the label stays — this head is READY and CI agrees with my local run: Analyze all four green, hw_gate skipping, pr_agent red as usual (coder-llm off). The duplicate run on the same head also completed without invalidating anything; no action needed from the author.
Task 3 of the A8W8 QDQ fast path (docs/onnx-pack.md). Stacked on #690 (which is stacked on #689) —
merge order is #689 → #690 → this one.
What changed
A non-zero zero point used to keep the group on the per-node path. Writing the shifted product out,
the GEMM already computes the first term, so the rest is exact int64 arithmetic in the epilogue — no
second pass:
colsum_w = Σ_k w_q[k][n]— computed once on the first run that needs it and kept for thesession (
std::mutexguards two concurrent first runs). A symmetric graph never computes it, and nodecode step pays
O(K·N)for it.zp_y— added after round-half-to-even. Rounding after an integer shift would move exact tiesto the wrong neighbour, so the order is load-bearing.
Scales and zero points each take the per-tensor or the two per-axis forms (rows for the activation,
output columns for the weight), so the fully per-axis asymmetric case is the same code path. A zero
point along K is refused, for the same reason a scale along K is: it needs
Σ_k zp_x[k]·w_q[k][n],a column-weighted vector of the weight that no kernel here precomputes.
gemm_q8now takes oneQ8Epilogueinstead of eight positional arguments, and the kernel includes itsown header for the first time — the signature had only been kept in sync by hand.
Measured gate (strixhalo, ORT 1.23.2; 5/5 in both an optimized and a fresh no-optimization build)
worst |d| = 0LSB vs the CPU EPworst |d| = 0LSBzp_x=−40,zp_w=−25,zp_y=−7)worst |d| = 0LSBzp_y)worst |d| = 0LSBqdq_groups=0), 0 LSB on the per-node pathONEBIT_EP_QDQ=0Evidence from the EP trace: the asymmetric runs read
zp_x=8 zp_w=6 zp_y=-7followed by oneqdq_colsum k=… n=…; symmetric runs readzp_x=1 zp_w=1 zp_y=0and log noqdq_colsumat all —i.e. the column sums are exactly as lazy as claimed.
Both DQ nodes now always carry a zero point input, and the proof generates the per-axis shapes ONNX
requires beside a per-axis scale (a scalar beside a per-axis scale is rejected by the CPU EP — the
first attempt did that and the reference session refused to run, which is the check working).
Also fixed
A use-after-move in the plan logging (
plan->has_colsumread afterstd::move(plan)into storage) —segfaulted immediately in the optimized build.
Still open
gemm_q8. Feeding int8 into thedevice BO needs an NPU-runtime entry plus a QDQ-GO window; blocked on pi-ortep's answer in
~/briefs/TORTEP-STATE.md.Report:
~/briefs/TQDQ-STATE.md.