vllm.v1.attention.backends.utils ¶
_KV_CACHE_LAYOUT_OVERRIDE module-attribute ¶
_KV_CACHE_LAYOUT_OVERRIDE: KVCacheLayoutType | None = None
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
_fused_pcp_qkv_select_kernel ¶
_fused_pcp_qkv_select_kernel(
q_ptr,
q_stride_B,
q_stride_H,
k_ptr,
k_stride_B,
k_stride_H,
v_ptr,
v_stride_B,
v_stride_H,
query_start_ptr,
out_q_head_ptr,
out_q_tail_ptr,
out_k_head_ptr,
out_k_tail_ptr,
out_v_head_ptr,
out_v_tail_ptr,
pcp_world_size: constexpr,
pcp_rank: constexpr,
n_head: constexpr,
q_head_dim: constexpr,
k_head_dim: constexpr,
v_head_dim: constexpr,
SEQ_BLOCK_SIZE: constexpr,
DIM_BLOCK_SIZE: constexpr,
)
Source code in vllm/v1/attention/backends/utils.py
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 | |
compute_causal_conv1d_metadata ¶
Source code in vllm/v1/attention/backends/utils.py
create_fast_prefill_custom_backend ¶
create_fast_prefill_custom_backend(
prefix: str,
underlying_attn_backend: type[AttentionBackend],
) -> type[AttentionBackend]
Source code in vllm/v1/attention/backends/utils.py
extend_all_queries_by_1 ¶
extend_all_queries_by_1(
common_attn_metadata: CommonAttentionMetadata,
arange: Tensor,
new_slot_mapping: Tensor,
) -> CommonAttentionMetadata
Creates a new CommonAttentionMetadata with all query lengths increased by 1. Also all seq lens are increased by 1. This is useful e.g. in speculative decoding with draft models, where we extend each sequence by 1 token. The slot mapping is computed externally, as it requires more information.
Source code in vllm/v1/attention/backends/utils.py
fused_pcp_qkv_select ¶
fused_pcp_qkv_select(
q: Tensor,
k: Tensor,
v: Tensor,
query_start_loc: Tensor,
pcp_world_size: int,
pcp_rank: int,
)
Select the query and kv tensors for PCP. Instead of calling torch.index_select multiple times, this function fuses the selection for Q, K, and V into a single kernel to reduce kernel launch overhead. Args: q: query tensor on the current PCP rank. k: key tensor across PCP ranks. v: value tensor across PCP ranks. query_start_loc: start location of each query. pcp_world_size: number of PCP ranks. pcp_rank: rank of the current PCP rank. Returns: q_head: selected query tensor for pcp head. k_head: selected key tensor for pcp head. v_head: selected value tensor for pcp head. q_tail: selected query tensor for pcp tail. k_tail: selected key tensor for pcp tail. v_tail: selected value tensor for pcp tail.
Source code in vllm/v1/attention/backends/utils.py
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 | |
get_dcp_local_seq_lens ¶
get_dcp_local_seq_lens(
seq_lens: Tensor,
dcp_size: int = 1,
dcp_rank: int | None = None,
dcp_kv_cache_interleave_size: int = 1,
) -> Tensor
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_kv_cache_layout cached ¶
Source code in vllm/v1/attention/backends/utils.py
get_pcp_query_restore_idx ¶
Get restore index for PCP query splitting.
When queries are split into head/tail halves for PCP, this returns the argsort index to restore original order after processing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cu_num_tokens | Tensor | cumulative token counts, shape [num_reqs + 1] | required |
Returns:
| Name | Type | Description |
|---|---|---|
restore_idx | Tensor | tensor to reorder concatenated [head, tail] back to original |
Source code in vllm/v1/attention/backends/utils.py
get_per_layer_parameters ¶
get_per_layer_parameters(
vllm_config: VllmConfig,
layer_names: list[str],
cls_: type[AttentionImpl],
) -> dict[str, PerLayerParameters]
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 ¶
infer_global_hyperparameters(
per_layer_params: dict[str, PerLayerParameters],
) -> PerLayerParameters
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
is_valid_kv_cache_layout ¶
make_kv_sharing_fast_prefill_common_attn_metadata ¶
make_kv_sharing_fast_prefill_common_attn_metadata(
common_attn_metadata: CommonAttentionMetadata,
) -> CommonAttentionMetadata
Source code in vllm/v1/attention/backends/utils.py
make_local_attention_virtual_batches ¶
make_local_attention_virtual_batches(
attn_chunk_size: int,
common_attn_metadata: CommonAttentionMetadata,
block_size: int = 0,
) -> tuple[
CommonAttentionMetadata, Callable[[Tensor], Tensor]
]
Source code in vllm/v1/attention/backends/utils.py
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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
mamba_get_block_table_tensor ¶
mamba_get_block_table_tensor(
block_table: Tensor,
seq_lens: Tensor,
kv_cache_spec: KVCacheSpec,
mamba_cache_mode: str,
) -> Tensor
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)); output (#requests, cdiv(max_model_len, block_size)).
-
"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
pcp_kv_allgather_and_restore ¶
pcp_kv_allgather_and_restore(
key: Tensor,
value: Tensor,
num_actual_tokens: int,
pcp_allgather_restore_idx: Tensor,
pcp_group: GroupCoordinator,
)
All-gather key and value tensors across PCP ranks and restore the original order. Args: key: key tensor for the current pcp rank. value: value tensor for the current pcp rank. num_actual_tokens: number of actual tokens (Exclude graph padding tokens). pcp_allgather_restore_idx: indices to restore the original order. pcp_group: PCP group coordinator. Returns: key: all-gathered and restored key tensor. value: all-gathered and restored value tensor.
Source code in vllm/v1/attention/backends/utils.py
reorder_batch_to_split_decodes_and_prefills ¶
reorder_batch_to_split_decodes_and_prefills(
input_batch: InputBatch,
scheduler_output: SchedulerOutput,
decode_threshold: int = 1,
) -> bool
Reorders the batch to split into prefill and decode requests; places all requests with <= decode_threshold tokens at the front of the batch.
Returns:
| Type | Description |
|---|---|
bool | True if the batch was modified, False otherwise. |
Source code in vllm/v1/attention/backends/utils.py
reshape_attn_output_for_spec_decode ¶
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 ¶
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
set_kv_cache_layout ¶
set_kv_cache_layout(cache_layout: KVCacheLayoutType)
split_decodes_and_prefills ¶
split_decodes_and_prefills(
common_attn_metadata: CommonAttentionMetadata,
decode_threshold: int = 1,
require_uniform: bool = False,
) -> tuple[int, int, int, int]
Assuming a reordered batch, finds the boundary between prefill and decode requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
common_attn_metadata | CommonAttentionMetadata | CommonAttentionMetadata object containing the batch metadata. | required |
decode_threshold | int | The maximum query length to be considered a decode. | 1 |
require_uniform | bool | 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. | False |
Returns:
| Name | Type | Description |
|---|---|---|
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 ¶
split_decodes_prefills_and_extends(
common_attn_metadata: CommonAttentionMetadata,
decode_threshold: int = 1,
) -> tuple[int, int, int, int, int, int]
Assuming a reordered batch, finds the boundary between prefill and decode requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
common_attn_metadata | CommonAttentionMetadata | CommonAttentionMetadata object containing the batch metadata. | required |
decode_threshold | int | The maximum query length to be considered a decode. | 1 |
Returns:
| Name | Type | Description |
|---|---|---|
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 ¶
split_prefill_chunks(
seq_lens_cpu: Tensor,
workspace_size: int,
request_offset: int = 0,
) -> list[tuple[int, int]]
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:
| Name | Type | Description | Default |
|---|---|---|---|
seq_lens_cpu | Tensor | The sequence lengths of the prefill requests on CPU. | required |
workspace_size | int | The maximum workspace size (in tokens) per chunk. | required |
request_offset | int | The offset to add to the request indices. | 0 |
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 ¶
subclass_attention_metadata(
name_prefix: str,
metadata_cls: Any,
fields: list[tuple[str, Any, Any]],
) -> Any
Return a new subclass of metadata_cls with additional fields