vllm.v1.attention.backends.mla.flashmla_sparse ¶
MIN_HEADS_FOR_BF16_PREFILL module-attribute ¶
NOTE: FlashMLA Sparse uses an fp8 cache with the following format
In the "FP8 with scale" format, each token's KV cache is 656 Bytes, structured as: - First 512 bytes: The "quantized NoPE" part, containing 512 float8_e4m3 values. - Next 16 bytes: Scale factors, containing 4 float32 values. The first float32 is the scale for the first 128 float8_e4m3 values, the second for the next 128, and so on. - Last 128 bytes: The "RoPE" part, containing 64 bfloat16 values. This part is not quantized for accuracy.
FlashMLASparseBackend ¶
Bases: AttentionBackend
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
supported_kv_cache_dtypes class-attribute ¶
supported_kv_cache_dtypes: list[CacheDType] = [
"auto",
"fp8_ds_mla",
]
get_builder_cls staticmethod ¶
get_builder_cls() -> type[FlashMLASparseMetadataBuilder]
get_impl_cls staticmethod ¶
get_impl_cls() -> type[FlashMLASparseImpl]
get_kv_cache_shape staticmethod ¶
get_kv_cache_shape(
num_blocks: int,
block_size: int,
num_kv_heads: int,
head_size: int,
cache_dtype_str: str = "auto",
) -> tuple[int, ...]
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
get_supported_head_sizes classmethod ¶
get_supported_kernel_block_sizes staticmethod ¶
get_supported_kernel_block_sizes() -> list[
int | MultipleOf
]
supports_compute_capability classmethod ¶
supports_compute_capability(
capability: DeviceCapability,
) -> bool
FlashMLASparseImpl ¶
Bases: MLACommonBaseImpl[FlashMLASparseMetadata]
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
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 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 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 | |
prefill_workspace_shape instance-attribute ¶
__init__ ¶
__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,
topk_indice_buffer: Tensor | None = None,
indexer: Optional[Indexer] = None,
**mla_args,
) -> None
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
_bf16_flash_mla_kernel ¶
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
_forward_bf16_kv ¶
_forward_bf16_kv(
q: Tensor,
kv_c_and_k_pe_cache: Tensor,
topk_indices: Tensor,
attn_metadata: FlashMLASparseMetadata,
) -> Tensor
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
_forward_fp8_kv_mixed_batch ¶
_forward_fp8_kv_mixed_batch(
q: Tensor,
kv_c_and_k_pe_cache: Tensor,
topk_indices: Tensor,
attn_metadata: FlashMLASparseMetadata,
) -> Tensor
Mixed batch FP8 forward path that treats all tokens as one batch.
This is equivalent to main branch's approach and avoids the BF16 prefill kernel which has head padding overhead when num_heads is small. Used when use_mixed_batch is True.
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
_forward_fp8_kv_separate_prefill_decode ¶
_forward_fp8_kv_separate_prefill_decode(
q: Tensor,
kv_c_and_k_pe_cache: Tensor,
topk_indices: Tensor,
attn_metadata: FlashMLASparseMetadata,
) -> Tensor
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 | |
_fp8_flash_mla_kernel ¶
_fp8_flash_mla_kernel(
q: Tensor,
kv_c_and_k_pe_cache: Tensor,
topk_indices: Tensor,
kernel_metadata: FP8KernelMetadata,
) -> Tensor
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
forward ¶
forward(
layer: AttentionLayer,
q: Tensor,
k_c_normed: Tensor,
k_pe: Tensor,
kv_cache: Tensor,
attn_metadata: FlashMLASparseMetadata | None,
output: Tensor | None = None,
output_scale: Tensor | None = None,
output_block_scale: Tensor | None = None,
) -> Tensor
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
947 948 949 950 951 952 953 954 955 956 957 958 959 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 | |
FlashMLASparseMetadata dataclass ¶
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
fp8_extra_metadata class-attribute instance-attribute ¶
fp8_extra_metadata: (
FP8SeperatePrefillDecode | FP8KernelMetadata | None
) = None
FP8KernelMetadata dataclass ¶
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
FP8SeperatePrefillDecode dataclass ¶
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
Decode dataclass ¶
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
Prefill dataclass ¶
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
Chunk dataclass ¶
Metadata for a chunk of prefill requests.
Prefill requests may be chunked to fit within the fixed workspace size.
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
__init__ ¶
__init__(
num_reqs: int,
max_query_len: int,
max_seq_len: int,
num_actual_tokens: int,
query_start_loc: Tensor,
slot_mapping: Tensor,
block_table: Tensor,
req_id_per_token: Tensor,
block_size: int = 64,
topk_tokens: int = 2048,
fp8_extra_metadata: FP8SeperatePrefillDecode
| FP8KernelMetadata
| None = None,
fp8_use_mixed_batch: bool = False,
) -> None
FlashMLASparseMetadataBuilder ¶
Bases: AttentionMetadataBuilder[FlashMLASparseMetadata]
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
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 | |
dummy_block_table instance-attribute ¶
dummy_block_table = empty(
(max_num_seqs, 1), dtype=int32, device=device
)
max_model_len_tensor instance-attribute ¶
max_model_len_tensor = full(
(max_num_seqs,),
max_model_len,
device=device,
dtype=int32,
)
num_splits_buffer instance-attribute ¶
num_splits_buffer = empty(
(max_num_seqs + 1,), dtype=int32, device=device
)
req_id_per_token_buffer instance-attribute ¶
req_id_per_token_buffer = empty(
(max_num_batched_tokens,), dtype=int32, device=device
)
tile_scheduler_metadata_buffer instance-attribute ¶
tile_scheduler_metadata_buffer = empty(
(max_num_sm_parts, 8), dtype=int32, device=device
)
topk_tokens_tensor instance-attribute ¶
topk_tokens_tensor = full(
(max_num_seqs,), topk_tokens, device=device, dtype=int32
)
__init__ ¶
__init__(
kv_cache_spec: AttentionSpec,
layer_names: list[str],
vllm_config: VllmConfig,
device: device,
) -> None
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
_build_fp8_mixed_decode_prefill ¶
_build_fp8_mixed_decode_prefill(
common_attn_metadata: CommonAttentionMetadata,
) -> FP8KernelMetadata
Build FP8 metadata treating all tokens as one mixed batch.
This matches main branch's approach and avoids the BF16 prefill kernel which has head padding overhead when num_heads is small (high TP case).
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
_build_fp8_separate_prefill_decode ¶
_build_fp8_separate_prefill_decode(
common_attn_metadata: CommonAttentionMetadata,
) -> FP8SeperatePrefillDecode
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
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 | |
build ¶
build(
common_prefix_len: int,
common_attn_metadata: CommonAttentionMetadata,
fast_build: bool = False,
) -> FlashMLASparseMetadata
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
_convert_req_index_to_global_index_kernel ¶
_convert_req_index_to_global_index_kernel(
req_id_ptr,
block_table_ptr,
token_indices_ptr,
out_ptr,
prefill_request_id_ptr,
workspace_starts_ptr,
max_num_blocks_per_req: constexpr,
BLOCK_SIZE: constexpr,
BLOCK_N: constexpr,
HAS_PREFILL: constexpr,
bt_stride0,
bt_stride1,
ti_stride0,
ti_stride1,
out_stride0,
out_stride1,
)
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
get_prefill_workspace_size ¶
get_prefill_workspace_size(max_model_len: int)
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
triton_convert_req_index_to_global_index ¶
triton_convert_req_index_to_global_index(
req_id: Tensor,
block_table: Tensor,
token_indices: Tensor,
BLOCK_SIZE: int = 64,
NUM_TOPK_TOKENS: int = 2048,
BLOCK_N: int = 128,
HAS_PREFILL_WORKSPACE: bool = False,
prefill_workspace_request_ids: Tensor | None = None,
prefill_workspace_starts: Tensor | None = None,
)
out[token_id, indice_id] = block_table[req_id[token_id], token_indices[token_id, indice_id] // BLOCK_SIZE] * BLOCK_SIZE + token_indices[token_id, indice_id] % BLOCK_SIZE
Only when token_indices[token_id, indice_id] == -1 do we output -1. For safety, we also output -1 if the derived block_id would be out-of-bounds.
When HAS_PREFILL_WORKSPACE is True, prefill tokens are mapped to workspace offsets instead of global cache slots. prefill_workspace_request_ids and prefill_workspace_starts must be provided.
int32 [num_tokens], -1 for decode else
prefill request index (maps to prefill_workspace_starts)
prefill_workspace_starts: int32 [num_prefills], 0-indexed workspace starts for each prefill request
Source code in vllm/v1/attention/backends/mla/flashmla_sparse.py
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 | |