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

Merged
merged 7 commits into from
Aug 8, 2025

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 94.75524% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.09%. Comparing base (da23d76) to head (aa6e5de).
⚠️ Report is 3 commits behind head on main.

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 95.07% 4 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2363      +/-   ##
==========================================
+ Coverage   69.81%   70.09%   +0.27%     
==========================================
  Files         209      211       +2     
  Lines       25313    25599     +286     
  Branches     2525     2568      +43     
==========================================
+ Hits        17673    17944     +271     
- Misses       6762     6770       +8     
- Partials      878      885       +7     

☔ 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 ?

@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

@Johansmm Johansmm force-pushed the 2301-fold-pad-into-conv branch from 8fc4326 to aa6e5de Compare August 4, 2025 19:14
@Johansmm
Copy link
Contributor Author

Johansmm commented Aug 4, 2025

Last push with last @justinchuby suggestions

@Johansmm Johansmm requested a review from justinchuby August 4, 2025 19:16
Copy link
Contributor

@titaiwangms titaiwangms left a comment

Choose a reason for hiding this comment

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

LGTM! Thank you! (@justinchuby is on vacation, so feel free to merge it)

@github-project-automation github-project-automation bot moved this from Todo to Done in ONNX Script Review Board Aug 6, 2025
@justinchuby justinchuby merged commit d8ad301 into microsoft:main Aug 8, 2025
25 of 32 checks passed
@Johansmm Johansmm deleted the 2301-fold-pad-into-conv branch August 8, 2025 11:04
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