Remove concat->reshapes->slice - #5183
Open
pfultz2 wants to merge 9 commits into
Open
Conversation
pfultz2
requested review from
CharlieL7,
TedThemistokleous,
bdevorem and
kahmed10
and
a balanced review from Copilot
and removed request for
CharlieL7
August 24, 2026 17:46
pfultz2
marked this pull request as ready for review
August 24, 2026 17:48
Contributor
There was a problem hiding this comment.
Pull request overview
Optimizes concat/view/slice chains by forwarding aligned slices directly to concat inputs.
Changes:
- Adds descriptor-based slice-axis tracking, including
reshape_lazy. - Adds the new simplification matcher and broadens KV-cache attention matching.
- Adds descriptor and reshape simplification tests.
Review performed as a single pass without agent fan-out; tests were not executed.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
src/simplify_reshapes.cpp |
Implements concat/view/slice elimination. |
src/shape_transform_descriptor.cpp |
Adds slice-axis mapping and lazy reshape handling. |
src/include/migraphx/shape_transform_descriptor.hpp |
Exposes the slice-axis API. |
src/fuse_attention.cpp |
Relaxes the KV-cache query matcher. |
test/simplify_reshapes_test.cpp |
Tests aligned, misaligned, leading-dimension, and nonstandard cases. |
test/shape_transform_descriptor.cpp |
Tests slicing and reshape_lazy tracking. |
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| auto gemm1 = match::name("dot")(match::arg(0)(queries), match::arg(1)(k_transpose)); | ||
| auto gemm1_maybe_cvt = match::skip(match::name("convert"))(gemm1); | ||
| auto scale = match::name("mul")(match::any_arg(0, 1)(gemm1_maybe_cvt)); | ||
| auto gemm1 = match::name("dot")(match::arg(1)(k_transpose)); |
Collaborator
There was a problem hiding this comment.
it would be a good idea to add a test here
Comment on lines
+857
to
+858
| if(concat_ins->get_shape().dynamic()) | ||
| return; |
Comment on lines
+5953
to
+5955
| // The slices do not align with the segment boundary, so the concat must remain | ||
| EXPECT( | ||
| std::any_of(m1.begin(), m1.end(), [](const auto& ins) { return ins.name() == "concat"; })); |
Regressions detected 🔴 * No develop baseline was found for this PR's branch point; compared against the latest available develop run instead. |
|
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
Models that repack tensors (such as qkv weights) into a concat which an op decomposition then re-slices leave behind a concat->reshapes/transposes->slice chain that copies data at runtime for no reason. When a slice selects exactly one input of the concat, the slice can be rewritten directly on that input with the equivalent view ops. This also allows the kv-cache attention matcher in
fuse_attentionto drop its requirement that the first gemm input be a slice, since the slice is now removed before fusion.Technical Details
find_slice_reshaped_concatmatcher tosimplify_reshapesthat matchesslice(view_ops...(concat)), where the view ops arereshape,reshape_lazy,squeeze,unsqueeze,flatten, ortranspose. Directslice(concat)is left to the existingfind_concat_slice.shape_transform_descriptorrather than strides, so the transformation works with non-standard shapes (such as a layout-preserving concat output) and with non-unit dimensions before the concat axis.shape_transform_descriptor::slice_axis(axis, slice_axes, starts, ends)member that restricts a source axis to the range selected by slicing the output dimensions. The subdimensions of the axis ordered by their split lineage form a mixed-radix decomposition, so the slice is accepted only when it selects one contiguous range[start, end)of that axis alone, which is returned to the caller. Unit subdimensions are renumbered to output order when possible to avoid generating a gratuitous transpose.shape_transform_descriptor::generateapplied to that input.shape_transform_descriptor::applynow handlesreshape_lazythe same asreshape.slice_axismember, andreshape_lazydescriptor tracking.Changelog Category
Add a
CHANGELOG.mdentry for any option other thanNot ApplicableFollow the LLVM AI Tool Use Policy for contributions using AI.