OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[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__.pyindex 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_offsetsfrom openvino.runtime.opset15.ops import embedding_bag_packedfrom openvino.runtime.opset15.ops import scatter_nd_updatefrom 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_shiftdiff --git a/src/bindings/python/src/openvino/runtime/opset15/ops.py b/src/bindings/python/src/openvino/runtime/opset15/ops.pyindex 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, Typefrom openvino.runtime.opset1 import convert_likefrom openvino.runtime.opset14 import constantfrom 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_opfrom 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.pyindex 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 pytestfrom openvino import Typeimport 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() == 1assert list(model.get_output_shape(0)) == shapeassert 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)