From 5279ceef286456474718718026174dd688e441cb Mon Sep 17 00:00:00 2001 From: Oleg Pipikin Date: Thu, 21 Mar 2024 16:37:17 +0400 Subject: [PATCH 1/3] Calculate model weights hash in parallel --- src/inference/src/compilation_context.cpp | 29 +++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/inference/src/compilation_context.cpp b/src/inference/src/compilation_context.cpp index 30720353a014c3..b12bed51c22b59 100644 --- a/src/inference/src/compilation_context.cpp +++ b/src/inference/src/compilation_context.cpp @@ -12,6 +12,7 @@ #endif #include "itt.hpp" +#include "openvino/core/parallel.hpp" #include "openvino/pass/manager.hpp" #include "openvino/util/file_util.hpp" #include "openvino/util/xml_parse_utils.hpp" @@ -121,8 +122,32 @@ std::string ModelCache::compute_hash(const std::string& modelStr, auto ptr = static_cast(tensor.data()); size_t size = tensor.get_size() / sizeof(size_t); - for (size_t i = 0; i < size; i++) - seed = hash_combine(seed, ptr[i]); + + size_t block_size = 100000000; + size_t blocks_num = size / block_size; + std::vector block_hashes(blocks_num + 1); + + ov::parallel_for(blocks_num, [&](size_t block_idx) { + auto& block_hash = block_hashes[block_idx]; + auto local_ptr = ptr + block_size * block_idx; + for (size_t i = 0; i < block_size; i++) { + block_hash = hash_combine(block_hash, local_ptr[i]); + } + }); + + { + auto& block_hash = block_hashes[blocks_num]; + auto local_ptr = ptr + block_size * blocks_num; + auto elements_left = size - block_size * blocks_num; + for (size_t i = 0; i < elements_left; i++) { + block_hash = hash_combine(block_hash, local_ptr[i]); + } + } + + for (auto hash : block_hashes) { + seed = hash_combine(seed, hash); + } + auto size_done = size * sizeof(size_t); auto ptr_left = static_cast(tensor.data()) + size_done; size_t size_left = tensor.get_size() - size_done; From 192c8a090bf023b1512a106ced88b68c3b7ac834 Mon Sep 17 00:00:00 2001 From: Oleg Pipikin Date: Thu, 21 Mar 2024 23:52:49 +0400 Subject: [PATCH 2/3] Apply comments --- src/inference/src/compilation_context.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/inference/src/compilation_context.cpp b/src/inference/src/compilation_context.cpp index b12bed51c22b59..f14d4cd351bdd5 100644 --- a/src/inference/src/compilation_context.cpp +++ b/src/inference/src/compilation_context.cpp @@ -123,25 +123,28 @@ std::string ModelCache::compute_hash(const std::string& modelStr, auto ptr = static_cast(tensor.data()); size_t size = tensor.get_size() / sizeof(size_t); - size_t block_size = 100000000; + // 1MB block size in size_t + const size_t block_size = 1000000 / sizeof(size_t); size_t blocks_num = size / block_size; - std::vector block_hashes(blocks_num + 1); + std::vector block_hashes(blocks_num + 1, 0); ov::parallel_for(blocks_num, [&](size_t block_idx) { - auto& block_hash = block_hashes[block_idx]; + uint64_t local_hash = 0; auto local_ptr = ptr + block_size * block_idx; for (size_t i = 0; i < block_size; i++) { - block_hash = hash_combine(block_hash, local_ptr[i]); + local_hash = hash_combine(local_hash, local_ptr[i]); } + block_hashes[block_idx] = local_hash; }); { - auto& block_hash = block_hashes[blocks_num]; + uint64_t local_hash = 0; auto local_ptr = ptr + block_size * blocks_num; auto elements_left = size - block_size * blocks_num; for (size_t i = 0; i < elements_left; i++) { - block_hash = hash_combine(block_hash, local_ptr[i]); + local_hash = hash_combine(local_hash, local_ptr[i]); } + block_hashes[blocks_num] = local_hash; } for (auto hash : block_hashes) { From caea4fee0b91d1c921bbd10c3f37fb4cb0e7b7b5 Mon Sep 17 00:00:00 2001 From: Oleg Pipikin Date: Fri, 22 Mar 2024 13:13:51 +0400 Subject: [PATCH 3/3] Apply comments --- src/inference/src/compilation_context.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/inference/src/compilation_context.cpp b/src/inference/src/compilation_context.cpp index f14d4cd351bdd5..f13dbc6edea583 100644 --- a/src/inference/src/compilation_context.cpp +++ b/src/inference/src/compilation_context.cpp @@ -123,8 +123,8 @@ std::string ModelCache::compute_hash(const std::string& modelStr, auto ptr = static_cast(tensor.data()); size_t size = tensor.get_size() / sizeof(size_t); - // 1MB block size in size_t - const size_t block_size = 1000000 / sizeof(size_t); + // 10MB block size in size_t + const size_t block_size = 10000000 / sizeof(size_t); size_t blocks_num = size / block_size; std::vector block_hashes(blocks_num + 1, 0);