Repository navigation
[FlyDSL] [Perf] fused_qknorm_idxrqknorm: FlyDSL NVFP4 insert for MiniMax-M3 TP4 prefill - #8576
ganyi1996ppo wants to merge 1 commit into
Conversation
…Max-M3 TP4 prefill Same bytes as the HIP kernel, 1.6-3.0x faster on device and 3.5x less host time per call for calls of at least 1024 tokens (nvfp4 K/V/index-K, 16/1/1 heads, rotary 64, gfx950). Ported from amd-kernels. AITER_FUSED_QKNORM_NVFP4_FLYDSL=0 keeps the HIP kernel. Co-Authored-By: Claude <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
WAITING ON AUTHORPR Self-ReviewRevision Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision. Questions to answer in the PR description
Edit the Missing or invalid answers
Shadow mode reports what would block but keeps the status successful. Set |
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved critical fast-path defects can corrupt caches and outputs, permit unsafe GPU memory access, and incorrectly reuse device-bound launch handles across GPUs.
6 open issues
Fast path skips pointer residency, extent, and alignment checks · New Output shape validation is missing, allowing overlapping writes · New Compiled launcher cache is not partitioned by device · New Int32 buffer offsets overflow on large cache spans · New Consecutive-slot assumption can corrupt cache writes · New NaN clamp differs from HIP FP8 conversion · New
What changed in this PR
Adds a FlyDSL fast path for fused Q/K normalization, RoPE, and NVFP4 cache insertion, targeting MiniMax-M3 TP4 prefill on gfx950.
Changes:
- Adds main and prefill kernel variants with token-dependent selection.
- Adds public-op dispatch for eligible calls of at least 1024 tokens, with an environment-variable opt-out.
- Adds HIP comparison tests, optional timing, and large-cache address coverage.
| File | Description |
|---|---|
op_tests/test_fused_qknorm_nvfp4_flydsl.py |
Adds correctness comparisons, dispatch checks, timing, and large-cache cases. |
aiter/ops/fused_qknorm_idxrqknorm.py |
Adds conditional FlyDSL dispatch before the HIP fallback. |
aiter/ops/flydsl/kernels/fused_qknorm_idxr_nvfp4_insert.py |
Implements the main kernel, eligibility checks, and launch selection. |
aiter/ops/flydsl/kernels/fused_qknorm_idxr_nvfp4_insert_prefill.py |
Adds a prefill variant designed to reduce memory instructions. |
Review effort: Balanced
| if q_out is None or q_out.dtype != torch.bfloat16 or not q_out.is_contiguous(): | ||
| return "q_out" |
| index_q_out is None | ||
| or index_q_out.dtype != torch.float8_e4m3fn | ||
| or index_q_out.stride(1) != 1 | ||
| or index_q_out.stride(0) % 4 |
| torch.cuda.current_stream(qkv.device), | ||
| ) |
| Vec( | ||
| buffer_ops.buffer_load( | ||
| qkv, | ||
| toks[t] * (ROW // 2) + col * (D // 2) + hl * 2, |
| and index_cache_dtype == "nvfp4" | ||
| and not skip_index_branch | ||
| and not asm_layout | ||
| and qkv.shape[0] >= _NVFP4_INSERT_FLYDSL_MIN_TOKENS |
| iq_col = (s - (nq + nkv + 1)) * D | ||
| for t in range_constexpr(tp): | ||
| f = [ | ||
| _minnum(_maxnum(x, _f32(F448N)), _f32(F448)) |
|
Merged into #8472: the same kernel family now has a constexpr-selected HIP prefill variant (byte-identical to this FlyDSL kernel in its contract; within 3% of it at 384-2048 and 8192-32768 tokens, faster at 512, 768, 1024, 16384, 32768; host time 8.7 us vs 8.3 us through a raw-pointer entry). Closing in favour of #8472. |
…ort reorder from the QKV front branch The QKV front kernels import only the device helpers of the FlyDSL NVFP4 insert, not the insert kernels. Keep those helpers (qkv_front_common.py) and remove the rest: the decode and prefill FlyDSL insert kernels, their test, and the FlyDSL route inside fused_qknorm_idxrqknorm (the NVFP4 insert stays the HIP kernel of ROCm#8472). Only the deferred QKV front hook remains in fused_qknorm_idxrqknorm.py. bench_models.py goes back to main (an import reorder only). Co-Authored-By: Claude <[email protected]>
…ch (1-64 rows) + split-K (65-128 rows) GEMM / norm / RoPE / NVFP4 insert (#8607) * [FlyDSL] [Perf] fused_qknorm_idxrqknorm: FlyDSL NVFP4 insert for MiniMax-M3 TP4 prefill Same bytes as the HIP kernel, 1.6-3.0x faster on device and 3.5x less host time per call for calls of at least 1024 tokens (nvfp4 K/V/index-K, 16/1/1 heads, rotary 64, gfx950). Ported from amd-kernels. AITER_FUSED_QKNORM_NVFP4_FLYDSL=0 keeps the HIP kernel. Co-Authored-By: Claude <[email protected]> * [FlyDSL] mxfp8 activation quantizer for the decode GEMM tiers: (16, 16) code layout + shuffled E8M0 scales gfx950 FlyDSL quantizer: bf16 [M, K] -> fp8 e4m3 codes in the weight's (16, 16) preshuffled layout (rows padded to the row tile, padding rows zero) plus E8M0 scales in AITER's shuffled layout, or row-major codes. One launch, a workgroup per 32-row x 256-K tile. Prerequisite of the one-launch QKV front chain (next PR): that kernel reads exactly these codes. Technique source: amd-kernels gemm/mxfp8_common/mxfp8_quant.py and the scale/convert helpers of mxfp8_gemm_decode.py (ported, no import). op_tests/test_flydsl_mxfp8_quant_shuffled.py compares codes and scales byte for byte against a torch reference of the Triton MXFP8 rule (60 shape/magnitude cases). Co-Authored-By: Claude <[email protected]> * [FlyDSL] [Perf] one-launch MiniMax-M3.1 QKV front chain for 17-64 decode rows (TP4) QKV MXFP8 GEMM (deferred split-3) + q/k/index-q/index-k RMSNorm + partial RoPE + NVFP4 K/V/index-K page inserts in ONE launch: 240 five-wave workgroups, a head's 12 workgroups publish sc1 slabs and bump an epoch counter, the last roles reduce and insert eight rows each. Replaces the three launches of the public decode path (quantize, GEMM, fused_qknorm_idxrqknorm) for 17-64 rows. Host wrapper qkv_chain_cooperative_insert keeps counters per device/stream or HIP capture id. Technique source: amd-kernels attention/mxfp8_qkv_norm_rope_nvfp4_insert_n2560_k6144_rot64_p128 (kernel_cooperative.py, host.py) and the decode GEMM primitives of mxfp8_gemm_decode.py (ported, no import). Helpers come from the FlyDSL NVFP4 insert of #8576, activation codes from the quantizer of the previous commit. Co-Authored-By: Claude <[email protected]> * [FlyDSL] [Perf] split-3 decode MXFP8 GEMM (fp32 K-third slabs) for 65-128 rows, MiniMax-M3.1 TP4 QKV One-wave workgroups over RT 16-row x NI 16-column tiles of one K third, both operands in the (16, 16) preshuffled layout, W / A blocks issued ahead of the scaled MFMAs, odd-RT A scale re-pairing; writes the fp32 slabs a fused reduce-insert reads. Bit-identical to the amd-kernels decode GEMM at 65..128 rows; graph replay 8.1-9.1 us vs 9.7-11.7 us for the public GEMM. Technique source: amd-kernels gemm/mxfp8_common/mxfp8_gemm_decode.py (ported, no import). Co-Authored-By: Claude <[email protected]> * [FlyDSL] [Perf] split-3 reduce + norm / RoPE + NVFP4 insert, QKV chain for 65-128 decode rows (TP4) Sums the three fp32 K-third slabs, rounds to bf16 and runs the norm / RoPE / NVFP4 K, V, index-K insert arithmetic of fused_qknorm_idxr_nvfp4_insert in one launch (V in workgroups of its own through LDS); qkv_chain_insert serves 17-128 rows (one launch to 64 rows, GEMM + reduce-insert above). Bit-identical to the amd-kernels chain at 65..128 rows; graph replay 13.7-16.3 us vs 16.9-20.0 us for quantize + public GEMM + fused_qknorm_idxrqknorm. Technique source: amd-kernels attention/mxfp8_qkv_norm_rope_nvfp4_insert_n2560_k6144_rot64_p128 (reduce_insert.py, packing.py). Co-Authored-By: Claude <[email protected]> * [FlyDSL] mxfp8 quantizer review fixes: bf16/contiguous input check, per-device compiled handle + x.device stream, whole-tile rule for rpw>2, op test entry point Co-Authored-By: Claude <[email protected]> * [FlyDSL] qkv chain review fixes: operand validation (devices, dtypes, extents), per-device handle + stream, stricter page checks, op test entry point Co-Authored-By: Claude <[email protected]> * [FlyDSL] [Perf] QKV front tier chosen by a tuned table: gemm_a8w8_qkv_front + deferred fused epilogue in fused_qknorm_idxrqknorm The MXFP8 QKV projection of a decode layer is one interface (gemm_a8w8_qkv_front); a tuned row (a8w8_mxfp8_qkv_front_tuned_gemm.csv, kernelId qkvfront) picks plain / one-launch chain (17-64 rows) / split-3 chain (65-128 rows). A fused tier only quantizes in the projection and runs GEMM + norm + RoPE + NVFP4 insert inside the fused_qknorm_idxrqknorm call on the same tensor. Whole-front tuner: op_tests/tune_gemm_a8w8_qkv_front.py. Co-Authored-By: Claude <[email protected]> * [FlyDSL] qkv chain: row-major activation mode (a_row) for the one-launch tier and the front, to measure (16, 16) vs plain A Co-Authored-By: Claude <[email protected]> * [FlyDSL] qkv front on row-major A only: drop the (16, 16) activation layout, the shuffled quantizer and code_rows; retune the one-launch lookahead; regenerate the tier rows Co-Authored-By: Claude <[email protected]> * [FlyDSL] qkv front: split-K factor ks as a tuning config (1/2/3/4/6) through the kernel, the reduce-insert and the tuned rows; tuner enumerates ks x tile; #8610's row-major split kernel merged in Co-Authored-By: Claude <[email protected]> * drop the shuffled-A split-K op test (replaced by test_gemm_a8w8_qkv_front.py row-major split cases) Co-Authored-By: Claude <[email protected]> * [FlyDSL] QKV front as one op: static tier by row count (17-64 one-launch, 65-128 split-K, ks a kernel parameter), no tuned table / tuner / config family; hand-off failure modes documented and checked; quantizer guard-byte and fused-vs-unfused bitwise tests Co-Authored-By: Claude <[email protected]> * [FlyDSL] one-launch QKV front serves 1-16 rows too (RT=1 tile; no structural limit found), window 1-128 Co-Authored-By: Claude <[email protected]> * [FlyDSL] drop the closed #8576 FlyDSL insert and an unrelated import reorder from the QKV front branch The QKV front kernels import only the device helpers of the FlyDSL NVFP4 insert, not the insert kernels. Keep those helpers (qkv_front_common.py) and remove the rest: the decode and prefill FlyDSL insert kernels, their test, and the FlyDSL route inside fused_qknorm_idxrqknorm (the NVFP4 insert stays the HIP kernel of #8472). Only the deferred QKV front hook remains in fused_qknorm_idxrqknorm.py. bench_models.py goes back to main (an import reorder only). Co-Authored-By: Claude <[email protected]> * [FlyDSL] QKV front review fixes: operand validation as exceptions, op-boundary shape / head checks, device-keyed split launches - asserts that validated caller operands (shapes, dtypes, devices, strides, ranges, ks) are ValueError now, so python -O keeps them; one _validate shared by the one-launch and split tiers (the split tier had fewer checks) - the head set (16 / 1 / 1, rotary 64) and N 2560 / K 6144 are checked with a message naming the supported shape, at gemm_a8w8_qkv_front and at both chain entries; ks outside (1, 2, 3, 4, 6) is rejected - the split GEMM and the reduce-insert launches key the compiled handle by device and run under the operand's device and stream (a handle reused on another device does not launch) Co-Authored-By: Claude <[email protected]> * [FlyDSL] [Build] AOT prebuild of the QKV front kernels aiter.aot.flydsl.qkv_front replays qkv_chain_insert on FakeTensors over 1-128 rows, bisected where the tile or tier switches, so the one-launch kernels (16 / 32 / 48 / 64 rows), the split-K GEMM tiles and the reduce-insert are in the prebuilt cache; registered in AOT_MODULES and the AOT README. Co-Authored-By: Claude <[email protected]> * [FlyDSL] QKV front: drop stale skips (not-in-this-tree) and tuned-table wording from tests and comments Co-Authored-By: Claude <[email protected]> * [FlyDSL] [Build] dry_run: the Event stand-in accepts record / synchronize The QKV front's counter pool records and waits on a cuda Event on first use; under the AOT dry run the stand-in had neither method, so the replay of qkv_chain_insert failed with AttributeError. Co-Authored-By: Claude <[email protected]> * [FlyDSL] QKV front: document the slot_mapping run contract (consecutive slots per octet, no gapped or split-octet runs) Co-Authored-By: Claude <[email protected]> * [FlyDSL] qkv front _validate: require f32 k/v scales and non-overlapping uint8 KV pages Harden the operand checks the raw-pointer launch depends on (Copilot review, PR #8607): - k_scale / v_scale: the kernels read the scalar via _gptr(..., T.f32, 4), so require float32 with numel() >= 1; a 1-element bf16 scalar previously passed but would be a 2-byte read reinterpreted as f32. - kv_cache / index_cache: require uint8 and stride(0) >= full page footprint (2*128*72 / 128*72). The kernels index pages by slot>>7, so an expanded or overlapping page layout (e.g. stride(0)==0) would let distinct slots alias the same page. dtype uint8 is now also enforced. Co-Authored-By: Claude Opus 4.8 <[email protected]> * op_tests: consolidate gemm_a8w8_qkv_front tests into test_flydsl_qkv_chain.py (remove standalone file) All chain and gemm_a8w8_qkv_front correctness cases are retained verbatim in the single file; no coverage change (7 test functions / 257 parametrized cases before and after). Shared `needs_gfx950` mark and `NB` constant deduplicated; the standalone file is removed. Co-Authored-By: Claude Opus 4.8 <[email protected]> --------- Co-authored-by: Claude <[email protected]>


Summary
fused_qknorm_idxrqknormwithkv_cache_dtype="nvfp4"+index_cache_dtype="nvfp4"(the MiniMax-M3 prefill / non-mono decode insert) now runs a FlyDSL kernel on gfx950 for the TP4 head set (16 q / 1 kv / 1 index heads, head_dim 128, rotary 64). Same bytes as the HIP kernel, 2.0-3.0x faster on device (1024-16384 tokens) and 3.5x less host time per call. It is used for calls of at least 1024 tokens (prefill chunks); smaller calls and every other call (other head sets, dtypes, layouts,skip_index_branch) stay on the HIP kernel.Motivation
Per layer the NVFP4 insert is a visible part of a prefill step: the HIP kernel takes 58 us at 8192 tokens and 106 us at 16384 (60 layers: 3.5 ms / 6.4 ms per forward) and scales with n at ~6.5 ns/token. A FlyDSL kernel that does the same norm / RoPE / NVFP4 quantization in one launch with fewer vector memory instructions per wave takes 21 / 35 us.
Changes
aiter/ops/flydsl/kernels/fused_qknorm_idxr_nvfp4_insert.py: the main kernel (waves of2 * tptokens: q heads | k heads | index-k | index-q heads | v heads) plus host wrapper, blocker (nvfp4_insert_blocker) and token-pair selection. Used below 1537 tokens.aiter/ops/flydsl/kernels/fused_qknorm_idxr_nvfp4_insert_prefill.py: the prefill variant (scalar position / slot / scale loads, cos/sin shared by two token pairs, V waves load a run's rows only when they write it). Used from 1537 tokens (eight pairs per wave in 24576..32768).aiter/ops/fused_qknorm_idxrqknorm.py: dispatch in front of the HIP call, gated onnvfp4+nvfp4, notskip_index_branch, notasm_layout, plus the blocker's checks (head set,block_size == 128, bf16qkv/ weights / cos-sin, int64 positions and slots, bf16q_out, e4m3index_q_out, one shared[pages, 2 * 9216]uint8 page buffer, 4-byte-aligned page stride, fp32[1]scales).AITER_FUSED_QKNORM_NVFP4_FLYDSL=0keeps the HIP kernel.op_tests/test_fused_qknorm_nvfp4_flydsl.py: FlyDSL vs HIP, byte for byte over every buffer.Kernel source: ported from amd-kernels (
attention/norm_rope_nvfp4_insert_d128_rot64_p128, Apache-2.0), credited in the file headers; no amd-kernels import.Contract
The HIP kernel's contract is "no two tokens of one call share a slot" and it merges the V octet (8 tokens) with atomics. The FlyDSL kernel merges an octet with a read-modify-write by the first token of a run, so tokens that share an octet must be consecutive rows of the call with consecutive slots (what every ATOM / vLLM
slot_mappinggives: one run per request,-1padding). Slots that are scattered into shared octets in an arbitrary order are outside this contract (the test's "sparse" kind, one token per octet, passes; arbitrary sharing does not). That is why the dispatch starts at 1024 tokens: decode-like calls (scattered slots, several requests per octet) stay on the atomics-based HIP kernel. A caller that scatters slots into shared octets in a large call can setAITER_FUSED_QKNORM_NVFP4_FLYDSL=0.Performance
Device time per call, cold graph replay (6 rotating caches), one MI355X GPU, M3.1 TP4 shapes, 3 alternating reps, median, us.
amd-kernelsis the kernel this is ported from:(P=32768 cached tokens; P=163840 and 8-64 / 289-512 / 24576 tokens are the same within 1-3%, see the test log.) Host time of one call (Python in front of the launch): 8.2 us vs 29.5 us for the HIP wrapper. Across 60 layers this is 2.2 ms (8K tokens) to 4.3 ms (16K) per forward. No end-to-end run was done for this PR.
Testing
python op_tests/test_fused_qknorm_nvfp4_flydsl.py [--perf] [--large]: n in {1, 7, 8, 64, 300, 488, 489, 512, 1024, 1536, 1537, 2048, 5248, 8192, 16384, 24576, 32768} x 4 slot layouts (one run, several runs with random offsets, run +-1padding, one token per octet) x 3 seeds, pages pre-filled with random bytes, all buffers equal to the HIP kernel's byte for byte (page buffers,q_out,index_q_out); the public op takes the FlyDSL path and matches;--largewrites the last pages of a 5.5 GB K/V + 2.8 GB index cache (byte offsets past 2^32).op_tests/test_fused_qknorm_nvfp4.pycases (modes seq / seq_off3 / seq_gaps / scatter / mtp at 300, 1024, 2048, 5248 tokens, 2 global scales, plus its 8192 / 32768 write-mode rows) report 0 errors against its torch reference with the dispatch on (run through a driver that skips the test's--rejectchildren: each of those aborts a process and apport writes a 5 GB core per child on our nodes). Without the 1024-token floor its scatter and mtp modes at 64-300 tokens fail, which is the octet contract above.test_fused_qknorm_idxrqknorm.pywas not re-run.Review
Self-reviewed with the repo's review-pr rules (A3/A4/B1/B4/B8/D9 relevant): gate (A3/B1): nvfp4 + nvfp4, gfx950, head set, dtypes/strides, >= 1024 tokens, kill switch; regression (A4): other dtypes untouched, existing nvfp4 test re-run; buffer bounds (B8): every descriptor is sized from n / cs_rows, page writes use the caller's slots as the HIP kernel does; D9:
--largewrites at byte offsets past 2^32. Finding fixed during review: the dispatch originally had no token floor and failed the existing scatter / mtp modes at small n, hence the 1024 floor.🤖 Generated with Claude Code