Simplify concat same broadcast - #5179
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a simplify_algebra optimization that replaces redundant concatenated broadcasts with one broadcast.
Changes:
- Adds the
find_concat_same_broadcastmatcher. - Adds positive and negative unit tests.
- Review performed without agent fan-out; tests were not run.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/simplify_algebra.cpp |
Implements and registers the rewrite. |
test/simplify_algebra_test.cpp |
Tests matching and rejection cases. |
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| return all_of(inputs, [&](instruction_ref i) { | ||
| auto y = i->inputs().front(); | ||
| return y == x or *y == *x; | ||
| }); |
There was a problem hiding this comment.
If its structurally the same then it should be computing the same value. That is how CSE works as well. There is an issue with random_seed but that is usually only called once per model. So its an existing issue but fixing that issue is beyond the scope of this PR.
| find_concat_conv{}, | ||
| find_conv_concat_split_fuse{}, | ||
| find_concat_same_input{}, | ||
| find_concat_same_broadcast{}, |
There was a problem hiding this comment.
Added a CHANGELOG entry for the find_concat_same_broadcast matcher under the Added section (#5179).
Regressions detected 🔴 * No develop baseline was found for this PR's branch point; compared against the latest available develop run instead. |
|
Co-authored-by: pfultz2 <1306044+pfultz2@users.noreply.github.com>
| if(inputs.empty()) | ||
| return false; |
There was a problem hiding this comment.
I don't think this would ever be reachable. Concat needs at least 1 input.
There was a problem hiding this comment.
That isnt really checked in compute_shape.
Co-authored-by: kahmed10 <15948690+kahmed10@users.noreply.github.com>
Motivation
When a
concatjoins severalmultibroadcastinstructions that all broadcast the same value along an axis that is itself broadcasted (stride 0), the concat is redundant: every slice of the output is the same broadcasted data. Without this simplification, constant propagation folds the concat and materializes a large literal, wasting memory and copy bandwidth for data that could remain a single small value with a broadcasted view.Technical Details
find_concat_same_broadcastmatcher tosimplify_algebrathat replaces the whole pattern with onemultibroadcastof the shared input directly to the concat's output shape.multibroadcastof the same instruction (compared by ref or by value equality), that the concat axis has stride 0 in all inputs, and that the axis does not map to a non-unit dimension of the underlying input (so a plain multibroadcast can reproduce the output). Dynamic shapes are excluded.MIGRAPHX_PRED_MATCHERrather than inapply, so declining a match does not shadow the later concat matchers (e.g.find_concat_opstill moves the concat before the broadcast when the concat axis is not broadcasted).Changelog Category
Add a
CHANGELOG.mdentry for any option other thanNot ApplicableFollow the LLVM AI Tool Use Policy for contributions using AI.