Skip to content

Simplify concat same broadcast - #5179

Open
pfultz2 wants to merge 5 commits into
developfrom
concat-same-broadcast
Open

Simplify concat same broadcast#5179
pfultz2 wants to merge 5 commits into
developfrom
concat-same-broadcast

Conversation

@pfultz2

@pfultz2 pfultz2 commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

Motivation

When a concat joins several multibroadcast instructions 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

  • Adds a find_concat_same_broadcast matcher to simplify_algebra that replaces the whole pattern with one multibroadcast of the shared input directly to the concat's output shape.
  • The match requires that every input is a single-input multibroadcast of 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.
  • All conditions live in a MIGRAPHX_PRED_MATCHER rather than in apply, so declining a match does not shadow the later concat matchers (e.g. find_concat_op still moves the concat before the broadcast when the concat axis is not broadcasted).
  • Unit tests cover the scalar and non-scalar positive cases, plus negatives: different broadcasted values, a non-broadcasted (nonzero-stride) concat axis, and a non-unit dimension under the concat axis.

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.

Follow the LLVM AI Tool Use Policy for contributions using AI.

@pfultz2
pfultz2 requested a balanced review from Copilot August 24, 2026 15:40
@pfultz2
pfultz2 marked this pull request as ready for review August 24, 2026 15:40
@pfultz2
pfultz2 requested a review from causten as a code owner August 24, 2026 15:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a simplify_algebra optimization that replaces redundant concatenated broadcasts with one broadcast.

Changes:

  • Adds the find_concat_same_broadcast matcher.
  • 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.

Comment thread src/simplify_algebra.cpp
Comment on lines +1169 to +1172
return all_of(inputs, [&](instruction_ref i) {
auto y = i->inputs().front();
return y == x or *y == *x;
});

@pfultz2 pfultz2 Aug 24, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

Comment thread src/simplify_algebra.cpp
find_concat_conv{},
find_conv_concat_split_fuse{},
find_concat_same_input{},
find_concat_same_broadcast{},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Added a CHANGELOG entry for the find_concat_same_broadcast matcher under the Added section (#5179).

@gh-app-migraphx-bot-pr-write

Copy link
Copy Markdown
Test Batch New Rate (cc735b) Old Rate (3a503c)* Diff Status
torchvision-resnet50 64 3,250.71 3,264.92 -0.44%
torchvision-resnet50_fp16 64 7,878.77 7,548.67 4.37%
torchvision-densenet121 32 1,846.60 2,483.99 -25.66% 🔴
torchvision-densenet121_fp16 32 5,014.86 5,004.24 0.21%
torchvision-inceptionv3 32 2,051.83 2,058.51 -0.32%
torchvision-inceptionv3_fp16 32 4,450.03 4,416.99 0.75%
cadene-inceptionv4 16 817.12 820.61 -0.42%
cadene-resnext64x4 16 785.31 782.78 0.32%
slim-mobilenet 64 8,371.94 8,386.36 -0.17%
slim-nasnetalarge 64 148.53 228.86 -35.10% 🔴
slim-resnet50v2 64 3,214.17 3,180.91 1.05%
bert-mrpc-onnx 8 1,163.41 1,168.84 -0.46%
bert-mrpc-tf 1 501.46 498.63 0.57%
pytorch-examples-wlang-gru 1 369.54 473.35 -21.93% 🔴
pytorch-examples-wlang-lstm 1 721.16 384.83 87.40% 🔆
torchvision-resnet50_1 1 1,047.86 1,046.63 0.12%
cadene-dpn92_1 1 451.55 437.32 3.26%
cadene-resnext101_1 1 364.48 365.89 -0.39%
onnx-taau-downsample 1 844.30 844.09 0.02%
dlrm-criteoterabyte 1 32.26 32.42 -0.51%
dlrm-criteoterabyte_fp16 1 51.47 51.80 -0.63%
agentmodel 1 15,370.19 9,209.12 66.90% 🔆
unet_fp16 2 58.28 58.80 -0.88%
resnet50v1_fp16 1 1,473.51 1,366.11 7.86% 🔆
resnet50v1_int8 1 1,754.41 1,883.96 -6.88% 🔴
bert_base_cased_fp16 64 1,098.86 1,098.16 0.06%
bert_large_uncased_fp16 32 347.18 345.59 0.46%
bert_large_fp16 1 206.46 206.59 -0.06%
distilgpt2_fp16 16 2,100.24 2,092.89 0.35%
yolov5s 1 559.84 558.33 0.27%
tinyllama 1 45.80 45.83 -0.06%
vicuna-fastchat 1 44.24 44.20 0.08%
whisper-tiny-encoder 1 412.91 411.87 0.25%
whisper-tiny-decoder 1 409.53 408.48 0.26%
llama2_7b 1 20.92 20.84 0.37%
qwen1.5-7b 1 23.66 23.58 0.35%
phi3-3.8b 1 28.44 26.72 6.44% 🔆
llama3-8b 1 22.69 21.80 4.07%
whisper-large-encoder 1 10.17 10.18 -0.06%
whisper-large-decoder 1 106.39 105.30 1.03%
mistral-7b 1 23.72 23.78 -0.23%
FLUX.1-schnell 1 786.63 755.22 4.16%

Regressions detected 🔴

* No develop baseline was found for this PR's branch point; compared against the latest available develop run instead.

@gh-app-migraphx-bot-pr-write

Copy link
Copy Markdown
Test Status Result
bert-mrpc-onnx PASSED: MIGraphX meets tolerance
bert-mrpc-tf ERROR - check error output
traceback
Traceback (most recent call last):
File "/src/AMDMIGraphX/tools/accuracy/accuracy_checker.py", line 377, in
main()
File "/src/AMDMIGraphX/tools/accuracy/accuracy_checker.py", line 313, in main
import tensorflow as tf
File "/usr/local/lib/python3.12/dist-packages/tensorflow/init.py", line 40, in
from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow # pylint: disable=unused-import
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 37, in
self_check.preload_check()
File "/usr/local/lib/python3.12/dist-packages/tensorflow/python/platform/self_check.py", line 63, in preload_check
from tensorflow.python.platform import _pywrap_cpu_feature_guard
ImportError: libnuma.so.1: cannot open shared object file: No such file or directory
pytorch-examples-wlang-gru 🔴 FAILED: MIGraphX is not within tolerance - check verbose output
pytorch-examples-wlang-lstm 🔴 FAILED: MIGraphX is not within tolerance - check verbose output
dlrm-criteoterabyte PASSED: MIGraphX meets tolerance
agentmodel PASSED: MIGraphX meets tolerance
unet PASSED: MIGraphX meets tolerance
resnet50v1 PASSED: MIGraphX meets tolerance
bert_base_cased_fp16 PASSED: MIGraphX meets tolerance
bert_large_uncased_fp16 🔴 FAILED: MIGraphX is not within tolerance - check verbose output
bert_large PASSED: MIGraphX meets tolerance
yolov5s PASSED: MIGraphX meets tolerance
tinyllama PASSED: MIGraphX meets tolerance
vicuna-fastchat PASSED: MIGraphX meets tolerance
whisper-tiny-encoder PASSED: MIGraphX meets tolerance
whisper-tiny-decoder PASSED: MIGraphX meets tolerance
distilgpt2_fp16 🔴 FAILED: MIGraphX is not within tolerance - check verbose output
llama2_7b PASSED: MIGraphX meets tolerance
qwen1.5-7b PASSED: MIGraphX meets tolerance
phi3-3.8b PASSED: MIGraphX meets tolerance
llama3-8b PASSED: MIGraphX meets tolerance
whisper-large-encoder PASSED: MIGraphX meets tolerance
whisper-large-decoder PASSED: MIGraphX meets tolerance
mistral-7b PASSED: MIGraphX meets tolerance
FLUX.1-schnell PASSED: MIGraphX meets tolerance

@bdevorem bdevorem left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

needs a changelog entry

Co-authored-by: pfultz2 <1306044+pfultz2@users.noreply.github.com>
Copilot AI requested a review from a team as a code owner August 25, 2026 19:08

@bdevorem bdevorem left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

Comment thread src/simplify_algebra.cpp
Comment on lines +1152 to +1153
if(inputs.empty())
return false;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't think this would ever be reachable. Concat needs at least 1 input.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

That isnt really checked in compute_shape.

Comment thread src/simplify_algebra.cpp Outdated
Co-authored-by: kahmed10 <15948690+kahmed10@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants