OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
Expand ov::Any to allow convert numerical values to others (#27824)
Pawel Raasz963b1be951b4
diff --git a/src/core/include/openvino/core/any.hpp b/src/core/include/openvino/core/any.hppindex 9badb007d52..e002756d361 100644--- a/src/core/include/openvino/core/any.hpp+++ b/src/core/include/openvino/core/any.hpp@@ -485,6 +485,7 @@ class OPENVINO_API Any {using Ptr = std::shared_ptr<Base>;virtual const std::type_info& type_info() const = 0;virtual std::vector<std::type_index> base_type_info() const = 0;+ bool is_base_type_info(const std::type_info& type_info) const;virtual const void* addressof() const = 0;void* addressof() {return const_cast<void*>(const_cast<const Base*>(this)->addressof());@@ -506,6 +507,9 @@ class OPENVINO_API Any {std::string to_string() const;bool is(const std::type_info& other) const;+ bool is_signed_integral() const;+ bool is_unsigned_integral() const;+ bool is_floating_point() const;template <class T>bool is() const {@@ -514,17 +518,24 @@ class OPENVINO_API Any {template <class T>T& as() & {- type_check(typeid(decay_t<T>));return *static_cast<decay_t<T>*>(addressof());}template <class T>const T& as() const& {- type_check(typeid(decay_t<T>));return *static_cast<const decay_t<T>*>(addressof());}+ template <class T>+ T convert() const;+protected:+ template <class U>+ [[noreturn]] U convert_impl() const;++ template <class U, class T, class... Others>+ U convert_impl() const;+virtual ~Base() = default;};@@ -685,6 +696,92 @@ class OPENVINO_API Any {T value;};+ // Generic if there is no specialization for T.+ template <class T>+ T& as_impl(...) {+ impl_check();+ if (is<T>()) {+ return _impl->as<T>();+ }++ OPENVINO_THROW("Bad as from: ", _impl->type_info().name(), " to: ", typeid(T).name());+ }++ template <class T, typename std::enable_if<std::is_same<T, std::string>::value>::type* = nullptr>+ T& as_impl(int) {+ if (_impl != nullptr) {+ if (_impl->is<T>()) {+ return _impl->as<T>();+ } else {+ _temp = std::make_shared<Impl<std::string>>();+ _impl->read_to(*_temp);+ return _temp->as<std::string>();+ }+ } else {+ _temp = std::make_shared<Impl<std::string>>();+ return _temp->as<std::string>();+ }+ }++ template <+ class T,+ typename std::enable_if<std::is_convertible<T, std::shared_ptr<RuntimeAttribute>>::value>::type* = nullptr>+ T& as_impl(int) {+ if (_impl == nullptr) {+ _temp = std::make_shared<Impl<decay_t<T>>>(T{});+ return _temp->as<T>();+ } else {+ if (_impl->is<T>()) {+ return _impl->as<T>();+ } else {+ auto runtime_attribute = _impl->as_runtime_attribute();+ if (runtime_attribute == nullptr) {+ OPENVINO_THROW("Any does not contains pointer to runtime_attribute. It contains ",+ _impl->type_info().name());+ }+ auto vptr = std::dynamic_pointer_cast<typename T::element_type>(runtime_attribute);+ if (vptr == nullptr && T::element_type::get_type_info_static() != runtime_attribute->get_type_info() &&+ T::element_type::get_type_info_static() != RuntimeAttribute::get_type_info_static()) {+ OPENVINO_THROW("Could not as Any runtime_attribute to ",+ typeid(T).name(),+ " from ",+ _impl->type_info().name(),+ "; from ",+ static_cast<std::string>(runtime_attribute->get_type_info()),+ " to ",+ static_cast<std::string>(T::element_type::get_type_info_static()));+ }+ _temp = std::make_shared<Impl<decay_t<T>>>(+ std::static_pointer_cast<typename T::element_type>(runtime_attribute));+ return _temp->as<T>();+ }+ }+ }++ template <class T,+ typename std::enable_if<std::is_arithmetic<T>::value &&+ !std::is_same<typename std::decay<T>::type, bool>::value>::type* = nullptr>+ T& as_impl(int);++ template <class T,+ typename std::enable_if<+ (util::Istreamable<T>::value || util::Readable<T>::value) && !std::is_same<T, std::string>::value &&+ (!std::is_arithmetic<T>::value || std::is_same<typename std::decay<T>::type, bool>::value)>::type* =+ nullptr>+ T& as_impl(int) {+ impl_check();++ if (is<T>()) {+ return _impl->as<T>();+ } else if (_impl->is<std::string>()) {+ _temp = std::make_shared<Impl<decay_t<T>>>();+ _impl->read_to(*_temp);+ return _temp->as<T>();+ }++ OPENVINO_THROW("Bad as from: ", _impl->type_info().name(), " to: ", typeid(T).name());+ }+friend class ::ov::RuntimeAttribute;friend class ::ov::CompiledModel;friend class ::ov::proxy::CompiledModel;@@ -704,11 +801,11 @@ public:/// @brief Default constructorAny() = default;- /// @brief Сopy constructor+ /// @brief Copy constructor/// @param other other Any objectAny(const Any& other);- /// @brief Сopy assignment operator+ /// @brief Copy assignment operator/// @param other other Any object/// @return reference to the current objectAny& operator=(const Any& other);@@ -756,8 +853,8 @@ public:* @brief Inplace value construction function** @tparam T Any type- * @tparam Args pack of paramter types passed to T constructor- * @param args pack of paramters passed to T constructor+ * @tparam Args pack of parameter types passed to T constructor+ * @param args pack of parameters passed to T constructor*/template <typename T, typename... Args>static Any make(Args&&... args) {@@ -786,130 +883,21 @@ public:*/template <class T>bool is() const {- if (_impl != nullptr) {- if (_impl->is(typeid(decay_t<T>))) {- return true;- }- for (const auto& type_index : _impl->base_type_info()) {- if (util::equal(type_index, typeid(decay_t<T>))) {- return true;- }- }- }- return false;- }-- /**- * Dynamic cast to specified type- * @tparam T type- * @return casted object- */- template <class T>- typename std::enable_if<std::is_convertible<T, std::shared_ptr<RuntimeAttribute>>::value, T>::type& as() {- if (_impl == nullptr) {- _temp = std::make_shared<Impl<decay_t<T>>>(T{});- return *static_cast<decay_t<T>*>(_temp->addressof());- } else {- if (_impl->is(typeid(decay_t<T>))) {- return *static_cast<decay_t<T>*>(_impl->addressof());- } else {- auto runtime_attribute = _impl->as_runtime_attribute();- if (runtime_attribute == nullptr) {- OPENVINO_THROW("Any does not contains pointer to runtime_attribute. It contains ",- _impl->type_info().name());- }- auto vptr = std::dynamic_pointer_cast<typename T::element_type>(runtime_attribute);- if (vptr == nullptr && T::element_type::get_type_info_static() != runtime_attribute->get_type_info() &&- T::element_type::get_type_info_static() != RuntimeAttribute::get_type_info_static()) {- OPENVINO_THROW("Could not cast Any runtime_attribute to ",- typeid(T).name(),- " from ",- _impl->type_info().name(),- "; from ",- static_cast<std::string>(runtime_attribute->get_type_info()),- " to ",- static_cast<std::string>(T::element_type::get_type_info_static()));- }- _temp = std::make_shared<Impl<decay_t<T>>>(- std::static_pointer_cast<typename T::element_type>(runtime_attribute));- return *static_cast<decay_t<T>*>(_temp->addressof());- }- }- }-- /**- * Dynamic cast to specified type- * @tparam T type- * @return casted object- */- template <class T>- typename std::enable_if<!std::is_convertible<T, std::shared_ptr<RuntimeAttribute>>::value &&- !std::is_same<T, std::string>::value && std::is_default_constructible<T>::value &&- (util::Istreamable<T>::value || util::Readable<T>::value),- T>::type&- as() {- impl_check();- if (_impl->is(typeid(decay_t<T>))) {- return *static_cast<decay_t<T>*>(_impl->addressof());- } else if (_impl->is(typeid(std::string))) {- _temp = std::make_shared<Impl<decay_t<T>>>();- _impl->read_to(*_temp);- return *static_cast<decay_t<T>*>(_temp->addressof());- }- for (const auto& type_index : _impl->base_type_info()) {- if (util::equal(type_index, typeid(decay_t<T>))) {- return *static_cast<decay_t<T>*>(_impl->addressof());- }- }- OPENVINO_THROW("Bad cast from: ", _impl->type_info().name(), " to: ", typeid(T).name());- }-- /**- * Dynamic cast to specified type- * @tparam T type- * @return casted object- */- template <class T>- typename std::enable_if<- !std::is_convertible<T, std::shared_ptr<RuntimeAttribute>>::value && !std::is_same<T, std::string>::value &&- (!std::is_default_constructible<T>::value || (!util::Istreamable<T>::value && !util::Readable<T>::value)),- T>::type&- as() {- impl_check();- if (_impl->is(typeid(decay_t<T>))) {- return *static_cast<decay_t<T>*>(_impl->addressof());- }- for (const auto& type_index : _impl->base_type_info()) {- if (util::equal(type_index, typeid(decay_t<T>))) {- return *static_cast<decay_t<T>*>(_impl->addressof());- }- }- OPENVINO_THROW("Bad cast from: ", _impl->type_info().name(), " to: ", typeid(T).name());+ return _impl && (_impl->is<T>() || _impl->is_base_type_info(typeid(decay_t<T>)));}/**- * Dynamic cast to specified type+ * Dynamic as to specified type* @tparam T type- * @return casted object+ * @return reference to caster object*/template <class T>- typename std::enable_if<std::is_same<T, std::string>::value, T>::type& as() {- if (_impl != nullptr) {- if (_impl->is(typeid(decay_t<T>))) {- return *static_cast<decay_t<T>*>(_impl->addressof());- } else {- _temp = std::make_shared<Impl<std::string>>();- _impl->read_to(*_temp);- return *static_cast<std::string*>(_temp->addressof());- }- } else {- _temp = std::make_shared<Impl<std::string>>();- return *static_cast<std::string*>(_temp->addressof());- }+ T& as() {+ return as_impl<T>(int{});}/**- * Dynamic cast to specified type+ * Dynamic as to specified type* @tparam T type* @return const reference to caster object*/@@ -983,4 +971,40 @@ inline static void PrintTo(const Any& any, std::ostream* os) {}/** @endcond */+template <>+OPENVINO_API unsigned long long Any::Base::convert<unsigned long long>() const;++template <>+OPENVINO_API long long Any::Base::convert<long long>() const;++template <>+OPENVINO_API double Any::Base::convert<double>() const;++template <class T,+ typename std::enable_if<std::is_arithmetic<T>::value &&+ !std::is_same<typename std::decay<T>::type, bool>::value>::type*>+T& Any::as_impl(int) {+ impl_check();+ if (is<T>()) {+ return _impl->as<T>();+ } else if (util::Readable<T>::value && _impl->is<std::string>()) {+ _temp = std::make_shared<Impl<decay_t<T>>>();+ _impl->read_to(*_temp);+ return _temp->as<T>();+ } else if (_impl->is_signed_integral()) {+ auto value = _impl->convert<long long>();+ _temp = std::make_shared<Impl<decay_t<T>>>(static_cast<T>(value));+ return _temp->as<T>();+ } else if (_impl->is_unsigned_integral()) {+ auto value = _impl->convert<unsigned long long>();+ _temp = std::make_shared<Impl<decay_t<T>>>(static_cast<T>(value));+ return _temp->as<T>();+ } else if (_impl->is_floating_point()) {+ auto value = _impl->convert<double>();+ _temp = std::make_shared<Impl<decay_t<T>>>(static_cast<T>(value));+ return _temp->as<T>();+ }++ OPENVINO_THROW("Bad as from: ", _impl->type_info().name(), " to: ", typeid(T).name());+}} // namespace ovdiff --git a/src/core/src/any.cpp b/src/core/src/any.cppindex 82dc01c9937..346819eced9 100644--- a/src/core/src/any.cpp+++ b/src/core/src/any.cpp@@ -6,6 +6,17 @@#include <limits>#include <string>+namespace {+template <class Container>+bool contains_type_index(Container&& types, const std::type_info& user_type) {+ for (auto&& type : types) {+ if (ov::util::equal(type, user_type)) {+ return true;+ }+ }+ return false;+}+} // namespacenamespace ov {@@ -68,6 +79,48 @@ void Any::Base::read_to(Base& other) const {}}+bool Any::Base::is_base_type_info(const std::type_info& user_type) const {+ return contains_type_index(base_type_info(), user_type);+}++bool Any::Base::is_signed_integral() const {+ return std::is_signed<char>::value ? contains_type_index(std::initializer_list<std::type_index>{typeid(char),+ typeid(signed char),+ typeid(short),+ typeid(int),+ typeid(long),+ typeid(long long)},+ type_info())+ : contains_type_index(std::initializer_list<std::type_index>{typeid(signed char),+ typeid(short),+ typeid(int),+ typeid(long),+ typeid(long long)},+ type_info());+}++bool Any::Base::is_unsigned_integral() const {+ return std::is_signed<char>::value+ ? contains_type_index(std::initializer_list<std::type_index>{typeid(unsigned char),+ typeid(unsigned short),+ typeid(unsigned int),+ typeid(unsigned long),+ typeid(unsigned long long)},+ type_info())+ : contains_type_index(std::initializer_list<std::type_index>{typeid(char),+ typeid(unsigned char),+ typeid(unsigned short),+ typeid(unsigned int),+ typeid(unsigned long),+ typeid(unsigned long long)},+ type_info());+}+bool Any::Base::is_floating_point() const {+ return contains_type_index(+ std::initializer_list<std::type_index>{typeid(float), typeid(double), typeid(long double)},+ type_info());+}+Any::~Any() {_temp = {};_impl = {};@@ -293,4 +346,42 @@ void Write<Any>::operator()(std::ostream& os, const Any& any) const {}} // namespace util++template <class U>+[[noreturn]] U Any::Base::convert_impl() const {+ OPENVINO_THROW("Bad cast from: ", type_info().name(), " to: ", typeid(U).name());+}++template <class U, class T, class... Others>+U Any::Base::convert_impl() const {+ return is<T>() ? static_cast<U>(as<T>()) : convert_impl<U, Others...>();+}++template <>+long long Any::Base::convert<long long>() const {+ return std::is_signed<char>::value ? convert_impl<long long, char, signed char, short, int, long, long long>()+ : convert_impl<long long, signed char, short, int, long, long long>();+}++template <>+unsigned long long Any::Base::convert<unsigned long long int>() const {+ return std::is_signed<char>::value ? convert_impl<unsigned long long,+ unsigned char,+ unsigned short,+ unsigned int,+ unsigned long,+ unsigned long long>()+ : convert_impl<unsigned long long,+ char,+ unsigned char,+ unsigned short,+ unsigned int,+ unsigned long,+ unsigned long long>();+}++template <>+double Any::Base::convert<double>() const {+ return convert_impl<double, float, double>();+}} // namespace ovdiff --git a/src/core/tests/any.cpp b/src/core/tests/any.cppindex 3914a617ff2..33e928d60b8 100644--- a/src/core/tests/any.cpp+++ b/src/core/tests/any.cpp@@ -11,7 +11,8 @@#include "common_test_utils/test_assertions.hpp"#include "openvino/core/runtime_attribute.hpp"-using namespace ov;+namespace ov {+namespace test {class DestructorTest {public:@@ -735,3 +736,70 @@ TEST_F(AnyTests, EmptyStringAsAny) {ASSERT_EQ(p.as<std::vector<float>>(), ref_f);ASSERT_EQ(p.as<std::vector<int>>(), ref_i);}++template <class T>+class AnyConversionTest : public AnyTests {};++TYPED_TEST_SUITE_P(AnyConversionTest);++using AnyArithmeticTypes = ::testing::Types<char,+ signed char,+ short,+ int,+ long,+ long long,+ unsigned char,+ unsigned short,+ unsigned int,+ unsigned long,+ unsigned long long,+ float,+ double>;++TYPED_TEST_P(AnyConversionTest, AnyToOtherValue) {+ const TypeParam test_value{static_cast<TypeParam>(23.15f)};+ const auto a = Any{test_value};++ EXPECT_EQ(a.as<int8_t>(), static_cast<int8_t>(test_value));+ EXPECT_EQ(a.as<int16_t>(), static_cast<int16_t>(test_value));+ EXPECT_EQ(a.as<int32_t>(), static_cast<int32_t>(test_value));+ EXPECT_EQ(a.as<int64_t>(), static_cast<int64_t>(test_value));++ EXPECT_EQ(a.as<uint8_t>(), static_cast<uint8_t>(test_value));+ EXPECT_EQ(a.as<uint16_t>(), static_cast<uint16_t>(test_value));+ EXPECT_EQ(a.as<uint32_t>(), static_cast<uint32_t>(test_value));+ EXPECT_EQ(a.as<uint64_t>(), static_cast<uint64_t>(test_value));+ EXPECT_EQ(a.as<size_t>(), static_cast<size_t>(test_value));++ EXPECT_EQ(a.as<float>(), static_cast<float>(test_value));+ EXPECT_EQ(a.as<double>(), static_cast<double>(test_value));+}++REGISTER_TYPED_TEST_SUITE_P(AnyConversionTest, AnyToOtherValue);+INSTANTIATE_TYPED_TEST_SUITE_P(InstantiationName, AnyConversionTest, AnyArithmeticTypes);++TEST_F(AnyTests, AnyAsOtherTypeIsIncosisoinet) {+ // To show member `as` current behaviour.+ // Maybe there should be two members `as` which return value+ // and `cast` returns reference if casted type is same as Any underlying type+ auto a = Any{10};++ auto& a_int = a.as<int>();+ auto& a_str = a.as<std::string>();++ EXPECT_EQ(a_int, 10);+ EXPECT_EQ(a_str, "10");++ a_int = 15;+ EXPECT_EQ(a_int, 15);+ // as string ref still has old value+ EXPECT_EQ(a_str, "10");++ a_str = "30";+ EXPECT_EQ(a_int, 15);+ // as string ref has new value but is not in sync what any contains.+ EXPECT_EQ(a_str, "30");+}++} // namespace test+} // namespace ov