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
[Snippets] Hash model rt info keys and typed leaf values (#38278)
Arseniy Obolenskiya74b9003100f
diff --git a/src/common/snippets/src/pass/hash.cpp b/src/common/snippets/src/pass/hash.cppindex 995ef23173d..5a7c8ce7554 100644--- a/src/common/snippets/src/pass/hash.cpp+++ b/src/common/snippets/src/pass/hash.cpp@@ -14,6 +14,7 @@#include <sstream>#include <string>#include <type_traits>+#include <typeinfo>#include <unordered_map>#include <unordered_set>#include <vector>@@ -303,21 +304,40 @@ std::vector<Edge> create_edge_mapping(const std::unordered_map<ov::Node*, int>&return edges;}-void hash_rt_info(uint64_t& hash, const ov::Any& data) {+template <typename T>+void hash_rt_info_leaf(uint64_t& hash, const std::string& key, const ov::Any& data) {+ hash = hash_combine(hash_combine(hash, key), data.type_info().hash_code());+ hash = hash_combine(hash, data.as<T>());+}++void hash_rt_info(uint64_t& hash, const std::string& key, const ov::Any& data) {if (data.is<std::shared_ptr<ov::Meta>>()) {const auto& meta = data.as<std::shared_ptr<ov::Meta>>();ov::AnyMap& map = *meta;+ hash = hash_combine(hash_combine(hash, key), typeid(ov::AnyMap).hash_code());+ hash = hash_combine(hash, map.size());for (const auto& it : map) {- hash_rt_info(hash, it.second);+ hash_rt_info(hash, it.first, it.second);}} else if (data.is<ov::AnyMap>()) {const auto& any_map = data.as<ov::AnyMap>();+ hash = hash_combine(hash_combine(hash, key), typeid(ov::AnyMap).hash_code());+ hash = hash_combine(hash, any_map.size());for (const auto& it : any_map) {- hash_rt_info(hash, it.second);+ hash_rt_info(hash, it.first, it.second);}+ } else if (data.is<bool>()) {+ hash_rt_info_leaf<bool>(hash, key, data);+ } else if (data.is<int64_t>()) {+ hash_rt_info_leaf<int64_t>(hash, key, data);+ } else if (data.is<uint64_t>()) {+ hash_rt_info_leaf<uint64_t>(hash, key, data);+ } else if (data.is<float>()) {+ hash_rt_info_leaf<float>(hash, key, data);+ } else if (data.is<double>()) {+ hash_rt_info_leaf<double>(hash, key, data);} else {- const auto& value = data.as<std::string>();- hash = hash_combine(hash_combine(hash, AttrType::value), value);+ hash_rt_info_leaf<std::string>(hash, key, data);}}@@ -409,7 +429,7 @@ void ovfunction_2_hash(uint64_t& hash, const ov::Model& model) {// Serialize rt infohash = hash_combine(hash, AttrType::rt_info);for (const auto& it : model.get_rt_info()) {- hash_rt_info(hash, it.second);+ hash_rt_info(hash, it.first, it.second);}}diff --git a/src/common/snippets/tests/sources.cmake b/src/common/snippets/tests/sources.cmakeindex 86194b11627..9fd5594d8b9 100644--- a/src/common/snippets/tests/sources.cmake+++ b/src/common/snippets/tests/sources.cmake@@ -28,6 +28,7 @@ set(SNIPPETS_TESTS_SRCS${CMAKE_CURRENT_SOURCE_DIR}/src/pass/gated_mlp_tokenization.cpp${CMAKE_CURRENT_SOURCE_DIR}/src/pass/gn_decomposition.cpp${CMAKE_CURRENT_SOURCE_DIR}/src/pass/gn_tokenization.cpp+ ${CMAKE_CURRENT_SOURCE_DIR}/src/pass/hash.cpp${CMAKE_CURRENT_SOURCE_DIR}/src/pass/mha_tokenization.cpp${CMAKE_CURRENT_SOURCE_DIR}/src/pass/mlp_seq_tokenization.cpp${CMAKE_CURRENT_SOURCE_DIR}/src/pass/online_softmax_decomposition.cppdiff --git a/src/common/snippets/tests/src/pass/hash.cpp b/src/common/snippets/tests/src/pass/hash.cppnew file mode 100644index 00000000000..6060da0a4d5--- /dev/null+++ b/src/common/snippets/tests/src/pass/hash.cpp@@ -0,0 +1,64 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "snippets/pass/hash.hpp"++#include <gtest/gtest.h>++#include "openvino/opsets/opset1.hpp"++namespace ov::test::snippets {+namespace {++std::shared_ptr<ov::Model> make_model(const ov::AnyMap& rt_info) {+ const auto parameter = std::make_shared<ov::opset1::Parameter>(ov::element::f32, ov::Shape{1});+ const auto result = std::make_shared<ov::opset1::Result>(parameter);+ auto model = std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{parameter});+ model->get_rt_info() = rt_info;+ return model;+}++uint64_t get_hash(const ov::AnyMap& rt_info) {+ uint64_t hash = 0;+ ov::snippets::pass::Hash{hash}.run_on_model(make_model(rt_info));+ return hash;+}++} // namespace++TEST(SnippetsHash, ModelRuntimeInfoKeysAndNestedMaps) {+ const ov::AnyMap first{{"key", "value"}};+ const ov::AnyMap same{{"key", "value"}};+ const ov::AnyMap different_key{{"other_key", "value"}};+ const ov::AnyMap nested{{"outer", ov::AnyMap{{"inner", "value"}}}};+ const ov::AnyMap same_nested{{"outer", ov::AnyMap{{"inner", "value"}}}};+ const ov::AnyMap different_outer_key{{"other_outer", ov::AnyMap{{"inner", "value"}}}};+ const ov::AnyMap different_nested_key{{"outer", ov::AnyMap{{"other_inner", "value"}}}};++ EXPECT_EQ(get_hash(first), get_hash(same));+ EXPECT_NE(get_hash(first), get_hash(different_key));+ EXPECT_EQ(get_hash(nested), get_hash(same_nested));+ EXPECT_NE(get_hash(nested), get_hash(different_outer_key));+ EXPECT_NE(get_hash(nested), get_hash(different_nested_key));+}++TEST(SnippetsHash, ModelRuntimeInfoLeafTypes) {+ const std::vector<ov::Any> values{+ std::string{"1"},+ true,+ int64_t{1},+ uint64_t{1},+ 1.F,+ 1.,+ };++ for (size_t i = 0; i < values.size(); ++i) {+ EXPECT_EQ(get_hash(ov::AnyMap{{"value", values[i]}}), get_hash(ov::AnyMap{{"value", values[i]}}));+ for (size_t j = i + 1; j < values.size(); ++j) {+ EXPECT_NE(get_hash(ov::AnyMap{{"value", values[i]}}), get_hash(ov::AnyMap{{"value", values[j]}}));+ }+ }+}++} // namespace ov::test::snippets