pnnx: fix Conv2d padding tuple normalization#6694
Conversation
|
|
Normalize 4-element aten pad values from (left, right, top, bottom) to nn.Conv2d padding order (height, width) when fusing Conv2d modules. Also add regression coverage for asymmetric ZeroPad2d + depthwise Conv2d to keep the output shape and ncnn pad params correct.
00e393e to
02f5a84
Compare
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
There was a problem hiding this comment.
Pull request overview
This PR addresses incorrect padding tuple normalization when fusing PyTorch Conv2d with non-zero padding_mode in pnnx, specifically fixing the (left,right,top,bottom) vs (height,width) ordering mismatch that can break asymmetric-per-axis padding cases. It also adds regression coverage for the asymmetric pad → depthwise conv → batchnorm fusion scenario described in #6463.
Changes:
- Normalize 4-element 2D pad lists from
aten::pad/reflection_pad2d/replication_pad2dintonn.Conv2d’s 2-tuple(height, width)form when symmetric per axis. - Reuse the same normalization logic across the relevant padding-node cases in the Conv2d module fusion pass.
- Add/extend tests to cover asymmetric padding fusion and
padding=(1,2)withpadding_mode='reflect'.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tools/pnnx/src/pass_level1/nn_Conv2d.cpp | Adds a shared helper to normalize 4-element 2D pads into Conv2d padding parameters during module fusion. |
| tools/pnnx/tests/test_pnnx_fuse_asymmetric_pad_conv2d.py | New regression test for asymmetric ZeroPad2d → depthwise Conv2d(stride=2,k=5) → BatchNorm2d, including param checks. |
| tools/pnnx/tests/test_nn_Conv2d.py | Extends Conv2d test coverage with padding=(1,2) and padding_mode='reflect'. |
| tools/pnnx/tests/CMakeLists.txt | Registers the new asymmetric pad/conv2d fuse regression test in the test suite. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Related to #6463
This PR fixes Conv2d module padding normalization in pnnx and adds regression coverage for the asymmetric padding chain discussed in #6463.
TorchScript
aten::padstores 2D pads as(left, right, top, bottom), whilenn.Conv2dpadding params are(height, width). The previous logic only resized equal 4-element pads, which could preserve the wrong order for per-axis padding such aspadding=(1, 2)withpadding_mode=reflect/replicate.Changes:
(top, left)before storingnn.Conv2dparamsaten::pad,reflection_pad2d, andreplication_pad2dZeroPad2d((1, 2, 1, 2)) -> depthwise Conv2d(kernel=5, stride=2) -> BatchNorm2d(1, 4, 28, 28), folds BN into Conv2d, and emits ncnn padsleft=1, top=1, right=2, bottom=2test_nn_Conv2dwithpadding=(1, 2), padding_mode='reflect'Verified on Windows with VS2022/v143 Release pnnx build:
test_nn_Conv2d: passedtest_pnnx_fuse_asymmetric_pad_conv2dwith BatchNorm2d: passed(1, 144, 28, 28), pnnx allclose passes, and ncnn param keeps padsleft=1, top=1, right=2, bottom=2Note: direct
cteston Windows fails before running pnnx because the existing test scripts call../src/pnnxthroughcmd; I verified the same tests manually with the builtpnnx.exe.