Skip to content

Opt qk_norm_rope_group_quant in gfx1250 - #5712

Merged
junhaha666 merged 5 commits into
ROCm:mainfrom
yzhou103:opt/qk_issue_bound_gfx1250
Sep 21, 2026
Merged

junhaha666 merged 5 commits into
ROCm:mainfrom
yzhou103:opt/qk_issue_bound_gfx1250

Conversation

@yzhou103

@yzhou103 yzhou103 commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Technical Details

  1. 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.

  2. 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.

  3. 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.

  4. 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

yzhou added 5 commits September 18, 2026 10:17
…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.
@yzhou103
yzhou103 requested review from a team and a lite review from Copilot September 21, 2026 02:27
@github-actions github-actions Bot changed the title Opt/qk issue bound gfx1250 [HIP] Opt/qk issue bound gfx1250 Sep 21, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
multigpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 5712 --add-label <label>

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to opt this PR out.

@github-actions github-actions Bot added the HIP label Sep 21, 2026
@yzhou103 yzhou103 changed the title [HIP] Opt/qk issue bound gfx1250 Opt qk_norm_rope_group_quant in gfx1250 Sep 21, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Medium severity

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.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants