From 8f86ab2e7be61ee52932f08ec57b41000ba0ee4b Mon Sep 17 00:00:00 2001 From: "Kazantsev, Roman" Date: Sun, 26 Nov 2023 15:05:34 +0400 Subject: [PATCH] Complete constant.hpp template methods for string Tensor Signed-off-by: Kazantsev, Roman --- src/core/include/openvino/op/constant.hpp | 85 ++++++++++++++++++++++- 1 file changed, 82 insertions(+), 3 deletions(-) diff --git a/src/core/include/openvino/op/constant.hpp b/src/core/include/openvino/op/constant.hpp index ff5dacd75da..609833df7ff 100644 --- a/src/core/include/openvino/op/constant.hpp +++ b/src/core/include/openvino/op/constant.hpp @@ -161,6 +161,9 @@ public: case Type_t::nf4: fill_data(value); break; + case Type_t::string: + fill_data(value); + break; case Type_t::undefined: case Type_t::dynamic: OPENVINO_THROW("unsupported type"); @@ -364,6 +367,9 @@ public: case Type_t::u64: cast_vector(rc, num_elements_to_cast); break; + case Type_t::string: + cast_vector(rc, num_elements_to_cast); + break; default: OPENVINO_THROW("unsupported type"); } @@ -454,7 +460,7 @@ private: template ::type = true> void cast_vector(std::vector& output_vector, size_t num_elements) const { // this function is workaround for waring during windows building @@ -511,6 +517,24 @@ private: }); } + template ::type = true> + void cast_vector(std::vector& output_vector, size_t num_elements) const { + auto output_size = std::min(num_elements, shape_size(m_shape)); + output_vector.reserve(output_size); + const auto p = get_data_ptr(); + std::copy(p, p + output_size, std::back_inserter(output_vector)); + } + + template ::type = true> + void cast_vector(std::vector& output_vector, size_t num_elements) const { + auto output_type = std::string(typeid(OUT_T{}).name()); + OPENVINO_ASSERT(false, + "cast_vector does not support casting string ov::Tensor to std::vector with elements of type " + + output_type); + } + template ::type = true> @@ -569,11 +593,19 @@ private: output.resize(element_number); } + template , + typename std::enable_if::type = true> + void fill_data(const std::string& value) { + OPENVINO_THROW("Called fill_data(std::string) with non-string element_type"); + } + template , typename std::enable_if::type = true> void fill_data(const T& value) { #ifdef __clang__ @@ -614,6 +646,22 @@ private: std::fill_n(get_data_ptr_nc(), size, v); } + template ::type = true> + void fill_data(const std::string& value) { + auto num_elements = shape_size(m_shape); + std::fill_n(get_data_ptr_nc(), num_elements, value); + } + + template , + typename std::enable_if::type = true> + void fill_data(const T& value) { + std::string type_name(typeid(value).name()); + OPENVINO_ASSERT(false, + "fill_data does not support to fill ov::Tensor of string type with value of " + type_name); + } + template , @@ -658,7 +706,8 @@ private: typename T, typename StorageDataType = fundamental_type_for, typename std::enable_if::type = true> void write_buffer(const std::vector& source) { auto p = get_data_ptr_nc(); @@ -667,6 +716,33 @@ private: } } + template ::type = true> + void write_buffer(const std::vector& source) { + // elements of string ov::Tensor is already pre-initialized in allocate_buffer + auto p = get_data_ptr_nc(); + auto num_elements = std::min(shape_size(m_shape), source.size()); + std::copy_n(source.begin(), num_elements, p); + } + + template ::type = true> + void write_buffer(const std::vector& source) { + OPENVINO_ASSERT(false, + "write_buffer does not support writing std::string elements into ov::Tensor of type:" + + ov::element::Type(Type).to_string()); + } + + template ::type = true> + void write_buffer(const std::vector& source) { + if (source.size() > 0) { + auto source_type = std::string(typeid(source[0]).name()); + OPENVINO_ASSERT( + false, + "write_buffer does not support writing elements of type " + source_type + " into string ov::Tensor"); + } + } + template , @@ -801,6 +877,9 @@ private: case Type_t::nf4: write_buffer(source); break; + case Type_t::string: + write_buffer(source); + break; case element::Type_t::undefined: case element::Type_t::dynamic: OPENVINO_THROW("unsupported type");