Skip to content

Fix fuse_pad_into_conv incorrectly fusing when Pad contains negative values#2841

Merged
titaiwangms merged 4 commits into
mainfrom
copilot/fix-negative-pad-values
Jun 3, 2026
Merged

Fix fuse_pad_into_conv incorrectly fusing when Pad contains negative values#2841
titaiwangms merged 4 commits into
mainfrom
copilot/fix-negative-pad-values

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 4, 2026

The fuse_pad_into_conv optimization would fuse a Pad node into Conv even when pads contained negative values (valid for Pad as crop operations, but invalid for Conv's pads attribute), producing an invalid model.

Changes

  • _fuse_pad_into_conv.py: Added a guard in _FuseConvPadBase.check() to bail out when any computed pad value is negative, preventing fusion that would produce an invalid Conv node:
    if any(p < 0 for p in self._pads_list):
        return check_result.fail(f"{pads.name} must not contain negative values.")
  • _fuse_pad_into_conv_test.py: Added a test case to test_unsupported_fuse_pad_into_conv verifying that fusion is rejected when spatial pads are negative.
Original prompt

This section details on the original issue you should resolve

<issue_title>fuse_pad_into_conv optimization incorrectly fuses when 'pads' contain negative values</issue_title>
<issue_description>converting the attached ONNX model with the following script:

from onnxscript import ir
from onnxscript.optimizer import optimize

ir_model = ir.load("input.onnx")
optimized_model = optimize(ir_model)
ir.save(optimized_model, "output.onnx")

results in the following output model:

Image

The Conv layer has negative pad values, which is not allowed per spec. This optimization should check for negative pad values.

input.onnx.zip

versions of relevant packages:

ml_dtypes         0.5.4
onnx              1.20.1
onnx-ir           0.1.16
onnxscript        0.6.2
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix optimization to handle negative pad values in Conv layer Fix fuse_pad_into_conv incorrectly fusing when Pad contains negative values Mar 4, 2026
@bas-aarts
Copy link
Copy Markdown

What is the reason the PR cannot be merged? It's a rathe simple fix.

@justinchuby justinchuby requested a review from Copilot June 1, 2026 16:21
@justinchuby justinchuby marked this pull request as ready for review June 1, 2026 16:22
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

This PR fixes an invalid optimization in the rewriter where Pad was being fused into Conv even when Pad.pads contained negative values (crop semantics), which would produce an invalid Conv node (negative pads are not allowed for Conv).

Changes:

  • Add a guard in _FuseConvPadBase.check() to reject fusion when any computed pad value is negative.
  • Add a regression test ensuring fusion is rejected when spatial pad values are negative.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
onnxscript/rewriter/rules/common/_fuse_pad_into_conv.py Prevents PadConv fusion when computed pads contain negative values, avoiding invalid Conv attributes.
onnxscript/rewriter/rules/common/_fuse_pad_into_conv_test.py Adds coverage for the negative-padding (crop) case to ensure the fusion is correctly rejected.

@titaiwangms titaiwangms self-requested a review June 1, 2026 16:30
@github-project-automation github-project-automation Bot moved this from Todo to Done in ONNX Script Review Board Jun 1, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 1, 2026

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
11842 1 11841 2255
View the top 1 failed test(s) by shortest run time
onnxscript.rewriter.ort_fusions.fused_matmul_rule_sets_test.TestFusedMatmulRules::test_fused_matmul_div_models_2_matmul_div_div
Stack Traces | 0.024s run time
.nox\test\Lib\site-packages\parameterized\parameterized.py:620: in standalone_func
    return func(*(a + p.args), **p.kwargs, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
onnxscript\rewriter\ort_fusions\fused_matmul_rule_sets_test.py:323: in test_fused_matmul_div_models
    self._check_model(model_proto, rewritten_model, atol=1e-6)
onnxscript\rewriter\ort_fusions\fused_matmul_rule_sets_test.py:289: in _check_model
    np.testing.assert_allclose(a, b, atol=atol, rtol=rtol)
E   AssertionError: 
E   Not equal to tolerance rtol=1e-07, atol=1e-06
E   
E   Mismatched elements: 1 / 6 (16.7%)
E   Mismatch at index:
E    [0, 1]: -27.57023048400879 (ACTUAL), -27.570234298706055 (DESIRED)
E   Max absolute difference among violations: 3.8146973e-06
E   Max relative difference among violations: 1.3836289e-07
E    ACTUAL: array([[-10.351939, -27.57023 ,  -5.415182],
E          [-20.712341,  -1.450797,   0.456769]], dtype=float32)
E    DESIRED: array([[-10.35194 , -27.570234,  -5.415183],
E          [-20.712343,  -1.450797,   0.45677 ]], dtype=float32)

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

@titaiwangms titaiwangms enabled auto-merge (squash) June 3, 2026 17:03
@titaiwangms titaiwangms merged commit b16ef16 into main Jun 3, 2026
31 of 33 checks passed
@titaiwangms titaiwangms deleted the copilot/fix-negative-pad-values branch June 3, 2026 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

fuse_pad_into_conv optimization incorrectly fuses when 'pads' contain negative values

5 participants