Commit c448d4f
feat: emit GroupQueryAttention directly in Attention when EP supports it (#134)
## Summary
Eliminates the post-hoc `RotaryAttentionToGQA` rewrite rule as the
primary path for GQA-capable EPs, replacing it with direct
`com.microsoft::GroupQueryAttention` emission at graph construction
time.
## What changed
### New: `GQAContext` NamedTuple (`components/_attention.py`)
A typed bundle of per-graph scalars passed through the decoder stack as
`attention_bias`:
- `seqlens_k`: per-batch KV sequence length `[batch]` INT32
- `total_seq_len`: scalar INT32
- `cos_cache` / `sin_cache`: full RoPE tables (not gathered slices)
### Modified: `Attention.forward()`
Checks `isinstance(attention_bias, GQAContext)` at the top and
dispatches to `_forward_gqa()` which emits `GroupQueryAttention`
directly with `do_rotary=1`, bypassing the external
`RotaryEmbeddingBase.forward()` + `apply_rotary_pos_emb()` path.
### Modified: `TextModel.forward()` (`models/base.py`)
Adds an EP-driven dispatch at graph construction time:
```python
caps = ep_capabilities()
use_gqa = (
attention_mask is not None
and get_build_dtype() in caps.gqa_dtypes
and caps.supports_fused_rope
and isinstance(self.rotary_emb, BaseRope)
)
```
When True: calls `self.rotary_emb(op, position_ids)` to realize
`cos_cache`/`sin_cache` as ONNX initializers (discards result), then
builds `GQAContext` with the raw parameter tensors and computes
`seqlens_k`/`total_seq_len` from `attention_mask`.
## What stays unchanged
- `RotaryAttentionToGQA` rewrite rule — kept as fallback for:
- Qwen3.5 with `_MRopeBase` 3D mRoPE (`isinstance(..., BaseRope)` is
True but `supports_fused_rope=False` on affected EPs excludes it)
- DML EP (`supports_fused_rope=False`)
- Any model not using `TextModel` (VLMs, Qwen3.5, etc.)
- Graph I/O: `position_ids` remains in the graph inputs for consistency
with the rewrite-rule path (also leaves `position_ids` as a dead input
post-optimization in both paths)
## Tests
Five new tests in
`components/_attention_test.py::TestGQAContextDispatch`:
- `test_gqa_context_emits_group_query_attention`: component-level,
verifies GQA is emitted
- `test_gqa_context_respects_rotary_interleaved`: checks
`rotary_interleaved` attribute
- `test_standard_attention_when_no_gqa_context`: standard path
unaffected
- `test_build_with_cuda_ep_emits_gqa_directly`: CUDA EP + f16 → GQA
present, Attention absent
- `test_build_with_default_ep_uses_standard_attention`: default EP →
standard Attention
All 2327 tests pass.
---------
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>1 parent e6b658d commit c448d4f
4 files changed
Lines changed: 404 additions & 23 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
| |||
109 | 110 | | |
110 | 111 | | |
111 | 112 | | |
| 113 | + | |
112 | 114 | | |
113 | 115 | | |
114 | 116 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
17 | | - | |
18 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
19 | 51 | | |
20 | 52 | | |
21 | 53 | | |
| |||
51 | 83 | | |
52 | 84 | | |
53 | 85 | | |
| 86 | + | |
54 | 87 | | |
55 | 88 | | |
56 | 89 | | |
| |||
125 | 158 | | |
126 | 159 | | |
127 | 160 | | |
| 161 | + | |
128 | 162 | | |
129 | 163 | | |
130 | 164 | | |
| |||
144 | 178 | | |
145 | 179 | | |
146 | 180 | | |
| 181 | + | |
147 | 182 | | |
148 | 183 | | |
149 | 184 | | |
| |||
187 | 222 | | |
188 | 223 | | |
189 | 224 | | |
| 225 | + | |
| 226 | + | |
190 | 227 | | |
191 | 228 | | |
192 | 229 | | |
| |||
231 | 268 | | |
232 | 269 | | |
233 | 270 | | |
234 | | - | |
| 271 | + | |
235 | 272 | | |
236 | 273 | | |
237 | 274 | | |
| |||
254 | 291 | | |
255 | 292 | | |
256 | 293 | | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
257 | 305 | | |
258 | 306 | | |
259 | 307 | | |
| |||
284 | 332 | | |
285 | 333 | | |
286 | 334 | | |
| 335 | + | |
287 | 336 | | |
288 | 337 | | |
289 | 338 | | |
290 | 339 | | |
291 | 340 | | |
292 | 341 | | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
293 | 399 | | |
294 | 400 | | |
295 | 401 | | |
| |||
0 commit comments