vllm.v1.attention.backend ¶
AttentionBackend ¶
Bases: ABC
Abstract class for attention backends.
Source code in vllm/v1/attention/backend.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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 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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
full_cls_name classmethod ¶
get_builder_cls abstractmethod staticmethod ¶
get_impl_cls abstractmethod staticmethod ¶
get_impl_cls() -> type[AttentionImpl]
get_kv_cache_shape abstractmethod staticmethod ¶
get_kv_cache_stride_order staticmethod ¶
Get the physical (memory layout) ordering of the kv cache dimensions. e.g. if the KV cache shape is [2, num_blocks, block_size, num_heads, head_size], and get_kv_cache_stride_order returns (1, 3, 0, 2, 4) then the physical ordering of dimensions is [num_blocks, num_heads, 2, block_size, head_size].
If this function is unimplemented / raises NotImplementedError, the physical layout of the KV cache will match the logical shape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_num_layers_dimension | bool | if True, includes an additional num_layers dimension, which is assumed to be prepended to the logical KV cache shape. With the above example, a return value (2, 4, 0, 1, 3, 5) corresponds to [num_blocks, num_heads, num_layers, 2, block_size, head_size]. If an additional dimension is NOT included in the returned tuple, the physical layout will not include a layers dimension. | False |
Returns:
| Type | Description |
|---|---|
tuple[int, ...] | A tuple of ints which is a permutation of range(len(shape)). |
Source code in vllm/v1/attention/backend.py
get_required_kv_cache_layout classmethod ¶
get_required_kv_cache_layout() -> KVCacheLayoutType | None
get_supported_head_sizes classmethod ¶
get_supported_kernel_block_sizes staticmethod ¶
get_supported_kernel_block_sizes() -> list[
int | MultipleOf
]
supports_attn_type classmethod ¶
Check if backend supports a given attention type.
By default, only supports decoder attention. Backends should override this to support other attention types.
Source code in vllm/v1/attention/backend.py
supports_block_size classmethod ¶
Source code in vllm/v1/attention/backend.py
supports_combination classmethod ¶
supports_combination(
head_size: int,
dtype: dtype,
kv_cache_dtype: CacheDType | None,
block_size: int,
use_mla: bool,
has_sink: bool,
use_sparse: bool,
device_capability: DeviceCapability,
) -> str | None
Source code in vllm/v1/attention/backend.py
supports_compute_capability classmethod ¶
supports_compute_capability(
capability: DeviceCapability,
) -> bool
supports_dtype classmethod ¶
supports_head_size classmethod ¶
supports_kv_cache_dtype classmethod ¶
supports_kv_cache_dtype(
kv_cache_dtype: CacheDType | None,
) -> bool
validate_configuration classmethod ¶
validate_configuration(
head_size: int,
dtype: dtype,
kv_cache_dtype: CacheDType | None,
block_size: int,
use_mla: bool,
has_sink: bool,
use_sparse: bool,
use_mm_prefix: bool,
device_capability: DeviceCapability,
attn_type: str,
) -> list[str]
Source code in vllm/v1/attention/backend.py
AttentionCGSupport ¶
Bases: Enum
Constants for the cudagraph support of the attention backend Here we do not consider the cascade attention, as currently it is never cudagraph supported.
Source code in vllm/v1/attention/backend.py
ALWAYS class-attribute instance-attribute ¶
Cudagraph always supported; supports mixed-prefill-decode
UNIFORM_BATCH class-attribute instance-attribute ¶
Cudagraph supported for batches the only contain query lengths that are the same, this can be used for spec-decode i.e. "decodes" are 1 + num_speculative_tokens
UNIFORM_SINGLE_TOKEN_DECODE class-attribute instance-attribute ¶
Cudagraph supported for batches the only contain query_len==1 decodes
AttentionImpl ¶
Source code in vllm/v1/attention/backend.py
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 | |
can_return_lse_for_decode class-attribute instance-attribute ¶
can_return_lse_for_decode: bool = False
need_to_return_lse_for_decode class-attribute instance-attribute ¶
need_to_return_lse_for_decode: bool = False
supports_mtp_with_cp_non_trivial_interleave_size class-attribute instance-attribute ¶
supports_mtp_with_cp_non_trivial_interleave_size: bool = (
False
)
supports_quant_query_input class-attribute instance-attribute ¶
supports_quant_query_input: bool = False
__init__ abstractmethod ¶
__init__(
num_heads: int,
head_size: int,
scale: float,
num_kv_heads: int | None = None,
alibi_slopes: list[float] | None = None,
sliding_window: int | None = None,
kv_cache_dtype: str = "auto",
logits_soft_cap: float | None = None,
attn_type: str = DECODER,
kv_sharing_target_layer_name: str | None = None,
) -> None
Source code in vllm/v1/attention/backend.py
__new__ ¶
Source code in vllm/v1/attention/backend.py
forward abstractmethod ¶
forward(
layer: AttentionLayer,
query: Tensor,
key: Tensor,
value: Tensor,
kv_cache: Tensor,
attn_metadata: T,
output: Tensor | None = None,
output_scale: Tensor | None = None,
output_block_scale: Tensor | None = None,
) -> Tensor
Source code in vllm/v1/attention/backend.py
fused_output_quant_supported ¶
fused_output_quant_supported(quant_key: QuantKey)
Does this attention implementation support fused output quantization. This is used by the AttnFusionPass to only fuse output quantization onto implementations that support it.
:param quant_key: QuantKey object that describes the quantization op :return: is fusion supported for this type of quantization
Source code in vllm/v1/attention/backend.py
AttentionLayer ¶
Bases: Protocol
Source code in vllm/v1/attention/backend.py
AttentionMetadata ¶
AttentionMetadataBuilder ¶
Source code in vllm/v1/attention/backend.py
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 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 | |
reorder_batch_threshold class-attribute instance-attribute ¶
reorder_batch_threshold: int | None = None
supports_update_block_table class-attribute instance-attribute ¶
supports_update_block_table: bool = False
__init__ abstractmethod ¶
__init__(
kv_cache_spec: AttentionSpec,
layer_names: list[str],
vllm_config: VllmConfig,
device: device,
)
Source code in vllm/v1/attention/backend.py
_init_reorder_batch_threshold ¶
_init_reorder_batch_threshold(
reorder_batch_threshold: int | None = 1,
supports_spec_as_decode: bool = False,
supports_dcp_with_varlen: bool = False,
) -> None
Source code in vllm/v1/attention/backend.py
build abstractmethod ¶
build(
common_prefix_len: int,
common_attn_metadata: CommonAttentionMetadata,
fast_build: bool = False,
) -> M
Central method that builds attention metadata. Some builders (MLA) require reorder_batch to be called prior to build.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
common_prefix_len | int | The length of the common prefix of the batch. | required |
common_attn_metadata | CommonAttentionMetadata | The common attention metadata. | required |
fast_build | bool | The meta-data will prioritize speed of building over then speed at execution. Can be used for spec-decode where the result of a build call may only be used for few layers/iters. | False |
Source code in vllm/v1/attention/backend.py
build_for_cudagraph_capture ¶
build_for_cudagraph_capture(
common_attn_metadata: CommonAttentionMetadata,
) -> M
Build attention metadata for CUDA graph capture. Uses build by default. Subclasses that override this method should call self.build or super().build_for_cudagraph_capture.
Source code in vllm/v1/attention/backend.py
build_for_drafting ¶
build_for_drafting(
common_attn_metadata: CommonAttentionMetadata,
draft_index: int,
) -> M
Build attention metadata for draft model. Uses build by default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
common_attn_metadata | CommonAttentionMetadata | The common attention metadata. | required |
draft_index | int | The index of the current draft operation. When speculating a chain of tokens, this index refers to the draft attempt for the i-th token. For tree-based attention, this index instead refers to the draft attempt for the i-th level in the tree of tokens. | required |
Source code in vllm/v1/attention/backend.py
get_cudagraph_support classmethod ¶
get_cudagraph_support(
vllm_config: VllmConfig, kv_cache_spec: AttentionSpec
) -> AttentionCGSupport
Get the cudagraph support level of this builder class.
Source code in vllm/v1/attention/backend.py
update_block_table ¶
Update the block table for the attention metadata. Faster when theres multiple kv-cache groups that create virtually the same metadata but just with different block tables.
Only needs to be implemented if supports_update_block_table is True.
Source code in vllm/v1/attention/backend.py
AttentionType ¶
Attention type. Use string to be compatible with torch.compile.
Source code in vllm/v1/attention/backend.py
DECODER class-attribute instance-attribute ¶
Decoder attention between previous layer Q/K/V.
ENCODER class-attribute instance-attribute ¶
Encoder attention between previous layer Q/K/V for encoder-decoder.
ENCODER_DECODER class-attribute instance-attribute ¶
Attention between dec. Q and enc. K/V for encoder-decoder.
ENCODER_ONLY class-attribute instance-attribute ¶
Encoder attention between previous layer Q/K/V.
CommonAttentionMetadata dataclass ¶
Per-batch attention metadata, shared across layers and backends. AttentionMetadataBuilder instances use it to construct per-layer metadata.
For many of the tensors we keep both GPU and CPU versions.
Source code in vllm/v1/attention/backend.py
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 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 | |
_num_computed_tokens_cache class-attribute instance-attribute ¶
_num_computed_tokens_cache: Tensor | None = None
_num_computed_tokens_cpu class-attribute instance-attribute ¶
_num_computed_tokens_cpu: Tensor | None = None
dcp_local_seq_lens_cpu class-attribute instance-attribute ¶
dcp_local_seq_lens_cpu: Tensor | None = None
Sequence lengths of the local rank in decode context parallelism world
encoder_seq_lens_cpu class-attribute instance-attribute ¶
encoder_seq_lens_cpu: ndarray | None = None
logits_indices_padded class-attribute instance-attribute ¶
logits_indices_padded: Tensor | None = None
query_start_loc_cpu instance-attribute ¶
query_start_loc_cpu: Tensor
(batch_size + 1,), the start location of each request in query Tensor
seq_lens instance-attribute ¶
seq_lens: Tensor
(batch_size,), the number of computed tokens for each request
__init__ ¶
__init__(
query_start_loc: Tensor,
query_start_loc_cpu: Tensor,
seq_lens: Tensor,
num_reqs: int,
num_actual_tokens: int,
max_query_len: int,
max_seq_len: int,
block_table_tensor: Tensor,
slot_mapping: Tensor,
causal: bool = True,
logits_indices_padded: Tensor | None = None,
num_logits_indices: int | None = None,
encoder_seq_lens: Tensor | None = None,
encoder_seq_lens_cpu: ndarray | None = None,
dcp_local_seq_lens: Tensor | None = None,
dcp_local_seq_lens_cpu: Tensor | None = None,
_seq_lens_cpu: Tensor | None = None,
_num_computed_tokens_cpu: Tensor | None = None,
_num_computed_tokens_cache: Tensor | None = None,
) -> None
compute_num_computed_tokens ¶
compute_num_computed_tokens() -> Tensor
Compute num_computed_tokens on device (seq_lens - query_lens).
Source code in vllm/v1/attention/backend.py
unpadded ¶
unpadded(
num_actual_tokens: int, num_actual_reqs: int
) -> CommonAttentionMetadata
Source code in vllm/v1/attention/backend.py
MLAAttentionImpl ¶
Bases: AttentionImpl[T], Generic[T]
Source code in vllm/v1/attention/backend.py
__init__ abstractmethod ¶
__init__(
num_heads: int,
head_size: int,
scale: float,
num_kv_heads: int,
alibi_slopes: list[float] | None,
sliding_window: int | None,
kv_cache_dtype: str,
logits_soft_cap: float | None,
attn_type: str,
kv_sharing_target_layer_name: str | None,
q_lora_rank: int | None,
kv_lora_rank: int,
qk_nope_head_dim: int,
qk_rope_head_dim: int,
qk_head_dim: int,
v_head_dim: int,
kv_b_proj: ColumnParallelLinear,
indexer: object | None = None,
) -> None
Source code in vllm/v1/attention/backend.py
forward abstractmethod ¶
forward(
layer: AttentionLayer,
hidden_states_or_cq: Tensor,
kv_c_normed: Tensor,
k_pe: Tensor,
kv_cache: Tensor,
attn_metadata: T,
output: Tensor | None = None,
output_scale: Tensor | None = None,
output_block_scale: Tensor | None = None,
) -> Tensor
Source code in vllm/v1/attention/backend.py
MultipleOf ¶
Source code in vllm/v1/attention/backend.py
is_quantized_kv_cache ¶
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/backend.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]