Skip to content

vllm.models.nemotron_shortcut.nvidia.moe.kernels

Triton expert GEMMs for Nemotron Shortcut's clamped ReLU² experts.

routed_experts reproduces the training numerics: the clamp, the ReLU², and the routing weight are applied after the up projection and rounded to the activation dtype before the down projection, and the top-k sum is kept in FP32. Both projections optionally read MXFP8 weights and quantize their activations in-kernel.

Functions:

mxfp8_quantize_block(x)

Quantize rows of 32 values to E4M3 with E8M0 scales.

Source code in vllm/models/nemotron_shortcut/nvidia/moe/kernels.py
@triton.jit
def mxfp8_quantize_block(x):
    """Quantize rows of 32 values to E4M3 with E8M0 scales."""
    x = x.to(tl.float32)
    # The scale puts the block amax at the top of the E4M3 range rather than
    # at 1.0, or small elements of the block end up in the subnormals.
    amax = tl.maximum(tl.max(tl.abs(x), axis=1), 1.1754943508222875e-38)
    sb = tl.ceil(tl.log2(amax / 448.0)) + 127.0
    sb = tl.minimum(tl.maximum(sb, 0.0), 254.0)
    # Scale by the reciprocal rather than dividing by exp2(sb - 127): for an
    # all-zero block sb == 0 makes that divisor subnormal in FP32.
    rescale = tl.exp2(127.0 - sb)
    xq = (x * rescale[:, None]).to(tl.float8e4nv)
    return xq, sb.to(tl.uint8)

routed_experts(x, up, down, ids, weights, begin, clamp, up_scale=None, down_scale=None)

Run the local experts on every route and sum the top-k in FP32.

up and down hold the experts [begin, begin + len(up)); routes to other experts contribute zero, so EP ranks return partial sums.

Source code in vllm/models/nemotron_shortcut/nvidia/moe/kernels.py
def routed_experts(
    x: torch.Tensor,
    up: torch.Tensor,
    down: torch.Tensor,
    ids: torch.Tensor,
    weights: torch.Tensor,
    begin: int,
    clamp: float | None,
    up_scale: torch.Tensor | None = None,
    down_scale: torch.Tensor | None = None,
) -> torch.Tensor:
    """Run the local experts on every route and sum the top-k in FP32.

    `up` and `down` hold the experts `[begin, begin + len(up))`; routes to
    other experts contribute zero, so EP ranks return partial sums.
    """
    ids, order = ids.sort(dim=-1)
    weights = weights.gather(1, order)
    tokens, top = ids.shape
    intermediate, width = up.shape[1:]
    output = torch.empty_like(x, dtype=torch.float32)
    if tokens == 0:
        return output
    hidden = x.new_empty((tokens * top, intermediate))
    parts = x.new_empty((tokens * top, width))
    for first, inp, matrix, out, scale in (
        (True, x, up, hidden, up_scale),
        (False, hidden, down, parts, down_scale),
    ):
        inner, outer = (width, intermediate) if first else (intermediate, width)
        # Routed assignments can exceed CUDA's 65535-block grid Y limit, so
        # they go on the X axis.
        _expert_mm[(tokens * top, triton.cdiv(outer, 64))](
            inp,
            matrix,
            scale,
            ids,
            weights,
            out,
            top,
            inner,
            outer,
            first,
            begin,
            begin + up.shape[0],
            clamp,
            64,
            128 if scale is not None else 32,
            MXFP8_BLOCK_SIZE,
            scale is not None,
            enable_fp_fusion=False,
        )
    _expert_sum[(tokens, triton.cdiv(width, 256))](
        parts,
        ids,
        output,
        top,
        width,
        begin,
        begin + up.shape[0],
        256,
        enable_fp_fusion=False,
    )
    return output