[ExecuTorch][WebGPU] Add optimized linear_fp32 op#20847
Open
JCNTH wants to merge 4 commits into
Open
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/20847
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 97d1fc2 with merge base c2b273e ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
This was referenced Jul 10, 2026
[ExecuTorch][WebGPU] Add relu op (shared unary handler; sigmoid adopts make_compute_pipeline)
#20863
Open
This was referenced Jul 10, 2026
This was referenced Jul 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack from ghstack (oldest at bottom):
fp32
aten.linear.defaultnow runs on the WebGPU backend as a shared-memory-tiled GEMM (with an optional bias), so the dense projections in the Florence-2 BART decoder and DaViT vision encoder execute on-device instead of falling back.aten.linear.defaultis the projection primitive throughout those attention stacks; without a WebGPU kernel it AOT-delegates but throws at graph load.Problem — The WebGPU backend had no
aten.linear.defaultkernel. The op partitions into the delegate at export time, but at runtime there was no registered handler, so any graph containing an fp32 linear could not run. A naive one-thread-per-output GEMM would also be memory-bound: every output element would re-read a full row of the input and a full column of the weight from global memory with no reuse.Solution — Before:
aten.linear.defaulthad no runtime kernel (load-time failure). After: the handler bindsin, the prepacked[N,K]weight, an optionalbias, and an output, then dispatches a tiled GEMM that computesout[m,n] = sum_k in[m,k] * weight[n,k] (+ bias[n]). The 32x32 output tile is staged through workgroup shared memory: each workgroup cooperatively loads a 32x32 slab of A (input) and B (weight) intovar<workgroup>arrays,workgroupBarrier-syncs, then accumulates acrossceil(K/32)k-tiles so each loaded value is reused across the tile instead of re-fetched from global memory. WhenK % 4 == 0the handler routes to a vec4 variant that views the input and weight buffers asarray<vec4<f32>>over K and reduces withdot, halving the shared-memory traffic and issuing wide 16-byte loads.Implementation — Shared-memory tiling:
TILE = 32,@workgroup_size(8, 8, 1), each of the 64 threads owns a 4x4 register sub-tile (RPT = 4) so one 8x8 workgroup covers the full 32x32 output tile; twovar<workgroup>arraysa_sub/b_substage the A/B tiles. The scalar tiled kernel (linear_fp32_tiled.wgsl) reads the transposed[N,K]weight viaread_batt_weight[col*K + krow]; the vec4 kernel (linear_fp32_vec4.wgsl) uses[32][8]vec4 tiles (TILE4 = 8) with a flat cooperative tile load (256 vec4 elements spread across the 64 threads) and adot(av, b_sub[..])inner product. Both apply the same bias epiloguev = v + t_bias[c]gated onparams.has_bias. Build-time path routing:LinearFp32.cppselectslinear_fp32_vec4_wgslwhenK % 4 == 0, elselinear_fp32_tiled_wgsl(LinearFp32.cpp:77,100). Dispatch is a genuine 2D gridceil(N/32) x ceil(M/32), throwing before any allocation if either dimension exceeds 65535 (no 1D-flat stride recovery needed). Uses the sharedutils::make_compute_pipeline(shader/layout/pipeline/bind-group builder),utils::make_uniform(16-byteLinearParams {M,N,K,has_bias}), andutils::make_optional_binding(a 16-byte zero-filled dummy storage buffer for the None-bias binding that the shader gates off and never reads). Mirrors Vulkanbackends/vulkan/runtime/graph/ops/impl/Linear.cpp(linear_packed_weight: tiled-vs-vec shader selection on width alignment, transposed prepacked weight, bias epilogue).Constraints — fp32 only (validated by an output byte-size check);
weightis the prepacked[N,K]constant copied byet_vk.prepack;biasmay be None; the vec4 path requiresK % 4 == 0; 2D dispatch is capped at 65535 workgroups per dimension; fixed@workgroup_size(8, 8, 1)(no override constant).Co-authored-with: Claude Code.
@exported-using-ghexport
Differential Revision: D110836675
Differential Revision: D110836675