nemo_automodel.components.models.hy_v4_flash.layers
nemo_automodel.components.models.hy_v4_flash.layers
Norms, hyper-connections and interleaved multimodal RoPE of Hy4-flash.
Semantics follow the transformers reference (PR 50903, modeling_hy_v4_flash.py):
HYV4FlashRMSNormcasts the normalized activations back to the input dtype before the weight multiply.- The hyper-connection (iHC) has no Sinkhorn mixing: a sigmoid
preweight collapses thehc_multstreams and a sigmoidpostweight scatters the sub-layer output back, the old streams passing through unchanged. - RoPE rotates interleaved pairs of the trailing
qk_rope_head_dimchannels, with the time/height/width planes of M-RoPE interleaved in the frequency axis (Qwen3-VL style).
Module Contents
Classes
Functions
Data
API
Bases: Module
iHC site: collapse hc_mult residual streams to one input and produce the scatter weights.
All parameters are fp32 and the arithmetic runs in fp32 whatever the activation dtype.
Scatter a sub-layer output onto the streams; shapes as in _hc_expand_core.
Compute the collapsed sub-layer input and the post weights of streams [batch, sequence, hc_mult, hidden].
Returns post (fp32 [batch, sequence, hc_mult]) and collapsed ([batch, sequence, hidden], input dtype).
Initialize as the reference does: small fn, scale 0.01 and a -log(hc_mult - 1) pre bias.
Bases: Module
Final collapse of the streams before the output norm.
Collapse the streams.
Parameters:
Tensor of shape [batch, sequence, hc_mult, hidden].
Returns: torch.Tensor
Tensor of shape [batch, sequence, hidden] in the dtype of streams.
Initialize as the reference does.
Bases: Module
RMSNorm that multiplies the weight after casting back to the activation dtype.
Parameters:
Size of the normalized axis.
Added to the mean square.
Dtype of the weight.
Whether every call sees the same input shape. The compiled core (BackendConfig.compile_norm)
specializes on the shape, so a norm whose input length follows the data (the indexer’s pooled group
keys: one call per document) passes False and always runs the eager core.
Normalize the last axis.
Parameters:
Tensor of shape […, hidden], with arbitrary leading dimensions.
Returns: torch.Tensor
Tensor of the same shape and dtype as hidden_states.
Collapse the residual streams with sigmoid pre weights and produce the post scatter weights.
Parameters:
Tensor of shape [batch, sequence, hc_mult, hidden].
fp32 tensor of shape [2 * hc_mult, hc_mult * hidden] (pre rows first, then post rows).
fp32 tensor of shape [2 * hc_mult].
fp32 tensor of shape [2] (pre scale, post scale).
Epsilon of the unweighted RMS of the flattened streams.
Offset added to both weight groups.
Range of the post weights.
Returns: torch.Tensor
post: fp32 tensor of shape [batch, sequence, hc_mult]; collapsed: tensor of shape
Scatter a sub-layer output onto the streams: post * output + residual in fp32.
post is fp32 [batch, sequence, hc_mult], output [batch, sequence, hidden] and residual
[batch, sequence, hc_mult, hidden]; the result has the shape and dtype of residual.
Normalize the last axis in fp32, cast back, then apply the weight (reference order).
hidden_states has shape […, hidden] and weight [hidden]; the result keeps the input’s shape and dtype.
Rotate interleaved channel pairs (x[2i], x[2i + 1]) of the last axis in fp32.
x has shape […, rope_dim]; cos and sin broadcast to […, rope_dim // 2] (e.g.
[tokens, 1, rope_dim // 2] for per-head queries). The result keeps the shape and dtype of x.
Wrap the hyper-connection collapse and expand cores with torch.compile (BackendConfig.compile_hc).
Runs once per process, static shapes: a new sequence length compiles the two cores again, so the knob is meant
for fixed-length batches (padded or packed). No fullgraph: past the recompile limit a core runs eager.
Wrap the fp32 RMSNorm core with torch.compile (BackendConfig.compile_norm); runs once per process.
Static shapes (one specialization per norm site shape and grad mode) and no fullgraph: past dynamo’s
recompile limit a shape runs eager instead of raising.
fp32 inverse frequencies theta ** (-2i / rope_dim) of shape [rope_dim // 2], computed from the config.
Interleaved M-RoPE tables.
The frequency axis takes the time plane everywhere except every third slot (starting at 1 for height and
at 2 for width) within 3 * section entries, which take the height / width planes.
Parameters:
Integer tensor of shape [3, batch, sequence] (time, height, width positions).
fp32 tensor of shape [rope_dim // 2].
Three entries summing to rope_dim // 2.
Output dtype.
Returns: tuple[torch.Tensor, torch.Tensor]
(cos, sin), each of shape [batch, sequence, rope_dim // 2] (one value per rotated pair).