Repository navigation
[HIP] [Perf] fused_qknorm_idxrqknorm: DPP lane exchanges, xor-atomic V nibbles, FMA reciprocal (stacked on #8473) - #8580
ganyi1996ppo wants to merge 3 commits into
Conversation
…ope operand loads up front In fusedQKNormIdxrQKNormKernel the loads of one warp's work were a chain: slot_mapping, then the row and position behind the V branch, then the norm weight (folded into the norm's predicated region, so it waited right after issuing), then cos/sin. Load the row and slot_mapping before the V branch (V uses both), and issue the position, norm weight and cos/sin loads together past it, none behind a condition and none converted until used. The arithmetic is unchanged. Co-Authored-By: Claude <[email protected]>
…4 converts nvQuantLane's e4m3 scale byte, its decode and the e2m1 codes were integer / compare chains. On gfx950 use v_cvt_pk_fp8_f32, v_cvt_f32_fp8 and v_cvt_scalef32_pk_fp4_f32 (scale 1) for the same values; the integer routines stay as the other arches' path. Both exact reciprocals (__fdiv_rn) are kept. The bytes are unchanged: exhaustive over all 2^32 fp32 inputs for the e2m1 codes and over [0, 448] for the e4m3 byte, with a NaN mapped to the zero of its sign as the integer code does. The op test now runs NaN, infinity, signed zero, subnormal, scale-tie and random-bit-pattern V rows and extreme global scales against a reference that states those semantics. Co-Authored-By: Claude <[email protected]>
…V nibbles, FMA reciprocal NVFP4 decode insert (gfx950): replace the ds_bpermute shuffles of the norm / amax butterflies and the RoPE partner exchange by DPP moves and v_permlane16_swap, land the V nibble with one xor per word (two 64-bit atomics per lane instead of four and + or pairs), and compute the two exact reciprocals of the quantizer with v_rcp_f32 plus two FMA Newton steps. Bit-identical output. Technique credit: amd-kernels norm_rope_nvfp4_insert. Co-Authored-By: Claude <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
DRAFT — evaluation pausedPR Self-ReviewRevision This pull request is a draft, so readiness evaluation is paused and no verdict applies. Evaluation resumes when it is marked ready for review and the required CI completes. |
There was a problem hiding this comment.
🟡 Changes recommended
Accepted 4-byte-aligned cache layouts can reach wide loads and atomics that require stricter alignment, causing undefined behavior without a 32-bit fallback.
1 open issue
What changed in this PR
This PR reduces latency in aiter’s HIP fused Q/K normalization, RoPE, and NVFP4 cache-insert kernel for gfx950 decode workloads. It builds on #8472 and #8473, aiming to preserve output bytes while reducing instruction and memory-access costs.
Changes:
- Replaces selected lane shuffles with faster gfx950 lane-exchange instructions.
- Combines V-cache nibble updates into two 64-bit XOR atomics per lane.
- Uses a refined reciprocal fast path with division fallback for exceptional values.
| File | Description |
|---|---|
op_tests/test_fused_qknorm_nvfp4.py |
Inherited from #8473: expands special-value reference behavior and extreme-scale coverage. |
csrc/kernels/fused_qknorm_idxrqknorm.cu |
Adds lane-exchange, V-write, and reciprocal optimizations atop the stacked kernel changes. |
aiter/ops/fused_qknorm_idxrqknorm.py |
Comment-only changes inherited from #8473 clarify positive scales and hardware rounding. |
Review effort: Balanced
| const uint4 old = *reinterpret_cast<const uint4*>(words); // in flight during the quantization | ||
| float v[kElemsPerLane]; | ||
| loadElems(row, v); | ||
| uint32_t packed; | ||
| const uint32_t sb = nvQuantLane(v, gs, packed); | ||
| // The nibble of token `pos` is written by this token only (a slot is written once per call), so | ||
| // one xor of (old ^ new) << shift per word lands it without disturbing the other tokens' | ||
| // nibbles of the octet (they flip disjoint bits, and xors commute): two 64-bit atomics per lane | ||
| // instead of 4 x (and + or). `old` is a plain load issued before the quantization; only its own | ||
| // nibble is used, and no other token changes that nibble. | ||
| const uint32_t d0 = ((((old.x >> shift) ^ (packed >> 0)) & 0xFu) << shift); | ||
| const uint32_t d1 = ((((old.y >> shift) ^ (packed >> 4)) & 0xFu) << shift); | ||
| const uint32_t d2 = ((((old.z >> shift) ^ (packed >> 8)) & 0xFu) << shift); | ||
| const uint32_t d3 = ((((old.w >> shift) ^ (packed >> 12)) & 0xFu) << shift); | ||
| using u64 = unsigned long long; | ||
| u64* w64 = reinterpret_cast<u64*>(words); | ||
| atomicXor(w64, static_cast<u64>(d0) | (static_cast<u64>(d1) << 32)); | ||
| atomicXor(w64 + 1, static_cast<u64>(d2) | (static_cast<u64>(d3) << 32)); |

