Skip to content

[HIP] fused_qknorm_idxrqknorm: write NVFP4 K/V/index-K caches - #7824

Merged
valarLip merged 10 commits into
mainfrom
ganyi/nvfp4-fused-write-pr
Nov 16, 2026
Merged

valarLip merged 10 commits into
mainfrom
ganyi/nvfp4-fused-write-pr

Conversation

@ganyi1996ppo

@ganyi1996ppo ganyi1996ppo commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds an NVFP4 write mode to fused_qknorm_idxrqknorm (the MXFP4 mode is #7057): kv_cache_dtype="nvfp4" together with index_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 refuses nvfp4 off 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 kNvfp4 template flag on the same kernel, not a second kernel; callers that do not need native NVFP4 should keep using MXFP4.

Page format

  • Index page, 9216 B: [128 tok x 64 B e2m1][128 tok x 8 B e4m3], one e4m3 scale per 16 elements, global scale 1.0.
  • KV page, 2 * num_kv_heads * 9216 B: [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_scale are the fp32 [1] global scales (gs); kv_cache_k and kv_cache_v are the same uint8 [num_pages, 2 * nkv * 9216] tensor, block_size 128.

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

file lines
csrc/kernels/fused_qknorm_idxrqknorm.cu +325 / -42: kNvfp4 template flag; NVFP4 row stores (K, V, index-K); arg checks
aiter/ops/fused_qknorm_idxrqknorm.py +13 / -2: docs of the new dtype and the gfx950 gate
op_tests/test_fused_qknorm_nvfp4.py +503: new test
op_tests/test_fused_qknorm_idxrqknorm.py +1 / -1: one expected message string, reworded because the error now covers nvfp4

Existing modes only gain if constexpr(kNvfp4) branches, the dtype/arg validation, and a false template argument in the MXFP4/fp8 launches.

Perf (MI355X gfx950, M3.1 TP4 shapes, nkv 1, one call)

tokens nvfp4 us nvfp4 TB/s mxfp4 us mxfp4 TB/s fp8 us fp8 TB/s
1 5.32 0.002 3.55 0.003 4.67 0.002
64 6.00 0.102 4.14 0.147 5.21 0.119
8192 52.9 1.48 35.2 2.23 41.3 1.92
32768 179.9 1.74 159.4 1.96 179.8 1.76

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.
  • Before this PR, the same pages were also compared with an independent vLLM reference outside the repo: 189 cases / 0 mismatching bytes, and 36/36 K+V and index pages equal.
  • Existing modes unchanged: 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.
  • Review round 2: the byte-exact sweep now also covers (a) KV pages with a 64 B padded page stride (allocated [pages, size+64] and narrowed; the pad bytes are compared too), (b) kv_cache_dtype="nvfp4" + index_cache_dtype="auto" + skip_index_branch=True with 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).
  • The gfx950 refusal for nvfp4 was checked by forcing get_gfx_runtime() to gfx942 in a host-only call; no gfx942 GPU was run.
  • The fp4_kv_cache rename and the static_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, cmp equal).
  • JIT build of module_fused_qknorm_idxrqknorm on main plus this change: 25 s, no CK headers needed.
  • Review round 3 (e1d7e24: the index-side fp4_index_cache rename, the redundant kIndexFp4 && dropped, error messages and comments that now cover nvfp4; c580578: three new REJECT entries, index nvfp4 with kv auto / kv mxfp4 and kv nvfp4 with index mxfp4, plus the test docstring, and one expected message string updated in test_fused_qknorm_idxrqknorm.py): on gfx950 test_fused_qknorm_nvfp4.py passes 880 byte-exact checks with the refusal cases 6 -> 9; test_fused_qknorm_idxrqknorm.py passes 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, .text and 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.
  • Second 11-15 review round (f6b44b8: the wrapper's description of the nvfp4 page (K scale in MFMA lane order, V data in 32-bit words, V scale [group][token]) and an unreachable nvfp4 clause removed from the asm_layout comment; b287735: two more REJECT cases, index not whole 9216 B pages and index with fewer pages than kv): on gfx950 test_fused_qknorm_nvfp4.py passes 880 byte-exact checks with the refusal cases 9 -> 11, test_fused_qknorm_idxrqknorm.py passes 3538 checks and 155 with --minimax_tp4, before and after; the comment edit leaves the gfx950 code object unchanged (disassembly, metadata, .text, .rodata and symbol table identical; only the per-build __hip_cuid_ differs). A misaligned index_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.
  • black, ruff 0.16.0 and clang-format 18 (on the added lines) clean.

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" with index_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.py compares 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 to origin/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 than qkv, which could not be exercised with one visible GPU.

PR-specific question

Selection: only kv_cache_dtype="nvfp4" together with index_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 on qkv's device; anything else fails an AITER_CHECK before launch. Architecture: the wrapper refuses kv_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 forcing get_gfx_runtime() to gfx942 in a host-only call; no gfx942 GPU was run). A static_assert ties kNvfp4 to kKvFp4 && kIndexFp4 so 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 existing test_fused_qknorm_idxrqknorm.py covers 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

ganyi1996ppo and others added 2 commits November 11, 2026 08:23
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]>
@ganyi1996ppo
ganyi1996ppo requested review from a team and a balanced review from Copilot November 11, 2026 14:23
@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 7824 --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.

