vllm.models.deepseek_v41.nvidia.specialized_decode.ampere.sparse_mla
¶
DeepSeek-V4.1 sparse MLA on SM80 in CuTe DSL.
Each query token attends with all its heads to its own list of keys: the window keys (528-byte MXFP8 records named by SWA slots) followed by the compressed keys its indexer selected (288-byte NVFP4 records named by compressed-cache slots). Keys double as values. Per head the result is
softmax([q . k * scale for each key] + [sink]) applied to the keys,
the sink adding exp(sink) to the denominator only, then the inverse RoPE
on dims 448-511 at the token's position, rounded to BF16 (wo_a's input).
Two kernels compute it:
SparseMlaPartial(grid(splits, tokens, head groups), 256 threads) walks the token's key list in 64-key chunks; CTAsplittakes chunks[split * chunks_per_split, ...). Per chunk it decodes the 64 records into a BF16 tile in shared memory (codecs.py: two codes at a time, exact), multipliesS^T = K Q^Tfor its 8 or 16 heads onmma.syncBF16 tensor cores (ldmatrixoperands, FP32 accumulation), takes the online softmax of each head, and accumulatesO^T += V^T P^TwithProunded to BF16. It stores its FP32 output and the heads' running (max, sum) to a workspace.SparseMlaMerge(grid(tokens, heads), 128 threads) combines the splits with the sink, divides, rounds to BF16, inverse-RoPEs and rounds. With one split per token the partial kernel finishes its rows itself with the same arithmetic (_finish_pair), and neither the FP32 workspace nor the merge is needed.
A token without keys outputs zeros. Scores are scaled in base 2
(scale * log2(e)) and exponentiated with exp2.
A variant (v4_records) reads DeepSeek's V4 records from both caches
instead, as SM90 stores them: 448 E4M3 dims with one UE8M0 scale per 64 and
64 BF16 RoPE dims. A page holds its tokens' 576 value bytes, then their 8
scale bytes (7 used); the cache tensor names each token's 584 bytes. The
RoPE dims are already BF16 and pass through; the others decode as above.
Its callers apply the inverse RoPE in their output projection, so the
variant's output keeps dims 448-511 rotated (inverse_rope=False).
Gathering a chunk is a chain of dependent global loads (a key's slot, then
its record), so the partial kernel keeps every load of a chunk in flight at
once: the 64 slots of chunk c + 1 are loaded (one per thread of the first
64) while chunk c is decoded, turned into record addresses and staged in
shared memory; each thread then issues all the record words of its 16 keys
before decoding any. A chunk thus waits for about one memory round trip.
Tensor parallelism leaves 8 heads per GPU at TP8. The MMAs therefore take
keys and dims as their M (16 per tile) and heads as their N (8 per tile),
so no tile row is padding: S^T runs on two groups of 4 warps, one per
half of the 512 dims (16 keys per warp, the halves added in the softmax), and
O^T on 8 warps of 64 dims. A CTA holds 8 heads at TP8 (two CTAs fit an
SM and overlap each other's waits), 16 otherwise. default_splits spreads
each token's chunks over as many CTAs as fill one wave of resident CTAs: one
per chunk for a few tokens, one per token for a wide batch or prefill.
Classes:
-
SparseMlaMerge–Combine the splits of each (token, head), add the sink, normalize and
-
SparseMlaPartial–Partial sparse MLA of (token, split, head group) items; see the module
Functions:
-
default_splits–CTAs per (token, head group) item: as many as fill one wave of resident
-
sparse_mla–Sparse MLA of
q [T, H, 512](BF16) intoout [T, H, 512], with
SparseMlaMerge
¶
Combine the splits of each (token, head), add the sink, normalize and
(with inverse_rope) inverse-RoPE; see the module docstring.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/ampere/sparse_mla.py
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 | |
SparseMlaPartial
¶
Partial sparse MLA of (token, split, head group) items; see the module
docstring. head_rows is the number of heads a CTA holds (8 or 16);
v4_records reads V4 records from both caches and inverse_rope
rotates the output's RoPE dims back.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/ampere/sparse_mla.py
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 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 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 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 | |
_load_slot(index, row, swa_len, cmp_len, swa_indices, swa_index_stride, cmp_indices, cmp_index_stride)
¶
Issue the load of key index of row's list (window keys,
then compressed keys): its cache slot, -1 past the list.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/ampere/sparse_mla.py
_stage_key(key, index, slot, swa_len, swa_cache, swa_block_size, swa_block_bytes, cmp_cache, cmp_block_size, cmp_block_bytes, record, kind, scale_offset)
¶
Store a key's kind (0 none, 1 window, 2 compressed) and record
address; a key without a record gets the first window record, a valid
address whose bytes are loaded and then ignored. A V4 record's
address is that of its values, and scale_offset holds its scale
bytes' offset from there.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/ampere/sparse_mla.py
_block_bytes(cache, record)
¶
(block size in records, block stride in bytes) of a paged record cache
[blocks, block_size, record] (uint8).
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/ampere/sparse_mla.py
_cos_sin_row(positions, cos_sin, row, cos_sin_stride, inverse_rope)
¶
The RoPE (cos, sin) row of token row's position; without
inverse_rope the table's first row, which nothing reads.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/ampere/sparse_mla.py
_fake(dtype, align)
¶
A placeholder pointer for compilation.
_finish_pair(acc_even, acc_odd, top, denominator, dim, cos_sin_row, out_row, inverse_rope)
¶
Store output dims dim, dim + 1 (dim even) of one (token, head):
the accumulators over the softmax denominator rounded to BF16 (zeros when
the row's maximum top is -inf: no keys), with inverse_rope
inverse-RoPE'd on dims 448-511 with the token's cos/sin row and rounded
to BF16 again.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/ampere/sparse_mla.py
_merge_kernel(device_index, inverse_rope)
cached
¶
SparseMlaMerge compiled for a device.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/ampere/sparse_mla.py
_multiprocessors(device_index)
cached
¶
_partial_kernel(device_index, has_compressed, head_rows, v4_records, inverse_rope)
cached
¶
SparseMlaPartial compiled for a device.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/ampere/sparse_mla.py
_pointer(dtype, tensor, align)
¶
A CuTe DSL global-memory pointer to tensor's data.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/ampere/sparse_mla.py
default_splits(items, chunks, head_rows, device)
¶
CTAs per (token, head group) item: as many as fill one wave of resident CTAs (two per SM with 8-head CTAs, else one), at most one per chunk.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/ampere/sparse_mla.py
sparse_mla(q, swa_cache, swa_indices, swa_lens, compressed_cache, compressed_indices, compressed_lens, sink, scale, positions, cos_sin_cache, out, splits=None, v4_records=False)
¶
Sparse MLA of q [T, H, 512] (BF16) into out [T, H, 512], with
the arguments of reference.sparse_mla.
splits CTAs share one token's key list (default:
default_splits). v4_records reads both caches as 584-byte V4
records; without positions and cos_sin_cache the output's RoPE
dims stay rotated.
Source code in vllm/models/deepseek_v41/nvidia/specialized_decode/ampere/sparse_mla.py
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 | |