Skip to content

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

[PyOV] Extend Python API with SearchSorted-15 (#27230)

Mateusz Mikolajczyk8faeed130b85
diff --git a/src/bindings/python/src/openvino/runtime/opset15/__init__.py b/src/bindings/python/src/openvino/runtime/opset15/__init__.py
index 58fd90e7fd1..a12225f719a 100644
--- a/src/bindings/python/src/openvino/runtime/opset15/__init__.py
+++ b/src/bindings/python/src/openvino/runtime/opset15/__init__.py
@@ -17,3 +17,4 @@ from openvino.runtime.opset15.ops import bitwise_left_shift
from openvino.runtime.opset15.ops import bitwise_right_shift
from openvino.runtime.opset15.ops import slice_scatter
from openvino.runtime.opset15.ops import stft
+from openvino.runtime.opset15.ops import search_sorted
diff --git a/src/bindings/python/src/openvino/runtime/opset15/ops.py b/src/bindings/python/src/openvino/runtime/opset15/ops.py
index c278120dab7..45b01a11bc3 100644
--- a/src/bindings/python/src/openvino/runtime/opset15/ops.py
+++ b/src/bindings/python/src/openvino/runtime/opset15/ops.py
@@ -327,3 +327,24 @@ def stft(
"""
inputs = as_nodes(data, window, frame_size, frame_step, name=name)
return _get_node_factory_opset15().create("STFT", inputs)
+
+
+@nameable_op
+def search_sorted(
+ sorted_sequence: NodeInput,
+ values: NodeInput,
+ right_mode: bool = False,
+ name: Optional[str] = None,
+) -> Node:
+ """Return a node which generates SearchSorted operation.
+
+ :param sorted_sequence: The node providing sorted sequence to search in.
+ :param values: The node providing searched values.
+ :param right_mode: If set to False, return the first suitable index that is found for given value.
+ If set to True, return the last such index. Defaults to False.
+ :param name: The optional name for the created output node.
+ :return: The new node performing SearchSorted operation.
+ """
+ inputs = as_nodes(sorted_sequence, values, name=name)
+ attributes = {"right_mode": right_mode}
+ return _get_node_factory_opset15().create("SearchSorted", inputs, attributes)
diff --git a/src/bindings/python/tests/test_graph/test_create_op.py b/src/bindings/python/tests/test_graph/test_create_op.py
index 940f8244f42..87787e1e29b 100644
--- a/src/bindings/python/tests/test_graph/test_create_op.py
+++ b/src/bindings/python/tests/test_graph/test_create_op.py
@@ -2502,6 +2502,34 @@ def test_stft():
assert op.get_output_shape(0) == [4, 13, 6, 2]
+def test_search_sorted():
+ sorted_sequence = ov.parameter([7, 256, 200, 200], name="sorted", dtype=np.float32)
+ values = ov.parameter([7, 256, 200, 10], name="values", dtype=np.float32)
+ op = ov_opset15.search_sorted(sorted_sequence=sorted_sequence, values=values, name="default")
+ assert op.get_type_name() == "SearchSorted"
+ assert op.get_output_size() == 1
+ assert op.get_output_element_type(0) == Type.i64
+ assert op.get_output_shape(0) == [7, 256, 200, 10]
+ assert op.get_attributes()["right_mode"] is False
+ assert op.get_friendly_name() == "default"
+
+ op = ov_opset15.search_sorted(sorted_sequence, values, right_mode=True, name="right")
+ assert op.get_type_name() == "SearchSorted"
+ assert op.get_output_size() == 1
+ assert op.get_output_element_type(0) == Type.i64
+ assert op.get_output_shape(0) == [7, 256, 200, 10]
+ assert op.get_attributes()["right_mode"] is True
+ assert op.get_friendly_name() == "right"
+
+ op = ov_opset15.search_sorted(sorted_sequence, values, False, name="left")
+ assert op.get_type_name() == "SearchSorted"
+ assert op.get_output_size() == 1
+ assert op.get_output_element_type(0) == Type.i64
+ assert op.get_output_shape(0) == [7, 256, 200, 10]
+ assert op.get_attributes()["right_mode"] is False
+ assert op.get_friendly_name() == "left"
+
+
def test_parameter_get_attributes():
parameter = ov.parameter([2, 2], dtype=np.float32, name="InputData")
parameter_attributes = parameter.get_attributes()