vllm.v1.worker.utils ¶
Classes:
-
EncoderTimingStats–Per-request timing statistics for encoder forward pass.
-
KVBlockZeroer–Manages efficient zeroing of KV cache blocks via a Triton kernel.
Functions:
-
add_kv_sharing_layers_to_kv_cache_groups–Sets up KV cache sharing by reusing the allocated KV caches in
kv_caches -
allocate_kv_cache–Allocate the KV cache and view it as
[B, H, N, C]per layer. -
bind_kv_cache–Bind the allocated KV cache to both ModelRunner and forward context so
-
get_uniform_decode_token_count–Per-request token count of a uniform decode batch, or None.
-
is_residual_scattered_for_sp–Check if the residual tensor is scattered for sequence parallelism.
-
is_uniform_query_len–Whether every request in the batch has the same query length.
-
prepare_kernel_block_sizes–Generate kernel_block_sizes that matches each block_size.
-
request_memory–Calculate the amount of memory required by vLLM, then validate
-
sanity_check_mm_encoder_outputs–Perform sanity checks for the result of
-
select_common_block_size–Select a block size that is supported by all backends and is a factor of
EncoderTimingStats dataclass ¶
Per-request timing statistics for encoder forward pass.
Attributes:
-
encoder_forward_secs(float) –Time spent in vision encoder forward pass (seconds).
-
num_encoder_calls(int) –Number of times encoder was called for this request.
Source code in vllm/v1/worker/utils.py
KVBlockZeroer ¶
Manages efficient zeroing of KV cache blocks via a Triton kernel.
Construct once after KV caches are allocated to precompute segment addresses, then call :meth:zero_block_ids each step to zero newly-allocated blocks.
Methods:
-
__init__–Precompute the absolute-address table for the Triton zeroing kernel.
-
warmup–JIT-compile the zeroing kernel before the first real request.
-
zero_block_ids–Zero the KV cache memory for the given block IDs.
Source code in vllm/v1/worker/utils.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | |
__init__(device, attn_groups_iter, kernel_block_sizes, static_forward_context, runner_only_attn_layers=None) ¶
Precompute the absolute-address table for the Triton zeroing kernel.
Each entry is the absolute byte address of a segment start on the GPU, so segments in different CUDA allocations work correctly.
Per-layer views are standardized [B, H, N, C] with blocks at dim 0; dims physically outside B (separate head groups under LHBNC) each get their own segment. A segment's page spans everything inside its block, so under BHLNC it also covers the block's other layers -- safe, since block IDs are global pool indices and a newly allocated block owns its whole tile.
Block IDs from the scheduler reference logical blocks whose size may differ from the kernel block size (virtual block splitting). Each virtual block is represented as an independent segment so its physical block stride and zeroed page span remain independent.
Only AttentionSpec layers are processed; Mamba layers are skipped.
Source code in vllm/v1/worker/utils.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
warmup(num_kv_blocks) ¶
zero_block_ids(block_ids) ¶
Zero the KV cache memory for the given block IDs.
Source code in vllm/v1/worker/utils.py
_zero_kv_blocks_kernel(seg_addrs_ptr, seg_block_strides_ptr, seg_page_sizes_ptr, block_ids_ptr, BLOCK_SIZE) ¶
Zero KV cache blocks across all segments in a single launch.
Each segment is a contiguous region of one block's data. Layer-compact layouts have one segment per layer buffer; dimensions physically outside the block dim (separate head groups under LHBNC) and virtual block splits each get their own segment.
Segments may have different block strides and page sizes (e.g. packed KV views or models with multiple KV cache groups like MLA + DSA indexer). Each segment's block stride determines where a logical block begins, while its page size determines how many elements are cleared.
seg_addrs_ptr holds absolute byte addresses (int64) for each segment, allowing segments to live in different CUDA allocations.
Programs are mapped directly onto a 3-D grid as (block_index, seg_index, chunk_index).
Source code in vllm/v1/worker/utils.py
add_kv_sharing_layers_to_kv_cache_groups(shared_kv_cache_layers, kv_cache_groups, runner_only_attn_layers=None) ¶
Sets up KV cache sharing by reusing the allocated KV caches in kv_caches for layers that do not allocate its own KV cache, based on the mapping in shared_kv_cache_layers. Adds these layers to the corresponding KV cache group, which is needed to ensure that attention metadata is assigned later.
Parameters:
-
(shared_kv_cache_layers¶dict[str, str]) –Layer pairings for cross-layer KV sharing. If an Attention layer
layer_nameis in the keys of this dict, it means this layer will perform attention using the keys and values from the KV cache ofshared_kv_cache_layers[layer_name]. -
(kv_cache_groups¶list[KVCacheGroupSpec]) –The KV cache groups of the model.
Source code in vllm/v1/worker/utils.py
allocate_kv_cache(kv_cache_config, device, layout, kernel_block_sizes=None) ¶
Allocate the KV cache and view it as [B, H, N, C] per layer.
Every KVCacheTensor places its layers in the same backing allocation: layer l of block b starts at offset + l * layer_stride + b * block_stride. Cache groups overlay each other, so tensors may address the same bytes.
Source code in vllm/v1/worker/utils.py
bind_kv_cache(kv_caches, forward_context, runner_kv_caches, num_attn_module=1) ¶
Bind the allocated KV cache to both ModelRunner and forward context so that the KV cache can be used in the forward pass.
This function
1) Fills the ModelRunner's kv cache list (runner_kv_caches) with kv_caches. 2) Associates each attention layer in the forward_context with its corresponding KV cache in kv_caches.
Parameters:
-
(kv_caches¶dict[str, Tensor]) –The allocated kv_caches with layer names as keys.
-
(forward_context¶dict[str, Attention]) –The global forward context containing all Attention layers with layer names as keys.
-
(runner_kv_caches¶list[Tensor]) –The kv_cache declared by ModelRunner.
Source code in vllm/v1/worker/utils.py
get_uniform_decode_token_count(num_reqs, num_tokens, max_query_len, has_prefill) ¶
Per-request token count of a uniform decode batch, or None.
Source code in vllm/v1/worker/utils.py
is_residual_scattered_for_sp(vllm_config, num_input_tokens) ¶
Check if the residual tensor is scattered for sequence parallelism.
The residual tensor is scattered across tensor parallel ranks when sequence parallelism and tensor parallelism is enabled. SP is only supported in full-graph compilation mode.
Source code in vllm/v1/worker/utils.py
is_uniform_query_len(num_reqs, num_tokens, max_query_len) ¶
Whether every request in the batch has the same query length.
Shape test only; use get_uniform_decode_token_count to classify a scheduled batch, since a prompt chunk can have a decode batch's shape.
Source code in vllm/v1/worker/utils.py
prepare_kernel_block_sizes(kv_cache_config, attn_groups) ¶
Generate kernel_block_sizes that matches each block_size.
For attention backends that support virtual block splitting, use the supported block sizes from the backend. For other backends (like Mamba), use the same block size (no splitting).
Parameters:
-
(kv_cache_config¶KVCacheConfig) –The KV cache configuration.
-
(attn_groups¶list[list[AttentionGroup]]) –Attention groups indexed by KV cache group id.
Returns:
Source code in vllm/v1/worker/utils.py
request_memory(init_snapshot, cache_config) ¶
Calculate the amount of memory required by vLLM, then validate that the current amount of free memory is sufficient for that.
Source code in vllm/v1/worker/utils.py
sanity_check_mm_encoder_outputs(mm_embeddings, expected_num_items) ¶
Perform sanity checks for the result of vllm.model_executor.models.SupportsMultiModal.embed_multimodal.
Source code in vllm/v1/worker/utils.py
select_common_block_size(kv_manager_block_size, backends) ¶
Select a block size that is supported by all backends and is a factor of kv_manager_block_size.
If kv_manager_block_size is supported by all backends, return it directly. Otherwise, return the max supported size.
Parameters:
-
(kv_manager_block_size¶int) –Block size of KV cache.
-
(backends¶list[type[AttentionBackend]]) –List of attention backend classes.
Returns:
-
int–The selected block size.
Raises:
-
ValueError–If no valid block size found.