Skip to content

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

Add `StringTensorUnpack-15` and `StringTensorPack-15` to Core (#25606)

Przemyslaw Wysockie0d233d9df49
diff --git a/docs/articles_en/documentation/openvino-ir-format/operation-sets/operation-specs/type/string-tensor-pack-15.rst b/docs/articles_en/documentation/openvino-ir-format/operation-sets/operation-specs/type/string-tensor-pack-15.rst
index 77f44f8d687..4a697882353 100644
--- a/docs/articles_en/documentation/openvino-ir-format/operation-sets/operation-specs/type/string-tensor-pack-15.rst
+++ b/docs/articles_en/documentation/openvino-ir-format/operation-sets/operation-specs/type/string-tensor-pack-15.rst
@@ -109,7 +109,7 @@ but not including, ``b``. That is why in the example given the length of "IntelO
<dim>2</dim> <!-- ends = [1, 9] -->
</port>
<port id="2" precision="U8">
- <dim>13</dim> <!-- symbols = "123456789" encoded in an utf-8 array -->
+ <dim>9</dim> <!-- symbols = "123456789" encoded in an utf-8 array -->
</port>
</input>
<output>
diff --git a/src/core/include/openvino/op/ops.hpp b/src/core/include/openvino/op/ops.hpp
index 4e2cde0882d..749069e5bbf 100644
--- a/src/core/include/openvino/op/ops.hpp
+++ b/src/core/include/openvino/op/ops.hpp
@@ -186,6 +186,8 @@
#include "openvino/op/squared_difference.hpp"
#include "openvino/op/squeeze.hpp"
#include "openvino/op/strided_slice.hpp"
+#include "openvino/op/string_tensor_pack.hpp"
+#include "openvino/op/string_tensor_unpack.hpp"
#include "openvino/op/subtract.hpp"
#include "openvino/op/swish.hpp"
#include "openvino/op/tan.hpp"
diff --git a/src/core/include/openvino/op/string_tensor_pack.hpp b/src/core/include/openvino/op/string_tensor_pack.hpp
new file mode 100644
index 00000000000..1fac0724292
--- /dev/null
+++ b/src/core/include/openvino/op/string_tensor_pack.hpp
@@ -0,0 +1,32 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "openvino/op/op.hpp"
+
+namespace ov {
+namespace op {
+namespace v15 {
+/// \brief Operator packing a concatenated batch of strings into a batched string tensor.
+/// \ingroup ov_ops_cpp_api
+class OPENVINO_API StringTensorPack : public ov::op::Op {
+public:
+ OPENVINO_OP("StringTensorPack", "opset15", ov::op::Op);
+
+ StringTensorPack() = default;
+ /// \brief Constructs a StringTensorPack operation.
+ ///
+ /// \param begins Indices of each string's beginnings
+ /// \param ends Indices of each string's endings
+ /// \param symbols Concatenated input strings encoded in utf-8 bytes
+ StringTensorPack(const Output<Node>& begins, const Output<Node>& ends, const Output<Node>& symbols);
+
+ void validate_and_infer_types() override;
+ std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& new_args) const override;
+};
+
+} // namespace v15
+} // namespace op
+} // namespace ov
diff --git a/src/core/include/openvino/op/string_tensor_unpack.hpp b/src/core/include/openvino/op/string_tensor_unpack.hpp
new file mode 100644
index 00000000000..0b70e60fc24
--- /dev/null
+++ b/src/core/include/openvino/op/string_tensor_unpack.hpp
@@ -0,0 +1,30 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "openvino/op/op.hpp"
+
+namespace ov {
+namespace op {
+namespace v15 {
+/// \brief Operator unpacking a batch of strings into three tensors.
+/// \ingroup ov_ops_cpp_api
+class OPENVINO_API StringTensorUnpack : public ov::op::Op {
+public:
+ OPENVINO_OP("StringTensorUnpack", "opset15", ov::op::Op);
+
+ StringTensorUnpack() = default;
+ /// \brief Constructs a StringTensorUnpack operation.
+ ///
+ /// \param data Input strings encoded in utf-8 bytes
+ StringTensorUnpack(const Output<Node>& data);
+
+ void validate_and_infer_types() override;
+ std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& new_args) const override;
+};
+
+} // namespace v15
+} // namespace op
+} // namespace ov
diff --git a/src/core/shape_inference/include/string_tensor_pack_shape_inference.hpp b/src/core/shape_inference/include/string_tensor_pack_shape_inference.hpp
new file mode 100644
index 00000000000..cb22d3d93fa
--- /dev/null
+++ b/src/core/shape_inference/include/string_tensor_pack_shape_inference.hpp
@@ -0,0 +1,68 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <cmath>
+
+#include "openvino/op/string_tensor_pack.hpp"
+#include "utils.hpp"
+
+namespace ov {
+namespace op {
+namespace v15 {
+namespace util {
+template <class TShape, class TRShape = result_shape_t<TShape>>
+static void validate_indices(const size_t input_index,
+ const ITensorAccessor& tensor_accessor,
+ const StringTensorPack* op,
+ const std::vector<TShape>& input_shapes) {
+ const auto& indices_shape = input_shapes[input_index];
+ if (indices_shape.is_static()) {
+ if (const auto data = ov::op::get_input_const_data_as<TRShape, int64_t>(op, input_index, tensor_accessor)) {
+ const auto element_count = data->size();
+ if (element_count > 0) {
+ NODE_SHAPE_INFER_CHECK(op, input_shapes, (*data)[0] >= 0, "Indices cannot be negative.");
+ const auto& symbols_shape = input_shapes[2];
+ if (symbols_shape.is_static()) {
+ // ends indices are offset by 1
+ const auto biggest_idx = symbols_shape[0].get_length() + (input_index == 0 ? 0 : 1);
+ NODE_SHAPE_INFER_CHECK(
+ op,
+ input_shapes,
+ ov::cmp::lt(data->back(), biggest_idx),
+ "The biggest index cannot be higher than the amount or characters in symbols input.");
+ }
+ const auto are_indices_ascending = std::is_sorted(data->begin(), data->end());
+ NODE_SHAPE_INFER_CHECK(op, input_shapes, are_indices_ascending, "Indices must be in ascending order.");
+ }
+ }
+ }
+}
+} // namespace util
+template <class TShape, class TRShape = result_shape_t<TShape>>
+std::vector<TRShape> shape_infer(const StringTensorPack* op,
+ const std::vector<TShape>& input_shapes,
+ const ITensorAccessor& tensor_accessor = make_tensor_accessor()) {
+ NODE_SHAPE_INFER_CHECK(op, input_shapes, input_shapes.size() == 3);
+ util::validate_indices(0, tensor_accessor, op, input_shapes);
+ util::validate_indices(1, tensor_accessor, op, input_shapes);
+ const auto& begins_shape = input_shapes[0];
+ const auto& ends_shape = input_shapes[1];
+ NODE_SHAPE_INFER_CHECK(op, input_shapes, begins_shape.compatible(ends_shape));
+ const auto& symbols_shape = input_shapes[2];
+ NODE_SHAPE_INFER_CHECK(op, input_shapes, symbols_shape.rank().compatible(1), "Symbols input must be 1D.");
+
+ // begins_shape and ends_shape always have to match
+ auto output_shapes = std::vector<TRShape>();
+ if (begins_shape.is_dynamic() && ends_shape.is_static()) {
+ output_shapes.push_back(ends_shape);
+ } else {
+ output_shapes.push_back(begins_shape);
+ }
+ return {std::move(output_shapes)};
+}
+} // namespace v15
+} // namespace op
+} // namespace ov
diff --git a/src/core/shape_inference/include/string_tensor_unpack_shape_inference.hpp b/src/core/shape_inference/include/string_tensor_unpack_shape_inference.hpp
new file mode 100644
index 00000000000..f7b79504764
--- /dev/null
+++ b/src/core/shape_inference/include/string_tensor_unpack_shape_inference.hpp
@@ -0,0 +1,54 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <cmath>
+
+#include "openvino/op/string_tensor_unpack.hpp"
+#include "shape_infer_type_utils.hpp"
+#include "utils.hpp"
+
+namespace ov {
+namespace op {
+namespace v15 {
+namespace util {
+static inline Tensor get_string_tensor(const Node* op, const ITensorAccessor& tensor_accessor) {
+ if (auto t = tensor_accessor(0)) {
+ return t;
+ } else if (const auto& constant = as_type_ptr<v0::Constant>(op->get_input_node_shared_ptr(0))) {
+ return constant->get_tensor_view();
+ } else {
+ return {};
+ }
+}
+} // namespace util
+template <class TShape, class TRShape = result_shape_t<TShape>>
+std::vector<TRShape> shape_infer(const StringTensorUnpack* op,
+ const std::vector<TShape>& input_shapes,
+ const ITensorAccessor& tensor_accessor = make_tensor_accessor()) {
+ NODE_VALIDATION_CHECK(op, input_shapes.size() == 1);
+ const auto& data_shape = input_shapes[0];
+ auto output_shapes = std::vector<TRShape>{data_shape, data_shape};
+ if (data_shape.is_static()) {
+ if (const auto string_data = util::get_string_tensor(op, tensor_accessor)) {
+ const auto string_count = string_data.get_size();
+ const auto tensor_data = string_data.data<std::string>();
+ size_t total_length = 0;
+ for (auto it = tensor_data; it != std::next(tensor_data, string_count); ++it) {
+ total_length += (*it).length();
+ }
+ output_shapes.emplace_back(TRShape{static_cast<typename TRShape::value_type>(total_length)});
+ } else {
+ output_shapes.emplace_back(ov::PartialShape{ov::Dimension::dynamic()});
+ }
+ } else {
+ output_shapes.emplace_back(ov::PartialShape{ov::Dimension::dynamic()});
+ }
+
+ return output_shapes;
+}
+} // namespace v15
+} // namespace op
+} // namespace ov
diff --git a/src/core/src/op/string_tensor_pack.cpp b/src/core/src/op/string_tensor_pack.cpp
new file mode 100644
index 00000000000..791d3be1866
--- /dev/null
+++ b/src/core/src/op/string_tensor_pack.cpp
@@ -0,0 +1,52 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "itt.hpp"
+#include "openvino/core/validation_util.hpp"
+#include "openvino/op/op.hpp"
+#include "openvino/op/string_tensor_unpack.hpp"
+#include "string_tensor_pack_shape_inference.hpp"
+
+namespace ov {
+namespace op {
+namespace v15 {
+
+StringTensorPack::StringTensorPack(const Output<Node>& begins, const Output<Node>& ends, const Output<Node>& symbols)
+ : Op({begins, ends, symbols}) {
+ constructor_validate_and_infer_types();
+}
+
+void StringTensorPack::validate_and_infer_types() {
+ OV_OP_SCOPE(v15_StringTensorPack_validate_and_infer_types);
+
+ const auto& begins_element_type = get_input_element_type(0);
+ const auto& ends_element_type = get_input_element_type(1);
+ const bool is_valid_index_type = (begins_element_type == element::i32 || begins_element_type == element::i64) &&
+ begins_element_type == ends_element_type;
+ NODE_VALIDATION_CHECK(
+ this,
+ is_valid_index_type,
+ "The element types of the begins and ends input tensors must match and be of i32 or i64 type. Got: ",
+ begins_element_type,
+ " and ",
+ ends_element_type);
+
+ const auto& data_element_type = get_input_element_type(2);
+ NODE_VALIDATION_CHECK(this,
+ data_element_type == ov::element::u8,
+ "StringTensorPack expects a tensor with ov::element::u8 elements. Got: ",
+ data_element_type);
+
+ const auto output_shapes = shape_infer(this, ov::util::get_node_input_partial_shapes(*this));
+ set_output_type(0, ov::element::string, output_shapes[0]);
+}
+
+std::shared_ptr<Node> StringTensorPack::clone_with_new_inputs(const ov::OutputVector& new_args) const {
+ OV_OP_SCOPE(v15_StringTensorPack_clone_with_new_inputs);
+ check_new_args_count(this, new_args);
+ return std::make_shared<StringTensorPack>(new_args.at(0), new_args.at(1), new_args.at(2));
+}
+} // namespace v15
+} // namespace op
+} // namespace ov
diff --git a/src/core/src/op/string_tensor_unpack.cpp b/src/core/src/op/string_tensor_unpack.cpp
new file mode 100644
index 00000000000..8cc7fbf5df3
--- /dev/null
+++ b/src/core/src/op/string_tensor_unpack.cpp
@@ -0,0 +1,42 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/op/string_tensor_unpack.hpp"
+
+#include "itt.hpp"
+#include "openvino/core/validation_util.hpp"
+#include "openvino/op/op.hpp"
+#include "string_tensor_unpack_shape_inference.hpp"
+
+namespace ov {
+namespace op {
+namespace v15 {
+
+StringTensorUnpack::StringTensorUnpack(const Output<Node>& data) : Op({data}) {
+ constructor_validate_and_infer_types();
+}
+
+void StringTensorUnpack::validate_and_infer_types() {
+ OV_OP_SCOPE(v15_StringTensorUnpack_validate_and_infer_types);
+
+ const auto& data_element_type = get_input_element_type(0);
+ NODE_VALIDATION_CHECK(this,
+ data_element_type == ov::element::string,
+ "StringTensorUnpack expects a tensor with string elements. Got: ",
+ data_element_type);
+
+ const auto output_shapes = shape_infer(this, ov::util::get_node_input_partial_shapes(*this));
+ set_output_type(0, ov::element::i32, output_shapes[0]);
+ set_output_type(1, ov::element::i32, output_shapes[1]);
+ set_output_type(2, ov::element::u8, output_shapes[2]);
+}
+
+std::shared_ptr<Node> StringTensorUnpack::clone_with_new_inputs(const ov::OutputVector& new_args) const {
+ OV_OP_SCOPE(v15_StringTensorUnpack_clone_with_new_inputs);
+ check_new_args_count(this, new_args);
+ return std::make_shared<StringTensorUnpack>(new_args.at(0));
+}
+} // namespace v15
+} // namespace op
+} // namespace ov
diff --git a/src/core/tests/type_prop/string_tensor_pack.cpp b/src/core/tests/type_prop/string_tensor_pack.cpp
new file mode 100644
index 00000000000..005ee56efef
--- /dev/null
+++ b/src/core/tests/type_prop/string_tensor_pack.cpp
@@ -0,0 +1,140 @@
+// Copyright (C) 2018-2023 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/op/string_tensor_pack.hpp"
+
+#include <gtest/gtest.h>
+
+#include "common_test_utils/test_assertions.hpp"
+#include "common_test_utils/type_prop.hpp"
+
+namespace ov {
+namespace test {
+
+using ov::op::v0::Parameter;
+using testing::HasSubstr;
+
+class TypePropStringTensorPackTestSuite : public ::testing::TestWithParam<ov::PartialShape> {};
+
+TEST_P(TypePropStringTensorPackTestSuite, TypePropStringTensorPackTestSuite) {
+ const auto& indices_shape = GetParam();
+ const auto begins = std::make_shared<Parameter>(element::i32, indices_shape);
+ const auto ends = std::make_shared<Parameter>(element::i32, indices_shape);
+ const auto symbols = std::make_shared<Parameter>(element::u8, PartialShape{100});
+ const auto op = std::make_shared<op::v15::StringTensorPack>(begins, ends, symbols);
+
+ EXPECT_EQ(op->get_output_element_type(0), element::string);
+ EXPECT_EQ(op->get_output_partial_shape(0), indices_shape);
+}
+
+INSTANTIATE_TEST_SUITE_P(TypePropStringTensorPackTestSuite,
+ TypePropStringTensorPackTestSuite,
+ ::testing::Values(PartialShape{3},
+ PartialShape{3, 9},
+ PartialShape{3, 9, 1},
+ PartialShape::dynamic(),
+ PartialShape{{4, 5}, {5, 6}},
+ PartialShape{{4, 5}, 5},
+ PartialShape{3, Dimension::dynamic()}));
+
+using TypePropStringTensorPackV15Test = TypePropOpTest<op::v15::StringTensorPack>;
+
+TEST_F(TypePropStringTensorPackV15Test, begins_static_ends_dynamic) {
+ auto begins_shape = PartialShape{3};
+ auto ends_shape = PartialShape{Dimension::dynamic()};
+ auto begins_symbols = set_shape_symbols(begins_shape);
+ auto ends_symbols = set_shape_symbols(ends_shape);
+ const auto begins = std::make_shared<Parameter>(element::i32, begins_shape);
+ const auto ends = std::make_shared<Parameter>(element::i32, ends_shape);
+ const auto symbols = std::make_shared<Parameter>(element::u8, PartialShape{Dimension::dynamic()});
+ const auto op = make_op(begins, ends, symbols);
+
+ EXPECT_EQ(op->get_output_element_type(0), element::string);
+ EXPECT_EQ(op->get_output_partial_shape(0), begins_shape);
+ EXPECT_EQ(get_shape_symbols(op->get_output_partial_shape(0)), begins_symbols);
+}
+
+TEST_F(TypePropStringTensorPackV15Test, ends_static_begins_dynamic) {
+ auto begins_shape = PartialShape{Dimension::dynamic()};
+ auto ends_shape = PartialShape{5};
+ auto begins_symbols = set_shape_symbols(begins_shape);
+ auto ends_symbols = set_shape_symbols(ends_shape);
+ const auto begins = std::make_shared<Parameter>(element::i32, begins_shape);
+ const auto ends = std::make_shared<Parameter>(element::i32, ends_shape);
+ const auto symbols = std::make_shared<Parameter>(element::u8, PartialShape{Dimension::dynamic()});
+ const auto op = make_op(begins, ends, symbols);
+
+ EXPECT_EQ(op->get_output_element_type(0), element::string);
+ EXPECT_EQ(op->get_output_partial_shape(0), ends_shape);
+ EXPECT_EQ(get_shape_symbols(op->get_output_partial_shape(0)), ends_symbols);
+}
+
+TEST_F(TypePropStringTensorPackV15Test, default_case) {
+ const auto begins = std::make_shared<Parameter>(element::i32, PartialShape{3});
+ const auto ends = std::make_shared<Parameter>(element::i32, PartialShape{3});
+ const auto symbols = std::make_shared<Parameter>(element::u8, PartialShape{100});
+ const auto op = make_op(begins, ends, symbols);
+
+ EXPECT_EQ(op->get_output_element_type(0), element::string);
+ EXPECT_EQ(op->get_output_partial_shape(0), PartialShape{3});
+}
+
+TEST_F(TypePropStringTensorPackV15Test, int64_indices) {
+ const auto begins = std::make_shared<Parameter>(element::i64, PartialShape{3});
+ const auto ends = std::make_shared<Parameter>(element::i64, PartialShape{3});
+ const auto symbols = std::make_shared<Parameter>(element::u8, PartialShape{100});
+ const auto op = make_op(begins, ends, symbols);
+
+ EXPECT_EQ(op->get_output_element_type(0), element::string);
+ EXPECT_EQ(op->get_output_partial_shape(0), PartialShape{3});
+}
+
+TEST_F(TypePropStringTensorPackV15Test, incorrect_symbols_shape) {
+ const auto begins = std::make_shared<Parameter>(element::i32, PartialShape{2});
+ const auto ends = std::make_shared<Parameter>(element::i32, PartialShape{2});
+ const auto symbols = std::make_shared<Parameter>(element::u8, PartialShape{100, 3});
+ OV_EXPECT_THROW(std::ignore = make_op(begins, ends, symbols),
+ NodeValidationFailure,
+ HasSubstr("Symbols input must be 1D."));
+}
+
+TEST_F(TypePropStringTensorPackV15Test, begins_ends_shape_mismatch) {
+ const auto begins = std::make_shared<Parameter>(element::i32, PartialShape{3});
+ const auto ends = std::make_shared<Parameter>(element::i32, PartialShape{2});
+ const auto symbols = std::make_shared<Parameter>(element::u8, PartialShape{100});
+ OV_EXPECT_THROW(std::ignore = make_op(begins, ends, symbols),
+ NodeValidationFailure,
+ HasSubstr("Check 'begins_shape.compatible(ends_shape)' failed"));
+}
+
+TEST_F(TypePropStringTensorPackV15Test, incorrect_types) {
+ {
+ const auto begins = std::make_shared<Parameter>(element::i8, PartialShape{3});
+ const auto ends = std::make_shared<Parameter>(element::i8, PartialShape{3});
+ const auto symbols = std::make_shared<Parameter>(element::u8, PartialShape{100});
+ OV_EXPECT_THROW(
+ std::ignore = make_op(begins, ends, symbols),
+ NodeValidationFailure,
+ HasSubstr("The element types of the begins and ends input tensors must match and be of i32 or i64 type"));
+ }
+ {
+ const auto begins = std::make_shared<Parameter>(element::i32, PartialShape{3});
+ const auto ends = std::make_shared<Parameter>(element::i64, PartialShape{3});
+ const auto symbols = std::make_shared<Parameter>(element::u8, PartialShape{100});
+ OV_EXPECT_THROW(
+ std::ignore = make_op(begins, ends, symbols),
+ NodeValidationFailure,
+ HasSubstr("The element types of the begins and ends input tensors must match and be of i32 or i64 type"));
+ }
+ {
+ const auto begins = std::make_shared<Parameter>(element::i32, PartialShape{3});
+ const auto ends = std::make_shared<Parameter>(element::i32, PartialShape{3});
+ const auto symbols = std::make_shared<Parameter>(element::f32, PartialShape{100});
+ OV_EXPECT_THROW(std::ignore = make_op(begins, ends, symbols),
+ NodeValidationFailure,
+ HasSubstr("StringTensorPack expects a tensor with ov::element::u8 elements"));
+ }
+}
+} // namespace test
+} // namespace ov
diff --git a/src/core/tests/type_prop/string_tensor_unpack.cpp b/src/core/tests/type_prop/string_tensor_unpack.cpp
new file mode 100644
index 00000000000..afdd4470663
--- /dev/null
+++ b/src/core/tests/type_prop/string_tensor_unpack.cpp
@@ -0,0 +1,89 @@
+// Copyright (C) 2018-2023 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/op/string_tensor_unpack.hpp"
+
+#include <gtest/gtest.h>
+
+#include "common_test_utils/test_assertions.hpp"
+#include "common_test_utils/type_prop.hpp"
+#include "openvino/op/constant.hpp"
+
+namespace ov {
+namespace test {
+namespace util {
+size_t static get_character_count(const std::vector<std::string> text) {
+ size_t total_char_count = 0;
+ for (auto it = text.begin(); it != text.end(); it = std::next(it)) {
+ total_char_count += it->size();
+ }
+ return total_char_count;
+}
+} // namespace util
+
+using ov::op::v0::Constant;
+using ov::op::v0::Parameter;
+using testing::HasSubstr;
+
+class TypePropStringTensorUnpackTestSuite : public ::testing::TestWithParam<ov::PartialShape> {};
+
+TEST_P(TypePropStringTensorUnpackTestSuite, TypePropStringTensorUnpackTestSuite) {
+ const auto& data_shape = GetParam();
+ const auto data = std::make_shared<Parameter>(element::string, data_shape);
+ const auto op = std::make_shared<op::v15::StringTensorUnpack>(data);
+ op->validate_and_infer_types();
+
+ EXPECT_EQ(op->get_output_element_type(0), element::i32);
+ EXPECT_EQ(op->get_output_element_type(1), element::i32);
+ EXPECT_EQ(op->get_output_element_type(2), element::u8);
+ EXPECT_EQ(op->get_output_partial_shape(0), data_shape);
+ EXPECT_EQ(op->get_output_partial_shape(1), data_shape);
+ EXPECT_EQ(op->get_output_partial_shape(2), PartialShape{Dimension::dynamic()});
+}
+
+INSTANTIATE_TEST_SUITE_P(TypePropStringTensorUnpackTestSuite,
+ TypePropStringTensorUnpackTestSuite,
+ ::testing::Values(PartialShape{3},
+ PartialShape{3, 9},
+ PartialShape{3, 9, 1},
+ PartialShape::dynamic(),
+ PartialShape{{4, 5}, {5, 6}},
+ PartialShape{{4, 5}, 5},
+ PartialShape{3, Dimension::dynamic()}));
+
+class TypePropStringTensorUnpackTestSuiteWithConstant
+ : public ::testing::TestWithParam<std::tuple<ov::Shape, std::vector<std::string>>> {};
+
+TEST_P(TypePropStringTensorUnpackTestSuiteWithConstant, TypePropStringTensorUnpackTestSuiteWithConstant) {
+ const auto& params = GetParam();
+ const auto& shape = std::get<0>(params);
+ const auto& text = std::get<1>(params);
+ const auto data = std::make_shared<Constant>(element::string, shape, text);
+ const auto op = std::make_shared<op::v15::StringTensorUnpack>(data);
+
+ EXPECT_EQ(op->get_output_element_type(0), element::i32);
+ EXPECT_EQ(op->get_output_element_type(1), element::i32);
+ EXPECT_EQ(op->get_output_element_type(2), element::u8);
+ EXPECT_EQ(op->get_output_shape(0), shape);
+ EXPECT_EQ(op->get_output_shape(1), shape);
+ EXPECT_EQ(op->get_output_shape(2), Shape{util::get_character_count(text)});
+}
+
+INSTANTIATE_TEST_SUITE_P(
+ TypePropStringTensorUnpackTestSuiteWithConstant,
+ TypePropStringTensorUnpackTestSuiteWithConstant,
+ ::testing::Values(std::make_tuple(Shape{2}, std::vector<std::string>{"Intel", "OpenVINO"}),
+ std::make_tuple(Shape{1, 1, 2}, std::vector<std::string>{"Intel", "OpenVINO"}),
+ std::make_tuple(Shape{0}, std::vector<std::string>{}),
+ std::make_tuple(Shape{4}, std::vector<std::string>{"Intel", "OpenVINO", "", "AI"}),
+ std::make_tuple(Shape{2, 2}, std::vector<std::string>{"Intel", "OpenVINO", "Gen", "AI"})));
+
+TEST(type_prop, StringTensorUnpack_incorrect_data_type) {
+ const auto data = std::make_shared<Parameter>(element::u8, PartialShape{3, 6});
+ OV_EXPECT_THROW(std::ignore = std::make_shared<op::v15::StringTensorUnpack>(data),
+ NodeValidationFailure,
+ HasSubstr("StringTensorUnpack expects a tensor with string elements"));
+}
+} // namespace test
+} // namespace ov
diff --git a/src/core/tests/visitors/op/string_tensor_pack.cpp b/src/core/tests/visitors/op/string_tensor_pack.cpp
new file mode 100644
index 00000000000..286e6ac3735
--- /dev/null
+++ b/src/core/tests/visitors/op/string_tensor_pack.cpp
@@ -0,0 +1,20 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+#include "openvino/op/string_tensor_pack.hpp"
+
+#include <gtest/gtest.h>
+
+#include "visitors/visitors.hpp"
+
+TEST(attributes, string_tensor_pack_op) {
+ ov::test::NodeBuilder::opset().insert<ov::op::v15::StringTensorPack>();
+ const auto begins = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::Shape{3});
+ const auto ends = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::Shape{3});
+ const auto symbols = std::make_shared<ov::op::v0::Parameter>(ov::element::u8, ov::Shape{3});
+ const auto string_tensor_pack = std::make_shared<ov::op::v15::StringTensorPack>(begins, ends, symbols);
+ ov::test::NodeBuilder builder(string_tensor_pack, {begins, ends, symbols});
+ ASSERT_NO_THROW(std::ignore = ov::as_type_ptr<ov::op::v15::StringTensorPack>(builder.create()));
+ const auto expected_attr_count = 0;
+ EXPECT_EQ(builder.get_value_map_size(), expected_attr_count);
+}
diff --git a/src/core/tests/visitors/op/string_tensor_unpack.cpp b/src/core/tests/visitors/op/string_tensor_unpack.cpp
new file mode 100644
index 00000000000..e90176d60a6
--- /dev/null
+++ b/src/core/tests/visitors/op/string_tensor_unpack.cpp
@@ -0,0 +1,18 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+#include "openvino/op/string_tensor_unpack.hpp"
+
+#include <gtest/gtest.h>
+
+#include "visitors/visitors.hpp"
+
+TEST(attributes, string_tensor_unpack_op) {
+ ov::test::NodeBuilder::opset().insert<ov::op::v15::StringTensorUnpack>();
+ const auto data = std::make_shared<ov::op::v0::Parameter>(ov::element::string, ov::Shape{4});
+ const auto string_tensor_unpack = std::make_shared<ov::op::v15::StringTensorUnpack>(data);
+ ov::test::NodeBuilder builder(string_tensor_unpack, {data});
+ ASSERT_NO_THROW(std::ignore = ov::as_type_ptr<ov::op::v15::StringTensorUnpack>(builder.create()));
+ const auto expected_attr_count = 0;
+ EXPECT_EQ(builder.get_value_map_size(), expected_attr_count);
+}
diff --git a/src/plugins/intel_cpu/src/shape_inference/shape_inference.cpp b/src/plugins/intel_cpu/src/shape_inference/shape_inference.cpp
index ac432ac424d..947d7552321 100644
--- a/src/plugins/intel_cpu/src/shape_inference/shape_inference.cpp
+++ b/src/plugins/intel_cpu/src/shape_inference/shape_inference.cpp
@@ -111,6 +111,8 @@
#include "squeeze_shape_inference.hpp"
#include "static_shape.hpp"
#include "strided_slice_shape_inference.hpp"
+#include "string_tensor_pack_shape_inference.hpp"
+#include "string_tensor_unpack_shape_inference.hpp"
#include "tile_shape_inference.hpp"
#include "topk_shape_inference.hpp"
#include "transpose_shape_inference.hpp"
@@ -402,6 +404,8 @@ using IStaticShapeInferFactory =
template <>
const IStaticShapeInferFactory::TRegistry IStaticShapeInferFactory::registry{
// opset15
+ _OV_OP_SHAPE_INFER_MASK_REG(op::v15::StringTensorUnpack, ShapeInferTA, util::bit::mask(0)),
+ _OV_OP_SHAPE_INFER_MASK_REG(op::v15::StringTensorPack, ShapeInferTA, util::bit::mask(0, 1)),
_OV_OP_SHAPE_INFER_MASK_REG(opset15::EmbeddingBagOffsets, ShapeInferTA, util::bit::mask()),
_OV_OP_SHAPE_INFER_MASK_REG(opset15::EmbeddingBagPacked, ShapeInferTA, util::bit::mask()),
_OV_OP_SHAPE_INFER_MASK_REG(op::v15::Col2Im, ShapeInferTA, util::bit::mask(1, 2)),
diff --git a/src/plugins/intel_cpu/tests/unit/shape_inference_test/string_tensor_pack_shape_inference_test.cpp b/src/plugins/intel_cpu/tests/unit/shape_inference_test/string_tensor_pack_shape_inference_test.cpp
new file mode 100644
index 00000000000..1ddf2c8636d
--- /dev/null
+++ b/src/plugins/intel_cpu/tests/unit/shape_inference_test/string_tensor_pack_shape_inference_test.cpp
@@ -0,0 +1,282 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <gtest/gtest.h>
+
+#include "common_test_utils/test_assertions.hpp"
+#include "utils.hpp"
+#include "string_tensor_pack_shape_inference.hpp"
+
+using namespace ov;
+using namespace ov::intel_cpu;
+using ov::op::v0::Constant;
+using ov::op::v0::Parameter;
+
+class StringTensorPackStaticTestSuite : public ::testing::TestWithParam<std::tuple<
+ Shape, // begins/ends indices shape
+ std::vector<int32_t>, // begins
+ std::vector<int32_t>, // ends
+ std::vector<uint8_t> // symbols
+ >> {};
+
+TEST_P(StringTensorPackStaticTestSuite, StringTensorPackStaticShapeInference) {
+ const auto& param = GetParam();
+ const auto& indices_shape = std::get<0>(param);
+ const auto& begins_param = std::get<1>(param);
+ const auto& ends_param = std::get<2>(param);
+ const auto& symbols_param = std::get<3>(param);
+
+ const auto begins = std::make_shared<Constant>(element::i32, indices_shape, begins_param);
+ const auto ends = std::make_shared<Constant>(element::i32, indices_shape, ends_param);
+ const auto symbols = std::make_shared<Constant>(element::u8, Shape{symbols_param.size()}, symbols_param);
+
+ const auto input_shapes = ShapeVector{indices_shape, indices_shape, Shape{symbols_param.size()}};
+ const auto input_shape_refs = make_static_shape_refs(input_shapes);
+ const auto op = std::make_shared<op::v15::StringTensorPack>(begins, ends, symbols);
+ auto shape_infer = make_shape_inference(op);
+ const auto output_shapes = *shape_infer->infer(input_shape_refs, make_tensor_accessor());
+
+ EXPECT_EQ(output_shapes.size(), 1);
+ EXPECT_EQ(output_shapes[0], StaticShape(indices_shape));
+}
+
+INSTANTIATE_TEST_SUITE_P(
+ StringTensorPackStaticShapeInferenceTests,
+ StringTensorPackStaticTestSuite,
+ ::testing::Values(
+ // "Intel"
+ std::make_tuple(
+ Shape{1},
+ std::vector<int32_t>{0},
+ std::vector<int32_t>{5},
+ std::vector<uint8_t>{0x49, 0x6e, 0x74, 0x65, 0x6c}),
+ // "Intel", "OpenVINO"
+ std::make_tuple(
+ Shape{2},
+ std::vector<int32_t>{0, 5},
+ std::vector<int32_t>{5, 13},
+ std::vector<uint8_t>{0x49, 0x6e, 0x74, 0x65, 0x6c, 0x4f,
+ 0x70, 0x65, 0x6e, 0x56, 0x49, 0x4e, 0x4f}),
+ // " "
+ std::make_tuple(
+ Shape{1},
+ std::vector<int32_t>{0},
+ std::vector<int32_t>{0},
+ std::vector<uint8_t>{0x20}),
+ // ""
+ std::make_tuple(
+ Shape{0},
+ std::vector<int32_t>{},
+ std::vector<int32_t>{},
+ std::vector<uint8_t>{}),
+ // (2, 2) shape; "1", "2", "3", "4"
+ std::make_tuple(
+ Shape{2, 2},
+ std::vector<int32_t>{0, 1, 2, 3},
+ std::vector<int32_t>{1, 2, 3, 4},
+ std::vector<uint8_t>{0x31, 0x32, 0x33, 0x34}),
+ // (1, 2) shape; "1", "2"
+ std::make_tuple(
+ Shape{1, 2},
+ std::vector<int32_t>{0, 1},
+ std::vector<int32_t>{1, 2},
+ std::vector<uint8_t>{0x31, 0x32}),
+ // skipped symbols; "1", "9"
+ std::make_tuple(
+ Shape{2},
+ std::vector<int32_t>{0, 8},
+ std::vector<int32_t>{1, 9},
+ std::vector<uint8_t>{0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x3}),
+ // mixed strings; "1", "", " ", "4"
+ std::make_tuple(
+ Shape{2, 2},
+ std::vector<int32_t>{0, 1, 1, 2},
+ std::vector<int32_t>{1, 1, 2, 3},
+ std::vector<uint8_t>{0x31, 0x20, 0x34})
+ )
+);
+
+class StringTensorPackStaticShapeInferenceWithTensorAccessorTest: public OpStaticShapeInferenceTest<op::v15::StringTensorPack> {};
+
+TEST_F(StringTensorPackStaticShapeInferenceWithTensorAccessorTest, data_from_tensor_accessor_1) {
+ const auto begins_param = std::make_shared<Parameter>(element::i32, ov::PartialShape::dynamic());
+ const auto ends_param = std::make_shared<Parameter>(element::i32, ov::PartialShape::dynamic());
+ const auto symbols_param = std::make_shared<Parameter>(element::u8, ov::PartialShape::dynamic());
+ const auto op = make_op(begins_param, ends_param, symbols_param);
+ int32_t begins[] = {0};
+ int32_t ends[] = {5};
+ uint8_t symbols[] = {0x49, 0x6e, 0x74, 0x65, 0x6c};
+ const auto const_inputs = std::unordered_map<size_t, Tensor>{
+ {0, {element::i32, Shape{1}, begins}},
+ {1, {element::i32, Shape{1}, ends}},
+ {2, {element::u8, Shape{5}, symbols}}};
+
+ const auto input_shapes = ShapeVector{Shape{1}, Shape{1}, Shape{5}};
+ auto shape_infer = make_shape_inference(op);
+ const auto input_shape_refs = make_static_shape_refs(input_shapes);
+ const auto output_shapes = *shape_infer->infer(input_shape_refs, make_tensor_accessor(const_inputs));
+ EXPECT_EQ(output_shapes.size(), 1);
+ EXPECT_EQ(output_shapes[0], StaticShape({1}));
+}
+
+TEST_F(StringTensorPackStaticShapeInferenceWithTensorAccessorTest, data_from_tensor_accessor_2) {
+ const auto begins_param = std::make_shared<Parameter>(element::i32, ov::PartialShape::dynamic());
+ const auto ends_param = std::make_shared<Parameter>(element::i32, ov::PartialShape::dynamic());
+ const auto symbols_param = std::make_shared<Parameter>(element::u8, ov::PartialShape::dynamic());
+ const auto op = make_op(begins_param, ends_param, symbols_param);
+ int32_t begins[] = {0, 1, 2, 3};
+ int32_t ends[] = {1, 2, 3, 4};
+ uint8_t symbols[] = {0x31, 0x32, 0x33, 0x34};
+ const auto const_inputs = std::unordered_map<size_t, Tensor>{
+ {0, {element::i32, Shape{2, 2}, begins}},
+ {1, {element::i32, Shape{2, 2}, ends}},
+ {2, {element::u8, Shape{4}, symbols}}};
+
+ const auto input_shapes = ShapeVector{Shape{2, 2}, Shape{2, 2}, Shape{4}};
+ auto shape_infer = make_shape_inference(op);
+ const auto input_shape_refs = make_static_shape_refs(input_shapes);
+ const auto output_shapes = *shape_infer->infer(input_shape_refs, make_tensor_accessor(const_inputs));
+ EXPECT_EQ(output_shapes.size(), 1);
+ EXPECT_EQ(output_shapes[0], StaticShape({2, 2}));
+}
+
+TEST_F(StringTensorPackStaticShapeInferenceWithTensorAccessorTest, data_from_tensor_accessor_3) {
+ const auto begins_param = std::make_shared<Parameter>(element::i32, ov::PartialShape::dynamic());
+ const auto ends_param = std::make_shared<Parameter>(element::i32, ov::PartialShape::dynamic());
+ const auto symbols_param = std::make_shared<Parameter>(element::u8, ov::PartialShape::dynamic());
+ const auto op = make_op(begins_param, ends_param, symbols_param);
+ int32_t begins[] = {0, 1};
+ int32_t ends[] = {1, 2};
+ uint8_t symbols[] = {0x31, 0x32};
+ const auto const_inputs = std::unordered_map<size_t, Tensor>{
+ {0, {element::i32, Shape{1, 2}, begins}},
+ {1, {element::i32, Shape{1, 2}, ends}},
+ {2, {element::u8, Shape{2}, symbols}}};
+
+ const auto input_shapes = ShapeVector{Shape{1, 2}, Shape{1, 2}, Shape{2}};
+ auto shape_infer = make_shape_inference(op);
+ const auto input_shape_refs = make_static_shape_refs(input_shapes);
+ const auto output_shapes = *shape_infer->infer(input_shape_refs, make_tensor_accessor(const_inputs));
+ EXPECT_EQ(output_shapes.size(), 1);
+ EXPECT_EQ(output_shapes[0], StaticShape({1, 2}));
+}
+
+TEST_F(StringTensorPackStaticShapeInferenceWithTensorAccessorTest, data_from_tensor_accessor_4) {
+ const auto begins_param = std::make_shared<Parameter>(element::i32, ov::PartialShape::dynamic());
+ const auto ends_param = std::make_shared<Parameter>(element::i32, ov::PartialShape::dynamic());
+ const auto symbols_param = std::make_shared<Parameter>(element::u8, ov::PartialShape::dynamic());
+ const auto op = make_op(begins_param, ends_param, symbols_param);
+ int32_t begins[] = {0, 8};
+ int32_t ends[] = {1, 9};
+ uint8_t symbols[] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x3};
+ const auto const_inputs = std::unordered_map<size_t, Tensor>{
+ {0, {element::i32, Shape{2}, begins}},
+ {1, {element::i32, Shape{2}, ends}},
+ {2, {element::u8, Shape{9}, symbols}}};
+
+ const auto input_shapes = ShapeVector{Shape{2}, Shape{2}, Shape{9}};
+ auto shape_infer = make_shape_inference(op);
+ const auto input_shape_refs = make_static_shape_refs(input_shapes);
+ const auto output_shapes = *shape_infer->infer(input_shape_refs, make_tensor_accessor(const_inputs));
+ EXPECT_EQ(output_shapes.size(), 1);
+ EXPECT_EQ(output_shapes[0], StaticShape({2}));
+}
+
+TEST_F(StringTensorPackStaticShapeInferenceWithTensorAccessorTest, data_from_tensor_accessor_5) {
+ const auto begins_param = std::make_shared<Parameter>(element::i32, ov::PartialShape::dynamic());
+ const auto ends_param = std::make_shared<Parameter>(element::i32, ov::PartialShape::dynamic());
+ const auto symbols_param = std::make_shared<Parameter>(element::u8, ov::PartialShape::dynamic());
+ const auto op = make_op(begins_param, ends_param, symbols_param);
+ int32_t begins[] = {0, 1, 1, 2};
+ int32_t ends[] = {1, 1, 2, 3};
+ uint8_t symbols[] = {0x31, 0x20, 0x34};
+ const auto const_inputs = std::unordered_map<size_t, Tensor>{
+ {0, {element::i32, Shape{2, 2}, begins}},
+ {1, {element::i32, Shape{2, 2}, ends}},
+ {2, {element::u8, Shape{3}, symbols}}};
+
+ const auto input_shapes = ShapeVector{Shape{2, 2}, Shape{2, 2}, Shape{3}};
+ auto shape_infer = make_shape_inference(op);
+ const auto input_shape_refs = make_static_shape_refs(input_shapes);
+ const auto output_shapes = *shape_infer->infer(input_shape_refs, make_tensor_accessor(const_inputs));
+ EXPECT_EQ(output_shapes.size(), 1);
+ EXPECT_EQ(output_shapes[0], StaticShape({2, 2}));
+}
+
+TEST_F(StringTensorPackStaticShapeInferenceWithTensorAccessorTest, indices_validation) {
+ const auto begins_param = std::make_shared<Parameter>(element::i32, ov::PartialShape::dynamic());
+ const auto ends_param = std::make_shared<Parameter>(element::i32, ov::PartialShape::dynamic());
+ const auto symbols_param = std::make_shared<Parameter>(element::u8, ov::PartialShape::dynamic());
+ const auto op = make_op(begins_param, ends_param, symbols_param);
+ uint8_t symbols[] = {0x31, 0x20, 0x34};
+ const auto input_shapes = ShapeVector{Shape{2, 2}, Shape{2, 2}, Shape{3}};
+ auto shape_infer = make_shape_inference(op);
+ const auto input_shape_refs = make_static_shape_refs(input_shapes);
+ { // negative begins indices
+ int32_t begins[] = {-1, 1, 1, 2};
+ int32_t ends[] = {1, 1, 2, 3};
+ const auto const_inputs = std::unordered_map<size_t, Tensor>{
+ {0, {element::i32, Shape{2, 2}, begins}},
+ {1, {element::i32, Shape{2, 2}, ends}},
+ {2, {element::u8, Shape{3}, symbols}}};
+ OV_EXPECT_THROW(std::ignore = *shape_infer->infer(input_shape_refs, make_tensor_accessor(const_inputs)),
+ NodeValidationFailure,
+ testing::HasSubstr("Indices cannot be negative"));
+ }
+ { // negative ends indices
+ int32_t begins[] = {1, 1, 1, 2};
+ int32_t ends[] = {-1, 1, 2, 3};
+ const auto const_inputs = std::unordered_map<size_t, Tensor>{
+ {0, {element::i32, Shape{2, 2}, begins}},
+ {1, {element::i32, Shape{2, 2}, ends}},
+ {2, {element::u8, Shape{3}, symbols}}};
+ OV_EXPECT_THROW(std::ignore = *shape_infer->infer(input_shape_refs, make_tensor_accessor(const_inputs)),
+ NodeValidationFailure,
+ testing::HasSubstr("Indices cannot be negative"));
+ }
+ { // begins out of bounds
+ int32_t begins[] = {1, 1, 1, 3};
+ int32_t ends[] = {1, 1, 2, 3};
+ const auto const_inputs = std::unordered_map<size_t, Tensor>{
+ {0, {element::i32, Shape{2, 2}, begins}},
+ {1, {element::i32, Shape{2, 2}, ends}},
+ {2, {element::u8, Shape{3}, symbols}}};
+ OV_EXPECT_THROW(std::ignore = *shape_infer->infer(input_shape_refs, make_tensor_accessor(const_inputs)),
+ NodeValidationFailure,
+ testing::HasSubstr("The biggest index cannot be higher than the amount or characters in symbols input"));
+ }
+ { // ends out of bounds
+ int32_t begins[] = {1, 1, 1, 2};
+ int32_t ends[] = {1, 1, 2, 4};
+ const auto const_inputs = std::unordered_map<size_t, Tensor>{
+ {0, {element::i32, Shape{2, 2}, begins}},
+ {1, {element::i32, Shape{2, 2}, ends}},
+ {2, {element::u8, Shape{3}, symbols}}};
+ OV_EXPECT_THROW(std::ignore = *shape_infer->infer(input_shape_refs, make_tensor_accessor(const_inputs)),
+ NodeValidationFailure,
+ testing::HasSubstr("The biggest index cannot be higher than the amount or characters in symbols input"));
+ }
+ { // unsorted begins
+ int32_t begins[] = {1, 3, 1, 2};
+ int32_t ends[] = {1, 1, 2, 3};
+ const auto const_inputs = std::unordered_map<size_t, Tensor>{
+ {0, {element::i32, Shape{2, 2}, begins}},
+ {1, {element::i32, Shape{2, 2}, ends}},
+ {2, {element::u8, Shape{3}, symbols}}};
+ OV_EXPECT_THROW(std::ignore = *shape_infer->infer(input_shape_refs, make_tensor_accessor(const_inputs)),
+ NodeValidationFailure,
+ testing::HasSubstr("Indices must be in ascending order"));
+ }
+ { // unsorted ends
+ int32_t begins[] = {1, 1, 1, 2};
+ int32_t ends[] = {1, 1, 5, 3};
+ const auto const_inputs = std::unordered_map<size_t, Tensor>{
+ {0, {element::i32, Shape{2, 2}, begins}},
+ {1, {element::i32, Shape{2, 2}, ends}},
+ {2, {element::u8, Shape{3}, symbols}}};
+ OV_EXPECT_THROW(std::ignore = *shape_infer->infer(input_shape_refs, make_tensor_accessor(const_inputs)),
+ NodeValidationFailure,
+ testing::HasSubstr("Indices must be in ascending order"));
+ }
+}
diff --git a/src/plugins/intel_cpu/tests/unit/shape_inference_test/string_tensor_unpack_shape_inference_test.cpp b/src/plugins/intel_cpu/tests/unit/shape_inference_test/string_tensor_unpack_shape_inference_test.cpp
new file mode 100644
index 00000000000..8fdf3de0413
--- /dev/null
+++ b/src/plugins/intel_cpu/tests/unit/shape_inference_test/string_tensor_unpack_shape_inference_test.cpp
@@ -0,0 +1,158 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <gtest/gtest.h>
+
+#include "common_test_utils/test_assertions.hpp"
+#include "utils.hpp"
+#include "string_tensor_unpack_shape_inference.hpp"
+
+using namespace ov;
+using namespace ov::intel_cpu;
+using ov::op::v0::Constant;
+using ov::op::v0::Parameter;
+
+static size_t get_character_count(const std::vector<std::string>& vec) {
+ size_t count = 0;
+ for (const auto& str : vec) {
+ count += str.size();
+ }
+ return count;
+}
+
+class StringTensorUnpackStaticTestSuite : public ::testing::TestWithParam<std::tuple<
+ std::vector<std::string>, // input data
+ Shape // input shape
+ >> {};
+
+class StringTensorUnpackStaticShapeInferenceTest: public OpStaticShapeInferenceTest<op::v15::StringTensorUnpack> {};
+
+TEST_F(StringTensorUnpackStaticShapeInferenceTest, data_from_tensor_accessor_1) {
+ const auto data = std::make_shared<Parameter>(element::string, ov::PartialShape::dynamic());
+ const auto op = make_op(data);
+ std::string data_val[] = {"Intel", "OpenVINO"};
+ auto const_inputs = std::unordered_map<size_t, Tensor>{{0, {element::string, Shape{2}, data_val}}};
+
+ const auto input_shapes = ShapeVector{Shape{2}};
+ auto shape_infer = make_shape_inference(op);
+ const auto input_shape_refs = make_static_shape_refs(input_shapes);
+ const auto output_shapes = *shape_infer->infer(input_shape_refs, make_tensor_accessor(const_inputs));
+ EXPECT_EQ(output_shapes.size(), 3);
+ EXPECT_EQ(output_shapes[0], StaticShape({2}));
+ EXPECT_EQ(output_shapes[1], StaticShape({2}));
+ EXPECT_EQ(output_shapes[2], StaticShape({13}));
+}
+
+TEST_F(StringTensorUnpackStaticShapeInferenceTest, data_from_tensor_accessor_2) {
+ const auto data = std::make_shared<Parameter>(element::string, ov::PartialShape::dynamic());
+ const auto op = make_op(data);
+ std::string data_val[] = {"Intel Corp", " ", "Open VINO", "", "Artificial Intelligence"};
+ auto const_inputs = std::unordered_map<size_t, Tensor>{{0, {element::string, Shape{5}, data_val}}};
+
+ const auto input_shapes = ShapeVector{Shape{5}};
+ auto shape_infer = make_shape_inference(op);
+ const auto input_shape_refs = make_static_shape_refs(input_shapes);
+ const auto output_shapes = *shape_infer->infer(input_shape_refs, make_tensor_accessor(const_inputs));
+ EXPECT_EQ(output_shapes.size(), 3);
+ EXPECT_EQ(output_shapes[0], StaticShape({5}));
+ EXPECT_EQ(output_shapes[1], StaticShape({5}));
+ EXPECT_EQ(output_shapes[2], StaticShape({45}));
+}
+
+TEST_F(StringTensorUnpackStaticShapeInferenceTest, data_from_tensor_accessor_3) {
+ const auto data = std::make_shared<Parameter>(element::string, ov::PartialShape::dynamic());
+ const auto op = make_op(data);
+ std::string data_val[] = {"Intel", "OpenVINO", "AI", "Edge", "Compute", "Vision", "Neural", "Networks"};
+ auto const_inputs = std::unordered_map<size_t, Tensor>{{0, {element::string, Shape{2, 2, 2}, data_val}}};
+
+ const auto input_shapes = ShapeVector{Shape{2, 2, 2}};
+ auto shape_infer = make_shape_inference(op);
+ const auto input_shape_refs = make_static_shape_refs(input_shapes);
+ const auto output_shapes = *shape_infer->infer(input_shape_refs, make_tensor_accessor(const_inputs));
+ EXPECT_EQ(output_shapes.size(), 3);
+ EXPECT_EQ(output_shapes[0], StaticShape({2, 2, 2}));
+ EXPECT_EQ(output_shapes[1], StaticShape({2, 2, 2}));
+ EXPECT_EQ(output_shapes[2], StaticShape({46}));
+}
+
+TEST_F(StringTensorUnpackStaticShapeInferenceTest, data_from_tensor_accessor_4) {
+ const auto data = std::make_shared<Parameter>(element::string, ov::PartialShape::dynamic());
+ const auto op = make_op(data);
+ std::string data_val[] = {"In@tel", "Open#VINO", "A$I"};
+ auto const_inputs = std::unordered_map<size_t, Tensor>{{0, {element::string, Shape{1, 3}, data_val}}};
+
+ const auto input_shapes = ShapeVector{Shape{1, 3}};
+ auto shape_infer = make_shape_inference(op);
+ const auto input_shape_refs = make_static_shape_refs(input_shapes);
+ const auto output_shapes = *shape_infer->infer(input_shape_refs, make_tensor_accessor(const_inputs));
+ EXPECT_EQ(output_shapes.size(), 3);
+ EXPECT_EQ(output_shapes[0], StaticShape({1, 3}));
+ EXPECT_EQ(output_shapes[1], StaticShape({1, 3}));
+ EXPECT_EQ(output_shapes[2], StaticShape({18}));
+}
+
+TEST_P(StringTensorUnpackStaticTestSuite, StringTensorUnpackStaticShapeInference) {
+ const auto& param = GetParam();
+ const auto& input_strings = std::get<0>(param);
+ const auto& input_shape = std::get<1>(param);
+
+ const auto data = std::make_shared<Constant>(element::string, input_shape, input_strings);
+ const auto op = std::make_shared<op::v15::StringTensorUnpack>(data);
+ const auto input_shapes = ShapeVector{input_shape};
+ auto shape_infer = make_shape_inference(op);
+ const auto input_shape_refs = make_static_shape_refs(input_shapes);
+ const auto output_shapes = *shape_infer->infer(input_shape_refs, make_tensor_accessor());
+
+ EXPECT_EQ(output_shapes.size(), 3);
+ EXPECT_EQ(output_shapes[0], StaticShape(input_shape));
+ EXPECT_EQ(output_shapes[1], StaticShape(input_shape));
+ EXPECT_EQ(output_shapes[2], StaticShape({get_character_count(input_strings)}));
+}
+
+INSTANTIATE_TEST_SUITE_P(
+ StringTensorUnpackStaticShapeInferenceTests,
+ StringTensorUnpackStaticTestSuite,
+ ::testing::Values(
+ // single string
+ std::make_tuple(
+ std::vector<std::string>{"Intel"},
+ Shape{1}),
+ // multiple strings
+ std::make_tuple(
+ std::vector<std::string>{"Intel", "OpenVINO", "AI"},
+ Shape{3}),
+ // empty string
+ std::make_tuple(
+ std::vector<std::string>{""},
+ Shape{1}),
+ // strings with special characters
+ std::make_tuple(
+ std::vector<std::string>{"In@tel", "Open#VINO", "A$I"},
+ Shape{3}),
+ // strings with spaces and an empty string
+ std::make_tuple(
+ std::vector<std::string>{"Intel Corp", " ", "Open VINO", "", "Artificial Intelligence"},
+ Shape{1, 5}),
+ // empty vector
+ std::make_tuple(
+ std::vector<std::string>{},
+ Shape{0}),
+ // different shapes
+ std::make_tuple(
+ std::vector<std::string>{"Intel", "OpenVINO", "AI", "Edge"},
+ Shape{2, 2}),
+ std::make_tuple(
+ std::vector<std::string>{"Intel", "OpenVINO", "AI", "Edge", "Compute", "Vision"},
+ Shape{2, 3}),
+ std::make_tuple(
+ std::vector<std::string>{"Intel", "OpenVINO", "AI", "Edge", "Compute", "Vision", "Neural", "Networks"},
+ Shape{2, 2, 2}),
+ std::make_tuple(
+ std::vector<std::string>{"Intel", "OpenVINO", "AI", "Edge"},
+ Shape{1, 4}),
+ std::make_tuple(
+ std::vector<std::string>{"Intel"},
+ Shape{1, 1})
+ )
+);