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__
¶
Build the op. Shapes and dtypes are taken from the first call.
Parameters:
-
trans_a(bool, default:False) –Whether
ais stored transposed (\([K \times M]\)). -
trans_b(bool, default:True) –Whether
bis stored transposed (\([N \times K]\)). DefaultTrue(NT). -
out_dtype(dtype | None, default:None) –torch.float32for an unrounded float32d;Nonerounds to the operands' dtype. -
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
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_bis 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.
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__
¶
Build the op. Shapes and dtypes are taken from the first call.
Parameters:
-
out_dtype(dtype, default:bfloat16) –Output dtype,
torch.bfloat16ortorch.float16. -
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
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.float32scales fora: per-tensor \([1 \times 1]\), or per 1x128 block \([M \times \lceil K/128 \rceil]\). -
b_scale(Tensor) –torch.float32scales forb: \([1 \times 1]\) whena_scaleis, 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.
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__
¶
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,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
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 orderrepackreturns. -
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.
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__
¶
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,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
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 orderrepackreturns. -
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.
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__
¶
Build the op. Shapes and scale granularities are taken from each call.
Parameters:
-
out_dtype(dtype, default:bfloat16) –Output dtype,
torch.bfloat16ortorch.float16. -
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
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.float32scale ofa: \([1 \times 1]\), or one per row, \([M \times 1]\). -
b_scale(Tensor) –torch.float32scale ofb: \([1 \times 1]\), or one per row ofb, \([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.
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__
¶
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,
BUILTINfor the in-tree kernels, orNoneto 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
¶
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.
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__
¶
Build the op. Shapes and dtypes are taken from the first call.
Parameters:
-
out_dtype(dtype, default:bfloat16) –Output tensor dtype (
torch.float16ortorch.bfloat16). -
trans_b(bool, default:False) –Whether
b's axes are \([B \times N \times K]\) rather than \([B \times K \times N]\). DefaultFalse. -
target(Target, default:None) –Which set of kernels serves this op — a target name,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
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 defaulttrans_b, or \([B \times N \times K]\) whentrans_bis true. -
a_scale(Tensor) –Per-tensor scale for
a, a 0-dimtorch.float32tensor. -
b_scale(Tensor) –Per-tensor scale for
b, a 0-dimtorch.float32tensor.
Returns:
-
Tensor–The scaled product, \([B \times M \times N]\), in
out_dtype.
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__
¶
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,
BUILTINfor the in-tree kernels, orNoneto 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
¶
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:outwhere given.
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__
¶
Build the op. Shapes and dtype are taken from the first call.
Parameters:
-
trans_a(bool, default:False) –Whether the groups split the contraction:
ais \([\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,
BUILTINfor the in-tree kernels, orNoneto decide from the input device.
forward
¶
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, 1Dtorch.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.