Skip to content

[FlyDSL] MiniMax-M3 index score: MXFP4 index-cache decode and prefill scorers - #7058

Closed
ganyi1996ppo wants to merge 2 commits into
mainfrom
ganyi/m3-index-score-mxfp4
Closed

ganyi1996ppo wants to merge 2 commits into
mainfrom
ganyi/m3-index-score-mxfp4

Conversation

@ganyi1996ppo

@ganyi1996ppo ganyi1996ppo commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

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 in aiter/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

  • MXFP4: e2m1 data, one e8m0 scale per 32 elements.
  • Page of 128 tokens = [128 x 64 B data][128 x 4 B scale] = 8704 B. The kernels take two views, k_data [pages,128,64] and k_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).
  • e8m0 bytes 0 and 255 are outside the contract. e2m1 x 2^e is exact in e4m3 for biased e8m0 in [119, 133]; outside that window the e4m3 paths saturate (the same range limit the unit-scale fp8 index cache has). Values outside the window were not measured on real tensors; the tests walk the byte across the window and cover wide scale ranges against the oracle.

Kernels

  • Decode score_flydsl_fp4 (same work map, geometry and CP shard as the fp8 scorer). bf16 Q: K is expanded to bf16 with v_cvt_scalef32_pk_bf16_fp4 and fed to the bf16 16x16x32 MFMA, so 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 = fp4 K with its e8m0 byte, B = Q as e4m3; products are exact in fp32. Optional q_scale (positive) is folded into the score multiplier. K tiles in flight = 6 (fp8 uses 3; an MXFP4 tile is half the bytes).
  • Prefill 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=True keeps the fallback: a Triton kernel dequantizes the referenced pages into a compact fp8 workspace and the fp8 prefill scorer runs on it unchanged.
  • The decode builder and the prefill builder are copies of the existing fp8 builders with the K side replaced, not parameterisations of them, so they can drift from the fp8 versions (an epilogue or work-map change has to be mirrored). I would like the maintainers to decide whether to merge the abstraction.

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)
shape B ctx fp8 (us) MXFP4 bf16 Q (us) MXFP4 e4m3 Q (us) bf16 Q / fp8 e4m3 Q / fp8
S4 H1, no CP 8 32768 9.65 6.24 5.71 0.65 0.59
S4 H1, no CP 8 100000 24.09 13.48 12.29 0.56 0.51
S4 H1, no CP 8 131072 26.72 14.59 13.47 0.55 0.50
S4 H1, no CP 16 32768 15.25 8.88 8.39 0.58 0.55
S4 H1, no CP 16 100000 40.33 20.88 19.99 0.52 0.50
S4 H1, no CP 16 131072 48.79 24.83 23.55 0.51 0.48
S4 H1, no CP 32 32768 26.65 14.38 13.52 0.54 0.51
S4 H1, no CP 32 100000 78.49 38.35 36.67 0.49 0.47
S4 H1, no CP 32 131072 93.79 45.37 44.20 0.48 0.47
S4 H4, CP4 rank 0 8 32768 5.53 4.28 3.95 0.77 0.71
S4 H4, CP4 rank 0 8 100000 8.97 5.85 5.19 0.65 0.58
S4 H4, CP4 rank 0 8 131072 9.77 6.08 5.68 0.62 0.58
S4 H4, CP4 rank 0 16 32768 6.21 4.92 4.47 0.79 0.72
S4 H4, CP4 rank 0 16 100000 15.14 8.52 7.67 0.56 0.51
S4 H4, CP4 rank 0 16 131072 16.25 8.80 8.15 0.54 0.50
S4 H4, CP4 rank 0 32 32768 9.81 6.09 5.71 0.62 0.58
S4 H4, CP4 rank 0 32 100000 25.35 12.97 11.80 0.51 0.47
S4 H4, CP4 rank 0 32 131072 31.44 15.43 14.27 0.49 0.45
S8 H4, CP4 rank 0 (MTP7) 8 32768 6.31 5.04 4.50 0.80 0.71
S8 H4, CP4 rank 0 (MTP7) 8 100000 10.05 7.08 5.97 0.70 0.59
S8 H4, CP4 rank 0 (MTP7) 8 131072 10.56 7.18 6.38 0.68 0.60
S8 H4, CP4 rank 0 (MTP7) 16 32768 7.02 5.52 5.12 0.79 0.73
S8 H4, CP4 rank 0 (MTP7) 16 100000 16.77 10.66 8.54 0.64 0.51
S8 H4, CP4 rank 0 (MTP7) 16 131072 17.76 11.42 8.85 0.64 0.50
S8 H4, CP4 rank 0 (MTP7) 32 32768 10.85 7.39 6.46 0.68 0.60
S8 H4, CP4 rank 0 (MTP7) 32 100000 27.83 16.90 13.62 0.61 0.49
S8 H4, CP4 rank 0 (MTP7) 32 131072 32.46 20.24 15.28 0.62 0.47

