Skip to content

count_include_pad calculates the the most right output with dividing it by kernel size regardless it's not padded. #16203

@titaiwangms

Description

@titaiwangms

Describe the issue

From onnx/onnx#5276
count_include_pad calculates the most right output with dividing it by kernel size regardless it's not padded. I think it might be a bug that the original intention should be divided by the number of remaining pixels (including padding).

To reproduce

I am sorry, but it's easier to repro with onnx-script eager mode (backend ORT)

import numpy as np
import onnxscript
from onnxscript.onnx_opset import opset18 as op

x = np.array([[[ 2.0903,  4.6493,  1.6320, -3.2051,  4.6975,  4.7296,  3.3653,-1.5815, -2.3832],
               [ 0.9628, -1.5899, -2.6820,  5.7529,  7.7346, -0.8910, -2.0151,0.1313, -0.5374]]]).astype(np.float32)


@onnxscript.script(default_opset=op)
def avg_pool(x):
    result = op.AveragePool(x, kernel_shape=[7], strides=[3], pads=[3,3], ceil_mode=True, count_include_pad=True)
    return result

print(avg_pool(x))
# [[[ 0.73807144  2.5655572   0.8032287  -0.08562858]
#   [ 0.34911433  1.0389      1.4536142  -0.34588572]]]

In PyTorch, it is divided by the number of remaining pixels (including padding):

import torch

a = torch.tensor([[[ 2.0903,  4.6493,  1.6320, -3.2051,  4.6975,  4.7296,  3.3653,-1.5815, -2.3832],[ 0.9628, -1.5899, -2.6820,  5.7529,  7.7346, -0.8910, -2.0151,0.1313, -0.5374]]])
p = torch.nn.AvgPool1d((7,), (3,), (3,), ceil_mode=True, count_include_pad=True)
p(a)

# tensor([[[ 0.7381,  2.5656,  0.8032, -0.0999],
#               [ 0.3491,  1.0389,  1.4536, -0.4035]]])

To summary, we can take a specific looke on the output of the first row. The reason that they are mis matched is because they are divided by different number of remaing pixels:

In the last window slide,
PyTorch: sum([3.3653,-1.5815, -2.3832, 0, 0, 0]) / 6 = -0.0999
ORT: sum([3.3653,-1.5815, -2.3832, 0, 0, 0]) / 7 = -0.08562858

Urgency

No response

Platform

Linux

OS Version

20.04.5

ONNX Runtime Installation

Built from Source

ONNX Runtime Version or Commit ID

NIghtly

ONNX Runtime API

Python

Architecture

X64

Execution Provider

Default CPU

Execution Provider Library Version

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    converter:dynamoissues related supporting the PyTorch Dynamo exportercore runtimeissues related to core runtime

    Type

    No type

    Projects

    Status

    Filed Bugs

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions