Skip to content

[ExecuTorch][WebGPU] Add embedding op#20853

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

[ExecuTorch][WebGPU] Add embedding op#20853
JCNTH wants to merge 3 commits into
gh/JCNTH/28/basefrom
gh/JCNTH/28/head

Conversation

@JCNTH

@JCNTH JCNTH commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

fp32 aten.embedding.default now runs on the WebGPU backend as a row-gather, so the BART token and positional embedding lookups in Florence-2 execute on-device. This was the remaining attention-stack op that AOT-delegated but had no WebGPU runtime kernel.

Problem — The WebGPU backend had no aten.embedding.default kernel. The op partitions into the delegate at export, but with no registered handler the graph failed at runtime load, blocking the BART embedding lookup.

Solution — Before: aten.embedding.default had no runtime kernel (load-time failure). After: the handler binds the fp32 weight, the int32 indices, and the output, then dispatches a plain gather computing out[row, col] = weight[indices[row], col]. Forward embedding is exactly this row gather; the padding_idx/scale_grad_by_freq/sparse args only affect the backward pass and are ignored.

Implementationembedding.wgsl runs one thread per output element: from the linear global_invocation_id, row = idx / embed_dim and col = idx % embed_dim, then token = u32(t_indices[row]) and t_out[idx] = t_weight[token*embed_dim + col], with an early idx >= num_elements guard. Dispatch is 1D: the workgroup size is clamped to the device limit via utils::clamp_workgroup_size and the count computed by the shared utils::compute_1d_workgroup_count (which throws if the count exceeds the device's per-dimension limit), passed to the shader through a wg_size override constant. Uses the shared utils::make_compute_pipeline and utils::make_uniform (16-byte EmbeddingParams {embed_dim, num_elements}). Because the backend carries no runtime dtype, per-type byte guards enforce int32 indices and fp32 weight/out. Mirrors Vulkan backends/vulkan/runtime/graph/ops/impl/Embedding.cpp (add_embedding_node: a plain buffer row-gather registered for aten.embedding.default).

Constraints — fp32 weight/out, int32 indices (the backend's index convention, matching index.wgsl / embedding_q4gsw), enforced by byte-size guards; weight must be at least 2D with its last dim equal to the output embed_dim; 1D dispatch capped at the device's per-dimension workgroup limit.

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

Differential Revision: D110836667

Differential Revision: D110836667

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

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 2bf50ae 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.

@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