-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Open
Description
in https://github.com/onnx/onnx/blob/main/docs/Operators.md#Pad , example mode=reflect as belows
data = [
[1.0, 1.2],
[2.3, 3.4],
[4.5, 5.7],
]
pads = [0, 2, 0, 0]
mode = 'reflect'
output = [
[1.0, 1.2, 1.0, 1.2],
[2.3, 3.4, 2.3, 3.4],
[4.5, 5.7, 4.5, 5.7],
]
it's strange, I test with code and shows different results and the same behaivor in pytorch got error:
(3, 2)
(3, 4)
ori
[[1. 1.2]
[2.3 3.4]
[4.5 5.7]]
reflect pad
[[0. 1.2 1. 1.2]
[0. 3.4 2.3 3.4]
[0. 5.7 4.5 5.7]]
torch.Size([3, 2])
Traceback (most recent call last):
File "/data/pengyancao/workspace/npu-codebase/cache/t_ort.py", line 41, in <module>
ty = torch.nn.functional.pad(x, pad=[2,0],mode="reflect")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/torch/nn/functional.py", line 5290, in pad
return torch._C._nn.pad(input, pad, mode, value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Argument #4: Padding size should be less than the corresponding input dimension, but got: padding (2, 0) at dimension 1 of input [3, 2]
test code:
import onnxruntime as ort
import onnxruntime
import onnx
import numpy as np
from onnx.onnx_pb import TensorProto
import torch
op = onnx.OperatorSetIdProto()
op.version = 11
opsets = [op]
x_type = onnx.helper.make_tensor_type_proto(TensorProto.FLOAT, None)
y_type = onnx.helper.make_tensor_type_proto(TensorProto.FLOAT, None)
inp = onnx.helper.make_value_info("x", x_type)
outp = onnx.helper.make_value_info("y", y_type)
pads = np.array([0,2,0,0])
pads = onnx.numpy_helper.from_array(pads, "pad")
node = onnx.helper.make_node("Pad", ["x", "pad"], ["y"], "op1", mode="reflect")
graph = onnx.helper.make_graph([node],"g1", [inp], [outp], [pads])
model = onnx.helper.make_model(graph, ir_version=6, opset_imports=opsets)
# onnx.checker.check_model(model)
onnx.save_model(model, "cache/t_reflex_pad.onnx")
session = ort.InferenceSession("cache/t_reflex_pad.onnx")
x = np.array([
[1.0, 1.2],
[ 2.3, 3.4],
[ 4.5, 5.7],
]).astype(np.float32)
print(x.shape)
y = session.run(None, {"x": x})
print(y[0].shape)
print("ori \n", x)
print("reflect pad \n",y[0])
x = torch.from_numpy(x)
print(x.shape)
ty = torch.nn.functional.pad(x, pad=[2,0],mode="reflect")
print(ty.shape)
print("reflect pad torch \n",ty)
Metadata
Metadata
Assignees
Labels
No labels