Skip to content

vllm.models.inkling2.nvidia.ops.nvfp4

NVFP4 per-token activation quantization ("fouroversix_mae" scale rule).

Each row gets a per-token global scale amax / 1536. For every 16-element block both a 6.0- and a 4.0-based e4m3 block scale are tried and the one with the smaller absolute reconstruction error is kept.

Functions:

_block_scales(block_max, encoder_scale, decoder_scale)

The 6.0- and 4.0-based block scales of blocks with max |x| block_max and their inverses.

Source code in vllm/models/inkling2/nvidia/ops/nvfp4.py
@triton.jit
def _block_scales(block_max, encoder_scale, decoder_scale):
    """The 6.0- and 4.0-based block scales of blocks with max |x| `block_max`
    and their inverses."""
    scale6 = tl.minimum(block_max * (encoder_scale * (1.0 / 6.0)), 448.0)
    scale4 = tl.minimum(block_max * (encoder_scale * 0.25), 448.0)
    scale6 = scale6.to(tl.float8e4nv).to(tl.float32)
    scale4 = scale4.to(tl.float8e4nv).to(tl.float32)
    inverse6 = tl.minimum(tl.div_rn(1.0, scale6 * decoder_scale), FP32_MAX)
    inverse4 = tl.minimum(tl.div_rn(1.0, scale4 * decoder_scale), FP32_MAX)
    return scale6, scale4, inverse6, inverse4

_candidate(x, inverse, scale, outer_scale)

Packed values (uint64) and the reconstruction error of the blocks x (16 columns) with block scale scale: the error is summed pairwise, as in _block_error.

Source code in vllm/models/inkling2/nvidia/ops/nvfp4.py
@triton.jit
def _candidate(x, inverse, scale, outer_scale):
    """Packed values (uint64) and the reconstruction error of the blocks
    `x` (16 columns) with block scale `scale`: the error is summed
    pairwise, as in `_block_error`."""
    low, low_error = _octet(x, 0, inverse, scale, outer_scale)
    high, high_error = _octet(x, 1, inverse, scale, outer_scale)
    return low | high, low_error + high_error

_e2m1(x)

The e2m1 code (sign in bit 3) of fp32 x rounded to nearest even with saturation (cvt.rn.satfinite), and its value.

Source code in vllm/models/inkling2/nvidia/ops/nvfp4.py
@triton.jit
def _e2m1(x):
    """The e2m1 code (sign in bit 3) of fp32 `x` rounded to nearest even
    with saturation (`cvt.rn.satfinite`), and its value."""
    a = tl.abs(x)
    code = tl.where(
        a <= 0.25,
        0,
        tl.where(
            a < 0.75,
            1,
            tl.where(
                a <= 1.25,
                2,
                tl.where(
                    a < 1.75,
                    3,
                    tl.where(
                        a <= 2.5, 4, tl.where(a < 3.5, 5, tl.where(a <= 5.0, 6, 7))
                    ),
                ),
            ),
        ),
    ).to(tl.uint32)
    exponent = code >> 1
    mantissa = (code & 1).to(tl.float32)
    value = tl.where(
        exponent == 0,
        mantissa * 0.5,
        tl.where(
            exponent == 1,
            1.0 + mantissa * 0.5,
            tl.where(exponent == 2, 2.0 + mantissa, 4.0 + 2.0 * mantissa),
        ),
    )
    sign = x.to(tl.uint32, bitcast=True) >> 31
    return code | (sign << 3), tl.where(sign != 0, -value, value)

_e2m1_pair(a, b)

The e2m1x2 byte of (a, b) (a in the low nibble) and both values rounded to e2m1.

Source code in vllm/models/inkling2/nvidia/ops/nvfp4.py
@triton.jit
def _e2m1_pair(a, b):
    """The e2m1x2 byte of `(a, b)` (`a` in the low nibble) and both
    values rounded to e2m1."""
    if not _HW_E2M1:
        low_code, low = _e2m1(a)
        high_code, high = _e2m1(b)
        return low_code | (high_code << 4), low, high
    return tl.inline_asm_elementwise(
        "{ .reg .b8 q; .reg .b32 h; .reg .b16 l, u; "
        "cvt.rn.satfinite.e2m1x2.f32 q, $4, $3; cvt.u32.u8 $0, q; "
        "cvt.rn.f16x2.e2m1x2 h, q; mov.b32 {l, u}, h; "
        "cvt.f32.f16 $1, l; cvt.f32.f16 $2, u; }",
        "=r,=f,=f,f,f",
        [a, b],
        dtype=(tl.uint32, tl.float32, tl.float32),
        is_pure=True,
        pack=1,
    )

_pair(x, k, inverse, scale, outer_scale)

Columns 2k and 2k + 1 of _candidate: their e2m1 byte at byte k and their reconstruction error.

