vllm.v1.attention.backends.mla.sparse_swa
¶
Classes:
-
ComputeDSparkNoncausalSWAIndicesKernel– -
ComputePrefillMetadataKernel– -
DeepseekSparseSWAMetadataBuilder–Builds metadata for DeepseekV4 SWA cache.
ComputeDSparkNoncausalSWAIndicesKernel
¶
Bases: VllmTritonJitKernel['ComputeDSparkNoncausalSWAIndicesKernel.CompileKey']
Methods:
-
kernel–Non-causal per-token indices for the DSpark draft block.
kernel(swa_indices_ptr, swa_indices_stride, swa_lens_ptr, window_size, index_width, query_start_loc_ptr, seq_lens_ptr, token_to_req_indices_ptr, is_valid_token_ptr, block_table_ptr, block_table_stride, block_size, token_offset, TRITON_BLOCK_SIZE)
staticmethod
¶
Non-causal per-token indices for the DSpark draft block.
Here, we populate the topk indices with the trailing window of context tokens, plus all query tokens (including future ones).
ComputePrefillMetadataKernel
¶
Bases: VllmTritonJitKernel['ComputePrefillMetadataKernel.CompileKey']
Methods:
-
kernel–Compute prefill gather_lens in a single pass.
kernel(prefill_gather_lens_ptr, seq_lens_ptr, query_start_loc_ptr, replay_start_ptr, num_prefills, num_decodes, window_size, BLOCK_SIZE)
staticmethod
¶
Compute prefill gather_lens in a single pass.
DeepseekSparseSWAMetadataBuilder
¶
Bases: AttentionMetadataBuilder
Builds metadata for DeepseekV4 SWA cache.
Similar to the indexer, this handles mixed batches by: 1. Using split_decodes_and_prefills() to determine the boundary 2. Building separate metadata for decode and prefill portions
Supports: - Mixed decode/prefill batches - MTP (Multi-Token Prediction) where decode has query_len > 1 - Chunked prefill (aligns with the indexer's chunking)
Methods:
-
build–Build SWA metadata for mixed decode/prefill batches.
-
build_tile_scheduler–Allocate one empty
FlashMLASchedMetaper present DeepseekV4 layer type.
_build_deepseek_v4_metadata(num_decodes, num_prefills, seq_lens, seq_lens_cpu, query_start_loc, query_start_loc_cpu, replay_start)
¶
Pre-compute DeepseekV4 prefill metadata during the metadata build phase.
Returns a dict of keyword arguments to pass to the DeepseekSparseSWAMetadata constructor.
Note: C128A sparse metadata is computed by the FlashMLASparse builder (which owns the C128A block_table), not here.
_build_image_visibility(num_reqs, mm_ranges, num_decode_tokens, num_prefill_tokens, seq_lens, query_start_loc, token_to_req_indices)
¶
Upload image spans and compute per-token in-image visibility.
Returns (left, right) int32 tensors indexed by absolute (decode-first) token position; only prefill rows are meaningful.
build(common_prefix_len, common_attn_metadata, fast_build=False, replay_start=None)
¶
Build SWA metadata for mixed decode/prefill batches.
The batch is assumed to be reordered with decodes first (by vLLM scheduler). We use split_decodes_and_prefills() to find the boundary, then build separate window_topk_idxs for each portion.
For prefill, we use chunked prefill to align with the indexer's chunking.
replay_start ([num_reqs], SWA bounded replay): the position from
which each request holds window KV; the model state passes it for every
batch of a replaying group, graph captures pass nothing.
build_tile_scheduler(num_decode_tokens)
¶
Allocate one empty FlashMLASchedMeta per present DeepseekV4 layer type.
Returned instances have tile_scheduler_metadata / num_splits
set to None; the FlashMLA C++ decode path will allocate them and
run the tile-scheduler planner on the first flash_mla_with_kvcache
call of each type. Subsequent same-type calls reuse the plan because
the tensors (and have_initialized) are populated on the struct.
Returns all-None when there are no decode tokens this step, so
_forward_decode sees a clean sentinel.
_compute_image_visibility_kernel(left_visible_ptr, right_visible_ptr, span_indptr_ptr, span_starts_ptr, span_ends_ptr, query_start_loc_ptr, seq_lens_ptr, token_to_req_indices_ptr, max_image_tokens, token_offset)
¶
Per-token in-image visible counts (port of get_image_visible).
One program per prefill token. A token at position pos inside a span [span_start, span_end] sees min(pos - span_start, max_image_tokens - 1) extra tokens to its left and min(span_end - pos, max_image_tokens) to its right; tokens outside every span get 0/0 (plain causal window).