Skip to content

Quantization and Dequantization Operators

Every op on this page is used the same way: construct it once, then call it. The constructor takes what the kernel is compiled with; the call takes the tensors. Both are documented under each op — __init__ and forward, where forward is what runs when you call op(...).

INT8 quantization

tileops.quantization.INT8QuantPerTensorFwdOp

Symmetric INT8 quantization of a 2-D tensor against one scale for the whole tensor.

scale is amax(|x|) / 127 in float32, or 1.0 when x is all zero, and is the dequantization multiplier: x ~= q * scale. q is x / scale rounded half to even and clamped to [-127, 127]. Both are bit-equal to the torch expression.

A NaN or an infinity in x reaches scale as it does in torch; q is then unspecified.

__init__

__init__(
    *,
    target=None
)

Build the op; shapes and dtypes arrive with each call. A subclass with manifest parameters assigns them first and then calls this.

It selects and builds no kernel and reads no device property: an op constructs wherever it is imported, and a device it cannot run on is refused at the first call.

Parameters:

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

Raises:

  • ValueError –

    A manifest parameter is outside its signature, or an implementation breaks its kernel interface's contract.

  • TypeError –

    An implementation breaks its kernel interface's contract.

forward

forward(
    x,
)

Quantize x against its absolute maximum.

Parameters:

  • x (Tensor) –

    \([M \times K]\), float16, bfloat16 or float32.

Returns:

  • tuple[Tensor, Tensor] –

    q \([M \times K]\) in int8 and scale \([1]\) in float32.

tileops.quantization.INT8QuantPerChannelFwdOp

Symmetric INT8 quantization of a weight with one scale per output channel (row).

scale[n] is amax(|w[n, :]|) / 127 in float32, or 1.0 for an all-zero row, and is the dequantization multiplier: w[n, :] ~= q[n, :] * scale[n]. q is the row divided by its scale, rounded half to even and clamped to [-127, 127].

Both are bit-equal to the torch expression.

A NaN or an infinity in a row reaches that row's scale as it does in torch; the row's q is then unspecified, as it is when the row's amax is so small (below about 8.8e-44, reachable only in float32) that its scale rounds to zero.

__init__

__init__(
    *,
    target=None
)

Build the op; shapes and dtypes arrive with each call. A subclass with manifest parameters assigns them first and then calls this.

It selects and builds no kernel and reads no device property: an op constructs wherever it is imported, and a device it cannot run on is refused at the first call.

Parameters:

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

Raises:

  • ValueError –

    A manifest parameter is outside its signature, or an implementation breaks its kernel interface's contract.

  • TypeError –

    An implementation breaks its kernel interface's contract.

forward

forward(
    w,
)

Quantize each row of w against its own absolute maximum.

Parameters:

  • w (Tensor) –

    \([N \times K]\), float16, bfloat16 or float32.

Returns:

  • tuple[Tensor, Tensor] –

    q \([N \times K]\) in int8 and scale \([N]\) in float32.

tileops.quantization.INT8QuantPerTokenGroupFwdOp

Symmetric INT8 quantization with one scale per 128 contiguous elements along K.

Each row of x is split into blocks of 128 elements along K; the last block of a row holds K % 128 elements when K is not a multiple of 128. A block's scale is amax(|block|) / 127 in float32, or 1.0 for an all-zero block, and is the dequantization multiplier. q is each element divided by its block's scale, rounded half to even and clamped to [-127, 127].

Both are bit-equal to the torch expression.

A NaN or an infinity in a block reaches that block's scale as it does in torch; the block's q is then unspecified, as it is when the block's amax is so small (below about 8.8e-44, reachable only in float32) that its scale rounds to zero.

__init__

__init__(
    *,
    target=None
)

Build the op; shapes and dtypes arrive with each call. A subclass with manifest parameters assigns them first and then calls this.

It selects and builds no kernel and reads no device property: an op constructs wherever it is imported, and a device it cannot run on is refused at the first call.

Parameters:

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

Raises:

  • ValueError –

    A manifest parameter is outside its signature, or an implementation breaks its kernel interface's contract.

  • TypeError –

    An implementation breaks its kernel interface's contract.

forward

forward(
    x,
)

Quantize each 128-element block of x against its own absolute maximum.

Parameters:

  • x (Tensor) –

    \([M \times K]\), float16, bfloat16 or float32.

