Skip to content

Linear Algebra 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(...).

Dense GEMM

tileops.gemm.GEMMFwdOp

Dense GEMM with an optional bias. Nothing is committed at construction: m, n, k and the dtype come from the forward inputs, so eval_roofline() is valid only after a call.

The (trans_a, trans_b) pair selects one of four layouts:

Flags Layout Product
(False, True) NT, the default \(d = a \mathbin{@} b^{\top}\)
(False, False) NN \(d = a \mathbin{@} b\)
(True, False) TN \(d = a^{\top} \mathbin{@} b\)
(True, True) TT \(d = a^{\top} \mathbin{@} b^{\top}\)

A bias is added to every row of the product: the contraction accumulates in float32, the bias is added to that sum, and the result is rounded once to the input dtype, as torch.nn.functional.linear rounds it. out_dtype=torch.float32 returns that sum unrounded, as F.linear(a.float(), b.float()) computes it.

Accuracy of a float32 output: each K step's products are added to the float32 sum with truncation, so the error grows as \(K^{1.5}\) (below about 2e-3 at \(K = 4096\) over standard normal operands).

__init__

__init__(
    trans_a=False,
    trans_b=True,
    *,
    out_dtype=None,
    target=None
)

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

Parameters:

  • trans_a (bool, default: False ) –

    Whether a is stored transposed (\([K \times M]\)).

  • trans_b (bool, default: True ) –

    Whether b is stored transposed (\([N \times K]\)). Default True (NT).

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

    torch.float32 for an unrounded float32 d; None rounds to the operands' dtype.

  • 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(
    a,
    b,
    bias=None,
)

Multiply the two matrices under the layout the constructor selected, and add the bias.

Parameters:

  • a (Tensor) –

    Left operand, \([M \times K]\), or \([K \times M]\) when trans_a.

  • b (Tensor) –

    Right operand, \([N \times K]\) under the default NT layout, or \([K \times N]\) when trans_b is false.

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

    Optional bias, \([N]\), in the dtype of the inputs.

Returns:

  • Tensor –

    The product plus bias, \([M \times N]\), in out_dtype, or the dtype of the

  • Tensor –

    inputs when it is None.

Example
1
2
3
4
op = GEMMFwdOp()                      # NT by default
d = op(a, b)                          # a=[M,K], b=[N,K] -> d=[M,N]
d = op(a, b, bias)                    # bias=[N], added to every row
flops, nbytes = op.eval_roofline()    # valid after the forward

tileops.gemm.GEMMFP8FwdOp

Dense FP8 NT GEMM, input-inferred: \(d = (a \cdot s_a) \mathbin{@} (b \cdot s_b)^{\top} + \text{bias}\).

a is \([M \times K]\) and b is \([N \times K]\) (NT only, no transpose flag). a_scale and b_scale are both per-tensor \([1 \times 1]\) scales, or a_scale is per 1x128 block along K, \([M \times \lceil K/128 \rceil]\), with b_scale per 1x128 block, \([N \times \lceil K/128 \rceil]\), or per 128x128 block, \([\lceil N/128 \rceil \times \lceil K/128 \rceil]\).

__init__

__init__(
    out_dtype=torch.bfloat16,
    *,
    target=None
)

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

Parameters:

  • out_dtype (dtype, default: bfloat16 ) –

    Output dtype, torch.bfloat16 or torch.float16.

  • 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(
    a,
    b,
    a_scale,
    b_scale,
    bias=None,
)

Multiply the two FP8 matrices, apply the scales, and add the bias.

Parameters:

  • a (Tensor) –

    Left operand, \([M \times K]\), torch.float8_e4m3fn.

  • b (Tensor) –

    Right operand, \([N \times K]\), same dtype as a.

  • a_scale (Tensor) –

    torch.float32 scales for a: per-tensor \([1 \times 1]\), or per 1x128 block \([M \times \lceil K/128 \rceil]\).

  • b_scale (Tensor) –

    torch.float32 scales for b: \([1 \times 1]\) when a_scale is, else per 1x128 block \([N \times \lceil K/128 \rceil]\) or per 128x128 block \([\lceil N/128 \rceil \times \lceil K/128 \rceil]\).

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

    Optional bias, \([N]\), in out_dtype.

Returns:

  • Tensor –

    The scaled product plus bias, \([M \times N]\), in out_dtype.

Example
1
2
3
op = GEMMFP8FwdOp(out_dtype=torch.bfloat16)
d = op(a, b, a_scale, b_scale)        # per-tensor scales
flops, nbytes = op.eval_roofline()    # valid after the forward

tileops.gemm.GEMMW4A16FwdOp

