Repository navigation
Opt qk_norm_rope_group_quant in gfx1250 - #5712
Conversation
…tore The coarse Q head wrote its row as two stores: a b128 of fp8 nope per lane, and a separate b16 for the duplicated e8m0 run. The run is 14 bytes, but it lands in the same 128B sector the payload store is already writing, so that sector takes a second partial write. An ablation over the head loop puts it at 7.1% of the kernel at T=16384 H=128; cutting it to one lane per group instead of all four changed nothing, so the cost is the store, not its lanes. Hand the run to the one lane whose payload slot already covers it, gathered through 32B of per-wave LDS after the TDM ring. The pickup has to go through __builtin_memcpy: a typed load lets alias analysis reorder it against the uint16_t publishes, which showed up as one wrong e8m0 byte in 29,360,128 at G=32. MERGE_Q_SCALE is a template parameter rather than a runtime flag. Trading a store for an LDS round trip pays once the store path sets the time and loses while per-head latency does, and selecting at runtime cost the small shapes ~6% with the branch never taken.
With the e8m0 run folded in, the payload store covers 464 of the row's 512 bytes, so its last sector is still partially written. Widen it to every lane -- the slots past the run carry zeros -- and the row goes out as one fully covered store. That is 10% more bytes on the Q output stream and wins more back than it costs, which is the same result the ablation pointed at: on this shape the price is partial sectors, not bytes. Interleaved against pristine main, same toolchain, medians of 4 x 9 x 200: T=16384 H=128 264.40 -> 248.37 us 12.90 -> 13.74 TB/s T=16384 H= 32 79.53 -> 73.82 us 10.97 -> 11.82 TB/s T= 8192 H=128 135.98 -> 127.90 us 12.52 -> 13.36 TB/s T= 512 H=128 10.567 -> 10.539 us unchanged (merge gated off)
Cycle counts and stall percentages from old traces, which no longer describe the current code and cannot be checked against it. The constraints they were attached to are kept: cos/sin is a scattered read the descriptor build and TDM prologue can cover, a register-based prefetch is what VGPR pressure made neutral, the kernarg fields the preload window has to reach, and that a decode launch is mostly ramp and drain. Comments only; the code object is unchanged.
The ring refills the slot it has just read, and tensorcnt orders TDM loads
against each other, not against this wave's DS reads -- tensor_load_to_lds is
the TDM engine writing LDS asynchronously. So the refill races the ds_read of
the slot it overwrites. Closing that with an explicit s_wait_dscnt works but
drains the DS pipeline every head, and measured +2.1% at T=4096 H=32, a tier
where nothing else in this series applies.
Deferring by one head closes it for free: the slot being refilled was read a
head ago, and the sum_sq loop has since consumed vec_q behind the s_wait_dscnt
that read needs anyway. Correctness no longer depends on where the issue lands,
which is just as well -- the scheduler hoists it back up next to the read.
Holding the same two heads of lead with a deferred issue needs one more slot,
so Q_TDM_DEPTH goes to 3 and the tensorcnt threshold to DEPTH-2. Three is also
the ceiling: the hardware allows 3 tensor ops in flight per wave.
T=4096 H=32 24.90 -> 24.58 us (24.90 is pristine main; the explicit
s_wait_dscnt form measured 25.32)
Under SWA the rotated K row is stored to two places, and the op test compares them as the pure copy they should be. They disagreed by one bf16 code on exactly one element of 131072, at T=1024 H=16 and H=32, deterministically. The f32 result there is -0.4736328125, the exact tie between bf16 0xbef2 and 0xbef3, so one f32 ulp decides it. The compiler rematerialises the expression for the second store instead of keeping it live, and contracts the other product when it does: folding x*cos lands on the tie and rounds to even, folding y*sin lands one ulp above it and rounds up. Spelling the fma explicitly was not enough on its own -- neither value moved -- so the empty asm is what pins it, by making each result something the compiler has to materialise once. The explicit fma stays because it fixes which product is folded. The fine-grained K path gets the same fma; it has no observed failure, only the same shape of risk. Paged-SWA rope warnings 8 -> 0 over the 60 SWA configs. Cost is within run to run noise on both the plain and the SWA fp8 shapes, and 14 of 16 NaN/Inf probes stay bit-identical -- the two that move differ only in the sign bit of a quiet NaN.
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
PR title tags & labels: |
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The xlarge dispatch reserves merge LDS scratch for bf16-Q workloads where the merge path is unused, adding avoidable resource usage.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 1
Open (1)
What changed in this PR
Optimizes the gfx1250 coarse QK norm/RoPE/group-quant kernel for xlarge prefill workloads.
Changes:
- Merges Q e8m0 scales into the payload store using LDS scratch.
- Revises TDM prefetch refill scheduling and depth.
- Stabilizes RoPE arithmetic with explicit FMAs.
| File | Description |
|---|---|
csrc/kernels/fused_qk_norm_rope_cache_quant.cu |
Implements gfx1250 TDM and scale-store optimizations. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Motivation
Technical Details
Carry the Q e8m0 run in the payload store. Hand the run to the lane whose payload slot already covers it, gathered through 32B of per-wave LDS. The pickup goes through __builtin_memcpy — a typed load lets alias analysis reorder it against the uint16_t publishes (one wrong byte in 29,360,128 at G=32). MERGE_Q_SCALE is a template parameter, not a runtime flag: selecting at runtime cost the small shapes ~6% with the branch never taken.
Write the whole Q row in one store. Widening to all 32 lanes costs 10% more bytes and wins more back — the price here is partial sectors, not bytes.
Defer the Q TDM refill by one head. The refill races the ds_read of the slot it overwrites (latent on main); tensorcnt does not order the TDM engine against this wave's DS reads. Deferring puts the math's own s_wait_dscnt in between, so the race closes for free — an explicit drain cost +2.1%. Needs Q_TDM_DEPTH=3 to keep the same lead.
Pin the K RoPE value for the SWA copy. The f32 result sits on the exact bf16 tie, and the compiler rematerialises the expression for the second store with the other fma contraction. Explicit fma was not enough on its own; an empty asm is what pins it
Test Plan
144 op-test configs show the same failure set as pristine main on the same toolchain (all pre-existing). 60 paged-SWA configs: byte-exact warnings 8 → 0. NaN/Inf probe over 16 poison placements: 14 bit-identical, 2 differ only in a quiet NaN's sign bit. All five commits compile cleanly, so the series bisects.
Test Result
Submission Checklist