Repository navigation
[FlyDSL] MiniMax-M3 index score: MXFP4 index-cache decode and prefill scorers - #7058
ganyi1996ppo wants to merge 2 commits into
Conversation
New kernel `score_flydsl_fp4` scores an MXFP4 index cache (e2m1 data plus one e8m0 scale per 32 elements) given as two views, k_data [pages,128,64] and k_scale [pages,128,4], each with its own page stride, so separate tensors and a single-region 8704 B page both work. Query dtype selects the K path: - bf16 Q: K is widened to bf16 with v_cvt_scalef32_pk_bf16_fp4 and fed to the same bf16 16x16x32 MFMA as the fp8 scorer; the numerics contract (K lifted to Q's precision) is unchanged. - e4m3 Q: one v_mfma_scale_f32_16x16x128_f8f6f4 per 16-token tile, A = the lane's 16 B of fp4 K plus its e8m0 byte, B = Q as e4m3 (unit scale). Products are exact in fp32, so scores are those of dequantize(K) against Q as stored. An optional q_scale (default 1.0, positive) is folded into the score multiplier. K tiles in flight is 6 rather than the fp8 scorer's 3: an MXFP4 tile is half the bytes, and 6 was 15-35% faster than 3 on small batch*context cells in a 2/3/4/6/8 sweep. Work map, geometry and CP shard are imported from the fp8 scorer. The builder is a copy of `build_index_score` with the K side replaced, not a parameterisation of it, so the two can drift and an epilogue or work-map change must be mirrored. e8m0 bytes 0 and 255 are outside the contract. Only exports are added to aiter/ops/flydsl/__init__.py; the fp8 path is untouched. Co-Authored-By: Claude <[email protected]>
`score_prefill_flydsl_fp4` scores a ragged prefill batch over an MXFP4 index cache (same k_data/k_scale views as the decode scorer), with a bf16 or e4m3 query and an optional q_scale. Default route is native: the fp8 prefill scorer with only its K side replaced. Pages are fetched as fp4 data plus the e8m0 dword, kept as MXFP4 through LDS (8.5 KB per buffer instead of 16 KB), and fed to the scaled MFMA as its fp4 A operand with the e8m0 byte as block scale and Q as the e4m3 B operand. No separate dequantization pass. Scores are bit-identical to the staged route. `staged=True` keeps the fallback: a Triton kernel dequantizes the pages the batch references into a compact fp8 buffer (an e4m3 query is widened to bf16 in the same launch) and the existing fp8 prefill scorer runs on it unchanged. e2m1 x 2^e is exact in e4m3 for biased e8m0 scales in [119, 133]; outside that window the value saturates, the same range limit the unit-scale fp8 index cache has. e8m0 bytes 0 and 255 are outside the contract. Tests judge the result against aiter's pa_sparse_block_score_prefill and the fp8 FlyDSL kernel on the same dequantized values, pin the staging against dequantize(K).to(fp8), and walk the e8m0 bytes across the exact-e4m3 window. The native file covers the default route; the older tests pin staged=True. 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: |
Summary
Adds MiniMax-M3 index-score kernels that read an MXFP4 index K cache (e2m1 data plus one e8m0 scale per 32 elements): a decode scorer and a prefill scorer, each taking a bf16 or an e4m3 query. M3.1 can keep its index cache in MXFP4, which halves the index-cache bytes per token (and so raises the KV pool capacity); these kernels are the reading side. The existing fp8 kernels are not modified.
New files:
minimax_m3_index_score_fp4.py(decode),minimax_m3_index_score_prefill_fp4.py(prefill entry and staged fallback),minimax_m3_index_score_prefill_fp4_native.py(native prefill kernel), two op_tests, and three lazy exports inaiter/ops/flydsl/__init__.py:minimax_m3_index_score_fp4_flydsl,minimax_m3_index_score_fp4_flydsl_supported,minimax_m3_index_score_prefill_fp4_flydsl. No C++ and no existing file other than the export list is touched.Format
[128 x 64 B data][128 x 4 B scale]= 8704 B. The kernels take two views,k_data [pages,128,64]andk_scale [pages,128,4], each with its own page stride, so the layout is the caller's choice (separate tensors or the single-region page above both work; a test checks the two agree bitwise).Kernels
score_flydsl_fp4(same work map, geometry and CP shard as the fp8 scorer). bf16 Q: K is expanded to bf16 withv_cvt_scalef32_pk_bf16_fp4and fed to the bf16 16x16x32 MFMA, so the numerics contract (K lifted to Q's precision) is unchanged. e4m3 Q: onev_mfma_scale_f32_16x16x128_f8f6f4per 16-token tile, A = fp4 K with its e8m0 byte, B = Q as e4m3; products are exact in fp32. Optionalq_scale(positive) is folded into the score multiplier. K tiles in flight = 6 (fp8 uses 3; an MXFP4 tile is half the bytes).score_prefill_flydsl_fp4. Default is the native kernel: the fp8 prefill scorer with only its K side replaced; the page stays MXFP4 through LDS (8.5 KB per buffer instead of 16 KB) and is fed to the scaled MFMA as the fp4 A operand with the e8m0 byte as block scale. A bf16 Q is rounded to e4m3 in-kernel (as the fp8 prefill kernel does); an e4m3 Q is taken directly.staged=Truekeeps the fallback: a Triton kernel dequantizes the referenced pages into a compact fp8 workspace and the fp8 prefill scorer runs on it unchanged.Performance
Measured on gfx950 (MI355X), one GPU, FlyDSL 0.4.0, image
rocm/atom-dev:nightly_202610261558(see Tests for how main's FlyDSL files were run on it). Method: CUDA-graph replay of K calls (K = 2..16 decode, 2..8 prefill) over disjoint regions of a 300k-page pool to defeat the MALL, median of 25 replays, same card and process, every cell run in two rounds with the arm order reversed (decode reports the mean of the two rounds, prefill the median). Random K data, e8m0 in [122,127], uniform context per batch, no padding bound beyond the context. Times are microseconds per call. The fp8 baseline is the existing fp8 kernel with a bf16 Q. Ratios below 1 mean MXFP4 is faster.Decode (S = queries per request, H = index heads; B = batch):
Range over the 27 cells below: bf16 Q / fp8 = 0.48-0.80, e4m3 Q / fp8 = 0.45-0.73.
Decode table (27 cells)
Prefill (fp8 = existing fp8 scorer, bf16 Q; staged =
staged=True, dequant launch plus fp8 scorer; native = default):The staged route is slower than fp8 on every cell except the 100K-prefix chunk (1.04-2.45 with a bf16 Q); the native kernel is faster than fp8 on all seven measured cells (0.75-0.96). These are kernel-level numbers only.
Tests
The skips are the perf-sweep entries that run only from the script. The first commit alone (decode) passes its own test file (142 passed, 1 skipped). The tests are standalone: the cache is built with aiter's
per_1x32_f4_quantand dequantized withfp4_utils, with no dependency on any writer kernel. Scores are judged against the fp8 test's own oracle and againstpa_sparse_block_score_prefill; the fp4 and fp8 kernels are compared on the same dequantized values. The tests cover decode geometries, CP shards, CUDA-graph buffers, split vs single-region views,q_scale, alignment and rejection rules, prefill ragged and mixed-length batches, trailing empty requests, strided views, and e8m0 extremes.Mutation check (each applied to the kernel source in turn, then reverted, with the matching test file): dropping
q_scalefrom the decode multiplier (1 failure), swapping the decode nibble order (60 failures), reading the wrong e8m0 byte in the native prefill (38 failures), droppingq_scalein the native prefill (2 failures).Environment: the image's aiter (41d4d8d) is older than main and main's full Python tree does not import against the image's csrc, so I ran main's
aiter/ops/flydsl/directory (at 340bea3) andop_testson top of the image's aiter; the new tests and the existing fp8 test both ran in that setup. Style:blackandruff==0.16.0clean on the touched files.Known limits / not measured
Related work
This PR is independent and can be reviewed and merged on its own. The MXFP4 index-K cache write (in
fused_qknorm_idxrqknorm) is #7057, the MXFP4 paged-attention decode is #7056, and the ATOM integration is ROCm/ATOM#2770 (draft; depends on these aiter PRs and on yuhua/m3.1-flash).🤖 Generated with Claude Code