You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adds a non-causal fused scaled-dot-product-attention kernel to the WebGPU backend, enabling the attention blocks of vision encoders (Florence-2 DaViT / SigLIP, SAM2) and non-causal cross-attention (BART) to run end-to-end on GPU.
Problem — the delegate had no kernel for et_vk.sdpa.default, the plain non-causal attention softmax(q @ kᵀ * scale + attn_mask) @ v that the et_vk source transform plugs into every vision-encoder attention block. Without it the attention layers broke the graph. This is distinct from the causal KV-cache sdpa_with_kv_cache: no cache, an optional additive mask, and it must handle asymmetric sequence lengths (S_q != S_kv, e.g. a Hiera pooled query or cross-attention).
Solution
Before: no non-causal fused-attention kernel — vision-encoder attention blocks could not lower into the delegate.
After: et_vk_sdpa_impl chains three compute dispatches over [B, H, S, D] (DSHB) row-major tensors — et_vk_sdpa_qk.wgsl computes the scaled Q·Kᵀ scores plus an optional additive mask, the reused sdpa_softmax.wgsl does the row-wise softmax, and et_vk_sdpa_av.wgsl computes softmax · V into the output.
Implementation
QK phase (et_vk_sdpa_qk.wgsl): one GPU thread per (b, h, s) row of the [B, H, S_q, S_kv] attention-weight buffer; q/k are bound as array<vec4<f32>> over D, and the thread loops over c (key positions) and d4 (D/4) accumulating dot(q4, k4), then multiplies by scale and adds mask[...] when has_mask. Row count B*H*S_q stays well under the 1D dispatch limit for any ViT.
Softmax phase: reuses the existing sdpa_softmax.wgsl (one workgroup per row, hardcoded @workgroup_size(64,1,1)) dispatched on a near-square 2D workgroup grid past the 65535 ceiling; the shader recovers the flat row index from @builtin(num_workgroups), so no override constant is needed.
AV phase (et_vk_sdpa_av.wgsl): one thread per (b, h, s, d4) computing a vec4<f32> of four output elements; v/out are bound as array<vec4<f32>> over D and the thread contracts over c scalar (S_kv is not guaranteed % 4 == 0), accumulating sm[...] * v4.
Two fp32 scratch buffers (attn, softmax, each B*H*S_q*S_kv) are allocated via graph.create_scratch_buffer; scale defaults to 1/sqrt(D) when the arg is None or takes the Double value; the three uniforms are compact structs (QkParams/AvParams 32 bytes, SoftmaxParams 16 bytes).
Uses the shared runtime helpers: utils::make_compute_pipeline, utils::make_uniform, utils::check_vec4_aligned (guards D % 4 == 0), utils::clamp_workgroup_size + utils::compute_1d_workgroup_count (QK / AV grids), utils::compute_2d_workgroup_count (softmax grid), and utils::make_optional_binding (a 4-byte dummy satisfies the mask binding when absent; the shader never reads it under has_mask == 0).
Constraints — fp32 only (bails on q/out byte mismatch); non-causal (causality is expressed as a baked additive mask input, not a code path); D % 4 == 0 for the vec4 QK/AV kernels (every model in scope uses D=64 or 128); q rank ≥ 3; k.dims == v.dims, q/k/v share H and D and all leading batch dims, and out.dims == q.dims; asymmetric S_q != S_kv is supported (reduces bit-identically to self-attention when equal); a supplied mask must be [B, H, S_q, S_kv] fp32.
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"
meta-claBot
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
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
CLA SignedThis label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
2 participants
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):
Adds a non-causal fused scaled-dot-product-attention kernel to the WebGPU backend, enabling the attention blocks of vision encoders (Florence-2 DaViT / SigLIP, SAM2) and non-causal cross-attention (BART) to run end-to-end on GPU.
Problem — the delegate had no kernel for
et_vk.sdpa.default, the plain non-causal attentionsoftmax(q @ kᵀ * scale + attn_mask) @ vthat the et_vk source transform plugs into every vision-encoder attention block. Without it the attention layers broke the graph. This is distinct from the causal KV-cachesdpa_with_kv_cache: no cache, an optional additive mask, and it must handle asymmetric sequence lengths (S_q != S_kv, e.g. a Hiera pooled query or cross-attention).Solution
et_vk_sdpa_implchains three compute dispatches over[B, H, S, D](DSHB) row-major tensors —et_vk_sdpa_qk.wgslcomputes the scaledQ·Kᵀscores plus an optional additive mask, the reusedsdpa_softmax.wgsldoes the row-wise softmax, andet_vk_sdpa_av.wgslcomputessoftmax · Vinto the output.Implementation
et_vk_sdpa_qk.wgsl): one GPU thread per(b, h, s)row of the[B, H, S_q, S_kv]attention-weight buffer;q/kare bound asarray<vec4<f32>>overD, and the thread loops overc(key positions) andd4(D/4) accumulatingdot(q4, k4), then multiplies byscaleand addsmask[...]whenhas_mask. Row countB*H*S_qstays well under the 1D dispatch limit for any ViT.sdpa_softmax.wgsl(one workgroup per row, hardcoded@workgroup_size(64,1,1)) dispatched on a near-square 2D workgroup grid past the 65535 ceiling; the shader recovers the flat row index from@builtin(num_workgroups), so no override constant is needed.et_vk_sdpa_av.wgsl): one thread per(b, h, s, d4)computing avec4<f32>of four output elements;v/outare bound asarray<vec4<f32>>overDand the thread contracts overcscalar (S_kvis not guaranteed% 4 == 0), accumulatingsm[...] * v4.attn,softmax, eachB*H*S_q*S_kv) are allocated viagraph.create_scratch_buffer;scaledefaults to1/sqrt(D)when the arg isNoneor takes theDoublevalue; the three uniforms are compact structs (QkParams/AvParams32 bytes,SoftmaxParams16 bytes).utils::make_compute_pipeline,utils::make_uniform,utils::check_vec4_aligned(guardsD % 4 == 0),utils::clamp_workgroup_size+utils::compute_1d_workgroup_count(QK / AV grids),utils::compute_2d_workgroup_count(softmax grid), andutils::make_optional_binding(a 4-byte dummy satisfies the mask binding when absent; the shader never reads it underhas_mask == 0).backends/vulkan/runtime/graph/ops/impl/SDPA.cppSDPAMode::FUSED(general non-cache SDPA,[B, H, S, D]DSHB layout, optional additiveattn_mask, optionalscale, unpadded fp32 attention weights).Constraints — fp32 only (bails on
q/outbyte mismatch); non-causal (causality is expressed as a baked additive mask input, not a code path);D % 4 == 0for the vec4 QK/AV kernels (every model in scope usesD=64or128);qrank ≥ 3;k.dims == v.dims,q/k/vshareHandDand all leading batch dims, andout.dims == q.dims; asymmetricS_q != S_kvis supported (reduces bit-identically to self-attention when equal); a supplied mask must be[B, H, S_q, S_kv]fp32.Co-authored-with: Claude Code.
Differential Revision: D110836679