Repository navigation
[HIP] fused_qknorm_idxrqknorm: write NVFP4 K/V/index-K caches - #7824
Conversation
kv_cache_dtype / index_cache_dtype "nvfp4": K, V and index-K are quantized to NVFP4 (e2m1 + one e4m3 scale per 16 elements) straight into the pages in the same pass as norm/RoPE, mirroring the existing MXFP4 branch. Other modes are unchanged. Co-Authored-By: Claude <[email protected]>
…vfp4 write 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: |
WAITING ON AUTHORPR Self-ReviewRevision Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision. Questions to answer in the PR description
Edit the Missing or invalid answers
Shadow mode reports what would block but keeps the status successful. Set |
There was a problem hiding this comment.
🟡 Changes recommended
Validation still permits overlapping cache pages and cross-device scale pointers, risking cache corruption and invalid device-memory access.
2 open issues
What changed in this PR
Adds direct NVFP4 cache writes to the fused QK normalization/RoPE operator, avoiding bf16 scratch buffers and a separate quantization kernel. The page format matches the related NVFP4 index scorers and sparse paged-attention consumers.
Changes:
- Adds NVFP4 quantization, K/V/index-K page stores, and argument validation.
- Documents the new cache mode and caller requirements.
- Adds byte-exact reference checks, rejection cases, and write-mode benchmarks.
| File | Description |
|---|---|
op_tests/test_fused_qknorm_nvfp4.py |
Adds NVFP4 correctness checks and comparative benchmarks. |
csrc/kernels/fused_qknorm_idxrqknorm.cu |
Implements fused NVFP4 cache writes and validation. |
aiter/ops/fused_qknorm_idxrqknorm.py |
Documents NVFP4 layouts, scales, and slot requirements. |
Review effort: Balanced
… cross-device operands The nvfp4 cache check only required 4 B alignment, so an expanded view with page stride 0 was accepted and distinct pages aliased. Require stride(0) >= size(1) when there is more than one page; padded page strides still pass. The launch runs on qkv's device and dereferences the caches and the global scales through raw pointers, but the nvfp4 path only required them to be GPU tensors. Require the KV pages, k_scale, v_scale and (when the index branch runs) index_cache to be on qkv's device. The other modes keep their existing checks. Host-side checks only; the kernel and its outputs are unchanged. Co-Authored-By: Claude <[email protected]>
There was a problem hiding this comment.
🔵 Needs a closer look
The packed-cache ABI and shared-word atomic V updates require independent GPU validation of NVFP4 byte compatibility and existing-mode performance, which this static review environment cannot provide.
No open issues 🎉
2 resolved since last review
Review effort: Balanced
valarLip
left a comment
There was a problem hiding this comment.
⚠️ Action required before merge (4)
- State in the description why NVFP4 is needed next to the MXFP4 cache from #7057. The kernel side is done well: one
kNvfp4template flag on the same kernel, not a second kernel. The numbers alone argue against it (216 vs 204 B/token, 52.9 vs 35.2 us write at 8192 tokens, no GSM8K gain in ATOM #3310), so say the real reason: NVFP4 is MiniMax-M3.1's native fp4 KV/index format and the benchmark rules do not accept a non-native fp4 cache. Same ask as on #7813. - Gate nvfp4 to gfx950 in the wrapper.
aiter/ops/fused_qknorm_idxrqknorm.py:127refusesmxfp4off gfx950, butnvfp4has no gate. The new test returns early off gfx950 (op_tests/test_fused_qknorm_nvfp4.py:430), and the only consumer, ATOM #3310, is gfx950-only. Either add"nvfp4"to that check, or run the byte-exact test on gfx942 in CI and remove the early return. - Test the two accepted layouts the op test never runs. First,
kv_cache_dtype="nvfp4"withindex_cache_dtype="auto"andskip_index_branch=True, which the host check atfused_qknorm_idxrqknorm.cu:1213explicitly allows. Second, a padded page stride, which the commit message says still passes (k_s_block = stride(0)at.cu:1411). For the second, allocate[pages, size + 64]and narrow it;ref_pagesalready honoursstride(0). - Make the flag lattice explicit.
mxfp4_kv_cachenow also means nvfp4 (.cu:1209). Rename it tofp4_kv_cache. Also addstatic_assert(!kNvfp4 || (kKvFp4 && kIndexFp4))next to the template parameter at.cu:491, so an instantiation withkNvfp4=trueandkKvFp4=falsecannot silently fall back to MXFP4 stores. Minor:or t == 64inmain()'s case filter is dead.
Copilot's two findings are fixed at this head: the page-stride check at .cu:1403 and the device checks at .cu:1406. CI is green, and test_fused_qknorm_nvfp4.py ran and passed in MI35X shard 1.
Reviewed at 2f0bda9. Already fixed: aliasing page stride, cross-device cache/scale operands.
The wrapper refused mxfp4 off gfx950 but not nvfp4. Gate both; the only consumer is gfx950-only and the byte-exact test already skips elsewhere. Co-Authored-By: Claude <[email protected]>
mxfp4_kv_cache also means nvfp4, so rename it fp4_kv_cache; add a static_assert that kNvfp4 implies kKvFp4 and kIndexFp4 so it cannot fall back to MXFP4 stores; drop a dead `or t == 64` in the op test case filter. No change to the generated device code (hsaco byte-identical). Co-Authored-By: Claude <[email protected]>
…ayers Add three variants to the byte-exact sweep: KV pages with a 64 B page-stride pad (pad bytes must stay), skip_index_branch with index_cache_dtype "auto" (index operands untouched), and the same with no index operands passed. Co-Authored-By: Claude <[email protected]>
|
Thanks @valarLip. All four items are addressed at head 382b6e5 (3 commits on top of 2f0bda9); the description is updated too.
Not changed: the two Copilot threads were already fixed in 2f0bda9. The new |
valarLip
left a comment
There was a problem hiding this comment.
⚠️ Action required before merge (3)
- Finish the flag-lattice rename on the index side.
csrc/kernels/fused_qknorm_idxrqknorm.cu:1227mxfp4_index_cache = index_cache_dtype == "mxfp4" || nvfp4_index_cacheis the same problem item 4 of my last review fixed for K/V: rename it tofp4_index_cache(uses at :1228, :1289, :1298, :1384, :1601 and the launch chain), and drop the now-redundantkIndexFp4 &&inif constexpr(kIndexFp4 && kNvfp4)at :790 (thestatic_assertat :539 implies it; the K branch at :818 already testskNvfp4alone). - Make the messages and comments that now cover nvfp4 say so.
.cu:1229"index_cache_dtype='mxfp4' requires the sparse insert mode" and.cu:1368"(uint8 for mxfp4)" fire for nvfp4 too;.cu:1381(index_holds "8704 B pages for mxfp4") and.cu:1601("MXFP4 main K/V and/or index K") describe the shared fp4 path;.cu:384saysnvStoreRowDatawrites a scale byte "at scale_off" (it takes no scale_off and writes none);.cu:399saysstoreNvfp4IndexRowalso serves main K (main K usesstoreNvfp4KRow). In the test docstring (op_tests/test_fused_qknorm_nvfp4.py:8), "ATOM's nvfp4_pages.py" points outside this repo; reference the page-format block below it instead. - Add the converse refusal case.
REJECTcovers kvnvfp4+ indexauto, but not indexnvfp4with kvauto/mxfp4(and kvnvfp4+ indexmxfp4), which the new check at.cu:1215refuses. Add those entries toREJECT; they fit the existing "go together" message.
The kernel side checks out. A CPU emulation of nvE4m3Byte/nvE2m1Code matches torch's e4m3 RNE cast and nearest-even e2m1 on 3.4M inputs, including every midpoint, with 0 mismatches. nvKScaleOff, the V nibble shift and the page offsets match the consumer layout in #7825. CI: test_fused_qknorm_nvfp4.py passed in MI35X shard 1. The only failure, shard 7, is a test_flydsl_hgemm.py segfault that also hits unrelated PRs, so it is not from this change.
Re-reviewed at 382b6e5. Already fixed: NVFP4-vs-MXFP4 rationale in the description, nvfp4 gfx950 gate in the wrapper, padded-stride / skip_index_branch test variants, fp4_kv_cache rename + static_assert, dead or t == 64; Copilot's page-stride and device checks.
…where it applies Rename mxfp4_index_cache to fp4_index_cache (it is true for nvfp4 too), drop the kIndexFp4 && that kNvfp4 already implies (static_assert), and fix the messages and comments that read as mxfp4-only although nvfp4 takes the same path, or are wrong about nvStoreRowData / storeNvfp4IndexRow. No code generation change. Co-Authored-By: Claude <[email protected]>
Add the converse refusal cases (index nvfp4 with kv auto / mxfp4, kv nvfp4 with index mxfp4) and point the docstring at the page format in the file. The mxfp4 reject check expects the reworded 'mxfp4'/'nvfp4' sparse-insert message. Co-Authored-By: Claude <[email protected]>
There was a problem hiding this comment.
🔵 Needs a closer look
The NVFP4 packed-cache layout and cross-warp atomic writes require domain review because GPU execution and consumer compatibility could not be independently validated, and no head-matched gfx950 validation report was supplied.
No open issues 🎉
Review effort: Balanced
|
@valarLip, re-review of 11-15 17:51, point by point. Pushed head is now c580578 (fast-forward of 382b6e5; fixes in e1d7e24 and c580578). The description is updated to match.
On gfx950 |
valarLip
left a comment
There was a problem hiding this comment.
⚠️ Action required before merge (3)
- Fix the wrapper's description of the nvfp4 KV page.
aiter/ops/fused_qknorm_idxrqknorm.py:110-111says "K scale and V in MFMA lane order", but only the K scale uses lane order (nvKScaleOff). V data is 32-bit words of 8 tokens, and V scale is[group][token](.cu:315, test docstring). Use the wording from the PR description: "K scale in MFMA lane order, V data in 32-bit words (8 tokens x 4 bit), V scale [group][token]". - Drop the nvfp4 clause added to the asm_layout caller-contract comment.
.cu:1490-1491"numel / 9216 * 128 for nvfp4" sits in theelse if(asm_layout)branch, which nvfp4 never reaches: an nvfp4 index requires nvfp4 K/V, and nvfp4 K/V takes theif(nvfp4_kv_cache)branch at.cu:1390. Keep only the mxfp4 part. - Update the "What changes" table in the description. It still says
.cu+308/-30, wrapper "+9: docs of the new dtype" (it now also carries the gfx950 gate), and test +459. At head the numbers are +327/-43, +12/-2 and +495, and the table does not list the one-line change totest_fused_qknorm_idxrqknorm.py. Optional: add aREJECTcase for the nvfp4 index-cache check at.cu:1294(uint8, whole 9216 B pages).
All three asks from my last review are fixed at head. fp4_index_cache replaces mxfp4_index_cache at every use, and the redundant kIndexFp4 && is gone. The messages and comments on the shared fp4 path now cover nvfp4, the ATOM reference is out of the docstring, and the three reverse REJECT cases each reach the "go together" check at .cu:1216 before any other check. CI is green except MI35X shard 3, which is still pending. test_fused_qknorm_nvfp4.py passed in shard 1.
Re-reviewed at c580578. Already fixed: fp4_index_cache rename + redundant kIndexFp4 &&, nvfp4 wording in the sparse-insert/uint8 messages and the nvStoreRowData/storeNvfp4IndexRow/index_holds/launch comments, test docstring ATOM reference, reverse go-together refusal cases; earlier: NVFP4 rationale, gfx950 gate, padded-stride/skip variants, fp4_kv_cache + static_assert, Copilot's page-stride and device checks.
The wrapper doc said V is in MFMA lane order (only the K scale is; V data is 32-bit words of 8 tokens, V scale [group][token]). The asm_layout caller-contract comment named nvfp4, which never reaches that branch. Comments only. Co-Authored-By: Claude <[email protected]>
Two more refusal cases: index_cache that is not whole 9216 B pages, and an index_cache with fewer pages than the kv cache. Co-Authored-By: Claude <[email protected]>
|
@valarLip, re-review of 11-15 21:03, point by point. Pushed head is now b287735 (fast-forward of c580578): f6b44b8 (comments) and b287735 (test). The description is updated to match.
On gfx950 @valarLip @valar-cop re-review at b287735. |
There was a problem hiding this comment.
🔵 Needs a closer look
The byte-exact NVFP4 layouts and shared-word atomic V-cache writes require gfx950 validation that this environment could not independently reproduce, and no head-bound validation report was supplied.
No open issues 🎉
Review effort: Balanced
…950, gfx1250) kv_cache_dtype="nvfp4" with byte k_scale / v_scale planes (uint8 or float8_e4m3fn) writes K (normed, roped) and V straight into the NVFP4 row layout of MiniMax-M3's sparse pool and of the FlyDSL paged decode: per (page, kv head) an e2m1 payload [128, 64] and E4M3 scales [128, 8], no global scale, page and head strides free. ATOM staged these K/V in BF16 and quantized them in two more launches per sparse layer. - The scale dtype picks the layout: fp32 [1] k_scale / v_scale keep the page layout of ROCm#7824, unchanged; byte planes select the row layout. - scale = e4m3(amax / 6) (RNE, saturating at 448), codes = e2m1(x / scale) half to even, sign from x < 0, IEEE divisions: the arithmetic of ATOM's Triton NVFP4 store, so the bytes match it. Integer and compare ops only, so it also runs on gfx1250 (the page-layout nvfp4 / mxfp4 stay gfx950). - The index cache keeps its "auto" / fp8 write; an fp4 index with the row-layout K/V is refused. The scale-plane strides ride in the page-128 token-stride arguments, which the row layout does not use. - One new kernel template flag, kNvRows (default false): the existing kernels compile to identical ISA on gfx1250 and gfx950. op_tests/test_fused_qknorm_nvfp4.py: test_nvfp4_rows checks the rows byte for byte against "auto" + a torch reference of the rule, on strided fields of one pre-filled pool; test_rows_reject covers the host refusals and the wrapper's arch gate per scale dtype. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…950, gfx1250) kv_cache_dtype="nvfp4" with byte k_scale / v_scale planes (uint8 or float8_e4m3fn) writes K (normed, roped) and V straight into the NVFP4 row layout of MiniMax-M3's sparse pool and of the FlyDSL paged decode: per (page, kv head) an e2m1 payload [128, 64] and E4M3 scales [128, 8], no global scale, page and head strides free. ATOM staged these K/V in BF16 and quantized them in two more launches per sparse layer. - The scale dtype picks the layout: fp32 [1] k_scale / v_scale keep the page layout of ROCm#7824, unchanged; byte planes select the row layout. - scale = e4m3(amax / 6) (RNE, saturating at 448), codes = e2m1(x / scale) half to even, sign included, in IEEE arithmetic: the rule of ATOM's Triton NVFP4 store, so the bytes match it. A negative element of a zero-scale block therefore codes as +0. The sign is read from x's bits, because this file is built with fp32 denormals flushed and x / scale can be one. Integer and compare ops only, so it also runs on gfx1250 (the page-layout nvfp4 / mxfp4 stay gfx950). - The index cache keeps its "auto" / fp8 write; an fp4 index with the row-layout K/V is refused. The scale-plane strides ride in the page-128 token-stride arguments, which the row layout does not use. - One new kernel template flag, kNvRows (default false): the existing kernels compile to identical ISA on gfx1250 and gfx950. The group amax of the two NVFP4 layouts is one helper, nvGroupAmax. op_tests/test_fused_qknorm_nvfp4.py: test_nvfp4_rows checks the rows byte for byte against "auto" + a torch reference of the rule, on strided fields of one pre-filled pool, including zero-scale blocks of either sign and elements whose x / scale is an fp32 denormal; test_rows_reject covers the host refusals and the wrapper's arch gate per scale dtype. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…he row layout (gfx950, gfx1250) kv_cache_dtype="nvfp4" with byte k_scale / v_scale planes (uint8 or float8_e4m3fn) writes K (normed, roped) and V straight into the NVFP4 row layout of MiniMax-M3's sparse pool and of the FlyDSL paged decode: per (page, kv head) an e2m1 payload [128, 64] and E4M3 scales [128, 8], no global scale. With it, index_cache_dtype="nvfp4" and the new index_scale argument write the index key in the same layout and the same pass: payload [pages, 128, 64], scales [pages, 128, 8], addressed by index_slot_mapping. ATOM staged these rows in BF16 and quantized them in three more launches per sparse layer. - The scale dtype picks the layout. fp32 [1] k_scale / v_scale keep the page layout of ROCm#7824, still gfx950-only; byte planes select the row layout, on gfx950 and gfx1250. The wrapper uses this only for its arch check; the C++ host checks own every other rule. - With the row-layout K/V the index cache is "auto", fp8 or NVFP4 rows (index_scale). An mxfp4 or page-layout nvfp4 index cache is refused. - scale = e4m3(amax / 6) (RNE, saturating at 448), codes = e2m1(x * (1 / scale)) half to even, sign included, in IEEE arithmetic: the rule of ATOM's Triton NVFP4 stores (kv_nvfp4_store, index_fp4_store), so the bytes match them. A negative element of a zero-scale block therefore codes as +0. The sign is read from x's bits, because this file is built with fp32 denormals flushed and x * (1 / scale) can be one. No hardware fp4 / fp8 convert is used (the rounding is integer and compare ops), so the rule also runs on gfx1250. - Token rows are contiguous inside a (page, head); the base and the page and head strides of every plane are free. The 16-bit payload store (nvStoreRowData, which the page layout shares) assumes no alignment and compiles to the same code as before; the host refuses a plane in which two (page, head)s share bytes. - Kernel: template flags kNvRows and kNvIndexRows (default false) and one trailing NvRows argument: the K/V scale-plane strides, and the index rows' scale plane and page strides. The existing kernels keep their instructions and resources; only the implicit-kernarg offset moves, by the argument's 40 B. The group amax of the two NVFP4 layouts is one helper, nvGroupAmax. op_tests/test_fused_qknorm_nvfp4.py: test_nvfp4_rows checks the K/V and index rows byte for byte against "auto" + a torch reference of the rule, on strided fields of one pre-filled pool, also with odd payload bases and odd page and head strides, including zero-scale blocks of either sign and elements whose x * (1 / scale) is an fp32 denormal; test_rows_reject covers the host refusals and the wrapper's arch gate per scale dtype. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…he row layout (gfx950, gfx1250) kv_cache_dtype="nvfp4" with byte k_scale / v_scale planes (uint8 or float8_e4m3fn) writes K (normed, roped) and V straight into the NVFP4 row layout of MiniMax-M3's sparse pool and of the FlyDSL paged decode: per (page, kv head) an e2m1 payload [128, 64] and E4M3 scales [128, 8], no global scale. With it, index_cache_dtype="nvfp4" and the new index_scale argument write the index key in the same layout and the same pass: payload [pages, 128, 64], scales [pages, 128, 8], addressed by index_slot_mapping. ATOM staged these rows in BF16 and quantized them in three more launches per sparse layer. - The scale dtype picks the layout. fp32 [1] k_scale / v_scale keep the page layout of ROCm#7824, still gfx950-only; byte planes select the row layout, on gfx950 and gfx1250. The wrapper uses this only for its arch check; the C++ host checks own every other rule. - The row layout runs the generic kernel at every token count. The prefill variant and the raw-pointer entry of ROCm#8472 are the page layout's: their V runs, K scale order and single page stride are that format's, and the variant is compiled for gfx950 only. The launcher reaches it under kNvfp4, which the row layout does not set. Neither Python fast path (the raw front, the deferred QKV front of ROCm#8607) takes a call with index_scale, so a page-layout call that passes it is refused (by the C++ checks, or by the deferred front itself) instead of having it dropped. - With the row-layout K/V the index cache is "auto", fp8 or NVFP4 rows (index_scale). An mxfp4 or page-layout nvfp4 index cache is refused (each would be its own set of kernels). - scale = e4m3(amax / 6) (RNE, saturating at 448), codes = e2m1(x * (1 / scale)) half to even, sign included, in IEEE arithmetic: the rule of ATOM's Triton NVFP4 stores (kv_nvfp4_store, index_fp4_store), so the bytes match them on finite rows. The scale byte and the magnitudes come from nvQuantLane with a unit global scale (the page layout's quantizer, on gfx950 its hardware converts): amax * float32(1/6) rounds to the e4m3 byte of amax / 6 for every bf16 and fp16 amax. The sign is the row layout's own: none for a NaN, a zero or any element of a zero-scale group, read from x's bits because this file is built with fp32 denormals flushed and x * (1 / scale) can be one. The store is storeNvfp4IndexRow with a kRows flag. - Token rows are contiguous inside a (page, head); the base and the page and head strides of every plane are free, each plane on its own. The 16-bit payload store (nvStoreRowData, which the page layout shares) assumes no alignment and compiles to the same code as before; the host refuses a plane in which two (page, head)s share bytes. - Kernel: template flags kNvRows and kNvIndexRows (default false) and one trailing NvRows argument: the K and V scale-plane strides, and the index rows' scale plane and page strides. 16 new kernels. The existing ones keep their instructions and resources; only the implicit-kernarg offset moves, by the argument's 56 B. The prefill kernels are unchanged. op_tests/test_fused_qknorm_nvfp4.py: test_nvfp4_rows checks the K/V and index rows byte for byte against "auto" + a torch reference of the rule, on strided fields of one pre-filled pool: also with odd payload bases and odd page and head strides, with every plane on its own strides and the two scale planes of different byte dtypes, and in the call shape of the prefill variant (16 q / 1 kv / 1 index heads, bf16, e4m3 index_q_out). The rows include zero-scale blocks of either sign, elements whose x * (1 / scale) is an fp32 denormal, NaN and infinities. test_rows_reject covers the host refusals, the wrapper's arch gate per scale dtype and the deferred front's refusal; test_reject gains the page-layout call of the raw front's shape with index_scale. stress_qkv's largest magnitude is now the dtype's (3e38 does not fit fp16). Co-Authored-By: Claude Fable 5.2 <[email protected]>
…he row layout (gfx950, gfx1250) kv_cache_dtype="nvfp4" with byte k_scale / v_scale planes (uint8 or float8_e4m3fn) writes K (normed, roped) and V straight into the NVFP4 row layout of MiniMax-M3's sparse pool and of the FlyDSL paged decode: per (page, kv head) an e2m1 payload [128, 64] and E4M3 scales [128, 8], no global scale. With it, index_cache_dtype="nvfp4" and the new index_scale argument write the index key in the same layout and the same pass: payload [pages, 128, 64], scales [pages, 128, 8], addressed by index_slot_mapping. ATOM staged these rows in BF16 and quantized them in three more launches per sparse layer. - The scale dtype picks the layout. fp32 [1] k_scale / v_scale keep the page layout of ROCm#7824, still gfx950-only; byte planes select the row layout, on gfx950 and gfx1250. The wrapper uses this only for its arch check; the C++ host checks own every other rule. - The row layout runs the generic kernel at every token count. The prefill variant and the raw-pointer entry of ROCm#8472 are the page layout's: their V runs, K scale order and single page stride are that format's, and the variant is compiled for gfx950 only. The launcher reaches it under kNvfp4, which the row layout does not set. Neither Python fast path (the raw front, the deferred QKV front of ROCm#8607) takes a call with index_scale, so a page-layout call that passes it is refused (by the C++ checks, or by the deferred front itself) instead of having it dropped. - With the row-layout K/V the index cache is "auto", fp8 or NVFP4 rows (index_scale). An mxfp4 or page-layout nvfp4 index cache is refused (each would be its own set of kernels). - scale = e4m3(amax / 6) (RNE, saturating at 448), codes = e2m1(x * (1 / scale)) half to even, sign included, in IEEE arithmetic: the rule of ATOM's Triton NVFP4 stores (kv_nvfp4_store, index_fp4_store), so the bytes match them on finite rows. The scale byte and the magnitudes come from nvQuantLane with a unit global scale (the page layout's quantizer, on gfx950 its hardware converts): amax * float32(1/6) rounds to the e4m3 byte of amax / 6 for every bf16 and fp16 amax. The sign is the row layout's own: none for a NaN, a zero or any element of a zero-scale group, read from x's bits because this file is built with fp32 denormals flushed and x * (1 / scale) can be one. The store is storeNvfp4IndexRow with a kRows flag. - Token rows are contiguous inside a (page, head); the base and the page and head strides of every plane are free, each plane on its own. The 16-bit payload store (nvStoreRowData, which the page layout shares) assumes no alignment and compiles to the same code as before; the host refuses a plane in which two (page, head)s share bytes. - Kernel: template flags kNvRows and kNvIndexRows (default false) and one trailing NvRows argument: the K and V scale-plane strides, and the index rows' scale plane and page strides. 16 new kernels. The existing ones keep their instructions and resources; only the implicit-kernarg offset moves, by the argument's 56 B. The prefill kernels are unchanged. op_tests/test_fused_qknorm_nvfp4.py: test_nvfp4_rows checks the K/V and index rows byte for byte against "auto" + a torch reference of the rule, on strided fields of one pre-filled pool: also with odd payload bases and odd page and head strides, with every plane on its own strides and the two scale planes of different byte dtypes, and in the call shape of the prefill variant (16 q / 1 kv / 1 index heads, bf16, e4m3 index_q_out). The rows include zero-scale blocks of either sign, elements whose x * (1 / scale) is an fp32 denormal, NaN and infinities. test_rows_reject covers the host refusals, the wrapper's arch gate per scale dtype and the deferred front's refusal; test_reject gains the page-layout call of the raw front's shape with index_scale. stress_qkv's largest magnitude is now the dtype's (3e38 does not fit fp16). Co-Authored-By: Claude Fable 5.2 <[email protected]>


Summary
Adds an NVFP4 write mode to
fused_qknorm_idxrqknorm(the MXFP4 mode is #7057):kv_cache_dtype="nvfp4"together withindex_cache_dtype="nvfp4"quantizes K, V and index-K straight into the NVFP4 pages in the same pass as norm/RoPE, so a caller no longer needs a bf16 scratch plus a separate quantize kernel. The rounding is integer/compare arithmetic (no hardware convert), so the bytes should not depend on the arch; the wrapper nevertheless refusesnvfp4off gfx950 (tested on gfx950 only). Other modes are untouched.Why NVFP4 next to the MXFP4 cache (#7057). The numbers alone do not favour NVFP4: 216 vs 204 B/token of cache written, and the write is slower (52.9 vs 35.2 us at 8192 tokens, see Perf); ATOM #3310 showed no GSM8K gain. The reason for this mode is format, not speed: NVFP4 is MiniMax-M3.1's native fp4 KV/index format, and the benchmark rules do not accept a non-native fp4 cache, so the MXFP4 cache cannot be used for that benchmark. It is one
kNvfp4template flag on the same kernel, not a second kernel; callers that do not need native NVFP4 should keep using MXFP4.Page format
[128 tok x 64 B e2m1][128 tok x 8 B e4m3], one e4m3 scale per 16 elements, global scale 1.0.2 * num_kv_heads * 9216B:[K data | K scale | V data | V scale], per head 8192 B data + 1024 B scale. K data token-major, K scale in MFMA lane order, V data in 32-bit words (8 tokens x 4 bit), V scale[group][token].k_scale/v_scaleare the fp32[1]global scales (gs);kv_cache_kandkv_cache_vare the same uint8[num_pages, 2 * nkv * 9216]tensor,block_size128.Layout matches the NVFP4 index PRs (#7813, #7814) and the sparse-PA kernels.
Caller contract
V is written with atomic and/or on 32-bit words (up to 8 tokens share a word, in any warp). This assumes no two tokens of one call share a slot. A whole-octet vector store was measured slower (32K tokens: 249 us vs 173 us), so it is not included.
What changes
csrc/kernels/fused_qknorm_idxrqknorm.cukNvfp4template flag; NVFP4 row stores (K, V, index-K); arg checksaiter/ops/fused_qknorm_idxrqknorm.pyop_tests/test_fused_qknorm_nvfp4.pyop_tests/test_fused_qknorm_idxrqknorm.pyExisting modes only gain
if constexpr(kNvfp4)branches, the dtype/arg validation, and afalsetemplate argument in the MXFP4/fp8 launches.Perf (MI355X gfx950, M3.1 TP4 shapes, nkv 1, one call)
The NVFP4 write is slower than the MXFP4 write of the same kernel; TB/s is nominal traffic (qkv read, q/index-q written, cache rows written).
Tests run
op_tests/test_fused_qknorm_nvfp4.py(gfx950): torch reference in the file, whole pages compared byte for byte (K, V, index, q) on garbage-prefilled pages; tokens 1/7/64/300 x nkv 1/2/4 x gs 1.0/0.37/3.0 x slots scatter/seq/seq_off3/seq_gaps/mtp, zero/saturating/tie/signed-zero rows, plus the 4 perf shapes: 0 mismatching bytes; refusals checked.op_tests/test_fused_qknorm_idxrqknorm.py(default and--minimax_tp4) run on main and on this branch with every call's tensors hashed: 6732 + 9 call hashes identical, and identical between two runs on main.[pages, size+64]and narrowed; the pad bytes are compared too), (b)kv_cache_dtype="nvfp4"+index_cache_dtype="auto"+skip_index_branch=Truewith index operands passed (index cache and index q must stay untouched), (c) the same with no index operands: 216 rows on gfx950, 0 mismatching bytes, exit 0. A reference that ignores the page stride is caught (kv err 0.52 in a one-off control).get_gfx_runtime()to gfx942 in a host-only call; no gfx942 GPU was run.fp4_kv_cacherename and thestatic_assert(!kNvfp4 || (kKvFp4 && kIndexFp4))do not change generated code: the gfx950 device code object (hsaco) of the JIT-built module is byte-identical before and after (7,893,408 B,cmpequal).module_fused_qknorm_idxrqknormon main plus this change: 25 s, no CK headers needed.fp4_index_cacherename, the redundantkIndexFp4 &&dropped, error messages and comments that now cover nvfp4; c580578: three newREJECTentries, index nvfp4 with kv auto / kv mxfp4 and kv nvfp4 with index mxfp4, plus the test docstring, and one expected message string updated intest_fused_qknorm_idxrqknorm.py): on gfx950test_fused_qknorm_nvfp4.pypasses 880 byte-exact checks with the refusal cases 6 -> 9;test_fused_qknorm_idxrqknorm.pypasses 3538 checks, and 155 with--minimax_tp4, before and after. The rename does not change generated code: I built the module before and after and compared the gfx950 code objects; the disassembly (575,534 lines), kernel metadata,.rodata,.textand the sorted symbol table are identical, and the only difference is the per-build__hip_cuid_id (it also changes when the same source is built in another directory). gfx942, CI's ruff 0.16.0 and the nvfp4 timing table were not re-run.REJECTcases, index not whole 9216 B pages and index with fewer pages than kv): on gfx950test_fused_qknorm_nvfp4.pypasses 880 byte-exact checks with the refusal cases 9 -> 11,test_fused_qknorm_idxrqknorm.pypasses 3538 checks and 155 with--minimax_tp4, before and after; the comment edit leaves the gfx950 code object unchanged (disassembly, metadata,.text,.rodataand symbol table identical; only the per-build__hip_cuid_differs). A misalignedindex_cache(data_ptr offsets 1, 2, 3, 6) wrote the same bytes as the aligned run and did not fault on gfx950, on one small shape; no alignment check was added.Not in this PR
ATOM integration (calling the op with the nvfp4 dtypes).
PR Self-Review
Reviewed revision
b287735 (head after the second 11-15 review round; the full sha is the PR head)
Behavior and intent
Adds
kv_cache_dtype="nvfp4"withindex_cache_dtype="nvfp4": K, V and index-K are quantized into NVFP4 pages (e2m1 codes, e4m3 scale per 16 elements, fp32 global scale for K/V) in the same pass as norm and RoPE. Before, a caller needed a bf16 scratch and a second quantize kernel. The NVFP4 write is slower than the MXFP4 write of the same kernel (179.9 vs 159.4 us at 32768 tokens), which is why it is opt-in.Validation and unverified scope
Run on one MI355X (gfx950):
python3 op_tests/test_fused_qknorm_nvfp4.pycompares whole pages byte for byte against a torch reference (tokens 1/7/64/300, nkv 1/2/4, gs 1.0/0.37/3.0, 5 slot patterns, zero/saturating/tie/signed-zero rows): 0 mismatching bytes, exit 0 on the head above. Existing modes:op_tests/test_fused_qknorm_idxrqknorm.py(default and--minimax_tp4) with per-call tensor hashes (6732 + 9) identical toorigin/main, re-measured on the head above. Outside the repo: 189 cases / 0 mismatching bytes against an independent vLLM reference (run before the review fixes, which touch only host-side checks). Unverified: other GPU archs (gfx942 etc.), callers that violate the unique-slot contract, and the new refusal for operands on a different device thanqkv, which could not be exercised with one visible GPU.PR-specific question
Selection: only
kv_cache_dtype="nvfp4"together withindex_cache_dtype="nvfp4"(index may be "auto" only with skip_index_branch) enters the new path. It needs the sparse insert mode,block_size==128, uint8 pages[num_pages, 2*nkv*9216](4 B aligned, k and v the same tensor, page stride at least the page size), fp32[1]k_scale/v_scale, and the cache, scales and index cache onqkv's device; anything else fails an AITER_CHECK before launch. Architecture: the wrapper refuseskv_cache_dtype/index_cache_dtype="nvfp4"(as"mxfp4") off gfx950 with a RuntimeError before any launch; the kernel itself has no arch gate. The NVFP4 rounding is integer/compare arithmetic with no hardware convert, but only gfx950 was run (the refusal on another arch was checked by forcingget_gfx_runtime()to gfx942 in a host-only call; no gfx942 GPU was run). Astatic_asserttieskNvfp4tokKvFp4 && kIndexFp4so the NVFP4 path cannot fall back to MXFP4 stores. Invariant for slots: V is written with 32-bit atomic and/or, so no two tokens of one call may share a slot (documented caller contract, not checked on device).Changed op tests
op_tests/test_fused_qknorm_nvfp4.py(new, +503): the only test of the NVFP4 write mode. It builds the expected K, V and index pages with an in-file torch reference and compares whole pages byte for byte on garbage-prefilled pages, plus refusal checks (including an aliased-pages case) and the 4 perf shapes. The existingtest_fused_qknorm_idxrqknorm.pycovers only the MXFP4/fp8/bf16 modes and cannot exercise this layout, so it is not redundant. It also runs the padded page stride and the index-less layers (skip_index_branch with index_cache_dtype "auto", with and without index operands).🤖 Generated with Claude Code