Skip to content

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

[Python API] Handle zero-sized string Tensor data (#39166)

Yug Shahb4bb0d4faf5b
diff --git a/src/bindings/python/src/pyopenvino/core/common.cpp b/src/bindings/python/src/pyopenvino/core/common.cpp
index 7ec1d9fc221..68967712e77 100644
--- a/src/bindings/python/src/pyopenvino/core/common.cpp
+++ b/src/bindings/python/src/pyopenvino/core/common.cpp
@@ -133,12 +133,15 @@ py::array bytes_array_from_tensor(ov::Tensor&& t) {
if (t.get_element_type() != ov::element::string) {
OPENVINO_THROW("Tensor's type must be a string!");
}
+ if (t.get_size() == 0) {
+ return py::array(py::dtype("S1"), t.get_shape());
+ }
auto data = t.data<std::string>();
- auto max_element = std::max_element(data, data + t.get_size(), [](const std::string& x, const std::string& y) {
- return x.length() < y.length();
- });
- auto max_stride = max_element->length();
+ size_t max_stride = 1;
+ for (size_t i = 0; i < t.get_size(); ++i) {
+ max_stride = std::max(max_stride, data[i].length());
+ }
auto dtype = py::dtype("|S" + std::to_string(max_stride));
// Adjusting strides to follow the numpy convention:
@@ -155,7 +158,7 @@ py::array bytes_array_from_tensor(ov::Tensor&& t) {
// Create an empty array and populate it with utf-8 encoded strings:
auto array = py::array(dtype, t.get_shape(), py_array_strides());
- auto ptr = reinterpret_cast<char*>(array.mutable_data());
+ auto ptr = static_cast<char*>(array.mutable_data());
for (size_t i = 0; i < t.get_size(); ++i) {
const auto length = data[i].length();
ptr = std::copy_n(data[i].begin(), length, ptr);
@@ -168,6 +171,9 @@ py::array string_array_from_tensor(ov::Tensor&& t) {
if (t.get_element_type() != ov::element::string) {
OPENVINO_THROW("Tensor's type must be a string!");
}
+ if (t.get_size() == 0) {
+ return py::array(py::dtype("U1"), t.get_shape());
+ }
// Approach that is compact and faster than np.char.decode(tensor.data):
auto data = t.data<std::string>();
py::list _list;
diff --git a/src/bindings/python/tests/test_runtime/test_tensor_string.py b/src/bindings/python/tests/test_runtime/test_tensor_string.py
index 4b7564db387..f5ef614468b 100644
--- a/src/bindings/python/tests/test_runtime/test_tensor_string.py
+++ b/src/bindings/python/tests/test_runtime/test_tensor_string.py
@@ -72,6 +72,49 @@ def test_empty_string_tensor(init_type):
assert tensor.element_type == ov.Type.string
[email protected]("shape", [(0,), (2, 0)])
+def test_zero_sized_string_tensor_data(shape):
+ tensor = ov.Tensor(type=ov.Type.string, shape=shape)
+
+ bytes_data = tensor.bytes_data
+ assert bytes_data.shape == shape
+ assert bytes_data.dtype.kind == "S"
+
+ string_data = tensor.str_data
+ assert string_data.shape == shape
+ assert string_data.dtype.kind == "U"
+
+ with pytest.warns(RuntimeWarning, match="Data of string type will be copied"):
+ data = tensor.data
+ assert data.shape == shape
+ assert data.dtype.kind == "S"
+
+ round_trip = ov.Tensor(bytes_data, shared_memory=False)
+ assert round_trip.shape == shape
+ assert round_trip.bytes_data.shape == shape
+ assert round_trip.bytes_data.size == 0
+
+
+def test_bytes_data_with_empty_strings():
+ tensor = ov.Tensor(type=ov.Type.string, shape=[2])
+
+ bytes_data = tensor.bytes_data
+ assert bytes_data.tolist() == [b"", b""]
+ bytes_data[0] = b"x"
+ assert bytes_data[1] == b""
+ assert bytes_data.tolist() == [b"x", b""]
+
+ copy = ov.Tensor(tensor.bytes_data, shared_memory=False)
+ assert copy.bytes_data.tolist() == [b"", b""]
+ assert tensor.str_data.tolist() == ["", ""]
+
+ bytes_data[1] = b"y"
+ assert bytes_data.tolist() == [b"x", b"y"]
+
+ round_trip = ov.Tensor(bytes_data, shared_memory=False)
+ assert round_trip.bytes_data.tolist() == [b"x", b"y"]
+
+
@pytest.mark.parametrize(
("string_data"),
[