You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adds native_layer_norm to the WebGPU backend. LayerNorm is pervasive in the BART decoder and the Florence-2 DaViT vision encoder (and the Whisper/Voxtral hidden dims 768/1024/1280/3072), so it is required to delegate those transformer stacks end-to-end.
Problem — The backend had no aten.native_layer_norm.default kernel. LayerNorm also needs a numerically robust mean/variance (a naive E[x^2]-E[x]^2 cancels badly on large-mean/small-variance activations), and some graphs (the group_norm LN-reframe) pass None for weight and bias, so the affine step must be optional.
Solution
Before: aten.native_layer_norm.default was unsupported.
After: native_layer_norm.wgsl computes per-row mean, rstd, and the normalized (optionally affine) output in a single kernel launch — one workgroup per row — writing all three outputs (out, mean, rstd) of the op's ValueList.
Implementation
Single-pass, numerically robust mean+variance via Chan et al.'s parallel Welford: each of the 64 threads folds its strided vec4<f32> slice of the row into a running (n, mean, M2), then a shared-memory tree reduction pairwise-merges the per-thread triples — no E[x^2]-E[x]^2 cancellation.
t_in/t_out/t_weight/t_bias are viewed as array<vec4<f32>> over the row width for wide loads/stores; the second pass re-streams the row applying (x-mean)*rstd and the affine only when has_affine == 1.
Rows past the 65535 per-dimension ceiling are handled by a near-square 2D workgroup grid: utils::compute_row_dispatch_grid computes count_x/count_y and a stride_x override constant, and the shader decodes the flat row index as wid.y * stride_x + wid.x.
Weight/bias are optional: when either is None the handler binds dummy storage via utils::make_optional_binding and sets has_affine == 0; the pipeline is built with the shared utils::make_compute_pipeline, epsilon read via utils::scalar_or.
Mirrors Vulkan backends/vulkan/runtime/graph/ops/impl/NativeLayerNorm.cpp (same arg layout and [out, mean, rstd] ValueList; the WebGPU kernel additionally supports the no-affine path, which the Vulkan handler rejects).
Constraints — fp32 only (byte-size must equal numel * sizeof(float)); normalizes over the last dim only; the last dim (row_width) must be a multiple of 4 for the vec4 view (all in-scope hidden dims are); non-empty input; 2D dispatch grid for the row count.
meta-claBot
added
the
CLA Signed
This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
label
Jul 10, 2026
If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.
To add a label, you can comment to pytorchbot, for example @pytorchbot label "release notes: none"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CLA SignedThis label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
2 participants
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack from ghstack (oldest at bottom):
Adds
native_layer_normto the WebGPU backend. LayerNorm is pervasive in the BART decoder and the Florence-2 DaViT vision encoder (and the Whisper/Voxtral hidden dims 768/1024/1280/3072), so it is required to delegate those transformer stacks end-to-end.Problem — The backend had no
aten.native_layer_norm.defaultkernel. LayerNorm also needs a numerically robust mean/variance (a naiveE[x^2]-E[x]^2cancels badly on large-mean/small-variance activations), and some graphs (the group_norm LN-reframe) passNonefor weight and bias, so the affine step must be optional.Solution
aten.native_layer_norm.defaultwas unsupported.native_layer_norm.wgslcomputes per-row mean, rstd, and the normalized (optionally affine) output in a single kernel launch — one workgroup per row — writing all three outputs (out,mean,rstd) of the op's ValueList.Implementation
vec4<f32>slice of the row into a running(n, mean, M2), then a shared-memory tree reduction pairwise-merges the per-thread triples — noE[x^2]-E[x]^2cancellation.t_in/t_out/t_weight/t_biasare viewed asarray<vec4<f32>>over the row width for wide loads/stores; the second pass re-streams the row applying(x-mean)*rstdand the affine only whenhas_affine == 1.utils::compute_row_dispatch_gridcomputescount_x/count_yand astride_xoverride constant, and the shader decodes the flat row index aswid.y * stride_x + wid.x.Nonethe handler binds dummy storage viautils::make_optional_bindingand setshas_affine == 0; the pipeline is built with the sharedutils::make_compute_pipeline,epsilonread viautils::scalar_or.backends/vulkan/runtime/graph/ops/impl/NativeLayerNorm.cpp(same arg layout and[out, mean, rstd]ValueList; the WebGPU kernel additionally supports the no-affine path, which the Vulkan handler rejects).Constraints — fp32 only (byte-size must equal
numel * sizeof(float)); normalizes over the last dim only; the last dim (row_width) must be a multiple of 4 for the vec4 view (all in-scope hidden dims are); non-empty input; 2D dispatch grid for the row count.Co-authored-with: Claude Code.
Differential Revision: D110836681