Source code in vllm/models/inkling2/nvidia/ops/nvfp4.py
@triton.jit
def _pair(x, k: tl.constexpr, inverse, scale, outer_scale):
    """Columns `2k` and `2k + 1` of `_candidate`: their e2m1 byte at
    byte `k` and their reconstruction error."""
    byte, low, high = _e2m1_pair(x[2 * k] * inverse, x[2 * k + 1] * inverse)
    error = tl.abs((low * scale) * outer_scale - x[2 * k]) + tl.abs(
        (high * scale) * outer_scale - x[2 * k + 1]
    )
    return byte.to(tl.uint64) << (8 * k), error

_quantize_columns(x, encoder_scale, decoder_scale, outer_scale)

_quantize_block of blocks given as a tuple x of their 16 fp32 columns, for each thread to hold whole blocks: packed values (uint64) and e4m3 scales.

Source code in vllm/models/inkling2/nvidia/ops/nvfp4.py
@triton.jit
def _quantize_columns(x, encoder_scale, decoder_scale, outer_scale):
    """`_quantize_block` of blocks given as a tuple `x` of their 16 fp32
    columns, for each thread to hold whole blocks: packed values (uint64) and
    e4m3 scales."""
    block_max = tl.abs(x[0])
    for j in tl.static_range(1, 16):
        block_max = tl.maximum(block_max, tl.abs(x[j]))
    scale6, scale4, inverse6, inverse4 = _block_scales(
        block_max, encoder_scale, decoder_scale
    )
    packed6, error6 = _candidate(x, inverse6, scale6, outer_scale)
    packed4, error4 = _candidate(x, inverse4, scale4, outer_scale)
    choose4 = error4 < error6
    return (
        tl.where(choose4, packed4, packed6),
        tl.where(choose4, scale4, scale6).to(tl.float8e4nv),
    )

_quantize_row(X, Values, Scales, Amax, row, K, BLOCK)

Quantizes row row of X, with row-major scales and the row amax in Amax.

Source code in vllm/models/inkling2/nvidia/ops/nvfp4.py
@triton.jit
def _quantize_row(X, Values, Scales, Amax, row, K: tl.constexpr, BLOCK: tl.constexpr):
    """Quantizes row `row` of `X`, with row-major scales and the row amax
    in `Amax`."""
    row = row.to(tl.int64)
    amax = tl.zeros((BLOCK,), tl.float32)
    for start in range(0, K, BLOCK):
        cols = start + tl.arange(0, BLOCK)
        x = tl.load(X + row * K + cols, cols < K, other=0)
        amax = tl.maximum(amax, tl.abs(x).to(tl.float32))
    amax = tl.max(amax, 0)
    tl.store(Amax + row, amax)
    encoder_scale = tl.minimum(tl.div_rn(1536.0, amax), FP32_MAX)
    decoder_scale = tl.div_rn(1.0, encoder_scale)
    outer_scale = amax * (1.0 / 1536.0)
    for start in range(0, K, BLOCK):
        cols = start + tl.arange(0, BLOCK)
        x = tl.load(X + row * K + cols, cols < K, other=0).to(tl.float32)
        packed, scales = _quantize_block(
            x, encoder_scale, decoder_scale, outer_scale, BLOCK
        )
        _store_block(Values, Scales, row, start, packed, scales, K, BLOCK, 0)

nvfp4_quantize(x)

Quantize rows of bf16 x [M, K] to NVFP4.

Returns packed e2m1 values, row-major e4m3 block scales and the fp32 per-row amax.

Source code in vllm/models/inkling2/nvidia/ops/nvfp4.py
def nvfp4_quantize(x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
    """Quantize rows of bf16 `x` [M, K] to NVFP4.

    Returns packed e2m1 values, row-major e4m3 block scales and the fp32
    per-row amax.
    """
    assert x.ndim == 2 and x.is_contiguous() and x.shape[1] % 16 == 0
    num_rows, k = x.shape
    values = torch.empty((num_rows, k // 2), dtype=torch.uint8, device=x.device)
    scales = torch.empty(
        (num_rows, k // 16), dtype=torch.float8_e4m3fn, device=x.device
    )
    amax = torch.empty(num_rows, dtype=torch.float32, device=x.device)
    # Wide programs for latency with few rows, narrow ones for occupancy.
    block, num_warps = (512, 4) if num_rows <= 512 else (256, 1)
    block = math.gcd(k, block)
    _nvfp4_quantize_kernel[(min(num_rows, 8192),)](
        x,
        values,
        scales,
        amax,
        num_rows,
        K=k,
        BLOCK=block,
        num_warps=num_warps,
        enable_fp_fusion=False,
        launch_pdl=True,
    )
    return values, scales, amax