vllm.models.deepseek_v41.nvidia.specialized_decode.attention.sparse_mla
¶
The FlashInfer sparse-MLA decode of a specialized decode step.
It is the decode branch of DeepseekV4FlashInferMLAAttention.forward_mqa
with two changes, neither of which issues a collective:
- Shared sparse-index tables (
SparseIndexTables). Compressed layers that share an index source build equal tables in a step, so the first of them builds the table and the others reuse it; the first layer of such a run also builds, on a side stream, the tables its later layers look up in other SWA KV-cache groups. - Persistent multi-CTA KV counters. Without a caller-owned counter buffer,
trtllm_batch_decode_sparse_mla_dsv4allocates and zeroes a fresh one before every call, a small fill kernel ahead of each decode launch. The kernel returns every counter to zero when it finishes, so a buffer zeroed once stays valid for launches that do not overlap, as in the FlashInfer MLA backend.
Classes:
-
SparseIndexTables–The sparse-index tables of one decode step, shared by equal keys.
-
SparseMlaDecode–forward_mqaof a pure-decode step on the shared tables and the
Functions:
-
kv_counter_buffer–Zeroed multi-CTA KV counters on the current device for decode launches
-
sparse_index_key–Layers with equal keys build equal sparse-index tables in a step: a
-
trtllm_decode_sparse_mla_dsv4–trtllm_batch_decode_sparse_mla_dsv4forsparse_indicesthat are
SparseIndexTables
¶
The sparse-index tables of one decode step, shared by equal keys.
Each SWA KV-cache group has its own metadata, and the layers of a run of
equal keys interleave the groups: each group would build each key's table
on the critical path. A table depends only on the step's metadata, the
group's SWA indices and block table and, for compressed layers, the index
source's top-k indices; not on any layer's KV write. So the first layer of
a run builds its own group's table through the stock
_build_sparse_index_metadata and then forks, onto stream, the
builds that later layers of the run look up in other groups:
lookupreturns a layer's table, building and forking on a miss and making the current stream wait for a table built onstream.joinmakes the current stream wait for everythingstreamran. Index sources call it before their indexer overwrites the top-k indices, and the executor at the end of its step, so a CUDA graph capture ends joined.
Events are reused across forks: a wait on an event that a later fork re-recorded waits for more work on the same in-order stream, never less.
Methods:
-
join–Make the current stream wait for every build since the last join.
-
lookup–attn._build_sparse_index_metadata, on the step's shared tables. -
reset–Forget the previous step's tables.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla.py
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 | |
_fork(attn, key, kv_cache, swa_metadata, attn_metadata, swa_only)
¶
Build, on stream, the tables of the other KV-cache groups whose
later layers of the run look key up: for each group, the build its
first such layer would run on a miss. The key fixes the compressed
cache and its metadata, so these are attn's.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla.py
join()
¶
Make the current stream wait for every build since the last join.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla.py
lookup(attn, kv_cache, swa_k_cache, swa_metadata, attn_metadata, swa_only)
¶
attn._build_sparse_index_metadata, on the step's shared tables.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla.py
SparseMlaDecode
¶
forward_mqa of a pure-decode step on the shared tables and the
persistent KV counters.
Methods:
-
run–Write
attn's sparse MLA ofqintoout.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla.py
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 | |
run(attn, q, out)
¶
Write attn's sparse MLA of q into out.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla.py
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 | |
kv_counter_buffer(max_rows)
¶
Zeroed multi-CTA KV counters on the current device for decode launches
of up to max_rows rows (requests x heads), or None off the checked
FlashInfer version.
Allocated when the executor is built, outside any CUDA graph capture. Every layer shares the buffer, as it shares the workspace: their decode launches run one after another on one stream. Decode launches that overlap on one device would race on it.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla.py
sparse_index_key(attn)
¶
Layers with equal keys build equal sparse-index tables in a step: a compressed layer's table depends on its compressed cache, compress ratio and top-k indices, which only its index source writes.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla.py
trtllm_decode_sparse_mla_dsv4(*, query, swa_kv_cache, compressed_kv_cache, workspace_buffer, sparse_indices, sparse_topk_lens, seq_lens, out, bmm1_scale, bmm2_scale, sinks, cum_seq_lens_q, max_q_len, kv_counters)
¶
trtllm_batch_decode_sparse_mla_dsv4 for sparse_indices that are
KV storage offsets, on kv_counters (from kv_counter_buffer) when
they are large enough for the launch.