Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions python/tvm/relay/frontend/onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -5095,9 +5095,10 @@ def _impl_v11(cls, inputs, attr, params):
# Onnx round uses Banker's rounding which rounds .5 to the nearest even integer

x = inputs[0]
half = _expr.const(0.5, dtype="float32")
one = _expr.const(1, dtype="float32")
two = _expr.const(2, dtype="float32")
dtype = infer_type(x).checked_type.dtype
half = _expr.const(0.5, dtype=dtype)
one = _expr.const(1, dtype=dtype)
two = _expr.const(2, dtype=dtype)

rounded = _op.ceil(x - half)
bankers_mask = one - (_op.ceil(x + half) - _op.floor(x + half))
Expand Down
29 changes: 23 additions & 6 deletions tests/python/frontend/onnx/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,24 +966,38 @@ def add_noop_to_input_attr(attr_name, attr):


def _test_onnx_op_elementwise(
target, dev, inshape, outfunc, npargs, dtype, opname, kwargs, opset=None
target, dev, inshape, outfunc, npargs, dtype, opname, kwargs, opset=None, verify=True
):
indata = np.random.uniform(-1, 1, size=inshape).astype(dtype)
outdata = outfunc(indata, **npargs)

y = helper.make_node(opname, ["in"], ["out"], **kwargs)

ONNX_DTYPE = mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype(dtype)]

graph = helper.make_graph(
[y],
opname + "_test",
inputs=[helper.make_tensor_value_info("in", TensorProto.FLOAT, list(indata.shape))],
outputs=[helper.make_tensor_value_info("out", TensorProto.FLOAT, list(outdata.shape))],
inputs=[helper.make_tensor_value_info("in", ONNX_DTYPE, list(indata.shape))],
outputs=[helper.make_tensor_value_info("out", ONNX_DTYPE, list(outdata.shape))],
)

model = helper.make_model(graph, producer_name=opname + "_test")
verify_with_ort_with_inputs(
model, [indata], [outdata.shape], opset=opset, dtype=dtype, target=target, dev=dev
)
if verify:
verify_with_ort_with_inputs(
model, [indata], [outdata.shape], opset=opset, dtype=dtype, target=target, dev=dev
)
else:
get_tvm_output(
model,
[indata],
target,
dev,
[outdata.shape],
dtype,
opset=opset,
opt_level=3,
)


@tvm.testing.parametrize_targets
Expand Down Expand Up @@ -1058,6 +1072,9 @@ def test_clip_min_max_as_inputs(target, dev):
@tvm.testing.parametrize_targets
def test_round(target, dev):
_test_onnx_op_elementwise(target, dev, (2, 4, 5, 6), np.round, {}, "float32", "Round", {})
_test_onnx_op_elementwise(
target, dev, (2, 4, 5, 6), np.round, {}, "float64", "Round", {}, verify=False
) # TODO: enable verification once ORT supports float64


def _test_finite_ops(target, dev, inshape, outfunc, npargs, dtype, opname, kwargs):
Expand Down