Skip to content

XPU: gdn_out's scale bookkeeping in cheaper exact forms (Qwen3.8-27B on Arc Pro B70, -22.6% kernel time, bytes equal) - #2921

Merged
ashhart merged 2 commits into
ashhart:mainfrom
dinadur:xpu-gdn-out-rescale
Nov 13, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
dinadur:xpu-gdn-out-rescale

Conversation

@dinadur

@dinadur dinadur commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

gdn_out (the prompt GDN's output kernel on XPU) computes the same bytes with less work around its products. Before: 736 ms of GPU time on a 32,768-token prompt on an Arc Pro B70. After: 570 ms. No product, operand or summation order changes, and gdn_prep and gdn_state compile to the same instructions as on main.

Three edits, all inside gdn_out:

  1. The two rescales after the products are one multiply where ldexp is one multiply. ldexp(x, n) with a per-lane n compiles (IGC 2.40.13) to x * 2^n * m0 * m1 behind three range selects, about 11 SIMD16 instructions an element, 128 elements a subgroup. For n in [-124, 127] both m0 and m1 are 1.0, so the result is the single product x * 2^n. The kernel now tests once a subgroup that every lane's n is in that range and, if so, multiplies by the power of two built from the exponent bits; otherwise it runs the original ldexp loops unchanged.
  2. The row exponent comes from the float's exponent field instead of ilogb. clamp(141 - ((bits >> 23) & 255), -100, 100) equals clamp(14 - ilogb(m), -100, 100) for every positive float: normals by definition, subnormals clamp to 100 either way, infinity to -100 either way.
  3. exp(la[t]) is computed once a row in the first half and read back from local memory in the second, instead of in every subgroup.

Checks on this head

  • Head commit: bf0cc2e273f145219a32b0ca0b45e70f1d632bd4, on main 93a2e93ecebff8aa52b88e58c5dd222516e69c5f. Every number below was measured on that base with this file's exact bytes (kernel source sha256 b21179948be2888350994aab6cc27263ce0694ae6e90f1914c0e86b540f4842f), built before the commit existed.
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 386 and 386, 0 new (lean_new.py).
  • Tests run, with the command and pass, fail and skip counts:
    • ./zig-out/bin/tf-xpu-contract-run --device 0: SUMMARY bmg-g31 0xe223 9 kernels: 9 pass gates 1-4, 0 fail, 0 known findings; 5 controls: 5 red as planted, 0 missed; 0 limit problems, exit 0, the same line on main.
    • ./zig-out/bin/tf-xpu-qwen27-means-test: prompt 4.19e-7, decode 2.80e-7 (bound 2e-6), exit 0, the same on main.
    • ./zig-out/bin/tf-xpu-qwen27-seam-test MODEL: cut at 64 and cut at 1024 each give 0 of 248,320 differing logits, exit 0, the same on main.
    • XPU_FP64_REF=1 xpu-qwen27 MODEL IDS 8 on 2,048 real tokens: all 197 table lines equal main's.
    • git diff --check: clean.
  • CI: 8 jobs passed (fmt, pytest, zig, zig-windows, zig-macos-dev-tools, release-paths, both benchmark-clients); host-nightly-linux and host-nightly-macos were skipped.
  • Not run, and why:

Receipt

  • Main against branch: cells and numbers, or why they can't be run: raw runner, XPU_CTX=33024 XPU_PROFILE=1 xpu-qwen27 MODEL --bench=32768 32, a fresh process each run, two A-B-B-A rounds after warm-up runs brought the card to 80 °C.

    Run order Build gdn_out ms gdn_prep ms gdn_state ms Prompt s tok/s
    1 main 734.89 531.2 870.3 20.789 1,576
    2 branch 569.26 531.6 869.7 20.637 1,588
    3 branch 570.29 531.6 871.1 20.658 1,586
    4 main 736.81 532.5 872.5 20.865 1,571
    5 main 736.52 532.5 872.4 20.860 1,571
    6 branch 570.65 533.8 874.4 20.765 1,578
    7 branch 570.43 533.2 873.7 20.761 1,578
    8 main 736.87 532.1 872.3 20.862 1,571

    gdn_out: 736.3 to 570.2 ms, 166 ms (22.6%) lower in every pair. Whole prompt: 20.844 to 20.705 s, 0.67%; in the second round, with the card fully hot, 0.47%. The card is power-limited during a prompt (PL2 at 230 W), and the other kernels read slightly higher when gdn_out gets lighter, so the wall-clock gain is smaller than the kernel's.

  • Boxes it reaches: Intel XPU only, the Qwen3.8-27B prompt path (gdn_chunk.cl).

  • New switch? Default, accepted values, profile row: none.

  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: main 93a2e93e (1.2.7); Zig 0.17.0; ocloc and compute runtime 26.31.39395.13 (IGC 2.40.13), Level Zero 1.32.0, nGEN headers from oneDNN 55e24fbf; Arc Pro B70 (8086:e223) capped at 230 W, Core Ultra 7 270K Plus, 61 GiB RAM, Ubuntu 26.04, kernel 7.0.0-42, xe; SergiioB/Qwen3.8-27B-GPTQ-Int4-sym-G128-MTP-BF16 at 9d189a60. A second B70 in the host kept serving another model; everything else on the test card was stopped. The test card had taken one engine reset earlier the same day and was not rebooted before these runs: a diagnostic variant of this kernel, from this same work, fed fp16 bits to a bf16 product to time it, produced garbage values, and the run ended in an xe engine memory CAT error. Clock state: every prompt sample in all eight runs is PL2-throttled at 2,083 to 2,250 MHz (per-run means 2,162 to 2,208 for both builds); the sampling is one reading every 5 s, too sparse to bound the gap between arms below 1%.

  • Paths and platforms the change reaches, which I ran and which I could not: the XPU prompt GDN for Qwen3.8-27B, run on one B70 through the raw runner and, in the follow-up, through serve without a drafter at 1 and 2 streams. Not run: drafted decode, any other Intel GPU.

  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false: in the follow-up, bench_concurrent.py --tokens 64 --levels 1,2 --alone --serial on main and on the branch: every cell equal to its alone and serial outputs, 0 unequal, 0 failed. "draft": false comparison: not run (no drafter was loaded). Bytes: XPU_DUMP_H (every row's final normed hidden state, the prompt's rows and the 7 decoded ones) on main and on the branch.

    Prompt Bytes sha256, main Branch
    First 32,768 tokens of CHANGELOG.md at 93a2e93e (checkpoint tokenizer, no special tokens) 335,616,000 a80d58299b1e2f2536c427cfb561af56de9c6958270de09bf3ec12858b3a7fee 0 differing bytes
    --bench=32768 335,616,000 bc29a385e8573e0de0121d113e972a200b43a244ddee2264116718b2ff1da07b 0 differing bytes
    First 8,192 tokens of the same file 83,957,760 d430ed6a9d83e98dc6492bcad653f399b8134fd71afdd2fd344560e2eff32d12 0 differing bytes; main also reproduces its own dump

    The comparison XPU (B70): the 1.1.0 prompt GDN costs about 5% of 32K prefill on Qwen3.8-27B, and the README table predates it #2859 named, run in the follow-up (comment): Y after every gdn_out launch (48 GDN layers × 32 chunks) on the 32K CHANGELOG.md prompt, from a local host hook that is not in this pull request. Main and the branch give identical files: 19,327,352,832 bytes, sha256 1c1d63886151939587485057b92bec16b640e39d61baaf98c28b691aece48018. With the fallback forced (one = 0 at line 371, local build only) Y and the final hidden states on the same prompt are identical to main's too. A deliberately wrong kernel through the same comparison on the 8,192-token prompt differs in both.

    The 8 greedy tokens after each prompt are equal too. gdn_prep and gdn_state: IGC's assembly for both is the same as main's apart from the two program-hash marker moves.

  • Main against this branch, same machine and checkpoint, posted before asking for a run on our machines (or why you can't): above.

  • Decode speed before and after, from tools/bench_openai.py: --tokens 256 --reps 3 --temperatures 0 through serve, two fresh servers a build: 33.4 tok/s median for main and for the branch. gdn_out runs only on prompt chunks, and decode GPU time is 1,246.2 to 1,246.6 ms for 31 tokens in all eight runs above.

  • Prompt speed before and after: raw runner above (32K). Cold ttft_s from tools/bench_cache.py --require-cold through serve --parallel 4 --context 66560 --prompt-cache-gib 0, no drafter, two fresh servers a build (the server reported 3 streams of 66,560 tokens for both builds), at 2,048 / 8,192 / 32,768 / 65,536 tokens: main 1.174, 1.178 / 4.520, 4.525 / 20.911, 20.940 / 49.283, 49.278 s; branch 1.159, 1.162 / 4.492, 4.492 / 20.806, 20.806 / 49.057, 49.032 s. Every row cold with 0 cached tokens and qualified; token_sha256 equal between builds at every size.

  • Licence of any new weights or drafter, or "n/a": n/a.

Kernel contract, only when a GPU kernel is new or changed
  • Kernels registered with the contract, and the kernel each one replaces: none added. gdn_out is changed in place; on main none of the three prompt-GDN kernels is registered (the nine XPU registrations are glue kernels). The review said registration is not needed for this pull request.
  • A planted control for each new kernel, and each one fails as planted: no new kernel.
  • Each backend's SUMMARY line and exit status: XPU, exit 0: SUMMARY bmg-g31 0xe223 9 kernels: 9 pass gates 1-4, 0 fail, 0 known findings; 5 controls: 5 red as planted, 0 missed; 0 limit problems.
  • Chips I could not run: every chip but one BMG-G31.

How far the equality argument goes

  • Edit 2 is an integer identity for every float, so it holds on any compiler.
  • Edit 1 is exact against this compiler's ldexp by its own expansion (the dumped LLVM IR shows x * 2^n * m0 * m1, and multiplying by 1.0 twice changes nothing). Against any correct ldexp it is exact whenever x * 2^n is a normal float or zero, because that product is then exactly representable. For a subnormal result the two could differ on a compiler or device that treats subnormals differently in ldexp and in a multiply; that was not probed here. Outside [-124, 127] the original code runs.
  • The fallback has been exercised only by forcing it: a local build with one = 0 gives Y and final hidden states identical to main's on the 32K prompt. I did not measure how often a subgroup takes the one-multiply path on these prompts.
  • Edit 3 relies on exp of the same scalar giving the same bits in the first half as in the second. The byte comparisons test that; I have no proof of it.

What remains for #2859

  • gdn_state has the same per-lane ldexp pattern (32 an 8-row block a chunk) and its row split is computed twice a chunk; gdn_prep is untouched. Neither is in this pull request.
  • What the rescale cost: removing it outright (wrong output, timing only) took gdn_out to 474 ms. This branch is 96 ms above that, not all of it rescale work. Removing the row-maximum analysis took 40 ms; the low-plane products and reads are 116 ms.
  • Whether an fp16 DPAS costs more than a bf16 one is still unmeasured.
  • The optimisation ledger has no XPU cell I could find for this; the review said no row is needed.

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency. (No new test: the change is meant to leave every byte equal, and the receipt is a byte comparison. Say if you want one in the tree.)
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.
  • Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off. (Not reached by this file.)
  • Drafted output equals "draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (Concurrent equals alone and serial at 1 and 2 streams, see Receipt. Drafted and resumed: not run.)
  • No precision traded for speed. If the bits change, the description says which sums change and why.
  • Decode and prompt processing are no slower than main at one stream and the widest admitted; with --drafter, no slower than without it, greedy and sampled. (Raw runner: prompt 0.5 to 0.7% faster. Through serve without a drafter: cold ttft_s 0.5 to 1.3% lower at 2k to 64k, decode within 0.2% at 1 and 2 streams. The server admitted 3 streams; 3 streams, --drafter and sampled: not run.)

The Y comparisons, the forced fallback and the serve numbers above were measured after review and are reported in full in this comment; they were made after a reboot of the host, in a boot with no xe fault lines. This description and the change were prepared with an AI coding agent working on my machine under my direction; the measurements are from my B70.

🤖 Generated with Claude Code

@ashhart

ashhart commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Thanks @dinadur. This is careful work, and the "How far the equality argument goes" section saved us a lot of checking. We read all three edits against main and agree they're exact:

  • The exponent from the bits is the same integer as 14 - ilogb(m) for every float, after the clamp.
  • x * 2^n is ldexp(x, n) whenever 2^n is normal. The guard at gdn_chunk.cl:371 keeps n in [-124, 127], and the edge bit patterns are right.
  • el is the same exp of the same argument.

The A-B-B-A kernel times settle the speed. Two runs and it's ready:

  1. Compare Y itself on the 32K CHANGELOG.md prompt, main against this head. This is the gate XPU (B70): the 1.1.0 prompt GDN costs about 5% of 32K prefill on Qwen3.8-27B, and the README table predates it #2859 named. A hash of Y after every gdn_out launch (all 48 GDN layers × 32 chunks) is enough. A temporary hook after zig/src/xpu/forward.zig:389 in your local build is fine and doesn't belong in the PR. XPU_DUMP's y0 only covers layer 0 of the first chunk (forward.zig:398).
  2. Run the same comparison once with the fallback forced (one = 0 at zig/kernels/xpu/qwen27/gdn_chunk.cl:371, local build only), and post that it also matches main. Real prompts probably never leave the one-multiply range, so the ldexp branches at :377-380 and :395-398 haven't run on this head.

Answers to your questions:

CI is waiting on our approval, which we'll give. Main is frozen for today's release. Once the two comparisons are posted, mark the PR ready and we'll queue it to land after the release.

  • Synthena (Ash's Agent)

@dinadur

dinadur commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the read-through. Results for the two comparisons, from two windows today on the same B70 (head bf0cc2e2 on main 93a2e93e, same toolchain as the description, kernel 7.0.0-42). The host has been rebooted since the runs in the description and this boot has no xe fault lines. Everything else on the test card was stopped.

The Y hook is the one you described: a local host patch after the gdn_out launch in forward.zig that appends each prompt chunk's Y (T × 6144 bf16) for every GDN layer to a file. It is not in the PR. I compared the files byte for byte rather than hashing per launch.

1. Y on the 32K CHANGELOG.md prompt, main against this head: identical.

Bytes sha256
Y, 48 GDN layers × 32 chunks, main 19,327,352,832 1c1d63886151939587485057b92bec16b640e39d61baaf98c28b691aece48018
Y, this head 19,327,352,832 the same; cmp finds no difference
Final hidden states (XPU_DUMP_H), main and this head 335,616,000 a80d5829…, equal, and equal to the value in the description

2. The same comparison with the fallback forced: identical.

Build: this head with line 371 changed to const int one = 0 & sub_group_all(...) and the same Y hook, nothing else. Its compiled gdn_chunk program differs from this head's and equals the one in the unhooked forced-fallback build. Main's reference was dumped again in the same window and reproduces the hash above.

Fallback forced, against main Bytes Result
Y, 32K CHANGELOG.md prompt, 48 layers × 32 chunks 19,327,352,832 identical, sha256 1c1d6388… as in the table above
Final hidden states, the 32K prompt 335,616,000 identical (a80d5829…)
Y and final hidden states, first 8,192 tokens 4,831,838,208 and 83,957,760 identical

Timing in the first window (--bench=32768, XPU_PROFILE=1, A-B-B-A after warm-ups to 80 °C): gdn_out 733.7 and 735.7 ms on main, 568.5 and 569.9 ms on this head, 703.0 ms with the fallback forced.

Planted control. A deliberately wrong kernel (high planes only) through the same comparison on the 8,192-token prompt: Y differs from the third byte and the hidden states differ in 46,043,072 bytes, so the comparison does catch a wrong gdn_out.

Also run, though you said they are not needed. Upstream's own XPU_DUMP on the 8K prompt with unpatched builds: all 15 files equal. Through serve --parallel 4 --context 66560 --prompt-cache-gib 0 with no drafter, main, head, head, main, a fresh server each (the server reported 3 streams of 66,560 tokens for both builds):

bench_cache.py --require-cold, ttft_s 2,048 8,192 32,768 65,536
main, two servers 1.174, 1.178 4.520, 4.525 20.911, 20.940 49.283, 49.278
this head, two servers 1.159, 1.162 4.492, 4.492 20.806, 20.806 49.057, 49.032

token_sha256 is equal between builds at every size, every row is cold with 0 cached tokens and qualified. bench_openai.py --tokens 256 --reps 3 --temperatures 0: 33.4 tok/s median for both. bench_concurrent.py --tokens 64 --levels 1,2 --alone --serial: 33.5 and 60.1 tok/s aggregate on main, 33.5 and 60.2 on this head (code prompt); every cell equal to its alone and serial outputs, 0 unequal, 0 failed, for both.

CI on this head: 8 jobs passed; host-nightly-linux and host-nightly-macos were skipped.

Still not run: zig build test test-golden, and any card other than this B70.

Measurements are from my B70; this comment was prepared with an AI coding agent working under my direction.

@ashhart
ashhart marked this pull request as ready for review November 11, 2026 20:55
@ashhart

ashhart commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Thanks @dinadur, that settles both. Y for all 48 GDN layers over the 32K prompt is byte-identical to main's, on this head and with the fallback forced, and the planted wrong kernel differs from the third byte, so the comparison would have caught a real error. The fallback's own timing (703 ms against main's 734) is a nice bonus.

CI is green and it merges cleanly with main, so we've marked the PR ready for you and queued it to land.

  • Synthena (Ash's Agent)

dinadur and others added 2 commits November 13, 2026 15:11
…e, reads row exponents from the bits, and takes e^la once a row

The prompt GDN's output kernel keeps every product and its order. The two
rescales after the products multiply by 2^n when every lane's n is in
[-124, 127], where ldexp is that one product, and run ldexp otherwise. The
row exponent is the float's exponent field, equal to 14 - ilogb(m) under the
same clamp for every float. e^la is computed once a row in the first half.

Refs ashhart#2859
@ashhart
ashhart force-pushed the xpu-gdn-out-rescale branch from bf0cc2e to fdd6994 Compare November 13, 2026 15:11
@ashhart
ashhart merged commit aaac299 into ashhart:main Nov 13, 2026
@ashhart

ashhart commented Nov 13, 2026

Copy link
Copy Markdown
Owner

Thank you @dinadur. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

ashhart added a commit that referenced this pull request Nov 13, 2026
Figures and scope corrected to the PRs (#2948, #2863, #2934, #2933, #2921, #2906, #2878, #2892, #2851, #2728, #3072,
#2915, #2864, #2854 and others), promises of later work removed (#2903, #2875, #1630, #1720), #3074 and #3047/#3046
said as their PRs scope them, the duplicate #2861, #2585, #2321 and #2719 entries and two copies of released 1.2.5
lines removed, #1953's line named for its PR, and a line for #3255. Released sections unchanged.
@ashhart

ashhart commented Nov 13, 2026

Copy link
Copy Markdown
Owner

ashhart added a commit that referenced this pull request Nov 17, 2026
Seven duplicate entries go (a PR's own line beside its lines-file line), five outside authors are thanked, #1503 gets the line its close note promised, and eleven lines now match their PRs' code and receipts (#3129, #3194, #3258, #2696, #2921, #3365, #3345, #3384, #3679, #3682, #3699, #3393).
@ashhart

ashhart commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants