| --- |
| library_name: kernels |
| license: apache-2.0 |
| tags: |
| - kernel |
| - webgpu |
| - wgsl |
| --- |
| # ai.onnx.SimplifiedLayerNormalization |
|
|
| `ai.onnx` · ONNX Runtime compatibility operator · default-domain since_version 1 |
| |
| ## Description |
| |
| Implements ONNX Runtime's legacy RMS normalization for models that serialize `SimplifiedLayerNormalization` in the default ONNX domain: `Y = (X / sqrt(mean(X^2) + epsilon)) * scale`. It shares kernels with [ONNX `RMSNormalization`](https://onnx.ai/onnx/operators/onnx__RMSNormalization.html), but retains scale-before-output-cast ordering and optional `inv_std_var`. See the [compatibility discussion](https://github.com/onnx/onnx/issues/6582#issuecomment-3591862327). Supports float16/float32, float32 statistics, and a nonempty normalization suffix. |
| |
| ## Inputs |
| |
| | Name | Bind key | Logical dtype | Rank | Shape | Description | Presence | |
| | --- | --- | --- | --- | --- | --- | --- | |
| | `X` | `x` | `T` | — | — | Input tensor; the mean square is taken over the suffix dimensions starting at `axis`. | required | |
| | `scale` | `scale` | `V` | — | — | Scale tensor, unidirectionally broadcastable to `X`; output `Y` has this tensor's dtype. | required | |
| |
| ## Outputs |
| |
| | Name | Bind key | Logical dtype | Rank | Shape | Description | Presence | |
| | --- | --- | --- | --- | --- | --- | --- | |
| | `Y` | `y` | `V` | same as `X` | same as `X` | Normalized and scaled output; same shape as `X` and same dtype as `scale`. | required | |
| | `inv_std_var` | `invStdVar` | `U` | same as `X` | derived; see description | Optional inverse RMS statistic `1 / sqrt(mean(X^2) + epsilon)`; same rank as `X`, with the dimensions from `axis` onward set to one. | optional | |
| |
| ## Attributes |
| |
| Default values (overridable per request): |
| |
| | Attribute | Default | Description | |
| | --- | --- | --- | |
| | `axis` | `-1` | The first dimension of the normalization suffix; negative values count from the end, so the default `-1` normalizes only the last dimension. | |
| | `epsilon` | `0.00001` | Small constant added to the mean square before taking the square root to avoid division by zero. | |
| | `stash_type` | `1` | TensorProto element type of `inv_std_var`; this package implements the legacy float32 value `1`. | |
| | `keep_dims` | `1` | Legacy compatibility attribute accepted and ignored by ONNX Runtime. The supported value `1` records the runtime's fixed behavior: optional statistics retain the input rank with normalized dimensions set to one. | |
|
|
| ## Type constraints |
|
|
| | Variable | Allowed dtypes | |
| | --- | --- | |
| | `T` | `float32`, `float16` | |
| | `V` | `float32`, `float16` | |
| | `U` | `float32` | |
|
|
| ## 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 |
| - [`norm-row-stats.wgsl.jinja`](build/webgpu/norm-row-stats.wgsl.jinja) |
| - [`rms-normalization-splitk-normalize.wgsl.jinja`](build/webgpu/rms-normalization-splitk-normalize.wgsl.jinja) |
| - [`rms-normalization-splitk-partials.wgsl.jinja`](build/webgpu/rms-normalization-splitk-partials.wgsl.jinja) |
| - [`rms-normalization.wgsl.jinja`](build/webgpu/rms-normalization.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.SimplifiedLayerNormalization", { version: 1 }); |
| const { y } = await kernel({ x: { data: xData, shape: [8] }, scale: { data: scaleData, shape: [8] } }); |
| ``` |
|
|