ai.onnx.MatMul
ai.onnx · standard ONNX operator · ONNX opset ≥ 9
Description
Computes the matrix product A * B with ONNX MatMul semantics, including vector promotion and NumPy-style broadcasting over batch dimensions. This package supports float16, float32, int32, and uint32 tensors. All legal rank-1 through rank-4 combinations are implemented; rank-5 is supported only when both inputs have rank 5. Mixed-rank combinations involving rank 5, higher-rank inputs, and other ONNX-supported types are unsupported.
See the ONNX MatMul spec for the reference semantics.
Inputs
| Name | Upstream name | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|---|
a |
A |
T |
— | — | N-dimensional left-hand matrix. | required |
b |
B |
T |
— | — | N-dimensional right-hand matrix. | required |
Outputs
| Name | Upstream name | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|---|
y |
Y |
T |
derived | ONNX MatMul result of a and b |
Matrix multiply result of A * B, including vector promotion and broadcast batch dimensions. |
required |
Type constraints
| Variable | Allowed dtypes |
|---|---|
T |
float32, float16, int32, uint32 |
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_64x64,native16_packed_128x128,native16_packed_64x128— Computes rank-two half-precision products, and batched-by-rank-two products folded into rows, with packed operand staging, f32 register accumulation and reusable result-fragment storage before one half output conversion.native16_register_64x64_k32— Reuses blocks of f16 matrix operands with f32 accumulation and one f16 output conversion. Guarded scalar or whole-word loads stage complete tiles; a runtime check of the subgroup layout falls back to a portable path.vector_matrix_deep_f16— Partitions a deep single-row half-precision reduction across a workgroup, then tree-folds float32 partials for four adjacent output columns.subgroup_matrix_16x16x16— Experimental16x16x16matrix primitive withf16inputs,f32accumulation and a32x32output tile. The workgroup verifies its 32- or 64-lane subgroup layout before choosing between native and portablef32paths. Both paths handle M, N and K that are not tile multiples.subgroup_matrix— Computes padded tiles with subgroup matrices and float32 accumulation. AnNthat is not a multiple of the 64-wide column tile clamps the trailing tile'sBcolumns toN - 1and skips every store at or pastN. Ranked lower when rows fill little of each row tile andrank2_band_vec4_splitkapplies, or the last column tile is mostly padding.subgroup_matrix_splitk— Partitions K across subgroup-matrix workgroups, then combines float32 partials, reassociating the reduction. Used when few rows leave most of each row tile as padding, K is deep, splitting K yields enough workgroups, and each split's slice ofAstays small. A rank-3Awith a unit row axis is read as a contiguous[M, K]view.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.rank2_notrans_f32_vec4_tiled_reg_splitk— Splits thef32register tile's K reduction acrossdispatch.zinto power-of-two slices with at least four K tiles each, then sums partials in a second pass. Tiles stage four-wide B words and span 64 columns by 128 rows forM >= 128, 32 forM >= 32, or 16 otherwise.folded_rows_notrans_f32_vec4_tiled_reg,rank2_notrans_f32_vec4_tiled_reg,batched_notrans_f32_vec4_tiled_reg— Float32 register-blocked matrix multiplication with four-wide input and output access. Compact row tiles stage both operands as vectors; larger tiles stage B by scalar columns. Partial row and column tiles are zero-filled and their excess stores are omitted.rank2_m1_vector_matrix_tail— Computes y = a @ B for one row when N is not a multiple of four, readingBrows at their actual, unpadded stride: four f32 outputs per invocation, or one f16 output per invocation that prefetches bounded groups of K operands before ordered f32 accumulation and one final f16 conversion.rank2_band_vec4_splitk— Splits the vec4 band's K axis into up to sixteen workgroup ranges. Each range writes an f32 partial band that a combine pass sums.band_vec4— Few-row variant for 2 to 16 rows withvec4loads. A rank-2A, or a rank-3/4Awhose batch folds into the rows, runs as one band against a rank-2B; equal-batch rank-3/rank-4 operands run one band per workgroup row, with K slices derived from the batch-times-column-group count.vector_matrix_vec4— One output row times a rank-2B: a rank-1A, a one-row rank-2A, or a batchedAwhose rows fold to one. Each lane owns four output columns.matrix_vector_subgroup— Assigns one output row to each workgroup, with vec4 loads for a vec4-aligned reduction width and scalar loads otherwise, then folds the row with a subgroup reduction, plus an in-order fold of subgroup sums when a subgroup is narrower than the row. It is the parallel matrix-vector variant on devices with subgroups.matrix_vector_nosg— Uses the same one-workgroup-per-row traversal but folds partials through workgroup memory. It applies when subgroups are unavailable.dot_splitk— Partitions a long vector dot product across workgroups and combines float32 partials to expose parallelism beyond one workgroup. The combine reassociates the sum, so results can differ in the last bits from the single-workgroupdotvariant.dot— Folds a vector dot product inside one workgroup: every lane strides the vectors, then a shared-memory tree fold sums the lane partials. Integer inputs accumulate in their own type; half inputs widen to float32.common_register_gemm— Register-blocked GEMM on 64x64 output tiles, the same kernel as pointwiseConv: packed operand tiles are staged in workgroup memory, accumulation is f32, and output stores are bounds-checked scalars.
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 casesconv-1x1-gemm-tiled-reg.wgsl.jinjadot-splitk-partial.wgsl.jinjamatmul-band-vec4.wgsl.jinjamatmul-batched-vector.wgsl.jinjamatmul-batched.wgsl.jinjamatmul-dot.wgsl.jinjamatmul-matrix-vector-subgroup.wgsl.jinjamatmul-native16-packed-register.wgsl.jinjamatmul-native16-register.wgsl.jinjamatmul-notrans-vec4-tiled-reg.wgsl.jinjamatmul-subgroup-matrix-ext.wgsl.jinjamatmul-tiled-general-reg.wgsl.jinjamatmul-tiled-general.wgsl.jinjamatmul-vector-matrix-deep.wgsl.jinjamatmul-vector-matrix-tail.wgsl.jinjamatmul-vector-matrix-vec4.wgsl.jinjamatmul-vector-product.wgsl.jinjamatmul.wgsl.jinjamatrix-shaped-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/ai.onnx.MatMul", { version: 1 });
const aData = Float32Array.of(
0.18795393, 0.18723874, 0.17931889, 0.16589965, 0.14892825, 0.13047631, 0.11261654,
0.09730019, 0.08624214, 0.08081959, 0.08198991, 0.09023206, 0.10551491, 0.12729393,
0.1545368,
);
const bData = Float32Array.of(
0.17091933, 0.16533019, 0.15063293, 0.1313149, 0.11202312, 0.097040445, 0.08980678,
0.092533216, 0.10594729, 0.12919523, 0.15991104, 0.19444641, 0.22823927, 0.25628528,
0.2736664, 0.2760843, 0.2603464, 0.2247556, 0.16936424, 0.09606541,
);
const { y } = await kernel({ a: { data: aData, shape: [3, 5] }, b: { data: bData, shape: [5, 4] } });
console.log({ y });
- Downloads last month
- -
Requires WebGPU support. See the compatibility table.