Skip to content

[HIP] [Perf] fused_qknorm_idxrqknorm: nvfp4 quantizer on the gfx950 fp8 / fp4 converts (stacked on #8472) - #8473

Closed
ganyi1996ppo wants to merge 2 commits into
ROCm:mainfrom
ganyi1996ppo:ganyi/fused-qknorm-idxr-hw-convert
Closed

ganyi1996ppo wants to merge 2 commits into
ROCm:mainfrom
ganyi1996ppo:ganyi/fused-qknorm-idxr-hw-convert

Conversation

@ganyi1996ppo

Copy link
Copy Markdown
Contributor

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_f32 at 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

nvQuantLane was 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 / nvE2m1CodeInt are the old routines unchanged; nvE4m3ByteHw / nvE4m3ValueHw / nvE2m1PackHw (#if defined(__gfx950__)) are the converts; nvE4m3Byte / nvE4m3Value / nvE2m1Pack pick one at compile time.
  • nvQuantLane returns 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.
  • Equivalence (all checked on MI355X, standalone harness compiling the two routines side by side from this file, not committed):
    • e2m1 code: all 2^32 fp32 bit patterns x the 4 nibble positions: 0 mismatches. The hardware convert gives NaN the largest magnitude while the integer code gives it magnitude 0 (every compare false) with the NaN's own sign bit, so a NaN is first replaced by the zero of its sign (4 compare + select per lane). Infinities, both zeros, subnormals and every tie agree without help.
    • e4m3 byte: every fp32 in [0, 448] (including subnormals, ties, -0 via the & 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).
    • e4m3 value: every byte the byte routine can return (0 .. 0x7E): 0 mismatches.
    • The quantizer needs gs > 0 (a negative scale gives a negative sf, where the two routines differ; the old integer path gave garbage there). k_scale / v_scale are 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, fmin clamp, NaN -> code 0 with its sign, infinity -> largest magnitude, a saturating nearest-grid search with ties to the even code); stress_qkv adds 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:

tokens 8 32 64 256 512 4096 8192
base (main) 5.489 5.760 5.969 6.619 8.019 27.389 50.639
#8472 5.429 5.669 5.879 6.439 7.489 27.049 49.860
this PR 4.909 5.159 5.339 5.829 7.429 25.610 48.320
this PR vs #8472 -9.6% -9.0% -9.2% -9.5% -0.8% -5.3% -3.1%
this PR vs main -10.6% -10.4% -10.6% -11.9% -7.4% -6.5% -4.6%

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

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X
python op_tests/test_fused_qknorm_nvfp4.py -t 1 7 8 16 64 300 --perf_tokens 8     # 1260 byte-for-byte checks, 0 failed
python op_tests/test_fused_qknorm_idxrqknorm.py                                      # passed

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

  • Docstrings updated (positive global scales)

Dependencies

  • No new third-party dependencies added

Breaking Changes

None for positive scales (the contract); a non-positive k_scale / v_scale was 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 != 64 for the nvfp4 test (the quantizer does not depend on it; test_fused_qknorm_idxrqknorm.py covers 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

ganyi1996ppo and others added 2 commits November 22, 2026 21:06
…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]>
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

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

Extended tests (opt-in via labels):

Label Tests
ci:mi300x Run Aiter standard tests on MI300X in PRs; main branch always runs them
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 (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the Radeon R9700 ROCm runner
ci:multi-gpu 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: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
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 8473 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

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 stop the title rewrites; labels stay in sync either way.

Reviews:
A reviewer is usually assigned within one working day after the PR is ready for review (see Code Review Process). If nobody has responded for about a week, mention the area contact listed in CONTACTS.md. Want to help review? See REVIEWERS.md.

ganyi1996ppo pushed a commit to ganyi1996ppo/aiter that referenced this pull request Nov 23, 2026
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]>
@ganyi1996ppo

Copy link
Copy Markdown
Contributor Author

Merged into #8472 (same kernel, now three commits on one branch). Closing in favour of #8472.

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.

1 participant