Skip to content

[HIP] fused_qknorm_idxrqknorm: write NVFP4 K/V and the index key in the row layout (gfx950, gfx1250) - #8290

Open
lirui927 wants to merge 4 commits into
ROCm:mainfrom
lirui927:lirui/nvfp4-qknorm-rows
Open

lirui927 wants to merge 4 commits into
ROCm:mainfrom
lirui927:lirui/nvfp4-qknorm-rows

Conversation

@lirui927

@lirui927 lirui927 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What

fused_qknorm_idxrqknorm can now write NVFP4 K/V, and optionally the index key, straight into the NVFP4 row layout. This is the layout of MiniMax-M3's sparse NVFP4 pool and of the FlyDSL paged decode's NVFP4 K/V.

  • K/V: kv_cache_dtype="nvfp4" with byte k_scale / v_scale (uint8 or float8_e4m3fn) selects the row layout. Per (page, kv head) it writes an e2m1 payload [128, 64] and an E4M3 scale plane [128, 8], with no global scale.
  • Index key: index_cache_dtype="nvfp4" with the new optional index_scale writes the index key in the same row layout and in the same pass. The payload is [pages, 128, 64], the plane is [pages, 128, 8], and rows are addressed by index_slot_mapping.
  • Strides: 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, so the payloads and planes can be strided fields of one pool at any byte offset. The 16-bit payload store assumes no alignment. One thing is required, and checked on the host: no two (page, head)s of a tensor share bytes (an expanded view would alias its pages).
  • Arithmetic: scale = e4m3(amax / 6) (RNE, saturating at 448), then codes = e2m1(x * (1 / scale)) rounded half to even, sign included, in IEEE arithmetic. This is the rule of ATOM's Triton NVFP4 stores (kv_nvfp4_store, index_fp4_store). The scale byte and the code magnitudes come from the page layout's quantizer (nvQuantLane) with a unit global scale: amax * float32(1/6) rounds to the same e4m3 byte as amax / 6 for every bf16 and fp16 amax. Only the sign is the row layout's own: none for a NaN, a zero, or any element of a zero-scale block. It is read from the bits of x, because this file is built with fp32 denormals flushed and x * (1 / scale) can be one.
  • Kernel paths after [HIP] [Perf] fused_qknorm_idxrqknorm: early loads, hardware converts, DPP exchanges, xor-atomic V nibbles and a prefill variant #8472: the row layout runs the generic kernel at every token count, on gfx950 and gfx1250 (gfx1250-strict counts as gfx1250: the kernel source has no gfx1250-only switch). On gfx950 it uses [HIP] [Perf] fused_qknorm_idxrqknorm: early loads, hardware converts, DPP exchanges, xor-atomic V nibbles and a prefill variant #8472's hardware converts and DPP exchanges through the shared quantizer; elsewhere the integer routines. [HIP] [Perf] fused_qknorm_idxrqknorm: early loads, hardware converts, DPP exchanges, xor-atomic V nibbles and a prefill variant #8472's prefill variant and raw-pointer entry stay the page layout's (see "Relationship").
  • Unchanged paths: fp32 [1] k_scale / v_scale still select the [HIP] fused_qknorm_idxrqknorm: write NVFP4 K/V/index-K caches #7824 page layout, and that path behaves as before. The page-layout nvfp4 and mxfp4 modes stay gfx950-only. With the row-layout K/V, the index cache may be "auto", fp8, or NVFP4 rows; the mxfp4 and page-layout nvfp4 index caches are refused by the C++ host checks (each would be its own set of kernels).
  • Kernel: two template flags, kNvRows and kNvIndexRows (both default false), plus one trailing argument, NvRows: the page and head strides of the K and V scale planes, and the index rows' scale plane and page strides. The K/V payloads use the existing page and head stride arguments. The row store is the page layout's index-row store (storeNvfp4IndexRow) with a kRows flag.

Why

ATOM staged MiniMax-M3 sparse-layer K/V and the index key in BF16, then quantized them to NVFP4 in extra launches: two for K/V and one for the index key, per sparse layer. With this change all three come out of the one qk-norm launch, and the BF16 staging buffers go away.

The consumer is ROCm/ATOM#3581 (draft; ROCm/ATOM#3634 is stacked on it): its sparse layers call this op with the pool's E4M3 planes as k_scale / v_scale, and with index_scale for the index key. The K/V rows are read by the FlyDSL pa_decode NVFP4 path carried by draft #8365 (#8289, which introduced it, was closed unmerged).

Timing on the current head (MI455X, one GPU, bf16, 16 q heads per kv head, slot-mapped writes; median of 5 runs × 200 iterations of run_perftest, min–max in parentheses; a script outside the tree):

tokens kv heads "auto" K/V + fp8 index (µs) NVFP4 rows K/V + fp8 index (µs) NVFP4 rows K/V + NVFP4 index rows (µs)
64 1 3.91 (3.89–3.92) 4.00 (3.99–4.02) 4.03 (4.03–4.04)
64 4 4.32 (4.29–5.49) 4.36 (4.35–4.37) 4.38 (4.38–4.41)
3000 1 18.58 (18.34–18.89) 14.82 (14.80–14.87) 15.00 (14.98–15.03)
3000 4 49.91 (49.81–50.22) 43.27 (43.21–43.34) 43.48 (43.44–43.53)
16384 1 69.21 (69.00–69.35) 62.63 (62.62–62.74) 63.42 (63.31–63.48)
16384 4 222.22 (222.02–222.40) 216.27 (216.12–216.42) 218.06 (217.89–218.21)

The table of the previous description was measured before #8472 reworked the kernel and is withdrawn. This PR does not measure an end-to-end gain.

Relationship to #7824, #8472, #8607 and #8011

Testing

