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 — Experimental 16x16x16 matrix primitive with f16 inputs, f32 accumulation and a 32x32 output tile. The workgroup verifies its 32- or 64-lane subgroup layout before choosing between native and portable f32 paths. Both paths handle M, N and K that are not tile multiples.
  • subgroup_matrix — Computes padded tiles with subgroup matrices and float32 accumulation. An N that is not a multiple of the 64-wide column tile clamps the trailing tile's B columns to N - 1 and skips every store at or past N. Ranked lower when rows fill little of each row tile and rank2_band_vec4_splitk applies, 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 of A stays small. A rank-3 A with 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 the f32 register tile's K reduction across dispatch.z into 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 for M >= 128, 32 for M >= 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, reading B rows 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 with vec4 loads. A rank-2 A, or a rank-3/4 A whose batch folds into the rows, runs as one band against a rank-2 B; 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-2 B: a rank-1 A, a one-row rank-2 A, or a batched A whose 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-workgroup dot variant.
  • 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 pointwise Conv: 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

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
-
kernel
webgpu
wgsl
apache-2.0
WebGPU

Requires WebGPU support. See the compatibility table.