Skip to content
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

Pad behavior is inconsistent with ONNX ReferenceEvaluator and documentation #21037

Open
vincentme opened this issue May 24, 2024 · 2 comments
Open
Assignees
Labels
converter:dynamo issues related supporting the PyTorch Dynamo exporter core runtime issues related to core runtime

Comments

@vincentme
Copy link

behavior

In mode 'reflect', the result from onnxscript and onnxruntime is inconsistent with onnx.reference.ReferenceEvaluator, onnx documentation, and numpy.pad.

In the eager mode of onnxscript (res_onnxscript2), the computation is indeed conduct by onnxruntime, right? But is the res_onnxscript1 from onnxscript evaluator? I don't know much about onnxscript internal. But the result from onnxruntime is defenitely inconsistent, so I also created an issue there.

code to reproduce

import numpy as np
from onnxscript import script, FLOAT, opset20 as op
import onnx

data = np.array([
    [1.0, 1.2],
    [2.3, 3.4],
    [4.5, 5.7],
], dtype = np.float32)

mode = 'reflect'

res_np = np.pad(data, ((0, 0), (2, 0)), mode = mode)
res_onnxscript1 = op.Pad(data, pads = [0, 2, 0, 0], mode = mode)

@script()
def test(data: FLOAT[None, None]) -> FLOAT[None, None]:
    return op.Pad(data, pads = [0, 2, 0, 0], mode = mode)
res_onnxscript2 = test(data)

model = test.to_model_proto()
feeds = {'data': data,}

from onnx.reference import ReferenceEvaluator
sess = ReferenceEvaluator(model)
outpus_onnx = sess.run(None, feeds)

from onnxruntime import InferenceSession
sess = InferenceSession(model.SerializeToString())
outpus_onnxruntime = sess.run(None, feeds)

print(f'numpy:\n{res_np}\n')
print(f'onnxscript evaluator:\n{res_onnxscript1}\n')
print(f'onnxscript evaluator (called onnxruntime?):\n{res_onnxscript2}\n')
print(f'onnx ReferenceEvaluator:\n{outpus_onnx[0]}\n')
print(f'onnxruntime:\n{outpus_onnxruntime[0]}\n')

print(onnx.printer.to_text(model))

result

numpy:
[[1.  1.2 1.  1.2]
 [2.3 3.4 2.3 3.4]
 [4.5 5.7 4.5 5.7]]

onnxscript evaluator:
Tensor(array([[0. , 1.2, 1. , 1.2],
       [0. , 3.4, 2.3, 3.4],
       [0. , 5.7, 4.5, 5.7]], dtype=float32))

onnxscript evaluator (called onnxruntime?):
[[0.  1.2 1.  1.2]
 [0.  3.4 2.3 3.4]
 [0.  5.7 4.5 5.7]]

onnx ReferenceEvaluator:
[[1.  1.2 1.  1.2]
 [2.3 3.4 2.3 3.4]
 [4.5 5.7 4.5 5.7]]

onnxruntime:
[[0.  1.2 1.  1.2]
 [0.  3.4 2.3 3.4]
 [0.  5.7 4.5 5.7]]

<
   ir_version: 9,
   opset_import: ["" : 20]
>
test (float[?,?] data) => (float[?,?] return_val) {
   const = Constant <value: tensor = int64[4] const {0,2,0,0}> ()
   return_val = Pad <mode: string = "reflect"> (data, const)
}
@vincentme vincentme changed the title op.Pad havavior inconsistent with onnx ReferenceEvaluator and doctumentation op.Pad havavior inconsistent with onnx ReferenceEvaluator and documentation May 24, 2024
@vincentme vincentme changed the title op.Pad havavior inconsistent with onnx ReferenceEvaluator and documentation op.Pad behavior is inconsistent with onnx ReferenceEvaluator and documentation May 24, 2024
@justinchuby
Copy link
Contributor

Looks like ONNX Runtime is not following the spec to me. @gramalingam does it look like it for you?

@gramalingam
Copy link
Contributor

Yes, it looks like a bug in onnxruntime.

@justinchuby justinchuby transferred this issue from microsoft/onnxscript Jun 13, 2024
@justinchuby justinchuby changed the title op.Pad behavior is inconsistent with onnx ReferenceEvaluator and documentation Pad behavior is inconsistent with ONNX ReferenceEvaluator and documentation Jun 13, 2024
@justinchuby justinchuby added core runtime issues related to core runtime converter:dynamo issues related supporting the PyTorch Dynamo exporter labels Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
converter:dynamo issues related supporting the PyTorch Dynamo exporter core runtime issues related to core runtime
Projects
None yet
Development

No branches or pull requests

3 participants