Skip to content

AveragePool v19+ ignores end padding in computation when count_include_pad=1 #24681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
skywall opened this issue May 7, 2025 · 1 comment

Comments

@skywall
Copy link

skywall commented May 7, 2025

Describe the issue

Following test demonstrates different behavior of AveragePool v11 and v19 with respect to end input padding when it is used with attribute count_include_pad=1. V11 variant seems to work fine, because is correctly includes zero values from both start and end padding in final computation. V19 includes only zeros from start padding.

Issue can be reproduced also on newest nightly - onnxruntime-1.22.0.dev20250505003.

To reproduce

def test_average_pool_issue():
    input_shape = [1, 1, 6, 2]

    node = onnx.helper.make_node("AveragePool", ["x"], ["output"], kernel_shape=[5, 1], auto_pad="SAME_LOWER",
                                 count_include_pad=1)
    graph = onnx.helper.make_graph(
        [node],
        'AveragePool test',
        [onnx.helper.make_tensor_value_info("x", TensorProto.FLOAT, input_shape)],
        [onnx.helper.make_tensor_value_info("output", TensorProto.FLOAT, ())],
    )

    onnx_model = onnx.helper.make_model(
        graph, opset_imports=[onnx.helper.make_operatorsetid("", 19)]
    )

    input_data = np.arange(np.prod(input_shape)).reshape(input_shape).astype(np.float32)
    # input_data = [[[
    #    [0.0, 1.0],
    #    [2.0, 3.0],
    #    [4.0, 5.0],
    #    [6.0, 7.0],
    #    [8.0, 9.0],
    #    [10.0, 11.0]
    # ]]]

    # Input is internally effectively padded to:
    # [[[
    #    [0.0, 0.0],
    #    [0.0, 0.0],
    #    [0.0, 1.0],
    #    [2.0, 3.0],
    #    [4.0, 5.0],
    #    [6.0, 7.0],
    #    [8.0, 9.0],
    #    [10.0, 11.0]
    #    [0.0, 0.0],
    #    [0.0, 0.0],
    # ]]]
    # to satisfy "SAME_LOWER" padding mode.

    model_input = {"x": input_data}

    sess = onnxruntime.InferenceSession(onnx_model.SerializeToString())
    output_data_19 = sess.run(None, model_input)
    # output_data_19 = [[[[
    #    [1.2 1.8],
    #    [2.4 3.2],
    #    [4.  5. ],
    #    [6.  7. ],
    #    [7.  8. ],  <-- incorrect values
    #    [8.  9. ]   <-- incorrect values
    # ]]]]

    onnx_model = onnx.helper.make_model(
        graph, opset_imports=[onnx.helper.make_operatorsetid("", 11)]
    )

    sess = onnxruntime.InferenceSession(onnx_model.SerializeToString())
    output_data_11 = sess.run(None, model_input)
    # output_data_11 = [[[[
    #    [1.2 1.8],
    #    [2.4 3.2],
    #    [4.  5. ],
    #    [6.  7. ],
    #    [5.6 6.4],
    #    [4.8 5.4]
    # ]]]]

    assert np.allclose(output_data_19, output_data_11)

Urgency

No response

Platform

Linux

OS Version

WSL Ubuntu 22.04

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.21.1

ONNX Runtime API

Python

Architecture

X64

Execution Provider

Default CPU

Execution Provider Library Version

No response

@xadupre
Copy link
Member

xadupre commented May 8, 2025

cc @liqunfu

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

No branches or pull requests

2 participants