Skip to content

vllm.model_executor.layers.attention.sparse_mla_attention

Shared MHA implementation and metadata builder for sparse MLA backends.

Classes:

Functions:

SharedTopkIndicesBuffer

Resolves the shared top-k index buffer for sparse MLA implementations.

The indexer owns the buffer, but LLMBaseProposer.load_model repoints the draft's indexers at the target's buffer after the impls are constructed, so it must be resolved per read rather than snapshotted. Backbone skip-topk layers have no indexer and pass the buffer explicitly.

SparseMLACommonImpl

Bases: MLACommonBaseImpl[T], SharedTopkIndicesBuffer, Generic[T]

Sparse MLA base with dense and masked-MHA prefill paths.

_try_build_global_mask(topk_per_req, q_lens, max_query_len, max_seq_len, topk_mask_workspace) staticmethod

Build a full-sequence top-k mask if it fits within the budget.

When the mask fits, it is reused across the suffix and all context chunks, avoiding per-chunk mask rebuilds. Returns None when the mask is too large, signalling the caller to fall back to per-chunk index remapping.

SparseMLACommonMetadataBuilder

Bases: AttentionMetadataBuilder[T]

_topk_masks_fit(batch_size, max_query_len, chunked_context)

Whether this prefill batch's top-k masks fit the workspace.

_build_topk_mask(topk_indices_per_req, q_lens, max_q_len, max_seq_len, out)

Build a bit-packed top-k mask while preserving padded row storage.

_is_masked_mha_available(num_heads_total, kv_lora_rank, qk_nope_head_dim, qk_rope_head_dim, v_head_dim, kv_cache_dtype)

Check if masked MHA can ever fire for this model configuration.

_masked_mha_workspace_fits(batch_size, max_query_len, max_context_chunk_seq_len, workspace_numel)

Return whether the suffix and per-context-chunk masks fit the workspace.

The global mask is excluded: it always needs more, and has its own check.

_topk_mask_shape(batch_size, max_query_len, max_key_len, reserve_key_starts_word=False)

Shape of a bit-packed top-k mask, shared by every site that builds one.

build_shardq_metadata(split, metadata, device, gathered_context=None)

This step's ShardQ, with metadata narrowed to this rank's tokens. Under DCP, those tokens attend the step's gathered_context.