Support bidirectional GroupQueryAttention on CPU and CUDA - #31704
Conversation
Co-authored-by: tianleiwu <30328909+tianleiwu@users.noreply.github.com>
Co-authored-by: tianleiwu <30328909+tianleiwu@users.noreply.github.com>
Co-authored-by: tianleiwu <30328909+tianleiwu@users.noreply.github.com>
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
Pull request overview
Adds a new causal attribute to the com.microsoft::GroupQueryAttention contrib op to support bidirectional (non-causal) attention on CPU and CUDA while preserving the existing causal-by-default behavior for backward compatibility. The change propagates the attribute through backend selection and masking logic, and updates documentation plus test coverage across EPs.
Changes:
- Introduce
causalattribute (default1) in the operator schema and documentation; treat0as bidirectional attention. - CPU/CUDA: wire
causalinto masking and backend eligibility (e.g., disable causal-only kernels like XQA whencausal=0). - WebGPU/JS: explicitly reject
causal=0withNOT_IMPLEMENTED, with corresponding tests.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| onnxruntime/test/python/transformers/test_gqa.py | Adds causal to test config/node attributes and expands CUDA parity + rejection coverage (including quantized bidirectional cases). |
| onnxruntime/test/contrib_ops/group_query_attention_op_test.cc | Adds focused unit tests for default-causal behavior, bidirectional masking, invalid values, and WebGPU NOT_IMPLEMENTED. |
| onnxruntime/core/graph/contrib_ops/bert_defs.cc | Extends the GroupQueryAttention schema with the causal attribute (default 1) and updates operator doc text. |
| onnxruntime/contrib_ops/webgpu/bert/group_query_attention.h | Enforces causal ∈ {0,1} and rejects causal=0 as not implemented for WebGPU. |
| onnxruntime/contrib_ops/js/bert/group_query_attention.h | Rejects causal=0 as not implemented for the JS implementation. |
| onnxruntime/contrib_ops/cuda/bert/group_query_attention.cc | Parses/validates causal, propagates it to parameters, blocks causal-only XQA when bidirectional, and threads it into cuDNN SDPA. |
| onnxruntime/contrib_ops/cuda/bert/group_query_attention_impl.cu | Propagates causal into MEA/cuDNN calls; improves the unfused-path error for quantized KV-cache unsupported cases. |
| onnxruntime/contrib_ops/cpu/bert/group_query_attention.cc | Propagates causal into runtime parameters and prevents using the CPU flash path for bidirectional attention. |
| onnxruntime/contrib_ops/cpu/bert/gqa_attention_base.h | Adds causal_ parsing/validation and adjusts masking bounds for bidirectional vs causal behavior. |
| docs/ContribOperators.md | Documents the new causal attribute for GroupQueryAttention. |
| docs/contrib_ops/cuda/gqa.md | Updates CUDA GQA documentation to describe causal and backend support/eligibility for bidirectional attention. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Automated review synthesisVerdict: request changes. Core CPU/CUDA masking math for 🔴 Critical1. Bidirectional prompt test validates against a causal reference — 🟠 Major2.
Nothing rejects this attribute combination. Recommend either rejecting 3. DML and WebNN silently ignore the new
These EPs will now compute causal output for a model author who explicitly requested bidirectional attention, with no error. Recommend adding a rejection for 4. Test coverage gaps
🟡 Minor
✅ Cleared (hypotheses checked and refuted)
Open questions (need execution to settle; not run as part of this review)
|
Reject undefined or unsupported causal combinations across providers, correct parity references, and strengthen mask/decode coverage so bidirectional models cannot silently execute causal behavior.
|
Addressed the actionable feedback in commit
I left the OpenVINO/MIGraphX allowlists unchanged: those EPs delegate GQA to external backend frontends and do not have an in-repo GQA lowering where this attribute can be gated. Their bidirectional support needs backend-specific capability confirmation separately. |
Re-review after updateRe-checked the latest push (commits
Remaining, non-blocking:
No remaining blocking issues from this round. LGTM pending CI. |
Description
GroupQueryAttention previously always applied a causal mask. This adds a
causalattribute, defaulting to1for backward compatibility.causal=0.local_window_size != -1with bidirectional attention because local-window alignment is defined only for causal attention.local_window_size != -1with bidirectional attention.NOT_IMPLEMENTEDinstead of reading them incorrectly.NOT_IMPLEMENTEDforcausal=0.causal=0during kernel creation, and WebNN declines the node during capability checks, avoiding silent causal output.Motivation and Context
Bidirectional models require each query token to attend to the full valid key sequence. The new attribute enables this on CPU and CUDA while preserving existing causal behavior by default. Generation conversion stamps
causal=1explicitly because its decoder attention is unidirectional by definition.