vllm.models.deepseek_v4_1.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:
-
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–The n-gram hash table, sharded by complete hash heads over TP ranks.
Functions:
-
build_compressed_token_map–Map every token id onto a smaller id space where tokens that normalize
-
compute_hash_multipliers–One multiplier per (layer, lookback), from a per-layer RNG so layers
-
find_next_prime–The smallest prime above
startthat has not been handed out yet.
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–Gather this layer's rows on the main stream before decoder layers.
Source code in vllm/models/deepseek_v4_1/common/engram.py
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 1027 1028 1029 1030 1031 1032 1033 | |
embed(hash_ids) ¶
Gather heads, returning only local tokens when SP is enabled.
Source code in vllm/models/deepseek_v4_1/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_v4_1/common/engram.py
prepare_embeddings(hash_ids) ¶
Gather this layer's rows on the main stream before decoder layers.
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_v4_1/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:
-
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_v4_1/common/engram.py
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 | |
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_v4_1/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_v4_1/common/engram.py
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 | |
ParallelEngramEmbedding ¶
Bases: Module
The n-gram hash table, sharded by complete hash heads over TP ranks. Rows stay fp8 and are dequantized with ue8m0 per-32 scales on lookup.
With cpu_offload the shard lives in pinned host memory and is read over UVA instead of HBM; the TP sharding is unchanged either way.
Methods:
-
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_v4_1/common/engram.py
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 | |
_storage() ¶
Parameters when resident, else cached UVA views of the pinned shard.
Rebuilt if anything swaps .data, so a stale device pointer cannot survive silently.
Source code in vllm/models/deepseek_v4_1/common/engram.py
forward(indices) ¶
indices: [num_tokens, n_hash_cols] -> [num_tokens, n_hash_cols, dim] bf16, gathered from all TP shards.
Source code in vllm/models/deepseek_v4_1/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_v4_1/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_v4_1/common/engram.py
_engram_lookup_kernel(weight, scales, ids, 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) ¶
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.
Source code in vllm/models/deepseek_v4_1/common/engram.py
_is_prime(n) ¶
Deterministic Miller-Rabin for n < 2**32 (avoids a sympy import).
Source code in vllm/models/deepseek_v4_1/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_v4_1/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_v4_1/common/engram.py
find_next_prime(start, seen_primes) ¶
The smallest prime above start that has not been handed out yet.