microsoft / onnxruntime

ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator
https://onnxruntime.ai
MIT License
14.34k stars 2.87k forks source link

Operator Pad reflect mode does not yield correct results #16401

Open peter249 opened 1 year ago

peter249 commented 1 year ago

Describe the issue

Reflect mode in the Pad operator produces incorrect output as documented in the examples of the Operators.md

Sample PadExamples.onnx file has model to test for all 3 modes (opset 18 as 19 not yet released) for Nuget package

The "reflection_edge_and_wrap_pad" test does works but is 4D (1, 3, 4, 5) not 2D (3, 2) as in the example

Maybe the tests should be updated to catch these 2D bugs

To reproduce

PadExamples.zip

input with input = [ [1.0, 1.2], [2.3, 3.4], [4.5, 5.7], ]

reflect should get = [ [1.0, 1.2, 1.0, 1.2], [2.3, 3.4, 2.3, 3.4], [4.5, 5.7, 4.5, 5.7], ]

reflect gets = [ [0.0, 1.2, 1.0, 1.2] , [0.0, 3.4, 2.3, 3.4] , [0.0, 5.7, 4.5, 5.7], ]

other modes (outputs on model) are correct

Urgency

Bug in Pad reflection when doing examples from documentation (operators.md) - no real urgency -- should be fixed

Platform

Windows

OS Version

Win11

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.15.1 NuGet package

ONNX Runtime API

C#

Architecture

X64

Execution Provider

Default CPU

Execution Provider Library Version

v4.0.30319

liqunfu commented 2 months ago

I experimented this issue with following code in onnx pad.py: @staticmethod def export_reflection_pad_16401() -> None: mode = "reflect" node = onnx.helper.make_node( "Pad", inputs=["x", "pads"], outputs=["y"], mode=mode ) x = np.array( [[1.0, 1.2], [2.3, 3.4], [4.5, 5.7]]).astype(np.float32) pads = np.array([1, 1, 0, 0]).astype( np.int64 ) # pad order [x1_begin, x2_begin, ..., x1_end, x2_end, ...] y = pad_impl(x, pads, mode)

    import pdb; pdb.set_trace()
    expect(node, inputs=[x, pads], outputs=[y], name=f"test_reflection_pad_16401")

ort is producing same outcome as numpy: array([[3.4, 2.3, 3.4], [1.2, 1. , 1.2], [3.4, 2.3, 3.4], [5.7, 4.5, 5.7]], dtype=float32)

The repro zip has dimensions being 1 therefore reflection is not well defined and ort fills pads with 0s.