Skip to content

[Feat] [AMD] minimax: gfx950 sparse attention, NVFP4 KV and DSpark verify kernels - #46690

Merged
hnyls2002 merged 30 commits into
sgl-project:mainfrom
kevin-mii:m31-amd/k1-attention
Nov 1, 2026
Merged

hnyls2002 merged 30 commits into
sgl-project:mainfrom
kevin-mii:m31-amd/k1-attention

Conversation

@kevin-mii

@kevin-mii kevin-mii commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Part of the MiniMax-M3.1-Flash AMD series that replaces #45936 (closed with its base branch minimax-m3.1-flash): kernel PRs #46690 (attention), #46691 (MoE / GEMM) and #46692 (quick all-reduce), each on main; then #46693 with the M3.1 call sites and #46694 (indexer CP). It follows the DeepSeek-V4.1 AMD layout (#41018-#41021, then #41308).

Summary

  • NVFP4 KV (MSA-v1 layout). minimax_sparse/common/nvfp4.py reads the packed rows for the Triton kernels and nvfp4_gfx950.py dequantizes them in Gluon. The kernel tests build their packed caches with a torch quantizer in NVFP4KVQuantizeUtil's layout (nvfp4_kv_quantize in sglang/test/quant_ref_utils.py).
  • Sparse decode. decode/nvfp4_decode_gfx950.py runs sparse decode over the packed NVFP4 K/V in Gluon (one program per row, top-k chunk and KV head; per-warp online softmax). topk_sparse.py dispatches an Nvfp4Cache to it, and the NVFP4 verify scorer sits beside the bf16 one in decode/verify_topk_idx.py.
  • Verify indexer. decode/verify_topk_idx.py scores a linear draft chain against one shared prefix; decode/verify_topk_gfx950.py fuses the per-block max score with a streaming top-k on MFMA tiles.
  • Prefill. prefill/index_topk_gfx950.py is the fused prefill indexer; prefill/gluon_prefill_pair_gfx950.py runs two queries per program over the union of their top-k blocks; prefill/nvfp4_prefill.py gathers the NVFP4 cache into the Gluon prefill's paged layout; prefill/gluon_prefill.py and prefill/flash_with_topk_idx.py take the new cache.
  • fp8 O epilogue. common/out_quant_gfx950.py is shared by the Gluon decode and pair prefill: they can write O directly as the fp8 o_proj input, with the same bytes and scales as aiter's separate per-token or 1x128 quant.
  • QK-norm + RoPE. minimax_m3_qk_norm_rope.py adds the gfx950 fused Gemma Q/K RMSNorm + partial RoPE + KV-cache write.
  • HiCache. The NVFP4 page-row copy takes its CTA quota as a template argument: 32 on HIP (MI350X needs 16+ to saturate PCIe), 2 on CUDA as before.
  • Windowed verify. verify_mla_window.py lets the gfx950 grouped-head split-KV verify serve the DSpark draft's sliding window (its non-causal draft block, window 4095) with a one-pass combine, as a VerifyMLA subclass; num_stages=2 was 13-26% faster for that window. The shared verify_mla.py only gains the stage-1 window mask (a constexpr, compiled out without a window), the caller's opt-in in can_handle, and the two class hooks the subclass sets; Qwen3.5 and Kimi-K3 verify outputs are bitwise unchanged.
  • The M3.1 call sites (NVFP4 pool, verify indexer, fp8 O for o_proj, windowed draft verify) are in the integration PR.

Serving gain of each kernel

Measured in the integration PR at TP4 on MI350X (NVFP4 KV, DSpark acceptance simulated at 3 tokens per step, 32K-token contexts), each change turned off alone; the share decode TPOT or prefill latency grows without it, mean of two GPU sets:

  • Windowed draft verify (verify_mla_window): decode TPOT +20.6% at 1 request, +8.2-9.6% at 8-64.
  • Two-queries-per-program Gluon prefill (gluon_prefill_pair_gfx950): 32K-token prefill +10.3%, decode +4.2-5.4% at 8-64.
  • Gluon prefill over an FP8 KV pool (SGLANG_MINIMAX_M3_AMD_KV_FORMAT=fp8): prefill +11.7% (32K tokens) and +12.4% (eight 8K requests).
  • NVFP4 KV itself (kernel level, CUDA-graph replay, per layer): the verify indexer at 110K context takes 0.75-0.84x FP8's time from 8 requests and sparse decode 0.42-0.68x, with 1.78x the tokens per GB of KV.

