Skip to content

Preserve shape ops when removing QDQ pairs#5048

Open
ikalinic wants to merge 6 commits into
developfrom
ikalinic/simplify-qdq-preserve-shape-ops-fix
Open

Preserve shape ops when removing QDQ pairs#5048
ikalinic wants to merge 6 commits into
developfrom
ikalinic/simplify-qdq-preserve-shape-ops-fix

Conversation

@ikalinic

@ikalinic ikalinic commented Jul 8, 2026

Copy link
Copy Markdown
Member

Motivation

Fix simplify_qdq so Q/DQ pair removal does not accidentally drop intervening shape operations or produce shape-changing rewrites.

Technical Details

Updated Q/DQ removal to replay supported shape operations when removing non-FP4 fake quantization chains, while continuing to drop FP4 pack/unpack adapter operations where appropriate. Added guards for concat QLinear splitting when sliced scale or zero-point shapes do not match the corresponding concat input. Added a regression test for preserving slice and reshape through Q/DQ removal.

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.

@ikalinic
ikalinic requested review from a team and causten as code owners July 8, 2026 14:22
@codecov

codecov Bot commented Jul 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.76923% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/simplify_qdq.cpp 91.80% 5 Missing ⚠️
src/include/migraphx/instruction_traversal.hpp 75.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #5048      +/-   ##
===========================================
+ Coverage    92.78%   92.81%   +0.03%     
===========================================
  Files          596      597       +1     
  Lines        32011    32164     +153     
===========================================
+ Hits         29700    29851     +151     
- Misses        2311     2313       +2     
Files with missing lines Coverage Δ
src/include/migraphx/instruction_traversal.hpp 87.50% <75.00%> (-12.50%) ⬇️
src/simplify_qdq.cpp 93.98% <91.80%> (-0.63%) ⬇️

... and 12 files with indirect coverage changes

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

Comment thread src/simplify_qdq.cpp Outdated
Comment thread src/simplify_qdq.cpp Outdated
Comment thread src/simplify_qdq.cpp Outdated
Comment thread src/simplify_qdq.cpp Outdated
@gh-app-migraphx-bot-pr-write

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

Copy link
Copy Markdown
Test Batch New Rate (850093) Old Rate (e1e03e) Diff Status
torchvision-resnet50 64 3,137.89 2,693.41 16.50% 🔆
torchvision-resnet50_fp16 64 6,626.64 6,415.71 3.29%
torchvision-densenet121 32 2,694.17 2,702.20 -0.30%
torchvision-densenet121_fp16 32 4,539.49 4,553.39 -0.31%
torchvision-inceptionv3 32 1,795.70 950.94 88.83% 🔆
torchvision-inceptionv3_fp16 32 2,817.92 2,269.97 24.14% 🔆
cadene-inceptionv4 16 822.81 538.17 52.89% 🔆
cadene-resnext64x4 16 783.23 459.28 70.53% 🔆
slim-mobilenet 64 8,378.87 8,429.89 -0.61%
slim-nasnetalarge 64 203.92 89.65 127.47% 🔆
slim-resnet50v2 64 3,161.20 1,563.17 102.23% 🔆
bert-mrpc-onnx 8 844.86 1,165.41 -27.51% 🔴
bert-mrpc-tf 1 174.18 486.44 -64.19% 🔴
pytorch-examples-wlang-gru 1 537.51 487.62 10.23% 🔆
pytorch-examples-wlang-lstm 1 386.12 392.17 -1.54%
torchvision-resnet50_1 1 768.85 771.94 -0.40%
cadene-dpn92_1 1 359.30 442.37 -18.78% 🔴
cadene-resnext101_1 1 365.32 364.35 0.27%
onnx-taau-downsample 1 398.53 400.76 -0.56%
dlrm-criteoterabyte 1 21.90 32.68 -32.96% 🔴
dlrm-criteoterabyte_fp16 1 51.80 52.60 -1.51%
agentmodel 1 9,635.21 9,162.71 5.16% 🔆
unet_fp16 2 23.74 57.35 -58.60% 🔴
resnet50v1_fp16 1 108.75 1,017.75 -89.31% 🔴
resnet50v1_int8 1 189.11 937.30 -79.82% 🔴
bert_base_cased_fp16 64 573.10 1,102.37 -48.01% 🔴
bert_large_uncased_fp16 32 343.72 346.79 -0.88%
bert_large_fp16 1 205.34 203.94 0.69%
distilgpt2_fp16 16 2,093.70 2,094.08 -0.02%
yolov5s 1 557.62 560.76 -0.56%
tinyllama 1 45.80 45.87 -0.17%
vicuna-fastchat 1 44.01 44.20 -0.43%
whisper-tiny-encoder 1 412.56 415.10 -0.61%
whisper-tiny-decoder 1 413.09 409.89 0.78%
llama2_7b 1 20.84 20.96 -0.56%
qwen1.5-7b 1 23.51 23.66 -0.61%
phi3-3.8b 1 26.69 26.82 -0.49%
llama3-8b 1 21.68 21.88 -0.91%
whisper-large-encoder 1 10.18 10.21 -0.25%
whisper-large-decoder 1 106.13 103.92 2.13%
mistral-7b 1 23.61 23.89 -1.16%
FLUX.1-schnell 1 751.25 757.77 -0.86%

