Skip to content

[ExecuTorch][WebGPU] Add optimized gelu op#20843

Open
JCNTH wants to merge 3 commits into
gh/JCNTH/18/basefrom
gh/JCNTH/18/head
Open

[ExecuTorch][WebGPU] Add optimized gelu op#20843
JCNTH wants to merge 3 commits into
gh/JCNTH/18/basefrom
gh/JCNTH/18/head

Conversation

@JCNTH

@JCNTH JCNTH commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Adds the gelu activation to the WebGPU backend. GELU is the feed-forward (MLP) activation in the vision and VLM transformer blocks this backend targets — the Florence-2 DaViT encoder, BART, and the Whisper/Voxtral stacks — so it is on the critical path for delegating those models without graph breaks.

Problem — The WebGPU backend had no aten.gelu.default kernel, so any model whose FFN uses GELU could not be fully delegated. PyTorch's default is the exact (erf) formulation, and the target encoders use it; the tanh approximation must also be supported.

Solution

  • Before: aten.gelu.default was unsupported — no kernel, forcing a partition break around every GELU.
  • After: a single gelu.wgsl computes GELU on the GPU. The exact (erf) vs tanh choice is resolved by compiling the matching entry point (main_erf / main_tanh) into the pipeline, so each dispatch runs only its own formula — no per-invocation select() and no double-eval of both branches.

Implementation

  • 1D dispatch with one thread per 4 elements: a vec4 body + scalar-tail idiom loads up to 4 elements via select()-guarded scalar reads, computes GELU as one vec4<f32> op, and scatters back only the in-bounds lanes — so arbitrary FFN widths that are not a multiple of 4 are handled without a separate remainder pass.
  • The erf path uses the Abramowitz & Stegun 7.1.26 rational approximation (max abs err ~1.5e-7); the tanh path uses the clamped 0.5*x*(1+tanh(...)) form.
  • The approximate arg (args[1]: "none" selects erf, anything else selects tanh) picks the entry point at build time through the shared utils::make_compute_pipeline; workgroup size comes from utils::clamp_workgroup_size, the dispatch count from utils::compute_1d_workgroup_count, and the uniform from utils::make_uniform.
  • Mirrors Vulkan backends/vulkan/runtime/graph/ops/impl/UnaryOp.cpp (its gelu handler; args[1] is the approximate string). Note the WebGPU kernel additionally implements the exact/erf path, which the Vulkan handler does not currently support.

Constraints — fp32 only (both operands must be 4-byte aligned, enforced in the handler); input and output must have identical byte size; 1D dispatch only (throws if the workgroup count would exceed the 65535 cap). Element count need not be a multiple of 4 (scalar tail); static shape (no resize hook).

Co-authored-with: Claude Code.
@exported-using-ghexport

Differential Revision: D110836674

Differential Revision: D110836674

[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/20843

Note: Links to docs will display an error until the docs builds have been completed.

❌ 57 New Failures, 2 Unrelated Failures, 7 Unclassified Failures

As of commit 85c5a81 with merge base c2b273e (image):

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:

  • pull / unittest / windows / windows-job (gh) (matched win rule in flaky-rules.json)
    Can't find 'action.yml', 'action.yaml' or 'Dockerfile' under 'C:\actions-runner\_work\executorch\executorch\test-infra\.github\actions\teardown-windows'. Did you forget to run actions/checkout before running your local action?
  • pull / unittest-editable / windows / windows-job (gh) (matched win rule in flaky-rules.json)
    Can't find 'action.yml', 'action.yaml' or 'Dockerfile' under 'C:\actions-runner\_work\executorch\executorch\test-infra\.github\actions\teardown-windows'. Did you forget to run actions/checkout before running your local action?

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.

[ghstack-poisoned]
[ghstack-poisoned]
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. meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants