ai.onnx.QLinearMatMul

ai.onnx · standard ONNX operator · ONNX opset ≥ 21

Description

Quantized matrix multiplication of tensors a and b with supported ONNX MatMul vector and batch-broadcasting shapes. Each operand and the output carry their own scale and zero point; the result is rounded to nearest even and saturated to the output type. This package requires single-element rank-1 quantization tensors for a and y. For b, it supports single-element rank-1 tensors and selected per-column layouts; per-row quantization of a is unsupported.

See the ONNX QLinearMatMul spec for the reference semantics.

Inputs

Name Logical dtype Rank Shape Description Presence
a TA — — N-dimensional quantized matrix a. required
a_scale TF 1 — Per-tensor scale of quantized input a, supplied as a single-element rank-1 tensor. required
a_zero_point TA 1 — Zero point of quantized input a; must have the same shape as a_scale. required
b TB — — N-dimensional quantized matrix b. required
b_scale TF — — Scale of quantized input b: a single-element rank-1 tensor for per-tensor quantization, a length-N vector for a rank-2 matrix, or [batch, 1, N] for a rank-3 matrix with per-column quantization. required
b_zero_point TB — — Zero point of quantized input b: a single-element rank-1 tensor, a length-N vector or [1, N] for a rank-2 matrix, or [batch, 1, N] for a rank-3 matrix. Rank-2 per-column scales and zero points may independently use their per-tensor forms. required
y_scale TF 1 — Per-tensor scale of quantized output y, supplied as a single-element rank-1 tensor. required
y_zero_point TY 1 — Per-tensor zero point of quantized output y, supplied as a single-element rank-1 tensor. required

Outputs

Name Logical dtype Rank Shape Description Presence
y TY derived ONNX MatMul result of a and b Quantized matrix multiply result of a * b. required

Type constraints

Variable Allowed dtypes
TA uint8, int8
TB uint8, int8
TY uint8, int8
TF 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.

  • sgmat_precast_f16 — A first pass converts each byte minus its zero point to an exact f16 value once; the subgroup matrices then load these directly, accumulating in f32. Row groups follow matrix geometry, output rows, invocation limits and workgroup storage; bounded partial sums retain exact modular int32 arithmetic.
  • portable_precast_f16 — A first pass converts each byte minus its zero point to an exact f16 value; a vector workgroup GEMM then runs with exact bounded f32 partials and modular int32 totals. Geometry follows output rows, invocation and storage limits; f32 storage requires enough row reuse to amortize preparation.
  • sgmat_precast — A first pass converts each byte minus its zero point to an exact f32 value once; the subgroup matrices then load these directly, accumulating in f32. Row groups follow matrix geometry, output rows, invocation limits and workgroup storage; bounded partial sums retain exact modular int32 arithmetic.
  • portable_precast — A first pass converts each byte minus its zero point to an exact f32 value; a vector workgroup GEMM then runs with exact bounded f32 partials and modular int32 totals. Geometry follows output rows, invocation and storage limits; f32 storage requires enough row reuse to amortize preparation.
  • sgmat_exact — Exact integer products through bounded f32 matrix partials, fused with scalar or per-column requantization. Tile height follows workgroup memory and invocation limits.
  • portable_accurate_dp4a_scalar_quantization, portable_accurate_dp4a_rank2_per_column_quantization, portable_accurate_dp4a_rank2_per_column_scale_scalar_zp_tail, portable_accurate_dp4a_rank2_scalar_scale_per_column_zp_tail — Uses tiled integer accumulation and accurate requantization when a fused path is unavailable, including devices whose storage-binding limit cannot accommodate it. Native packed dot products are used when available.
  • sgmat_integer_16x16x16 — Experimental 16x16x16 raw-byte matrix multiplication with exact int32 accumulation and scalar or per-column B scales and zero points. Requantization rounds ties to even. Unsupported subgroup layouts use scalar accumulation.
  • sgmat_integer_16x16x32 — Experimental 16x16x32 raw-byte matrix multiplication with exact int32 accumulation and scalar or per-column B scales and zero points. Requantization rounds ties to even. Unsupported subgroup layouts use scalar accumulation.
  • sgmat_integer_16x8x32 — Experimental 16x8x32 raw-byte matrix multiplication with exact int32 accumulation and scalar or per-column B scales and zero points. Requantization rounds ties to even. Unsupported subgroup layouts use scalar accumulation.
  • sgmat_tiled_i8 — Byte-packed 8-bit operands feed the general subgroup-matrix GEMM, whose exact zero-point-corrected int32 sums are written to a temporary buffer for the requantization pass.

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.QLinearMatMul", { version: 1 });

const aData = Uint8Array.of(1);
const a_scaleData = Float32Array.of(1);
const a_zero_pointData = new Uint8Array(1);
const bData = Int8Array.of(1, -1);
const b_scaleData = Float32Array.of(1);
const b_zero_pointData = new Int8Array(1);
const y_scaleData = Float32Array.of(1e-20);
const y_zero_pointData = Uint8Array.of(128);

const { y } = await kernel({
  a: { data: aData, shape: [1, 1] },
  a_scale: { data: a_scaleData, shape: [1] },
  a_zero_point: { data: a_zero_pointData, shape: [1] },
  b: { data: bData, shape: [1, 2] },
  b_scale: { data: b_scaleData, shape: [1] },
  b_zero_point: { data: b_zero_pointData, shape: [1] },
  y_scale: { data: y_scaleData, shape: [1] },
  y_zero_point: { data: y_zero_pointData, shape: [1] },
});
console.log({ y });
Downloads last month
-
kernel
webgpu
wgsl
apache-2.0
WebGPU

Requires WebGPU support. See the compatibility table.