Changes to existing kernels

On CUDA nothing changes: topk_sparse only dispatches the new Nvfp4Cache type, the decode top-k only moves into select_decode_topk, and the page-row quota stays 2.

On gfx950, main's MiniMax-M3 layers switch kernels when this merges, with no new call site:

  • QK-norm + RoPE + cache store (sparse_qk_index_gemma_rmsnorm_rope_cache) runs the tiled kernel for M3's 128-wide head with 64-dim neox RoPE; its bf16 outputs equal the per-head kernel's bit for bit.
  • The Gluon sparse prefill pairs two queries per program for bf16 batches from 2048 tokens (16 query heads), and now also serves an fp8 KV pool at its per-tensor k/v scales, which minimax_sparse_prefill now passes on; before, an fp8 pool stayed on Triton. Both match the Triton kernel up to bf16 rounding.
  • The prefill indexer uses the fused gfx950 index top-k for score-only max layers at unit scales and 128-aligned pages; on these bf16 / fp8 index caches it emits the Triton two-pass indexer's block ids exactly. It keeps Triton when Gluon's CDNA4 module is missing.

On other ROCm GPUs, the per-head QK-norm kernel, which serves them (and gfx950 heads other than M3's), no longer writes main K/V to slot 0, the reserved CUDA-graph padding slot. The unfused store_cache already skips it, and no real token is stored there.

Verification

  • MI350X (ROCm 7.2 image rocm/sgl-dev:v0.5.20-rocm720-mi35x-20260926, AITER acf8fdf93, SGLANG_USE_AITER=1), this branch alone on main: test_minimax_gfx950_prefill 35 passed, test_minimax_m3_qk_norm_rope 55, test_minimax_nvfp4_sparse_decode 13, test_minimax_verify_topk_idx 68 (the kernel cases; the integration PR adds 8 more), test_verify_shared_kv 10. pre-commit clean; main's callers of every modified kernel module import.
  • Not executed on this branch alone: CUDA (shared files keep main's behavior there), end-to-end serving.

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ✅ Run #40902705557
Latest PR Test (Extra): ❌ Run #40902705801
Latest PR Test (AMD ROCm 10): ❌ Run #40902705369

Kevin Mi and others added 8 commits October 26, 2026 21:01
…rify kernels

Kernels for MiniMax-M3.1-Flash on MI350X / MI355X with no callers yet: the NVFP4 sparse decode and Gluon prefill, the verify top-k indexer, QK-norm + RoPE, the NVFP4 KV and HiCache page-row paths, and the DSpark draft / accept kernels.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… kernels

- gluon_prefill: `_get_scratch` always allocates the scratch at bf16 width
  and only views it as fp8, so "256 MiB at fp8" and the grow step's
  "at bf16" misstated the footprint; the grow-step line is back to main's.
- dspark kernel parity test: the HIP-only case list holds the TP-shard
  argmax as well as the step bias, so the comment names both.
- verify_mla: the 13-26% speedup is a benchmark result for the PR body; the
  line only needs where num_stages=2 was measured.
- index_topk_gfx950: reflow a three-line comment whose last line was a
  single word.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
"as in the Triton kernel" read as if a Triton NVFP4 decode kernel existed;
the attention has only the Gluon kernel, and what it matches up to fp32
rounding is the bf16 Triton decode kernel run on the dequantized cache, the
comparison test_minimax_nvfp4_sparse_decode makes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ke it

`verify_topk_idx` and `_use_fused_index_topk` both branch on
`isinstance(k_cache, Nvfp4Cache)`, but their signatures still said
`torch.Tensor`; the sibling `_use_fused_verify_topk` already spells
`torch.Tensor | Nvfp4Cache`. Annotation only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`scale_cast_to_cache` and `cache_inv_scale` are used only inside
minimax_m3_qk_norm_rope.py (here and in every later branch of the series),
and every other helper the fused cache kernel calls is `_`-prefixed; the
public names suggested an API other modules import.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`_gluon_sparse_prefill_group` ran `isinstance(k_cache, Nvfp4Cache)` for the
KV-scale tensors and again three lines later for the scratch dtype; bind it
once before both. `k_cache` is only rebound after the gather, below either
use, so the two checks always agreed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- accept_greedy_target_predict looped over method-name strings and
  resolved them with getattr; iterate over `AcceptGreedy.torch` /
  `AcceptGreedy.triton` themselves, as the module docstring says every
  case does.
- markov_step_bias imported `MarkovStepBias` midway through the case and
  re-bound it as `cls`; import it first, like the TP-shard argmax case,
  and drop the alias.

Same inputs, same seeds, same assertions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ason

test/README.md requires a file registered on CUDA plus another accelerator
to carry a `backend-specific:` comment above the extra registration naming
what only that backend catches. Here that is the gfx950-only step-bias and
TP-shard argmax cases, which `_CASES` gates on `torch.version.hip`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kevin-mii

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Oct 27, 2026
Kevin Mi and others added 5 commits October 27, 2026 01:43
…bits

At the M3.1 draft's full 200,064-row vocab, MarkovStepBias is not bitwise equal to the embedding + F.linear step bias it replaced (about 0.01% of elements differ at 1 and 8 tokens), and it saved only 27-45 us of a decode step. HIP now runs the same VanillaMarkov path as CUDA.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`nvfp4_kv_quantize` has no production caller: the pool quantizes through
the fused qk-norm kernel, and only the gfx950 kernel tests use the Triton
quantizer to build their packed caches. It moves from
`kernels/ops/quantization/nvfp4_kv_triton.py` to
`sglang/test/kernels/nvfp4_kv.py`, beside the other test-only kernels, and
takes `nvfp4_quantize_rows`, its only user, out of `common/nvfp4.py`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The gfx950 NVFP4 kernel tests only need a packed cache to feed the kernels;
they check each kernel against another run on the same bytes or against
their own dequantization, never the quantizer. Main's NVFP4 quantizers are
FlashInfer's, so the tests carried a Triton kernel; a dozen lines of torch
do the same job. It writes the same bytes (checked against the Triton kernel
on random, tiny, large, zero and e2m1-boundary inputs at two global
scales), saturating an out-of-range block scale at 448 as the hardware
conversion does instead of torch's NaN.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…store

Their only callers, the gfx950 deferred verify-logits gather and the
stacked DSpark context-K/V writer, left the integration PR: measured on and
off at TP4 neither moved decode TPOT past run-to-run noise outside
simulated-acceptance benchmarks. dspark_accept's precomputed target_predict
goes with the argmax.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ferences

sglang/test/quant_ref_utils.py already holds the hand-written NVFP4
references the parity tests check srt against (independent of srt, e2m1 and
E4M3 constants included), so the torch NVFP4 KV quantizer moves there from
its own sglang/test/kernels/nvfp4_kv.py and reuses FLOAT4_E2M1_MAX and
FLOAT8_E4M3_MAX. Same bytes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the quant LLM Quantization label Oct 27, 2026
… like its siblings

The kernel imports the CDNA4 buffer and MFMA ops, so it runs on gfx950 only,
as do index_topk_gfx950, verify_topk_gfx950 and nvfp4_decode_gfx950 next to
it; the package names such kernels with the arch (kda_fused_decode_gfx950,
mla_v4_decode_gfx950). gluon_prefill_pair.py becomes
gluon_prefill_pair_gfx950.py.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Kevin Mi and others added 4 commits October 28, 2026 02:34
…dexer cannot import

The fused index top-k imports Gluon's CDNA4 module; on a gfx950 image whose
Triton lacks it, main's prefill indexer raised instead of keeping the Triton
kernel. The guard matches the verify indexer's and the NVFP4 decode's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
flash_decode_with_gqa_share_sparse, flash_prefill_with_topk_index and the Gluon
prefill's gather, gate and group take an Nvfp4Cache since this series but were
still annotated torch.Tensor.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The verify merge writes a caller's out as flat rows through stride(1), so a
multi-head out with a padded head stride landed rows in the wrong head; it now
asserts the shape, dtype and row layout as select_decode_topk does. The prefill
indexer's gate now requires unit-stride q columns, which its loads assume.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ANG_USE_AITER

fp8_utils binds aiter_per1x128_quant only when SGLANG_USE_AITER=1, so the
pair-prefill fp8 epilogue test failed to import under plain pytest.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kevin-mii

Copy link
Copy Markdown
Collaborator Author

CI at 553f99e456:

  • CUDA: base-a, every base-b shard and every JIT-kernel job pass. base-b needed two re-runs on unrelated flakes: a NIXL temp-file error in test_hicache_nixl_storage, then a runner without diffusers for test_image_preprocessing. In base-c, H20 shard 1 hit prefill/decode KV-transfer timeouts in a CUDA disaggregation test this PR does not touch.
  • AMD: stage A, the JIT kernel tests and both MI35x shards, which run this PR's six test files, pass. Still red, and red on main or every recent PR too: MI300 stage-B shard 0 (killed at about 36 minutes) and MI35x disaggregation (GSM8K 0.0 on main).
  • Not required: Xeon died on a full runner disk before any test ran (re-run), and the Extra workflows need the run-ci-extra label.

Kevin Mi and others added 4 commits October 29, 2026 00:00
The module held the NVFP4 verify scorer and a wrapper around the gfx950 decode
kernel, so its name said decode while most of it was the indexer. The scorer now
sits beside its fp8 counterpart in verify_topk_idx.py, and topk_sparse.py's
Nvfp4Cache branch gates and calls the Gluon kernel itself. No behavior changes:
the same kernels run with the same arguments, and a tensor cache, which is all
CUDA passes, never reaches the branch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…show

Drop the bandwidth numbers from measured-on notes, split multi-line docstring summaries into a summary line and one paragraph, use double backticks in docstrings, and make each file's comments one sentence style.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ndow.py

The windowed combine kernel, its tile table and row choice, and the stage-1
pipelining it needs now live in WindowedVerifyMLA, a VerifyMLA subclass. The
shared module keeps only what the window needs from it: the stage-1 prefix mask
(a constexpr, compiled out without a window), the caller's opt-in in can_handle,
and the class hooks (block_config, prefix_num_stages) the subclass sets. Outputs
are bitwise equal to the single-module version for the M3.1 window at 1-64
requests and for the Qwen3.5 and Kimi-K3 verify shapes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ot show

The module docstring says why one pass suffices (every draft key sits inside the window), the class docstring names the non-causal constraint can_handle enforces, and the comments share one style.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kevin-mii kevin-mii added the bypass-fail-fast CI: a failing job no longer aborts its siblings (lint still gates) label Oct 29, 2026
@kevin-mii

Copy link
Copy Markdown
Collaborator Author

CI at 1764758c08:

  • CUDA: base-a and all 19 base-b shards pass. base-b needed re-runs on unrelated flakes: a runner without diffusers for test_image_preprocessing, then the NIXL temp-file error in test_hicache_nixl_storage again. One JIT-kernel shard is red on main's test_gdn_prefill_graph (KVLayerRouter.from_layer_maps indexes an empty tuple), which [Fix] mem_cache: Build an empty KVLayerRouter from empty layer maps #47127 fixes; every other JIT-kernel job passes.
  • AMD: stage A, the JIT kernel tests, every MI300 stage-B and stage-C shard and the MI35x stage-C shard pass. The two MI35x stage-B shards, which run this PR's test files, are still queued. MI35x disaggregation is red (KV-transfer handshake failures in a non-MiniMax test), as on earlier heads.
  • Not required: Xeon builds its image from the PR branch but runs the tests from current main, so main tests newer than this branch's base fail to import (SchedulerOutputTimeStats, the qwen3_coder union fix). XPU's bench_one_batch fails on main's own TreeCacheNamespace missing req_to_token_pool. The Extra, MLU and MLX workflows need their opt-in labels.

Picks up sgl-project#47127, which fixes main's test_gdn_prefill_graph that pr-test-finish fails on.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blackwell SM100/SM120 bypass-fail-fast CI: a failing job no longer aborts its siblings (lint still gates) hicache Hierarchical Caching for SGLang jit-kernel quant LLM Quantization run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants