nemo_automodel.components.models.ergo.kernels
nemo_automodel.components.models.ergo.kernels
Kernels for Ergo’s elementwise chains: ReLU squared, RMSNorm and the residual blend.
An Ergo block spends more time in these chains than in its matrix products when they run as separate PyTorch operators: every cast, multiply and reduction is one pass over a [tokens, hidden] tensor (hidden is 8192, and the routed experts’ outputs hold eight rows per token), and autograd keeps FP32 copies of the intermediates for the backward.
A chain that a library kernel computes uses it: Transformer Engine’s squared ReLU (common/squared_relu.py),
and QuACK’s RMSNorm for the per-head Q/K normalization. Three chains have no library kernel and get Triton kernels here: the
per-expert output RMSNorm, where every row takes the scale of its expert and the row groups stay on the
device, the shared expert’s RMSNorm plus the routed output, and the branch RMSNorm plus the residual blend.
Each Triton kernel reads its inputs once or twice and writes one output; each backward is the analytic
gradient of the whole chain and saves the chain’s inputs and one FP32 inverse RMS per row.
The arithmetic is that of the eager modules in layers.py, including every rounding to the compute
dtype between two steps (noted per function). Sums over a row or a column are accumulated in a different
order than PyTorch’s reductions, so results agree with the eager chain to FP32 rounding of those sums,
not bit for bit.
Module Contents
Classes
Functions
Data
API
Bases: Function
beta * rms_norm(branch) + alpha * residual in FP32.
Return the gradients of the branch, the scale, the two coefficients and the residual.
Parameters:
Autograd context holding the forward’s inputs and inverse RMS.
FP32 upstream gradient with the shape of the output.
Returns: torch.Tensor | None
Gradients of branch (contiguous, its shape and dtype), weight, beta and alpha
Run the forward kernel and save what the required gradients read.
Parameters:
Autograd context.
Contiguous CUDA tensor of shape […, width] in the compute dtype.
FP32 normalization scale of shape [width].
FP32 branch coefficient of shape [width].
FP32 residual coefficient of shape [width].
Contiguous FP32 tensor with the shape of branch.
Value added to the mean square.
Returns: torch.Tensor
Contiguous FP32 tensor with the shape of branch.
Bases: Function
Weighted RMSNorm with the optional grouped scale and addend of rms_norm.
Return the gradients of the input, the scale and the addend.
Parameters:
Autograd context holding the forward’s inputs and inverse RMS.
Upstream gradient with the shape of the output.
Returns: torch.Tensor | None
Gradients of x (contiguous, its shape and dtype), weight (its shape, FP32) and addend
Run the forward kernel and save the chain’s inputs and the inverse RMS.
Parameters:
Autograd context.
CUDA tensor of shape […, width]; see rms_norm.
FP32 scale of shape [width], or [groups, width] with group.
Contiguous tensor with the shape and dtype of the output, or None.
Int32 scale row of every input row, shape [rows], or None for one scale.
Value added to the mean square.
Output dtype.
Returns: torch.Tensor
Contiguous tensor with the shape of x in out_dtype.
Return rows and columns of one tile: the columns cover a row of width in power-of-two steps.
Write the branch and residual gradients of _norm_residual_fwd_kernel.
Parameters:
Saved contiguous branch output of shape [rows, width].
FP32 normalization scale of shape [width].
FP32 branch coefficient of shape [width].
FP32 residual coefficient of shape [width].
Contiguous FP32 upstream gradient of shape [rows, width].
FP32 inverse RMS of every row, shape [rows], from the forward.
Contiguous branch gradient of shape [rows, width] in the branch dtype; written
only when WANT_GRAD_X.
Contiguous FP32 residual gradient of shape [rows, width], written here.
Number of rows.
Number of columns.
Compute the branch gradient.
Rows per program.
Columns per loop iteration.
Sum the per-column gradients of the scale and the two coefficients over the rows of one chunk.
Parameters:
Saved contiguous branch output of shape [rows, width].
FP32 normalization scale of shape [width].
Saved contiguous FP32 residual of shape [rows, width]; read only when
HAS_RESIDUAL.
Contiguous FP32 upstream gradient of shape [rows, width].
FP32 inverse RMS of every row, shape [rows], from the forward.
FP32 partial sums of shape [3, chunks, width], written here: grad * x * rstd
(the scale’s gradient before the branch coefficient), grad * normalized (the branch
coefficient’s gradient) and grad * residual (the residual coefficient’s gradient, only
when HAS_RESIDUAL).
Number of rows.
Number of columns.
Number of chunks, the first extent of the program grid.
Reduce the residual coefficient’s gradient.
Rows per program.
Rows per loop iteration.
Columns per program.
Write beta * rms_norm(x) + alpha * residual in FP32 for BLOCK_R rows.
Parameters:
Contiguous branch output of shape [rows, width], in the compute dtype.
FP32 normalization scale of shape [width].
FP32 branch coefficient of shape [width].
FP32 residual coefficient of shape [width].
Contiguous FP32 residual of shape [rows, width].
Contiguous FP32 output of shape [rows, width].
FP32 inverse RMS of every row, shape [rows], written here.
Number of rows.
Number of columns.
Value added to the mean square.
Rows per program.
Columns per loop iteration.
Write the input gradient of _rms_norm_fwd_kernel.
The rounding of the forward’s output passes the gradient unchanged, as a cast does in autograd.
Parameters:
Saved input, in the layout of the forward.
FP32 scale of shape [width] or [groups, width].
Int32 scale row of every input row, shape [rows]; read only when HAS_GROUP.
Contiguous upstream gradient of shape [rows, width].
FP32 inverse RMS of every row, shape [rows], from the forward.
Contiguous input gradient of shape [rows, width], written here.
Number of rows.
Number of columns.
Rows per outer index of the input layout.
Input stride between outer indices, in elements.
Input stride between rows of one outer index, in elements.
Row stride of a grouped scale, in elements.
The scale has one row per group.
Rows per program.
Columns per loop iteration.
Normalize BLOCK_R rows, scale them, and optionally add a second tensor.
Row r of the input starts at element (r // inner) * stride_outer + (r % inner) * stride_inner.
Parameters:
Input rows of width elements with unit column stride.
FP32 scale of shape [width], or [groups, width] with row stride stride_weight.
Int32 scale row of every input row, shape [rows]; read only when HAS_GROUP.
Contiguous tensor of shape [rows, width] in the input dtype; read only when
HAS_ADDEND.
Contiguous output of shape [rows, width].
FP32 inverse RMS of every row, shape [rows], written here.
Number of rows.
Number of columns.
Rows per outer index of the input layout.
Input stride between outer indices, in elements.
Input stride between rows of one outer index, in elements.
Row stride of a grouped scale, in elements.
Value added to the mean square.
The scale has one row per group.
Round the scaled values to the input dtype and add the second tensor in FP32.
Rows per program.
Columns per loop iteration.
Sum grad * normalized over the rows of one chunk, per group, for one block of columns.
Rows of one group are contiguous and groups ascend with the row index, so a chunk holds the groups from that of its first row to that of its last; each gets its own pass over the chunk.
Parameters:
Saved input, in the layout of the forward.
Int32 scale row of every input row, shape [rows]; read only when HAS_GROUP.
Contiguous upstream gradient of shape [rows, width].
FP32 inverse RMS of every row, shape [rows], from the forward.
Zero-initialized FP32 partial sums of shape [chunks, groups, width]; chunk c
covers rows c * CHUNK up to (c + 1) * CHUNK, and only its groups are written.
Number of rows.
Number of columns.
Number of scale rows.
Rows per outer index of the input layout.
Input stride between outer indices, in elements.
Input stride between rows of one outer index, in elements.
The scale has one row per group.
Rows per program.
Rows per loop iteration.
Columns per program.
Describe x as rows of its last dimension without copying a strided projection slice.
Parameters:
Tensor of shape […, width] with at least two dimensions. A slice of a wider projection, viewed as […, heads, width], is read in place.
Returns: torch.Tensor
The tensor to read (x, or a contiguous copy when its strides do not form rows), the number of
Apply a weighted RMSNorm with QuACK’s kernels.
Computes x * rsqrt(mean(x ** 2) + eps) * weight in FP32 and rounds once to out_dtype.
Parameters:
CUDA tensor of shape […, width] in FP32 or the compute dtype; a strided view is read in place.
FP32 scale of shape [width].
Value added to the mean square.
Dtype of the result.
Returns: torch.Tensor
Tensor with the shape of x in out_dtype.
Apply Ergo’s weighted RMSNorm, and optionally an addend, with one kernel per pass.
The steps and their roundings follow the eager chains:
x * rsqrt(mean(x ** 2) + eps) * weightin FP32, rounded once toout_dtype. This isF.rms_normof a BF16 input with an FP32 weight, as inErgoComputeRMSNormandErgoExpertOutputNorm.addend: the scaled values are rounded to the compute dtype and added toaddendin FP32 (the shared expert added to the routed experts’ sum).
Parameters:
CUDA tensor of shape […, width] in FP32 or the compute dtype. A […, heads, width] view of a slice of a wider projection is read in place.
FP32 scale of shape [width], or [groups, width] with group_sizes.
Value added to the mean square.
Dtype of the result.
Integer tensor of shape [groups] with the rows of x that belong to each row of a
grouped weight, in order; their sum is the number of rows. It is read on the device only.
Tensor with the shape of x in out_dtype.
Returns: torch.Tensor
Contiguous tensor with the shape of x in out_dtype.
Normalize a branch output in FP32 and blend it into the residual stream with one kernel per pass.
Computes beta * (branch * rsqrt(mean(branch ** 2) + eps) * weight) + alpha * residual in FP32,
the chain of ErgoRMSNorm and ErgoResidual.
Parameters:
CUDA tensor of shape […, width] in the compute dtype.
FP32 normalization scale of shape [width].
Value added to the mean square.
FP32 branch coefficient of shape [width].
FP32 residual coefficient of shape [width].
FP32 tensor with the shape of branch.
Returns: torch.Tensor
Contiguous FP32 tensor with the shape of branch.