Skip to content

vllm.v1.attention.backends.mla.sparse_utils

Utility functions for sparse MLA backends.

Classes:

Functions:

SparseMLAPrefillRoute

Bases: Enum

How a sparse MLA step runs its prefill tokens.

  • DENSE_MHA: every prefill fits in the top-k, so MHA over all keys.
  • MASKED_MHA: MHA with the top-k as a per-query key mask.
  • SPARSE_MQA: the prefill tokens run in forward_mqa, like the decodes.

Set once per step by SparseMLACommonMetadataBuilder._prefill_route; readers use it as is instead of re-deriving it.

_remap_tiling(NUM_TOPK_TOKENS, BLOCK_N, count_valid)

Pick the column tiling for the index remap kernel.

Counting the valid slots per row is the only reason the column tiles have to talk to each other, so when counting give one program the whole row: the count becomes an in-register reduction plus a plain store, needing neither atomics nor a zero-initialized counter. Pad modest non-power-of-two widths (including GLM's 2176 entries) to one tile; larger widths stay tiled and atomic.

Returns:

align_blocks_to_rows(spec, rows=1)

Sparse kernels read the cache as flat rows, viewed as rows-row pages, so blocks must be whole pages apart.

flat_kv_row_view(kv_cache, block_size)

Flat [row, head_dim] view of a paged cache and its physical rows per block.

Token offset is block_idx * block_stride_rows + offset_in_block. When other layers' pages sit between consecutive blocks of this cache, block_stride_rows exceeds block_size; those in-between rows are never indexed (triton_convert_req_index_to_global_index ensures this).

neutralize_dcp_empty_rows_(out, lse, topk_indices)

Set out to 0 and lse to -inf on rows where this DCP rank owns no top-k slot (all -1), the identity of the cross-rank LSE merge.

prepare_sparse_mla_safe_lengths(physical_indices, valid_counts)

Install dummy slots for empty queries and return nonzero kernel lengths.

Preserve the raw counts so empty outputs and LSE can be neutralized later.

request_row_bounds(req_idx)

Bounds of the runs of adjacent rows that belong to one request: run r is rows [bounds[r], bounds[r + 1]).

Under PCP a rank holds two adjacent chunk rows of a split prefill; the sparse backends give such a run one KV region.

triton_convert_req_index_to_global_index(req_id, block_table, token_indices, BLOCK_SIZE=64, BLOCK_STRIDE_ROWS=None, NUM_TOPK_TOKENS=2048, BLOCK_N=128, HAS_PREFILL_WORKSPACE=False, prefill_workspace_request_ids=None, prefill_workspace_starts=None, prefill_workspace_rank_stride=None, dcp_size=1, dcp_rank=0, cp_kv_cache_interleave_size=1, return_valid_counts=False, out=None, valid_counts_out=None, idx_mapping=None)

out[token_id, indice_id] = block_table[req_id[token_id], token_indices[token_id, indice_id] // BLOCK_SIZE] * BLOCK_SIZE + token_indices[token_id, indice_id] % BLOCK_SIZE

Only when token_indices[token_id, indice_id] == -1 do we output -1. For safety, we also output -1 if the derived block_id would be out-of-bounds.

When HAS_PREFILL_WORKSPACE is True, prefill tokens are mapped to workspace offsets instead of global cache slots. prefill_workspace_request_ids and prefill_workspace_starts must be provided.

int32 [num_tokens], -1 for decode else

prefill request index (maps to prefill_workspace_starts)

prefill_workspace_starts: int32 [num_prefills], 0-indexed workspace starts for each prefill request

When idx_mapping (int32 [num_requests]) is given, block_table is indexed by idx_mapping[req_id[token_id]] instead of req_id[token_id].

When return_valid_counts is True, also returns the count of valid (non -1) indices per row, computed during the same kernel pass (no extra overhead).

triton_filter_and_convert_dcp_index(req_id, block_table, token_indices, dcp_size, dcp_rank, cp_kv_cache_interleave_size=1, BLOCK_SIZE=64, BLOCK_STRIDE_ROWS=None, NUM_TOPK_TOKENS=2048, BLOCK_N=128, return_valid_counts=False, compact_valid_to_front=True)

Filter global per-request indices to this DCP rank's local slots.

With compact_valid_to_front (default), the conversion kernel scatters this rank's owned slots to a contiguous prefix [0, valid_count) and leaves the rest -1. DCP filtering marks non-owned slots -1 and so creates interior gaps; the trtllm-gen sparse kernel reads the first valid_count entries of each row, so they must be a contiguous prefix. Compaction is fused into the kernel (atomic slot allocator) rather than a separate sort/gather pass. Prefix order is unspecified (only the set matters).