[Opset13][pyAPI] Python API binary BitwiseAnd-13, BitwiseOr-13, BitwiseXor-13 (#20261)

* add xor

* Add and and or

* Add ch srequested in review
This commit is contained in:
Mateusz Mikolajczyk 2023-10-10 09:04:06 +02:00 committed by Alexander Nesterov
parent e8a8f04dac
commit 001e114d64
3 changed files with 125 additions and 2 deletions

View File

@ -18,6 +18,9 @@ from openvino.runtime.opset1.ops import avg_pool
from openvino.runtime.opset5.ops import batch_norm_inference
from openvino.runtime.opset2.ops import batch_to_space
from openvino.runtime.opset1.ops import binary_convolution
from openvino.runtime.opset13.ops import bitwise_and
from openvino.runtime.opset13.ops import bitwise_or
from openvino.runtime.opset13.ops import bitwise_xor
from openvino.runtime.opset3.ops import broadcast
from openvino.runtime.opset3.ops import bucketize
from openvino.runtime.opset1.ops import ceiling

View File

@ -8,7 +8,7 @@ from typing import Optional
from openvino.runtime import Node
from openvino.runtime.opset_utils import _get_node_factory
from openvino.runtime.utils.decorators import nameable_op
from openvino.runtime.utils.decorators import binary_op, nameable_op
from openvino.runtime.utils.types import (
NodeInput,
as_nodes,
@ -19,6 +19,76 @@ _get_node_factory_opset13 = partial(_get_node_factory, "opset13")
# -------------------------------------------- ops ------------------------------------------------
@binary_op
def bitwise_and(
left_node: NodeInput,
right_node: NodeInput,
auto_broadcast: str = "NUMPY",
name: Optional[str] = None,
) -> Node:
"""Return node which performs bitwise AND operation on input nodes element-wise.
For boolean input tensors, operator is equivalent to logical_and.
:param left_node: Tensor of integer or boolean datatype providing data.
:param right_node: Tensor of integer or boolean datatype providing data.
:param auto_broadcast: The type of broadcasting specifies rules used for auto-broadcasting of input tensors. Defaults to NUMPY.
:param name: The optional new name for output node.
:return: The node performing bitwise AND operation on input nodes corresponding elements.
"""
return _get_node_factory_opset13().create(
"BitwiseAnd",
[left_node, right_node],
{"auto_broadcast": auto_broadcast.upper()},
)
@binary_op
def bitwise_or(
left_node: NodeInput,
right_node: NodeInput,
auto_broadcast: str = "NUMPY",
name: Optional[str] = None,
) -> Node:
"""Return node which performs bitwise OR operation on input nodes element-wise.
For boolean input tensors, operator is equivalent to logical_or.
:param left_node: Tensor of integer or boolean datatype providing data.
:param right_node: Tensor of integer or boolean datatype providing data.
:param auto_broadcast: The type of broadcasting specifies rules used for auto-broadcasting of input tensors. Defaults to NUMPY.
:param name: The optional new name for output node.
:return: The node performing bitwise OR operation on input nodes corresponding elements.
"""
return _get_node_factory_opset13().create(
"BitwiseOr",
[left_node, right_node],
{"auto_broadcast": auto_broadcast.upper()},
)
@binary_op
def bitwise_xor(
left_node: NodeInput,
right_node: NodeInput,
auto_broadcast: str = "NUMPY",
name: Optional[str] = None,
) -> Node:
"""Return node which performs bitwise XOR operation on input nodes element-wise.
For boolean input tensors, operator is equivalent to logical_xor.
:param left_node: Tensor of integer or boolean datatype providing data.
:param right_node: Tensor of integer or boolean datatype providing data.
:param auto_broadcast: The type of broadcasting specifies rules used for auto-broadcasting of input tensors. Defaults to NUMPY.
:param name: The optional new name for output node.
:return: The node performing bitwise XOR operation on input nodes corresponding elements.
"""
return _get_node_factory_opset13().create(
"BitwiseXor",
[left_node, right_node],
{"auto_broadcast": auto_broadcast.upper()},
)
@nameable_op

View File

@ -8,7 +8,7 @@ import numpy as np
import pytest
from openvino.runtime import Type
import openvino.runtime.opset8 as ov
import openvino.runtime.opset13 as ov
@pytest.mark.parametrize(
@ -183,3 +183,53 @@ def test_power_v1():
assert node.get_output_size() == 1
assert list(node.get_output_shape(0)) == [8, 4, 6, 5]
assert node.get_output_element_type(0) == Type.f32
@pytest.mark.parametrize(
"graph_api_helper",
[ov.bitwise_and, ov.bitwise_or, ov.bitwise_xor],
)
@pytest.mark.parametrize(
"dtype",
[bool, np.int32],
)
@pytest.mark.parametrize(
("shape_a", "shape_b", "broadcast", "shape_out"),
[
([2, 2], [2, 2], "NONE", [2, 2]),
([2, 1, 5], [1, 4, 5], "NUMPY", [2, 4, 5]),
([3, 2, 1, 4], [5, 4], "NUMPY", [3, 2, 5, 4]),
([2, 3, 4, 5], [], "PDPD", [2, 3, 4, 5]),
([2, 3, 4, 5], [2, 3, 1, 5], "PDPD", [2, 3, 4, 5]),
],
)
def test_binary_bitwise_op(graph_api_helper, dtype, shape_a, shape_b, broadcast, shape_out):
parameter_a = ov.parameter(shape_a, name="A", dtype=dtype)
parameter_b = ov.parameter(shape_b, name="B", dtype=dtype)
model = graph_api_helper(parameter_a, parameter_b, broadcast)
assert model.get_output_size() == 1
assert list(model.get_output_shape(0)) == shape_out
assert model.get_output_element_type(0) == Type(dtype)
@pytest.mark.parametrize(
"graph_api_helper",
[ov.bitwise_and, ov.bitwise_or, ov.bitwise_xor],
)
@pytest.mark.parametrize(
"dtype",
[bool, np.int32],
)
def test_binary_bitwise_op_with_constant(graph_api_helper, dtype):
value_b = np.array([[3, 0], [-7, 21]], dtype=dtype)
shape = [2, 2]
parameter_a = ov.parameter(shape, name="A", dtype=dtype)
model = graph_api_helper(parameter_a, value_b)
assert model.get_output_size() == 1
assert list(model.get_output_shape(0)) == shape
assert model.get_output_element_type(0) == Type(dtype)