-
Notifications
You must be signed in to change notification settings - Fork 790
[BREAKING] Add asymmetric padding support for conv and pool operations #4263
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
base: main
Are you sure you want to change the base?
Conversation
This commit updates the padding configuration for 1D and 2D convolution and pooling modules to support asymmetric padding. The code now detects asymmetric padding and applies explicit padding using the pad operation before the main convolution or pooling operation. Tests and code generation are updated to handle and verify asymmetric padding cases. Asymmetric 3D padding is not supported and will panic at runtime.
Introduces new ONNX models and Rust/py scripts to test asymmetric padding for AvgPool1d/2d, MaxPool1d/2d, and Conv1d/2d. Updates build.rs to include these models, adds dependency on onnxscript, and extends test modules to verify output shapes and sums for correctness.
There was a problem hiding this 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 adds comprehensive support for asymmetric (per-side) padding in convolution and pooling operations, addressing limitations where only symmetric padding was previously supported. The implementation correctly handles asymmetric padding by applying an explicit pad operation before the convolution/pooling operation.
Key Changes:
- Extended
PaddingConfig1d,PaddingConfig2d, andPaddingConfig3dto support per-side padding values (breaking API change) - Implemented asymmetric padding handling in Conv1d, Conv2d, MaxPool1d, MaxPool2d, AvgPool1d, and AvgPool2d (Conv3d panics with clear error as 3D padding is not yet supported)
- Updated ONNX import pipeline to correctly parse and convert asymmetric padding from ONNX models
- Added comprehensive test coverage including unit tests and ONNX integration tests
Reviewed changes
Copilot reviewed 35 out of 41 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| crates/onnx-ir/src/node/padding.rs | Extended PaddingConfig enums to support per-side padding, removed asymmetric padding restrictions, added helper methods |
| crates/onnx-ir/src/node/{conv,pool}*.rs | Updated tests to match new PaddingConfig API signatures |
| crates/burn-nn/src/padding.rs | Extended PaddingConfig enums, added is_asymmetric() and as_tuple() methods, updated documentation |
| crates/burn-nn/src/modules/conv/*.rs | Implemented asymmetric padding via explicit pad operations for Conv1d/Conv2d, added panic for Conv3d |
| crates/burn-nn/src/modules/pool/*.rs | Implemented asymmetric padding via explicit pad operations with appropriate pad values for each pooling type |
| crates/burn-import/src/burn/codegen.rs | Updated ToTokens implementations to generate code for new PaddingConfig signatures |
| crates/burn-import/src/burn/node/*.rs | Updated codegen for conv and pool nodes to handle asymmetric padding |
| crates/burn-import/onnx-tests/tests/* | Added comprehensive integration tests with PyTorch/ONNX models for asymmetric padding |
| crates/burn-import/onnx-tests/build.rs | Registered new test model files |
| crates/burn-import/onnx-tests/pyproject.toml | Added onnxscript dependency |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Update to use 4-argument format (top, left, bottom, right) for explicit padding.
laggui
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a couple of changes needed, on top of the compilation error due to breaking Explicit(...) enum variant change.
|
I've also been looking into this to support models like Since native support in Instead of changing the runtime
This allows existing Burn versions to run these models without waiting for a breaking release or modifying the core tensor API. I have a branch with the initial code as a PoC. I might need to add the tests but it's pretty ready already |
Here's my commit on my fork. I think this is the path is the one of least resistance and less breaking changes: 258d47b |
|
@khengari77 thanks for looking into this as well. I thought going this route but it only benefits ONNX users and if users want to shift to Burn API then they need to do padding manually. I believe for this release we are okay to introduce a breaking change. |
Actually I think you can create an abstraction over padding that internally decomposes in the same way. Again this will not introduce any breaking changes. Anyways if you see that the other choice is better in the long term then that's something else. Thanks for your response. |
You mean having an #[derive(Config, Debug, PartialEq)]
pub enum PaddingConfig1d {
Same,
Valid,
Explicit(usize), // i.e., symmetric
Asymmetric(usize, usize),
}
#[derive(Config, Debug, PartialEq)]
pub enum PaddingConfig2d {
Same,
Valid,
Explicit(usize, usize), // i.e., symmetric
Asymmetric(usize, usize, usize, usize),
}that's not a bad alternative, but if we go with a separation between explicit symmetric and asymmetric padding I would still rename |
@laggui Quite what I was trying to say. I think you can add |
|
One problem though with So I think having a single |
Address review feedback from @laggui: - Extended ConvOptions with optional `padding_out` field for asymmetric padding - Moved asymmetric padding logic from module forward() to functional conv1d/conv2d - Functional layer now applies explicit pad operation when padding is asymmetric - Module layer uses ConvOptions::with_padding_out() instead of manual padding This ensures users calling functional conv1d/conv2d directly also benefit from asymmetric padding support, not just module users.
Added TODO comments in avg_pool1d, avg_pool2d, max_pool1d, and max_pool2d modules to indicate that handling of asymmetric padding should be moved to the functional level via PoolOptions, referencing issue tracel-ai#4362. Also performed a minor import formatting cleanup in conv1d.rs.
Add calculate_padding_*_pairs() methods that compute asymmetric padding for Same mode when the total padding is odd (e.g., even kernel sizes). This follows ONNX convention where extra padding goes to the end. Changes: - Add calculate_same_padding() helper in padding.rs - Add calculate_padding_1d_pair(), calculate_padding_2d_pairs(), calculate_padding_3d_pairs() methods - Update conv1d, conv2d, and all pool modules to use pairs methods - Remove check_same_padding_support restriction from these modules - Same padding with even kernels now works correctly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 43 out of 49 changed files in this pull request and generated 9 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Update to new 4-argument format: Explicit(top, left, bottom, right)
- Clarify dead_code comment in padding.rs - Update pad dimension comments in module.rs for 1D and 2D - Clarify pad comment in max_pool1d.rs - Update test comments in maxpool and avg_pool tests with ONNX pads format
Resolve file location conflicts for asymmetric padding test files after burn-import/onnx-tests was renamed to burn-onnx/onnx-tests in PR tracel-ai#4361.
|
@laggui, ready for your review; I have updated/merged with burn-onnx rename. |
Codecov Report❌ Patch coverage is ❌ Your project check has failed because the head coverage (68.79%) is below the target coverage (80.00%). You can increase the head coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #4263 +/- ##
==========================================
+ Coverage 68.65% 68.79% +0.14%
==========================================
Files 1411 1411
Lines 168676 169509 +833
==========================================
+ Hits 115804 116620 +816
- Misses 52872 52889 +17 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
laggui
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@laggui, ready for your review; I have updated/merged with burn-onnx rename.
Thanks!
| /// Padding at the end of each dimension (bottom/right for 2D). | ||
| /// If `None`, uses the same values as `padding` (symmetric). | ||
| /// If `Some`, enables asymmetric padding where start != end. | ||
| pub padding_out: Option<[usize; N]>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm not sure this is required to allow asymmetric padding, but I'll have to review this more carefully on Monday because too many changes have been made since my last review comment
Checklist
cargo run-checkscommand has been executed.Related Issues/PRs
Fixes #4253
Fixes #1370
Related #2676
Changes
burn-nn
onnx-ir
burn-import
Testing
Breaking / Migration
PaddingConfig's Explicit variant expanded. For a quick migration of existing code, duplicate each padding value:
Follow-up / left-overs