Summary
Stacked on #8473 (which is stacked on #8472): the first two commits of this branch are those PRs, review the top commit only; the diff shrinks as they land.
fused_qknorm_idxrqknorm, NVFP4 insert (gfx950, MiniMax-M3 decode rows 8..512): remove the remaining serial latency of the kernel with three changes, all bit-identical.__shfl_xor/__shfl, i.e.ds_bpermute(an LDS round trip each). On gfx950 xor-1/2/4/8 are DPP moves (quad_perm,row_shr/row_shl,row_ror:8) and xor-16 isv_permlane16_swap(inline asm: the two-result builtin is folded to one register by the compiler;s_nop 1around it). Same lane map, same summation order aswarpReduceSum(16, 8, 4, 2, 1).atomicAnd+atomicOrper word (8 atomics per lane). The token's nibble is written by that token only, so oneatomicXorof(old ^ new) << shiftlands it without touching the other tokens' nibbles of the octet (disjoint bits, xors commute): two 64-bit atomics per lane.oldis a plainuint4load issued before the quantization, only its own nibble is used.__fdiv_rn(1, x)ofnvQuantLane(about 25 instructions each with denormal-mode switching) becomev_rcp_f32plus two FMA Newton steps (rcpRN). Identical to__fdiv_rn(1, d)for every positive normal fp32 (all 2^31 patterns, exhaustive on MI355X, 0 mismatches); outside [2^-100, 2^100] and for 0 / inf / nan it falls back to__fdiv_rn.All three are gated on
__gfx950__; every other arch compiles to the previous code. The DPP rope partner is used forrotary_dim == 64(partner == lane ^ 8), other rotary dims keep__shfl.Motivation / known regression (stated on purpose)
The point is to run the MiniMax-M3.1 TP4 decode on aiter alone (no amd-kernels
norm_rope_nvfp4_insert). This kernel does not reach the amd-kernels one: it is still about +1.0 us per call slower (table below), which is about 60 sparse layers x 1.0-1.1 us = 0.06-0.07 ms per decode step, about 0.2-0.3% of a 28.95 ms G64 step. That regression was reviewed and accepted by the owner in exchange for dropping the amd-kernels dependency. Public ATOM already callsaiter.fused_qknorm_idxrqknormfor this insert (atom/model_ops/attention_mha.py), so no ATOM change is needed; this PR only makes that call faster.Tried and not included: reordering / pinning the kernel arguments in SGPRs (the serial
s_loadchain), no change (3.78 vs 3.77 us at M=8); a V "run writer" without atomics, slower below about 200 rows.Results (MI355X, gfx950, ROCm 7.2.4; graph replay of 64 distinct inserts, event timing, us per call)
Each cell is the median of 4 alternating rounds (A, B, C order repeated twice in one lease on one GPU). A = #8473 head, this PR = A + top commit, amd-kernels =
norm_rope_nvfp4_insert_d128_rot64_p128. TP4 head set (16 q, 1 kv, 1 index head, head_dim 128, rotary 64), bf16, random qkv x3, scattered positions.(1024 rows and above are prefill chunks; >= 1024 tokens go to the FlyDSL kernel of #8576 on the TP4 head set.) Versus #8473 the top commit saves 0.5 us at 8 rows to 2.0 us at 384 rows.
Correctness
bit-identical: Truein all 18 comparisons per arm).op_tests/test_fused_qknorm_nvfp4.py(unchanged): 912 checks pass (atol=0, rtol=0against the torch reference incl. special global scales, padded / skip layouts, seq / scatter / MTP slot patterns, refusals), exit 0; same on [HIP] [Perf] fused_qknorm_idxrqknorm: nvfp4 quantizer on the gfx950 fp8 / fp4 converts (stacked on #8472) #8473 for comparison. This covers the MXFP4 / fp8 modes that sharenormAndRope.__fdiv_rn(1, x).Reproduce:
ARMS=ak,ai MS=8,16,64,128,288,384,512,1024,4096 python3 mb.pystyle harness (graph of 64 inserts, event timing) on one idle MI355X;python3 op_tests/test_fused_qknorm_nvfp4.py.Review
Run by hand through the repo's
review-prskill (triage rules A1 A4 B2 C1 C2 D1 D8 P1-P3 P5 P6): no sibling variant carries a changed line, no guard removed; A4 (sharednormAndRope/ quantizer code executed by every mode): covered by the unchanged op test above plus bit-compare vs the previous kernel; D1: the xor depends on the old word but only on its own nibble (stale or uninitialized pages are fine); P6: measured base vs head back to back on one GPU (table above). Findings fixed before opening: DPP andrcpRNfirst gated on__GFX9__/ ungated, now gfx950 only since only gfx950 was tested; rope DPP restricted torotary_dim == 64; kernel-argument pinning and aQKDBGdebug hook from the experiments removed.Technique credit: amd-kernels
norm_rope_nvfp4_insert(DPP / permlane exchanges, run-wise V write idea).🤖 Generated with Claude Code