Skip to content
Open
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
4 changes: 3 additions & 1 deletion python/tvm/relax/frontend/onnx/onnx_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions tests/python/relax/test_frontend_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Loading