Returns:

  • tuple[Tensor, Tensor] –

    q \([M \times K]\) in int8 and scale \([M \times \lceil K / 128 \rceil]\) in float32.

tileops.quantization.SmoothQuantFwdOp

SmoothQuant activation quantization: per-channel smoothing, then symmetric INT8 per row.

Each column of x is divided by its smoothing factor, xs = x / smooth in float32, as in SmoothQuant (Xiao et al., 2023). scale[m] is amax(|xs[m, :]|) / 127, or 1.0 for an all-zero row, and is the dequantization multiplier of xs. q is xs[m, :] / scale[m] rounded half to even and clamped to [-127, 127].

Both are bit-equal to the torch expression.

A NaN or an infinity in xs (a zero smoothing factor gives one) reaches that row's scale as it does in torch; the row's q is then unspecified, as it is when the row's amax is so small (below about 8.8e-44) that its scale rounds to zero.

__init__

__init__(
    *,
    target=None
)

Build the op; shapes and dtypes arrive with each call. A subclass with manifest parameters assigns them first and then calls this.

It selects and builds no kernel and reads no device property: an op constructs wherever it is imported, and a device it cannot run on is refused at the first call.

Parameters:

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

Raises:

  • ValueError –

    A manifest parameter is outside its signature, or an implementation breaks its kernel interface's contract.

  • TypeError –

    An implementation breaks its kernel interface's contract.

forward

forward(
    x,
    smooth,
)

Smooth x by channel, then quantize each row against its absolute maximum.

Parameters:

  • x (Tensor) –

    \([M \times K]\), float16 or bfloat16.

  • smooth (Tensor) –

    \([K]\), float32: the per-channel smoothing factors.

Returns:

  • tuple[Tensor, Tensor] –

    q \([M \times K]\) in int8 and scale \([M]\) in float32.

FP8 quantization

tileops.quantization.FP8QuantPerTokenGroupFwdOp

Quantize each group of columns to FP8, or fake-quantize x in place.

vLLM's per_token_group_quant_fp8. A group is group_size adjacent columns of the last axis.

Without use_ue8m0, scale is the group's absolute maximum, floored at eps, times the float32 nearest 1 / 448, which is how torch on CUDA divides a tensor by 448.0, and q is the group scaled by the reciprocal of that scale to within a few float32 ulps, so for a finite group every element is within one float8_e4m3fn code of clamp(x / scale, -448, 448) rather than equal to it. With out_q the codes are written there and it is returned as q.

A group holding an infinity or a NaN has no defined result: whether the group maximum and the clamp carry the non-finite value depends on the kernel serving the call. Without use_ue8m0, neither has a group whose floored maximum is below 448 / 3.4e38, about 1.3e-36, which only an eps below that bound admits (an all-zero group under eps=0, for one): the reciprocal of its scale overflows float32.

use_ue8m0=None gives float32 scales, as vLLM resolves None where DeepGEMM's UE8M0 scales are not in use; tma_aligned_scales takes effect only with column_major_scales, as in vLLM.

With use_ue8m0 the scale is \(\max(2^{\lceil \log_2(\max(amax, eps) \cdot r) \rceil}, 2^{-126})\), r the float32 nearest 1 / 448, and q is clamp(x / scale, -448, 448) rounded to nearest even exactly: dividing by a power of two loses nothing. The floor, float32's smallest normal power of two, is the scale act_quant gives a subnormal max(amax, eps) * r; it keeps every finite group defined, an all-zero group under eps=0 included. With eps=1e-4 this is DeepSeek-V4-Flash inference/kernel.py act_quant with scale_fmt="ue8m0", bit for bit. scale_dtype float8_e8m0fnu stores these power-of-two scales.

With inplace, x is overwritten with q * scale in its own dtype, where it lies (a column slice of wider rows is written without a copy), and returned in the third slot, q and scale being None; otherwise the third slot is None.

__init__

__init__(
    group_size,
    eps=1e-10,
    dtype=None,
    column_major_scales=False,
    tma_aligned_scales=False,
    use_ue8m0=None,
    scale_dtype=torch.float32,
    inplace=False,
    *,
    target=None
)

Build the op. Shapes and the input dtype are taken from the first call.

