com.microsoft.TransposeMatMul
com.microsoft · ONNX Runtime contrib operator · contrib since_version 1
Description
Matrix product of two N-dimensional tensors A and B, following NumPy-style matrix-multiplication broadcasting, with optional transposition of either operand's last two dimensions and a scalar alpha multiplier. This is the strict subset of FusedMatMul that omits batch-dimension transposition, and it uses the same kernels. Float32 and float16 are supported; double and bfloat16 are not implemented.
See the ONNX Runtime TransposeMatMul contrib-operator spec for the reference semantics.
Inputs
| Name | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|
A |
T |
— | — | N-dimensional matrix A. | required |
B |
T |
— | — | N-dimensional matrix B. | required |
Outputs
| Name | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|
Y |
T |
derived | derived | Matrix-multiplication result whose shape follows NumPy-style rules after applying the requested batch and matrix transpositions. | required |
Attributes
Default values (overridable per request):
| Attribute | Default | Description |
|---|---|---|
alpha |
1 |
Scalar multiplier applied to the product of the input tensors. |
transA |
0 |
When non-zero, transposes A on its last two dimensions before multiplication. |
transB |
0 |
When non-zero, transposes B on its last two dimensions before multiplication. |
Type constraints
| Variable | Allowed dtypes |
|---|---|
T |
float32, float16 |
Implementation variants
One implementation is selected per call from the device capabilities, the request shapes and the dtypes; these notes say what each one covers.
native16_packed_shared_b_64x64,native16_packed_shared_b_128x128,native16_packed_shared_b_64x128— Multiplies contiguous half rows by shared rank-two weights, accumulating in float32 and applying alpha before one half output conversion.native16_batched_gemm— Batched half products on the general subgroup-matrix GEMM: a 2 x 2 subgroup grid owns 64 x 64 output tiles, staging each operand as stored, with no transpose copy (transposed operands load column-major fragments), accumulating in float32 and applying any alpha scale before one half conversion. Broadcast batch dimensions read with zero strides.tiled_reg— Register-blockedY = alpha * A @ Bwhere no subgroup-matrix variant applies: plain rank-2 products, rank-4 A against a rank-2 or rank-3 B, and broadcast products with physically transposed B. Keeps f32 accumulation and preserves scalar K order for f16 transposed B; with few tiles or heavy padding, the transposed-B form is tried after other variants.broadcast_transb_subgroup_matrix— Broadcast transposed-B product using a supported 8x8x8 subgroup-matrix configuration with f32 accumulation. Logical shapes and physical B strides share the generic matrix engine; insufficient output tiles or excessive padding retain the generic tile.m1_gemv_vec4— Vector-by-matrix specialization for a single output row: each workgroup owns 32 consecutive vec4 column groups and partitions the reduction across the workgroup's second dimension. The accumulator stays float32 for both tensor types.rank2_band_vec4_splitk— Splits the vec4 band's K axis across up to sixteen workgroups. Each range writes an f32 partial band with alpha applied, and a combine pass sums the partials.rank2_band_vec4,rank2_band_vec4_f32_preferred— Few-row kernel for a rank-2 product without transposes. Each lane owns one vec4 column group and one accumulator per row, so every B word feeds all 2 to 16 rows; alpha is applied at the store._f32_preferredis the same kernel, tried first for f32 unless the product has both many rows and a deep K.subgroup_matrix_splitk— Partitions the K reduction of small-M rank-2 products across subgroup-matrix workgroups, then combines float32 partials that already include alpha.subgroup_matrix_direct_b_partial_k— Reads complete 8×8 right-hand fragments directly from storage. The final partial reduction stage pads both operands in workgroup memory, preserving the last valid dot-product terms.subgroup_matrix— Subgroup-matrixY = alpha * op(A) @ op(B)over dense batches with float32 accumulation. An output width that is not a multiple of the 64-wide column tile switches the trailing tile to guarded addressing: its B columns clamp to N - 1 and its stores drop every column at or past N. Yields the shape when padding N to whole column tiles exceeds its limit.subgroup_matrix_16x16x16_plain— Experimental16x16x16half-precision subgroup matrices withf32accumulation. Thealphascale is applied inf32before the output cast. The matrix path runs only when the workgroup's subgroups are complete and of the expected width; otherwise the kernel computes every output element without them.
Device requirements
Some implementation variants require subgroup-matrix, shader-f16, and subgroups. Other variants run without them; which variant runs also depends on the request's shapes and dtypes.
Files
metadata.json— kernel metadata (id, file digests, manifest spec, provenance)manifest.json— the op contract (source of truth)test.json— correctness casesbench.json— benchmark casescontrib-native16-packed.wgsl.jinjafused-matmul-subgroup-matrix.wgsl.jinjamatmul-band-vec4.wgsl.jinjamatmul-subgroup-matrix-ext.wgsl.jinjamatmul-tiled-general-reg.wgsl.jinjamatmul-tiled-general.wgsl.jinjamatmul-vector-matrix-vec4.wgsl.jinjamatrix-shaped-projection-gemm.wgsl.jinjareduce-axis0-splitk-combine.wgsl.jinjasubgroup-matrix-gemm.wgsl.jinja
Use with @huggingface/kernels
npm install --save-exact @huggingface/kernels@0.0.1-preview.4
Paste the example into a browser app that resolves npm imports. Serve the app over HTTPS or localhost with WebGPU available.
Required output shapes and logical data types are inferred from the supplied inputs and attributes; result tensors are allocated automatically.
The version: 1 option selects the published kernel contract; it is independent of any operator opset, contrib since_version, or model version.
It follows the v1 branch as fixes land. To pin exact artifact bytes, pass a 40-character commit revision instead of version.
import { getKernel } from "@huggingface/kernels";
const kernel = await getKernel("webgpu-kernels/com.microsoft.TransposeMatMul", { version: 1 });
const AData = Float32Array.of(0, 1, 2);
const BData = Float32Array.of(0, 1, 2);
const { Y } = await kernel({ A: { data: AData, shape: [3] }, B: { data: BData, shape: [3] } });
console.log({ Y });
- Downloads last month
- -
Requires WebGPU support. See the compatibility table.