Skip to content

Lower reshape after eliminate_contiguous in a single pass - #5105

Open
ivarusic-amd wants to merge 13 commits into
developfrom
wip/lower-reshape-review
Open

Lower reshape after eliminate_contiguous in a single pass#5105
ivarusic-amd wants to merge 13 commits into
developfrom
wip/lower-reshape-review

Conversation

@ivarusic-amd

@ivarusic-amd ivarusic-amd commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Motivation

reshape lowering is currently split across three passes:

  1. lowering (add_reshape_lazy_op) expands every reshape to
    gpu::contiguous -> reshape_lazy -> gpu::contiguous, unconditionally.
  2. eliminate_contiguous deletes the redundant copies.
  3. propagate_reshape_layout rewrites any surviving standardizing contiguous into a
    layout to recover the discarded permutation.

Step 1 runs before eliminate_contiguous, so it cannot know the reshape's final input
layout. Steps 2 and 3 exist to undo its guess.

Technical Details

Defer lowering until after eliminate_contiguous and do it in one pass.

  • Remove add_reshape_lazy_op from lowering.cpp; reshape passes through untouched.
  • Remove propagate_reshape_layout (pass, header, test).
  • Add lower_reshape in the same pipeline slot (target.cpp: after
    eliminate_contiguous + dead_code_elimination, before adjust_allocation).

Per reshape, in order:

  1. reshape_dims(input, output_dims, {.lazy = true}) succeeds and == expected shape ->
    emit reshape_lazy only. Equality is required because reshape_dims can succeed with
    different strides than op::reshape::compute_shape produced.
  2. Else reshape_dims(output, input_dims, {.lazy = true}) -> find_permutation ->
    layout; verify forward that reshape_dims(layout_shape, output_dims, {.lazy = true}) == expected. Emits layout under gpu::precompile_op (one pointwise copy kernel via
    pointwise_compiler) + reshape_lazy. Forward check is required; the backwards
    derivation is ambiguous for singleton dims.
  3. Else gpu::contiguous + reshape_lazy.

The trailing gpu::contiguous is gone, so the result is no longer forced to standard;
non-standard views now propagate further downstream than they did before.

