Skip to content

[AIMIGRAPHX-1002] Match nop reshapes with broadcasted inputs#4841

Open
eddieliao wants to merge 8 commits into
developfrom
fused_reduce_failure
Open

[AIMIGRAPHX-1002] Match nop reshapes with broadcasted inputs#4841
eddieliao wants to merge 8 commits into
developfrom
fused_reduce_failure

Conversation

@eddieliao
Copy link
Copy Markdown
Contributor

Motivation

bcast → reduce_mean over axis of size 1 currently fails as the reduce op inside fused_reduce gets optimized out, leaving an submodule with no reduce ins

Technical Details

The matcher for simplify_reshapes needs to be updated to account for such cases in which the strides may not match, but the result is still a nop.

Changelog Category

Add a CHANGELOG.md entry for any option other than Not Applicable

    • Added: New functionality.
    • Changed: Changes to existing functionality.
    • Removed: Functionality or support that has been removed. (Compared to a previous release)
    • Optimized: Component performance that has been optimized or improved.
    • Resolved Issues: Known issues from a previous version that have been resolved.
    • Not Applicable: This PR is not to be included in the changelog.

@eddieliao eddieliao self-assigned this May 5, 2026
@eddieliao eddieliao requested a review from causten as a code owner May 5, 2026 17:56
Copilot AI review requested due to automatic review settings May 5, 2026 17:56
@eddieliao eddieliao added bugfix Fixes a bug found in the code. Matchers Updates or adds a change to compile time Matchers ok-to-test labels May 5, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates simplify_reshapes matching so that reductions over size-1 axes on broadcasted inputs can be recognized as no-ops even when input/output strides differ (fixing cases like broadcast -> reduce_mean where the reduce gets optimized away incorrectly in downstream fusion scenarios).

Changes:

  • Split the “nop reshape” matcher into shape-equality vs lens-equality cases, using lens-only matching for reduce_* ops.
  • Add a new matcher combinator match::same_lens(...) to support lens-only equivalence checks.
  • Add a regression test covering broadcast -> reduce_mean as a nop-reduction case.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
test/simplify_reshapes_test.cpp Adds regression test ensuring a nop reduce_mean over a size-1 broadcast axis is eliminated.
src/simplify_reshapes.cpp Updates nop-reshape matching to use full-shape equality for reshape-like ops and lens-only equality for reduce_*.
src/include/migraphx/matcher.hpp Introduces same_lens matcher utility used by simplify_reshapes.

Comment thread src/include/migraphx/matcher.hpp Outdated
Comment thread test/simplify_reshapes_test.cpp Outdated
@eddieliao eddieliao requested review from pfultz2 and shivadbhavsar May 5, 2026 20:04
@codecov
Copy link
Copy Markdown

codecov Bot commented May 5, 2026

Codecov Report

❌ Patch coverage is 92.30769% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/include/migraphx/matcher.hpp 88.89% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #4841      +/-   ##
===========================================
+ Coverage    92.80%   92.85%   +0.05%     
===========================================
  Files          584      584              
  Lines        30146    30159      +13     
===========================================
+ Hits         27976    28003      +27     
+ Misses        2170     2156      -14     
Files with missing lines Coverage Δ
src/simplify_reshapes.cpp 97.95% <100.00%> (+0.01%) ⬆️
src/include/migraphx/matcher.hpp 87.57% <88.89%> (+0.03%) ⬆️

... and 4 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

@shivadbhavsar shivadbhavsar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The core issue to me sounds like the reduce compute_shape methods are not preserving broadcast strides. Is there a potential fix there instead of changing the noop condition in this matcher?

Comment thread src/simplify_reshapes.cpp
Comment thread src/include/migraphx/matcher.hpp Outdated
Comment thread src/include/migraphx/matcher.hpp Outdated
@eddieliao eddieliao requested review from pfultz2 and shivadbhavsar May 6, 2026 20:15
Comment thread src/include/migraphx/matcher.hpp Outdated
Co-authored-by: Paul Fultz II <paul.fultz@amd.com>
@eddieliao eddieliao requested a review from pfultz2 May 8, 2026 17:09
@causten
Copy link
Copy Markdown
Collaborator

causten commented May 12, 2026

Rekicked due to CI internal issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Fixes a bug found in the code. Matchers Updates or adds a change to compile time Matchers ok-to-test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants