Skip to content

vllm.models.deepseek_v4.amd.silu_and_mul_mxfp8

SiLU-and-mul (optionally clamped) fused with the MXFP8 quant of its output.

On gfx950 the MXFP8 linear after the shared expert's activation quantizes the bf16 activation in a separate launch; this writes the e4m3 values and row-major [M, D / 32] E8M0 scales directly, bit-identical to SiluAndMul / SiluAndMulWithClamp followed by mxfp8_e4m3_quantize.

Functions:

_config(num_tokens, d)

(BLOCK_M, BLOCK_N, num_warps), tuned on MI355X for d = 2304.

Source code in vllm/models/deepseek_v4/amd/silu_and_mul_mxfp8.py
def _config(num_tokens: int, d: int) -> tuple[int, int, int]:
    """(BLOCK_M, BLOCK_N, num_warps), tuned on MI355X for d = 2304."""
    if num_tokens <= 256:
        block_m, block_n, warps = 1, 256, 1
    elif num_tokens <= 4096:
        block_m, block_n, warps = 16, 128, 4
    else:
        block_m, block_n, warps = 32, 256, 8
    while d % block_n:
        block_n //= 2
    return block_m, block_n, warps

silu_and_mul_mxfp8(x, swiglu_limit)

[M, 2 * D] gate/up -> MXFP8 [M, D] e4m3 values and [M, D / 32] uint8 E8M0 scales of silu(clamp(gate)) * clamp(up).

Source code in vllm/models/deepseek_v4/amd/silu_and_mul_mxfp8.py
def silu_and_mul_mxfp8(
    x: torch.Tensor, swiglu_limit: float | None
) -> tuple[torch.Tensor, torch.Tensor]:
    """`[M, 2 * D]` gate/up -> MXFP8 `[M, D]` e4m3 values and `[M, D / 32]`
    uint8 E8M0 scales of `silu(clamp(gate)) * clamp(up)`."""
    assert x.dim() == 2 and x.is_contiguous()
    num_tokens, d = x.shape[0], x.shape[1] // 2
    assert d % MXFP8_BLOCK_SIZE == 0
    values = torch.empty((num_tokens, d), dtype=MXFP8_VALUE_DTYPE, device=x.device)
    scales = torch.empty(
        (num_tokens, d // MXFP8_BLOCK_SIZE), dtype=MXFP8_SCALE_DTYPE, device=x.device
    )
    if num_tokens == 0:
        return values, scales
    block_m, block_n, warps = _config(num_tokens, d)
    _silu_and_mul_mxfp8_kernel[(triton.cdiv(num_tokens, block_m), d // block_n)](
        x,
        values,
        scales,
        num_tokens,
        D=d,
        LIMIT=0.0 if swiglu_limit is None else float(swiglu_limit),
        HAS_LIMIT=swiglu_limit is not None,
        BLOCK_M=block_m,
        BLOCK_N=block_n,
        FP8_MAX=float(torch.finfo(MXFP8_VALUE_DTYPE).max),
        TINY=float(torch.finfo(torch.float32).tiny),
        num_warps=warps,
    )
    return values, scales