Skip to content

Commit

Permalink
Support bitwise ops (#2192)
Browse files Browse the repository at this point in the history
* 10787 support bitwise ops
* 10787 add bitwise tests
* ANTIALIAS has been removed in Pillow 10 (2023-07-01).
Use LANCZOS instead.

Signed-off-by: Salvetti, Francesco <francesco.salvetti@nuance.com>
Signed-off-by: Jay Zhang <jiz@microsoft.com>
Co-authored-by: Jay Zhang <jiz@microsoft.com>
  • Loading branch information
f-salvetti and fatcat-z authored Jul 4, 2023
1 parent b27aa05 commit 25c977c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
4 changes: 4 additions & 0 deletions support_status.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
| BiasAdd | 1 ~ 17 |
| BiasAddV1 | 1 ~ 17 |
| Bincount | 11 ~ 17 |
| BitwiseAnd | 18 |
| BitwiseOr | 18 |
| BitwiseXor | 18 |
| BroadcastTo | 8 ~ 17 |
| CTCGreedyDecoder | 11 ~ 17 |
| Cast | 1 ~ 17 |
Expand Down Expand Up @@ -93,6 +96,7 @@
| Identity | 1 ~ 17 |
| IdentityN | 1 ~ 17 |
| If | 1 ~ 17 |
| Invert | 18 |
| InvertPermutation | 11 ~ 17 |
| IsFinite | 10 ~ 17 |
| IsInf | 10 ~ 17 |
Expand Down
2 changes: 1 addition & 1 deletion tests/run_pretrained_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_img(shape, path, dtype, should_scale=True):
resize_to = shape[1:3]
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), path)
img = PIL.Image.open(path)
img = img.resize(resize_to, PIL.Image.ANTIALIAS)
img = img.resize(resize_to, PIL.Image.LANCZOS)
img_np = np.array(img).astype(dtype)
img_np = np.stack([img_np] * shape[0], axis=0).reshape(shape)
if should_scale:
Expand Down
35 changes: 35 additions & 0 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -4896,6 +4896,41 @@ def func(x):
return tf.identity(x_, name=_TFOUTPUT)
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val})

@check_opset_min_version(18, "BitwiseAnd")
def test_bitwise_and(self):
x_val = np.array([21, 4, 1], dtype=np.int32)
y_val = np.array([45, 69, 3], dtype=np.int32)
def func(x, y):
x_ = tf.bitwise.bitwise_and(x, y)
return tf.identity(x_, name=_TFOUTPUT)
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val, _INPUT1: y_val})

@check_opset_min_version(18, "BitwiseOr")
def test_bitwise_or(self):
x_val = np.array([21, 4, 87], dtype=np.int32)
y_val = np.array([45, 69, 173], dtype=np.int32)
def func(x, y):
x_ = tf.bitwise.bitwise_or(x, y)
return tf.identity(x_, name=_TFOUTPUT)
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val, _INPUT1: y_val})

@check_opset_min_version(18, "BitwiseXor")
def test_bitwise_xor(self):
x_val = np.array([21, 4, 87], dtype=np.int32)
y_val = np.array([45, 69, 173], dtype=np.int32)
def func(x, y):
x_ = tf.bitwise.bitwise_xor(x, y)
return tf.identity(x_, name=_TFOUTPUT)
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val, _INPUT1: y_val})

@check_opset_min_version(18, "BitwiseNot")
def test_bitwise_not(self):
x_val = np.array([21, 4, 1], dtype=np.int32)
def func(x):
x_ = tf.bitwise.invert(x)
return tf.identity(x_, name=_TFOUTPUT)
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val})

@check_tf_min_version("1.14", "tensor_scatter_nd_update needs tf 1.14")
@check_opset_min_version(11, "ScatterND")
def test_tensor_scatter_update(self):
Expand Down
11 changes: 10 additions & 1 deletion tf2onnx/onnx_opset/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,6 @@ def version_11(cls, ctx, node, **kwargs):

@tf_op(["LeftShift", "RightShift"])
class BitShift:

@classmethod
def version_11(cls, ctx, node, **kwargs):
dir_map = {"LeftShift": "LEFT", "RightShift": "RIGHT"}
Expand All @@ -818,6 +817,16 @@ def version_11(cls, ctx, node, **kwargs):
ctx.copy_dtype(node.input[0], node.output[0])


@tf_op("BitwiseAnd")
@tf_op("BitwiseOr")
@tf_op("BitwiseXor")
@tf_op("Invert", onnx_op="BitwiseNot")
class BitwiseOps:
@classmethod
def version_18(cls, ctx, node, **kwargs):
pass


@tf_op("SquaredDistance", onnx_op="MeanSquaredDistance")
class SquaredDistance:
@classmethod
Expand Down

0 comments on commit 25c977c

Please sign in to comment.