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.
Two pieces of cross-forward state are needed because vLLM streams tokens
chunk-by-chunk while an n-gram at position p needs the token ids at
p-1..p-3:
token_map: token id -> compressed vocab id, built once from the model's tokenizer at init (deterministic; asserted againstengram_compressed_vocab_size).hash_cache: one int32 slot per KV slot of the first local layer's sliding-window cache, holding the compressed id (or DEAD) of the token last written to that slot. Slots are stable per (request, position) — the block table pins a position to a physical slot, prefix-cache hits reuse both the physical blocks and the identical token ids, and spec-decode rollbacks rewrite the same slots — so lookbacks read back exactly what the owning request wrote. Lookback depth (3) is far inside the sliding window (128), so window eviction never frees a block a live lookback still needs.
Slots are not part of the KV cache, so KV loaded from another instance
(P/D, offload connectors) leaves them unwritten. The runner therefore
passes lookback_token_ids, the ids just before each request's chunk
start, which take precedence over the slots. The V2 runner reads them
from its device-resident token history and needs no slot cache; the V1
runner's CPU token table holds placeholders for generated tokens under
async scheduling, so it passes prompt positions only and keeps the slot
cache for the rest.
Classes:
-
DPSharedEngramStorage–Registered host weights shared by a node-local DP group with one writer.
-
Engram–Writes an n-gram lookup into the residual stream, gated by how well it
-
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 /dev/shm 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.
DPSharedEngramStorage
¶
Registered host weights shared by a node-local DP group with one writer.
Source code in vllm/models/deepseek_v41/common/engram.py
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 | |
_allocate(num_bytes)
¶
Map and register one physical allocation across a node-local DP group.
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
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 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 1533 1534 1535 1536 1537 | |
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
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 | |
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
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 on the V2 runner, which supplies every lookback token id. On
the V1 runner it also keeps hash_cache, the slot-keyed rolling store
of compressed ids (see module docstring), for generated tokens.
Methods:
-
dummy_hashes–Participate in DP lookups without valid rows or hash-cache updates.
-
ensure_cache–Lazily size the slot-keyed cache from the bound SWA KV cache.
-
forward–Compute [tokens, layers, hash columns] int32 n-gram hashes.
Source code in vllm/models/deepseek_v41/common/engram.py
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 | |
dummy_hashes(input_ids)
¶
Participate in DP lookups without valid rows or hash-cache updates.
Source code in vllm/models/deepseek_v41/common/engram.py
ensure_cache()
¶
Lazily size the slot-keyed cache from the bound SWA KV cache.
Returns False while the KV cache is unbound (profile run); the caller skips engram hashing then. Without the slot cache only that check remains.
Source code in vllm/models/deepseek_v41/common/engram.py
forward(input_ids, positions, query_start_loc, dead_mask, lookback_token_ids, lookback_dead_mask, slot_mapping, block_table)
¶
Compute [tokens, layers, hash columns] int32 n-gram hashes.
History comes from the current chunk, then the runner's lookback window, then the optional V1 slot cache. V2 needs only one launch.
Source code in vllm/models/deepseek_v41/common/engram.py
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 | |
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 of [T, heads] into [T, local_heads, dim] bf16.
Source code in vllm/models/deepseek_v41/common/engram.py
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 | |
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 of [T, heads] into [T, local_heads, dim] bf16.
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
_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)
¶
Gather fp8 rows, apply their ue8m0 block scales, write 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
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 | |
_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[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
_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
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 /dev/shm 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).