Prefill (fp8 = existing fp8 scorer, bf16 Q; staged = staged=True, dequant launch plus fp8 scorer; native = default):

cell fp8 bf16 Q (us) staged e4m3 Q (us) native bf16 Q (us) native e4m3 Q (us) staged e4m3 / fp8 native bf16 / fp8 native e4m3 / fp8
b1 q4096 ctx 8K 12.8 16.3 11.6 10.7 1.27 0.90 0.83
b1 q4096 ctx 128K 63.8 70.9 58.6 58.6 1.11 0.92 0.92
b4 q512 ctx 8K 8.8 14.6 8.5 7.9 1.66 0.96 0.90
b8 q2048 ctx 16K 38.9 50.4 34.0 32.3 1.30 0.88 0.83
MTP7 b32 q8 ctx 32K 39.6 100.5 30.5 29.5 2.53 0.77 0.75
chunk 32768, prefix 0 102.3 111.5 93.2 90.8 1.09 0.91 0.89
chunk 32768, prefix 100K (ctx 132768) 465.5 457.9 430.1 414.3 0.98 0.92 0.89

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

pytest op_tests/test_flydsl_minimax_m3_index_score.py                     # existing fp8: 269 passed, 4 skipped (unchanged)
pytest op_tests/test_flydsl_minimax_m3_index_score_fp4.py                 # 188 passed, 1 skipped
pytest op_tests/test_flydsl_minimax_m3_index_score_prefill_fp4_native.py  # 60 passed, 1 skipped
python op_tests/test_flydsl_minimax_m3_index_score_fp4.py                 # perf sweep tables, exit 0
python op_tests/test_flydsl_minimax_m3_index_score_prefill_fp4_native.py  # perf sweep table, exit 0

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_quant and dequantized with fp4_utils, with no dependency on any writer kernel. Scores are judged against the fp8 test's own oracle and against pa_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_scale from the decode multiplier (1 failure), swapping the decode nibble order (60 failures), reading the wrong e8m0 byte in the native prefill (38 failures), dropping q_scale in 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) and op_tests on top of the image's aiter; the new tests and the existing fp8 test both ran in that setup. Style: black and ruff==0.16.0 clean on the touched files.

Known limits / not measured

  • Only gfx950 was tested.
  • Kernel numerics on real M3 tensors were not measured; the tests use synthetic caches quantized with aiter's quantizer.
  • e8m0 values outside [119, 133] on the e4m3 paths are the documented saturating range and have no real-data measurement.
  • End-to-end numbers are not part of this PR; they belong to the ATOM integration PR.
  • The decode and prefill builders are copies of the fp8 ones (see Kernels).

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

ganyi1996ppo and others added 2 commits October 27, 2026 21:22
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]>
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
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 gfx1201 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 7058 --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.

@ganyi1996ppo

Copy link
Copy Markdown
Contributor Author

Closing in favour of the split PRs: #7060 (index score decode scorer) and #7061 (index score prefill scorers). Same code, now reviewed and cleaned up separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant