vllm.v1.attention.backends.mla.flashinfer_mla_sparse
¶
FlashInfer sparse MLA attention backend.
Classes:
-
FlashInferMLASparseImpl–FlashInfer MLA Sparse implementation.
-
FlashInferMLASparseMetadata–Attention metadata for FlashInfer MLA Sparse backend.
-
FlashInferMLASparseMetadataBuilder–Builder for FlashInfer MLA Sparse attention metadata.
-
FlashInferMLASparseSM120Backend–FlashInfer sparse MLA backend for SM120.
-
FlashInferMLASparseTRTLLMBackend–FlashInfer sparse MLA backend using the TRTLLM-gen launcher.
-
FlashInferMLASparseTRTLLMMetadataBuilder–Metadata builder for the SM100 TRT-LLM sparse MLA kernel.
Functions:
-
compute_trtllm_sparse_mla_workspace_bytes–Workspace bytes needed by the trtllm-gen sparse MLA decode kernel.
-
trtllm_gen_query_heads–Query heads the trtllm-gen MLA decode runs: it takes no head count strictly
-
use_prims_ts–Whether FLASHINFER_MLA_SPARSE runs this model's sparse MLA on prims_ts.
FlashInferMLASparseImpl
¶
Bases: SparseMLACommonImpl[FlashInferMLASparseMetadata]
FlashInfer MLA Sparse implementation.
Uses the TRT-LLM MLA kernel with sparse_mla_top_k parameter for sparse attention computation.
Methods:
-
autotune_hisparse_decode–Autotune the largest legal HiSparse decode batch.
Source code in vllm/v1/attention/backends/mla/flashinfer_mla_sparse.py
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 | |
_forward_prims_ts(q, kv_cache, topk_indices, valid_counts, layer, block_size)
¶
Sparse MLA of every row of q [T, H, 576] (E4M3, quantized by the
layer with q_scale) over its valid_counts leading physical rows
of topk_indices (-1 entries are masked).
Source code in vllm/v1/attention/backends/mla/flashinfer_mla_sparse.py
autotune_hisparse_decode(layer)
¶
Autotune the largest legal HiSparse decode batch.
Source code in vllm/v1/attention/backends/mla/flashinfer_mla_sparse.py
FlashInferMLASparseMetadata
dataclass
¶
FlashInferMLASparseMetadataBuilder
¶
Bases: SparseMLACommonMetadataBuilder[FlashInferMLASparseMetadata]
Builder for FlashInfer MLA Sparse attention metadata.
Source code in vllm/v1/attention/backends/mla/flashinfer_mla_sparse.py
FlashInferMLASparseSM120Backend
¶
Bases: _FlashInferMLASparseBackendBase
FlashInfer sparse MLA backend for SM120.
Source code in vllm/v1/attention/backends/mla/flashinfer_mla_sparse.py
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 | |
FlashInferMLASparseTRTLLMBackend
¶
Bases: _FlashInferMLASparseBackendBase
FlashInfer sparse MLA backend using the TRTLLM-gen launcher.
Source code in vllm/v1/attention/backends/mla/flashinfer_mla_sparse.py
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 | |
FlashInferMLASparseTRTLLMMetadataBuilder
¶
Bases: FlashInferMLASparseMetadataBuilder
Metadata builder for the SM100 TRT-LLM sparse MLA kernel.
Source code in vllm/v1/attention/backends/mla/flashinfer_mla_sparse.py
_FlashInferMLASparseBackendBase
¶
Bases: AttentionBackend
Common metadata for concrete FlashInfer sparse MLA backends.
Source code in vllm/v1/attention/backends/mla/flashinfer_mla_sparse.py
_PrimsTSPlans
¶
Planned prims_ts wrappers, shared by all layers.
A plan fixes FlashInfer's schedule (chosen from its row capacity) and
compiles that schedule's kernel on first use, which takes seconds. So every
row capacity of a geometry is planned at once, at startup (from the profile
run's reserve_workspace), and a call runs on the smallest capacity that
holds its rows: no planning or compilation happens while serving, and the
plan count stays fixed. Each plan is a packed-query plan of capacity
one-token requests, so its schedule is the one FlashInfer picks for
capacity decode rows; a call with fewer rows passes the matching
offsets (qo_indptr = [0, 1, ..., rows, ..., rows]).
The sparse MLA layers run one at a time on one stream, so every plan shares one workspace. A geometry that needs more workspace replaces it; plans keep the buffer they were planned with, so graphs captured with them stay valid.
Methods:
-
get–{capacity: (wrapper, qo_indptr of a full call)} for one geometry.
Source code in vllm/v1/attention/backends/mla/flashinfer_mla_sparse.py
get(device, buckets, heads, kv_cache_format, max_topk, return_lse)
¶
{capacity: (wrapper, qo_indptr of a full call)} for one geometry.
Source code in vllm/v1/attention/backends/mla/flashinfer_mla_sparse.py
_finish_dcp_partials(out, lse, counts)
¶
In place, for trtllm-gen's DCP partials out [T, H, D] and lse
[T, H] (views of its head-padded buffers): rows without top-k entries
(counts 0; the DCP index conversion compacts a row's valid entries to
its front and counts them, so these are the rows whose entries are all -1)
get output 0 and LSE -inf, the empty-shard identity of the DCP combine. One
small kernel that writes only the empty rows.
Source code in vllm/v1/attention/backends/mla/flashinfer_mla_sparse.py
_finish_dcp_partials_kernel(out_ptr, lse_ptr, counts_ptr, heads, out_row_stride, lse_row_stride, HEAD_DIM, HEADS_BLOCK, OUT_BLOCK)
¶
One row of a trtllm-gen DCP partial: without top-k entries (count 0) its output is zeroed and its LSE set to -inf; other rows are not touched.
Source code in vllm/v1/attention/backends/mla/flashinfer_mla_sparse.py
_kv_counters(num_rows, num_heads)
¶
multi_ctas_kv_counter_buffer for one launch when the reserved
counters cover it; otherwise FlashInfer allocates its own.
Source code in vllm/v1/attention/backends/mla/flashinfer_mla_sparse.py
_pad_query_heads(query, heads)
¶
query [T, H, D] in a new contiguous [T, heads, D] buffer whose
extra heads are zero (the head count a decode kernel runs), in one kernel.
Source code in vllm/v1/attention/backends/mla/flashinfer_mla_sparse.py
_pad_query_heads_kernel(src_ptr, dst_ptr, src_row_stride, row_words, padded_row_words, BLOCK)
¶
One query row of 8-byte words, zero-extended to the padded heads.
Source code in vllm/v1/attention/backends/mla/flashinfer_mla_sparse.py
_prims_ts_row_buckets(vllm_config)
¶
Row capacities to plan: every CUDA graph size (graph replays keep the schedule FlashInfer picks for their exact row count) and the powers of two up to the largest call, which serve every other row count.
Source code in vllm/v1/attention/backends/mla/flashinfer_mla_sparse.py
_required_workspace_bytes(dcp_world_size, num_heads_per_rank, max_num_batched_tokens)
¶
Resolve the workspace size, honoring an explicit env override.
Source code in vllm/v1/attention/backends/mla/flashinfer_mla_sparse.py
_reserve_kv_counters(device, num_rows, num_heads)
¶
Allocates the zeroed trtllm-gen multi-CTA KV counters for launches of up
to num_rows x num_heads, before CUDA graph capture. The kernel
resets them after each launch, so all layers share them, as they share the
workspace. Without them FlashInfer zero-allocates counters on every call.
Source code in vllm/v1/attention/backends/mla/flashinfer_mla_sparse.py
compute_trtllm_sparse_mla_workspace_bytes(base_workspace_bytes, dcp_world_size, num_heads_per_rank, max_num_batched_tokens)
¶
Workspace bytes needed by the trtllm-gen sparse MLA decode kernel.
Under DCP the query is all-gathered across the DCP group in the head dim
and the kernel is asked for LSE, so trtllm-gen carves the softmax-stats
slab described above from the workspace before its (batch-independent)
counter and scratch regions. base_workspace_bytes must stay available
for those regions, so the slab is added on top of it (see #50781).
Without DCP no LSE is requested, no slab is carved, and the base size is
returned unchanged. A launch covers at most _TRTLLM_GEN_MAX_REQUESTS
tokens, and launches run in order on one stream, so larger batches reuse
the same slab.
Source code in vllm/v1/attention/backends/mla/flashinfer_mla_sparse.py
trtllm_gen_query_heads(num_heads)
¶
Query heads the trtllm-gen MLA decode runs: it takes no head count strictly between 64 and 128 (e.g. 96 gathered under DCP8 at 12 heads per rank), so those are zero-padded to 128.
Source code in vllm/v1/attention/backends/mla/flashinfer_mla_sparse.py
use_prims_ts(vllm_config, kv_cache_dtype)
¶
Whether FLASHINFER_MLA_SPARSE runs this model's sparse MLA on prims_ts.
"auto" uses prims_ts under DCP when it can run (faster than trtllm-gen for per-tensor fp8 prefill and decode on SM100 there) and trtllm-gen otherwise, including TP-only (DCP 1). "trtllm" keeps trtllm-gen for per-tensor fp8. nvfp4_ds_mla (no trtllm-gen kernel) always uses prims_ts. Raises if prims_ts is required but cannot run.