Skip to content

[ExecuTorch][WebGPU] Add relu op (shared unary handler; sigmoid adopts make_compute_pipeline)#20863

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

[ExecuTorch][WebGPU] Add relu op (shared unary handler; sigmoid adopts make_compute_pipeline)#20863
JCNTH wants to merge 1 commit into
gh/JCNTH/38/basefrom
gh/JCNTH/38/head

Conversation

@JCNTH

@JCNTH JCNTH commented Jul 10, 2026

Copy link
Copy Markdown

Stack from ghstack (oldest at bottom):

Adds relu to the WebGPU backend via a shared elementwise-unary handler. ReLU is on the SAM2/SAM3 mask-decoder MLP path, so it is needed to delegate those decoders.

Problem — The backend had no aten.relu.default kernel, and sigmoid (the only prior unary op) built its compute pipeline inline rather than through the shared helper — duplicating the bind-group/dispatch boilerplate that a second unary op would repeat.

Solution

  • Before: sigmoid was implemented with a bespoke inline pipeline; there was no relu.
  • After: a single generic add_unary_op helper (in runtime/ops/sigmoid/UnaryOp.cpp) builds the input/output/params binding and dispatch for any elementwise-unary WGSL; sigmoid_impl and the new relu_impl are thin wrappers over it, so sigmoid now goes through the same utils::make_compute_pipeline path as relu. relu.wgsl is a one-element-per-thread output[idx] = max(input[idx], 0.0).

Implementation

  • add_unary_op(graph, in, out, wgsl_source, wg_size_x, op_name) centralizes: the fp32/4-byte-alignment and same-size guards, utils::clamp_workgroup_size + utils::compute_1d_workgroup_count for the 1D dispatch, the wg_size override constant, the uniform (num_elements) via utils::make_uniform, and the three-binding pipeline via utils::make_compute_pipeline.
  • Dynamic shapes are supported: a graph.add_tensor_resize_hook recomputes num_elements, rewrites the uniform via wgpuQueueWriteBuffer, and updates the dispatch's workgroup count for the live shape; the graph owns the uniform buffer so the hook can rewrite it.
  • Both ops self-register: aten.sigmoid.default -> sigmoid_impl and aten.relu.default -> relu_impl.
  • Mirrors Vulkan backends/vulkan/runtime/graph/ops/impl/UnaryOp.cpp (add_unary_op_node); Vulkan expresses relu as clamp(0, inf), whereas this kernel uses a direct max(x, 0.0).

Constraints — fp32 only (both operands 4-byte aligned); input and output must have identical byte size (same-shape elementwise); 1D dispatch only (throws past the 65535 workgroup cap).

Co-authored-with: Claude Code.

Differential Revision: D110836664

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

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

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

This was referenced Jul 10, 2026
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