Two-input reshape now throws. It lowered to gpu::contiguous(data, out_buffer), but
gpu::contiguous::compute_shape uses inputs().at(0) only, so
reshape(x{2,3,4}, out{6,4}) reported {2,3,4} - and replace_instruction always
recomputes shape, so it is not overridable. No GPU copy op expresses a rank-changing copy;
all derive the kernel from one index space shared by src and dst, including hip::copy
("Ranks must be the same" dynamic, reorder_dims' rank assert static). Not a regression:
develop also fails here via a 3-arg gpu::contiguous tripping check_shapes.has(2).
simplify_dyn_ops already folds compile-time-knowable targets into the one-input form.

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.

ivarusic-amd and others added 2 commits July 30, 2026 03:57
Lower reshapes after contiguous elimination so layout decisions are validated locally and invalid lazy views fall back to safe copies.

Co-authored-by: Cursor <cursoragent@cursor.com>
… a gpu::contiguous that reports the wrong shape
@ivarusic-amd ivarusic-amd changed the title Wip/lower reshape review Wip/lower reshape draft Jul 30, 2026
@TedThemistokleous

Copy link
Copy Markdown
Collaborator

@ivarusic-amd what model are we seeing this in? Just message me in teams

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

gh-app-migraphx-bot-pr-write Bot commented Jul 30, 2026

Copy link
Copy Markdown
Test Batch New Rate (7c6f58) Old Rate (3a503c)* Diff Status
torchvision-resnet50 64 3,327.82 3,264.92 1.93%
torchvision-resnet50_fp16 64 7,882.80 7,548.67 4.43%
torchvision-densenet121 32 2,489.88 2,483.99 0.24%
torchvision-densenet121_fp16 32 5,037.87 5,004.24 0.67%
torchvision-inceptionv3 32 2,077.00 2,058.51 0.90%
torchvision-inceptionv3_fp16 32 4,495.52 4,416.99 1.78%
cadene-inceptionv4 16 820.89 820.61 0.03%
cadene-resnext64x4 16 783.23 782.78 0.06%
slim-mobilenet 64 8,391.30 8,386.36 0.06%
slim-nasnetalarge 64 224.96 228.86 -1.70%
slim-resnet50v2 64 3,238.47 3,180.91 1.81%
bert-mrpc-onnx 8 1,168.44 1,168.84 -0.03%
bert-mrpc-tf 1 498.24 498.63 -0.08%
pytorch-examples-wlang-gru 1 486.54 473.35 2.79%
pytorch-examples-wlang-lstm 1 387.99 384.83 0.82%
torchvision-resnet50_1 1 1,053.39 1,046.63 0.65%
cadene-dpn92_1 1 448.01 437.32 2.45%
cadene-resnext101_1 1 365.48 365.89 -0.11%
onnx-taau-downsample 1 845.13 844.09 0.12%
dlrm-criteoterabyte 1 32.17 32.42 -0.79%
dlrm-criteoterabyte_fp16 1 51.84 51.80 0.08%
agentmodel 1 8,857.79 9,209.12 -3.82%
unet_fp16 2 58.63 58.80 -0.29%
resnet50v1_fp16 1 1,416.93 1,366.11 3.72%
resnet50v1_int8 1 1,759.27 1,883.96 -6.62% 🔴
bert_base_cased_fp16 64 1,094.77 1,098.16 -0.31%
bert_large_uncased_fp16 32 345.70 345.59 0.03%
bert_large_fp16 1 205.04 206.59 -0.75%
distilgpt2_fp16 16 2,091.68 2,092.89 -0.06%
yolov5s 1 566.69 558.33 1.50%
tinyllama 1 45.82 45.83 -0.03%
vicuna-fastchat 1 44.18 44.20 -0.04%
whisper-tiny-encoder 1 412.55 411.87 0.16%
whisper-tiny-decoder 1 406.73 408.48 -0.43%
llama2_7b 1 20.85 20.84 0.07%
qwen1.5-7b 1 23.59 23.58 0.06%
phi3-3.8b 1 26.81 26.72 0.34%
llama3-8b 1 21.75 21.80 -0.25%
whisper-large-encoder 1 10.13 10.18 -0.43%
whisper-large-decoder 1 107.31 105.30 1.90%
mistral-7b 1 23.68 23.78 -0.41%
FLUX.1-schnell 1 771.41 755.22 2.14%

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

gh-app-migraphx-bot-pr-write Bot commented Jul 30, 2026

Copy link
Copy Markdown
Test Status Result
bert-mrpc-onnx PASSED: MIGraphX meets tolerance
bert-mrpc-tf PASSED: MIGraphX meets tolerance
pytorch-examples-wlang-gru PASSED: MIGraphX meets tolerance
pytorch-examples-wlang-lstm PASSED: MIGraphX meets tolerance
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

@ivarusic-amd ivarusic-amd changed the title Wip/lower reshape draft Lower reshape after eliminate_contiguous in a single pass Jul 31, 2026
@ivarusic-amd
ivarusic-amd force-pushed the wip/lower-reshape-review branch from 148a931 to 1ab2a09 Compare July 31, 2026 11:53
ivarusic-amd and others added 2 commits July 31, 2026 05:35
@ivarusic-amd

Copy link
Copy Markdown
Contributor Author

@ivarusic-amd what model are we seeing this in? Just message me in teams

Affecting
models82:dc-ae:encoder
models82:dc-ae:encoder-fp16

@ivarusic-amd ivarusic-amd added the Windows Related changes for Windows Environments label Jul 31, 2026
@ivarusic-amd
ivarusic-amd marked this pull request as ready for review July 31, 2026 13:02
@ivarusic-amd
ivarusic-amd requested a review from causten as a code owner July 31, 2026 13:02
Comment thread src/targets/gpu/lower_reshape.cpp Outdated

struct find_reshape : match::supports_dynamic_shapes
{
auto matcher() const { return match::name("reshape"); }

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.

This should check there is only one argument: match::name("reshape")(match::nargs(1))

Comment thread src/targets/gpu/lower_reshape.cpp Outdated
// the wrong shape or fails in the copy itself. Reject it explicitly rather than
// lowering to something that silently computes the wrong result.
if(ins->inputs().size() == 2)
MIGRAPHX_THROW("lower_reshape: reshape with a runtime output buffer (2 input form) is "

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.

Dont throw an error for this case. We could add a later pass to handle it.

Comment thread src/targets/gpu/lower_reshape.cpp Outdated
auto expected = ins->get_shape().to_symbolic();
auto output_dims = ins->get_shape().sym_dims();
auto reshaped = reshape_dims(s.to_symbolic(), output_dims, {.lazy = true});
if(reshaped and *reshaped == expected)

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.

This should use same_symbol with number elements:

Suggested change
if(reshaped and *reshaped == expected)
if(reshaped and same_symbol(reshaped->sym_elements(), expected.sym_elements())

Comment thread test/gpu/lower_reshape.cpp Outdated

auto result = std::prev(m.end())->inputs().front();
EXPECT(result->name() == "reshape_lazy");
EXPECT(result->inputs().front()->name() == "gpu::contiguous");

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.

Tests should build the expected module.

Comment thread src/targets/gpu/lower_reshape.cpp Outdated
namespace gpu {

namespace {
instruction_ref insert_copy(module& m,

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.

This should be named insert_precompile_op.

Comment thread src/targets/gpu/lower_reshape.cpp Outdated
instruction_ref pos,
instruction_ref input,
const operation& op,
const shape& output_shape)

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.

Remove this parameter, this can be computed from the op and input.

Comment thread src/targets/gpu/lower_reshape.cpp Outdated
pos, make_op("gpu::precompile_op", {{"op", to_value(op)}}), input, alloc);
}

instruction_ref insert_standard_copy(module& m, instruction_ref pos, instruction_ref input)

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.

This should be named insert_contiguous.

Comment thread src/targets/gpu/lower_reshape.cpp Outdated
Comment on lines +105 to +108
auto layout_op = make_op("layout", {{"permutation", perm}});
auto layout_shape = layout_op.compute_shape({s});
auto layout_reshape = reshape_dims(layout_shape, output_dims, {.lazy = true});
if(layout_reshape and *layout_reshape == expected)

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.

This should be removed. The reshape_dims or reshape_lazy should not fail here.

Suggested change
auto layout_op = make_op("layout", {{"permutation", perm}});
auto layout_shape = layout_op.compute_shape({s});
auto layout_reshape = reshape_dims(layout_shape, output_dims, {.lazy = true});
if(layout_reshape and *layout_reshape == expected)

@pfultz2
pfultz2 requested a review from shivadbhavsar July 31, 2026 16:22
@pfultz2

pfultz2 commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

I would like @shivadbhavsar to review this as well to make sure the symbolic things are correct.

@ivarusic-amd

Copy link
Copy Markdown
Contributor Author

Comments will be addressed by wensday

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

Labels

Windows Related changes for Windows Environments

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants