Skip to navigation

nemo_automodel.components.models.hy_v4_flash.layers

View as MarkdownOpen in Claude

Norms, hyper-connections and interleaved multimodal RoPE of Hy4-flash.

Semantics follow the transformers reference (PR 50903, modeling_hy_v4_flash.py):

  • HYV4FlashRMSNorm casts the normalized activations back to the input dtype before the weight multiply.
  • The hyper-connection (iHC) has no Sinkhorn mixing: a sigmoid pre weight collapses the hc_mult streams and a sigmoid post weight scatters the sub-layer output back, the old streams passing through unchanged.
  • RoPE rotates interleaved pairs of the trailing qk_rope_head_dim channels, with the time/height/width planes of M-RoPE interleaved in the frequency axis (Qwen3-VL style).

Module Contents

Classes

NameDescription
HYV4FlashHyperConnectioniHC site: collapse hc_mult residual streams to one input and produce the scatter weights.
HYV4FlashHyperHeadFinal collapse of the streams before the output norm.
HYV4FlashRMSNormRMSNorm that multiplies the weight after casting back to the activation dtype.

Functions

NameDescription
_hc_collapse_coreCollapse the residual streams with sigmoid pre weights and produce the post scatter weights.
_hc_expand_coreScatter a sub-layer output onto the streams: post * output + residual in fp32.
_rms_norm_coreNormalize the last axis in fp32, cast back, then apply the weight (reference order).
apply_interleaved_ropeRotate interleaved channel pairs (x[2i], x[2i + 1]) of the last axis in fp32.
compile_hc_coresWrap the hyper-connection collapse and expand cores with torch.compile (BackendConfig.compile_hc).
compile_norm_coreWrap the fp32 RMSNorm core with torch.compile (BackendConfig.compile_norm); runs once per process.
inverse_frequenciesfp32 inverse frequencies theta ** (-2i / rope_dim) of shape [rope_dim // 2], computed from the config.
mrope_cos_sinInterleaved M-RoPE tables.

Data

_HC_COMPILED

_NORM_COMPILED

_hc_collapse

_hc_expand

_rms_norm

API

class nemo_automodel.components.models.hy_v4_flash.layers.HYV4FlashHyperConnection(
)

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.

base
fn
hc_eps
= config.hc_eps
hc_magnitude
= config.hc_magnitude
hc_mult
= config.hc_mult
norm_eps
= config.rms_norm_eps
scale
nemo_automodel.components.models.hy_v4_flash.layers.HYV4FlashHyperConnection.expand(
post: torch.Tensor,
output: torch.Tensor,
residual: torch.Tensor
) -> torch.Tensor
staticmethod

Scatter a sub-layer output onto the streams; shapes as in _hc_expand_core.

nemo_automodel.components.models.hy_v4_flash.layers.HYV4FlashHyperConnection.forward(
streams: torch.Tensor
) -> tuple[torch.Tensor, torch.Tensor]

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).

nemo_automodel.components.models.hy_v4_flash.layers.HYV4FlashHyperConnection.init_weights(
initializer_range: float
) -> None

Initialize as the reference does: small fn, scale 0.01 and a -log(hc_mult - 1) pre bias.

class nemo_automodel.components.models.hy_v4_flash.layers.HYV4FlashHyperHead(
)

Bases: Module

Final collapse of the streams before the output norm.

eps
= config.hc_eps
hc_base
hc_fn
hc_mult
= config.hc_mult
hc_scale
norm_eps
= config.rms_norm_eps
nemo_automodel.components.models.hy_v4_flash.layers.HYV4FlashHyperHead.forward(
streams: torch.Tensor
) -> torch.Tensor

Collapse the streams.

Parameters:

streams
torch.Tensor

Tensor of shape [batch, sequence, hc_mult, hidden].

Returns: torch.Tensor

Tensor of shape [batch, sequence, hidden] in the dtype of streams.

nemo_automodel.components.models.hy_v4_flash.layers.HYV4FlashHyperHead.init_weights(
initializer_range: float
) -> None

Initialize as the reference does.

class nemo_automodel.components.models.hy_v4_flash.layers.HYV4FlashRMSNorm(
hidden_size: int,
eps: float = 1e-05,
dtype: torch.dtype | None = None,
static_shape: bool = True
)

