Skip to navigation

nemo_automodel.components.models.hy_v4_flash.engram

View as MarkdownOpen in Claude

Hy4-flash Engram: hashed bigram / trigram memory added to the hyper-connection streams.

The hash, the per-head norms and the gate follow the transformers reference (HYV4FlashEngramLayer); the table is the shared row-owner-sharded embedding of Qwen3.8-Flash-Next (one ~32 GB table per layer, sharded by rows across the owner group, lookups by all-to-all). Parameters keep the reference’s names; the forward stacks the per-head weights so each norm and projection runs as one kernel.

Precision: the weighted norms are evaluated as the reference’s nn.RMSNorm. The gate, and the sum of the gated signal and the per-stream branch, are evaluated in fp32 and cast back, where the reference stays in BF16; in fp32 the two are identical.

Module Contents

Classes

NameDescription
HYV4FlashEngramLayerOne Engram block (decoder layer layer_idx, one-based id layer_idx + 1).
HYV4FlashWeightOnlyNormHolder of an RMSNorm weight whose epsilon is the dtype epsilon (nn.RMSNorm(eps=None) semantics).

Functions

NameDescription
build_ngram_tokensStack each token with the max_ngram_size - 1 tokens before it.
draw_hash_multipliersOdd multipliers an n-gram is mixed with, drawn as the memory was built.
weighted_rms_normApply one weighted RMSNorm per slice of the second-to-last axis, as torch.nn.RMSNorm evaluates it.

API

class nemo_automodel.components.models.hy_v4_flash.engram.HYV4FlashEngramLayer(
layer_idx: int,
process_group: torch.distributed.ProcessGroup | None = None,
dtype: torch.dtype = torch.bfloat16
)

Bases: Module

One Engram block (decoder layer layer_idx, one-based id layer_idx + 1).

Parameters:

config
HYV4FlashTextConfig

Text configuration.

layer_idx
int

Zero-based decoder layer index; must satisfy layer_idx + 1 in config.engram_layer_ids.

process_group
dist.ProcessGroup | NoneDefaults to None

Row-owner group of the table; None builds the whole table locally (tests, tiny configs).

dtype
torch.dtypeDefaults to torch.bfloat16

Parameter dtype.

_head_vocab_sizes
= tuple(sizes)
_multipliers_drawn_for
tuple[int, int] | None = None
hash_multipliers
hc_mult
= config.hc_mult
head_offsets
head_vocab_sizes
heads_per_order
= config.engram_n_head_per_ngram
hidden_size
= config.hidden_size
initializer_range
= config.initializer_range
key_projs
layer_id
= layer_idx + 1
lookup_per_head_norms
lookup_table
max_ngram_size
= config.engram_max_ngram_size
multi_head_embedding
= nn.Module()
norm_key
norm_query
num_heads
= len(sizes)
num_rows
= sum(sizes)
pad_id
= config.engram_pad_id
seed
= config.engram_seed
unified_per_hc_scales
unified_pre_gamma
value_proj
nemo_automodel.components.models.hy_v4_flash.engram.HYV4FlashEngramLayer._lookup_table_state() -> tuple[int, int]

Identity of the current lookup-table contents without reading them: storage address and version counter.

Any way a checkpoint can reach the buffer changes one of the two: an in-place write (load_state_dict, copy_) bumps the version, a replaced or moved buffer has a new address. Weight initialization runs before the checkpoint is loaded, so multipliers drawn at initialization must not be kept once the frozen vocabulary map arrives; comparing this state on every forward costs no device read.

nemo_automodel.components.models.hy_v4_flash.engram.HYV4FlashEngramLayer.forward(
streams: torch.Tensor,
ngram_tokens: torch.Tensor,
engram_mask: torch.Tensor
) -> torch.Tensor

Return the update to add to streams [batch, sequence, hc_mult, hidden] (same shape and dtype).

ngram_tokens is an integer tensor [batch, sequence, max_ngram_size]; positions where engram_mask [batch, sequence] is False (image / padding) get zero.

nemo_automodel.components.models.hy_v4_flash.engram.HYV4FlashEngramLayer.hash_ngrams(
ngram_tokens: torch.Tensor
) -> torch.Tensor

Mix shifted token ids into one global table row per hash head.

Parameters:

ngram_tokens
torch.Tensor

Integer tensor of shape [batch, sequence, max_ngram_size], newest token first.

Returns: torch.Tensor

int64 tensor of shape [batch, sequence, num_heads] of rows of the layer’s flat table, n-gram-order major.

nemo_automodel.components.models.hy_v4_flash.engram.HYV4FlashEngramLayer.init_weights() -> None

Initialize as the reference does and refresh the hash state.

unified_pre_gamma is 1 and the per-stream scales are drawn from N(0, 0.006).

nemo_automodel.components.models.hy_v4_flash.engram.HYV4FlashEngramLayer.refresh_hash_state() -> None

Re-draw the hash multipliers from the loaded lookup table.

The multipliers are bounded by the number of distinct compressed ids, which is max(lookup_table) + 1 of the checkpoint’s frozen vocabulary map (the reference draws them after loading for the same reason).

class nemo_automodel.components.models.hy_v4_flash.engram.HYV4FlashWeightOnlyNorm(
size: int,
dtype: torch.dtype
)

Bases: Module

Holder of an RMSNorm weight whose epsilon is the dtype epsilon (nn.RMSNorm(eps=None) semantics).

weight
= nn.Parameter(torch.ones(size, dtype=dtype))
nemo_automodel.components.models.hy_v4_flash.engram.build_ngram_tokens(
input_ids: torch.Tensor,
document_positions: torch.Tensor,
max_ngram_size: int,
pad_id: int
) -> torch.Tensor

Stack each token with the max_ngram_size - 1 tokens before it.

Positions that reach before the start of their document read pad_id; the document start is where the leading (time) position plane restarts, so n-grams never span two packed documents.

Parameters:

input_ids
torch.Tensor

Integer tensor of shape [batch, sequence].

document_positions
torch.Tensor

Integer tensor of shape [batch, sequence], the time plane of the position ids.

max_ngram_size
int

Longest n-gram order.

pad_id
int

Id used for missing history.

Returns: torch.Tensor

Integer tensor of shape [batch, sequence, max_ngram_size], newest token first.

nemo_automodel.components.models.hy_v4_flash.engram.draw_hash_multipliers(
seed: int,
layer_id: int,
max_ngram_size: int,
compressed_vocab_size: int
) -> torch.Tensor

Odd multipliers an n-gram is mixed with, drawn as the memory was built.

NumPy’s default generator seeded with seed + 10007 * layer_id (layer_id is the one-based id used in the config) draws max_ngram_size values bounded so a product with any compressed id stays within int64.

Returns: torch.Tensor

int64 tensor of shape [max_ngram_size].

nemo_automodel.components.models.hy_v4_flash.engram.weighted_rms_norm(
x: torch.Tensor,
norms: torch.nn.ModuleList
) -> torch.Tensor

Apply one weighted RMSNorm per slice of the second-to-last axis, as torch.nn.RMSNorm evaluates it.

The normalization and the multiplication by the weight both run in fp32 and the result is cast once (for a BF16 input F.rms_norm(x) * weight would round before the weight is applied).

Parameters:

x
torch.Tensor

Tensor of shape […, slices, features].

norms
nn.ModuleList

slices modules, each holding a weight of shape [features].

Returns: torch.Tensor

Tensor of the shape and dtype of x.