vllm.v1.attention.backends.utils ¶
Classes:
-
PerLayerParameters–Currently, FlashInfer backend only support models in which all layers share
Functions:
-
compute_mm_prefix_range_tensor–Convert mm_prefix_range dict to padded tensor for Triton kernel.
-
fill_mm_prefix_query_ranges–Map each scheduled query token to the mm_prefix range containing it.
-
get_dcp_local_seq_lens–While using dcp, kv_cache size stored on each rank may be different,
-
get_num_attention_heads_from_layers–Per-TP-rank
num_headsshared by the named Attention layers. -
get_per_layer_parameters–Scan layers in
layer_namesand determine some hyperparameters -
infer_global_hyperparameters–Currently, FlashInfer backend other than trtllm-gen
-
mamba_get_block_table_tensor–Get the block table tensor for mamba kernels from the input
-
reorder_batch_to_split_decodes_and_prefills–Reorders the batch to split into prefill and decode requests; places all
-
reshape_attn_output_for_spec_decode–Reshapes the attention output tensor, so that
-
reshape_query_for_spec_decode–Reshapes the query tensor for the specified batch size, so that
-
split_decodes_and_prefills–Assuming a reordered batch, finds the boundary between prefill and decode
-
split_decodes_prefills_and_extends–Assuming a reordered batch, finds the boundary between prefill and decode
-
split_prefill_chunks–Split the prefill requests into chunks such that the total sequence length
-
subclass_attention_metadata–Return a new subclass of
metadata_clswith additional fields
PerLayerParameters dataclass ¶
Currently, FlashInfer backend only support models in which all layers share the same values for the following hyperparameters. Should not be used for trtllm-gen backend since it supports different values for the following hyperparameters.
Source code in vllm/v1/attention/backends/utils.py
compute_mm_prefix_range_tensor(mm_prefix_range, num_seqs, device) ¶
Convert mm_prefix_range dict to padded tensor for Triton kernel.
Returns shape: (num_seqs, max_ranges, 2) with 0-padding for empty ranges. Empty ranges have start==end==0, which kernel skips via is_valid check.
Source code in vllm/v1/attention/backends/utils.py
fill_mm_prefix_query_ranges(out, mm_prefix_range, query_start_loc_cpu, seq_lens_cpu) ¶
Map each scheduled query token to the mm_prefix range containing it.
Writes into out, a caller-owned (max_num_batched_tokens, 2) int32 staging buffer, and returns the number of rows written (0 if no range covers any scheduled query token, in which case out is untouched and the caller should skip the mask_mod entirely). Row i holds the absolute [start, end] bounds of the bidirectional range that query token i belongs to, or (-1, -1) when it is outside every range.
mm_prefix ranges never overlap, so "query and key share a range" is equivalent to "the key lies inside the query's own range". The kernel therefore needs no key-side lookup, and this metadata is sized by scheduled query tokens rather than by context length -- bounded by max_num_batched_tokens instead of num_seqs * max_seq_len.
Ranges are absolute prompt positions and may extend past the tokens scheduled so far under chunked prefill; the portion outside the current chunk is simply not recorded. Degenerate ranges (start >= end) are skipped to match the Triton path's start < end validity check.
seq_lens_cpu only needs to be exact for prefill rows, since mm_prefix ranges cover prompt tokens: an over-estimate on a decode row shifts that row's query position further past every range, which still matches nothing.
Source code in vllm/v1/attention/backends/utils.py
get_dcp_local_seq_lens(seq_lens, dcp_size=1, dcp_rank=None, cp_kv_cache_interleave_size=1) ¶
While using dcp, kv_cache size stored on each rank may be different, use this function to calculate split decode seq_lens of each dcp rank. Only consider dcp now, we can extend the case of cp based on this.
Source code in vllm/v1/attention/backends/utils.py
get_num_attention_heads_from_layers(vllm_config, layer_names) ¶
Per-TP-rank num_heads shared by the named Attention layers.
Use in metadata builders whose plan-time allocations depend on the head count: the model-wide get_num_attention_heads() is wrong for models with non-uniform per-layer head counts. All layers in one attention group must agree on num_heads; this is asserted. Returns None when no matching Attention layer is found.
Source code in vllm/v1/attention/backends/utils.py
get_per_layer_parameters(vllm_config, layer_names, cls_) ¶
Scan layers in layer_names and determine some hyperparameters to use during plan.
Source code in vllm/v1/attention/backends/utils.py
infer_global_hyperparameters(per_layer_params) ¶
Currently, FlashInfer backend other than trtllm-gen only support models in which all layers share the same values for the following hyperparameters: - window_left - logits_soft_cap - sm_scale
So this function asserts that all layers share the same values for these hyperparameters and returns the global values.
Source code in vllm/v1/attention/backends/utils.py
mamba_get_block_table_tensor(block_table, seq_lens, kv_cache_spec, mamba_cache_mode) ¶
Get the block table tensor for mamba kernels from the input common_attn_metadata.block_table_tensor given different mamba cache modes.
-
"all": input (#requests, cdiv(max_model_len, block_size) + num_speculative_blocks); output (#requests, cdiv(max_model_len, block_size) + num_speculative_blocks).
-
"none": input (#requests, 1 + num_speculative_blocks); output (#requests, 1 + num_speculative_blocks).
-
"align": input (#requests, cdiv(max_model_len, block_size)); output (#requests, 1 + num_speculative_blocks), which are the last 1 + num_speculative_blocks of each request.
Source code in vllm/v1/attention/backends/utils.py
reorder_batch_to_split_decodes_and_prefills(input_batch, scheduler_output, decode_threshold=1) ¶
Reorders the batch to split into prefill and decode requests; places all requests with <= decode_threshold tokens at the front of the batch.
The batch is reordered into 4 regions
decode: (num_scheduled <= threshold AND is not prefilling) short_extend: (num_scheduled <= threshold AND is chunked prefilling) long_extend: (num_scheduled > threshold AND is chunked prefilling) prefill: (num_computed == 0) # First chunks
Returns:
-
bool–True if the batch was modified, False otherwise.
Source code in vllm/v1/attention/backends/utils.py
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 | |
reshape_attn_output_for_spec_decode(attn_output) ¶
Reshapes the attention output tensor, so that the batch_size and seq_len dimensions are combined.
Source code in vllm/v1/attention/backends/utils.py
reshape_query_for_spec_decode(query, batch_size) ¶
Reshapes the query tensor for the specified batch size, so that it has shape (batch_size, seq_len, num_heads, head_dim).
Source code in vllm/v1/attention/backends/utils.py
split_decodes_and_prefills(common_attn_metadata, decode_threshold=1, require_uniform=False, treat_short_extends_as_decodes=True) ¶
Assuming a reordered batch, finds the boundary between prefill and decode requests.
The batch is expected to be ordered as
decode → short_extend → long_extend → prefill
Parameters:
-
(common_attn_metadata¶CommonAttentionMetadata) –CommonAttentionMetadata object containing the batch metadata.
-
(decode_threshold¶int, default:1) –The maximum query length to be considered a decode.
-
(require_uniform¶bool, default:False) –If True, requires that all decode requests have the same query length. When set, some queries may be considered prefills even if they are <= decode_threshold, in order to ensure uniformity.
-
(treat_short_extends_as_decodes¶bool, default:True) –If True (default), short extends (query_len <= threshold but still prefilling) are counted as decodes. If False, they are counted as prefills.
Returns:
-
num_decodes(int) –The number of decode requests.
-
num_prefills(int) –The number of prefill requests.
-
num_decode_tokens(int) –The number of tokens in the decode requests.
-
num_prefill_tokens(int) –The number of tokens in the prefill requests.
Source code in vllm/v1/attention/backends/utils.py
split_decodes_prefills_and_extends(common_attn_metadata, decode_threshold=1) ¶
Assuming a reordered batch, finds the boundary between prefill and decode requests.
Parameters:
-
(common_attn_metadata¶CommonAttentionMetadata) –CommonAttentionMetadata object containing the batch metadata.
-
(decode_threshold¶int, default:1) –The maximum query length to be considered a decode.
Returns:
-
num_decodes(int) –The number of decode requests.
-
num_extends(int) –The number of extend requests.
-
num_prefills(int) –The number of prefill requests.
-
num_decode_tokens(int) –The number of tokens in the decode requests.
-
num_extend_tokens(int) –The number of tokens in the extend requests.
-
num_prefill_tokens(int) –The number of tokens in the prefill requests.
Source code in vllm/v1/attention/backends/utils.py
split_prefill_chunks(seq_lens_cpu, workspace_size, request_offset=0) ¶
Split the prefill requests into chunks such that the total sequence length of each chunk is less than or equal to the workspace size.
Parameters:
-
(seq_lens_cpu¶Tensor) –The sequence lengths of the prefill requests on CPU.
-
(workspace_size¶int) –The maximum workspace size (in tokens) per chunk.
-
(request_offset¶int, default:0) –The offset to add to the request indices.
Returns: A list of tuples of (reqs_start, reqs_end) representing chunk boundaries.
Source code in vllm/v1/attention/backends/utils.py
subclass_attention_metadata(name_prefix, metadata_cls, fields) ¶
Return a new subclass of metadata_cls with additional fields