nemo_automodel.components.moe.swiglu_triton
nemo_automodel.components.moe.swiglu_triton
Triton kernels for the clamped SwiGLU expert activation (DeepSeek-V4 style, swiglu_limit > 0).
The routed experts apply silu(clamp(gate, max=L)) * clamp(up, -L, L) * probs to the grouped-GEMM output
x = [gate | up] (bf16, [rows, 2 * inter]) with fp32 routing probabilities probs ([rows, 1]). The
compiled PyTorch version (experts._swiglu_clamped_deepep_compiled) is one inductor kernel forward, but its
autograd-derived backward concatenates d_gate and d_up and ran at about a quarter of HBM bandwidth on
GB200 (0.7 ms for a 1.1 GB pass at the 16-node shape). These kernels do one pass each:
- forward: a 2-D tile per program, fp32 math, result rounded to
x.dtype; - backward: one program per block of rows looping over the columns, writing
d_gate/d_upstraight into the two halves ofgrad_xand accumulating the per-row probability gradient in registers.
Clamp gradients follow the installed scalar torch.clamp subgradient: inclusive before PyTorch 2.15,
and zero at exact bounds from 2.15 onward. The dispatcher in
experts.swiglu_clamped_deepep uses these kernels on contiguous CUDA inputs when Triton is importable and
falls back to the compiled version otherwise.
Module Contents
Classes
Functions
Data
API
Bases: Function
silu(clamp(gate)) * clamp(up) * probs with the Triton forward and backward kernels.
Return fused SwiGLU gradients with the runtime’s scalar-clamp subgradient.
Parameters:
Contiguous input tensor of shape [rows, 2 * inter], where rows counts routed tokens and inter is the expert intermediate width; gate is followed by up.
FP32 probability tensor of shape [rows] or [rows, 1].
Upstream gradient tensor of shape [rows, inter].
Scalar clamp limit.
Returns: torch.Tensor
Tuple of independent input-gradient storage of shape [rows, 2 * inter] and
Write gradients using the installed PyTorch scalar-clamp boundary convention.
Parameters:
Input storage of shape [rows, 2 * inter], gate followed by up.
FP32 probability storage of shape [rows].
Upstream gradient storage of shape [rows, inter].
Output gradient storage of shape [rows, 2 * inter], written in place.
FP32 output gradient storage of shape [rows], written in place.
Number of token rows.
Intermediate width per gate/up half.
Input row stride in elements.
Upstream-gradient row stride in elements.
Output-gradient row stride in elements.
Scalar clamp limit.
Rows per program.
Columns per iteration.
Whether gradients include values exactly on a clamp bound.
Apply the clamped SwiGLU with the Triton kernels; callers check uses_triton_kernel first.
Whether the Triton kernels can take these inputs (contiguous 2-D CUDA x, one probability per row).