Skip to content

[Rewriter] Add optimizer to fold Pad operators into Conv #2363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Johansmm
Copy link
Contributor

@Johansmm Johansmm commented Jun 4, 2025

Following (#2301), fuse_pad_into_conv rule set is introduced to reduce the following list of operators:

  • Conv ∘ Pad -> Conv
  • ConvInteger ∘ Pad -> ConvInteger

Additionally, NormalizePadFormat is introduced in order to change auto_pads Conv attribute in its explicit pads list (ref: https://onnx.ai/onnx/operators/onnx__Conv.html).

@Johansmm Johansmm force-pushed the 2301-fold-pad-into-conv branch from 4b9b69b to 19b0418 Compare June 4, 2025 20:35
@Johansmm Johansmm requested a review from justinchuby June 4, 2025 22:09
Copy link

codecov bot commented Jun 4, 2025

Codecov Report

❌ Patch coverage is 95.42254% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.11%. Comparing base (38c4468) to head (8fc4326).

Files with missing lines Patch % Lines
onnxscript/rewriter/fuse_pad_into_conv_test.py 94.44% 4 Missing and 4 partials ⚠️
onnxscript/rewriter/fuse_pad_into_conv.py 96.42% 3 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2363      +/-   ##
==========================================
+ Coverage   69.81%   70.11%   +0.30%     
==========================================
  Files         209      211       +2     
  Lines       25323    25590     +267     
  Branches     2529     2565      +36     
==========================================
+ Hits        17678    17943     +265     
+ Misses       6770     6766       -4     
- Partials      875      881       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Johansmm Johansmm force-pushed the 2301-fold-pad-into-conv branch from 19b0418 to 1604446 Compare June 24, 2025 18:14
@Johansmm
Copy link
Contributor Author

Push force rebasing on main and fixing conflicts.

@Johansmm Johansmm marked this pull request as draft June 24, 2025 19:48
@Johansmm Johansmm requested a review from justinchuby June 24, 2025 21:52
Copy link
Contributor Author

@Johansmm Johansmm left a comment

Choose a reason for hiding this comment

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

@justinchuby I forgot to explain in the previous message that the changes were not ready yet (just fixing rebase with main).
In the last commits I update the code with all the suggestions.
Now this work is ready to be reviewed.

@Johansmm Johansmm marked this pull request as ready for review June 24, 2025 21:55
@justinchuby
Copy link
Collaborator

Could you update the PR title and description? Thanks

@Johansmm Johansmm changed the title 2301 fold pad into conv [Rewriter] Add optimizer to fold Pad operators into Conv Jun 26, 2025
@Johansmm
Copy link
Contributor Author

@justinchuby is ok the new description and title ?

Copilot

This comment was marked as outdated.

@Johansmm Johansmm force-pushed the 2301-fold-pad-into-conv branch from 0b166c1 to 7f7d17e Compare July 5, 2025 16:55
@Johansmm
Copy link
Contributor Author

Johansmm commented Jul 5, 2025

Last push force rebasing on main and addig @justinchuby suggestions

@Johansmm Johansmm requested a review from justinchuby July 5, 2025 16:55
@Johansmm Johansmm force-pushed the 2301-fold-pad-into-conv branch from 7f7d17e to 8fc4326 Compare July 29, 2025 22:02
Copy link
Contributor Author

@Johansmm Johansmm left a comment

Choose a reason for hiding this comment

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

Last push force rebasing on upstream/main and with @gramalingam suggestions

@Johansmm Johansmm requested a review from gramalingam July 29, 2025 22:04
@justinchuby justinchuby requested a review from Copilot July 29, 2025 22:49
Copy link
Contributor

@Copilot 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 introduces a new optimization to fuse Pad operators into subsequent Conv operators to reduce the number of operations in ONNX models. It adds pattern matching rules to convert sequences like Conv(Pad(x)) and ConvInteger(Pad(x)) into optimized Conv(x) and ConvInteger(x) operations respectively.

Key changes include:

  • Implementation of pattern matching and rewriting logic for Pad-Conv fusion
  • Addition of padding format normalization to convert auto_pad attributes to explicit padding lists
  • Integration of the new rule set into the default optimization pipeline

Reviewed Changes

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

File Description
onnxscript/rewriter/fuse_pad_into_conv.py Core implementation with pattern matching classes and rewrite logic for fusing Pad into Conv operations
onnxscript/rewriter/fuse_pad_into_conv_test.py Comprehensive test suite covering fusion scenarios, edge cases, and normalization functionality
onnxscript/rewriter/init.py Integration of the new rule set into the default optimization rules

# Conv constraints: inputs/outputs
input_shape = conv_node.inputs[0].shape
output_shape = conv_node.outputs[0].shape
if len(input_shape) <= 2:
Copy link
Collaborator

Choose a reason for hiding this comment

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

What happens when input_shape/output_shape is None? Do we need to handle it?

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

Successfully merging this pull request may close these issues.

4 participants