Repository navigation
[HIP] [Perf] fused_qknorm_idxrqknorm: nvfp4 quantizer on the gfx950 fp8 / fp4 converts (stacked on #8472) - #8473
ganyi1996ppo wants to merge 2 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]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
New doc (this branch had none). Documents what the branch is (origin/main + PRs ROCm#8465/ROCm#8472/ROCm#8473, confirmed a strict superset of the production container's baked-in aiter commit b9e08da, 114 commits ahead with zero divergence), the known prior gap this closes (the production recipe's container bakes aiter into its image, so this branch's code was never actually exercised by a real server run before this pass), and the real wired e2e verification performed this pass: a fresh clone wired via a PYTHONPATH prepend ahead of the image's baked-in copy, confirmed from a live server process's own log, ran a full 900s c1 AgentX point with zero errors. Also notes a real gotcha hit this pass (the composable_kernel git submodule needs --recursive) and that amd_kernels elimination is a separate, not-yet-completed effort on the ATOM side, not this branch. Co-Authored-By: Claude Sonnet 5 <[email protected]>
Summary
Stacked on #8472 (the first commit of this branch is that PR; review the top commit only, and the diff will shrink when #8472 lands).
NVFP4 quantizer of
fused_qknorm_idxrqknorm(index-K, main K, main V): on gfx950 compute the e4m3 scale byte, its decoded value and the e2m1 codes with the hardware converts (v_cvt_pk_fp8_f32,v_cvt_f32_fp8,v_cvt_scalef32_pk_fp4_f32at scale 1) instead of the integer / compare chains. Same bytes; the integer routines stay as the path of the other arches. Both exact reciprocals (__fdiv_rn) are kept.Motivation
nvQuantLanewas about a compare / select chain per element (7 compares + adds for the e2m1 code, a bit-twiddled RNE for the e4m3 byte and its decode). The kernel is short (5 us for 8 tokens, 25 us for 4096), so the VALU work of the quantizer is a visible part of it.Changes
nvE4m3ByteInt / nvE4m3ValueInt / nvE2m1CodeIntare the old routines unchanged;nvE4m3ByteHw / nvE4m3ValueHw / nvE2m1PackHw(#if defined(__gfx950__)) are the converts;nvE4m3Byte / nvE4m3Value / nvE2m1Packpick one at compile time.nvQuantLanereturns the four e2m1 codes of the lane packed as nibbles (element 0 lowest, the page layout); the K / index store writes it as is, the V store extracts each element's nibble.& 0x7F): 0 mismatches.sf = fminf(amax * (1/6) * (1/gs), 448)is in that range for every positive gs (fminf maps NaN, e.g. 0 * inf, to 448; amax is never NaN since fmaxf skips it).sf, where the two routines differ; the old integer path gave garbage there).k_scale/v_scaleare device values the host does not check; the op docstring now says positive.op_tests/test_fused_qknorm_nvfp4.py: the torch reference now states the NaN / infinity semantics (NaN-skipping amax,fminclamp, NaN -> code 0 with its sign, infinity -> largest magnitude, a saturating nearest-grid search with ties to the even code);stress_qkvadds V rows with NaN next to live values and as whole groups, infinities, NaN + infinity, negative zeros, bf16 subnormals, e4m3 scale ties (2^-10 -> 0, 1.5 * 2^-9 -> 2) and two rows of random bit patterns;--special_gs(default 1e-30, 1e30) adds extreme global scales on a few shapes.Performance (if applicable)
Device-side kernel time of the nvfp4 insert (M3.1 TP4 shapes nq=16 nkv=1 niq=1, rotary 64, torch profiler, median of 200 launches, inputs rotated, 6 runs alternating base / #8472 / this PR on one MI355X GPU), us:
This PR was lower than #8472 in 6/6 paired runs at every size except 512 tokens (5/6). Larger prefill chunks gain less in relative terms because the HBM traffic dominates; no end-to-end model run was done.
Testing
The same new test file also passes against the main build and the #8472 build (both integer path), 1260 / 0 failed each, so the new reference describes the existing kernel and the hardware path matches it.
Documentation
Dependencies
Breaking Changes
None for positive scales (the contract); a non-positive
k_scale/v_scalewas already producing garbage scale bytes.PR Self-Review
Behavior and intent
Bytes written to the NVFP4 pages are unchanged for every input of the quantizer with gs > 0; only the instruction sequence changes on gfx950. Other arches keep the integer routines (nvfp4 is refused off gfx950 by the Python wrapper today).
Validation and unverified scope
gfx950 only. The exhaustive hardware-vs-integer comparison was a standalone run (not in the repo: it needs the device functions side by side, which exist only in this translation unit); the op test checks the kernel against the torch oracle. Not verified: end-to-end model run,
rotary_dim != 64for the nvfp4 test (the quantizer does not depend on it;test_fused_qknorm_idxrqknorm.pycovers rotary 64 and 128 on #8472), fp32-subnormal global scales (the oracle does not model the kernel's subnormal arithmetic; both old and new paths behave the same there on the cases tried).Changed op tests
op_tests/test_fused_qknorm_nvfp4.py: reference semantics for NaN / Inf / signed zero / subnormal / scale ties, V rows that hit them, extreme global scales; these cases fail if the converts were swapped for something that disagrees on any of them (e.g. the NaN case, which the bare hardware convert gets wrong).🤖 Generated with Claude Code