Skip literals that are not scalars or iotas when fusing attention - #5211
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refines the fuse_attention pass’s constant-capture behavior so attention fusion only pulls in “inlinable” literal constants (scalars or iota/range literals), avoiding problematic large/non-iota literals while still enabling MLIR causal-mask detection.
Changes:
- Add a literal classifier in
src/fuse_attention.cppto restrict which literals get pulled into the attention subgraph during fusion. - Update existing attention-fusion tests to pass relevant literals as group inputs (instead of recreating them inside the grouped module).
- Add a new regression test covering a packed-but-nonstandard-stride bias literal that should not be inlined as an iota/range.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/fuse_attention.cpp |
Adds “range literal” detection and uses it to gate which evaluable constants are pulled into attention fusion. |
test/fuse_attention.cpp |
Updates expected fused graphs to pass literals via group inputs and adds a regression test for non-iota bias literals. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| bool is_range_literal(const literal& l) | ||
| { | ||
| bool result = false; | ||
| l.visit([&](auto x) { | ||
| result = std::adjacent_find(x.begin(), x.end(), [](auto cur, auto next) { | ||
| return not float_equal(next - cur, 1); | ||
| }) == x.end(); | ||
| }); | ||
| return result; | ||
| } |
| result = std::adjacent_find(x.begin(), x.end(), [](auto cur, auto next) { | ||
| return not float_equal(next - cur, 1); | ||
| }) == x.end(); |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
Suppressed comments (1)
src/fuse_attention.cpp:126
next - curcan overflow for signed integer literals even after the monotonicity check (for example,{INT64_MIN, INT64_MAX}), causing undefined behavior while running the fusion pass. Compare againstcur + 1instead;next > curguarantees that increment is safe and also expresses the iota condition directly.
result = std::adjacent_find(x.begin(), x.end(), [](auto cur, auto next) {
return next <= cur or not float_equal(next - cur, 1);
| return 0; | ||
| } | ||
|
|
||
| bool is_range_literal(const literal& l) |
CharlieL7
left a comment
There was a problem hiding this comment.
nit: add motivation section to the PR so we know why this change was made
Motivation
Fixes the error: 'migraphx.literal' op strides of non-splat literal are not in standard shape
Technical Details
Changelog Category
Add a
CHANGELOG.mdentry for any option other thanNot ApplicableFollow the LLVM AI Tool Use Policy for contributions using AI.