vllm.models.deepseek_v41.common.engram
¶
Engram: n-gram hash lookups gated into the hyper-connection stream.
Port of the reference inference/engram.py + Engram /
ParallelEngramEmbedding from inference/model.py (DeepSeek V4.1
checkpoint layout). Engram modules live on the backbone layers listed in
engram_layer_ids only.
An n-gram at position p needs the token ids at p-1..p-3 while vLLM
streams tokens chunk-by-chunk:
token_map: token id -> compressed vocab id, built once from the model's tokenizer at init (deterministic; asserted againstengram_compressed_vocab_size).lookback_token_ids: the ids just before each request's chunk start, which the runner reads from its device-resident token history.
Classes:
-
DPSharedEngramStorage–Host weights shared by a node-local DP group with one writer: host-NUMA
-
Engram–Writes an n-gram lookup into the residual stream, gated by how well it
-
EngramBatch–Stage every Engram layer's WKV input as MXFP8 with one exchange.
-
EngramLayout–Bucket layout of the n-gram hash tables.
-
EngramPeerGroup–GPUs of several engines that split each Engram table over their HBM.
-
NgramHashState–Maps each position to the hash ids of the n-grams ending there.
-
ParallelEngramEmbedding–Hash heads with FP8 or packed MXFP4 rows and per-block E8M0 scales.
Functions:
-
build_compressed_token_map–Map every token id onto a smaller id space where tokens that normalize
-
can_share_engram_tables–Whether co-located DP replicas exist and can hold the full tables.
-
compute_hash_multipliers–One multiplier per (layer, lookback), from a per-layer RNG so layers
-
engram_gathered_num_tokens–Per-replica token slot for the node-local Engram DP group.
-
engram_head_shard_rank–This rank's slot among the hash-head shards of one engram table.
-
find_next_prime–The smallest prime above
startthat has not been handed out yet. -
gather_engram_hashes–Collect the n-gram ids of every DP replica sharing one table.
-
gather_errors–Each rank's error, joined; empty if every rank succeeded.
-
join_engram_peer_group–This GPU's membership, shared by all of its process's tables.
DPSharedEngramStorage
¶
Host weights shared by a node-local DP group with one writer: host-NUMA cuMem where FABRIC handles work, else a registered /dev/shm mapping.
Methods:
-
share_shm_table–Map and register one /dev/shm file across a node-local DP group.
Source code in vllm/models/deepseek_v41/common/engram.py
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 | |
share_shm_table(num_bytes, group)
staticmethod
¶
Map and register one /dev/shm file across a node-local DP group.
Returns CPU and GPU views of the file. Raises on every rank, leaving nothing mapped, if any rank fails.
Source code in vllm/models/deepseek_v41/common/engram.py
Engram
¶
Bases: Module
Writes an n-gram lookup into the residual stream, gated by how well it matches that stream.
The hash ids fetch n_hash_cols rows; wkv turns them into one key per
hc copy plus a shared value. The gate is a normalized dot product of the
stream against the key, signed-sqrt'ed before the sigmoid (matching the
training kernel).
Methods:
-
embed–Gather heads, returning only local tokens when SP is enabled.
-
forward–hidden_states: [T, hc_mult, dim]; hash_ids: [T, n_hash_cols] (all
-
prepare_embeddings–Look up, or prefetch from host memory, rows before the decoder layers
Source code in vllm/models/deepseek_v41/common/engram.py
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 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 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 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 1964 1965 1966 1967 1968 1969 | |
_post_wkv(hidden_states, kv, token_mask, num_kv_tokens)
¶
The gated update from the wkv output of num_kv_tokens tokens.
Source code in vllm/models/deepseek_v41/common/engram.py
embed(hash_ids)
¶
Gather heads, returning only local tokens when SP is enabled.
Source code in vllm/models/deepseek_v41/common/engram.py
forward(hidden_states, hash_ids, token_mask=None)
¶
hidden_states: [T, hc_mult, dim]; hash_ids: [T, n_hash_cols] (all tokens, pre sequence-parallel shard); token_mask: [T], False shuts the gate so those positions pass through untouched.
Source code in vllm/models/deepseek_v41/common/engram.py
prepare_embeddings(hash_ids)
¶
Look up, or prefetch from host memory, rows before the decoder layers consume them; DP-sharded tables take the group's gathered hash IDs.
Source code in vllm/models/deepseek_v41/common/engram.py
EngramBatch
¶
Stage every Engram layer's WKV input as MXFP8 with one exchange.
Methods:
-
create–Batch DP-sharded tables whose WKV takes FlashInfer MXFP8 input.
-
prepare_embeddings–Stage every layer's WKV input before the decoder layers.
Source code in vllm/models/deepseek_v41/common/engram.py
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 | |
create(layers, max_tokens)
classmethod
¶
Batch DP-sharded tables whose WKV takes FlashInfer MXFP8 input.
Source code in vllm/models/deepseek_v41/common/engram.py
prepare_embeddings(hashes)
¶
Stage every layer's WKV input before the decoder layers.
DP-sharded tables decide on the group's slot, so every rank agrees.
Source code in vllm/models/deepseek_v41/common/engram.py
EngramLayout
¶
Bucket layout of the n-gram hash tables.
A position is hashed as max_ngram_size - 1 n-grams (2-gram .. max), each
split over n_heads heads. Every (n-gram size, head) pair owns its own
prime-sized bucket range in the layer's table; the primes are drawn in
order and never reused, which keeps the ranges disjoint.
Source code in vllm/models/deepseek_v41/common/engram.py
EngramPeerGroup
¶
GPUs of several engines that split each Engram table over their HBM.
They meet only at startup, through a file store; lookups then read peers' rows directly, so no engine's step ever waits on another's. Every GPU builds and loads the same tables in the same order, so its n-th exchange is every peer's n-th.
Methods:
-
all_gather–Every GPU's value for the next exchange in rank order, once all have
Source code in vllm/models/deepseek_v41/common/engram.py
all_gather(value)
¶
Every GPU's value for the next exchange in rank order, once all have set theirs.
Source code in vllm/models/deepseek_v41/common/engram.py
NgramHashState
¶
Bases: Module
Maps each position to the hash ids of the n-grams ending there.
Stateless: the runner supplies every lookback token id.
Methods:
-
dummy_hashes–Participate in DP lookups without valid rows.
-
ensure_cache–Return False while the KV cache is unbound (profile run); the caller
-
forward–Compute [tokens, layers, hash columns] int32 n-gram hashes.
Source code in vllm/models/deepseek_v41/common/engram.py
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 | |
dummy_hashes(input_ids)
¶
Participate in DP lookups without valid rows.
Source code in vllm/models/deepseek_v41/common/engram.py
ensure_cache()
¶
Return False while the KV cache is unbound (profile run); the caller skips engram hashing then.
forward(input_ids, positions, query_start_loc, dead_mask, lookback_token_ids, lookback_dead_mask)
¶
Compute [tokens, layers, hash columns] int32 n-gram hashes.
History comes from the current chunk, then the runner's lookback window.
Source code in vllm/models/deepseek_v41/common/engram.py
ParallelEngramEmbedding
¶
Bases: Module
Hash heads with FP8 or packed MXFP4 rows and per-block E8M0 scales.
Heads are TP-sharded, and additionally DP-sharded across a node-local group unless the table lives in (DP-shared) pinned host memory or its rows are split over a peer group's HBM. MXFP4 rows are E2M1 values, two per byte (even column in the low nibble), stored as uint8 since torch has no 4-bit dtype.
Methods:
-
collapse_huge_pages–Best-effort MADV_COLLAPSE (Linux >= 6.1) of pages that faulted small.
-
forward–indices: [num_tokens, n_hash_cols] -> [num_tokens, n_hash_cols, dim]
-
lookup–Look up local heads into BF16, or packed FP8 bytes followed by scales.
-
process_weights_after_loading–Peers read this table's rows directly, so serve only once every GPU
Source code in vllm/models/deepseek_v41/common/engram.py
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 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 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 | |
_uses_small_host_pages
property
¶
Whether the GPU maps this host table with pages too small for its TLB.
collapse_huge_pages()
¶
Best-effort MADV_COLLAPSE (Linux >= 6.1) of pages that faulted small.
Source code in vllm/models/deepseek_v41/common/engram.py
forward(indices)
¶
indices: [num_tokens, n_hash_cols] -> [num_tokens, n_hash_cols, dim] bf16, gathered from all shards for this replica's tokens.
Source code in vllm/models/deepseek_v41/common/engram.py
lookup(indices, out, background=False)
¶
Look up local heads into BF16, or packed FP8 bytes followed by scales.
background limits the grid to leave SMs for concurrent work.
Source code in vllm/models/deepseek_v41/common/engram.py
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 | |
process_weights_after_loading()
¶
Peers read this table's rows directly, so serve only once every GPU of the group has finished loading its own.
Source code in vllm/models/deepseek_v41/common/engram.py
_allocate_huge_page_storage(num_bytes)
¶
Register prefaulted huge pages, or return None for pinned-memory fallback.
Source code in vllm/models/deepseek_v41/common/engram.py
_allow_any_numa_node(address, num_bytes)
¶
Let a mapping's pages spill to any NUMA node despite --numa-bind.
mbind(MPOL_LOCAL) overrides the task's membind for this range: pages still go to the faulting CPU's node first, then to any other node.
Source code in vllm/models/deepseek_v41/common/engram.py
_engram_head_shard_weight_loader(param, loaded_weight)
¶
Load this rank's complete head buckets. ue8m0 scales arrive as float8_e8m0fnu; keep the raw bytes (the param stores uint8).
Source code in vllm/models/deepseek_v41/common/engram.py
_engram_lookup_kernel(weight, scales, ids, sorted_dst, out, vocab_start, vocab_end, num_rows, ids_stride_t, ids_stride_h, HEAD_START, LOCAL_HEADS, TOTAL_HEADS, DIM, QUANT_BLOCK, BLOCK_R, GRID, SORTED, PACKED, OUT_STRIDE, PEER_ROWS=0, WORD=tl.uint8, PACKED_FP4=False)
¶
Gather FP8 or MXFP4 rows as dequantized BF16, or FP8 rows as packed values/scales (copied WORD at a time).
Only this rank's heads are read; padded heads write zeros for all-gather.
weight/scales may address pinned host memory through UVA. If SORTED,
ids holds rows relative to vocab_start in table order and sorted_dst
their output rows.
Source code in vllm/models/deepseek_v41/common/engram.py
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 | |
_engram_lookup_thresholds(device)
¶
Host lookup thresholds on device: the rows from which to sort, and the
tokens (max over DP ranks) from which to run inline. None disables that path.
Source code in vllm/models/deepseek_v41/common/engram.py
_engram_select_rows(gathered, output, source_tokens, token_start, local_width)
¶
Copy one token window out of a rank-major gathered buffer.
Both gathers land rank-major ([rank][token][local width]); this walks the
window the rank keeps and lays its ranks out side by side as width.
Source code in vllm/models/deepseek_v41/common/engram.py
_engram_unpack_fp8_kernel(packed, values, scales, num_tokens, values_stride, scales_stride, HEADS, LOCAL_HEADS, LAYERS, RANK, DIM, BLOCK, PEER)
¶
Split [rank][token][layer][local head] packed rows into MXFP8 values and
F8_128x4 scales, one layer per grid column; packed holds per-rank
pointers if PEER. Scales past num_tokens zero-pad the 128-row tile.
Source code in vllm/models/deepseek_v41/common/engram.py
_gather_engram_rows(staged, num_tokens)
¶
Exchange DP tokens for heads, retaining only this replica's tokens.
Source code in vllm/models/deepseek_v41/common/engram.py
_is_prime(n)
¶
Deterministic Miller-Rabin for n < 2**32 (avoids a sympy import).
Source code in vllm/models/deepseek_v41/common/engram.py
_shm_shortfall(num_bytes)
¶
Why /dev/shm cannot hold a table of this size, or None if it can.
Source code in vllm/models/deepseek_v41/common/engram.py
build_compressed_token_map(tokenizer)
¶
Map every token id onto a smaller id space where tokens that normalize alike collapse together.
N-grams are hashed over these compressed ids, so " The", "the" and "THE" all hash the same way. The compressed size matters beyond bounds checking: every hash multiplier is derived from it.
Source code in vllm/models/deepseek_v41/common/engram.py
can_share_engram_tables(layout, block_size=32)
¶
Whether co-located DP replicas exist and can hold the full tables.
Source code in vllm/models/deepseek_v41/common/engram.py
compute_hash_multipliers(layer_ids, max_ngram_size, compressed_vocab_size)
¶
One multiplier per (layer, lookback), from a per-layer RNG so layers
hash differently. Kept odd and bounded so token_id * multiplier cannot
overflow int64.
Source code in vllm/models/deepseek_v41/common/engram.py
engram_gathered_num_tokens()
¶
Per-replica token slot for the node-local Engram DP group.
Source code in vllm/models/deepseek_v41/common/engram.py
engram_head_shard_rank()
¶
This rank's slot among the hash-head shards of one engram table.
TP-major, so the shards a DP gather brings in are contiguous heads and the following TP gather completes the head order.
Source code in vllm/models/deepseek_v41/common/engram.py
find_next_prime(start, seen_primes)
¶
The smallest prime above start that has not been handed out yet.
Source code in vllm/models/deepseek_v41/common/engram.py
gather_engram_hashes(hash_ids, *, dp_shared_memory=False)
¶
Collect the n-gram ids of every DP replica sharing one table.
Replicas are padded to a common token slot, so the gathered shape is static under CUDA graph capture (where DP already pads alike).
Source code in vllm/models/deepseek_v41/common/engram.py
gather_errors(group, error)
¶
Each rank's error, joined; empty if every rank succeeded.
Source code in vllm/models/deepseek_v41/common/engram.py
join_engram_peer_group(size, path)
cached
¶
This GPU's membership, shared by all of its process's tables.