Parameters:

  • group_size (int) –

    Columns per group; divides the last axis.

  • eps (float, default: 1e-10 ) –

    Floor of each group's absolute maximum.

  • dtype (dtype | None, default: None ) –

    The codes' dtype, torch.float8_e4m3fn, or None for it.

  • column_major_scales (bool, default: False ) –

    Lay the scales out column-major.

  • tma_aligned_scales (bool, default: False ) –

    Pad the column-major scales to TMA alignment; read only with column_major_scales.

  • use_ue8m0 (bool | None, default: None ) –

    Round each scale up to a power of two; None for float32 scales.

  • scale_dtype (dtype, default: float32 ) –

    torch.float32, or torch.float8_e8m0fnu with use_ue8m0.

  • inplace (bool, default: False ) –

    Overwrite x with its fake-quantized values instead of returning q and scale.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    x,
    out_q=None,
)

Quantize x group by group.

Parameters:

  • x (Tensor) –

    \([*S \times K]\), float16, bfloat16 or float32; written in place under inplace, a strided view included.

  • out_q (Tensor | None, default: None ) –

    \([*S \times K]\) contiguous float8_e4m3fn buffer the codes are written to, or None.

Returns:

  • Tensor | None –

    (q, scale, None): q \([*S \times K]\) in float8_e4m3fn (out_q where

  • Tensor | None –

    given) and scale \([*S \times K / group\_size]\) in scale_dtype; with

  • Tensor | None –

    inplace, (None, None, x).

tileops.quantization.FP8QuantPerBlockFwdOp

Block-scaled float8_e4m3fn quantization of a weight, one scale per 128x128 tile.

This is the block-scaled FP8 weight format of DeepSeek-V3 checkpoints, where scale is stored as weight_scale_inv. Edge tiles cover the N % 128 rows and K % 128 columns that remain. A tile's scale is amax(|tile|) / 448 in float32, or 1.0 for an all-zero tile, and is the dequantization multiplier. q is each element divided by its tile's scale, clamped to [-448, 448] and rounded to the nearest float8_e4m3fn.

Both are bit-equal to the torch expression for every input, a tile holding an infinity or a NaN and a tile whose scale is subnormal or rounds to zero included.

__init__

__init__(
    *,
    target=None
)

Build the op; shapes and dtypes arrive with each call. A subclass with manifest parameters assigns them first and then calls this.

It selects and builds no kernel and reads no device property: an op constructs wherever it is imported, and a device it cannot run on is refused at the first call.

Parameters:

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

Raises:

  • ValueError –

    A manifest parameter is outside its signature, or an implementation breaks its kernel interface's contract.

  • TypeError –

    An implementation breaks its kernel interface's contract.

forward

forward(
    w,
)

Quantize each 128x128 tile of w against its own absolute maximum.

Parameters:

  • w (Tensor) –

    \([N \times K]\), bfloat16, float16 or float32.

Returns:

  • tuple[Tensor, Tensor] –

    q \([N \times K]\) in float8_e4m3fn and scale \([\lceil N / 128 \rceil \times \lceil K / 128 \rceil]\) in float32.

FP4 quantization

tileops.quantization.FP4QuantPerGroupFwdOp

Quantize each block of columns to FP4 e2m1, or fake-quantize x in place.

DeepSeek-V4-Flash inference/kernel.py fp4_act_quant, bit for bit. A block is block_size adjacent columns of the last axis. Its scale is \(s = 2^{\lceil \log_2(\max(amax, 6 \cdot 2^{-126}) \cdot r) \rceil}\), r the float32 nearest 1 / 6, and each element's code is \(x / s\) clamped to \([-6, 6]\) and rounded to the nearest e2m1 value, ties to the even code. A zero keeps its sign: an element of \(x / s\) in \([-0.25, 0)\) has code 8, \(-0\).

q holds two codes a byte, the even column in the low nibble, as uint8: the logical dtype is float4_e2m1fn_x2, which q.view(torch.float4_e2m1fn_x2) gives. scale is float8_e8m0fnu. With inplace, x is overwritten with \(code \cdot s\), exact in bfloat16 below \(|x| = 1.75 \cdot 2^{127}\) and infinite from there, and returned in the third slot, q and scale being None; otherwise the third slot is None. A block holding an infinity or a NaN has no defined result.

__init__

__init__(
    block_size=32,
    inplace=False,
    *,
    target=None
)

Build the op. Shapes are taken from the first call.

Parameters:

  • block_size (int, default: 32 ) –

    Columns per block, each under one scale; divides the last axis.

  • inplace (bool, default: False ) –

    Overwrite x with its fake-quantized values instead of returning q and scale.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    x,
)

