com.microsoft.SkipGroupNorm
com.microsoft · ONNX Runtime contrib operator · contrib since_version 1
Description
Adds a residual and an optional bias to the input, then group-normalizes the sum with an optional fused SiLU: S = X + skip + bias, Y = gamma * (S - mean) / sqrt(variance + epsilon) + beta. Statistics are taken per batch item over each group of C / groups channels and every spatial position, under either layout. skip is either exactly X's shape or a per-channel (N, C) / (N, 1, 1, C) residual. The sum is rounded to the tensor type before the statistics see it, so Y does not depend on whether S was requested. X and gamma/beta carry independent element types.
See the ONNX Runtime SkipGroupNorm contrib-operator spec for the reference semantics.
Inputs
| Name | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|
X |
T |
4 |
— | Input image tensor, (N, H, W, C) when channels_last is 1 and (N, C, H, W) otherwise. |
required |
gamma |
M |
1 |
— | Per-channel affine scale of shape (C). |
required |
beta |
M |
1 |
— | Per-channel affine offset of shape (C). |
required |
skip |
T |
— | — | Residual added to X. Either exactly X's shape, or (N, C) / (N, 1, 1, C) to broadcast one value per batch item and channel. A (N, 1, 1, C) tensor at H = W = 1 is an exact match and takes the non-broadcast path, which is what upstream does. |
required |
bias |
T |
1 |
— | Optional per-channel bias of shape (C) added to X + skip before normalization. |
optional |
Outputs
| Name | Logical dtype | Rank | Shape | Description | Presence |
|---|---|---|---|---|---|
Y |
T |
4 |
same as X |
Normalized tensor with the same shape and element type as X. |
required |
S |
T |
4 |
same as X |
Optional second output holding X + skip + bias at the tensor's element type, the values the statistics are taken over. |
optional |
Attributes
Attributes and default values (overridable per request):
| Attribute | Default | Description |
|---|---|---|
activation |
— | Activation applied after the affine transform: 0 for none, 1 for SiLU. The request must supply it; the operator declares no default. |
channels_last |
1 |
1 when X and Y are (N, H, W, C), 0 when they are (N, C, H, W). Defaults to 1. Any non-zero value selects the channels-last layout. |
epsilon |
0.00001 |
Value added to the group variance before the reciprocal square root. Defaults to 1e-5. |
groups |
— | Number of channel groups the statistics are taken over. It must divide C. The request must supply it; the operator declares no default. |
Type constraints
| Variable | Allowed dtypes |
|---|---|
T |
float32, float16 |
M |
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.
split— Three dispatches: partial group statistics, combine mean and reciprocal standard deviation, then normalize and apply affine weights. Channels-last statistics use four, two, or one aligned values per lane; group blocks and spatial rows follow device limits. Fuses skip, optional bias, and optional residual output.fused— One workgroup per batch item and group, walking the group twice inside a single dispatch: once to form the residual sum (with the optional bias) and its moments, once to write the affine result and the optional residual sum output.
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 casesgroup-norm-combine.wgsl.jinjagroup-norm-fused.wgsl.jinjagroup-norm-slab.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/com.microsoft.SkipGroupNorm", { version: 1 });
const XData = Float32Array.of(
0.13557504, 0.11928559, 0.09828554, 0.08258986, 0.080196865, 0.09547822, 0.12824495,
0.17365529, 0.22298619, 0.26514226, 0.28864858, 0.2837891, 0.24452375, 0.16984999,
0.064364746, -0.06208784, -0.19564575, -0.32035294, -0.4204786, -0.48286447,
-0.4989248, -0.46597713, -0.387688, -0.27355963, -0.13753796, 0.0040372224, 0.1348138,
0.2407631, 0.3121024, 0.34450692, 0.33943146, 0.3035099, 0.2471504, 0.18257575,
0.12164538, 0.07382891, 0.044669542, 0.034990773, 0.04097107, 0.05506309, 0.067590006,
0.06873614, 0.050581627, 0.008822249, -0.05613185, -0.13888124, -0.22964951,
-0.31569627, -0.38330764, -0.42004034, -0.41684964, -0.36974403, -0.28068507,
-0.15757012, -0.013284875, 0.13603647, 0.27327567, 0.3827659, 0.45251575, 0.47587255,
0.4523666, 0.3876098, 0.29227516, 0.1803308, 0.06682128, -0.034441292, -0.112903185,
-0.16269025, -0.183206, -0.17882729, -0.15776913, -0.13032858, -0.106819496,
-0.09555917, -0.10125696, -0.12408387, -0.1595855, -0.19945575, -0.23304105,
-0.2493194, -0.23901464, -0.19648045, -0.121023655, -0.017426923, 0.10443713,
0.23085797, 0.346218, 0.43530023, 0.48560485, 0.48930198, 0.44450256, 0.35563886,
0.23288737, 0.09072184, -0.054176625, -0.18532194,
);
const gammaData = Float32Array.of(
1.4528207, 1.5647314, 1.6488526, 1.6965575, 1.7019613, 1.6624434, 1.5789037, 1.4557278,
);
const betaData = Float32Array.of(
0.29333884, 0.27272132, 0.24107324, 0.2017075, 0.15831904, 0.114782326, 0.07493955,
0.042390633,
);
const skipData = Float32Array.of(
0.2777067, 0.3479784, 0.3856279, 0.38270074, 0.33631626, 0.24917531, 0.12932365,
-0.010802323, -0.15592237, -0.289848, -0.3975492, -0.4670772, -0.49108717, -0.46775794,
-0.40098697, -0.29983643,
);
const { Y } = await kernel({
X: { data: XData, shape: [2, 3, 2, 8] },
gamma: { data: gammaData, shape: [8] },
beta: { data: betaData, shape: [8] },
skip: { data: skipData, shape: [2, 8] },
}, {
attrs: { groups: 2, activation: 0 },
});
console.log({ Y });
- Downloads last month
- -
Requires WebGPU support. See the compatibility table.