vllm.models.minimax_m3.common.sparse_attention ¶
Main block-sparse GQA attention for MiniMax M3 sparse layers.
The lightning indexer (indexer.py) selects the top-k KV blocks (written into the shared layer.topk_indices_buffer); this module holds the main attention that attends only to those blocks: the paged K/V cache backend, its metadata + builder, and the impl that reads the indexer's top-k from that buffer. The Triton attend kernel lives here; the SM100 (MSA) build_k2q_csr + sparse_atten_func attend lives in nvidia/sparse_attention_msa.py.
MiniMaxM3SparseBackend and MiniMaxM3SparseMetadata are referenced by the attention-backend registry (by dotted path) and by spec-decode, so they must keep these names and stay in this module.
Classes:
-
MiniMaxM3SparseBackend–Block-sparse GQA backend for MiniMax M3 sparse attention layers.
-
MiniMaxM3SparseDecodeMetadata–Per-decode state (cudagraph-safe).
decode_query_lenis the uniform -
MiniMaxM3SparseImpl–Abstract base for block-sparse GQA over the indexer-selected blocks.
-
MiniMaxM3SparseMetadata–Sparse-attention metadata, split into prefill and decode sub-metadata.
-
MiniMaxM3SparsePrefillMetadata–Per-prefill state;
cu_seqlens_k/total_kv_blocksfeed the MSA CSR. -
MiniMaxM3SparseTritonImpl–Triton block-sparse attend (
minimax_m3_sparse_attn) + Triton decode.
Functions:
-
minimax_m3_query_token_positions–Map each prefill query token to its request id and absolute KV position.
-
minimax_m3_rebase_slots_to_page16–Rebase a token slot mapping onto AITER's page-16 page numbering.
-
minimax_m3_use_aiter_sparse_pa–Whether to use the ROCm AITER page-16 sparse PA prototype.
-
select_main_backend_and_impl_cls–Pick the main attention backend and implementation.
-
select_main_impl_cls–Backward-compatible implementation-only selector.
MiniMaxM3SparseBackend ¶
Bases: AttentionBackend
Block-sparse GQA backend for MiniMax M3 sparse attention layers.
Source code in vllm/models/minimax_m3/common/sparse_attention.py
MiniMaxM3SparseDecodeMetadata dataclass ¶
Per-decode state (cudagraph-safe). decode_query_len is the uniform per-request query length (1, or 1 + num_speculative_tokens).
Source code in vllm/models/minimax_m3/common/sparse_attention.py
MiniMaxM3SparseImpl ¶
Bases: AttentionImplBase[MiniMaxM3SparseMetadata]
Abstract base for block-sparse GQA over the indexer-selected blocks.
Inherits AttentionImplBase for a custom forward signature (the layer pre-inserts K/V and runs the indexer, which writes the selected blocks into the shared layer.topk_indices_buffer; the attend reads them back from there). The Triton and MSA subclasses each own a full forward -- no shared forward code.
Methods:
-
forward–Attend the queries to the indexer-selected blocks. Per kernel.
Source code in vllm/models/minimax_m3/common/sparse_attention.py
forward(layer, query, kv_cache, output, *, query_fp8=None) ¶
Attend the queries to the indexer-selected blocks. Per kernel.
The indexer has already written the top-k block ids into layer.topk_indices_buffer (decode at [:, :nd], prefill at [:, nd:num_tokens]); the attend reads them from there.
Source code in vllm/models/minimax_m3/common/sparse_attention.py
MiniMaxM3SparseMetadata dataclass ¶
Bases: AttentionMetadata
Sparse-attention metadata, split into prefill and decode sub-metadata.
Source code in vllm/models/minimax_m3/common/sparse_attention.py
MiniMaxM3SparsePrefillMetadata dataclass ¶
Per-prefill state; cu_seqlens_k/total_kv_blocks feed the MSA CSR.
Source code in vllm/models/minimax_m3/common/sparse_attention.py
MiniMaxM3SparseTritonImpl ¶
Bases: MiniMaxM3SparseImpl
Triton block-sparse attend (minimax_m3_sparse_attn) + Triton decode.
Source code in vllm/models/minimax_m3/common/sparse_attention.py
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 | |
minimax_m3_query_token_positions(cu_seqlens_q, prefix_lens, total_q) ¶
Map each prefill query token to its request id and absolute KV position.
Both tensors are the same for every sparse layer in a step, so the AITER sparse PA block-table builder reads them from the metadata instead of rebuilding them once per layer.
Source code in vllm/models/minimax_m3/common/sparse_attention.py
minimax_m3_rebase_slots_to_page16(slot_mapping, block_size, out=None) ¶
Rebase a token slot mapping onto AITER's page-16 page numbering.
AITER's KV writer derives the destination page from slot // 16 and assumes pages are numbered consecutively. When the resolved layout stores both K/V sides inside a block, a block spans twice as many pages, so only the block component of the slot doubles -- the offset within the page must not move. Clamping before the division leaves padding slots at their negative sentinel, which is what tells the writer to skip them.
Source code in vllm/models/minimax_m3/common/sparse_attention.py
minimax_m3_use_aiter_sparse_pa(num_kv_heads) ¶
Whether to use the ROCm AITER page-16 sparse PA prototype.
Source code in vllm/models/minimax_m3/common/sparse_attention.py
select_main_backend_and_impl_cls(*, topk_blocks, kv_cache_dtype, num_kv_heads) ¶
Pick the main attention backend and implementation.
Blackwell (SM100) uses the MSA attend for supported top-k block counts when the KV cache is BF16 or FP8 E4M3; MI355 uses AITER sparse PA with shuffle KV cache layout; Other platforms and FP8 E5M2 fall back to Triton. The MSA modules are imported lazily to avoid import errors on unsupported platforms.
Source code in vllm/models/minimax_m3/common/sparse_attention.py
select_main_impl_cls(*, topk_blocks, kv_cache_dtype, num_kv_heads) ¶
Backward-compatible implementation-only selector.