Skip to content

pnnx ncnn legalize global pooling layout#6795

Merged
nihui merged 6 commits into
Tencent:masterfrom
nihui:pnnx-ncnn-gp-chain
Jul 3, 2026
Merged

pnnx ncnn legalize global pooling layout#6795
nihui merged 6 commits into
Tencent:masterfrom
nihui:pnnx-ncnn-gp-chain

Conversation

@nihui

@nihui nihui commented Jul 3, 2026

Copy link
Copy Markdown
Member

No description provided.

@tencent-adm

Copy link
Copy Markdown
Member

CLA assistant check
Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

nihui commented Jul 3, 2026

Copy link
Copy Markdown
Member Author

Supplementary notes for this PR:

This PR changes pnnx ncnn conversion around global pooling layout, without changing ncnn runtime Pooling behavior.

Design split:

  • insert_reshape_numpy_binaryop_broadcast still runs first and keeps torch/numpy broadcast semantics explicit. This is important for cases like [4] * [4,5,4], which is last-dim torch broadcast and must not be treated as ncnn channel broadcast.
  • legalize_global_pooling_layout then rewrites only proven global-pooling outputs into ncnn compact vector shape in Operand::shape:
    • [N,C,1,1] / [N,C,1,1,1] -> [N,C]
    • [C,1,1] / [C,1,1,1] -> [C]
      No extra layout metadata is introduced.
  • Global-pool branches stay compact through activation, Linear, and valid Conv2d 1x1 groups=1. Such convs are converted to nn.Linear, with weight shape changed from [out,in,1,1] to [out,in].
  • Rank-sensitive consumers such as permute, slice, non-vector reshape, depthwise/grouped/non-1x1 conv, and model output get a restore Tensor.reshape only at the boundary where torch rank is needed.
  • A later eliminate_reshape_binaryop_broadcast pass runs after ncnn layer rewriting. It only removes no-batch channel broadcast adapters that are already equivalent under ncnn BinaryOp, such as [C] -> [C,1,1] before [C,H,W]. It intentionally keeps last-dim broadcast adapters like [4] -> [1,1,4], and also keeps explicit batched model-input adapters [N,C] -> [N,C,1,1] for now.

Expected generated graph examples:

  • classifier head: Pooling -> InnerProduct
  • SE-style branch: Pooling -> InnerProduct -> activation -> InnerProduct -> sigmoid -> BinaryOp
  • rank-sensitive fallback: Pooling -> Reshape -> Permute/Slice/Convolution...

Tests added/updated:

  • test_ncnn_global_pooling_layout.py
    • direct global-pool output
    • no-batch global-pool output
    • avg/max global-pool -> flatten -> linear
    • 3D global-pool -> linear
    • F.linear
    • SE conv1x1 chain and BinaryOp broadcast
    • permute/slice/reshape fallback
    • depthwise and non-1x1 conv fallback
    • conversion without explicit inputshape
  • test_ncnn_binaryop_broadcast_cleanup.py
    • removable no-batch channel adapter
    • non-removable last-dim broadcast adapter
    • non-removable explicit batched adapter
  • test_ncnn_numpy_binaryop_broadcast.py
    • adds [4] with [4,5,4] regression coverage

Focused validation run locally:

cd tools/pnnx/build
make -j16 pnnx
ctest --output-on-failure -R "AdaptiveAvgPool|AdaptiveMaxPool|Linear|Conv2d|global_pooling|binaryop_broadcast_cleanup|numpy_binaryop_broadcast|batch_layout|solve_batch_index"

Result: 21/21 passed.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 05fbcc10ba

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

Comment thread tools/pnnx/src/pass_ncnn/legalize_global_pooling_layout.cpp Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2ee2f629e6

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

Comment thread tools/pnnx/src/pass_ncnn/legalize_global_pooling_layout.cpp Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fb079900f4

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

Comment thread tools/pnnx/src/pass_ncnn/eliminate_reshape_binaryop_broadcast.cpp
@nihui

nihui commented Jul 3, 2026

Copy link
Copy Markdown
Member Author

link #6777

@nihui
nihui merged commit b10a832 into Tencent:master Jul 3, 2026
26 of 27 checks passed
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.

2 participants