The two op tests ran on one idle MI455X (image rocm/fw-bringup:gfx1250-minimax-m3-20261112) at the head. The module was not rebuilt in that run; the C++ is unchanged since bc7cbbb (the three later commits change the wrapper's arch gate, the op test's gate and refusal table and one comment). The rest ran without a GPU.

  • MI455X (gfx1250), python3 op_tests/test_fused_qknorm_nvfp4.py: exit 0; 84 of 84 row-layout cases with zero byte error (kv err, idx err, q err all 0); row-layout reject: ok; the page-layout part skips on gfx1250 ("tested on gfx950 only").
    • test_nvfp4_rows: 84 cases against a torch reference of the rule, applied to the same call with "auto" caches, byte for byte. Cases cover bf16 / fp16; 1 / 2 / 4 kv heads; fp8, bf16 and NVFP4-rows index caches; uint8, e4m3 and mixed scale views; pad slots; and skip_index_branch. Each payload and plane is a strided field of one pool pre-filled with random bytes, and the whole pool is compared, so stray writes fail. 9 cases use odd bases and odd page and head strides; 9 put every plane on its own page stride and head stride; 3 are the call shape of [HIP] [Perf] fused_qknorm_idxrqknorm: early loads, hardware converts, DPP exchanges, xor-atomic V nibbles and a prefill variant #8472's prefill variant and raw entry (16 q / 1 kv / 1 index heads, bf16, e4m3 index_q_out) in the row layout. The V rows include ties, clamps, subnormal scales, signed zeros, zero-scale blocks of either sign, elements whose x * (1 / scale) is an fp32 denormal, NaN and infinities.
    • test_rows_reject: at bc7cbbb 15 calls, each in a child process (2 valid ones and 13 host refusals, each matched on its C++ message); the head keeps 11 of them (four refusals that repeated another case's check and message are dropped); plus, in process, the wrapper's arch gate for each scale dtype (gfx942 refused, gfx1250-strict taken for byte planes; gfx1250 refused for the fp32 globals) and the deferred front's refusal of index_scale.
  • The wrapper's arch gate without a GPU (local script gate_host.py, the C++ entry mocked and the arch faked): the row layout is taken on gfx950, gfx1250 and gfx1250-strict and refused on gfx942 and gfx1100; the page layout is taken on gfx950 only. At bc7cbbb the gfx1250-strict call was refused.
  • MI455X, python3 op_tests/test_fused_qknorm_idxrqknorm.py: exit 0; 21 passed, 0 failed.
  • The row store on a host CPU (local script quant_host.py): the source text of the quantizer and of storeNvfp4IndexRow is cut out of the .cu and compiled with g++ (the non-gfx950 path, which is what gfx1250 compiles; lane exchanges, __fdiv_rn, NaN-skipping min / max and flushed denormals are stand-ins). 809200 rows (the test's own rows for bf16 and fp16, every 16-bit pattern of both dtypes in three orders, the finite ones at four scales), each stored at a token of a pre-filled page: 0 bytes differ from the test's reference and nothing else is written. This models the source on a CPU; it is not what the GPU executes. Four mutants of the sign rule (sign from the product, no zero-scale guard, -0 signed, NaN signed) each differ. e4m3(amax / 6) against e4m3(amax * float32(1/6)): 0 of 64386 bf16 / fp16 amax values differ.
  • Host checks without a GPU (local script host_checks.py): the module is built for gfx1250 and for gfx950 in a GPU-less container, and its Python wrapper and C++ host checks are driven with the op test's operands as CPU tensors that claim a GPU device. Nothing can launch there: an accepted call stops at hipGetDevice. 145 calls on the gfx1250 build and 158 on the gfx950 build give the expected verdict: the 15 test_rows_reject calls, the 13 page-layout test_reject calls (gfx950 build), the 84 row-layout calls of the sweep, 28 further stride / dtype cases, 13 arch-gate cases and 5 dispatch cases. The dispatch cases mock the device and the C++ entries: a page-layout call of the prefill shape reaches the raw entry; the same call with index_scale, and row-layout calls, reach the generic entry; a parked QKV front given index_scale raises before its chain kernel.
    • Mutants (gfx1250 build): without the overlap check, all 11 of its refusals are accepted; without the byte-dtype check of the planes, the fp32-plane refusal is accepted; without the index_scale term of the raw-front gate, the page-layout + index_scale call reaches the raw entry and 15 row-layout calls enter the front (which declines them); without the deferred front's term, its chain kernel is called with index_scale dropped.
  • The test's own expectation, without a GPU (same script): test_nvfp4_rows is run with the op replaced by an emulation that quantizes rows with the test's reference and stores them at the byte addresses the kernel computes from each tensor's base and strides. All 84 cases come out byte-exact. With an emulated 16-bit store that assumes alignment, the 75 even-layout cases still pass and all 9 odd-layout cases fail. With V's scale plane or V's payload on K's strides, or the scale planes on their payload's page stride, 8 of the 9 split-layout cases fail (the ninth writes page 0 only); with V's scale plane on K's head stride, the 6 with more than one kv head fail.
  • Offline compile against main (4960107ee; gfx1250 and gfx950, device code, the JIT's flags):
    • Of the 683 existing kernels, 3 are byte-identical (both prefill kernels among them) and 680 keep their instructions and resources: only the implicit-kernarg offset moves, by the 56 B of the new trailing argument (0xe8 → 0x120, two instructions per kernel).
    • The 16 new instantiations use 34–40 VGPRs on gfx1250 and 24–28 on gfx950, with no scratch or LDS. Against the previous head's row kernels they are shorter (gfx1250: 835–1555 instructions, was 913–1753; gfx950: 663–1320, was 904–1711) and divide half as often (8–12 v_div per kernel, was 16–24).
    • Build warnings: 10 on each arch, the same as main (shared headers).
  • black and ruff pass. clang-format was not available; the C++ follows the file's style by hand.

Not covered

PR Self-Review

Reviewed revision

6a6934d

Behavior and intent

Validation and unverified scope

Reviewed revision 6a6934d31fc43d5fa6ec5e367b3ee5d876830bc0. Unless a bullet says otherwise, the items below were run at bc7cbbb, whose C++ is the head's (the three later commits touch the wrapper's arch gate, the op test's gate and refusal table and a comment). The CPU items used the image rocm/fw-bringup:gfx1250-minimax-m3-20261112 without devices; the two op tests ran on one MI455X at the head.

  • JIT build of module_fused_qknorm_idxrqknorm with GPU_ARCHS=gfx1250 and with GPU_ARCHS=gfx950 (aiter.jit.core.build_module, no GPU): both build.
  • python3 host_checks.py <arch> on each of the two builds (local script; AITER_AOT_REPLAY=1 GPU_ARCHS=<arch>, CPU tensors that claim device 0, one child process per call that reaches C++): 145 calls on the gfx1250 build and 158 on the gfx950 build give the expected verdict, 0 bad. Four mutants (overlap check, byte-dtype check, raw-front gate term, deferred-front term) are each caught.
  • Placement emulation of test_nvfp4_rows (same script, no GPU): 84 of 84 cases byte-exact; the alignment and the four stride mutants fail the cases built for them.
  • hostpy.py quant_host.py (local script, host g++; a host model of the kernel source, not what the GPU executes): 809200 rows, 0 bytes differ from the test's reference and no byte outside the row changes; four sign-rule mutants differ; quant_host.py sixth: 0 of 64386 amax values differ.
  • ISA comparison against main 4960107ee (hipcc --cuda-device-only -S with the JIT's flags, --offload-arch=gfx1250 and gfx950): 3 existing kernels identical, 680 differ only in the implicit-kernarg offset (0xe8 → 0x120), with the same resources; 16 new kernels.
  • python3 op_tests/test_fused_qknorm_nvfp4.py in the image without a GPU: exits 1 at the file's first tensor (No CUDA GPUs are available), the statement main's file has too.
  • At the head, on the host: hostpy.py gate_host.py (wrapper arch gate, C++ entry mocked, arch faked): row layout taken on gfx950 / gfx1250 / gfx1250-strict, refused on gfx942 / gfx1100; page layout taken on gfx950 only. scratch_np/isa_host.sh <tree> gfx1250 <out> "--cuda-host-only -fsyntax-only" on the kernel file: no errors.
  • At the head: python -m black --check and ruff check --no-cache on the two changed Python files: pass.
  • MI455X at this revision, one idle GPU: python3 op_tests/test_fused_qknorm_nvfp4.py exits 0 (84 row cases byte-exact, row-layout reject: ok; the page-layout part skips, "tested on gfx950 only") and python3 op_tests/test_fused_qknorm_idxrqknorm.py exits 0 (21 passed).
  • MI455X, existing paths against main 4960107ee, interleaved, 5 rounds ("auto" and fp8-asm K/V with an fp8 index at 64 / 3000 / 16384 tokens, 1 / 4 kv heads; 12 rows): this head − main is between −0.6% and +0.1% of the median, i.e. unchanged.
  • Unverified: the page-layout part of op_tests/test_fused_qknorm_nvfp4.py on hardware (skips on gfx1250; MI35X CI); gfx1250-strict itself (not compiled for strict, not run on A0); the in-bounds kernel mutants; gfx950 hardware (compile, ISA and the host-check dry run only; MI35X CI is the first run of the row layout there, on [HIP] [Perf] fused_qknorm_idxrqknorm: early loads, hardware converts, DPP exchanges, xor-atomic V nibbles and a prefill variant #8472's hardware converts); the byte comparison against ATOM's Triton stores, last run on MI455X at 1712b07 with the previous arithmetic; an end-to-end ATOM run; clang-format.

PR-specific question

Which architecture, dtype and shape boundaries select the changed kernel path, and what invariant prevents unsupported inputs from reaching it?

  • Selection: the new path runs only when kv_cache_dtype="nvfp4" and k_scale is a byte tensor (uint8 or float8_e4m3fn). The index key takes the new path only when, in addition, index_cache_dtype="nvfp4" and index_scale is given. fp32 [1] scales keep the [HIP] fused_qknorm_idxrqknorm: write NVFP4 K/V/index-K caches #7824 page layout, including [HIP] [Perf] fused_qknorm_idxrqknorm: early loads, hardware converts, DPP exchanges, xor-atomic V nibbles and a prefill variant #8472's prefill variant and raw entry; every other mode runs the same device code as before, apart from the implicit-kernarg offset.
  • Architecture: the Python wrapper refuses the row layout unless base_gfx_arch(get_gfx_runtime()) is gfx950 or gfx1250 (RuntimeError), so gfx1250-strict is taken. The page-layout nvfp4 and mxfp4 caches stay refused off gfx950. The wrapper decides nothing else about the row layout.
  • Dtype: qkv is bf16 or fp16 (the scale rule is checked for these two). Payloads must be uint8; each scale plane is uint8 or float8_e4m3fn.
  • Shape: block_size is 128 and head_dim is 128. K/V payloads are [pages, nkv, 128, 64] and planes [pages, nkv, 128, 8]; the index payload is [pages, 128, 64] and its plane [pages, 128, 8]. Token rows are contiguous (the layout's definition). The mode is sparse insert only (qkv carries the index slots).
  • Strides: the base and the page and head strides of every plane are free, at any alignment and each plane on its own. The one requirement is that no two (page, head)s of a tensor share bytes, because the kernel writes them through raw pointers. The stride of a one-entry dim is never stepped and is not constrained.
  • Invariant: the C++ entry checks all of the above with AITER_CHECK before any launch, together with the device of every tensor. It refuses an mxfp4 or page-layout nvfp4 index cache with the row-layout K/V, and refuses index_scale without index_cache_dtype="nvfp4", the row-layout K/V and the index branch. The two Python fast paths that bypass that entry (the raw front, the deferred front) do not take a call with index_scale. test_rows_reject and test_reject exercise these refusals.
  • Caller contract (as in asm_layout): slot values are not bounds-checked against the pages, for both the K/V pages and the index rows.

Changed op tests

  • op_tests/test_fused_qknorm_nvfp4.py (the only changed op test; CI runs it as python3 <file>):
    • test_nvfp4_rows covers the new row-layout write: K/V rows and index rows, byte for byte against a torch reference of the rule applied to what the same call writes with "auto" caches, on strided fields of one pre-filled pool compared whole. Its odd-layout cases put every payload at an odd base with odd strides; its split-layout cases give every plane its own page and head stride; its prefill-shape cases would write page-layout bytes into the pool if the call were routed to [HIP] [Perf] fused_qknorm_idxrqknorm: early loads, hardware converts, DPP exchanges, xor-atomic V nibbles and a prefill variant #8472's variant or raw entry.
    • test_rows_reject covers the new host refusals (one case per C++ check and message), the wrapper's arch gate for each scale dtype and the deferred front's refusal of index_scale. The aliased views are pages expanded from one, and the four tokens write page 0 only, so those cases stay in bounds even if their check regressed.
    • test_reject (page layout, gfx950) gains one case: a call of the raw front's shape with index_scale must be refused.
    • stress_qkv takes its largest magnitude from the dtype (3e38 does not fit fp16, which the row sweep runs).
    • Not redundant: the file's existing cases cover only the page layout and run on gfx950 only, and op_tests/test_fused_qknorm_idxrqknorm.py has no NVFP4 case. Nothing else checks the row layout, its strides, or gfx1250. The file is extended rather than a second suite added.

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

lirui927 pushed a commit to lirui927/ATOM that referenced this pull request Nov 19, 2026
…qk-norm launch

With NVFP4 K/V the FP4 index key was the last thing still staged: the fused
qk-norm/rope/cache launch wrote it in BF16 to a token-major staging buffer and
index_fp4_store quantized it into the index planes in a launch of its own.
aiter's fused_qknorm_idxrqknorm now quantizes it there too
(index_cache_dtype="nvfp4" with index_scale, the row layout of the NVFP4 K/V
write), so that launch and the staging buffer are gone; the builder no longer
allocates an index stage for an NVFP4 K/V layer. FP8/BF16 K/V layers with an
FP4 index keep staging + index_fp4_store.

tests/test_minimax_m3_nvfp4_fused_paths.py: the fused index planes against
staging + index_fp4_store, bitwise (zero- and subnormal-scale blocks included),
and the K/V pool, q_out and index query unchanged.

Needs aiter's NVFP4 index rows in fused_qknorm_idxrqknorm (ROCm/aiter#8290).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@lirui927
lirui927 force-pushed the lirui/nvfp4-qknorm-rows branch 2 times, most recently from 700c167 to 1712b07 Compare November 23, 2026 08:31
@lirui927
lirui927 marked this pull request as ready for review November 23, 2026 08:31
@lirui927
lirui927 requested a review from a team November 23, 2026 08:31
@github-actions github-actions Bot added the HIP label Nov 23, 2026
@aiter-bot

aiter-bot commented Nov 23, 2026 •

Copy link
Copy Markdown

READY FOR HUMAN REVIEW

PR Self-Review

Revision 6a6934d31fc4 · mode shadow

Author self-review accepted. Readiness report:

Author answers

  • PASS — current-SHA answers are present and structurally specific
Show the author's self-review answers

Behavior and intent

- **Change:** with `kv_cache_dtype="nvfp4"` and byte `k_scale` / `v_scale` planes, the op writes K/V, and with `index_scale` the index key, as NVFP4 rows (e2m1 payload plus E4M3 scale plane) in the same launch as norm and RoPE. Before, this combination of arguments was refused.
- **Why the existing behavior is insufficient:** the op could only write these rows as BF16 (or as the #7824 page layout, a different format), so ATOM staged them in BF16 and quantized them in three more launches per sparse layer, and the page layout is gfx950-only.
- **Unchanged:** every other mode (auto, fp8 variants, mxfp4, the #7824 nvfp4 page layout with #8472's prefill variant and raw entry) behaves as before, with two exceptions that only add refusals: a page-layout call that passes `index_scale` is refused, also through the raw front and the deferred front. Their kernels differ from main only in the implicit-kernarg offset; the prefill kernels not at all.

Validation and unverified scope

Reviewed revision `6a6934d31fc43d5fa6ec5e367b3ee5d876830bc0`. Unless a bullet says otherwise, the items below were run at bc7cbbb1e, whose C++ is the head's (the three later commits touch the wrapper's arch gate, the op test's gate and refusal table and a comment). The CPU items used the image `rocm/fw-bringup:gfx1250-minimax-m3-20261112` without devices; the two op tests ran on one MI455X at the head.

- JIT build of `module_fused_qknorm_idxrqknorm` with `GPU_ARCHS=gfx1250` and with `GPU_ARCHS=gfx950` (`aiter.jit.core.build_module`, no GPU): both build.
- `python3 host_checks.py <arch>` on each of the two builds (local script; `AITER_AOT_REPLAY=1 GPU_ARCHS=<arch>`, CPU tensors that claim device 0, one child process per call that reaches C++): 145 calls on the gfx1250 build and 158 on the gfx950 build give the expected verdict, 0 bad. Four mutants (overlap check, byte-dtype check, raw-front gate term, deferred-front term) are each caught.
- Placement emulation of `test_nvfp4_rows` (same script, no GPU): 84 of 84 cases byte-exact; the alignment and the four stride mutants fail the cases built for them.
- `hostpy.py quant_host.py` (local script, host g++; a host model of the kernel source, not what the GPU executes): 809200 rows, 0 bytes differ from the test's reference and no byte outside the row changes; four sign-rule mutants differ; `quant_host.py sixth`: 0 of 64386 `amax` values differ.
- ISA comparison against main `4960107ee` (`hipcc --cuda-device-only -S` with the JIT's flags, `--offload-arch=gfx1250` and `gfx950`): 3 existing kernels identical, 680 differ only in the implicit-kernarg offset (`0xe8` → `0x120`), with the same resources; 16 new kernels.
- `python3 op_tests/test_fused_qknorm_nvfp4.py` in the image without a GPU: exits 1 at the file's first tensor (`No CUDA GPUs are available`), the statement main's file has too.
- At the head, on the host: `hostpy.py gate_host.py` (wrapper arch gate, C++ entry mocked, arch faked): row layout taken on gfx950 / gfx1250 / gfx1250-strict, refused on gfx942 / gfx1100; page layout taken on gfx950 only. `scratch_np/isa_host.sh <tree> gfx1250 <out> "--cuda-host-only -fsyntax-only"` on the kernel file: no errors.
- At the head: `python -m black --check` and `ruff check --no-cache` on the two changed Python files: pass.
- MI455X at this revision, one idle GPU: `python3 op_tests/test_fused_qknorm_nvfp4.py` exits 0 (84 row cases byte-exact, `row-layout reject: ok`; the page-layout part skips, "tested on gfx950 only") and `python3 op_tests/test_fused_qknorm_idxrqknorm.py` exits 0 (21 passed).
- MI455X, existing paths against main `4960107ee`, interleaved, 5 rounds (`"auto"` and fp8-asm K/V with an fp8 index at 64 / 3000 / 16384 tokens, 1 / 4 kv heads; 12 rows): this head − main is between −0.6% and +0.1% of the median, i.e. unchanged.
- Unverified: the page-layout part of `op_tests/test_fused_qknorm_nvfp4.py` on hardware (skips on gfx1250; MI35X CI); gfx1250-strict itself (not compiled for strict, not run on A0); the in-bounds kernel mutants; gfx950 hardware (compile, ISA and the host-check dry run only; MI35X CI is the first run of the row layout there, on #8472's hardware converts); the byte comparison against ATOM's Triton stores, last run on MI455X at 1712b07a8 with the previous arithmetic; an end-to-end ATOM run; clang-format.

PR-specific question

Which architecture, dtype and shape boundaries select the changed kernel path, and what invariant prevents unsupported inputs from reaching it?

- **Selection:** the new path runs only when `kv_cache_dtype="nvfp4"` and `k_scale` is a byte tensor (uint8 or float8_e4m3fn). The index key takes the new path only when, in addition, `index_cache_dtype="nvfp4"` and `index_scale` is given. fp32 `[1]` scales keep the #7824 page layout, including #8472's prefill variant and raw entry; every other mode runs the same device code as before, apart from the implicit-kernarg offset.
- **Architecture:** the Python wrapper refuses the row layout unless `base_gfx_arch(get_gfx_runtime())` is gfx950 or gfx1250 (`RuntimeError`), so gfx1250-strict is taken. The page-layout nvfp4 and mxfp4 caches stay refused off gfx950. The wrapper decides nothing else about the row layout.
- **Dtype:** qkv is bf16 or fp16 (the scale rule is checked for these two). Payloads must be uint8; each scale plane is uint8 or float8_e4m3fn.
- **Shape:** `block_size` is 128 and head_dim is 128. K/V payloads are `[pages, nkv, 128, 64]` and planes `[pages, nkv, 128, 8]`; the index payload is `[pages, 128, 64]` and its plane `[pages, 128, 8]`. Token rows are contiguous (the layout's definition). The mode is sparse insert only (qkv carries the index slots).
- **Strides:** the base and the page and head strides of every plane are free, at any alignment and each plane on its own. The one requirement is that no two (page, head)s of a tensor share bytes, because the kernel writes them through raw pointers. The stride of a one-entry dim is never stepped and is not constrained.
- **Invariant:** the C++ entry checks all of the above with `AITER_CHECK` before any launch, together with the device of every tensor. It refuses an mxfp4 or page-layout nvfp4 index cache with the row-layout K/V, and refuses `index_scale` without `index_cache_dtype="nvfp4"`, the row-layout K/V and the index branch. The two Python fast paths that bypass that entry (the raw front, the deferred front) do not take a call with `index_scale`. `test_rows_reject` and `test_reject` exercise these refusals.
- **Caller contract (as in `asm_layout`):** slot values are not bounds-checked against the pages, for both the K/V pages and the index rows.

Changed op tests

- `op_tests/test_fused_qknorm_nvfp4.py` (the only changed op test; CI runs it as `python3 <file>`):
  - `test_nvfp4_rows` covers the new row-layout write: K/V rows and index rows, byte for byte against a torch reference of the rule applied to what the same call writes with `"auto"` caches, on strided fields of one pre-filled pool compared whole. Its odd-layout cases put every payload at an odd base with odd strides; its split-layout cases give every plane its own page and head stride; its prefill-shape cases would write page-layout bytes into the pool if the call were routed to #8472's variant or raw entry.
  - `test_rows_reject` covers the new host refusals (one case per C++ check and message), the wrapper's arch gate for each scale dtype and the deferred front's refusal of `index_scale`. The aliased views are pages expanded from one, and the four tokens write page 0 only, so those cases stay in bounds even if their check regressed.
  - `test_reject` (page layout, gfx950) gains one case: a call of the raw front's shape with `index_scale` must be refused.
  - `stress_qkv` takes its largest magnitude from the dtype (`3e38` does not fit fp16, which the row sweep runs).
  - Not redundant: the file's existing cases cover only the page layout and run on gfx950 only, and `op_tests/test_fused_qknorm_idxrqknorm.py` has no NVFP4 case. Nothing else checks the row layout, its strides, or gfx1250. The file is extended rather than a second suite added.

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply
  • ADVISORY — Copilot did not review the current head SHA

Code and op-test hygiene

  • PASS — no committed artifact or conflict marker detected

Required CI

  • PASS — Black: passed
  • PASS — Ruff: passed
  • PASS — Repository dependency: passed
  • PASS — Aiter Test Gate: passed

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

@aiter-bot

Copy link
Copy Markdown

aiter-bot — queued behind 5 review(s); expect to start in about 1.8 h. The box reviews one PR at a time.

@aiter-bot

Copy link
Copy Markdown

⚠️ aiter-bot — this review could not complete: a problem with the review pipeline itself (bot logic / prompts), not with this PR.

the review was withheld by the card gate -- the card did not meet the bar, which is usually cleared by running it again; re-comment to retry

@zufayu — please take a look.

Re-comment @aiter-bot review once it is resolved.

@junhaha666

Copy link
Copy Markdown
Contributor

@aiter-bot review

@aiter-bot

Copy link
Copy Markdown

aiter-bot — queued behind 6 review(s); expect to start in about 2.2 h. The box reviews one PR at a time.

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

ROCm/aiter PR #8290 — [HIP] fused_qknorm_idxrqknorm: write NVFP4 K/V in the row layout (gfx950, gfx1250)

Review (advisory): ⚠️ NEEDS WORK
Validation (deterministic): NOT RUN — auto-validation disabled (REVIEW_AUTO_VALIDATE=0), so no head-matched report exists; the target op_tests/test_fused_qknorm_nvfp4.py is real and ran green in this PR's MI35X CI at head, but that is the PR's CI, not a head-matched report
Perf (advisory): NOT RUN — wrong arch on this box (8× gfx942); the row layout and its timing sweep are gfx950/gfx1250-only and skip on gfx942, so the harness has nothing to time here
Size: 689 lines / 5 files
Design (advisory): the row layout reuses the kv_cache_dtype="nvfp4" string, switched by the k_scale/v_scale dtype (byte planes vs fp32 [1] globals) — the PR's own open question
Human: recommend Pleaplusone review whether PR #8290's scale-dtype switch between the two NVFP4 layouts is the right call-site contract for the MiniMax-M3 pool and the FlyDSL paged decode before ATOM builds on it

2 finding(s) posted inline, on the lines they are about:

  • csrc/kernels/fused_qknorm_idxrqknorm.cu:1560 — The row-layout checks (csrc/kernels/fused_qknorm_idxrqknorm.cu:1560) accept any page/head strides and base…
  • aiter/ops/fused_qknorm_idxrqknorm.py:96 — The row layout activates only when a consumer passes byte k_scale/v_scale planes and the new index_scale…

Comment thread csrc/kernels/fused_qknorm_idxrqknorm.cu Outdated
"kv_cache_dtype='nvfp4' pages hold 128 tokens: block_size must be 128");
AITER_CHECK(v_scale.has_value() && v_scale->dtype() == k_scale->dtype(),
"nvfp4 row-layout k_scale and v_scale must both be byte planes");
const auto is_payload = [&](const aiter_tensor_t& t) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ [verified] The row-layout checks (csrc/kernels/fused_qknorm_idxrqknorm.cu:1560) accept any page/head strides and base pointer, but nvStoreRowData's uint16 store (csrc/kernels/fused_qknorm_idxrqknorm.cu:395) needs a 2-byte-aligned payload base — a contract-legal odd head stride (e.g. 8193 on a [pages, nkv, 128, 64] view) or a stride-0 expanded view passes every AITER_CHECK and then issues misaligned 16-bit global stores or silently aliases pages, where the page layout guards its own free stride with 4 B alignment and page-size lower-bound checks (csrc/kernels/fused_qknorm_idxrqknorm.cu:1615-1622). Author must add the same alignment and stride-lower-bound checks for the row layout, or state the alignment requirement in the contract.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 647dceb: the payload store no longer needs any alignment, and overlapping planes are refused on the host.

  • Alignment: nvStoreRowData now writes its 16-bit unit without an alignment assumption (__builtin_memcpy), which is well defined for any address. A row-layout plane may therefore start at any byte and take any page and head strides. Only the C++ pointer type assumed alignment: for the alignment-free form the compiler emits the same 16-bit store, and the device assembly of the whole file, all 705 kernels, is byte-identical with and without the change on gfx950 and gfx1250. test_nvfp4_rows gains 9 cases on a pool whose payloads (K, V and index) have odd bases and whose fields have odd page strides and, with more than one kv head, odd head strides, compared byte for byte against the same reference as the aligned cases. Every byte the kernel can address in them lies inside the pool.
  • Aliasing: every row-layout plane now passes one predicate before any launch (nv_rows_apart in fused_qknorm_idxrqknorm_impl): the K/V payloads and scale planes, and the index payload and scale plane. No two (page, head)s of a tensor may share bytes: either the pages step over a whole page's heads, or the heads over a whole head's pages. A stride-0 expanded view is refused, and so are strides that interleave pages and heads; the stride of a one-entry dim is never stepped and is not constrained. test_rows_reject has a case for each of the four places the predicate is applied. Their four tokens write page 0 only, so each case stays in bounds even if its check regressed.

The mxfp4 row path (storeMxfp4Row) is unchanged from main and outside this PR.

v_scale: Tensor | None = None,
asm_layout: bool = False,
skip_index_branch: bool = False,
index_scale: Tensor | None = None,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📝 [verified] The row layout activates only when a consumer passes byte k_scale/v_scale planes and the new index_scale kwarg (aiter/ops/fused_qknorm_idxrqknorm.py:96), and the PR links no ATOM consumer PR — until that lands, the new path is unreachable from every existing caller. Reviewer should ask where the ATOM change that passes the byte planes and index_scale will land, and track it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The consumer is ROCm/ATOM#3581 (draft). Its sparse layers call fused_qknorm_idxrqknorm with kv_cache_dtype="nvfp4" and the pool's E4M3 planes as k_scale / v_scale, and pass index_scale so that the index key is written in the same launch. ROCm/ATOM#3634 is stacked on it. The K/V rows are read by the FlyDSL pa_decode NVFP4 path of #8289.

ATOM calls aiter directly, without probing for the new arguments, so ROCm/ATOM#3581 stays a draft until this PR and #8289 are in. Until then only the op test reaches the new path. The description now links all three.

@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 text the second commit made stale. aiter/ops/fused_qknorm_idxrqknorm.py:130-132 still says the row-layout K/V takes an index cache that "is "auto" or fp8". csrc/kernels/fused_qknorm_idxrqknorm.cu:1340 says "with any index cache but an fp4 one". Both should say "auto, fp8, or the NVFP4 index rows (index_scale)". The squash text has the same problem: a2a1ed0's body says "an fp4 index with the row-layout K/V is refused", "One new kernel template flag" and that existing kernels have "identical ISA", and 1712b07 contradicts all three. Please write one squash body that describes the final state.
  2. Decide the row layout in one place. fused_qknorm_idxrqknorm.py:156-159 repeats the C++ index-cache rule (cu:1349-1360) inside the Python arch gate. The C++ checks already refuse row K/V with an mxfp4 index, with a page-layout nvfp4 index, or with an nvfp4 index that has no index_scale, and they do so before any launch. So the Python predicate only needs kv_cache_dtype == "nvfp4" and a byte k_scale. Today the duplicate gives a wrong reason on gfx1250: an unsupported index combination is refused with "require gfx950" (and ROWS_REJECT["rows fp4 index"] asserts that message). Keep the arch check and let C++ own the combination rules.
  3. Pass the K/V scale-plane strides in their own named arguments. Right now the K/V scale-plane page and head strides go through k_s_token / v_s_token (cu:1586-1589). In the row layout those arguments mean something else: k_s_token is read as a page stride and v_s_token as a head stride, at cu:727-731 and cu:925-929, and only a static_assert comment says so. Commit 2 already adds a trailing struct. Rename NvIndexRows to something like NvRows and give it the K/V scale_page / scale_head fields. This costs no extra launch argument, and the meaning of each argument is visible at the call site.

Correctness checks, no change needed:

  • The e4m3 scale (__fdiv_rn(amax, 6), RNE, saturating at 448) matches ATOM's _nvfp4_store_kernel (atom/model_ops/minimax_m3/index_fp4.py). So do inv = 1/scale (0 when the scale is 0), the e2m1 thresholds, and the sign rule (+0 for a zero-scale block).
  • Gates for the two layouts: fp32 [1] scales still pass the page-layout check (cu:1534) unchanged. Byte planes take the new branch. Every mixed combination I enumerated is refused on the host.
  • The default path adds only a few host bool tests and a 24 B kernarg, which is far below 1 µs per step.
  • On MI35X, CI shard 2 ran test_nvfp4_rows and the row refusals, and they pass.

On aiter-bot's open alignment thread: the mxfp4 row path (storeMxfp4Row, free strides, cu:1636-1655) uses the same uint16 stores without an alignment check, so I don't see this as blocking. Please reply on the thread anyway, either with a 2 B check or with the alignment stated as part of the caller contract, so it can be resolved.

On your open question: the dtype switch is fine with me. After item 2, it is decided in a single place.


Reviewed at 1712b07 (deep tier, 1 hunter pass by the orchestrator; no pass B because the verdict is request_changes). Already fixed: n/a (first review).

@junhaha666

Copy link
Copy Markdown
Contributor

@lirui927 Please take a look at the aiter-bot review. It has been over 12 hours without a follow-up commit or explanation. Please address the feedback or leave a brief update on how you plan to proceed.

@lirui927

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews; they are being addressed now. Plan for the next push:

The change needs a gfx1250 run before it is pushed, so it will follow once the MI455X box is free.

@lirui927 lirui927 changed the title [HIP] fused_qknorm_idxrqknorm: write NVFP4 K/V in the row layout (gfx950, gfx1250) [HIP] fused_qknorm_idxrqknorm: write NVFP4 K/V and the index key in the row layout (gfx950, gfx1250) Nov 26, 2026
@lirui927
lirui927 force-pushed the lirui/nvfp4-qknorm-rows branch from 1712b07 to 647dceb Compare November 26, 2026 07:21
@lirui927

Copy link
Copy Markdown
Contributor Author

Force-pushed for both reviews. The branch is now one commit, 647dceb, on current main (e6d2d31); the previous head was 1712b07. Its message describes the final state, so the pre-filled squash text no longer carries the bodies of the intermediate commits. The description is rewritten for the final state as well, and its Self-Review section is on the new head.

valarLip's action items:

  1. Stale text. fused_qknorm_idxrqknorm.py and fused_qknorm_idxrqknorm.cu now say that the row-layout K/V takes an "auto", fp8 or NVFP4 index-rows (index_scale) index cache. The one squash body is the commit message.
  2. One place for the row layout. The Python arch gate looks only at kv_cache_dtype == "nvfp4" and a byte k_scale; which index caches go with the row layout is the C++ checks' alone. On gfx1250 an unsupported index combination now gets the C++ message, and ROWS_REJECT["rows fp4 index"] expects that message on every arch. A second case covers an mxfp4 index.
  3. Named stride fields. NvIndexRows is now NvRows {scale_page, scale_head, index_scale, index_page, index_scale_page}, and k_s_token / v_s_token are no longer reused. There is no extra launch argument. The 680 existing kernels keep their instructions and resources; only the implicit-kernarg offset moves (0xe8 → 0x110 against main, on gfx950 and gfx1250).

The dtype switch stays, as valarLip answered the open question; the description now states it as the rule instead of asking.

aiter-bot's threads, answered inline:

On MI455X, op_tests/test_fused_qknorm_nvfp4.py passes on the new head (72 of 72 row-layout cases byte-exact, 9 of them on the odd layout; 14 refusal-table calls; 2 arch-gate checks), and op_tests/test_fused_qknorm_idxrqknorm.py passes 21 of 21. Without a GPU, the module builds for gfx950 and gfx1250, and a dry run of its host checks on both builds gives the verdict the refusal tables expect. gfx950 hardware is left to CI.

@lirui927
lirui927 requested a review from valarLip November 26, 2026 07:21
XiaobingSuper added a commit to XiaobingSuper/aiter that referenced this pull request Nov 26, 2026
Replace the older copy of ROCm#8290 (ca789f8, 3284696) with its current
head. The five files of ROCm#8290 take its head version as is; this branch's
own three files are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@lirui927
lirui927 marked this pull request as draft November 27, 2026 08:19
…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]>
@lirui927
lirui927 force-pushed the lirui/nvfp4-qknorm-rows branch from 647dceb to bc7cbbb Compare November 30, 2026 07:21
@lirui927

Copy link
Copy Markdown
Contributor Author

Re-ported onto current main after #8472 (and #8607). The branch is one commit, bc7cbbb, on 4960107ee; the previous head was 647dceb. The description is rewritten for the new head, and its Self-Review section names it.

What changed against the previous head:

On one idle MI455X at this head: op_tests/test_fused_qknorm_nvfp4.py exits 0 (84 of 84 row cases byte-exact, the refusal table passes) and op_tests/test_fused_qknorm_idxrqknorm.py passes 21 of 21. The build, the device-assembly comparison, the host-check dry runs and a host-CPU run of the row store are in the description. Still owed: an interleaved timing of the existing paths against main. gfx950 hardware is left to CI, as before.

@zufayu
zufayu removed the request for review from junhaha666 December 1, 2026 03:28
@zufayu
zufayu removed the request for review from valarLip December 1, 2026 03:28
lirui927 and others added 3 commits December 2, 2026 06:52
…50-strict too

The wrapper compared get_gfx_runtime() with gfx950 / gfx1250, so a gfx1250-strict device
(MI450 A0) was refused although its device pass builds the same row-layout kernels: the
kernel file's only arch switch is __gfx950__, and opus pairs __gfx1250_strict__ with
__gfx1250__ for the fp8 convert. The gate and the op test's gate go through base_gfx_arch;
test_rows_reject takes one row-layout call with the arch faked to gfx1250-strict.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…rule as the kernel applies it

The comment said the sign is that of x * (1 / scale). The kernel reads it from the
bits of x: set for x < 0 with a non-zero scale, so -0, NaN and every element of a
zero-scale block code +0. Comment only.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…rlapping rows

Four of the child-process refusal cases repeated a C++ check another case already
reaches with the same message: a missing v_scale (the plane check, next to the fp32
plane), the mxfp4 index cache (next to the page-layout nvfp4 one) and two of the four
aliased-plane cases (one K/V payload and one index scale plane stay, both through
nv_plane). 11 calls instead of 15; the file is shorter than before this branch's
gfx1250-strict row.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@lirui927

lirui927 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

review-pr scan (ROCm/aiter main ea578b4, .claude/skills/review-pr), run locally on the posted head bc7cbbb with REVIEW_AUTO_VALIDATE=0; no GPU.

Card

ROCm/aiter PR #8290 — [HIP] fused_qknorm_idxrqknorm: write NVFP4 K/V and the index key in the row layout (gfx950, gfx1250)

Lets the fused qk-norm op write K, V and optionally the index key directly as NVFP4 rows (e2m1 payload plus E4M3 scale plane, free page and head strides) on gfx950 and gfx1250, selected by byte-typed scale planes and a new index_scale argument.

Review (advisory): ⚠️ NEEDS WORK
Validation (deterministic): NOT RUN — REVIEW_AUTO_VALIDATE=0 (target op_tests/test_fused_qknorm_nvfp4.py); an environment gap, not a PR defect
Perf (advisory): NOT RUN — no GPU used for this review; the body reports existing paths unchanged against main on MI455X (−0.6% .. +0.1%, 5 interleaved rounds, 12 rows)
Size: 805 lines / 5 files
Design (advisory): one dtype string "nvfp4" serves two layouts in fused_qknorm_idxrqknorm, told apart by the dtype of k_scale (fp32 [1] globals = #7824 page layout, byte planes = row layout), with index_scale as a new trailing argument, instead of a new kv_cache_dtype value
Human: recommend valarLip review whether fused_qknorm_idxrqknorm should select the NVFP4 row layout from the k_scale dtype or from an explicit kv_cache_dtype value

⚠️ [inferred] aiter/ops/fused_qknorm_idxrqknorm.py:494 compares get_gfx_runtime() with ("gfx950", "gfx1250"), and that helper returns gfx1250-strict on MI450 A0, so the row layout raises there although the kernel source has no gfx1250-only switch; op_tests/test_fused_qknorm_nvfp4.py:1001 skips the row tests there by the same comparison. A consumer on A0 would get a RuntimeError for a call the kernel can serve. Author must fold the arch with base_gfx_arch (as the rest of aiter does for strict) or state why strict is refused.
⚠️ [inferred] aiter/ops/fused_qknorm_idxrqknorm.py:494 enables the row layout on gfx950, where it has only been compiled and dry-run: there the rows depend on the hardware converts giving the integer routines' bytes. Would a byte mismatch show up only in MI35X CI? Author must point to a green MI35X run of op_tests/test_fused_qknorm_nvfp4.py at the head before merge.

Scan log

  files=5  types=12  rules=26/68
    [modified-kernel     ] A1 A4 B2 D1 D8 P6
    [new-routing-value   ] A4 B4 C4
    [dispatch-change     ] A4 B1 B3 B4 A3
    [ops-wrapper         ] B6 C5
    [api-signature       ] B6 E1 E5
    [fp8-quant           ] C1 C2 D1
    [perf                ] P1 P2 P3 P5 P6
    [invariant-removed   ] D4 B7
    [layout-contiguity   ] D8 B7
    [test-reference-dtype] HK10
    [defaulted-param     ] E2
    [scattered-diff      ] HK2
APPLIES: the diff still applies to ea578b418b43b25fc4937243c8dfd9bd6013f8b7
805 lines / 5 files
every new test file lands where a CI job will run it
IMPORTS RESOLVED: every first-party import the diff adds exists in the merge target or in this diff
SIBLINGS: no variant of a changed function still carries a changed line
KERNEL TESTS: no new kernel file without a collectible test
GUARDS DELETED -- 0 returned unchanged, 3 returned in changed form, 0 gone.
ANSWERS COMPLETE: 6/6
DIAGNOSTIC COMPLETE: 6/6
CORE FILES: none in this diff, declared
LEDGER COMPLETE: 26/26 rules adjudicated with evidence
CARD NAILED DOWN: 2 findings, each traced to an adjudication and anchored in a changed file; every FIRE accounted for
REFUTATIONS COMPLETE: 5 attempted, 2 survived, 3 killed before reaching the card
INDEPENDENTLY REFUTED: 3 judged, 2 defended, 1 killed by a reader who had not seen the reasoning
DESIGN ROUTED: size 805 lines / 5 files

The fired rules on the new head (<SHA after push>)

Finding on the card On the new head
Row layout refused on gfx1250-strict although the kernel has no gfx1250-only switch Fixed: the wrapper and the op test use base_gfx_arch(get_gfx_runtime()); test_rows_reject expects a strict call to be taken. Checked on the host with mocks. Not compiled for strict, not run on A0.
Row layout enabled on gfx950 without a hardware run Open: needs a green MI35X run of op_tests/test_fused_qknorm_nvfp4.py at the new head.

A third candidate (no timing entry point in the op test) was killed by the independent reader: test_nvfp4_rows times every row case with run_perftest.

@lirui927
lirui927 marked this pull request as ready for review December 2, 2026 12:05
@zhuyuhua-v

Copy link
Copy Markdown
Contributor

The review-pr scan (aiter skill, from main) at 6a6934d reports ✅ LGTM (no findings needing a change): 2 notes; all 8 Step 8 gates pass, and an independent reader kept both notes.

  • valarLip's three 11-24 requests are met at this head: the Python wrapper now checks only the arch, and the C++ host checks decide which index caches can use the row layout. The K/V scale-plane strides have their own named fields (NvRows) instead of reusing k_s_token / v_s_token. The stale comments are fixed, and the four commit messages no longer contradict each other. His CHANGES_REQUESTED still stands until he re-reviews. The author's MI455X timing of existing paths against main is −0.6% to +0.1%.
  • Note (evidence): the row layout is switched on for gfx950 but has never run on gfx950 hardware at the current code; the last MI35X run of the new row tests was at 647dceb on 11-26. The MI35X CI job that started at 12:05, when the PR left draft, is the first; it's worth confirming it is green.
  • Note (description): the body still names aiter#8289 as the reader of these rows, but [FlyDSL] gfx1250 pa_decode: NVFP4 K/V on a wave-owned scaled-WMMA tile #8289 was closed unmerged on 11-26 and its work is now in draft aiter#8365. The reader PR this is meant for should be confirmed.
Card

ROCm/aiter PR #8290 — [HIP] fused_qknorm_idxrqknorm: write NVFP4 K/V and the index key in the row layout (gfx950, gfx1250)

Lets the fused qk-norm/RoPE insert op quantize K, V and optionally the index key straight into NVFP4 rows (an e2m1 payload plus a per-16-element E4M3 scale plane, with free page and head strides) on gfx950 and gfx1250, selected by passing byte-typed k_scale/v_scale planes and a new index_scale argument.

Review (advisory): ✅ NO FINDINGS — two notes for the reviewer; valarLip's three 11-24 change requests are met at 6a6934d
Validation (deterministic): NOT RUN — auto-validation disabled (REVIEW_AUTO_VALIDATE=0) and no GPU used in this scan; the target op_tests/test_fused_qknorm_nvfp4.py exists and the changed kernel path runs at serving time, so this is an environment gap, not a PR defect. The MI35X Aiter Test run at this head was still in progress at review time.
Perf (advisory): NOT RUN — no GPU used in this scan (shared pool, no lease); the body reports the author's interleaved MI455X timing of existing paths against main (5 rounds, 12 rows, −0.6% to +0.1%), not reproduced here
Size: 794 lines / 5 files
Design (advisory): kv_cache_dtype="nvfp4" now names two layouts in fused_qknorm_idxrqknorm, told apart by k_scale's dtype (fp32 [1] globals = #7824 page layout, byte planes = row layout), with index_scale as a new trailing argument and the row strides in a trailing NvRows kernel struct
Human: recommend valarLip review whether PR #8290 at 6a6934d (the k_scale-dtype layout switch, NvRows named scale strides, C++-owned index-cache rules) clears the 11-24 CHANGES_REQUESTED review

📝 [verified] aiter/ops/fused_qknorm_idxrqknorm.py:497 turns the row layout on for gfx950, but the author's hardware runs at the current code are gfx1250 only. On gfx950 the row path (using #8472's hardware converts) first runs in the MI35X Aiter Test job that started at 12:05, when the PR left draft. The last MI35X row run was at 647dceb on 11-26, before the re-port onto #8472. The residual risk is small [inferred]: the page layout's index rows already run the same nvQuantLane at gs = 1 on gfx950. Reviewer should ask for that run's op_tests/test_fused_qknorm_nvfp4.py row cases to pass at 6a6934d before clearing.
📝 [verified] The body, and the 11-26 reply on aiter/ops/fused_qknorm_idxrqknorm.py:355, name aiter#8289 as the FlyDSL reader of these rows and as a dependency of ATOM#3581. But #8289 was closed unmerged on 2026-11-26, and its NVFP4 tile now rides in draft aiter#8365. The consumer chain the description gives (ATOM#3581 writes, #8289 reads) no longer exists as written. Reviewer should ask which reader PR the row layout is being merged for, and that the body point at it.

Step 8 gates
answers      ANSWERS COMPLETE: 6/6
diagnostic   DIAGNOSTIC COMPLETE: 6/6
corefiles    CORE FILES: none in this diff, declared
ledger       LEDGER COMPLETE: 26/26 rules adjudicated with evidence
card         CARD NAILED DOWN: 2 findings, each traced to an adjudication and anchored in a changed file; every FIRE accounted for
refutations  REFUTATIONS COMPLETE: 8 attempted, 2 survived, 6 killed before reaching the card
independent  INDEPENDENTLY REFUTED: 2 judged, 2 defended, 0 killed by a reader who had not seen the reasoning
design       DESIGN ROUTED: size 794 lines / 5 files

@zufayu
zufayu self-requested a review December 3, 2026 01:16
@lirui927

lirui927 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Status at 6a6934d3 of the 11-24 review (valarLip) and of the review-pr scan of 2026-12-02 at this head (zhuyuhua-v). Line numbers are at this head; .py = aiter/ops/fused_qknorm_idxrqknorm.py, .cu = csrc/kernels/fused_qknorm_idxrqknorm.cu.

Review of 11-24:

  1. Stale text, one squash body: fixed. The wrapper comment lists "auto", fp8 or the NVFP4 index rows (index_scale) (.py:391-393), and the branch is one commit (bc7cbbb1) describing the final state plus three small ones.
  2. Decide the row layout in one place: fixed. The Python predicate is kv_cache_dtype == "nvfp4" plus a byte k_scale and is used only for the arch check (.py:490-500); which index caches go with the row layout is decided by the C++ host checks.
  3. Named scale-plane strides: fixed. struct NvRows carries k_scale_page / k_scale_head / v_scale_page / v_scale_head and the index-row fields (.cu:731-743).
  4. The two inline threads (16-bit store alignment, consumer PR) were answered on 11-26. The store goes through memcpy and claims no alignment (nvStoreRowData, .cu:586-589), and the host check refuses a tensor whose (page, head)s overlap.

New since the 11-26 reply (three small commits on bc7cbbb1, no C++ change):

  1. gfx1250-strict is taken. The wrapper's arch gate and the op test now use base_gfx_arch(get_gfx_runtime()) (.py:497), where the gate used to compare the runtime name with ("gfx950", "gfx1250") and so refused the row layout on MI450 A0. The kernel file has only __gfx950__ switches, so strict compiles the same row kernels. test_rows_reject has a row that expects a strict call to be taken.
  2. The wrapper comment states the sign rule as the kernel applies it: read from the bits of x, set for x < 0 with a non-zero scale.
  3. test_rows_reject drops four child-process cases that repeated another case's C++ check and message; 11 calls instead of 15.

What ran at this head:

  • One idle MI455X (gfx1250), rocm/fw-bringup:gfx1250-minimax-m3-20261112: python3 op_tests/test_fused_qknorm_nvfp4.py exits 0, 84 of 84 row-layout cases byte-exact (kv err, idx err, q err all 0), row-layout reject: ok; the page-layout part skips ("tested on gfx950 only"). python3 op_tests/test_fused_qknorm_idxrqknorm.py exits 0, every row passed.
  • MI35X CI (gfx950), scan note 1: op_tests/test_fused_qknorm_nvfp4.py passed in Standard Tests shard 2 at this head, with the 84 test_nvfp4_rows cases, row-layout reject: ok and the page-layout part. This is the first gfx950 hardware run of the row layout on [HIP] [Perf] fused_qknorm_idxrqknorm: early loads, hardware converts, DPP exchanges, xor-atomic V nibbles and a prefill variant #8472's converts. op_tests/test_fused_qknorm_idxrqknorm.py passed in shard 4. Seven of the eight MI35X shards are green; shard 5 is still in progress as I write.

Scan note 2 (reader PR): correct. The description still names #8289 as the reader of these rows; #8289 was closed unmerged, and the reader is now draft #8365. The description will be corrected separately; ROCm/ATOM#3581 stays the writer-side consumer.

Still open:

  • The page-layout part of the test and everything on gfx950 rest on MI35X CI alone; no gfx950 hardware run of ours.
  • gfx1250-strict is taken on the strength of the source only: not compiled for strict and not run on A0. The arch gate was checked on the host with the C++ entry mocked.
  • No end-to-end ATOM run on this branch.

@lirui927
lirui927 requested a review from valarLip December 3, 2026 07:40

@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 (2)

  1. Nit: the kernel's sign-rule comment still has the old wording. csrc/kernels/fused_qknorm_idxrqknorm.cu:605-609 says "The sign is that of the IEEE product: none for a NaN, a zero, or any element of a zero-scale group". That is not what IEEE gives: -0 * (1/s) is -0, and a negative x times the 1/0 of a zero-scale group is -inf, so the product carries a sign in both cases. The kernel (cu:631-638) sets the sign only for x < 0 with a non-zero scale byte. 43874be fixed this sentence in the wrapper (aiter/ops/fused_qknorm_idxrqknorm.py:388-390); please use the same wording in the kernel.
  2. Nit: squash text and description. aiter squashes with the commit messages, so all four land on main.
    • 34d77c3, 43874be and 6a6934d describe in-branch history ("was refused although", "The comment said", "11 calls instead of 15; the file is shorter than before this branch's ..."), which means nothing on main.
    • bc7cbbb never mentions gfx1250-strict.
    • Please squash to one message: bc7cbbb's body plus one sentence on gfx1250-strict.
    • Also update the description's "Not covered" line ("gfx950 has never been run on hardware for the row layout"). MI35X CI at this head has run it (see below).

Prior asks (11-24)

  • Stale index-cache text and squash body: fixed (py:392-394, cu:2311-2313, cu:2330-2333). bc7cbbb is a single final-state body.
  • Python decides only the arch: fixed (py:491-500). The "rows fp4 index" case now asserts the C++ message.
  • Named scale-plane strides: fixed. NvRows is at cu:731-743, and its strides are read at cu:948-949 and cu:1159-1160. The split-stride cases pass on MI35X.
  • Both aiter-bot threads have author answers but are still unresolved. Please resolve them.

Root cause vs patch

The producer op now writes the consumer's NVFP4 row layout in the launch it already runs, so ATOM no longer stages BF16 and re-quantizes afterwards. It reuses #8472's nvQuantLane / storeNvfp4IndexRow through a kRows flag rather than copying them. The scale dtype selects the layout, and every combination rule is a C++ host check. Verdict: root fix.

Checked, no change needed

  • MI35X CI at 6a6934d (run 46877700011, shard 2/8) ran op_tests/test_fused_qknorm_nvfp4.py: 84 test_nvfp4_rows cases, all byte-exact, and both row-layout and page-layout refusals reported ok. Shard 4/8 ran the idxrqknorm and prefill tests; both passed. There is no gfx1250 CI lane. gfx1250-strict is enabled from source only, as the description says.
  • No other front can take a row-layout call: the raw front needs fp32 [1] scales, and the deferred front needs K and V to be one tensor. The nv_plane overlap check is sound.
  • Existing kernels change only a kernarg offset, and the host cost is a few bool tests.
  • The PR merges cleanly with current main (d21d704). #8011 edits the same files. Whichever PR lands second must route or refuse the row layout on the qkv_partials path.

Reviewed at 6a6934d (round 2, pass A; whole PR diff, since the branch was re-ported onto #8472).

This branch has not been deployed

No deployments
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.

5 participants