diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index 4c9996bc855a..45457fd6c58c 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -2046,7 +2046,7 @@ def _impl_v1(cls, inputs, attr, params): x = inputs[0] rois = inputs[1] batch_indices = inputs[2] - mode = attr.get("mode", "avg") + mode = attr.get("mode", b"avg") if mode != b"avg": raise ValueError("RoiAlign in Relay only uses avg mode") output_height = attr.get("output_height", 1) @@ -2056,7 +2056,7 @@ def _impl_v1(cls, inputs, attr, params): spatial_scale = attr.get("spatial_scale", 1.0) batch_indices = _op.expand_dims(batch_indices, axis=1, num_newaxis=1) - batch_indices = _op.cast(batch_indices, infer_type(rois).type_annotation.dtype) + batch_indices = _op.cast(batch_indices, infer_type(rois).checked_type.dtype) rois = _op.concatenate([batch_indices, rois], 1) return _vision.roi_align( @@ -2074,6 +2074,10 @@ def convert_attributes(inputs, attr, params): @classmethod def _impl_v1(cls, inputs, attr, params): + if "min" not in attr: + attr["min"] = -np.inf + if "max" not in attr: + attr["max"] = np.inf return Clip.convert_attributes(inputs, attr, params) @classmethod diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index df35a7e9bb56..96be6fba113a 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/test_forward.py @@ -840,7 +840,7 @@ def test_slice(): ) -def _test_onnx_op_elementwise(inshape, outfunc, npargs, dtype, opname, kwargs): +def _test_onnx_op_elementwise(inshape, outfunc, npargs, dtype, opname, kwargs, opset=None): indata = np.random.uniform(-1, 1, size=inshape).astype(dtype) outdata = outfunc(indata, **npargs) @@ -856,7 +856,7 @@ def _test_onnx_op_elementwise(inshape, outfunc, npargs, dtype, opname, kwargs): model = helper.make_model(graph, producer_name=opname + "_test") for target, ctx in tvm.testing.enabled_targets(): - tvm_out = get_tvm_output(model, indata, target, ctx, outdata.shape, dtype) + tvm_out = get_tvm_output(model, indata, target, ctx, outdata.shape, dtype, opset=opset) tvm.testing.assert_allclose(outdata, tvm_out) @@ -881,6 +881,26 @@ def test_clip(): {"min": -1.0, "max": 1.0}, ) + _test_onnx_op_elementwise( + (2, 4, 5, 6), + np.clip, + {"a_min": -np.inf, "a_max": 1.0}, + "float32", + "Clip", + {"max": 1.0}, + opset=1, + ) + + _test_onnx_op_elementwise( + (2, 4, 5, 6), + np.clip, + {"a_min": -1.0, "a_max": np.inf}, + "float32", + "Clip", + {"min": -1.0}, + opset=1, + ) + @tvm.testing.uses_gpu def test_clip_min_max_as_inputs():