Skip to content

[ExecuTorch][WebGPU] Add optimized layer_norm op#20845

Open
JCNTH wants to merge 1 commit into
gh/JCNTH/20/basefrom
gh/JCNTH/20/head
Open

[ExecuTorch][WebGPU] Add optimized layer_norm op#20845
JCNTH wants to merge 1 commit into
gh/JCNTH/20/basefrom
gh/JCNTH/20/head

Conversation

@JCNTH

@JCNTH JCNTH commented Jul 10, 2026

Copy link
Copy Markdown

Stack from ghstack (oldest at bottom):

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.

Co-authored-with: Claude Code.

Differential Revision: D110836681

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jul 10, 2026

Copy link
Copy Markdown

🔗 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.

✅ You can merge normally! (1 Unrelated Failure)

As of commit 8d54200 with merge base aceeb40 (image):

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot 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
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

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"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants