ai.onnx.BatchNormalization
ai.onnx · standard ONNX operator · ONNX opset ≥ 15
Description
Applies inference-mode batch normalization: Y = (X - input_mean) / sqrt(input_var + epsilon) * scale + B. This package supports training_mode=0, rank-2-or-higher inputs, and a common float16 or float32 dtype for every tensor. ONNX training mode is intentionally not implemented because this inference-only release does not expose its required running-mean and running-variance outputs.
See the ONNX BatchNormalization spec for the reference semantics.
Differences from the ONNX spec
- Output
running_mean: The inference-only package does not expose the optional training-state mean. - Output
running_var: The inference-only package does not expose the optional training-state variance. - Type
T: The inference kernels use one common dtype for data, affine parameters, and statistics.
Inputs
| Name | Upstream name | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|---|
x |
X |
T |
— | — | Input data tensor with shape (N, C, D1, ..., Dn), normalized independently per channel using the supplied estimated statistics. |
required |
scale |
— | T |
1 |
— | Per-channel scale tensor with shape (C). |
required |
b |
B |
T |
1 |
— | Per-channel bias tensor with shape (C). |
required |
input_mean |
— | T |
1 |
— | Precomputed estimated mean tensor with shape (C) used for inference. |
required |
input_var |
— | T |
1 |
— | Precomputed estimated variance tensor with shape (C) used for inference. |
required |
Outputs
| Name | Upstream name | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|---|
y |
Y |
T |
same as x |
same as x |
Batch-normalized output tensor with the same shape as X. |
required |
Attributes
Default values (overridable per request):
| Attribute | Default | Description |
|---|---|---|
epsilon |
0.00001 |
Small value added to the variance before taking the square root to avoid division by zero. |
momentum |
0.9 |
Standard ONNX running-statistics momentum. This inference-only package accepts the default 0.9; non-default values are reserved for the unsupported training-state update. |
training_mode |
0 |
Execution mode. This inference-only package supports the default value 0; value 1 is rejected because the ONNX training outputs are not exposed. |
Type constraints
| Variable | Allowed dtypes |
|---|---|
T |
float32, float16 |
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 casesbatch-normalization-nc-vec4.wgsl.jinjabatch-normalization-nchw-flat-vec4.wgsl.jinjabatch-normalization-nchw-vec4.wgsl.jinjabatch-normalization-nchw.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/ai.onnx.BatchNormalization", { version: 1 });
const xData = Float32Array.of(1, 2, 3, 4, 5, 6);
const scaleData = Float32Array.of(1, 0.5, 2);
const bData = Float32Array.of(0, 1, -1);
const input_meanData = Float32Array.of(2.5, 3.5, 4.5);
const input_varData = Float32Array.of(2.25, 2.25, 2.25);
const { y } = await kernel({
x: { data: xData, shape: [2, 3] },
scale: { data: scaleData, shape: [3] },
b: { data: bData, shape: [3] },
input_mean: { data: input_meanData, shape: [3] },
input_var: { data: input_varData, shape: [3] },
});
console.log({ y });
- Downloads last month
- -
Requires WebGPU support. See the compatibility table.