vllm.models.deepseek_v41.nvidia.specialized_decode.attention.sparse_mla_cute
¶
The sparse-MLA decode of small batches, in CuTe DSL.
The problem. In a decode step every query token (DeepSeek-V4's MLA after the query projection: 16 heads a rank at TP4, FP8 E4M3, head dim 512) attends to its own sparse set of KV rows. A row is 512 FP8 bytes and serves as both K and V. The set holds up to 640 columns: the 128 sliding-window rows of one KV pool, then the indexer's top-k rows of the compressed pool, all addressed as storage offsets into their pool, with -1 for an unused column and a per-token count of active columns. TRTLLM-gen splits a token's 640 rows over at most 3 CTAs, so at one or two requests (6 or 12 tokens with DSpark) its kernel runs about 10 µs on a GPU of 152 SMs; most of that is the latency of one CTA's serial work.
What this module computes. sparse_mla_decode writes, for each token t
and head h, the BF16 attention output over the token's active columns c
(c < lens[t] and indices[t, c] >= 0)::
s_c = bmm1_scale * q[t, h] . kv[c]
p_c = exp(s_c - m)
out[t, h] = bmm2_scale * sum_c p_c kv[c] / (sum_c p_c + exp(sink_h - m))
where kv[c] is row indices[t, c] of the sliding-window pool for the
first 128 columns and of the compressed pool for the others, m is the
maximum of the scores and the sink, and the sink term is omitted without sinks.
A token with no active column gets zeros. This is what
trtllm_batch_decode_sparse_mla_dsv4 computes for these shapes.
How. One CTA of 128 threads takes one token and one 128-column chunk of its
sparse set (chunk 0 = the sliding-window columns); the n = ceil(width / 128)
chunks of a token form a thread block cluster, so a 640-wide layer runs 5 CTAs
a token in parallel::
gather the chunk's KV rows and the query (cp.async, one group per 128-byte K
block) into 128B-swizzled tiles
└► S^T = KV x Q^T on tcgen05 (M 128 rows, N 16 heads, K 512, FP8 -> FP32 TMEM)
└► chunk softmax per head in base 2: max m_c, sum l_c, and
P^T = 256 * exp2(S - m_c) as FP8 (scaled: small weights stay normal)
└► O^T = V^T x P^T on tcgen05, V the same tile read MN-major
(four MMAs of 128 head dims) ...
└► ... while the cluster exchanges (m_c, l_c) per head (st.async)
└► each CTA scales its partial O by exp2(m_c - M) / L
(M, L over the cluster's chunks and the sink)
└► block j of the 512 head dims goes to CTA j % n, which
sums the n partials and stores BF16
The partial blocks travel by bulk copies between the CTAs' shared memory into the receiving CTA's KV tile, whose MMAs are done by then: a cluster barrier after each CTA's second MMA separates the two uses. A last cluster barrier keeps every CTA resident until the bulk copies reading its shared memory are complete.
Synchronization. The kernel reads the sinks (weights) before its grid dependency wait and everything else after it, and lets its dependents launch once its S MMAs are issued (programmatic dependent launch). Masked columns gather zeros and score -inf.
Scope. Two CTAs fit an SM (about 78 KB of shared memory each), so a launch is
one wave only while every token's cluster can be resident: _max_tokens allows
2 x the device's resident clusters of n CTAs at one CTA an SM (on a 152-SM
GB300, 8 requests with DSpark at width 640).
Larger batches keep TRTLLM-gen, which is faster once the clusters run in waves.
Classes:
-
SparseMlaDecodeCute–The sparse-MLA decode (module docstring) of tokens whose sparse sets
Functions:
-
bulk_copy_to_peer–Bulk-copy
sizebytes of this CTA's shared memory atsourceto -
copy16_shared_to_global–Copy 16 bytes from this CTA's shared memory to global memory.
-
cp_async_16–Copy 16 bytes from global to this CTA's shared memory (cp.async).
-
ld_global_f32–A float from read-only global memory.
-
ld_global_s32–A 32-bit integer from global memory an earlier grid wrote (side-effecting,
-
mma_f8–One tcgen05 FP8 MMA of this CTA: D (TMEM) = A x B (+ D if
-
pack_e4m3x4–Four floats as E4M3 bytes,
ain the lowest byte (round to nearest, -
precompile–Compile the kernels of DeepSeek-V4's two widths (128 sliding-window
-
sparse_mla_decode–Launch
SparseMlaDecodeCuteon the current stream (see -
st_async_peer_b32–Store a word at
smemof cluster CTApeer(the same offset as a -
st_shared_bf16–Store
valuerounded to BF16 at a shared address. -
st_shared_v4–Store four 32-bit words at a 16-byte aligned shared address.
-
st_shared_zero16–Store 16 zero bytes at a 16-byte aligned shared address.
-
supports_sparse_mla_cute–Whether
sparse_mla_decodetakes these arguments: 16 FP8 query heads
SparseMlaDecodeCute
¶
The sparse-MLA decode (module docstring) of tokens whose sparse sets
are at most chunks x 128 columns wide; has_sinks: add each head's
attention sink to the softmax denominator.
Methods:
-
__call__–Launch on
stream: one cluster ofnCTAs a token. Pointers
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla_cute.py
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 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 | |
__call__(q_ptr, q_stride, swa_ptr, swa_stride, comp_ptr, comp_stride, idx_ptr, idx_stride, idx_cols, lens_ptr, sinks_ptr, out_ptr, out_stride, scale_log2, out_scale, tokens, stream)
¶
Launch on stream: one cluster of n CTAs a token. Pointers
are addresses; strides are in elements (bytes for the FP8 query and
pools, BF16 elements for out); scale_log2 is
bmm1_scale * log2(e).
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla_cute.py
_asm(result, operands, text, constraints, side_effects, loc, ip)
¶
One PTX inline assembly operation.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla_cute.py
_is_pool(cache)
¶
An FP8 KV pool of 512-byte rows stride(-2) apart, 16-byte aligned.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla_cute.py
_kernel(device_index, chunks, has_sinks)
cached
¶
SparseMlaDecodeCute compiled for a device, chunk count and sinks.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla_cute.py
_max_tokens(device_index, chunks)
cached
¶
Tokens whose clusters are all resident at once: CTAS_PER_SM times
the resident clusters of chunks CTAs at one CTA an SM.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla_cute.py
_sdesc(lbo, sbo)
¶
A 128B-swizzled shared-memory matrix descriptor without its start address: leading and stride byte offsets, version 1.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla_cute.py
_v(value, loc, ip)
¶
bulk_copy_to_peer(dest, source, size, mbar, peer, *, loc=None, ip=None)
¶
Bulk-copy size bytes of this CTA's shared memory at source to
dest of cluster CTA peer (the same offset as a local address),
completing on the peer's mbar.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla_cute.py
copy16_shared_to_global(smem, gmem, *, loc=None, ip=None)
¶
Copy 16 bytes from this CTA's shared memory to global memory.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla_cute.py
cp_async_16(smem, gmem, *, loc=None, ip=None)
¶
Copy 16 bytes from global to this CTA's shared memory (cp.async).
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla_cute.py
ld_global_f32(address, *, loc=None, ip=None)
¶
A float from read-only global memory.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla_cute.py
ld_global_s32(address, *, loc=None, ip=None)
¶
A 32-bit integer from global memory an earlier grid wrote (side-effecting, so it stays after the grid dependency wait).
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla_cute.py
mma_f8(d_tmem, adesc, bdesc, idesc, accumulate, *, loc=None, ip=None)
¶
One tcgen05 FP8 MMA of this CTA: D (TMEM) = A x B (+ D if
accumulate), A and B from shared memory descriptors.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla_cute.py
pack_e4m3x4(a, b, c, d, *, loc=None, ip=None)
¶
Four floats as E4M3 bytes, a in the lowest byte (round to nearest,
saturating).
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla_cute.py
precompile(device_index, has_sinks)
¶
Compile the kernels of DeepSeek-V4's two widths (128 sliding-window columns alone, and 640) and count the resident clusters, outside any CUDA graph capture; other widths compile at their first launch.
Parameters:
-
(device_index¶int) –The CUDA device to compile for.
-
(has_sinks¶bool) –Whether the layers add attention sinks.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla_cute.py
sparse_mla_decode(query, swa_kv_cache, compressed_kv_cache, sparse_indices, sparse_topk_lens, out, bmm1_scale, bmm2_scale, sinks)
¶
Launch SparseMlaDecodeCute on the current stream (see
supports_sparse_mla_cute for the accepted layouts).
Parameters:
-
(query¶Tensor) –[tokens, 16, 512] FP8 E4M3.
-
(swa_kv_cache¶Tensor) –FP8 pool of 512-byte rows
stride(-2)apart; the first 128 columns ofsparse_indicesare its row offsets. -
(compressed_kv_cache¶Tensor) –The pool of the other columns, likewise.
-
(sparse_indices¶Tensor) –[tokens, width] int32 row offsets, -1 for none (
width<= 640). -
(sparse_topk_lens¶Tensor) –[tokens] int32 active columns of each token.
-
(out¶Tensor) –[tokens, 16, 512] BF16 attention output.
-
(bmm1_scale¶float) –Score scale (the softmax scale times the FP8 scales).
-
(bmm2_scale¶float) –Output scale.
-
(sinks¶Tensor | None) –[16] FP32 attention sinks (natural log), or None.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla_cute.py
st_async_peer_b32(smem, mbar, peer, value, *, loc=None, ip=None)
¶
Store a word at smem of cluster CTA peer (the same offset as a
local address), completing 4 bytes on the peer's mbar.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla_cute.py
st_shared_bf16(smem, value, *, loc=None, ip=None)
¶
Store value rounded to BF16 at a shared address.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla_cute.py
st_shared_v4(smem, w0, w1, w2, w3, *, loc=None, ip=None)
¶
Store four 32-bit words at a 16-byte aligned shared address.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla_cute.py
st_shared_zero16(smem, *, loc=None, ip=None)
¶
Store 16 zero bytes at a 16-byte aligned shared address.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/attention/sparse_mla_cute.py
supports_sparse_mla_cute(query, swa_kv_cache, compressed_kv_cache, sparse_indices, sparse_topk_lens, out, bmm1_scale, bmm2_scale, sinks)
¶
Whether sparse_mla_decode takes these arguments: 16 FP8 query heads
of 512 (BF16 output), FP8 pools, int32 index rows of at most 640 columns,
scalar scales, FP32 sinks, and a batch whose clusters are all resident.