diff --git a/python/tvm/relax/frontend/onnx/onnx_frontend.py b/python/tvm/relax/frontend/onnx/onnx_frontend.py index 52715b9fb56c..e59fc7086c93 100644 --- a/python/tvm/relax/frontend/onnx/onnx_frontend.py +++ b/python/tvm/relax/frontend/onnx/onnx_frontend.py @@ -913,7 +913,9 @@ class BitShift(BitwiseBase): @classmethod def _impl_v11(cls, bb, inputs, attr, params): - direction = attr.get("direction", "LEFT").decode("ascii") + direction = attr.get("direction", "LEFT") + if isinstance(direction, bytes): + direction = direction.decode("ascii") if direction == "LEFT": cls.numpy_op = _np.left_shift cls.relax_op = relax.op.left_shift diff --git a/tests/python/relax/test_frontend_onnx.py b/tests/python/relax/test_frontend_onnx.py index bb015f9ca1ba..9e1e1c13aa5e 100644 --- a/tests/python/relax/test_frontend_onnx.py +++ b/tests/python/relax/test_frontend_onnx.py @@ -15616,5 +15616,59 @@ def test_dft_float16_inverse_overflow(): assert res.dtype == np.float16 +@pytest.mark.parametrize("direction", ["LEFT", "RIGHT"]) +@pytest.mark.parametrize("dtype", [TensorProto.UINT8, TensorProto.UINT32, TensorProto.UINT64]) +def test_bitshift(direction, dtype): + np_dtype = helper.tensor_dtype_to_np_dtype(dtype) + a_data = np.array([1, 4, 16, 64], dtype=np_dtype) + b_data = np.array([1, 2, 1, 2], dtype=np_dtype) + inputs = {"a": a_data, "b": b_data} + + test_node = helper.make_node("BitShift", ["a", "b"], ["c"], direction=direction) + graph = helper.make_graph( + [test_node], + "bitshift_test", + inputs=[ + helper.make_tensor_value_info("a", dtype, [4]), + helper.make_tensor_value_info("b", dtype, [4]), + ], + outputs=[helper.make_tensor_value_info("c", dtype, [4])], + ) + model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 11)], ir_version=8) + check_correctness(model, inputs=inputs, opset=11) + + +def test_bitshift_default_direction(): + test_node = helper.make_node("BitShift", ["a", "b"], ["c"]) + graph = helper.make_graph( + [test_node], + "bitshift_default_test", + inputs=[ + helper.make_tensor_value_info("a", TensorProto.UINT32, [4]), + helper.make_tensor_value_info("b", TensorProto.UINT32, [4]), + ], + outputs=[helper.make_tensor_value_info("c", TensorProto.UINT32, [4])], + ) + model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 11)], ir_version=8) + mod = from_onnx(model, opset=11) + assert "left_shift" in str(mod) + + +def test_bitshift_uint16(): + test_node = helper.make_node("BitShift", ["a", "b"], ["c"], direction="RIGHT") + graph = helper.make_graph( + [test_node], + "bitshift_uint16_test", + inputs=[ + helper.make_tensor_value_info("a", TensorProto.UINT16, [4]), + helper.make_tensor_value_info("b", TensorProto.UINT16, [4]), + ], + outputs=[helper.make_tensor_value_info("c", TensorProto.UINT16, [4])], + ) + model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 11)], ir_version=8) + mod = from_onnx(model, opset=11) + assert "right_shift" in str(mod) + + if __name__ == "__main__": tvm_tirx.testing.main()