ai.onnx.QuantizeLinear

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

Description

Linearly quantizes a high-precision tensor to a lower-precision integer type using the formula y = saturate((x / y_scale) + y_zero_point), with rounding to nearest even. Supports per-tensor, per-axis, and blocked quantization granularities determined by the shape of y_scale.

See the ONNX QuantizeLinear spec for the reference semantics.

Inputs

Name Bind key Logical dtype Rank Shape Description Presence
x x TX N-D full-precision input tensor to be quantized. required
y_scale y_scale TS Scale factor; scalar for per-tensor, 1-D for per-axis, or same rank as x (with one axis blocked) for blocked quantization. required
y_zero_point y_zero_point TQ Zero point for quantization; must have the same shape as y_scale. Defaults to zero if omitted. optional

Outputs

Name Bind key Logical dtype Rank Shape Description Presence
y y TQ same as x same as x N-D quantized output tensor with the same shape as x. required

Attributes

Default values (overridable per request):

Attribute Default Description
axis 1 Axis of the quantization dimension in x, used for per-axis and blocked quantization; negative values count from the end.
block_size 0 Number of elements along axis that share a single scale value for blocked quantization; 0 means blocked quantization is not used.
output_dtype 0 ONNX TensorProto element-type code for y; 0 infers the type from y_zero_point, or uint8 when the zero point is omitted.
precision 0 ONNX TensorProto element-type code used for x / y_scale; 0 uses the dtype of y_scale, 1 selects FLOAT, and 10 selects FLOAT16.
saturate 1 Controls out-of-range conversion for float8 outputs. The implemented int8/uint8 subset accepts the ONNX default 1.

Type constraints

Variable Allowed dtypes
TX float32, float16
TS float32, float16
TQ uint8, int8

Files

Use with @huggingface/kernels

The loader automatically allocates outputs whose metadata it can derive from the manifest contract and this call.

The explicit outputs entries provide shape and logical dtype metadata for the results listed below:

  • y

Each entry either requests an optional result or supplies metadata that cannot be inferred from the inputs.

The version: 1 option selects the published kernel contract; it is independent of any operator opset, contrib since_version, or model version.

Replace each *Data placeholder with a typed array containing the corresponding input data.

import { getKernel } from "@huggingface/kernels";

const kernel = await getKernel("webgpu-kernels/ai.onnx.QuantizeLinear", { version: 1 });
// Explicit destinations request optional results or supply metadata that cannot be inferred.
const { y } = await kernel({ x: { data: xData, shape: [] }, y_scale: { data: y_scaleData, shape: [] } }, {
  outputs: { y: { shape: [], dtype: "uint8" } },
});
Downloads last month
-
kernel
webgpu
wgsl
apache-2.0
WebGPU

Requires WebGPU support. See the compatibility table.