Skip to navigation

nemo_automodel.components.moe.swiglu_triton

View as Markdown

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_up straight into the two halves of grad_x and 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

NameDescription
SwigluClampedTritonsilu(clamp(gate)) * clamp(up) * probs with the Triton forward and backward kernels.

Functions

NameDescription
_backwardReturn fused SwiGLU gradients with the runtime’s scalar-clamp subgradient.
_forward-
_swiglu_clamped_bwd_kernelWrite gradients using the installed PyTorch scalar-clamp boundary convention.
_swiglu_clamped_fwd_kernel-
swiglu_clamped_tritonApply the clamped SwiGLU with the Triton kernels; callers check uses_triton_kernel first.
uses_triton_kernelWhether the Triton kernels can take these inputs (contiguous 2-D CUDA x, one probability per row).

Data

HAVE_TRITON

_BWD_BLOCK_M

_BWD_BLOCK_N

_CLAMP_GRAD_INCLUSIVE

_FWD_BLOCK_M

_FWD_BLOCK_N

_SUPPORTED_DTYPES

API

class nemo_automodel.components.moe.swiglu_triton.SwigluClampedTriton()

Bases: Function

silu(clamp(gate)) * clamp(up) * probs with the Triton forward and backward kernels.

nemo_automodel.components.moe.swiglu_triton.SwigluClampedTriton.backward(
ctx,
grad_y: torch.Tensor
)
staticmethod
nemo_automodel.components.moe.swiglu_triton.SwigluClampedTriton.forward(
ctx,
x: torch.Tensor,
probs: torch.Tensor,
limit: float
) -> torch.Tensor
staticmethod
nemo_automodel.components.moe.swiglu_triton._backward(
x: torch.Tensor,
probs: torch.Tensor,
grad_y: torch.Tensor,
limit: float
) -> tuple[torch.Tensor, torch.Tensor]

Return fused SwiGLU gradients with the runtime’s scalar-clamp subgradient.

Parameters:

x
torch.Tensor

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.

probs
torch.Tensor

FP32 probability tensor of shape [rows] or [rows, 1].

grad_y
torch.Tensor

Upstream gradient tensor of shape [rows, inter].

limit
float

Scalar clamp limit.

Returns: torch.Tensor

Tuple of independent input-gradient storage of shape [rows, 2 * inter] and

nemo_automodel.components.moe.swiglu_triton._forward(
x: torch.Tensor,
probs: torch.Tensor,
limit: float
) -> torch.Tensor
nemo_automodel.components.moe.swiglu_triton._swiglu_clamped_bwd_kernel(
x_ptr,
probs_ptr,
grad_y_ptr,
grad_x_ptr,
grad_probs_ptr,
rows,
inter,
stride_x,
stride_gy,
stride_gx,
limit,
BLOCK_M: tl.constexpr,
BLOCK_N: tl.constexpr,
CLAMP_GRAD_INCLUSIVE: tl.constexpr
)

Write gradients using the installed PyTorch scalar-clamp boundary convention.

Parameters:

x_ptr

Input storage of shape [rows, 2 * inter], gate followed by up.

probs_ptr

FP32 probability storage of shape [rows].

grad_y_ptr

Upstream gradient storage of shape [rows, inter].

grad_x_ptr

Output gradient storage of shape [rows, 2 * inter], written in place.

grad_probs_ptr

FP32 output gradient storage of shape [rows], written in place.

rows

Number of token rows.

inter

Intermediate width per gate/up half.

stride_x

Input row stride in elements.

stride_gy

Upstream-gradient row stride in elements.

stride_gx

Output-gradient row stride in elements.

limit

Scalar clamp limit.

BLOCK_M
tl.constexpr

Rows per program.

BLOCK_N
tl.constexpr

Columns per iteration.

CLAMP_GRAD_INCLUSIVE
tl.constexpr

Whether gradients include values exactly on a clamp bound.

nemo_automodel.components.moe.swiglu_triton._swiglu_clamped_fwd_kernel(
x_ptr,
probs_ptr,
y_ptr,
rows,
inter,
stride_x,
stride_y,
limit,
BLOCK_M: tl.constexpr,
BLOCK_N: tl.constexpr
)
nemo_automodel.components.moe.swiglu_triton.swiglu_clamped_triton(
x: torch.Tensor,
probs: torch.Tensor,
limit: float
) -> torch.Tensor

Apply the clamped SwiGLU with the Triton kernels; callers check uses_triton_kernel first.

nemo_automodel.components.moe.swiglu_triton.uses_triton_kernel(
x: torch.Tensor,
probs: torch.Tensor
) -> bool

Whether the Triton kernels can take these inputs (contiguous 2-D CUDA x, one probability per row).

nemo_automodel.components.moe.swiglu_triton.HAVE_TRITON: bool = bool(_HAVE_TRITON and _HAVE_TRITON_LANGUAGE)
nemo_automodel.components.moe.swiglu_triton._BWD_BLOCK_M = 4
nemo_automodel.components.moe.swiglu_triton._BWD_BLOCK_N = 512
nemo_automodel.components.moe.swiglu_triton._CLAMP_GRAD_INCLUSIVE = Version(torch.__version__).release < (2, 15)
nemo_automodel.components.moe.swiglu_triton._FWD_BLOCK_M = 8
nemo_automodel.components.moe.swiglu_triton._FWD_BLOCK_N = 256
nemo_automodel.components.moe.swiglu_triton._SUPPORTED_DTYPES = (torch.bfloat16, torch.float16, torch.float32)