Skip to content

Fix fuse_pad_into_pool folding zero-padding into MaxPool - #318

Open
take-cheeze wants to merge 1 commit into
onnx:mainfrom
take-cheeze:claude/onnxsim-issue-290-2jtehp
Open

Fix fuse_pad_into_pool folding zero-padding into MaxPool#318
take-cheeze wants to merge 1 commit into
onnx:mainfrom
take-cheeze:claude/onnxsim-issue-290-2jtehp

Conversation

@take-cheeze

Copy link
Copy Markdown
Member

MaxPool ignores its padded elements, which is equivalent to padding with -inf. The pass previously folded a Pad with constant_value=0 (or an unspecified value, which defaults to 0) into MaxPool by moving the padding into the pool's pads attribute. This changes the result whenever a pooling window's real values are all negative: Pad(0)+MaxPool yields 0 while the fused MaxPool yields the (negative) window maximum.

Make the required Pad constant value depend on the pool type:

  • AveragePool (with count_include_pad=1): 0
  • MaxPool: -inf

Also restructure the constant-value check so an unspecified Pad value correctly blocks fusion into MaxPool instead of being treated as a match.

Update the MaxPool tests that encoded the old behavior to assert no fusion for value=0/default, and add companion tests confirming Pad(value=-inf) folds correctly.

Fixes onnxsim/onnxsim#290

Claude-Session: https://claude.ai/code/session_013sB8xKJyd86p47c2vfXMVD

@take-cheeze
take-cheeze requested review from a team as code owners July 24, 2026 08:56
MaxPool ignores its padded elements, which is equivalent to padding with
-inf. The pass previously folded a Pad with constant_value=0 (or an
unspecified value, which defaults to 0) into MaxPool by moving the padding
into the pool's `pads` attribute. This changes the result whenever a
pooling window's real values are all negative: Pad(0)+MaxPool yields 0
while the fused MaxPool yields the (negative) window maximum.

Make the required Pad constant value depend on the pool type:
  - AveragePool (with count_include_pad=1): 0
  - MaxPool:                                -inf

Also restructure the constant-value check so an unspecified Pad value
correctly blocks fusion into MaxPool instead of being treated as a match.

Update the MaxPool tests that encoded the old behavior to assert no fusion
for value=0/default, and add companion tests confirming Pad(value=-inf)
folds correctly.

Fixes onnxsim/onnxsim#290

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013sB8xKJyd86p47c2vfXMVD
Signed-off-by: take-cheeze <takechi101010@gmail.com>
@take-cheeze
take-cheeze force-pushed the claude/onnxsim-issue-290-2jtehp branch from f094c48 to 05adb3d Compare July 24, 2026 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] fold pad(constant_value=0) into maxpool

2 participants