Dense W4A16 NT GEMM with group-wise affine weight dequantization.

Inputs are activation [M, K], prepacked weight [N, K/2], and scale and zero-point tensors [N, K/group_size]. Weight (n, k) is (q - zero[n, g]) * scale[n, g] with g = k // group_size and q its INT4 value. repack converts a row-major packed weight once at load time; the two layouts have the same shape and dtype and cannot be distinguished at runtime. The output is activation @ W.T with shape [M, N]. The in-tree kernel serves group_size = 128 only and refuses any other value when it is built.

__init__

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

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

Parameters:

  • group_size (int, default: 128 ) –

    Weights per dequantization 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(
    activation,
    packed_weight,
    weight_scale,
    weight_zero,
)

Multiply the activations by the dequantized INT4 weight.

Parameters:

  • activation (Tensor) –

    Activations, \([M \times K]\), torch.float16.

  • packed_weight (Tensor) –

    Weights, \([N \times K/2]\), torch.uint8, in the order repack returns.

  • weight_scale (Tensor) –

    Group scales, \([N \times K/\text{group\_size}]\), in the activation dtype.

  • weight_zero (Tensor) –

    Group zero points, \([N \times K/\text{group\_size}]\), torch.uint8.

Returns:

  • Tensor –

    The product, \([M \times N]\), in torch.float16.

Example
1
2
3
op = GEMMW4A16FwdOp()
d = op(activation, packed_weight, weight_scale, weight_zero)
flops, nbytes = op.eval_roofline()    # valid after the forward

tileops.gemm.GEMMW4A8FwdOp

Dense W4A8 NT GEMM: FP8 activations times a group-scaled signed INT4 weight.

Inputs are activation [M, K] in float8_e4m3fn, prepacked weight [N, K/2], float8_e4m3fn group scales [N, K/group_size], and float32 channel scales [N] and token scales [M]; the output is [M, N] in bfloat16.

Weight (n, k) enters the product as q * weight_group_scale[n, k // group_size] rounded to float8_e4m3fn, nearest even, with q its signed INT4 value in [-8, 7]. Output (m, n) is that FP8 product summed over K in FP32, times activation_scale[m] * weight_channel_scale[n], rounded to bfloat16.

Accuracy: for finite inputs each output is within 5e-5 * K + 2e-2 * |r| of r, the FP32 contraction with token-first scaling and BF16 rounding, and the squared error of the whole output is at most 5e-5 of its and r's combined energy. Where a zero channel or token scale makes r zero, the output is exactly zero. Infinities and NaNs appear where r has them; NaN payloads and the sign of zero are unspecified. Results are not bitwise reproducible across calls.

forward reads the weight in the order repack returns, which has the shape and dtype of the row-major packing, so passing the row-major weight gives wrong results rather than an error. M must be positive, N a positive multiple of 16 and K a positive multiple of 128. The in-tree kernels serve group_size = 128 only and refuse any other value when one is selected. One instance must not run on two streams at once.

__init__

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

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

Parameters:

  • group_size (int, default: 128 ) –

    Weights per group scale 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(
    activation,
    packed_weight,
    weight_group_scale,
    weight_channel_scale,
    activation_scale,
)

Multiply the FP8 activations by the scaled INT4 weight.

Parameters:

  • activation (Tensor) –

    Activations, \([M \times K]\), torch.float8_e4m3fn.

  • packed_weight (Tensor) –

    Weights, \([N \times K/2]\), torch.uint8, in the order repack returns.

  • weight_group_scale (Tensor) –

    Group scales, \([N \times K/\text{group\_size}]\), torch.float8_e4m3fn.

  • weight_channel_scale (Tensor) –

    Channel scales, \([N]\), torch.float32.

  • activation_scale (Tensor) –

    Token scales, \([M]\), torch.float32.

Returns:

  • Tensor –

    The product, \([M \times N]\), in torch.bfloat16.

Example
1
2
3
4
op = GEMMW4A8FwdOp()
prepacked = op.repack(packed_weight)  # once, at load time
d = op(activation, prepacked, group_scale, channel_scale, token_scale)
flops, nbytes = op.eval_roofline()    # valid after the forward

tileops.gemm.GEMMINT8W8A8FwdOp

INT8 NT GEMM with per-token and per-channel dequantization, input-inferred: \(d = (a \mathbin{@} b^{\top}) \cdot s_a \cdot s_b + \text{bias}\).

a is an \([M \times K]\) activation and b an \([N \times K]\) weight, both torch.int8 (NT only, no transpose flag). a_scale is one float32 scale for a (\([1 \times 1]\)) or one per token (\([M \times 1]\)); b_scale one for b (\([1 \times 1]\)) or one per output channel (\([1 \times N]\)).

Accuracy: the product is accumulated in int32 and is exact while \(K \le 131071\) (\(|a_i b_i| \le 2^{14}\)); past it a sum can wrap. Each result is converted to float32, multiplied by its token scale and then its channel scale, the bias is added in float32, and the sum is rounded once to out_dtype. A result can differ from vllm._custom_ops.cutlass_scaled_mm by one unit in the last place of out_dtype, where the two order the float32 multiplies differently.

__init__

__init__(
    out_dtype=torch.bfloat16,
    *,
    target=None
)

Build the op. Shapes and scale granularities are taken from each call.

Parameters:

  • out_dtype (dtype, default: bfloat16 ) –

    Output dtype, torch.bfloat16 or torch.float16.

  • 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(
    a,
    b,
    a_scale,
    b_scale,
    bias=None,
)

Multiply the two INT8 matrices, dequantize, and add the bias.

Parameters:

  • a (Tensor) –

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

  • b (Tensor) –

    Weight, \([N \times K]\), torch.int8.

  • a_scale (Tensor) –

    torch.float32 scale of a: \([1 \times 1]\), or one per row, \([M \times 1]\).

  • b_scale (Tensor) –

    torch.float32 scale of b: \([1 \times 1]\), or one per row of b, \([1 \times N]\).

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

    Optional bias, \([N]\), in out_dtype.

Returns:

  • Tensor –

    The dequantized product plus bias, \([M \times N]\), in out_dtype.

Example
1
2
3
op = GEMMINT8W8A8FwdOp(out_dtype=torch.bfloat16)
d = op(a, b, a_scale, b_scale)        # a=[M,K], b=[N,K] -> d=[M,N]
flops, nbytes = op.eval_roofline()    # valid after the forward

Batched GEMM

tileops.gemm.BMMFwdOp

Batched dense GEMM: \(d_i = a_i \mathbin{@} b_i \quad \text{for } i \in [0, B)\).

Shapes are strictly 3D: a is \([B \times M \times K]\), b is \([B \times K \times N]\) as in torch.bmm (no transpose flag), and d is \([B \times M \times N]\). a and out may be strided: a batch-strided view, such as the transpose of an \([M \times B \times K]\) tensor, is read or written in place. Each new (batch, m, n, k, dtype) and each new strided layout of a or out compiles a kernel on first use. The in-tree kernels need \(K\) to be a multiple of 16.

__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(
    a,
    b,
    *,
    out=None
)

Multiply the two batches, one GEMM per batch item.

Parameters:

  • a (Tensor) –

    Left operand, \([B \times M \times K]\); any strides.

  • b (Tensor) –

    Right operand, \([B \times K \times N]\).

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

    \([B \times M \times N]\) buffer the product is written to, any strides, or None.

Returns:

  • Tensor –

    The product, \([B \times M \times N]\), in the dtype of the inputs: out

  • Tensor –

    where given.

Example
1
2
3
op = BMMFwdOp()
d = op(a, b)                          # a=[B,M,K], b=[B,K,N] -> d=[B,M,N]
flops, nbytes = op.eval_roofline()    # valid after the forward

tileops.gemm.BMMFP8FwdOp

Batched FP8 GEMM: d[i] = (a[i] @ b[i]) * a_scale * b_scale.

trans_b states which axis order b arrives in: False, the default, is torch.bmm's \([B \times K \times N]\); True is \([B \times N \times K]\).

A b that does not lie K-innermost in memory, under either value of trans_b, is first transposed into a new buffer (one extra read and write of b), because FP8 WGMMA reads b only K-innermost. The in-tree kernel needs \(K\) to be a multiple of 32.

__init__

__init__(
    out_dtype=torch.bfloat16,
    trans_b=False,
    *,
    target=None
)

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

Parameters:

  • out_dtype (dtype, default: bfloat16 ) –

    Output tensor dtype (torch.float16 or torch.bfloat16).

  • trans_b (bool, default: False ) –

    Whether b's axes are \([B \times N \times K]\) rather than \([B \times K \times N]\). Default False.

  • 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(
    a,
    b,
    a_scale,
    b_scale,
)

Multiply the two FP8 batches and apply the two scales.

Parameters:

  • a (Tensor) –

    Left operand, \([B \times M \times K]\), torch.float8_e4m3fn.

  • b (Tensor) –

    Right operand, same dtype as a: \([B \times K \times N]\) under the default trans_b, or \([B \times N \times K]\) when trans_b is true.

  • a_scale (Tensor) –

    Per-tensor scale for a, a 0-dim torch.float32 tensor.

  • b_scale (Tensor) –

    Per-tensor scale for b, a 0-dim torch.float32 tensor.

Returns:

  • Tensor –

    The scaled product, \([B \times M \times N]\), in out_dtype.

Example
1
2
3
op = BMMFP8FwdOp(trans_b=True)      # b as [B, N, K]
d = op(a, b_nk, a_scale, b_scale)
flops, nbytes = op.eval_roofline()    # valid after the forward

tileops.gemm.BMMW8A16FwdOp

Batched W8A16 NT GEMM: \(d_i = a_i \mathbin{@} \hat{w}_i^{\top}\) for \(i \in [0, B)\).

activation is \([B \times M \times K]\) bfloat16, weight \([B \times N \times K]\) e4m3 codes and weight_scale one float32 per 128x128 block of each weight, \([B \times \lceil N/128 \rceil \times K/128]\); weight \(\hat{w}\) is each code times its block's scale. The product accumulates in float32 and rounds to bfloat16. With power-of-two scales, as DeepSeek's FP8 checkpoints hold, every \(\hat{w}\) is the bfloat16 value dequantizing the checkpoint gives, so the product is the bfloat16 batched matmul on the dequantized weight up to the order of its float32 sums. activation and out may be strided: a batch-strided view, such as the transpose of an \([M \times B \times K]\) tensor, is read or written where it lies. The in-tree kernel needs \(N\) to be a multiple of 8.

__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(
    activation,
    weight,
    weight_scale,
    *,
    out=None
)

Multiply each activation batch item by its dequantized weight.

Parameters:

  • activation (Tensor) –

    \([B \times M \times K]\), torch.bfloat16; any strides.

  • weight (Tensor) –

    \([B \times N \times K]\), torch.float8_e4m3fn.

  • weight_scale (Tensor) –

    \([B \times \lceil N/128 \rceil \times K/128]\), torch.float32.

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

    \([B \times M \times N]\) bfloat16 buffer the product is written to, any strides, or None.

Returns:

  • Tensor –

    The product, \([B \times M \times N]\), torch.bfloat16: out where given.

Example
1
2
3
op = BMMW8A16FwdOp()
d = op(activation, weight, weight_scale)  # [B,M,K] x [B,N,K] -> [B,M,N]
flops, nbytes = op.eval_roofline()        # valid after the forward

Grouped GEMM

tileops.gemm.GroupedGEMMFwdOp

Grouped GEMM with configurable transpose modes. Nothing is committed at construction: the extents and the dtype come from the forward inputs. Rows are grouped by batch_sizes; there is no epilogue.

The (trans_a, trans_b) pair selects one of four layouts:

Flags Layout Product
(False, True) NT, the default \(C = A \mathbin{@} B^{\top}\)
(False, False) NN \(C = A \mathbin{@} B\)
(True, False) TN \(C = A^{\top} \mathbin{@} B\)
(True, True) TT \(C = A^{\top} \mathbin{@} B^{\top}\)

The metadata values are the caller's obligation and are not checked, since checking them would synchronise: batch_sizes sums to the packed row count, and batch_offsets is its exclusive prefix sum.

__init__

__init__(
    trans_a=False,
    trans_b=True,
    *,
    target=None
)

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

Parameters:

  • trans_a (bool, default: False ) –

    Whether the groups split the contraction: a is \([\mathit{batch\_sum} \times N]\) and the output keeps a group axis.

  • trans_b (bool, default: True ) –

    Whether the per-group operand is stored transposed. Default True (NT).

  • 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(
    a,
    b,
    batch_sizes,
    batch_offsets,
)

Run one GEMM per group, with the groups packed along a single axis.

Parameters:

  • a (Tensor) –

    Activations for every group, \([\mathit{batch\_sum} \times K]\), or \([\mathit{batch\_sum} \times N]\) when trans_a.

  • b (Tensor) –

    Per-group weights, \([\mathit{batch\_count} \times N \times K]\) under the default NT layout, \([\mathit{batch\_count} \times K \times N]\) under NN; with trans_a, \([K \times \mathit{batch\_sum}]\) or \([\mathit{batch\_sum} \times K]\).

  • batch_sizes (Tensor) –

    Rows per group, 1D torch.int32.

  • batch_offsets (Tensor) –

    Start row of each group in a, 1D torch.int32.

Returns:

  • Tensor –

    The per-group products in the dtype of the inputs:

  • Tensor –

    \([\mathit{batch\_sum} \times N]\), or

  • Tensor –

    \([\mathit{batch\_count} \times N \times K]\) when trans_a.

Example
op = GroupedGEMMFwdOp()               # NT by default
d = op(a, b, batch_sizes, batch_offsets)