[AIMIGRAPHX-1209] optimize kernel 2 for non kv cache flash decoding and update tests#5090
Open
bdevorem wants to merge 3 commits into
Open
[AIMIGRAPHX-1209] optimize kernel 2 for non kv cache flash decoding and update tests#5090bdevorem wants to merge 3 commits into
bdevorem wants to merge 3 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #5090 +/- ##
===========================================
- Coverage 92.90% 92.90% -0.00%
===========================================
Files 603 603
Lines 32528 32525 -3
===========================================
- Hits 30218 30215 -3
Misses 2310 2310
🚀 New features to boost your workflow:
|
Regressions detected 🔴 * No develop baseline was found for this PR's branch point; compared against the latest available develop run instead. |
|
Contributor
There was a problem hiding this comment.
Pull request overview
Optimizes the flash-decoding “kernel 2” recombination logic inside find_flash_decoding (in fuse_attention) by switching to an exp-normalize formulation that produces IR more amenable to downstream fusion/rewrites, and updates the corresponding attention fusion tests and changelog entry.
Changes:
- Rewrites flash-decoding recombination to compute numerator/denominator via unnormalized weights
exp(LSE - max)and divide. - Updates multiple flash-decoding test expectations to match the new kernel-2 IR structure.
- Adds a changelog note for the optimization.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/fuse_attention.cpp |
Reworks flash-decoding kernel-2 recombination IR to exp-normalize form for better downstream fusion. |
test/fuse_attention.cpp |
Updates expected graphs/IR construction in flash-decoding tests to reflect the new recombination form. |
CHANGELOG.md |
Documents the flash-decoding recombination optimization. |
Comments suppressed due to low confidence (1)
src/fuse_attention.cpp:776
- The shape-specific comment "[B, G, M] -> [B, G, M, D]" is misleading here because the actual LSE/partial-output ranks vary (e.g., LSE often has a trailing singleton dim and 4D attention adds a head dim). Consider making this comment rank-agnostic and describing the intent (broadcast to match O' for elementwise mul).
// broadcast weights to match O' shape
// [B, G, M] -> [B, G, M, D]
pfultz2
approved these changes
Jul 23, 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
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.
Motivation
Rewrites the flash decoding kernel 2 recombination step in find_flash_decoding to use the exp-normalize form:
O = sum(O' * exp(LSE - max)) / sum(exp(LSE - max))instead of normalizing weights first, then scaling and summing partial outputs. The result is mathematically equivalent but produces IR that fuses more cleanly downstream (e.g. with rewrite_broadcast in a follow-up PR).
This is a standalone slice extracted from #4546.
Technical Details
After kernel 1, flash decoding returns partial outputs
O'and log-sum-exp valuesLSEper group. Kernel 2 combines them into the final attention output.The previous IR did:
The new form avoids the intermediate normalized scale broadcast and instead computes numerator and denominator separately:
A follow-up PR will introduce a
rewrite_broadcastpass that further optimizes this down the line by rewritingmultibroadcast → convertintoconvert → multibroadcastandmultibroadcast → reduceintoreduce → multibroadcast, so convert and reduction run on the smaller tensor before broadcasting to the fullO'shape.Opus was used to make this PR description more concise.
Changelog Category
Add a
CHANGELOG.mdentry for any option other thanNot ApplicableFollow the LLVM AI Tool Use Policy for contributions using AI.