Skip to navigation

nemo_automodel.components.models.common.squared_relu

View as MarkdownOpen in Claude

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

Module Contents

Classes

NameDescription
_SquaredReLUReLU squared through Transformer Engine’s srelu and dsrelu kernels.

Functions

NameDescription
_transformer_engineReturn Transformer Engine’s kernel module, or None when Transformer Engine does not load.
squared_reluReturn relu(x) ** 2 in the dtype of x.

Data

_KERNEL_DTYPES

API

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

Bases: Function

ReLU squared through Transformer Engine’s srelu and dsrelu kernels.

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.

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.

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.

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.

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