@github-actions github-actions Bot added the HIP label Nov 11, 2026
@aiter-bot

aiter-bot commented Nov 11, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision b28773520b5d · mode shadow

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

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. Which architecture, dtype and shape boundaries select the changed kernel path, and what invariant prevents unsupported inputs from reaching it?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

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.

🟡 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

Comment thread csrc/kernels/fused_qknorm_idxrqknorm.cu Resolved
Comment thread csrc/kernels/fused_qknorm_idxrqknorm.cu Resolved
… 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]>
Copilot AI balanced review requested due to automatic review settings November 12, 2026 01:40

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.

🔵 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 valarLip left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

⚠️ Action required before merge (4)

  1. State in the description why NVFP4 is needed next to the MXFP4 cache from #7057. The kernel side is done well: one kNvfp4 template 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.
  2. Gate nvfp4 to gfx950 in the wrapper. aiter/ops/fused_qknorm_idxrqknorm.py:127 refuses mxfp4 off gfx950, but nvfp4 has 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.
  3. Test the two accepted layouts the op test never runs. First, kv_cache_dtype="nvfp4" with index_cache_dtype="auto" and skip_index_branch=True, which the host check at fused_qknorm_idxrqknorm.cu:1213 explicitly 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_pages already honours stride(0).
  4. Make the flag lattice explicit. mxfp4_kv_cache now also means nvfp4 (.cu:1209). Rename it to fp4_kv_cache. Also add static_assert(!kNvfp4 || (kKvFp4 && kIndexFp4)) next to the template parameter at .cu:491, so an instantiation with kNvfp4=true and kKvFp4=false cannot silently fall back to MXFP4 stores. Minor: or t == 64 in main()'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.

ganyi1996ppo and others added 3 commits November 15, 2026 03:58
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]>
Copilot AI balanced review requested due to automatic review settings November 15, 2026 10:02
@ganyi1996ppo

Copy link
Copy Markdown
Contributor Author