Quantize x block by block.

Parameters:

  • x (Tensor) –

    \([*S \times N]\), bfloat16; written in place under inplace, a strided view included.

Returns:

  • Tensor | None –

    (q, scale, None): q \([*S \times N / 2]\) in uint8 and scale

  • Tensor | None –

    \([*S \times N / block\_size]\) in float8_e8m0fnu; with inplace,

  • Tensor | None –

    (None, None, x).

Example
1
2
3
4
5
6
import torch
from tileops.quantization import FP4QuantPerGroupFwdOp

x = torch.randn(4096, 64, 128, dtype=torch.bfloat16, device="cuda")
q, scale, _ = FP4QuantPerGroupFwdOp()(x)
FP4QuantPerGroupFwdOp(inplace=True)(x)

INT4 quantization

tileops.quantization.INT4QuantPerGroupFwdOp

DeepSpeed's asymmetric INT4 quantization, grouped along K.

For a group's minimum lo and maximum hi, the quantization multiplier is 16 / (hi - lo) (1 for a constant group), and its offset is (hi + lo) / 2. Codes are clamp(round((w - offset) * multiplier), -8, 7), with ties to even. Adjacent signed codes occupy the high and low nibbles of each int8 byte. params stores the inverse multiplier and offset as float32, one row per group. Floating-point division may move a code at a rounding boundary by one step.

The in-tree kernels serve power-of-two group sizes from 32 to 1024 or multiples of 128 up to 65536, with N * K <= 2**31 - 1. Nonfinite groups are unspecified.

__init__

__init__(
    group_size=128,
    *,
    target=None
)

Build the op. Shapes and dtypes are taken from each call.

Parameters:

  • group_size (int, default: 128 ) –

    Elements per group along K (default 128).

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    w,
)

Quantize and pack the groups of w.

Parameters:

  • w (Tensor) –

    \([N \times K]\), float16.

Returns:

  • tuple[Tensor, Tensor] –

    packed_weight \([N \times K / 2]\) in int8 and params \([(N K / group\_size) \times 2]\) in float32.

INT8 dequantization

tileops.quantization.INT8DequantPerTensorFwdOp

Dequantize an INT8 matrix with one scale: x = (q.float() * scale).to(out_dtype), scale \([1]\).

__init__

__init__(
    out_dtype,
    *,
    target=None
)

Build the op. Shapes are taken from each call.

Parameters:

  • out_dtype (dtype) –

    Dtype of x: torch.float16, torch.bfloat16 or torch.float32.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    q,
    scale,
)

Dequantize q.

Parameters:

  • q (Tensor) –

    \([M \times K]\), int8.

  • scale (Tensor) –

    float32, in the shape the op states.

Returns:

  • Tensor –

    x \([M \times K]\) in out_dtype.

tileops.quantization.INT8DequantPerChannelFwdOp

Dequantize an INT8 matrix with one scale per row: x = (q.float() * scale[:, None]).to(out_dtype), scale \([M]\).

__init__

__init__(
    out_dtype,
    *,
    target=None
)

Build the op. Shapes are taken from each call.

Parameters:

  • out_dtype (dtype) –

    Dtype of x: torch.float16, torch.bfloat16 or torch.float32.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    q,
    scale,
)

Dequantize q.

Parameters:

  • q (Tensor) –

    \([M \times K]\), int8.

  • scale (Tensor) –

    float32, in the shape the op states.

Returns:

  • Tensor –

    x \([M \times K]\) in out_dtype.

tileops.quantization.INT8DequantPerBlockFwdOp

Dequantize an INT8 matrix with one scale per 128 contiguous elements of a row: x = (q.float() * scale.repeat_interleave(128, dim=1)[:, :K]).to(out_dtype), scale \([M \times \lceil K / 128 \rceil]\); the last block of a row may be partial.

__init__

__init__(
    out_dtype,
    *,
    target=None
)

Build the op. Shapes are taken from each call.

Parameters:

  • out_dtype (dtype) –

    Dtype of x: torch.float16, torch.bfloat16 or torch.float32.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    q,
    scale,
)

Dequantize q.

Parameters:

  • q (Tensor) –

    \([M \times K]\), int8.

  • scale (Tensor) –

    float32, in the shape the op states.

Returns:

  • Tensor –

    x \([M \times K]\) in out_dtype.