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
[Core] Fix RTTI hash setup (#38771)
Tomasz Jankowski2636ccab3ecd
diff --git a/src/bindings/python/src/pyopenvino/graph/discrete_type_info.hpp b/src/bindings/python/src/pyopenvino/graph/discrete_type_info.hppindex 7170fa5e119..0c449522d18 100644--- a/src/bindings/python/src/pyopenvino/graph/discrete_type_info.hpp+++ b/src/bindings/python/src/pyopenvino/graph/discrete_type_info.hpp@@ -6,10 +6,10 @@#include <pybind11/pybind11.h>-#include "openvino/core/type.hpp"#include <string>-namespace py = pybind11;+#include "openvino/core/type.hpp"+namespace py = pybind11;// DiscreteTypeInfo doesn't own provided memory. Wrapper allows to avoid leaks.class DiscreteTypeInfoWrapper : public ov::DiscreteTypeInfo {@@ -19,13 +19,16 @@ private:public:DiscreteTypeInfoWrapper(std::string _name_str, std::string _version_id_str)- : DiscreteTypeInfo(nullptr, nullptr, nullptr),- name_str(std::move(_name_str)),- version_id_str(std::move(_version_id_str)) {+ // Since constructor calculates hash based on name and version_id, it's needed to provide them even before+ // taking ownership of these strings.+ : DiscreteTypeInfo{_name_str.c_str(), _version_id_str.c_str(), nullptr},+ name_str{std::move(_name_str)},+ version_id_str{std::move(_version_id_str)} {+ // The reassignment is needed. The C++ standard does not guarantee that c_str() keeps the same address after a+ // std::string is move-constructed.name = name_str.c_str();version_id = version_id_str.c_str();}};-void regclass_graph_DiscreteTypeInfo(py::module m);diff --git a/src/bindings/python/tests/test_graph/test_type_info.py b/src/bindings/python/tests/test_graph/test_type_info.pyindex 66d9adcff19..576fed68e1a 100644--- a/src/bindings/python/tests/test_graph/test_type_info.py+++ b/src/bindings/python/tests/test_graph/test_type_info.py@@ -52,6 +52,19 @@ def test_discrete_type_info():assert type_info.version_id == "extension"+def test_discrete_type_info_comparison():+ same_a = DiscreteTypeInfo("Custom", "extension")+ same_b = DiscreteTypeInfo("Custom", "extension")+ other_name = DiscreteTypeInfo("Other", "extension")+ other_version = DiscreteTypeInfo("Custom", "other")++ assert same_a == same_b+ assert same_a.hash() == same_b.hash()+ assert same_a != other_name+ assert same_a != other_version+ assert same_a.hash() != other_name.hash()++def test_custom_add_op_type_info():data1 = np.array([1, 2, 3])data2 = np.array([4, 5, 6])diff --git a/src/core/include/openvino/core/rtti.hpp b/src/core/include/openvino/core/rtti.hppindex d27a3b6059e..316116d97cd 100644--- a/src/core/include/openvino/core/rtti.hpp+++ b/src/core/include/openvino/core/rtti.hpp@@ -15,8 +15,7 @@#define _OPENVINO_RTTI_WITH_TYPE_VERSION(TYPE_NAME, VERSION_NAME) \_OPENVINO_HIDDEN_METHOD static const ::ov::DiscreteTypeInfo& get_type_info_static() { \- static ::ov::DiscreteTypeInfo type_info_static{TYPE_NAME, VERSION_NAME}; \- type_info_static.hash(); \+ static const ::ov::DiscreteTypeInfo type_info_static{TYPE_NAME, VERSION_NAME}; \return type_info_static; \} \const ::ov::DiscreteTypeInfo& get_type_info() const override { \@@ -26,16 +25,15 @@#define _OPENVINO_RTTI_WITH_TYPE_VERSION_PARENT(TYPE_NAME, VERSION_NAME, PARENT_CLASS) \_OPENVINO_RTTI_WITH_TYPE_VERSIONS_PARENT(TYPE_NAME, VERSION_NAME, PARENT_CLASS)-#define _OPENVINO_RTTI_WITH_TYPE_VERSIONS_PARENT(TYPE_NAME, VERSION_NAME, PARENT_CLASS) \- _OPENVINO_HIDDEN_METHOD static const ::ov::DiscreteTypeInfo& get_type_info_static() { \- static ::ov::DiscreteTypeInfo type_info_static{TYPE_NAME, \- VERSION_NAME, \- &PARENT_CLASS::get_type_info_static()}; \- type_info_static.hash(); \- return type_info_static; \- } \- const ::ov::DiscreteTypeInfo& get_type_info() const override { \- return get_type_info_static(); \+#define _OPENVINO_RTTI_WITH_TYPE_VERSIONS_PARENT(TYPE_NAME, VERSION_NAME, PARENT_CLASS) \+ _OPENVINO_HIDDEN_METHOD static const ::ov::DiscreteTypeInfo& get_type_info_static() { \+ static const ::ov::DiscreteTypeInfo type_info_static{TYPE_NAME, \+ VERSION_NAME, \+ &PARENT_CLASS::get_type_info_static()}; \+ return type_info_static; \+ } \+ const ::ov::DiscreteTypeInfo& get_type_info() const override { \+ return get_type_info_static(); \}/// Helper macro that puts necessary declarations of RTTI block inside a class definition.@@ -100,8 +98,7 @@#define _OPENVINO_RTTI_BASE_WITH_TYPE_VERSION(TYPE_NAME, VERSION_NAME) \_OPENVINO_HIDDEN_METHOD static const ::ov::DiscreteTypeInfo& get_type_info_static() { \- static ::ov::DiscreteTypeInfo type_info_static{TYPE_NAME, VERSION_NAME}; \- type_info_static.hash(); \+ static const ::ov::DiscreteTypeInfo type_info_static{TYPE_NAME, VERSION_NAME}; \return type_info_static; \} \virtual const ::ov::DiscreteTypeInfo& get_type_info() const { \diff --git a/src/core/include/openvino/core/type.hpp b/src/core/include/openvino/core/type.hppindex 1e40044dd47..45bb95ec219 100644--- a/src/core/include/openvino/core/type.hpp+++ b/src/core/include/openvino/core/type.hpp@@ -45,13 +45,13 @@ struct OPENVINO_API DiscreteTypeInfo {: name(_name),version_id(_version_id),parent(_parent),- hash_value(0) {}+ hash_value(compute_hash(_name, _version_id)) {}constexpr DiscreteTypeInfo(const char* _name, const DiscreteTypeInfo* _parent = nullptr): name(_name),version_id(nullptr),parent(_parent),- hash_value(0) {}+ hash_value(compute_hash(_name)) {}bool is_castable(const DiscreteTypeInfo& target_type) const;@@ -67,10 +67,42 @@ struct OPENVINO_API DiscreteTypeInfo {operator std::string() const;+ /**+ * @brief Returns the hash of the type info.+ * @note The hash is computed in the constructors. The fallback to compute it lazily when it is 0 (and the non-const+ * overload caching it) is kept only for binaries built with older headers, whose inlined constructors leave it 0.+ * Will be removed in 2028.0 release, leaving hash() a plain getter.+ */size_t hash() const;size_t hash();private:+ // Murmur-style 64-bit mixer, duplicated from ov::util::u64_hash_combine().+ static constexpr uint64_t u64_hash_combine(uint64_t h, uint64_t k) {+ constexpr uint64_t m = 0xc6a4a7935bd1e995;+ constexpr int r = 47;+ k *= m;+ k ^= k >> r;+ k *= m;+ h ^= k;+ h *= m;+ return h + 0xe6546b64;+ }++ static constexpr uint64_t hash_c_str(const char* str) {+ uint64_t h = 0;+ for (; *str != '\0'; ++str) {+ h = u64_hash_combine(h, static_cast<uint64_t>(static_cast<unsigned char>(*str)));+ }+ return h;+ }++ static constexpr size_t compute_hash(const char* type_name, const char* type_version_id = nullptr) {+ const auto name_hash = type_name ? hash_c_str(type_name) : 0;+ const auto version_hash = type_version_id ? hash_c_str(type_version_id) : 0;+ return static_cast<size_t>(u64_hash_combine(name_hash, version_hash));+ }+size_t hash_value;};diff --git a/src/core/src/type.cpp b/src/core/src/type.cppindex 06ddadce33d..c55f1cb764c 100644--- a/src/core/src/type.cpp+++ b/src/core/src/type.cpp@@ -4,8 +4,6 @@#include "openvino/core/type.hpp"-#include "openvino/util/hash_util.hpp"-namespace std {size_t std::hash<ov::DiscreteTypeInfo>::operator()(const ov::DiscreteTypeInfo& k) const {return k.hash();@@ -14,15 +12,13 @@ size_t std::hash<ov::DiscreteTypeInfo>::operator()(const ov::DiscreteTypeInfo& knamespace ov {+// CVS-199851: Compatibility path for hash_value == 0 (objects built by older binaries). Remove in 2028.0 release.size_t DiscreteTypeInfo::hash() const {- if (hash_value != 0)- return hash_value;- size_t name_hash = name ? std::hash<std::string>()(std::string(name)) : 0;- size_t version_id_hash = version_id ? std::hash<std::string>()(std::string(version_id)) : 0;-- return ov::util::hash_combine(std::vector<size_t>{name_hash, version_id_hash});+ return (hash_value != 0) ? hash_value : compute_hash(name, version_id);}+// CVS-199851: Lazy caching kept only for old binaries, never reached for objects built with the current header.+// Remove in 2028.0 release.size_t DiscreteTypeInfo::hash() {if (hash_value == 0)hash_value = static_cast<const DiscreteTypeInfo*>(this)->hash();diff --git a/src/core/tests/type_info.cpp b/src/core/tests/type_info.cppindex 4ec3c68c6e0..ee7e238455a 100644--- a/src/core/tests/type_info.cpp+++ b/src/core/tests/type_info.cpp@@ -4,9 +4,10 @@#include <gtest/gtest.h>+#include <functional>+#include "openvino/core/type.hpp"#include "openvino/opsets/opset.hpp"-#include "openvino/util/hash_util.hpp"TEST(type_info, compare_old_type) {ov::DiscreteTypeInfo type1("type1");@@ -50,23 +51,33 @@ TEST(type_info, compare_new_with_old_type) {ASSERT_TRUE(type1 == type1_o);}+// Expected values are fixed on purpose: recomputing them with the tested algorithm would not detect its changes.TEST(type_info, check_hash_value) {- const auto& hash_val = [](const char* name, const char* version_id) -> size_t {- size_t name_hash = name ? std::hash<std::string>()(std::string(name)) : 0;- size_t version_id_hash = version_id ? std::hash<std::string>()(std::string(version_id)) : 0;- // don't use parent for hash calculation, it is not a part of type (yet)- return ov::util::hash_combine(std::vector<size_t>{name_hash, version_id_hash});- };- ov::DiscreteTypeInfo type("type1", "version1");- ov::DiscreteTypeInfo type_old("type1");- ov::DiscreteTypeInfo type_with_version("type1", "version1");- ov::DiscreteTypeInfo type_empty_name("");- ov::DiscreteTypeInfo type_empty_ver("type", "");- EXPECT_EQ(hash_val(type.name, type.version_id), type.hash());- EXPECT_EQ(hash_val(type_old.name, type_old.version_id), type_old.hash());- EXPECT_EQ(hash_val(type_with_version.name, type_with_version.version_id), type_with_version.hash());- EXPECT_EQ(hash_val(type_empty_name.name, type_empty_name.version_id), type_empty_name.hash());- EXPECT_EQ(hash_val(type_empty_ver.name, type_empty_ver.version_id), type_empty_ver.hash());+ EXPECT_EQ(static_cast<size_t>(0x1591723d39f0c6b2ULL), ov::DiscreteTypeInfo("type1", "version1").hash());+ EXPECT_EQ(static_cast<size_t>(0xf709f69eefe342f2ULL), ov::DiscreteTypeInfo("type1").hash());+ EXPECT_EQ(static_cast<size_t>(0xe6546b64ULL), ov::DiscreteTypeInfo("").hash());+ EXPECT_EQ(static_cast<size_t>(0x16ab13a078cd7dbbULL), ov::DiscreteTypeInfo("type", "").hash());+ EXPECT_EQ(static_cast<size_t>(0x0e3866429f7ecb27ULL), ov::DiscreteTypeInfo("Add", "opset1").hash());+}++TEST(type_info, hash_properties) {+ const ov::DiscreteTypeInfo type("type1", "version1");+ const char* no_version = nullptr;++ EXPECT_EQ(type.hash(), ov::DiscreteTypeInfo("type1", "version1").hash());+ EXPECT_NE(type.hash(), ov::DiscreteTypeInfo("type2", "version1").hash());+ EXPECT_NE(type.hash(), ov::DiscreteTypeInfo("type1", "version2").hash());+ EXPECT_NE(type.hash(), ov::DiscreteTypeInfo("version1", "type1").hash());+ // parent is not a part of the type identity+ EXPECT_EQ(type.hash(), ov::DiscreteTypeInfo("type1", "version1", &type).hash());+ // missing version is the same as empty one, as in operator==+ EXPECT_EQ(ov::DiscreteTypeInfo("type1", no_version).hash(), ov::DiscreteTypeInfo("type1", "").hash());+ EXPECT_EQ(ov::DiscreteTypeInfo("type1", no_version).hash(), ov::DiscreteTypeInfo("type1").hash());+ EXPECT_EQ(type.hash(), std::hash<ov::DiscreteTypeInfo>{}(type));++ constexpr ov::DiscreteTypeInfo constant_type{"type1", "version1"};+ EXPECT_EQ(type.hash(), constant_type.hash());+ EXPECT_EQ(type, constant_type);}TEST(type_info, find_in_map) {diff --git a/src/plugins/intel_gpu/src/graph/registry/implementation_manager.hpp b/src/plugins/intel_gpu/src/graph/registry/implementation_manager.hppindex f9a0d6a958d..60150f8c46b 100644--- a/src/plugins/intel_gpu/src/graph/registry/implementation_manager.hpp+++ b/src/plugins/intel_gpu/src/graph/registry/implementation_manager.hpp@@ -31,11 +31,12 @@ struct implementation_key {#define OV_GPU_PRIMITIVE_IMPL(TYPE_NAME) \_OPENVINO_HIDDEN_METHOD static const ::ov::DiscreteTypeInfo& get_type_info_static() { \- static ::ov::DiscreteTypeInfo type_info_static{TYPE_NAME}; \- type_info_static.hash(); \+ static const ::ov::DiscreteTypeInfo type_info_static{TYPE_NAME}; \return type_info_static; \} \- const ::ov::DiscreteTypeInfo& get_type_info() const override { return get_type_info_static(); }+ const ::ov::DiscreteTypeInfo& get_type_info() const override { \+ return get_type_info_static(); \+ }using ValidateFunc = std::function<bool(const program_node& node)>;struct ImplementationManager {