> This page is for version Nightly (default).
> 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.glm5_next.optimized_kernels

Optional model-owned mHC and RMSNorm compilation for GLM-5.3-Flash.

## Module Contents

### Classes

| Name                                                                               | Description                                                       |
| ---------------------------------------------------------------------------------- | ----------------------------------------------------------------- |
| [`HcCores`](#nemo_automodel-components-models-glm5_next-optimized_kernels-HcCores) | The three mHC functions selected together for one model instance. |

### Functions

| Name                                                                                                                   | Description                                                                                 |
| ---------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------- |
| [`_compile_options`](#nemo_automodel-components-models-glm5_next-optimized_kernels-_compile_options)                   | Precision-preserving options for the installed Inductor, or `None` if it cannot honor them. |
| [`_compile_options_for`](#nemo_automodel-components-models-glm5_next-optimized_kernels-_compile_options_for)           | Inductor options that preserve the eager BF16 truncation points between fused operations.   |
| [`_division_rounding_option`](#nemo_automodel-components-models-glm5_next-optimized_kernels-_division_rounding_option) | Name Inductor's eager division-rounding control as the given PyTorch spells it.             |
| [`_expand_core_four_streams`](#nemo_automodel-components-models-glm5_next-optimized_kernels-_expand_core_four_streams) | Fuse four-stream BF16 mixing with the expansion's existing cast boundaries.                 |
| [`collapse_core`](#nemo_automodel-components-models-glm5_next-optimized_kernels-collapse_core)                         | Accumulate FP32 pre-weighted streams, then round the output once.                           |
| [`expand_core`](#nemo_automodel-components-models-glm5_next-optimized_kernels-expand_core)                             | Keep coefficient, product, BMM output and sum rounding in GLM's order.                      |
| [`gated_norm_core`](#nemo_automodel-components-models-glm5_next-optimized_kernels-gated_norm_core)                     | Normalize and sigmoid-gate KDA output, preserving eager cast boundaries.                    |
| [`hc_cores`](#nemo_automodel-components-models-glm5_next-optimized_kernels-hc_cores)                                   | Select per-instance mHC functions without changing eager model instances.                   |
| [`norm_core`](#nemo_automodel-components-models-glm5_next-optimized_kernels-norm_core)                                 | Round normalized values before multiplying the norm weight, as GLM does.                    |
| [`project_core`](#nemo_automodel-components-models-glm5_next-optimized_kernels-project_core)                           | Normalize the flattened streams in FP32 before the coefficient GEMM.                        |
| [`rms_norm_core`](#nemo_automodel-components-models-glm5_next-optimized_kernels-rms_norm_core)                         | Select the decoder/final RMSNorm core, retaining GLM's cast order.                          |
| [`rms_norm_gated_core`](#nemo_automodel-components-models-glm5_next-optimized_kernels-rms_norm_gated_core)             | Select the KDA gated norm core with the same cast policy as decoder norms.                  |

### Data

[`CollapseCore`](#nemo_automodel-components-models-glm5_next-optimized_kernels-CollapseCore)

[`ExpandCore`](#nemo_automodel-components-models-glm5_next-optimized_kernels-ExpandCore)

[`GatedNormCore`](#nemo_automodel-components-models-glm5_next-optimized_kernels-GatedNormCore)

[`NormCore`](#nemo_automodel-components-models-glm5_next-optimized_kernels-NormCore)

[`ProjectionCore`](#nemo_automodel-components-models-glm5_next-optimized_kernels-ProjectionCore)

[`logger`](#nemo_automodel-components-models-glm5_next-optimized_kernels-logger)

### API

```python
class nemo_automodel.components.models.glm5_next.optimized_kernels.HcCores()
```

**Bases:** `NamedTuple`

The three mHC functions selected together for one model instance.

**`collapse`** `CollapseCore`

---

**`expand`** `ExpandCore`

---

**`project`** `ProjectionCore`

---

```python
nemo_automodel.components.models.glm5_next.optimized_kernels._compile_options() -> dict[str, bool] | None
```

Precision-preserving options for the installed Inductor, or `None` if it cannot honor them.

```python
nemo_automodel.components.models.glm5_next.optimized_kernels._compile_options_for(
    known_options: collections.abc.Collection[str]
) -> dict[str, bool] | None
```

Inductor options that preserve the eager BF16 truncation points between fused operations.

**Parameters:**

**`known_options`** `Collection[str]`

Option names of the installed Inductor config.

---

**Returns:** `dict[str, bool] | None`

Option mapping for `torch.compile(options=...)`, or `None` when

```python
nemo_automodel.components.models.glm5_next.optimized_kernels._division_rounding_option(
    known_options: collections.abc.Collection[str]
) -> str | None
```

Name Inductor's eager division-rounding control as the given PyTorch spells it.

Newer PyTorch spells it `eager_numerics.division_rounding`; older releases
such as 2.10 only accept the legacy `emulate_divison_rounding` key, and
releases such as 2.8 have neither. `torch.compile` rejects unknown keys.

**Parameters:**

**`known_options`** `Collection[str]`

Option names of the installed Inductor config.

---

**Returns:** `str | None`

The division-rounding option name present in `known_options`, or

```python
nemo_automodel.components.models.glm5_next.optimized_kernels._expand_core_four_streams(
    update: torch.Tensor,
    residual: torch.Tensor,
    post: torch.Tensor,
    comb: torch.Tensor
) -> torch.Tensor
```

Fuse four-stream BF16 mixing with the expansion's existing cast boundaries.

**Parameters:**

**`update`** `torch.Tensor`

BF16 tensor of shape \[batch, sequence, hidden].

---

**`residual`** `torch.Tensor`

BF16 tensor of shape \[batch, sequence, streams, hidden].

---

**`post`** `torch.Tensor`

FP32 tensor of shape \[batch, sequence, streams].

---

**`comb`** `torch.Tensor`

FP32 tensor of shape \[batch, sequence, streams, streams],
indexed \[source_stream, destination_stream].

---

**Returns:** `torch.Tensor`

Updated streams of shape \[batch, sequence, streams, hidden], in the

```python
nemo_automodel.components.models.glm5_next.optimized_kernels.collapse_core(
    hidden_streams: torch.Tensor,
    pre: torch.Tensor
) -> torch.Tensor
```

Accumulate FP32 pre-weighted streams, then round the output once.

**Parameters:**

**`hidden_streams`** `torch.Tensor`

Tensor of shape \[batch, sequence, streams, hidden].

---

**`pre`** `torch.Tensor`

FP32 tensor of shape \[batch, sequence, streams].

---

**Returns:** `torch.Tensor`

Tensor of shape \[batch, sequence, hidden] in the residual-stream dtype.

```python
nemo_automodel.components.models.glm5_next.optimized_kernels.expand_core(
    update: torch.Tensor,
    residual: torch.Tensor,
    post: torch.Tensor,
    comb: torch.Tensor
) -> torch.Tensor
```

Keep coefficient, product, BMM output and sum rounding in GLM's order.

**Parameters:**

**`update`** `torch.Tensor`

Tensor of shape \[batch, sequence, hidden] in the residual
dtype, the sublayer output.

---

**`residual`** `torch.Tensor`

Tensor of shape \[batch, sequence, streams, hidden].

---

**`post`** `torch.Tensor`

FP32 tensor of shape \[batch, sequence, streams].

---

**`comb`** `torch.Tensor`

FP32 tensor of shape \[batch, sequence, streams, streams],
indexed \[source_stream, destination_stream].

---

**Returns:** `torch.Tensor`

Tensor of shape \[batch, sequence, streams, hidden] in the residual

```python
nemo_automodel.components.models.glm5_next.optimized_kernels.gated_norm_core(
    hidden_states: torch.Tensor,
    gate: torch.Tensor,
    weight: torch.Tensor,
    eps: float
) -> torch.Tensor
```

Normalize and sigmoid-gate KDA output, preserving eager cast boundaries.

**Parameters:**

**`hidden_states`** `torch.Tensor`

Tensor of shape \[batch, sequence, heads, head_dim].

---

**`gate`** `torch.Tensor`

Tensor of shape \[batch, sequence, heads, head_dim].

---

**`weight`** `torch.Tensor`

Tensor of shape \[head_dim] in `hidden_states`'s dtype.

---

**`eps`** `float`

Additive RMS normalization epsilon.

---

**Returns:** `torch.Tensor`

Tensor of shape \[batch, sequence, heads, head_dim] in

```python
nemo_automodel.components.models.glm5_next.optimized_kernels.hc_cores(
    compiled: bool
) -> nemo_automodel.components.models.glm5_next.optimized_kernels.HcCores
```

Select per-instance mHC functions without changing eager model instances.

Static shapes match the model-owned DeepSeek V4.1 compilation pattern.
Past Dynamo's recompile limit, its normal eager fallback remains available.
Base/scale parameters and their FSDP holder are outside these functions.
PyTorch without the precision controls also keeps the eager functions.

```python
nemo_automodel.components.models.glm5_next.optimized_kernels.norm_core(
    hidden_states: torch.Tensor,
    weight: torch.Tensor,
    eps: float
) -> torch.Tensor
```

Round normalized values before multiplying the norm weight, as GLM does.

**Parameters:**

**`hidden_states`** `torch.Tensor`

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

---

**`weight`** `torch.Tensor`

Tensor of shape \[hidden] in the input dtype.

---

**`eps`** `float`

Additive RMS normalization epsilon.

---

**Returns:** `torch.Tensor`

Tensor with the input shape and dtype. The FP32 normalized values are

```python
nemo_automodel.components.models.glm5_next.optimized_kernels.project_core(
    hidden_streams: torch.Tensor,
    fn: torch.Tensor,
    eps: float
) -> torch.Tensor
```

Normalize the flattened streams in FP32 before the coefficient GEMM.

**Parameters:**

**`hidden_streams`** `torch.Tensor`

Tensor of shape \[batch, sequence, streams, hidden],
where streams is `config.hc_mult`.

---

**`fn`** `torch.Tensor`

Tensor of shape \[streams \* (2 + streams), streams \* hidden], the
projection weight.

---

**`eps`** `float`

Additive RMS normalization epsilon.

---

**Returns:** `torch.Tensor`

FP32 tensor of shape \[batch, sequence, streams \* (2 + streams)],

```python
nemo_automodel.components.models.glm5_next.optimized_kernels.rms_norm_core(
    compiled: bool
) -> nemo_automodel.components.models.glm5_next.optimized_kernels.NormCore
```

Select the decoder/final RMSNorm core, retaining GLM's cast order.

```python
nemo_automodel.components.models.glm5_next.optimized_kernels.rms_norm_gated_core(
    compiled: bool
) -> nemo_automodel.components.models.glm5_next.optimized_kernels.GatedNormCore
```

Select the KDA gated norm core with the same cast policy as decoder norms.

```python
nemo_automodel.components.models.glm5_next.optimized_kernels.CollapseCore = Callable[[torch.Tensor, torch.Tensor], torch.Tensor]
```

```python
nemo_automodel.components.models.glm5_next.optimized_kernels.ExpandCore = Callable[[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor], torch.Tensor]
```

```python
nemo_automodel.components.models.glm5_next.optimized_kernels.GatedNormCore = Callable[[torch.Tensor, torch.Tensor, torch.Tensor, float], torch.Tensor]
```

```python
nemo_automodel.components.models.glm5_next.optimized_kernels.NormCore = Callable[[torch.Tensor, torch.Tensor, float], torch.Tensor]
```

```python
nemo_automodel.components.models.glm5_next.optimized_kernels.ProjectionCore = Callable[[torch.Tensor, torch.Tensor, float], torch.Tensor]
```

```python
nemo_automodel.components.models.glm5_next.optimized_kernels.logger = logging.getLogger(__name__)
```