Skip to content

vllm.models.inkling2.nvidia.ops.activation

Softcapped SwiGLU of the Inkling2 experts, fused with the down-projection input quantization.

Functions:

_div_rn(x, DIVISOR)

Bitwise tl.div_rn(x, DIVISOR) without the division routine: the product with the rounded reciprocal and one FMA correction step (whose residual is clamped for infinite x) are exact for these divisors on every float32 input.

Source code in vllm/models/inkling2/nvidia/ops/activation.py
@triton.jit
def _div_rn(x, DIVISOR: tl.constexpr):
    """Bitwise `tl.div_rn(x, DIVISOR)` without the division routine: the
    product with the rounded reciprocal and one FMA correction step (whose
    residual is clamped for infinite `x`) are exact for these divisors on
    every float32 input."""
    tl.static_assert(DIVISOR == 4.0 or DIVISOR == 25.0)
    q = x * (1.0 / DIVISOR)
    if DIVISOR != 4.0:
        residual = tl.maximum(tl.fma(q, DIVISOR, -x), -FP32_MAX)
        q = tl.fma(-residual, 1.0 / DIVISOR, q)
    return q

_row_inputs(Rows, Gammas, CtaExpert, WAmax, r, num_rows, TILE, NVFP4)

Row, routing weight and (NVFP4) weight amax of routed row r.

Source code in vllm/models/inkling2/nvidia/ops/activation.py
@triton.jit
def _row_inputs(
    Rows, Gammas, CtaExpert, WAmax, r, num_rows, TILE: tl.constexpr, NVFP4: tl.constexpr
):
    """Row, routing weight and (NVFP4) weight amax of routed row `r`."""
    valid = r < num_rows
    row = tl.load(Rows + r, mask=valid, other=0).to(tl.int64)
    gamma = tl.load(Gammas + row, mask=valid)
    w_amax = 0.0
    if NVFP4:
        expert = tl.load(CtaExpert + row // TILE, mask=valid, other=0)
        w_amax = tl.load(WAmax + expert, mask=valid)
    return row, gamma, w_amax

_situ(X, row, start, gamma, N, BLOCK, GATE_CAP, UP_CAP)

fp32 values of the bf16 SiTU of columns [start, start + BLOCK).

Source code in vllm/models/inkling2/nvidia/ops/activation.py
@triton.jit
def _situ(X, row, start, gamma, N: tl.constexpr, BLOCK: tl.constexpr, GATE_CAP, UP_CAP):
    """fp32 values of the bf16 SiTU of columns `[start, start + BLOCK)`."""
    # Interleaved (gate, up) bf16 pairs as one word each, loaded so that each
    # thread holds whole 16-column blocks: column 16 * b + 4 * i + j at
    # [i, b, j].
    i = tl.arange(0, 4)[:, None, None]
    b = tl.arange(0, BLOCK // 16)[None, :, None]
    j = tl.arange(0, 4)[None, None, :]
    cols = start + 16 * b + 4 * i + j
    pairs = tl.load(X.to(tl.pointer_type(tl.uint32)) + row * N + cols)
    pairs = tl.reshape(tl.permute(pairs, (1, 0, 2)), (BLOCK,))
    gate = (pairs << 16).to(tl.float32, bitcast=True)
    up = (pairs & 0xFFFF0000).to(tl.float32, bitcast=True)
    sigmoid = _sigmoid_approx(gate)
    gate = _softcap(gate, GATE_CAP)
    up = _softcap(up, UP_CAP)
    return (((gate * sigmoid) * up) * gamma).to(tl.bfloat16).to(tl.float32)

swiglu_quantize(x, gammas, gate_cap, up_cap, rows, tile, weight_amax=None, weight_denominator=None)

Quantized (NVFP4 with weight_amax, else MXFP8) bf16 softcap(gate) * sigmoid(gate) * softcap(up) * gamma, computed in fp32.

x is [M, 2N] with interleaved (gate, up) columns in the expert layout of tile-row GEMM tiles. rows holds the routed rows (the others are left uninitialized), their number and the expert of each tile. Returns the values, block scales and, for NVFP4, each row's down-projection output scale.

Source code in vllm/models/inkling2/nvidia/ops/activation.py
def swiglu_quantize(
    x: torch.Tensor,
    gammas: torch.Tensor,
    gate_cap: float,
    up_cap: float,
    rows: tuple[torch.Tensor, torch.Tensor, torch.Tensor],
    tile: int,
    weight_amax: torch.Tensor | None = None,
    weight_denominator: torch.Tensor | None = None,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor | None]:
    """Quantized (NVFP4 with `weight_amax`, else MXFP8) bf16
    `softcap(gate) * sigmoid(gate) * softcap(up) * gamma`, computed in fp32.

    `x` is [M, 2N] with interleaved (gate, up) columns in the expert layout
    of `tile`-row GEMM tiles. `rows` holds the routed rows (the others are
    left uninitialized), their number and the expert of each tile. Returns the
    values, block scales and, for NVFP4, each row's down-projection output
    scale.
    """
    num_rows, n = x.shape[0], x.shape[1] // 2
    routed, num_routed, cta_expert = rows
    nvfp4 = weight_amax is not None
    max_rows = routed.numel()
    num_warps, maxnreg = (4, None) if max_rows <= 16384 else (2, 72)
    if not nvfp4 and max_rows <= 512:
        # Few rows: a program per 256 (or 128) columns.
        head, tail, num_warps = 256 if n % 256 == 0 else 128, 0, 2
        assert n % head == 0
    else:
        # Whole rows, as a power-of-two head and tail.
        head = 1 << (n.bit_length() - 1)
        tail = n - head
        assert tail & (tail - 1) == 0 and (tail == 0 or tail >= 32)
        if not nvfp4:
            num_warps, maxnreg = 2, None
    # The down projection's GEMM operands.
    if nvfp4:
        values = operand(num_rows, n // 2, dtype=torch.uint8, device=x.device)
        scales = operand(num_rows, n // 16, dtype=torch.float8_e4m3fn, device=x.device)
        row_scales = operand(num_rows, dtype=torch.float32, device=x.device)
    else:
        values = operand(num_rows, n, dtype=torch.float8_e4m3fn, device=x.device)
        scales = operand(num_rows, n // 32, dtype=torch.uint8, device=x.device)
        row_scales = None
    _situ_quantize_kernel[(min(max_rows, 16384), n // (head + tail))](
        x,
        gammas,
        values,
        scales,
        row_scales,
        routed,
        num_routed,
        cta_expert,
        weight_amax,
        weight_denominator,
        N=n,
        GATE_CAP=gate_cap,
        UP_CAP=up_cap,
        HEAD=head,
        TAIL=tail,
        TILE=tile,
        NVFP4=nvfp4,
        LAUNCH_DEPENDENTS=max_rows <= LAUNCH_DEPENDENTS_MAX_ROWS,
        num_warps=num_warps,
        maxnreg=maxnreg,
        enable_fp_fusion=False,
        launch_pdl=True,
    )
    return values, scales, row_scales