vllm.models.kimi_k3.nvidia.ops.dcp_topk_merge.kernels
¶
One-round DCP decode top-k merge for the DSA indexer (CuTe DSL).
Under DCP with interleave 1, rank r owns global token g iff
g % W == r and scores only its local tokens g // W. The global top-K
is merged from per-rank candidates:
- Each rank takes its local top-
N(N~ 1.5x the fair shareK/W) from the bf16 logits and packs every candidate into one 32-bit word,(ord16(score) << 16) | (0xFFFF - local[15:0]), plus a bit plane holdinglocal[16](one word per 32 candidates). Zero is padding. - The
[N + N/32]int32 rows are all-gathered over the DCP group (K / Ntimes fewer bytes than the 8-byte(score, id)pairs of the generic merge). - Every rank radix-selects the top-
Kof theW * Ncandidates under (bf16 score desc, global id asc) and writes their global ids.
The merge is exact: on every rank the selected scores equal, as a multiset,
the top-K of the union of all ranks' bf16 logits (equal scores may resolve
to either token; the choice is a function of the gathered words, so all ranks
agree). Round 1 can only miss a token of a saturated rank: one that sent
N words, all strictly above the K-th selected key tau. So the
select also writes per row sat_word = (mask << 16) | ord16(tau), mask bit
r = rank r is saturated (identical on every rank). If the mask is 0,
round 1 is exact. Otherwise (at most (K - 1) // N saturated ranks), each
saturated rank pushes its local top-K of the row into every rank's
symmetric receive slot (P2P stores, then a release flag) from inside its
select, which never waits; a persistent rescue kernel then re-selects the
row over the other ranks' round-1 words at or above tau plus the pushed
lists. The second round is exact without a third: every token it cannot see
scores at most the K-th of what it sees. Unsaturated rows cost the rescue
one mask load.
The rescue state (DCPTopkRescueWorkspace) is built once per DCP group by
init_dcp_topk_merge; the entry points refuse to run without it. Every DCP
rank must call every merge, the merges of a step must be stream-ordered, and
no rank may start the round-1 exchange of call n + 1 before its rescue of
call n has finished (true for the all-gather here).
Non-finite logits never cause an out-of-bounds access: the pack drops local
ids outside [0, min(row_end, S, 2**17)) (DeepSelect returns arbitrary ids
for a row holding a NaN) and candidates with a NaN score, and the select
never pushes past a row's rescue slots; +-inf scores are ordinary candidates
(+inf first, -inf last, ties by global id). A row holding a NaN is outside
the exactness contract: every rank still selects the same set, but it may
miss top tokens of the rank whose local top-N the NaN disturbed.
Classes:
-
DCPTopkPackKernel–Local top-N indices (-1 padded) + bf16 logits -> packed candidate rows.
-
DCPTopkRescueKernel–Second round of the exact merge, launched after every exact select.
-
DCPTopkRescueWorkspace–Rescue state of the exact merge: per ubatch a symmetric receive buffer
-
DCPTopkSelectKernel–Gathered packed rows
[W, T, N + N/32]-> the global top-K of each
Functions:
-
assert_packed_merge_contract–Check a packed-mode producer against the contract of
-
dcp_topk_merge–Exact global top-K ids over the DCP group from this rank's bf16
-
dcp_topk_merge_rows–Exact DCP top-K merge straight to this rank's physical KV rows.
-
dcp_topk_pack–Local top-N (DeepSelect) of the bf16
logitsas packed -
dcp_topk_pack_indices–Pack a local top-N (
local_idx[T, N]int32, -1 padded, N a -
dcp_topk_select–Exact global top-K of the gathered
[W, T, N + N/32]rows: the -
init_dcp_topk_rescue_workspace–Allocate (once) the exact merge's rescue workspace for (K, N) on the
-
register_dcp_topk_merge_warmup–Compile the pack kernel and one select (+ rescue) variant at JIT
DCPTopkPackKernel
¶
Bases: VllmCuTeDSLJitKernel['DCPTopkPackKernel.CompileKey']
Local top-N indices (-1 padded) + bf16 logits -> packed candidate rows.
An index outside [0, min(row_end, S, 2**17)) (row_end from
row_ends when given) or a NaN score packs as an empty slot.
Source code in vllm/models/kimi_k3/nvidia/ops/dcp_topk_merge/kernels.py
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 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 | |
DCPTopkRescueKernel
¶
Bases: VllmCuTeDSLJitKernel['DCPTopkRescueKernel.CompileKey']
Second round of the exact merge, launched after every exact select.
Persistent grid (G = min(T, #SMs) CTAs strided over rows). A row whose
saturation word has an empty mask is skipped (the fast path). Otherwise
the CTA waits for the pushes of the mask's ranks, re-selects the top-K of
the round-1 words of the other ranks (only ord16 >= tau16) plus the
pushed local top-K lists under the same tie rule, overwrites the row's
outputs and bumps seq2[row].
Source code in vllm/models/kimi_k3/nvidia/ops/dcp_topk_merge/kernels.py
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 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 | |
DCPTopkRescueWorkspace
¶
Rescue state of the exact merge: per ubatch a symmetric receive buffer
[T_max, S, K + K/32] int32 (S = rescue_slots(K, N) pushed local
top-K lists per row), symmetric flags [W, T_max], and local
seq2 / sat_word [T_max].
peer_recv / peer_flags hold every rank's buffer address (this
rank's included).
Source code in vllm/models/kimi_k3/nvidia/ops/dcp_topk_merge/kernels.py
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 | |
DCPTopkSelectKernel
¶
Bases: VllmCuTeDSLJitKernel['DCPTopkSelectKernel.CompileKey']
Gathered packed rows [W, T, N + N/32] -> the global top-K of each
row as global ids [T, K] and/or this rank's owned physical KV rows.
One CTA per row. Candidates become (ord16 << gid_bits) | ~gid keys
(unique, so the order is total) and a 9-bit radix select over the
16 + gid_bits key bits finds the K-th key; ties in the bf16 score are
resolved toward the lower global id. Rows with fewer than K candidates
keep all of them.
emit_ids: global ids, selected first (in a fixed order: the same ids in
the same order on every run), then -1.
emit_rows: the selected tokens owned by dcp_rank (gid % W) as
physical rows block_table[req_id[t], local // B] * stride_rows +
local % B (local = gid // W), compacted to the front (in a fixed
order: the same rows in the same order on every run),
then -1; plus their count. A block id past the block table is dropped.
exact: also derive the row's saturation word (mask << 16) | tau16
(tau16 the ord16 of the K-th selected key, 0 when fewer than K were
selected; bit r of mask set iff rank r sent N words, all with
ord16 > tau16) and, when this rank's bit is set, push its local top-K
(from logits) to every rank's rescue slot popc(mask & ((1 << rank)
- 1)) and release flags[rank][t] = seq2[t] + 1. Never waits.
Source code in vllm/models/kimi_k3/nvidia/ops/dcp_topk_merge/kernels.py
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 | |
_ExactArgs
dataclass
¶
Rescue state for one exact merge call (one ubatch of a workspace).
Source code in vllm/models/kimi_k3/nvidia/ops/dcp_topk_merge/kernels.py
_block_scan_inclusive(val, lane, warp, warp_totals, num_warps)
¶
Inclusive block-wide sum; warp_totals is smem [num_warps].
Source code in vllm/models/kimi_k3/nvidia/ops/dcp_topk_merge/kernels.py
_check_exact(gathered, topk, local_topk, rows, exact)
¶
Host checks of an exact select call: the kernels index the rescue workspace (and peers' symmetric memory) by row, rank and slot unchecked, and CuTe accepts mismatched tensors silently.
Source code in vllm/models/kimi_k3/nvidia/ops/dcp_topk_merge/kernels.py
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 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 | |
_decode_key(word, high, j, owner, world_size, gid_bits)
¶
B' word (+ its bit-plane word) of candidate slot j -> select key.
Source code in vllm/models/kimi_k3/nvidia/ops/dcp_topk_merge/kernels.py
_exact_args_for(topk, local_topk, logits, row_ends, group=None)
¶
Rescue state of an exact merge call. There is no lossy fallback: a
missing workspace is an error (see init_dcp_topk_merge). The call
itself is checked by dcp_topk_select.
Source code in vllm/models/kimi_k3/nvidia/ops/dcp_topk_merge/kernels.py
_make_radix_select(*, topk, world_size, keys_per_thread, emit_ids, block_size)
¶
Top-topk of the CTA's register keys (ord16 << gid_bits) | ~gid
(0 = empty) under (score desc, global id asc): writes the ids and/or this
rank's owned physical rows for row and returns the thread's smallest
selected ord16 (0x10000 when none). The select and the rescue share it, so
both emit bit-identical rows for the same candidate set.
Source code in vllm/models/kimi_k3/nvidia/ops/dcp_topk_merge/kernels.py
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 | |
_ord16(bits)
¶
Order-preserving bf16 key: flip negatives, set the sign bit of non-negatives.
Source code in vllm/models/kimi_k3/nvidia/ops/dcp_topk_merge/kernels.py
assert_packed_merge_contract(packed, logits, row_ends, local_topk)
¶
Check a packed-mode producer against the contract of
dcp_topk_merge_rows (raises AssertionError; syncs, for tests and
debugging): per row, exactly min(N, v) words (v the non-NaN
tokens below row_ends) of distinct local ids below row_ends, each
word's score the logit's bf16 bits, and no unsent non-NaN token scoring
above the lowest sent one.
Source code in vllm/models/kimi_k3/nvidia/ops/dcp_topk_merge/kernels.py
dcp_topk_merge(logits, row_ends, topk_indices, local_topk, max_local_len=None)
¶
Exact global top-K ids over the DCP group from this rank's bf16
logits (the stock indexer hook). Collective over the DCP group.
logits is [T, S] bf16 (row stride 1024-byte aligned for
DeepSelect), row_ends the per-row local lengths (at most
max_local_len, see dcp_topk_pack), topk_indices the
[T, K] int32 output (global token ids, -1 padded).
Source code in vllm/models/kimi_k3/nvidia/ops/dcp_topk_merge/kernels.py
dcp_topk_merge_rows(*, logits=None, row_ends=None, packed=None, local_topk, topk, req_id, block_table, block_size, stride_rows, dcp_rank, rows_out, valid_out, ids_out=None, group=None, max_local_len=None)
¶
Exact DCP top-K merge straight to this rank's physical KV rows.
Input: logits [T, S] bf16 (row stride 1024-byte aligned, as
DeepGEMM emits it) and row_ends [T] int32 local lengths, plus
optionally packed [T, N + N/32] int32 (this rank's local top-N
already in the B' layout); without packed the merge takes the local
top-N itself (DeepSelect).
Packed mode still requires logits and row_ends: a saturated rank
re-reads its row's logits for the second round. The producer guarantees
that packed[t] holds min(N, v_t) distinct local ids below
row_ends[t] (v_t its non-NaN tokens), that they are a top-N by
score of logits[t, :row_ends[t]], that each word's score is that
bf16 logit bit for bit, and that logits stays unmodified until this
call returns (in stream order).
Output (caller buffers, written for rows [0, T)): rows_out
[T, >= topk] int32 owned physical rows (compacted, -1 after; a rank
may own up to topk of a row), valid_out [T] their count,
ids_out [T, topk] the global ids (optional).
Collective over the DCP group (see the module docstring).
Source code in vllm/models/kimi_k3/nvidia/ops/dcp_topk_merge/kernels.py
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 | |
dcp_topk_pack(logits, row_ends, local_topk, out=None, max_local_len=None)
¶
Local top-N (DeepSelect) of the bf16 logits as packed
[T, N + N/32] rows (into out when given).
max_local_len bounds every row_ends entry. DeepGEMM sizes the
logits by the global max_model_len; DeepSelect picks its cluster
variant for <= 6 rows at >= 512K columns (~21 us against ~6 us), so the
columns past the local bound are cut off before it sees them.
Source code in vllm/models/kimi_k3/nvidia/ops/dcp_topk_merge/kernels.py
dcp_topk_pack_indices(logits, local_idx, out, row_ends=None)
¶
Pack a local top-N (local_idx [T, N] int32, -1 padded, N a
multiple of 32) of the bf16 logits [T, S] into out
[T, N + N/32] int32 (the B' layout, see __init__). Indices at or
past row_ends ([T] int32, optional) or S and NaN scores
become empty slots.
Source code in vllm/models/kimi_k3/nvidia/ops/dcp_topk_merge/kernels.py
dcp_topk_select(gathered, local_topk, topk_indices, *, topk=None, rows=None, exact=None)
¶
Exact global top-K of the gathered [W, T, N + N/32] rows: the
select, then the rescue (second round) of saturated rows.
Source code in vllm/models/kimi_k3/nvidia/ops/dcp_topk_merge/kernels.py
init_dcp_topk_rescue_workspace(max_rows, topk, local_topk, group=None)
¶
Allocate (once) the exact merge's rescue workspace for (K, N) on the DCP group. Must run on every DCP rank, outside CUDA-graph capture.
Source code in vllm/models/kimi_k3/nvidia/ops/dcp_topk_merge/kernels.py
register_dcp_topk_merge_warmup(topk, local_topk, world_size, *, emit_ids=True, block_size=0, exact=True)
¶
Compile the pack kernel and one select (+ rescue) variant at JIT warmup.
The stock indexer uses the defaults (global ids); a caller of
dcp_topk_merge_rows passes its block_size and whether it also
wants the global ids.