Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[PyOV] Extend Python API with BitwiseLeftShift-15 and BitwiseRightShift-15 (#26215)

Hubert Błaszczyk5d2457cb3c70
diff --git a/src/bindings/python/src/openvino/runtime/opset15/__init__.py b/src/bindings/python/src/openvino/runtime/opset15/__init__.py
index 13087ef74a7..03c9530e179 100644
--- a/src/bindings/python/src/openvino/runtime/opset15/__init__.py
+++ b/src/bindings/python/src/openvino/runtime/opset15/__init__.py
@@ -11,3 +11,5 @@ from openvino.runtime.opset15.ops import embedding_bag_offsets
from openvino.runtime.opset15.ops import embedding_bag_packed
from openvino.runtime.opset15.ops import scatter_nd_update
from openvino.runtime.opset15.ops import roi_align_rotated
+from openvino.runtime.opset15.ops import bitwise_left_shift
+from openvino.runtime.opset15.ops import bitwise_right_shift
diff --git a/src/bindings/python/src/openvino/runtime/opset15/ops.py b/src/bindings/python/src/openvino/runtime/opset15/ops.py
index 8ed73361db1..6e3436c123c 100644
--- a/src/bindings/python/src/openvino/runtime/opset15/ops.py
+++ b/src/bindings/python/src/openvino/runtime/opset15/ops.py
@@ -11,7 +11,7 @@ from openvino.runtime import Node, Type
from openvino.runtime.opset1 import convert_like
from openvino.runtime.opset14 import constant
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
_get_node_factory_opset15 = partial(_get_node_factory, "opset15")
@@ -186,3 +186,53 @@ def roi_align_rotated(
"clockwise_mode": clockwise_mode,
},
)
+
+
+@binary_op
+def bitwise_left_shift(
+ arg0: NodeInput,
+ arg1: NodeInput,
+ auto_broadcast: str = "NUMPY",
+ name: Optional[str] = None,
+) -> Node:
+ """Return node which performs BitwiseLeftShift operation on input nodes element-wise.
+
+ :param arg0: Node with data to be shifted.
+ :param arg1: Node with number of shifts.
+ :param auto_broadcast: The type of broadcasting specifies rules used for auto-broadcasting of input tensors.
+ Defaults to “NUMPY”.
+
+ :return: The new node performing BitwiseLeftShift operation.
+ """
+ return _get_node_factory_opset15().create(
+ "BitwiseLeftShift",
+ as_nodes(arg0, arg1, name=name),
+ {
+ "auto_broadcast": auto_broadcast.upper(),
+ },
+ )
+
+
+@binary_op
+def bitwise_right_shift(
+ arg0: NodeInput,
+ arg1: NodeInput,
+ auto_broadcast: str = "NUMPY",
+ name: Optional[str] = None,
+) -> Node:
+ """Return node which performs BitwiseRightShift operation on input nodes element-wise.
+
+ :param arg0: Tensor with data to be shifted.
+ :param arg1: Tensor with number of shifts.
+ :param auto_broadcast: The type of broadcasting specifies rules used for auto-broadcasting of input tensors.
+ Defaults to “NUMPY”.
+
+ :return: The new node performing BitwiseRightShift operation.
+ """
+ return _get_node_factory_opset15().create(
+ "BitwiseRightShift",
+ as_nodes(arg0, arg1, name=name),
+ {
+ "auto_broadcast": auto_broadcast.upper(),
+ },
+ )
diff --git a/src/bindings/python/tests/test_graph/test_ops_binary.py b/src/bindings/python/tests/test_graph/test_ops_binary.py
index 9831e219a8e..75c8f8daf2f 100644
--- a/src/bindings/python/tests/test_graph/test_ops_binary.py
+++ b/src/bindings/python/tests/test_graph/test_ops_binary.py
@@ -9,6 +9,7 @@ import pytest
from openvino import Type
import openvino.runtime.opset13 as ov
+import openvino.runtime.opset15 as ov_opset15
@pytest.mark.parametrize(
@@ -233,3 +234,38 @@ def test_binary_bitwise_op_with_constant(graph_api_helper, dtype):
assert model.get_output_size() == 1
assert list(model.get_output_shape(0)) == shape
assert model.get_output_element_type(0) == Type(dtype)
+
+
+ "graph_api_helper",
+ [ov_opset15.bitwise_left_shift, ov_opset15.bitwise_right_shift],
+)
+ "dtype",
+ [np.int32],
+)
+ ("shape_a", "shape_b", "broadcast", "shape_out", "is_const"),
+ [
+ ([2, 2], [2, 2], "NONE", [2, 2], False),
+ ([2, 2], [2, 2], "NONE", [2, 2], True),
+ ([2, 1, 5], [1, 4, 5], "NUMPY", [2, 4, 5], False),
+ ([3, 2, 1, 4], [5, 4], "NUMPY", [3, 2, 5, 4], False),
+ ([2, 3, 4, 5], [], "PDPD", [2, 3, 4, 5], False),
+ ([2, 3, 4, 5], [2, 3, 1, 5], "PDPD", [2, 3, 4, 5], False),
+ ],
+)
+def test_binary_bitwise_shift_op(graph_api_helper, dtype, shape_a,
+ shape_b, broadcast, shape_out, is_const):
+ parameter_a = ov.parameter(shape_a, name="A", dtype=dtype)
+ parameter_b = ov.parameter(shape_b, name="B", dtype=dtype)
+
+ if is_const:
+ value_b = np.array([[3, 0], [7, 21]], dtype=dtype)
+ model = graph_api_helper(parameter_a, value_b, broadcast)
+ else:
+ 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)