vllm.attention.backends.xformers
Attention layer with xFormers and PagedAttention.
XFormersBackend
¶
Bases: AttentionBackend
Source code in vllm/attention/backends/xformers.py
copy_blocks
staticmethod
¶
get_builder_cls
staticmethod
¶
get_builder_cls() -> Type[XFormersMetadataBuilder]
get_impl_cls
staticmethod
¶
get_impl_cls() -> Type[XFormersImpl]
get_kv_cache_shape
staticmethod
¶
get_metadata_cls
staticmethod
¶
get_metadata_cls() -> Type[AttentionMetadata]
get_state_cls
staticmethod
¶
get_state_cls() -> Type[CommonAttentionState]
swap_blocks
staticmethod
¶
XFormersImpl
¶
Bases: AttentionImpl[XFormersMetadata]
If the input tensors contain prompt tokens, the layout is as follows:
|<--------------- num_prefill_tokens ----------------->|
|<--prefill_0-->|<--prefill_1-->|...|<--prefill_N-1--->|
Otherwise, the layout is as follows:
|<----------------- num_decode_tokens ------------------>|
|<--decode_0-->|..........|<--decode_M-1-->|<--padding-->|
Generation tokens can contain padding when cuda-graph is used. Currently, prompt tokens don't contain any padding.
The prompts might have different lengths, while the generation tokens always have length 1.
If chunked prefill is enabled, prefill tokens and decode tokens can be batched together in a flattened 1D query.
|<----- num_prefill_tokens ---->|<------- num_decode_tokens --------->| |<-prefill_0->|...|<-prefill_N-1->|<--decode_0-->|...|<--decode_M-1-->|
Currently, cuda graph is disabled for chunked prefill, meaning there's no padding between prefill and decode tokens.
Source code in vllm/attention/backends/xformers.py
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 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 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 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 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 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 |
|
__init__
¶
__init__(
num_heads: int,
head_size: int,
scale: float,
num_kv_heads: int,
alibi_slopes: Optional[List[float]],
sliding_window: Optional[int],
kv_cache_dtype: str,
blocksparse_params: Optional[Dict[str, Any]] = None,
logits_soft_cap: Optional[float] = None,
attn_type: str = DECODER,
kv_sharing_target_layer_name: Optional[str] = None,
use_irope: bool = False,
) -> None
Source code in vllm/attention/backends/xformers.py
_run_memory_efficient_xformers_forward
¶
_run_memory_efficient_xformers_forward(
query: Tensor,
key: Tensor,
value: Tensor,
attn_metadata: XFormersMetadata,
attn_type: str = DECODER,
) -> Tensor
Attention for 1D query of multiple prompts. Multiple prompt
tokens are flattened in to query
input.
See https://facebookresearch.github.io/xformers/components/ops.html for API spec.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output
|
shape = [num_prefill_tokens, num_heads, head_size] |
required | |
query
|
Tensor
|
shape = [num_prefill_tokens, num_heads, head_size] |
required |
key
|
Tensor
|
shape = [num_prefill_tokens, num_kv_heads, head_size] |
required |
value
|
Tensor
|
shape = [num_prefill_tokens, num_kv_heads, head_size] |
required |
attn_metadata
|
XFormersMetadata
|
Metadata for attention. |
required |
attn_type
|
str
|
Select attention type, between encoder attention, decoder self-attention, or encoder/decoder cross- attention. Defaults to decoder self-attention, which is the vLLM default generally |
DECODER
|
Source code in vllm/attention/backends/xformers.py
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 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 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 |
|
forward
¶
forward(
layer: AttentionLayer,
query: Tensor,
key: Optional[Tensor],
value: Optional[Tensor],
kv_cache: Tensor,
attn_metadata: XFormersMetadata,
output: Optional[Tensor] = None,
output_scale: Optional[Tensor] = None,
) -> Tensor
Forward pass with xFormers and PagedAttention.
For decoder-only models: query, key and value must be non-None.
For encoder/decoder models: * XFormersImpl.forward() may be invoked for both self- and cross- attention layers. * For self-attention: query, key and value must be non-None. * For cross-attention: * Query must be non-None * During prefill, key and value must be non-None; key and value get cached for use during decode. * During decode, key and value may be None, since: (1) key and value tensors were cached during prefill, and (2) cross-attention key and value tensors do not grow during decode
A note on how the attn_type (attention type enum) argument impacts attention forward() behavior:
* DECODER: normal decoder-only behavior;
use decoder self-attention block table
* ENCODER: no KV caching; pass encoder sequence
attributes (encoder_seq_lens/encoder_seq_lens_tensor/
max_encoder_seq_len) to kernel, in lieu of decoder
sequence attributes (seq_lens/seq_lens_tensor/max_seq_len).
Used for encoder branch of encoder-decoder models.
* ENCODER_ONLY: no kv_caching, uses the normal attention
attributes (seq_lens/seq_lens_tensor/max_seq_len).
* ENCODER_DECODER: cross-attention behavior;
use cross-attention block table for caching KVs derived
from encoder hidden states; since KV sequence lengths
will match encoder sequence lengths, pass encoder sequence
attributes to kernel (encoder_seq_lens/encoder_seq_lens_tensor/
max_encoder_seq_len)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
Tensor
|
shape = [num_tokens, num_heads * head_size] |
required |
key
|
Optional[Tensor]
|
shape = [num_tokens, num_kv_heads * head_size] |
required |
value
|
Optional[Tensor]
|
shape = [num_tokens, num_kv_heads * head_size] |
required |
attn_metadata
|
XFormersMetadata
|
Metadata for attention. |
required |
attn_type
|
Select attention type, between encoder attention, decoder self-attention, or encoder/decoder cross- attention. Defaults to decoder self-attention, which is the vLLM default generally |
required |
Returns: shape = [num_tokens, num_heads * head_size]
Source code in vllm/attention/backends/xformers.py
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 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 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 |
|
XFormersMetadata
dataclass
¶
Bases: AttentionMetadata
, PagedAttentionMetadata
Metadata for XFormersbackend.
NOTE: Any python object stored here is not updated when it is
cuda-graph replayed. If you have values that need to be changed
dynamically, it should be stored in tensor. The tensor has to be
updated from CUDAGraphRunner.forward
API.
Source code in vllm/attention/backends/xformers.py
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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
|
_cached_decode_metadata
class-attribute
instance-attribute
¶
_cached_decode_metadata: Optional[XFormersMetadata] = None
_cached_prefill_metadata
class-attribute
instance-attribute
¶
_cached_prefill_metadata: Optional[XFormersMetadata] = None
context_lens_tensor
class-attribute
instance-attribute
¶
encoder_seq_lens_tensor
class-attribute
instance-attribute
¶
encoder_seq_start_loc
class-attribute
instance-attribute
¶
is_all_cross_attn_metadata_set
property
¶
All attention metadata required for enc/dec cross-attention is set.
Superset of encoder attention required metadata.
is_all_encoder_attn_metadata_set
property
¶
All attention metadata required for encoder attention is set.
max_decode_query_len
class-attribute
instance-attribute
¶
__init__
¶
__init__(
seq_lens_tensor: Optional[Tensor],
max_decode_seq_len: int,
block_tables: Optional[Tensor],
num_prefills: int,
num_prefill_tokens: int,
num_decode_tokens: int,
slot_mapping: Tensor,
multi_modal_placeholder_index_maps: Optional[
Dict[str, IndexMap]
],
enable_kv_scales_calculation: bool,
max_prefill_seq_len: int,
use_cuda_graph: bool,
seq_lens: Optional[List[int]] = None,
seq_start_loc: Optional[Tensor] = None,
context_lens_tensor: Optional[Tensor] = None,
max_query_len: Optional[int] = None,
max_decode_query_len: Optional[int] = None,
query_start_loc: Optional[Tensor] = None,
_cached_prefill_metadata: Optional[
XFormersMetadata
] = None,
_cached_decode_metadata: Optional[
XFormersMetadata
] = None,
encoder_seq_lens: Optional[List[int]] = None,
encoder_seq_lens_tensor: Optional[Tensor] = None,
encoder_seq_start_loc: Optional[Tensor] = None,
max_encoder_seq_len: Optional[int] = None,
num_encoder_tokens: Optional[int] = None,
cross_slot_mapping: Optional[Tensor] = None,
cross_block_tables: Optional[Tensor] = None,
) -> None
__post_init__
¶
Source code in vllm/attention/backends/xformers.py
XFormersMetadataBuilder
¶
_get_attn_bias
¶
_get_attn_bias(
attn_metadata: XFormersMetadata, attn_type: str
) -> Optional[AttentionBias]
Extract appropriate attention bias from attention metadata according to attention type.
Arguments:
- attn_metadata: Attention metadata structure associated with attention
- attn_type: encoder attention, decoder self-attention, encoder/decoder cross-attention
Returns: * Appropriate attention bias value given the attention type
Source code in vllm/attention/backends/xformers.py
_make_alibi_bias
¶
_make_alibi_bias(
alibi_slopes: Tensor,
num_kv_heads: int,
dtype: dtype,
seq_lens: List[int],
) -> List[AttentionBias]
Source code in vllm/attention/backends/xformers.py
_set_attn_bias
¶
_set_attn_bias(
attn_metadata: XFormersMetadata,
attn_bias: List[Optional[AttentionBias]],
attn_type: str,
) -> None
Update appropriate attention bias field of attention metadata, according to attention type.
Arguments:
- attn_metadata: Attention metadata structure associated with attention
- attn_bias: The desired attention bias value
- attn_type: encoder attention, decoder self-attention, encoder/decoder cross-attention