nemo_automodel.components.models.deepseek_v41.engram
nemo_automodel.components.models.deepseek_v41.engram
DeepSeek-V4.1 compressed-token Engram hashes and residual memory lookup.
The tokenizer normalization, per-layer hash seeds, and signed square-root gate
follow DeepSeek’s released inference/engram.py and inference/model.py.
V4.1 omits the short convolution used by earlier Engram architectures.
Module Contents
Classes
Functions
Data
API
Bases: Module
Read a row-owner-sharded Engram table and update all HC residual streams.
Parameters:
Text configuration containing logical table sizes and HC width.
Zero-based decoder layer ID, present in engram_layer_ids.
Linear backend for the fused key/value projection.
Runtime row-owner group. None creates the complete table and is appropriate only when the configuration fits on one device.
Whether hash IDs are checked with device-side asserts (CUDA, strict validation off).
The choice depends on the device and the table’s flag only, never on this rank’s request count: an empty requester must take the same path, and the same collectives, as its peers.
Parameters:
Integer tensor of shape [batch, sequence, hash_heads]; only its device matters here.
Returns: bool
True for the device-side path, False for the host-side, cross-rank-symmetric checks.
Inject the normalized, signed-square-root-gated memory residual.
Parameters:
Tensor of shape [batch, sequence, hc_mult, hidden].
Integer tensor of shape [batch, sequence, hash_heads] containing logical table rows for this Engram layer. Under CP both inputs contain only this rank’s local sequence positions.
Optional bool tensor of shape [batch, sequence], with False for image/padding positions that must remain unchanged.
Returns: torch.Tensor
Tensor of shape [batch, sequence, hc_mult, hidden] in the input
Initialize the table, projection, and learned branch normalization weights.
Bases: Module
Build all Engram layers’ hashes from complete, uncached input sequences.
Parameters:
Text configuration containing the released Engram dimensions.
Fast tokenizer whose normalized vocabulary must have exactly
config.engram_compressed_vocab_size entries.
Hash sequences without crossing document, image, or padding boundaries.
Parameters:
Integer tensor of shape [batch, sequence] containing raw tokenizer IDs for complete sequences.
Optional integer document IDs [batch, sequence]; zero marks padding.
Optional boolean tensor of shape [batch, sequence]. False marks image or padding tokens and blocks all lookback through those positions. The caller also masks their residual gate.
Returns: torch.Tensor
Integer tensor of shape [batch, sequence, engram_layers, hash_heads],
Restore derived integer buffers after meta-device materialization.
Normalize decoded vocabulary entries using the released token-ID contract.
Inject the normalized, signed-square-root-gated memory value into the fp32 streams.
Parameters:
Tensor of shape [batch, sequence, hc_mult, hidden] in the model dtype.
Tensor of shape [batch, sequence, hc_mult * hidden], the key half of the wkv projection.
Tensor of shape [batch, sequence, hidden], the value half of the wkv projection.
Learned branch normalization weight of shape [hc_mult, hidden].
Learned branch normalization weight of shape [hc_mult, hidden].
RMS normalization epsilon.
Optional bool tensor of shape [batch, sequence]; masked tokens keep their streams.
Returns: torch.Tensor
Tensor of shape [batch, sequence, hc_mult, hidden] in the dtype of hidden_states.
Find the first unused prime above start without an optional dependency.
Wrap the Engram gate core with torch.compile (BackendConfig.compile_engram).
Runs once per process; static shapes (dynamic=False): one specialization per Engram call shape and mask
presence. No fullgraph: past dynamo’s recompile limit a shape runs eager instead of raising. Inductor fuses
the three reductions over the hidden dimension (stream and key RMS, stream-key dot) into one kernel that reads
the fp32 streams once, the gated residual into another, and the backward through AOTAutograd; the eager chain
is about a dozen elementwise / reduction kernels per Engram layer plus the pow backward’s full-size clone.
Numerics are allclose to eager, not bitwise (fused reductions change the summation order).