Regressions detected 🔴

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

gh-app-migraphx-bot-pr-write Bot commented Jul 8, 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 PASSED: MIGraphX meets tolerance
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

@ikalinic
ikalinic requested a review from pfultz2 July 9, 2026 13:47
@TedThemistokleous
TedThemistokleous self-requested a review July 16, 2026 14:15

@TedThemistokleous TedThemistokleous left a comment

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.

Hey thanks for your contribution!

I have a few suggested changes and some initial questions

inline auto get_input_path(instruction_ref ins)
{
return unfold(ins, [](instruction_ref x) -> std::optional<instruction_ref> {
if(x->inputs().size() != 1)

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.

What if there's multiple inputs?

Is this intending on check for single inputs or non zero non multi inputs. Some ops can have two inputs like gather (calculate shape for indicies and data).

Comment thread src/simplify_qdq.cpp
if(q_pos == input_path.end())
return std::nullopt;
auto is_pack_unpack = [](auto x) {
return contains({"pack_fp4", "unpack_fp4"}, x->name());

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.

Make this a helper instead of a lamda so we can target all pack/unpacks in general.

Do the same for has_pack_unpack below

Comment thread src/simplify_qdq.cpp
if(contains(replay_ops(), x->name()))
{
if(not has_pack_unpack)
ops.push_back(x->get_operator());

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 think we should separate this between "should we add" and "we are adding the replay op" to the list. Is the intent here to check and ensure we have replays that we can then if not just return no replacement?

Comment thread src/simplify_qdq.cpp
if(replacement->get_shape() != dq_ins->get_shape())
return std::nullopt;
return replacement;
}

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.

There's a few code paths here where things just go to nullopt after a bunch of operations. Is there a way to reduce the complexity of this or return earlier by bundling a few of these checks?

Comment thread src/simplify_qdq.cpp
static const std::unordered_set<std::string> ops = {
"pack_fp4", "unpack_fp4", "broadcast", "slice", "reshape", "reshape_lazy", "pad"};
return ops;
}

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.

Not sure why this had to change to a function same with below for replay_ops return this back as a regular static const std:unordered_set for both.

Comment thread src/simplify_qdq.cpp
}
if(not has_pack_unpack)
{
std::reverse(ops.begin(), ops.end());

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.

Not sure why we need to do a reverse here?, also we're inserting new items in the graph with the insert below.

@TedThemistokleous

Copy link
Copy Markdown
Collaborator

Adding Shiv since he's looked a lot of the QDQ stuff

Comment thread src/simplify_qdq.cpp
new_cat_inputs.end(),
replacement_inputs.begin(),
[](const auto& input) { return *input; });
m.replace_instruction(ins, cat_ins->get_operator(), replacement_inputs);

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.

Can you add a regression test for this finder? So its easier to understand what was failing before and is now fixed by this change

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.

4 participants