com.microsoft.GatedRMSNorm

com.microsoft · ONNX Runtime contrib operator · contrib since_version 1

Description

Gated RMS normalization: Y = X * rsqrt(mean(X^2) + epsilon) * scale * gate_activation(gate). Activation defaults to silu (swish alias); sigmoid applies the sigmoid alone. Each contiguous group has C elements, equal to the number of scale values, so packed per-head inputs need no reshape. Accepts any input rank of at least one and counts rows by dividing the input element count by C. All intermediates, including gate activation, use float32; only the output store narrows. Bfloat16 is unsupported.

See the ONNX Runtime GatedRMSNorm contrib-operator spec for the reference semantics.

Inputs

Name Upstream name Logical dtype Rank Shape Description Presence
xT X T — — Input activations with shape (..., H * C). Any rank of at least 1 is accepted and the leading axes are folded into the row count. required
scaleT scale T — — Normalization weight. Its element count is the normalization span C; the standard spelling is (C), and the reshaped (1, C) and (C, 1) spellings of the same vector are accepted because upstream measures the span as an element count. required
gateT gate T — — Gate activations with exactly the same shape as X; the selected SiLU/swish or sigmoid activation is applied internally in float32. required

Outputs

Name Upstream name Logical dtype Rank Shape Description Presence
yT Y T same as xT same as xT Normalized, scaled and gated activations with the same shape and dtype as X. required

Attributes

Default values (overridable per request):

Attribute Default Description
activation "silu" Gate activation: silu (default) and its swish alias compute z * sigmoid(z); sigmoid computes only sigmoid(z). The activation stays in float32 for both float32 and float16 tensors, and sigmoid uses a stable logistic in both tails.
epsilon 0.00001 Constant added to the mean of squares before the reciprocal square root. The standard default is 1e-5; note that the Hugging Face gated-RMS archetype defaults to 1e-6 instead, so a value must be carried explicitly when porting between the two.

Type constraints

Variable Allowed dtypes
T 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.

  • subgroup_rows_vec4 — Proves complete native subgroup lane groups before using a subgroup row collective. Other legal partitions use the synchronized local-ID row tree, with the same f32 accumulation and store precision.
  • packed_rows_vec4 — vec4 variant that runs on any device: the workgroup is split into a power-of-two lane group per row and each group folds its own contiguous slice of the shared array, so a short row does not idle the workgroup and no subgroup support is required. It also carries rows too wide to stage in registers, which it walks twice.
  • subgroup_rows_scalar — Proves complete native subgroup lane groups before using a subgroup row collective. Other legal partitions use the synchronized local-ID row tree, with the same f32 accumulation and store precision.
  • packed_rows_scalar — Scalar fallback for a span that is not a multiple of four, and the variant every device can run. It keeps the lane-group-per-row schedule so a narrow span still fills the workgroup.
  • subgroup_wide_rows_vec4, subgroup_wide_rows_scalar — Proves full fixed-width subgroup populations before mapping rows through subgroup IDs and combining native row partials. Other legal populations use a local-ID row tree.

Device requirements

Some implementation variants require subgroups. Other variants run without it; 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/com.microsoft.GatedRMSNorm", { version: 1 });

const xTData = Float32Array.of(
  0.33235988, 0.43006405, 0.45976323, 0.4104722, 0.290668, 0.12620091, -0.04620629,
  -0.18821806, -0.26954928, -0.2755744, -0.2109471, -0.0983117,
);
const scaleTData = Float32Array.of(1.75);
const gateTData = Float32Array.of(
  -0.04278836, -0.34751478, -0.26068854, 0.30176142, 1.2277768, 2.2484548, 3.0381744,
  3.337231, 3.0499835, 2.2808857, 1.29459, 0.4154394,
);

const { yT } = await kernel({
  xT: { data: xTData, shape: [3, 4] },
  scaleT: { data: scaleTData, shape: [1] },
  gateT: { data: gateTData, shape: [3, 4] },
});
console.log({ yT });
Downloads last month
-
kernel
webgpu
wgsl
apache-2.0
WebGPU

Requires WebGPU support. See the compatibility table.