> This page is for version 0.5.0 · 26.06.
> For other versions, use one of these documentation indexes:
> - Nightly (default): https://docs.nvidia.com/nemo/automodel/nightly/llms.txt
> - Latest: https://docs.nvidia.com/nemo/automodel/latest/llms.txt
> - 0.7.0 · 26.10: https://docs.nvidia.com/nemo/automodel/v0.7/llms.txt
> - 0.5.0 · 26.06: https://docs.nvidia.com/nemo/automodel/v0.5/llms.txt
> - 0.4.0 · 26.04: https://docs.nvidia.com/nemo/automodel/v0.4/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/automodel/llms.txt.

# 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`):

* `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

| Name                                                                                                        | Description                                                                                 |
| ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------- |
| [`HYV4FlashHyperConnection`](#nemo_automodel-components-models-hy_v4_flash-layers-HYV4FlashHyperConnection) | iHC site: collapse `hc_mult` residual streams to one input and produce the scatter weights. |
| [`HYV4FlashHyperHead`](#nemo_automodel-components-models-hy_v4_flash-layers-HYV4FlashHyperHead)             | Final collapse of the streams before the output norm.                                       |
| [`HYV4FlashRMSNorm`](#nemo_automodel-components-models-hy_v4_flash-layers-HYV4FlashRMSNorm)                 | RMSNorm that multiplies the weight after casting back to the activation dtype.              |

### Functions

| Name                                                                                                    | Description                                                                                               |
| ------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------- |
| [`_hc_collapse_core`](#nemo_automodel-components-models-hy_v4_flash-layers-_hc_collapse_core)           | Collapse the residual streams with sigmoid `pre` weights and produce the `post` scatter weights.          |
| [`_hc_expand_core`](#nemo_automodel-components-models-hy_v4_flash-layers-_hc_expand_core)               | Scatter a sub-layer output onto the streams: `post * output + residual` in fp32.                          |
| [`_rms_norm_core`](#nemo_automodel-components-models-hy_v4_flash-layers-_rms_norm_core)                 | Normalize the last axis in fp32, cast back, then apply the weight (reference order).                      |
| [`apply_interleaved_rope`](#nemo_automodel-components-models-hy_v4_flash-layers-apply_interleaved_rope) | Rotate interleaved channel pairs `(x[2i], x[2i + 1])` of the last axis in fp32.                           |
| [`compile_hc_cores`](#nemo_automodel-components-models-hy_v4_flash-layers-compile_hc_cores)             | Wrap the hyper-connection collapse and expand cores with `torch.compile` (`BackendConfig.compile_hc`).    |
| [`compile_norm_core`](#nemo_automodel-components-models-hy_v4_flash-layers-compile_norm_core)           | Wrap the fp32 RMSNorm core with `torch.compile` (`BackendConfig.compile_norm`); runs once per process.    |
| [`inverse_frequencies`](#nemo_automodel-components-models-hy_v4_flash-layers-inverse_frequencies)       | fp32 inverse frequencies `theta ** (-2i / rope_dim)` of shape \[rope_dim // 2], computed from the config. |
| [`mrope_cos_sin`](#nemo_automodel-components-models-hy_v4_flash-layers-mrope_cos_sin)                   | Interleaved M-RoPE tables.                                                                                |

### Data

[`_HC_COMPILED`](#nemo_automodel-components-models-hy_v4_flash-layers-_HC_COMPILED)

[`_NORM_COMPILED`](#nemo_automodel-components-models-hy_v4_flash-layers-_NORM_COMPILED)

[`_hc_collapse`](#nemo_automodel-components-models-hy_v4_flash-layers-_hc_collapse)

[`_hc_expand`](#nemo_automodel-components-models-hy_v4_flash-layers-_hc_expand)

[`_rms_norm`](#nemo_automodel-components-models-hy_v4_flash-layers-_rms_norm)

### API

```python
class nemo_automodel.components.models.hy_v4_flash.layers.HYV4FlashHyperConnection(
    config: nemo_automodel.components.models.hy_v4_flash.config.HYV4FlashTextConfig
)
```

**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`**

---

```python
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`.

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

```python
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.

```python
class nemo_automodel.components.models.hy_v4_flash.layers.HYV4FlashHyperHead(
    config: nemo_automodel.components.models.hy_v4_flash.config.HYV4FlashTextConfig
)
```

**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`

---

```python
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`.

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

Initialize as the reference does.

```python
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`** `float` — default: 1e-05

Added to the mean square.

---

**`dtype`** `torch.dtype | None` — default: None

Dtype of the weight.

---

**`static_shape`** `bool` — default: 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))`

---

```python
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`.

```python
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

```python
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`.

```python
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.

```python
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`.

```python
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.

```python
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.

```python
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.

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

```python
nemo_automodel.components.models.hy_v4_flash.layers._HC_COMPILED = False
```

```python
nemo_automodel.components.models.hy_v4_flash.layers._NORM_COMPILED = False
```

```python
nemo_automodel.components.models.hy_v4_flash.layers._hc_collapse = _hc_collapse_core
```

```python
nemo_automodel.components.models.hy_v4_flash.layers._hc_expand = _hc_expand_core
```

```python
nemo_automodel.components.models.hy_v4_flash.layers._rms_norm = _rms_norm_core
```