Skip to content

Fix incorrect pad indices in AveragePool count_include_pad computation#27375

Merged
titaiwangms merged 3 commits intomicrosoft:mainfrom
chaya2350:fix-averagepool-count-include-pad
Feb 19, 2026
Merged

Fix incorrect pad indices in AveragePool count_include_pad computation#27375
titaiwangms merged 3 commits intomicrosoft:mainfrom
chaya2350:fix-averagepool-count-include-pad

Conversation

@chaya2350
Copy link
Copy Markdown
Contributor

Description

Fixes #26708

AveragePool2DTask and AveragePool3DTask in pool_functors.h used incorrect
pad indices when computing hend (and wend in 3D) for the count_include_pad
divisor calculation.

ONNX pads format for 2D is [h_begin, w_begin, h_end, w_end].
The code was using pads[1] (w_begin) instead of pads[2] (h_end) to clamp hend,
causing the padding region to be excluded from the divisor when
count_include_pad=1 with asymmetric pads (e.g. bottom/right only).

The same issue existed in AveragePool3DTask where pads format is
[h_begin, w_begin, d_begin, h_end, w_end, d_end]:

  • hend used pads[1] instead of pads[3]
  • wend used pads[3] instead of pads[4]

Example

Input [[1,1],[1,1]] with pads=[0,0,1,1], kernel_shape=[2,2], count_include_pad=1:

  • Before (wrong): [[1.0, 0.5], [1.0, 0.5]]
  • After (correct): [[1.0, 0.5], [0.5, 0.25]]

Verified against ONNX ReferenceEvaluator.

Changes

  • pool_functors.h: Fix pad indices in AveragePool2DTask and AveragePool3DTask
  • pool_op_test.cc: Add regression test AveragePool_CountIncludePad_AsymmetricPads

Motivation and Context

When using AveragePool with count_include_pad=1 and asymmetric padding
(e.g. only bottom and right), the average was computed with the wrong divisor,
producing results inconsistent with the ONNX spec.

Regression test for microsoft#26708
AveragePool with count_include_pad=1 and asymmetric pads (only bottom/right)
was using incorrect pad index for hend, producing wrong results.
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

Fixes incorrect padding-index usage in the CPU AveragePool functor implementations so count_include_pad=1 correctly includes asymmetric tail padding in the divisor calculation (aligning results with ONNX).

Changes:

  • Correct pad tail index usage for hend in AveragePool2DTask.
  • Correct pad tail index usage for hend/wend in AveragePool3DTask.
  • Add a 2D regression test covering asymmetric pads with count_include_pad=1.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
onnxruntime/core/providers/cpu/nn/pool_functors.h Fixes incorrect pad tail indices used when clamping window ends for count_include_pad divisor computation in 2D/3D AveragePool tasks.
onnxruntime/test/providers/cpu/nn/pool_op_test.cc Adds a regression test validating correct output for 2D asymmetric pads with count_include_pad=1.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread onnxruntime/test/providers/cpu/nn/pool_op_test.cc
@titaiwangms
Copy link
Copy Markdown
Contributor

/azp run Linux QNN CI Pipeline,Win_TRT_Minimal_CUDA_Test_CI,Windows ARM64 QNN CI Pipeline,Windows GPU Doc Gen CI Pipeline

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 4 pipeline(s).

Copy link
Copy Markdown
Contributor

@titaiwangms titaiwangms left a comment

Choose a reason for hiding this comment

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

Thank you!

@titaiwangms titaiwangms enabled auto-merge (squash) February 19, 2026 21:14
@titaiwangms titaiwangms merged commit 45f493e into microsoft:main Feb 19, 2026
88 checks passed
@chaya2350 chaya2350 deleted the fix-averagepool-count-include-pad branch February 22, 2026 07:26
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] AveragePool broken in version 21

3 participants