> This page is for version 0.4.0 · 26.04.
> 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.common.squared_relu

ReLU squared for non-gated MLPs, through Transformer Engine's kernels where they apply.

## Module Contents

### Classes

| Name                                                                                 | Description                                                             |
| ------------------------------------------------------------------------------------ | ----------------------------------------------------------------------- |
| [`_SquaredReLU`](#nemo_automodel-components-models-common-squared_relu-_SquaredReLU) | ReLU squared through Transformer Engine's `srelu` and `dsrelu` kernels. |

### Functions

| Name                                                                                               | Description                                                                               |
| -------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- |
| [`_transformer_engine`](#nemo_automodel-components-models-common-squared_relu-_transformer_engine) | Return Transformer Engine's kernel module, or None when Transformer Engine does not load. |
| [`squared_relu`](#nemo_automodel-components-models-common-squared_relu-squared_relu)               | Return `relu(x) ** 2` in the dtype of `x`.                                                |

### Data

[`_KERNEL_DTYPES`](#nemo_automodel-components-models-common-squared_relu-_KERNEL_DTYPES)

### API

```python
class nemo_automodel.components.models.common.squared_relu._SquaredReLU()
```

**Bases:** `Function`

ReLU squared through Transformer Engine's `srelu` and `dsrelu` kernels.

```python
nemo_automodel.components.models.common.squared_relu._SquaredReLU.backward(
    ctx: torch.autograd.function.FunctionCtx,
    grad_output: torch.Tensor
) -> torch.Tensor
```

staticmethod

Return the gradient of `x`: `2 * relu(x) * grad_output`.

**Parameters:**

**`ctx`** `torch.autograd.function.FunctionCtx`

Autograd context of the forward.

---

**`grad_output`** `torch.Tensor`

Gradient of the output, shape \[rows, width], any strides.

---

**Returns:** `torch.Tensor`

A new tensor with the shape and dtype of `x`.

```python
nemo_automodel.components.models.common.squared_relu._SquaredReLU.forward(
    ctx: torch.autograd.function.FunctionCtx,
    x: torch.Tensor
) -> torch.Tensor
```

staticmethod

Return `relu(x) ** 2` and save `x` for the backward.

**Parameters:**

**`ctx`** `torch.autograd.function.FunctionCtx`

Autograd context; saves `x`.

---

**`x`** `torch.Tensor`

Contiguous CUDA tensor of shape \[rows, width] in BF16, FP16 or FP32.

---

**Returns:** `torch.Tensor`

A new tensor with the shape and dtype of `x`.

```python
nemo_automodel.components.models.common.squared_relu._transformer_engine() -> types.ModuleType | None
```

Return Transformer Engine's kernel module, or None when Transformer Engine does not load.

```python
nemo_automodel.components.models.common.squared_relu.squared_relu(
    x: torch.Tensor
) -> torch.Tensor
```

Return `relu(x) ** 2` in the dtype of `x`.

A CUDA tensor in BF16, FP16 or FP32 runs through Transformer Engine's kernels, one per direction, which
keep `x` alone for the backward. The values and the gradient are those of the eager chain
`F.relu(x).pow(2)` bit for bit. Every other tensor, and every tensor where Transformer Engine does not
load, takes the eager chain.

**Parameters:**

**`x`** `torch.Tensor`

Tensor of shape \[..., width], any strides.

---

**Returns:** `torch.Tensor`

A new tensor with the shape and dtype of `x`.

```python
nemo_automodel.components.models.common.squared_relu._KERNEL_DTYPES = (torch.bfloat16, torch.float16, torch.float32)
```