Bases: Module

RMSNorm that multiplies the weight after casting back to the activation dtype.

Parameters:

hidden_size
int

Size of the normalized axis.

eps
floatDefaults to 1e-05

Added to the mean square.

dtype
torch.dtype | NoneDefaults to None

Dtype of the weight.

static_shape
boolDefaults to True

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.

weight
= nn.Parameter(torch.ones(hidden_size, dtype=dtype))
nemo_automodel.components.models.hy_v4_flash.layers.HYV4FlashRMSNorm.forward(
hidden_states: torch.Tensor
) -> torch.Tensor

Normalize the last axis.

Parameters:

hidden_states
torch.Tensor

Tensor of shape […, hidden], with arbitrary leading dimensions.

Returns: torch.Tensor

Tensor of the same shape and dtype as hidden_states.

nemo_automodel.components.models.hy_v4_flash.layers._hc_collapse_core(
streams: torch.Tensor,
fn: torch.Tensor,
base: torch.Tensor,
scale: torch.Tensor,
norm_eps: float,
hc_eps: float,
hc_magnitude: float
) -> tuple[torch.Tensor, torch.Tensor]

Collapse the residual streams with sigmoid pre weights and produce the post scatter weights.

Parameters:

streams
torch.Tensor

Tensor of shape [batch, sequence, hc_mult, hidden].

fn
torch.Tensor

fp32 tensor of shape [2 * hc_mult, hc_mult * hidden] (pre rows first, then post rows).

base
torch.Tensor

fp32 tensor of shape [2 * hc_mult].

scale
torch.Tensor

fp32 tensor of shape [2] (pre scale, post scale).

norm_eps
float

Epsilon of the unweighted RMS of the flattened streams.

hc_eps
float

Offset added to both weight groups.

hc_magnitude
float

Range of the post weights.

Returns: torch.Tensor

post: fp32 tensor of shape [batch, sequence, hc_mult]; collapsed: tensor of shape

nemo_automodel.components.models.hy_v4_flash.layers._hc_expand_core(
post: torch.Tensor,
output: torch.Tensor,
residual: torch.Tensor
) -> torch.Tensor

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.

nemo_automodel.components.models.hy_v4_flash.layers._rms_norm_core(
hidden_states: torch.Tensor,
weight: torch.Tensor,
eps: float
) -> torch.Tensor

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.

nemo_automodel.components.models.hy_v4_flash.layers.apply_interleaved_rope(
x: torch.Tensor,
cos: torch.Tensor,
sin: torch.Tensor
) -> torch.Tensor

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.

nemo_automodel.components.models.hy_v4_flash.layers.compile_hc_cores() -> None

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.

nemo_automodel.components.models.hy_v4_flash.layers.compile_norm_core() -> None

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.

nemo_automodel.components.models.hy_v4_flash.layers.inverse_frequencies(
rope_dim: int,
theta: float
) -> torch.Tensor

fp32 inverse frequencies theta ** (-2i / rope_dim) of shape [rope_dim // 2], computed from the config.

nemo_automodel.components.models.hy_v4_flash.layers.mrope_cos_sin(
position_ids: torch.Tensor,
inv_freq: torch.Tensor,
mrope_section: list[int],
dtype: torch.dtype
) -> tuple[torch.Tensor, torch.Tensor]

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:

position_ids
torch.Tensor

Integer tensor of shape [3, batch, sequence] (time, height, width positions).

inv_freq
torch.Tensor

fp32 tensor of shape [rope_dim // 2].

mrope_section
list[int]

Three entries summing to rope_dim // 2.

dtype
torch.dtype

Output dtype.

Returns: tuple[torch.Tensor, torch.Tensor]

(cos, sin), each of shape [batch, sequence, rope_dim // 2] (one value per rotated pair).

nemo_automodel.components.models.hy_v4_flash.layers._HC_COMPILED = False
nemo_automodel.components.models.hy_v4_flash.layers._NORM_COMPILED = False
nemo_automodel.components.models.hy_v4_flash.layers._hc_collapse = _hc_collapse_core
nemo_automodel.components.models.hy_v4_flash.layers._hc_expand = _hc_expand_core
nemo_automodel.components.models.hy_v4_flash.layers._rms_norm = _rms_norm_core