Skip to content

[ExecuTorch][WebGPU] Add optimized addmm op#20855

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

[ExecuTorch][WebGPU] Add optimized addmm op#20855
JCNTH wants to merge 1 commit into
gh/JCNTH/30/basefrom
gh/JCNTH/30/head

Conversation

@JCNTH

@JCNTH JCNTH commented Jul 10, 2026

Copy link
Copy Markdown

Stack from ghstack (oldest at bottom):

fp32 aten.addmm.default now runs on the WebGPU backend as a shared-memory-tiled GEMM, so the fused bias-plus-matmul that HuggingFace Linear lowers to executes on-device. In the Florence-2 BART and DaViT graphs the HF nn.Linear layers lower to aten.addmm (not aten.linear), making addmm the dominant dense GEMM in those stacks.

Problem — The WebGPU backend had no aten.addmm.default kernel, so any graph whose linears lowered through addmm delegated at export but failed at runtime load. As with plain matmul, a naive per-output kernel would re-stream both operands from global memory with no reuse.

Solution — Before: aten.addmm.default had no runtime kernel (load-time failure). After: the handler binds self (bias), mat1 [M,K], mat2 [K,N], and the output, and dispatches a tiled GEMM computing out = beta*self + alpha*(mat1 @ mat2). Each workgroup cooperatively stages 32x32 slabs of mat1 and mat2 into workgroup shared memory, workgroupBarrier-syncs, and accumulates across ceil(K/32) k-tiles so the coalesced tile load is amortized over the whole output tile. The beta*self + alpha*acc epilogue broadcasts self from either [N] (the common HF bias case) or a full [M,N].

Implementation — Shares the tiled-GEMM approach with linear_fp32: TILE = 32, RPT = 4, @workgroup_size(8, 8, 1), two var<workgroup> a_sub/b_sub arrays, each thread accumulating a 4x4 register sub-tile over ceil(K/32) tiles. It re-derives the B-side read for mat2's actual [K,N] layout: read_b reads t_mat2[krow*N + col] (N contiguous), unlike linear's transposed [N,K] weight read. No vec4 variant is provided: with mat2 [K,N] the N dimension is contiguous, so a vec4-over-K view would require a strided gather on the mat2 side that erodes the benefit, and the cooperative shared-memory tile load already closes the coalescing gap. Epilogue selects t_self[r*N + c] when self_2d, else the broadcast t_self[c], and writes beta*self_val + alpha*acc[ir][ic]. beta/alpha are read with the shared utils::scalar_or (Double/Int/Null-permissive, default 1.0). Dispatch is a 2D grid ceil(N/32) x ceil(M/32), throwing if either dimension exceeds 65535. Uses the shared utils::make_compute_pipeline and utils::make_uniform (32-byte AddmmParams {M,N,K,self_2d,beta,alpha,_pad[2]}). Validates non-null buffers, 2D mat1/mat2/out, mat2 shape == [K,N], and self broadcastable from [N] or [M,N].

Constraints — fp32 only (output byte-size check); mat1/mat2/out must be 2D with mat2 exactly [K,N]; self must be [N] or [M,N]; scalar-only tiled path (no vec4); 2D dispatch capped at 65535 workgroups per dimension; fixed @workgroup_size(8, 8, 1).

Co-authored-with: Claude Code.

Differential Revision: D110836673

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

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

✅ You can merge normally! (2 Unrelated Failures)

As of commit 30a4b34 with merge base aceeb40 (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

BROKEN TRUNK - The following job failed but was 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.

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