--- library_name: kernels license: apache-2.0 tags: - kernel - webgpu - wgsl --- # ai.onnx.LayerNormalization `ai.onnx` · standard ONNX operator · ONNX opset ≥ 17 ## Description Normalizes a tensor along a suffix of axes starting at `axis` by subtracting the mean and dividing by the square root of the variance plus `epsilon`, then scales and optionally shifts the result with learnable `Scale` and `B` tensors. The output `Y` has the same shape as `X`; optional outputs `Mean` and `InvStdDev` expose the per-normalization-group statistics computed during normalization. See the [ONNX `LayerNormalization` spec](https://onnx.ai/onnx/operators/onnx__LayerNormalization.html) for the reference semantics. ## Inputs | Name | Bind key | Logical dtype | Rank | Shape | Description | Presence | | --- | --- | --- | --- | --- | --- | --- | | `X` | `x` | `T` | — | — | Tensor to be normalized. | required | | `Scale` | `scale` | `T` | — | — | Scale tensor applied after normalization. | required | | `B` | `b` | `T` | — | — | Optional bias tensor added after scaling. | optional | ## Outputs | Name | Bind key | Logical dtype | Rank | Shape | Description | Presence | | --- | --- | --- | --- | --- | --- | --- | | `Y` | `y` | `T` | same as `X` | same as `X` | Normalized and scaled output tensor; same shape as X. | required | | `Mean` | `mean` | `float32` | same as `X` | — | Per-normalization-group mean in the ONNX broadcastable keepdims shape: dimensions before `axis` are preserved and dimensions from `axis` onward are 1. | optional | | `InvStdDev` | `invStdDev` | `float32` | same as `X` | — | Per-normalization-group reciprocal standard deviation `1 / sqrt(variance + epsilon)`, returned in the same ONNX broadcastable keepdims shape as `Mean`. | optional | ## Attributes Default values (overridable per request): | Attribute | Default | Description | | --- | --- | --- | | `axis` | `-1` | The first axis of the normalization range; all axes from `axis` to the last are normalized together. Negative values count from the end; the default `-1` normalizes only the last dimension. | | `epsilon` | `0.00001` | Small constant added to the variance before taking the square root to avoid division by zero. | | `stash_type` | `1` | TensorProto element type used for the normalization stage and optional statistics; the implemented ONNX route supports the standard float32 value (`1`). | ## Type constraints | Variable | Allowed dtypes | | --- | --- | | `T` | `float32`, `float16` | ## Files - [`metadata.json`](build/webgpu/metadata.json) — kernel metadata (id, digests, provenance) - [`manifest.json`](build/webgpu/manifest.json) — the op contract (source of truth) - [`test.json`](build/webgpu/test.json) — correctness cases - [`bench.json`](build/webgpu/bench.json) — benchmark + tuning cases - [`layer-normalization.wgsl.jinja`](build/webgpu/layer-normalization.wgsl.jinja) - [`norm-row-stats.wgsl.jinja`](build/webgpu/norm-row-stats.wgsl.jinja) ## Use with `@huggingface/kernels` The loader derives every required output's shape and logical dtype from the manifest contract and this call. It then allocates the result tensors automatically. 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. ```js import { getKernel } from "@huggingface/kernels"; const kernel = await getKernel("webgpu-kernels/ai.onnx.LayerNormalization", { version: 1 }); const { y } = await kernel({ x: { data: xData, shape: [1, 4] }, scale: { data: scaleData, shape: [4] }, }); ```