[ExecuTorch][WebGPU] Add optimized layer_norm op#20845
Open
JCNTH wants to merge 3 commits into
Open
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/20845
Note: Links to docs will display an error until the docs builds have been completed. ❌ 129 New Failures, 2 Unrelated Failures, 7 Unclassified FailuresAs of commit 257f66a with merge base c2b273e ( NEW FAILURES - The following jobs have failed:
UNCLASSIFIED FAILURES - DrCI could not classify the following jobs because the workflow did not run on the merge base. The failures may be pre-existing on trunk or introduced by this PR:
FLAKY - The following jobs failed but were likely due to flakiness present on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
This was referenced Jul 10, 2026
[ExecuTorch][WebGPU] Add relu op (shared unary handler; sigmoid adopts make_compute_pipeline)
#20863
Open
This was referenced Jul 10, 2026
This was referenced Jul 16, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
@exported-using-ghexport
Differential Revision: D110836681
Differential Revision: D110836681