Thanks @valarLip. All four items are addressed at head 382b6e5 (3 commits on top of 2f0bda9); the description is updated too.

  1. Why NVFP4 next to MXFP4: added to the description (new paragraph under Summary): the numbers do not favour it (216 vs 204 B/token, 52.9 vs 35.2 us at 8192 tokens, no GSM8K gain in ATOM Fix fp8 sparse MLA folded batch mapping #3310); the reason is that NVFP4 is MiniMax-M3.1's native fp4 KV/index format and the benchmark rules do not accept a non-native fp4 cache. Same wording as on [FlyDSL] M3 index score: NVFP4 decode scorer (score_flydsl_nvfp4) #7813.
  2. gfx950 gate: ec3fbd7. The wrapper check now covers {"mxfp4","nvfp4"} for either dtype argument and raises off gfx950. I took the gate option (ATOM Fix fp8 sparse MLA folded batch mapping #3310 is gfx950-only), so the test's early return stays. Checked by forcing get_gfx_runtime() to gfx942 in a host-only call: nvfp4/nvfp4, nvfp4/auto, auto/nvfp4 and mxfp4/mxfp4 all raise, auto/auto passes. No gfx942 GPU was run.
  3. The two untested layouts: 382b6e5 adds three variants to the byte-exact sweep (tokens 1/7/64/300 x nkv 1/2/4 x scatter/seq_off3, gs 0.37): padded (pages allocated [pages, size+64] and narrowed; pad bytes compared too), skip (nvfp4 + index "auto" + skip_index_branch=True with index operands; index cache and index q must stay untouched) and skip_bare (same with no index operands). On gfx950: 216 rows, 0 mismatching bytes. A reference that ignores the page stride is caught by the padded case (kv err 0.52).
  4. Flag lattice: 48ba7ac. mxfp4_kv_cache is now fp4_kv_cache (all 10 uses); static_assert(!kNvfp4 || (kKvFp4 && kIndexFp4)) sits at the top of the kernel body (a static_assert cannot go inside the template parameter list, and the body is the first place that sees all three flags); dead or t == 64 removed. The rename and the assert do not change generated code: the gfx950 hsaco of the rebuilt module is byte-identical to the one from 2f0bda9.

Not changed: the two Copilot threads were already fixed in 2f0bda9. The new padded case also covers the "padded stride still passes" side of that check.

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.

🔵 Needs a closer look

NVFP4 encoding and concurrent packed cache writes require gfx950 runtime confirmation, but the review was static and no head-matched validation report was supplied.

No open issues 🎉

Review effort: Balanced

@valarLip valarLip left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

⚠️ Action required before merge (3)

  1. Finish the flag-lattice rename on the index side. csrc/kernels/fused_qknorm_idxrqknorm.cu:1227 mxfp4_index_cache = index_cache_dtype == "mxfp4" || nvfp4_index_cache is the same problem item 4 of my last review fixed for K/V: rename it to fp4_index_cache (uses at :1228, :1289, :1298, :1384, :1601 and the launch chain), and drop the now-redundant kIndexFp4 && in if constexpr(kIndexFp4 && kNvfp4) at :790 (the static_assert at :539 implies it; the K branch at :818 already tests kNvfp4 alone).
  2. 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:384 says nvStoreRowData writes a scale byte "at scale_off" (it takes no scale_off and writes none); .cu:399 says storeNvfp4IndexRow also serves main K (main K uses storeNvfp4KRow). 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.
  3. Add the converse refusal case. REJECT covers kv nvfp4 + index auto, but not index nvfp4 with kv auto/mxfp4 (and kv nvfp4 + index mxfp4), which the new check at .cu:1215 refuses. Add those entries to REJECT; 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.

ganyi1996ppo and others added 2 commits November 15, 2026 12:16
…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]>
Copilot AI balanced review requested due to automatic review settings November 15, 2026 18:31

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.

🔵 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

@ganyi1996ppo

Copy link
Copy Markdown
Contributor Author

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

  1. Finish the flag-lattice rename on the index side: fixed in e1d7e24. mxfp4_index_cache is now fp4_index_cache (it is true for nvfp4 too), and if constexpr(kIndexFp4 && kNvfp4) is if constexpr(kNvfp4) (the static_assert at the top of the kernel already implies the first term). No change to the generated code: I built the module before and after and compared the gfx950 code objects. The disassembly (575,534 lines), kernel metadata, .rodata, .text and the sorted symbol table are identical; the only difference is the per-build __hip_cuid_ id, which also changes when the same source is built in another directory.
  2. Messages and comments that now cover nvfp4: fixed in e1d7e24 (and the test docstring in c580578). You were right on all of them: nvStoreRowData takes no scale_off and writes no scale (its callers do); storeNvfp4IndexRow is index-K only (main K uses storeNvfp4KRow). The two messages now say "'mxfp4'/'nvfp4'" and "uint8 for mxfp4/nvfp4"; the index_holds, caller-contract and launch comments cover the shared fp4 path, and I fixed the same stale wording in one more launch comment found by grep. The test docstring points to the page format below it instead of ATOM's file. I left the messages that really are mxfp4-only, and the Python wrapper had no such wording. The reworded message needed one expected string updated in test_fused_qknorm_idxrqknorm.py (same commit as the tests).
  3. Converse refusal cases: fixed in c580578. REJECT has three new entries: index nvfp4 with kv auto, index nvfp4 with kv mxfp4, and kv nvfp4 with index mxfp4. Each is refused by the "go together" check and the test asserts that on the real op.

On gfx950 test_fused_qknorm_nvfp4.py passes 880 byte-exact checks, with the refusal cases 6 -> 9; test_fused_qknorm_idxrqknorm.py passes 3538 checks (155 with --minimax_tp4) before and after. Not run: gfx942, CI's ruff 0.16.0, and the nvfp4 timing table.

@valarLip valarLip left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

⚠️ Action required before merge (3)

  1. Fix the wrapper's description of the nvfp4 KV page. aiter/ops/fused_qknorm_idxrqknorm.py:110-111 says "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]".
  2. Drop the nvfp4 clause added to the asm_layout caller-contract comment. .cu:1490-1491 "numel / 9216 * 128 for nvfp4" sits in the else if(asm_layout) branch, which nvfp4 never reaches: an nvfp4 index requires nvfp4 K/V, and nvfp4 K/V takes the if(nvfp4_kv_cache) branch at .cu:1390. Keep only the mxfp4 part.
  3. 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 to test_fused_qknorm_idxrqknorm.py. Optional: add a REJECT case 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.

ganyi1996ppo and others added 2 commits November 15, 2026 18:49
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]>
Copilot AI balanced review requested due to automatic review settings November 16, 2026 00:50
@ganyi1996ppo

