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
_make_metadata_with_slice ¶
_make_metadata_with_slice(
ubatch_slice: UBatchSlice,
attn_metadata: CommonAttentionMetadata,
) -> CommonAttentionMetadata
This function creates a new CommonAttentionMetadata that corresponds to the requests included in ubatch_slice
Source code in vllm/v1/attention/backends/utils.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 | |
compute_causal_conv1d_metadata ¶
compute_causal_conv1d_metadata(query_start_loc_p: Tensor)
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
get_dcp_local_seq_lens ¶
get_dcp_local_seq_lens(
seq_lens: Tensor,
dcp_size: int = 1,
dcp_rank: int | None = None,
cp_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_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
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | |
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)
slice_query_start_locs ¶
Creates a new query_start_loc that corresponds to the requests in request_slice.
Note: This function creates a new tensor to hold the new query_start_locs. This will break cudagraph compatibility.
Source code in vllm/v1/attention/backends/utils.py
split_attn_metadata ¶
split_attn_metadata(
ubatch_slices: list[UBatchSlice],
common_attn_metadata: CommonAttentionMetadata,
) -> list[CommonAttentionMetadata]
Creates a new CommonAttentionMetadata instance that corresponds to the requests for each UBatchSlice in ubatch_slices.
Note: This function does not modify common_attn_metadata
Source code in vllm/v1/attention/backends/utils.py
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_backend ¶
subclass_attention_backend(
name_prefix: str,
attention_backend_cls: type[AttentionBackend],
builder_cls: type[AttentionMetadataBuilder[M]],
) -> type[AttentionBackend]
Return a new subclass where get_builder_cls returns builder_cls.
Source code in vllm/v1/attention/backends/utils.py
subclass_attention_backend_with_overrides ¶
subclass_attention_backend_with_overrides(
name_prefix: str,
attention_backend_cls: type[AttentionBackend],
overrides: dict[str, Any],
) -> type[AttentionBackend]
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