-
Notifications
You must be signed in to change notification settings - Fork 2
feat: emit GroupQueryAttention directly in Attention when EP supports it #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to rewrite for 3D mRoPE? What's the difference? @copilot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
GroupQueryAttentionkernel'sdo_rotary=1mode expects a simple 1D cos/sin lookup table[max_seq, rotary_dim]and a scalar position per token — it gathers one vector per position from those tables internally.3D mRoPE (
_MRopeBasesubclasses:ChunkedMRopefor Qwen2.5-VL,InterleavedMRopefor Qwen3-VL) usesposition_idsof shape(3, batch, seq_len)with three separate coordinates per token (temporal T, height H, width W). The RoPE is computed by:Wherenodes controlled byh_mask/w_maskarraysThe resulting per-token, per-channel cos/sin embeddings can't be expressed as a single lookup table that GQA's
do_rotary=1understands — the "table" is dynamically mixed rather than a simple positional index.So for 3D mRoPE the direct GQA path is skipped, and the
RotaryAttentionToGQA/AttentionToGQArewrite rules handle these models instead, usingdo_rotary=0(Q/K already have RoPE pre-applied before entering GQA).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kunal-vaishnavi does this sound reasonable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot would it be a good idea to just set do_rotary=0 here? What's missing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes,
do_rotary=0could work for 3D mRoPE at build time — theAttentionToGQArewrite rule already does exactly this today. Making it work directly in the build path is feasible but requires a few pieces that are currently missing:Different payload in context:
GQAContextcurrently carriescos_cache/sin_cache(full tables[max_seq, rotary_dim]) specifically fordo_rotary=1— GQA indexes them internally. Fordo_rotary=0, we need the already-gathered, per-token(cos, sin)tensors(batch, seq, rotary_dim)produced by_MRopeBase.forward(), not raw tables. These are a completely different shape/purpose and would need to be plumbed through (either by extendingGQAContextor creating a separate context type).Apply RoPE inside
_forward_gqa(): Whendo_rotary=0,apply_rotary_pos_emb()must be called on Q/K before emitting theGroupQueryAttentionnode (no cos/sin inputs to GQA in this mode). This branch doesn't exist in_forward_gqa()yet.Remove the
_MRopeBaseguard inTextModel.forward(), or replace it with a condition that choosesdo_rotary=0for_MRopeBasevsdo_rotary=1for standardBaseRope.Worth noting: the
supports_fused_ropeguard would also need revisiting —do_rotary=0doesn't need fused RoPE inside the GQA kernel, so it could be applied even on EPs wheresupports_fused_rope=False(as long asdtype in caps.gqa_dtypes).Happy to add this if you'd like to extend the PR. It would fully eliminate
AttentionToGQAas a fallback for mRoPE models on GQA-capable EPs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do it in a separate PR.