Skip to content

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

[PyOV] Extend Python API with `StringTensorUnpack-15` and `StringTensorPack-15` (#26210)

Przemyslaw Wysocki5a96b829b5c2
diff --git a/src/bindings/python/src/openvino/runtime/opset15/__init__.py b/src/bindings/python/src/openvino/runtime/opset15/__init__.py
index 03c9530e179..1349508e84b 100644
--- a/src/bindings/python/src/openvino/runtime/opset15/__init__.py
+++ b/src/bindings/python/src/openvino/runtime/opset15/__init__.py
@@ -11,5 +11,7 @@ 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 string_tensor_pack
+from openvino.runtime.opset15.ops import string_tensor_unpack
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 6e3436c123c..777fc165443 100644
--- a/src/bindings/python/src/openvino/runtime/opset15/ops.py
+++ b/src/bindings/python/src/openvino/runtime/opset15/ops.py
@@ -188,6 +188,44 @@ def roi_align_rotated(
)
+@nameable_op
+def string_tensor_unpack(
+ data: NodeInput,
+ name: Optional[str] = None,
+) -> Node:
+ """Perform an operation which unpacks a batch of strings into three tensors.
+
+ :param data: The node providing input data.
+
+ :return: The new node performing StringTensorUnpack operation.
+ """
+ return _get_node_factory_opset15().create(
+ "StringTensorUnpack",
+ as_nodes(data, name=name)
+ )
+
+
+@nameable_op
+def string_tensor_pack(
+ begins: NodeInput,
+ ends: NodeInput,
+ symbols: NodeInput,
+ name: Optional[str] = None,
+) -> Node:
+ """Perform an operation which packs a concatenated batch of strings into a batched string tensor.
+
+ :param begins: ND tensor of non-negative integer numbers containing indices of each string's beginnings.
+ :param ends: ND tensor of non-negative integer numbers containing indices of each string's endings.
+ :param symbols: 1D tensor of concatenated strings data encoded in utf-8 bytes.
+
+ :return: The new node performing StringTensorPack operation.
+ """
+ return _get_node_factory_opset15().create(
+ "StringTensorPack",
+ as_nodes(begins, ends, symbols, name=name)
+ )
+
+
@binary_op
def bitwise_left_shift(
arg0: NodeInput,
diff --git a/src/bindings/python/tests/test_graph/test_string_tensor_pack.py b/src/bindings/python/tests/test_graph/test_string_tensor_pack.py
new file mode 100644
index 00000000000..8607124a3ff
--- /dev/null
+++ b/src/bindings/python/tests/test_graph/test_string_tensor_pack.py
@@ -0,0 +1,28 @@
+# -*- coding: utf-8 -*-
+# Copyright (C) 2018-2024 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+
+from openvino import Type
+import openvino.runtime.opset15 as ov
+import pytest
+
+
[email protected](("dtype"), [
+ Type.i32,
+ Type.i64
+])
[email protected](("indices_shape"), [
+ (4,),
+ (1, 3, 4),
+ (3, 1, 2, 5)
+])
+def test_string_tensor_pack(dtype, indices_shape):
+ begins = ov.parameter(indices_shape, name="input_data", dtype=dtype)
+ ends = ov.parameter(indices_shape, name="input_data", dtype=dtype)
+ symbols = ov.parameter((100,), name="input_data", dtype=Type.u8)
+ node = ov.string_tensor_pack(begins, ends, symbols)
+
+ assert node.get_type_name() == "StringTensorPack"
+ assert node.get_output_size() == 1
+ assert list(node.get_output_shape(0)) == list(indices_shape)
+ assert node.get_output_element_type(0) == Type.string
diff --git a/src/bindings/python/tests/test_graph/test_string_tensor_unpack.py b/src/bindings/python/tests/test_graph/test_string_tensor_unpack.py
new file mode 100644
index 00000000000..596326e5f2e
--- /dev/null
+++ b/src/bindings/python/tests/test_graph/test_string_tensor_unpack.py
@@ -0,0 +1,26 @@
+# -*- coding: utf-8 -*-
+# Copyright (C) 2018-2024 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+
+from openvino import Type, PartialShape, Dimension
+import openvino.runtime.opset15 as ov
+import pytest
+
+
[email protected]("input_shape", [
+ ((4,)),
+ ((1, 3, 4)),
+ ((3, 1, 2, 5))
+])
+def test_string_tensor_unpack(input_shape):
+ input_data = ov.parameter(input_shape, name="input_data", dtype=Type.string)
+ node = ov.string_tensor_unpack(input_data)
+
+ assert node.get_type_name() == "StringTensorUnpack"
+ assert node.get_output_size() == 3
+ assert list(node.get_output_shape(0)) == list(input_shape)
+ assert list(node.get_output_shape(1)) == list(input_shape)
+ assert node.get_output_partial_shape(2) == PartialShape([Dimension.dynamic()])
+ assert node.get_output_element_type(0) == Type.i32
+ assert node.get_output_element_type(1) == Type.i32
+ assert node.get_output_element_type(2) == Type.u8