File size: 9,306 Bytes
fb9dcfe 746b1e6 fb9dcfe 746b1e6 fb9dcfe 746b1e6 bb8ee13 746b1e6 bb8ee13 746b1e6 bb8ee13 aa658d4 d8aeb25 bb8ee13 aa658d4 bb8ee13 aa658d4 bb8ee13 aa658d4 bb8ee13 746b1e6 aa658d4 746b1e6 aa658d4 746b1e6 bb8ee13 aa658d4 bb8ee13 aa658d4 bb8ee13 746b1e6 bb8ee13 746b1e6 aa658d4 746b1e6 aa658d4 746b1e6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | ---
library_name: kernels
license: apache-2.0
tags:
- kernel
- webgpu
- wgsl
---
# com.microsoft.FusedMatMul
`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. Supports optional transposition of either operand's last two dimensions, optional batch-dimension transposition, and a scalar `alpha` multiplier. Float32 and float16 are supported; double and bfloat16 are not.
See the [ONNX Runtime `FusedMatMul` contrib-operator spec](https://github.com/microsoft/onnxruntime/blob/main/docs/ContribOperators.md#com.microsoft.FusedMatMul) 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. |
| `transBatchA` | `0` | When non-zero, transposes `A` on its first dimension and batch dimensions (dim-1 to dim-rank-2) before multiplication. |
| `transBatchB` | `0` | When non-zero, transposes `B` on its first dimension and batch dimensions (dim-1 to dim-rank-2) 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-blocked `Y = alpha * A @ B` where 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.
- `subgroup_matrix_transbatch_b` — Aligned rank-3 transBatchB product with sufficient reduction depth and output tiles to amortize subgroup-matrix staging.
- `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_preferred` is 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-matrix `Y = 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.
- `transbatch_b_tiled_reg` — Register-blocked logical rank3 product with an interleaved physical B batch axis.
- `subgroup_matrix_16x16x16_plain` — Experimental `16x16x16` half-precision subgroup matrices with `f32` accumulation. The `alpha` scale is applied in `f32` before 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`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedMatMul/blob/v1/build/webgpu/metadata.json) — kernel metadata (id, file digests, manifest spec, provenance)
- [`manifest.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedMatMul/blob/v1/build/webgpu/manifest.json) — the op contract (source of truth)
- [`test.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedMatMul/blob/v1/build/webgpu/test.json) — correctness cases
- [`bench.json`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedMatMul/blob/v1/build/webgpu/bench.json) — benchmark cases
- [`contrib-native16-packed.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedMatMul/blob/v1/build/webgpu/contrib-native16-packed.wgsl.jinja)
- [`fused-matmul-subgroup-matrix.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedMatMul/blob/v1/build/webgpu/fused-matmul-subgroup-matrix.wgsl.jinja)
- [`matmul-band-vec4.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedMatMul/blob/v1/build/webgpu/matmul-band-vec4.wgsl.jinja)
- [`matmul-subgroup-matrix-ext.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedMatMul/blob/v1/build/webgpu/matmul-subgroup-matrix-ext.wgsl.jinja)
- [`matmul-tiled-general-reg.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedMatMul/blob/v1/build/webgpu/matmul-tiled-general-reg.wgsl.jinja)
- [`matmul-tiled-general.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedMatMul/blob/v1/build/webgpu/matmul-tiled-general.wgsl.jinja)
- [`matmul-vector-matrix-vec4.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedMatMul/blob/v1/build/webgpu/matmul-vector-matrix-vec4.wgsl.jinja)
- [`matrix-shaped-projection-gemm.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedMatMul/blob/v1/build/webgpu/matrix-shaped-projection-gemm.wgsl.jinja)
- [`reduce-axis0-splitk-combine.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedMatMul/blob/v1/build/webgpu/reduce-axis0-splitk-combine.wgsl.jinja)
- [`subgroup-matrix-gemm.wgsl.jinja`](https://huggingface.co/kernels/webgpu-kernels/com.microsoft.FusedMatMul/blob/v1/build/webgpu/subgroup-matrix-gemm.wgsl.jinja)
## Use with `@huggingface/kernels`
```sh
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`.
```js
import { getKernel } from "@huggingface/kernels";
const kernel = await getKernel("webgpu-kernels/com.microsoft.FusedMatMul", { 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 });
```
|