com.microsoft.MatMulBnb4
com.microsoft · ONNX Runtime contrib operator · contrib since_version 1
Description
Computes A @ dequant(B)^T where B uses bitsandbytes 4-bit quantization: quant_type = 0 selects FP4 and quant_type = 1 selects NF4. Supports rank-2 float16/float32 A, transB = 1, and training_mode = 0; rank-1 and rank-3-or-higher A, bfloat16, transB = 0, and training are not implemented. B is the flattened [N, K] weight, two codes per byte with the even flat index in the high nibble. Each code indexes a fixed 16-entry codebook, and the value is codebook[code] * absmax[flat_index / block_size].
See the ONNX Runtime MatMulBnb4 contrib-operator spec for the reference semantics.
Inputs
| Name | Upstream name | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|---|
aT |
A |
T1 |
2 |
— | Float input matrix of shape (M, K), not quantized. |
required |
bT |
B |
T2 |
1 |
— | The [N, K] weight, flattened and quantized to 4 bits, stored as (N * K + 1) / 2 bytes; the ONNX type is uint8. The kernels read the uint8 data directly as 32-bit words, four bytes each, so no repacking is needed. |
required |
absmaxT |
absmax |
T1 |
1 |
— | Per-block absolute-maximum dequantization scales of shape ((N * K + block_size - 1) / block_size), same dtype as A. |
required |
Outputs
| Name | Upstream name | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|---|
yT |
Y |
T1 |
2 |
[aT[0], N] |
Result of A multiplied by the dequantized, transposed weight matrix, with shape (M, N) and the same dtype as A. |
required |
Attributes
Attributes and default values (overridable per request):
| Attribute | Default | Description |
|---|---|---|
K |
— | Input feature count (the shared dimension). |
N |
— | Output feature count. |
block_size |
— | Number of weights sharing one absmax scale; a power of two, at least 16. |
quant_type |
— | Codebook selector: 0 = FP4, 1 = NF4. |
training_mode |
0 |
Whether training outputs are requested. This inference-only implementation supports the standard default value 0. |
transB |
1 |
Whether the quantized weight is stored transposed. This implementation supports the standard default value 1. |
Type constraints
| Variable | Allowed dtypes |
|---|---|
T1 |
float32, float16 |
T2 |
uint8 |
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— Decodes 4-bit bitsandbytes weights into packed shared half operands and multiplies half activations by them with float32 accumulation, converting each output to half once.gemv_row_groups— Distributes small matrix products across independent column and row groups with cooperative f32 reduction along K.gemv_rows— Shares each dequantized weight across a small query-row block and cooperatively reduces f32 sums along K.sgmat_hybrid_rows— Combines full and partial row tiles in one matrix dispatch. Complete tiles read activations directly; partial tiles stage bounded rows in f32 and reuse weight staging for output. Float16 requests widen before the multiply and narrow once afterward.sgmat_widened— Runs the subgroup-matrix variant for a float16 request by widening the activations to float32 either side of the multiply, since the device's float16 configuration returns a float16 result. Full row tiles read activations directly; a partial last tile stages bounded rows and reuses weight staging for output.sgmat— Multiplies activations by dequantized weight tiles on subgroup matrices. Full float32 row tiles read activations directly; partial row counts stage activation rows in f32 and reuse dead weight staging for masked output stores.gemv_words— Few-row products for up to 8 rows: thirty-two lanes share an output column and eight columns a workgroup; each lane decodes four independent stored words per step into eight K-consecutive weights each and applies them to every row from registers, then a tree reduction closes each column.tiled_reg_splitk— Splits underfilled register tiles (16 to 128 rows by 64 columns, sized to the batch) along K: each slice decodes one stored word per lane into eight K-consecutive float32 weights of a lane-interleaved tile and stores float32 partials, which a combine pass sums in slice order.tiled_reg— Register tiles of 16 to 128 rows by 64 columns, sized to the batch: each lane decodes one stored word into eight K-consecutive float32 weights of a K-major, lane-interleaved tile, then accumulates its rows by four columns with chained FMAs in K order.
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 casescast-scalar-x4.wgsl.jinjamatmul-bnb4-gemv-words.wgsl.jinjamatmul-bnb4-gemv.wgsl.jinjamatmul-bnb4-native16-packed.wgsl.jinjamatmul-bnb4-sgmat.wgsl.jinjamatmul-bnb4-tiled-reg.wgsl.jinjamatmul-bnb4-tiled.wgsl.jinjamatmul-bnb4.wgsl.jinjareduce-axis0-splitk-combine.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.MatMulBnb4", { version: 1 });
const aTData = 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, 0.008510161,
-0.08814437, -0.18763463, -0.28310272, -0.3676744, -0.43505034, -0.48005563,
-0.49909735, -0.49048677, -0.4545969, -0.3938397, -0.31246522, -0.21620105,
-0.11176469, -0.0062940842, 0.09325158, 0.18066607, 0.2509931, 0.30090147, 0.32890537,
0.3354117, 0.32259256, 0.29409772, 0.25463665, 0.20947203, 0.16387495, 0.122594416,
0.08939305,
);
const bTData = Uint8Array.of(
1, 35, 69, 103, 137, 171, 205, 239, 254, 220, 186, 152, 118, 84, 50, 16, 90, 1, 35, 69,
103, 137, 171, 205, 239, 254, 220, 186, 152, 118, 84, 50, 16, 90, 1, 35,
);
const absmaxTData = Float32Array.of(0.07530118, 0.07724273, 0.07906845, 0.08075418, 0.08227701);
const { yT } = await kernel({
aT: { data: aTData, shape: [2, 24] },
bT: { data: bTData, shape: [36] },
absmaxT: { data: absmaxTData, shape: [5] },
}, {
attrs: {
K: 24,
N: 3,
block_size: 16,
quant_type: 1,
},
});
console.log({ yT });
- Downloads last month
- -
Requires WebGPU support. See the compatibility table.