Copy link
Copy Markdown
Contributor Author

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

  1. Wrapper doc: "K scale and V in MFMA lane order": fixed in f6b44b8. You were right, only the K scale uses the MFMA lane order. The doc now says "K scale in MFMA lane order, V data in 32-bit words (8 tokens x 4 bit), V scale [group][token]", the same as the PR description.
  2. "numel / 9216 * 128 for nvfp4" in the asm_layout branch: fixed in f6b44b8, removed; an nvfp4 index requires nvfp4 K/V, which takes the nvfp4 branch above, so it never reaches the asm_layout branch. The mxfp4 part stays. The edit is comments only: I built the module before and after and the gfx950 code objects are identical (disassembly of 575,534 lines, kernel metadata, .text, .rodata and symbol table; only the per-build __hip_cuid_ id differs, which also changes when the same source is built in another directory).
  3. "What changes" table, and the optional REJECT case: the table in the description now reads .cu +325/-42, wrapper +13/-2 (docs of the new dtype and the gfx950 gate), test_fused_qknorm_nvfp4.py +503, and lists the +1/-1 change to test_fused_qknorm_idxrqknorm.py (one expected message string reworded because the error now covers nvfp4); the Self-Review revision is b287735. The REJECT case is added in b287735: index not whole pages (an index_cache of one page plus 128 bytes, refused with "whole 9216 B pages"). I also added index fewer pages than kv (refused with "index_cache must contain at least num_blocks * block_size rows"), which the code already refuses.

On gfx950 test_fused_qknorm_nvfp4.py passes 880 byte-exact checks, with the refusal cases 9 -> 11; test_fused_qknorm_idxrqknorm.py passes 3538 checks, 155 with --minimax_tp4, before and after. One more thing I checked, from our own review of this head: a misaligned index_cache (data_ptr offsets 1, 2, 3 and 6) wrote the same bytes as the aligned run and did not fault on gfx950, on one small shape; I added no alignment check. Not run: gfx942 and CI's ruff 0.16.0.

@valarLip @valar-cop re-review at b287735.

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.

🔵 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

@valar-cop valar-cop left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM

ci green.

@valarLip
valarLip merged commit 47d2301 into main Nov 16, 2026
@valarLip
valarLip deleted the ganyi/nvfp4-fused-write-pr branch November 16, 2026 05:47
lirui927 pushed a commit to lirui927/aiter that referenced this pull request Nov 23, 2026
…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]>
lirui927 pushed a commit to lirui927/aiter that referenced this pull request Nov 23, 2026
…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]>
lirui927 pushed a commit to lirui927/aiter that referenced this pull request Nov 26, 2026
…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]>
lirui927 pushed a commit to lirui927/aiter that referenced this pull request Nov 30, 2026
…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]>
XiaobingSuper pushed a commit to XiaobingSuper/aiter that referenced this pull request Dec 3, 2026
…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]>
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.

6 participants