[webgpu] support smooth softmax for non-FA GQA implementation - #25285
Merged
Conversation
fs-eire
marked this pull request as draft
July 4, 2025 02:01
fs-eire
force-pushed
the
fs-eire/webgpu-smooth-softmax
branch
5 times, most recently
from
July 5, 2025 19:11
c201d49 to
5845d0e
Compare
fs-eire
marked this pull request as ready for review
July 5, 2025 19:11
fs-eire
force-pushed
the
fs-eire/webgpu-smooth-softmax
branch
from
July 5, 2025 19:13
5845d0e to
3a7b54f
Compare
Contributor
There was a problem hiding this comment.
Pull Request Overview
Adds support for smooth softmax, attention bias, and a per-head “sink” value in the non-FlashAttention path of GroupQueryAttention.
- Introduce three new optional inputs (
position_ids,attention_bias,head_sink) in both WebGPU and CPU GQA kernels - Wire
attention_biasandhead_sinkthroughComputeInternal,ApplyAttention, and the softmax shader - Update program constructors and shader generation to respect
use_smooth_softmax,has_seqlen_k, andhas_head_sinkflags
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| onnxruntime/contrib_ops/webgpu/bert/group_query_attention.cc | Added new inputs and custom‐input checks; passed attention_bias & head_sink into ComputeInternal and ApplyAttention |
| onnxruntime/contrib_ops/webgpu/bert/flash_attention.cc | Noted TODO for smooth softmax/head_sink support in FlashAttention |
| onnxruntime/contrib_ops/webgpu/bert/attention_common.h | Extended ApplyAttention signature to include head_sink |
| onnxruntime/contrib_ops/webgpu/bert/attention.h | Modified program constructors to take use_smooth_softmax, has_seqlen_k, has_head_sink flags |
| onnxruntime/contrib_ops/webgpu/bert/attention.cc | Enhanced InPlaceSoftmaxProgram generation to handle the new flags in the shader |
| onnxruntime/contrib_ops/cpu/bert/group_query_attention_helper.h | Added validation logic for the new head_sink tensor |
| onnxruntime/contrib_ops/cpu/bert/group_query_attention.cc | Loaded head_sink input and invoked validation in the CPU kernel |
Comments suppressed due to low confidence (3)
onnxruntime/contrib_ops/webgpu/bert/attention.cc:226
- The new smooth_softmax and head_sink branches in the shader code introduce distinct behavior; please add unit or integration tests to cover both code paths in WebGPU and CPU implementations.
Status InPlaceSoftmaxProgram::GenerateShaderCode(ShaderHelper& shader) const {
onnxruntime/contrib_ops/webgpu/bert/group_query_attention.cc:155
- The operator schema and user‐facing documentation need updating to expose the new optional inputs (
position_ids,attention_bias, andhead_sink) and explain their semantics.
const Tensor* position_ids = context.Input<Tensor>(9); // TODO: support sliding window
onnxruntime/contrib_ops/webgpu/bert/attention.h:72
- [nitpick] The term
head_sinkmay be unfamiliar to new readers; consider renaming it to something more descriptive or adding a comment in the header to clarify its intended effect on the softmax.
InPlaceSoftmaxProgram(int work_group_size, int components, bool use_smooth_softmax, bool has_seqlen_k, bool has_head_sink)
guschmue
suggested changes
Jul 7, 2025
guschmue
approved these changes
Jul 7, 2025
daijh
pushed a commit
to daijh/onnxruntime
that referenced
this pull request
Jul 10, 2025
…oft#25285) ### Description support smooth softmax for non-FA GQA implementation This change depends on: - microsoft#25269 Work items: - [x] support smooth softmax - [x] support bias - [x] support head sink (per-head smooth softmax) The following will not be included in this PR: - support for FlashAttention - support sliding window
qti-yuduo
pushed a commit
to CodeLinaro/onnxruntime
that referenced
this pull request
Aug 8, 2025
…oft#25285) ### Description support smooth softmax for non-FA GQA implementation This change depends on: - microsoft#25269 Work items: - [x] support smooth softmax - [x] support bias - [x] support head sink (per-head smooth softmax) The following will not be included in this PR: - support for FlashAttention - support sliding window
sanketkaleoss
pushed a commit
to sanketkaleoss/onnxruntime
that referenced
this pull request
Aug 11, 2025
…oft#25285) ### Description support smooth softmax for non-FA GQA implementation This change depends on: - microsoft#25269 Work items: - [x] support smooth softmax - [x] support bias - [x] support head sink (per-head smooth softmax) The following will not be included in this PR: - support for FlashAttention - support sliding window
This was referenced Jul 3, 2026
[Feature Request] CUDA EP: support
attention_bias in GroupQueryAttention (last EP missing it)
#29506
Closed
tianleiwu
pushed a commit
that referenced
this pull request
Jul 8, 2026
…path (#29525) ### Description `com.microsoft.GroupQueryAttention`'s optional `attention_bias` input (input #10) is implemented by the CPU EP (#23944) and the WebGPU EP (#25285, #26769), but the CUDA EP rejects it at runtime. This PR wires it through. No new kernel is needed: the unfused GQA fallback added for #28195 calls `LaunchUnfusedAttention`, whose kernel already implements an additive bias with dim-0/dim-1 broadcast, per-batch `seqlens_k`, causal/sliding-window masking and softcap — the op just passed `attn_bias=nullptr`. The change is dispatch plumbing: - **`group_query_attention.cc`** — remove the blanket rejection; validate the bias element type; set `broadcast_attn_bias_dim_0/1` from the bias shape (the fields already exist on `AttentionParameters`); add `!has_attention_bias` to the XQA / cuDNN SDPA / flash / flash-fast-decode / MEA eligibility so bias-carrying nodes dispatch to the unfused fallback; set `data.attention_bias`. - **`attention_data.h`** — add the `attention_bias` pointer to `GroupQueryAttentionData`. - **`group_query_attention_impl.cu`** — pass the real pointer and broadcast flags in `UnfusedGqaAttention` (previously hardcoded `nullptr`/`false`). Why each fused path stays disqualified with a bias: - flash / flash fast-decode: `flash_api.h` has no bias parameter (same exclusion as MHA and the ONNX `Attention`-op CUDA kernel). - XQA: no bias parameter. - cuDNN SDPA: GQA's cuDNN path is bottom-right causal, which cuDNN documents as incompatible with a bias (`multihead_attention.cc` has the same restriction). - cutlass MEA: the wrapper computes the bias row stride from `kv_sequence_length`, which GQA sets to the KV-cache capacity (`seqlen_present_kv_cache`) rather than `total_sequence_length`, so rows would be misaligned under past/present buffer sharing. Left for a follow-up (needs an explicit `attn_bias_strideM` in `MemoryEfficientAttentionParams`). Kept `NOT_IMPLEMENTED` (explicit, clear errors instead of the previous blanket rejection): bias × quantized KV cache (unfused requires `T == U`), bias × smooth-softmax/head_sink. ### Tests New `TestGQAAttentionBias` in `test_gqa.py`: prompt and past/decode parity across packed/unpacked QKV, shared/separate KV buffer, rotary, odd head sizes (40/80), and a subsequent multi-token prompt. The bias is **non-zero random** so a kernel that silently ignores the input fails parity (the harness previously modeled a zeros bias). Also fixes the test graph builder declaring the bias input's last dim as the KV-cache capacity instead of `total_sequence_length` (the shape the op validates). Full `test_gqa.py` suite: 476 tests pass, no regressions (SM89, CUDA 12.8). ### Motivation and Context Fixes #29506. Transformers.js-exported speech models carry non-causal attention patterns as `attention_bias` and currently cannot run on the CUDA EP at all, while running fine on WebGPU and CPU: - [`onnx-community/Voxtral-Mini-4B-Realtime-2602-ONNX`](https://huggingface.co/onnx-community/Voxtral-Mini-4B-Realtime-2602-ONNX) (streaming ASR) - [`onnx-community/cohere-transcribe-03-2026-ONNX`](https://huggingface.co/onnx-community/cohere-transcribe-03-2026-ONNX) End-to-end validation with this patch on an RTX 4070 SUPER: the full Voxtral-Mini-4B-Realtime streaming pipeline (q4f16) transcribes correctly at **RTF 0.23** (31 s clip, 25.6 tok/s sustained decode, 6.4 GB VRAM) — on this workload the unfused-attention path outperforms the same model on the WebGPU EP (RTF 0.26). (While validating, an unrelated pre-existing issue surfaced: `GroupQueryAttentionFusion` breaks graphs whose GQA nodes carry >9 inputs — filed as #29524.) --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
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.
Description
support smooth softmax for non-FA GQA implementation
This change depends on:
Work items:
The following will not be included in this PR: