Skip to content

pnnx: fix Conv2d padding tuple normalization#6694

Open
Yeuvoir wants to merge 3 commits into
Tencent:masterfrom
Yeuvoir:codex/pnnx-conv2d-padding-order
Open

pnnx: fix Conv2d padding tuple normalization#6694
Yeuvoir wants to merge 3 commits into
Tencent:masterfrom
Yeuvoir:codex/pnnx-conv2d-padding-order

Conversation

@Yeuvoir
Copy link
Copy Markdown

@Yeuvoir Yeuvoir commented Apr 26, 2026

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::pad stores 2D pads as (left, right, top, bottom), while nn.Conv2d padding params are (height, width). The previous logic only resized equal 4-element pads, which could preserve the wrong order for per-axis padding such as padding=(1, 2) with padding_mode=reflect/replicate.

Changes:

  • normalize symmetric 4-element 2D pads to (top, left) before storing nn.Conv2d params
  • reuse the normalization for aten::pad, reflection_pad2d, and replication_pad2d
  • add regression coverage for ZeroPad2d((1, 2, 1, 2)) -> depthwise Conv2d(kernel=5, stride=2) -> BatchNorm2d
  • verify the regression keeps pnnx output shape (1, 4, 28, 28), folds BN into Conv2d, and emits ncnn pads left=1, top=1, right=2, bottom=2
  • extend test_nn_Conv2d with padding=(1, 2), padding_mode='reflect'

Verified on Windows with VS2022/v143 Release pnnx build:

Note: direct ctest on Windows fails before running pnnx because the existing test scripts call ../src/pnnx through cmd; I verified the same tests manually with the built pnnx.exe.

@tencent-adm
Copy link
Copy Markdown
Member

tencent-adm commented Apr 26, 2026

CLA assistant check
Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ Yeuvoir
❌ nihui
You have signed the CLA already but the status is still pending? Let us recheck it.

@Yeuvoir Yeuvoir marked this pull request as ready for review April 26, 2026 07:38
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.
@Yeuvoir Yeuvoir force-pushed the codex/pnnx-conv2d-padding-order branch from 00e393e to 02f5a84 Compare April 26, 2026 07:56
@nihui nihui requested a review from Copilot May 7, 2026 11:52
@nihui
Copy link
Copy Markdown
Member

nihui commented May 7, 2026

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

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 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_pad2d into nn.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) with padding_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.

Comment thread tools/pnnx/src/pass_level1/nn_Conv2d.cpp
nihui and others added 2 commits May 7, 2026 20:06
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants