vllm.model_executor.layers.attention.mla_attention ¶
MLA Common Components¶
This file implements common components for MLA implementations.
First we define:
Sq as Q sequence length Skv as KV sequence length
MLA has two possible ways of computing, a data-movement friendly approach and a compute friendly approach. We generally want to use the compute friendly approach for "prefill" (i.e. the ratio Sq / Skv is relatively large, often near 1) and the data-movement friendly approach for "decode" (i.e. the ratio Sq / Skv is small, often near 0).
NOTE what we deem small and large is currently determined by if it is labelled prefill or decode by the scheduler, but this is something we should probably tune.
Main reference: DeepseekV2 paper, and FlashInfer Implementation (https://arxiv.org/abs/2405.04434 and https://github.com/flashinfer-ai/flashinfer/pull/551).
Deepseek's MLA attention works the following way: * Use a single latent vector to represent the per-token entry of the KV cache. * For decode (i.e. the memory friendly approach) the attention "simulates" a multi-head attention, while the compute is similar to multi-query attention.
Below is an example of both paths assuming batch size = 1
More Extent Definitions:¶
C Context length, Skv - Sq H hidden size N number of attention heads Lq latent dimension for Q 1536 in DSV3 Lkv latent dimension for K/V 512 in DSV3 P nope dimension, no rope. 128 in DSV3 R rope dimension, goes through rope. 64 in DSV3 V V head dim. 128 in DSV3
Vector/Matrix Definitions¶
h_t hidden states (input to attention) shape [Sq, H] q_c latent/compressed Q shape [Sq, Lq] q_nope uncompressed Q (no-rope) shape [Sq, N, P] q_pe uncompressed Q (rope) shape [Sq, N, R] kv_c latent/compressed KV shape [Skv, Lkv] k_pe decoupled k position embeddings shape [Skv, R] new_kv_c new kv_c from current iter shape [Sq, Lkv] new_k_pe new k_pe from current iter shape [Sq, R] cache_kv_c cached k_c from previous iters shape [C, Lkv] cache_k_pe cached k_pe from previous iters shape [C, R] W_DQ project h_t to q_c shape [H, Lq] W_UQ project q_c to q_nope shape [Lq, N * P] W_QR project q_c to q_pe shape [Lq, N * R] W_DKV project h_t to kv_c shape [H, Lkv] W_UK project kv_c to k_nope shape [Lkv, N, P] W_KR project h_t to k_pe shape [H, R] W_UV project kv_c to v shape [Lkv, N, V] W_O project v to h_t shape [N * V, H]
Compute Friendly Approach (i.e. "forward_mha"):¶
q_c = h_t @ W_DQ q_nope = (q_c @ W_UQ).view(Sq, N, P) q_pe = RoPE(q_c @ W_QR).view(Sq, N, R) new_kv_c = h_t @ W_DKV new_k_pe = RoPE(h_t @ W_KR) kv_c = torch.cat([new_kv_c, cache_kv_c], dim=0) k_pe = torch.cat([new_k_pe, cache_k_pe], dim=0) k_nope = (kv_c @ W_UK.view(Lkv, N * P)).view(Skv, N, P) v = (kv_c @ W_UV.view(Lkv, N * V)).view(Skv, N, V)
// MHA with QK headdim = P + R // V headdim = V // sdpa_o shape [Sq, N, V] sdpa_o = scaled_dot_product_attention( torch.cat([q_nope, q_pe], dim=-1), torch.cat([k_nope, k_pe.unsqueeze(1).expand(-1, N, -1)], dim=-1), v ) return sdpa_o @ W_O
in the actual code,
kv_b_proj is [W_UK; W_UV] concatenated per head q_b_proj is [W_UQ; W_QR] concatenated per head out_proj is W_O
Data-Movement Friendly Approach (i.e. "forward_mqa"):¶
Runtime q_c = h_t @ W_DQ q_nope = (q_c @ W_UQ).view(-1, N, P) ql_nope = einsum("snh,lnh->snl", q_nope, W_UK) q_pe = RoPE(q_c @ W_QR).view(Sq, N, R) new_kv_c = h_t @ W_DKV new_k_pe = RoPE(h_t @ W_KR) kv_c = torch.cat([new_kv_c, cache_kv_c], dim=0) k_pe = torch.cat([new_k_pe, cache_k_pe], dim=0)
// MQA with QK headdim = Lkv + R // V headdim = Lkv // sdpa_o shape [Sq, N, Lkv] // NOTE: this is less compute-friendly since Lkv > P // but is more data-movement friendly since its MQA vs MHA sdpa_o = scaled_dot_product_attention( torch.cat([ql_nope, q_pe], dim=-1), torch.cat([kv_c, k_pe], dim=-1), kv_c )
o = einsum("snl,lnv->snv", sdpa_o.reshape(-1, N, Lkv), W_UV) return o.view(-1, N * V) @ W_O
Chunked Prefill¶
For chunked prefill we want to use the compute friendly algorithm. We are assuming sufficiently large Sq / Skv ratio, in the future may want to switch to the data-movement friendly approach if the chunk (i.e. Sq) is small.
However, the compute-friendly approach can potentially run out of memory if Skv is large due to: k_nope = (kv_c @ W_UK).view(Skv, N, P)
To mitigate this, we chunk the computation of attention with respect to the current context (i.e. cache_kv_c and cache_k_pe) so that we can used a fixed workspace size.
The chunked prefill approach is as follows:
W Workspace rows, i.e. the context rows we may gather at once, used to bound the memory usage
The context is scheduled per request: requests are packed in order, splitting the next request on an aligned boundary when needed to fill W. So a chunk covers a contiguous run of prefills and only ever the tokens and context rows of those prefills — attention, up-projection and merging are charged only to the requests it covers, and no chunk contains an empty context span. See plan_mla_context_chunks.
Only a chunk's first request can be a continuation, so accumulation performs at most one request-slice merge and one bulk write per chunk.
q_c = h_t @ W_DQ q_nope = (q_c @ W_UQ).view(Sq, N, P) q_pe = RoPE(q_c @ W_QR).view(Sq, N, R) new_kv_c = h_t @ W_DKV new_k_pe = RoPE(h_t @ W_KR) new_k_nope = (new_kv_c @ W_UK.view(Lkv, N * P)).view(Sq, N, P) new_v = (new_kv_c @ W_UV.view(Lkv, N * V)).view(Sq, N, V)
// MHA between queries and new KV // with QK headdim = P + R // V headdim = V // curr_o shape [Sq, N, V] // curr_lse shape [N, Sq], this is just order FA returns curr_o, curr_lse = scaled_dot_product_attention( torch.cat([q_nope, q_pe], dim=-1), torch.cat([new_k_nope, new_k_pe.unsqueeze(1).expand(-1, N, -1)], dim=-1), new_v, causal=True, return_softmax_lse=True )
// Compute attention with the already existing context. Shown for a single // request; with a batch, a chunk covers a run of requests and both q and the // gathered context below are sliced to that run. for chunk_idx in range(cdiv(C, W)): chunk_start = chunk_idx * W chunk_end = min(chunk_start + W, C) Sc = chunk_end - chunk_start cache_kv_c_chunk = cache_kv_c[chunk_start:chunk_end] cache_k_pe_chunk = cache_k_pe[chunk_start:chunk_end] cache_k_nope_chunk = (cache_kv_c_chunk @ W_UK).view(-1, N, P) cache_v_chunk = (cache_kv_c_chunk @ W_UV).view(-1, N, V)
chunk_o, chunk_lse = scaled_dot_product_attention(
torch.cat([q_nope, q_pe], dim=-1),
torch.cat([cache_k_nope_chunk,
cache_k_pe_chunk.unsqueeze(1).expand(-1, N, -1)],
dim=-1),
cache_v_chunk,
causal=False,
return_softmax_lse=True
)
curr_o, curr_lse = merge_attn_states(
suffix_output=curr_o,
suffix_lse=curr_lse,
prefix_output=chunk_o,
prefix_lse=chunk_lse,
)
return curr_o @ W_O
Classes:
-
MLAAttention–Multi-Head Latent Attention layer.
-
MLACommonBackend– -
MLACommonBaseImpl–Shared MLA base providing dense-MHA prefill (via the selected
-
MLACommonImpl–NOTE: Please read the comment at the top of the file before trying to
-
MLACommonMetadata–Metadata for MLACommon.
-
MLACommonMetadataBuilder–NOTE: Please read the comment at the top of the file before trying to
-
MLACommonPrefillMetadata–Prefill Specific Metadata
-
QueryLenSupport–Defines the level of query length support for an attention backend's
Functions:
-
accumulate_mla_context_chunk–Fold one chunk's partial into the running context partial.
-
backend_supports_prefill_query_quantization–Check if the selected MLA prefill backend supports query quantization.
-
build_mla_chunked_context_metadata–Build chunked-context metadata for an MLA prefill.
-
init_mla_context_partial–Allocate the running context partial over all prefill tokens.
-
neutralize_empty_context_partials–Neutralize the partial of every prefill that no chunk covers.
-
plan_mla_context_chunks–Pack per-request contexts into workspace-sized chunks.
-
reorg_kvcache–reorg and unpad kvcache after cp local gather to tp layout for attn kernel.
-
unified_mla_kv_cache_update–Returns a dummy that is passed to unified_attention to signal a side effect and
MLAAttention ¶
Bases: Module, AttentionLayerBase
Multi-Head Latent Attention layer.
NOTE: Please read the comment at the top of the file before trying to understand this class
This class takes query, and compressed key/value tensors as input. The class does the following:
- Store the input key and value tensors in the KV cache.
- Perform (multi-head/multi-query/grouped-query) attention.
- Return the output tensor.
Source code in vllm/model_executor/layers/attention/mla_attention.py
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 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 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 1084 1085 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 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 | |
MLACommonBackend ¶
Bases: AttentionBackend
Methods:
-
customize_spec–Per-token-head modes pack an inline fp32 scale pair after the
Source code in vllm/model_executor/layers/attention/mla_attention.py
customize_spec(spec) classmethod ¶
Per-token-head modes pack an inline fp32 scale pair after the latent data (single-sided: head_size_v == 0 for MLA).
Source code in vllm/model_executor/layers/attention/mla_attention.py
MLACommonBaseImpl ¶
Bases: MLAAttentionImpl[A], Generic[A]
Shared MLA base providing dense-MHA prefill (via the selected MLAPrefillBackend) for both dense and sparse impls; subclasses add decode (forward_mqa).
Source code in vllm/model_executor/layers/attention/mla_attention.py
2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 | |
_concat_k_nope_k_pe(k_nope, k_pe) ¶
Efficiently concatenate k_nope and k_pe tensors along the last dimension.
This function avoids the performance penalty of torch.cat with expanded non-contiguous tensors by pre-allocating the output and using direct copies.
Parameters:
-
(k_nope¶Tensor) –Tensor of shape [..., nope_dim]
-
(k_pe¶Tensor) –Tensor to broadcast and concatenate, typically shape [..., 1, pe_dim] or [..., pe_dim]
Returns:
-
Tensor–Tensor of shape [..., nope_dim + pe_dim]
Source code in vllm/model_executor/layers/attention/mla_attention.py
MLACommonImpl ¶
Bases: MLACommonBaseImpl[M], Generic[M]
NOTE: Please read the comment at the top of the file before trying to understand this class
Source code in vllm/model_executor/layers/attention/mla_attention.py
2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 | |
MLACommonMetadata dataclass ¶
Bases: AttentionMetadata, Generic[D]
Metadata for MLACommon.
NOTE: Please read the comment at the top of the file before trying to understand this class
Source code in vllm/model_executor/layers/attention/mla_attention.py
MLACommonMetadataBuilder ¶
Bases: AttentionMetadataBuilder[M]
NOTE: Please read the comment at the top of the file before trying to understand this class
Methods:
-
build_for_cudagraph_capture–This method builds the metadata for full cudagraph capture.
-
determine_prefill_query_data_type–Determine the query data type for prefill queries.
Source code in vllm/model_executor/layers/attention/mla_attention.py
2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 | |
build_for_cudagraph_capture(common_attn_metadata) ¶
This method builds the metadata for full cudagraph capture. Currently, only decode is supported for full cudagraphs with MLA.
Source code in vllm/model_executor/layers/attention/mla_attention.py
determine_prefill_query_data_type(vllm_config, model_dtype) staticmethod ¶
Determine the query data type for prefill queries. Return FP8 dtype if cache is FP8 and prefill query quantization is enabled, else model dtype.
Source code in vllm/model_executor/layers/attention/mla_attention.py
MLACommonPrefillMetadata dataclass ¶
Prefill Specific Metadata
Classes:
-
ContextChunk–One workspace-sized slice of paged context for a run of prefills.
Source code in vllm/model_executor/layers/attention/mla_attention.py
ContextChunk dataclass ¶
One workspace-sized slice of paged context for a run of prefills.
Source code in vllm/model_executor/layers/attention/mla_attention.py
QueryLenSupport ¶
Bases: Enum
Defines the level of query length support for an attention backend's decode pipeline.
- SINGLE_ONLY: Decode pipeline only supports single-token queries (query_len=1)
- UNIFORM: Decode pipeline supports uniform multi-token queries (all requests must have same query_len > 1)
- VARLEN: Decode pipeline supports variable-length queries (mixed query lengths in same batch)
Source code in vllm/model_executor/layers/attention/mla_attention.py
_ContextChunkPlan dataclass ¶
Request-space layout of one context chunk, before tensors are built.
Source code in vllm/model_executor/layers/attention/mla_attention.py
_DecodeConcatQuantFP8 ¶
Bases: QuantFP8
QuantFP8 variant that concatenates decode_ql_nope and decode_q_pe before quantization. When disabled, forward_native is compiled via torch.compile, fusing cat/reshape/quant/view together.
Source code in vllm/model_executor/layers/attention/mla_attention.py
_make_forward(quant_fn) ¶
Factory to create forward methods that concat before quantization.
Source code in vllm/model_executor/layers/attention/mla_attention.py
_detect_output_quant_key(output, output_scale, output_block_scale, output_dim) ¶
Detect the output quantization key from fusion pass parameters.
Returns the appropriate QuantKey, or None if no quantization is needed. Detection is based on output dtype and which scale tensors are present.
Source code in vllm/model_executor/layers/attention/mla_attention.py
_flat_int32(values) ¶
Pinned int32 CPU tensor backing one concatenated per-chunk field.
accumulate_mla_context_chunk(chunk, attn_output, attn_softmax_lse, output, output_lse, output_written=False) ¶
Fold one chunk's partial into the running context partial.
Only the first request may be a continuation; its tokens are merged and the remaining token range is initialized.
Parameters:
-
(output_written¶bool, default:False) –The chunk's attention output already landed in
output[chunk.token_slice]because the backend was handed it asout, leaving only the lse to fold. Invalid for a continuation chunk, whose leading tokens must be merged rather than overwritten.
Source code in vllm/model_executor/layers/attention/mla_attention.py
backend_supports_prefill_query_quantization() cached ¶
Check if the selected MLA prefill backend supports query quantization.
Currently supported backends: - FlashInfer - TRT-LLM Ragged
Not supported: - FlashAttention (FA3/FA4) - Non-GB200 devices (FP8 prefill requires device capability 100)
Source code in vllm/model_executor/layers/attention/mla_attention.py
build_mla_chunked_context_metadata(*, context_lens_cpu, prefill_query_start_loc_cpu, chunked_prefill_workspace, chunked_prefill_workspace_size, block_size, align_chunk_to_block, device, dcp_world_size, dcp_local_block_size, dcp_virtual_block_size, dcp_manager=None) ¶
Build chunked-context metadata for an MLA prefill.
Shared by dense and sparse builders. Packs the prefill contexts into a flat list of workspace-sized per-request chunks and, under DCP, plans the per-rank interleaved local chunks the all-gather reduction consumes.
Parameters:
-
(context_lens_cpu¶Tensor) –Per-prefill context length (seq_len - query_len).
-
(prefill_query_start_loc_cpu¶Tensor) –Prefill query cumulative offsets (0-based).
-
(chunked_prefill_workspace¶Tensor) –Scratch buffer the context gather writes to.
-
(chunked_prefill_workspace_size¶int) –Row capacity of the workspace.
-
(block_size¶int) –KV cache page size for chunk-start alignment.
-
(align_chunk_to_block¶bool) –Round the chunk size down to
block_size. -
(device¶device) –Target device for the returned tensors.
-
(dcp_world_size¶int) –Decode-context-parallel world size (1 if disabled).
-
(dcp_local_block_size¶int) –Per-rank interleave block size for DCP.
-
(dcp_virtual_block_size¶int) –dcp_local_block_size * dcp_world_size. -
(dcp_manager¶MLADCPManager | None, default:None) –Shared MLA DCP collective manager.
Returns:
-
ChunkedContextMetadata | None–The chunked-context metadata, or None when no prefill has any context.
Source code in vllm/model_executor/layers/attention/mla_attention.py
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 | |
init_mla_context_partial(chunked_context, attn_output, attn_softmax_lse, num_tokens) ¶
Allocate the running context partial over all prefill tokens.
Laid out like the chunk partials so the final whole-batch merge against the suffix partial sees matching head strides. Callers whose backend honors an out tensor already know that layout and can allocate directly, pairing it with neutralize_empty_context_partials.
Source code in vllm/model_executor/layers/attention/mla_attention.py
neutralize_empty_context_partials(chunked_context, output, output_lse) ¶
Neutralize the partial of every prefill that no chunk covers.
A prefill without context is never gathered, so nothing would write its rows; a zero output with an -inf lse carries no weight into the final merge against the suffix partial.
Source code in vllm/model_executor/layers/attention/mla_attention.py
plan_mla_context_chunks(context_lens, row_budget, max_context_chunk, split_alignment, padded_rows) ¶
Pack per-request contexts into workspace-sized chunks.
Source code in vllm/model_executor/layers/attention/mla_attention.py
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 | |
reorg_kvcache(allgatered_kv_c_normed, allgatered_k_pe, padded_local_chunk_seq_lens_lst, local_context_lens_allranks, local_starts, sum_seq_len, max_seq_len, toks) ¶
reorg and unpad kvcache after cp local gather to tp layout for attn kernel. e.g. allgatered_kv_c_normed = [T0_0, T0_1, T0_2, T0_3, T1_0, T1_1, ..., T0_4, T0_5, pad, pad, T1_2, pad, ...] -> reorganized_kv_c_normed = [T0_0, T0_1, T0_2, T0_3, T0_4, T0_5, T1_0, T1_1, T1_2, ...] Args: padded_local_chunk_seq_lens_lst: local chunk context lengths under current CP rank. local_context_lens_allranks: local context lengths on each CP rank. local_starts: per-request local offset into the context this chunk starts at. sum_seq_len: the sum of cp_chunk_seq_lens_lst. max_seq_len: the max value of cp_chunk_seq_lens_lst. toks: the number of tokens for local gather cache.
Source code in vllm/model_executor/layers/attention/mla_attention.py
2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 | |
unified_mla_kv_cache_update(kv_c_normed, k_pe, layer_name, kv_cache_dtype, k_scale) ¶
Returns a dummy that is passed to unified_attention to signal a side effect and the data dependency between them to ensure torch.compile preserves ordering.