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.
-
NgramHashState–Maps each position to the hash ids of the n-grams ending there.
-
ParallelEngramEmbedding–Hash heads with FP8 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.
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
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 | |
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
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 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 1716 1717 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 | |
_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
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 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 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 | |
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
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
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 | |
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 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.
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.
Source code in vllm/models/deepseek_v41/common/engram.py
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 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 | |
_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
_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)
¶
Gather FP8 rows as packed values/scales or dequantized BF16.
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
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 | |
_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.