Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[core] Core implementation to add weight sharing context when compile/import model (#34581)

Pawel Raasz1653ec77c3be
diff --git a/src/inference/dev_api/openvino/runtime/icache_manager.hpp b/src/inference/dev_api/openvino/runtime/icache_manager.hpp
index f3ddd433e42..374ceb21ae0 100644
--- a/src/inference/dev_api/openvino/runtime/icache_manager.hpp
+++ b/src/inference/dev_api/openvino/runtime/icache_manager.hpp
@@ -87,7 +87,7 @@ public:
* @brief Gets context from the storage
* @return The stored context
*/
- virtual ov::weight_sharing::Context get_context() const = 0;
+ virtual std::shared_ptr<ov::wsh::Context> get_context() const = 0;
};
/**
diff --git a/src/inference/dev_api/openvino/runtime/single_file_storage.hpp b/src/inference/dev_api/openvino/runtime/single_file_storage.hpp
index 70f0be86a74..f9c01f56838 100644
--- a/src/inference/dev_api/openvino/runtime/single_file_storage.hpp
+++ b/src/inference/dev_api/openvino/runtime/single_file_storage.hpp
@@ -57,7 +57,7 @@ public:
* @brief Get the weight sharing context from the storage.
* @return The weight sharing context stored in the storage.
*/
- weight_sharing::Context get_context() const override;
+ std::shared_ptr<wsh::Context> get_context() const override;
using BlobIdType = uint64_t;
using DataIdType = uint64_t;
@@ -72,7 +72,7 @@ private:
std::string model_name;
};
std::unordered_map<BlobIdType, BlobInfo> m_blob_index;
- weight_sharing::Context m_shared_context;
+ std::shared_ptr<wsh::Context> m_shared_context;
bool build_content_index(std::ifstream& stream);
static BlobIdType convert_blob_id(const std::string& blob_id);
diff --git a/src/inference/src/cache_manager.hpp b/src/inference/src/cache_manager.hpp
index 984701815b7..199952486df 100644
--- a/src/inference/src/cache_manager.hpp
+++ b/src/inference/src/cache_manager.hpp
@@ -42,13 +42,9 @@ public:
* @brief Constructor
*
*/
- FileStorageCacheManager(std::filesystem::path cache_path) : m_cache_path(std::move(cache_path)) {}
-
- /**
- * @brief Destructor
- *
- */
- ~FileStorageCacheManager() override = default;
+ FileStorageCacheManager(std::filesystem::path cache_path) : m_cache_path(std::move(cache_path)) {
+ util::create_directory_recursive(m_cache_path);
+ }
private:
void write_cache_entry(const std::string& id, StreamWriter writer) override {
@@ -86,5 +82,4 @@ private:
}
}
};
-
} // namespace ov
diff --git a/src/inference/src/dev/core_impl.cpp b/src/inference/src/dev/core_impl.cpp
index 6f159fe918b..2c82ec3898f 100644
--- a/src/inference/src/dev/core_impl.cpp
+++ b/src/inference/src/dev/core_impl.cpp
@@ -28,6 +28,7 @@
#include "openvino/runtime/make_tensor.hpp"
#include "openvino/runtime/remote_context.hpp"
#include "openvino/runtime/shared_buffer.hpp"
+#include "openvino/runtime/single_file_storage.hpp"
#include "openvino/runtime/threading/executor_manager.hpp"
#include "openvino/util/common_util.hpp"
#include "openvino/util/file_util.hpp"
@@ -863,9 +864,9 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model(const std::shared_ptr<
is_proxy_device(patched_device_name));
auto plugin = get_plugin(parsed.m_device_name);
const auto& [cache_dir, cache_manager] = parsed.m_core_config.get_cache_config_for_device(plugin);
- auto res = import_compiled_model(plugin, {}, config, model);
+ auto compiled_model = import_compiled_model(plugin, {}, config, model);
// Skip caching for proxy plugin. HW plugin will load network from the cache
- if (res) {
+ if (compiled_model) {
// hint::compiled_blob is set and imported skip compilation
} else if (cache_manager && device_supports_model_caching(plugin, parsed.m_config) && !is_proxy_device(plugin)) {
emplace_cache_dir_if_supported(parsed.m_config, plugin, cache_dir);
@@ -886,13 +887,13 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model(const std::shared_ptr<
}
const auto lock = m_cache_guard.get_hash_lock(cache_content.m_blob_id);
- res = load_model_from_cache(cache_content, plugin, parsed.m_config, {}, [&]() {
+ compiled_model = load_model_from_cache(cache_content, plugin, parsed.m_config, {}, [&]() {
return compile_model_and_cache(plugin, model, parsed.m_config, {}, cache_content);
});
} else {
- res = plugin.compile_model(model, parsed.m_config);
+ compiled_model = plugin.compile_model(model, parsed.m_config);
}
- return res;
+ return compiled_model;
}
ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model(const std::shared_ptr<const ov::Model>& model_,
@@ -910,9 +911,9 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model(const std::shared_ptr<
parse_device_name_into_config(device_name, m_core_config, config_with_batch, is_proxy_device(device_name));
auto plugin = get_plugin(parsed.m_device_name);
const auto& [cache_dir, cache_manager] = parsed.m_core_config.get_cache_config_for_device(plugin);
- auto res = import_compiled_model(plugin, context, parsed.m_config, model);
+ auto compiled_model = import_compiled_model(plugin, context, parsed.m_config, model);
// Skip caching for proxy plugin. HW plugin will load network from the cache
- if (res) {
+ if (compiled_model) {
// hint::compiled_blob is set and imported skip compilation
} else if (cache_manager && device_supports_model_caching(plugin, parsed.m_config) && !is_proxy_device(plugin)) {
emplace_cache_dir_if_supported(parsed.m_config, plugin, cache_dir);
@@ -922,13 +923,15 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model(const std::shared_ptr<
return ModelCache::compute_hash(model, cache_content.m_model_path, compiled_config);
});
cache_content.model = model;
- res = load_model_from_cache(cache_content, plugin, parsed.m_config, context, [&]() {
+
+ const auto lock = m_cache_guard.get_hash_lock(cache_content.m_blob_id);
+ compiled_model = load_model_from_cache(cache_content, plugin, parsed.m_config, context, [&]() {
return compile_model_and_cache(plugin, model, parsed.m_config, context, cache_content);
});
} else {
- res = plugin.compile_model(model, context, parsed.m_config);
+ compiled_model = plugin.compile_model(model, context, parsed.m_config);
}
- return res;
+ return compiled_model;
}
ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model(const std::filesystem::path& model_path,
@@ -1487,13 +1490,23 @@ bool ov::CoreImpl::device_supports_internal_property(const ov::Plugin& plugin, c
ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model_and_cache(ov::Plugin& plugin,
const std::shared_ptr<const ov::Model>& model,
- const ov::AnyMap& parsedConfig,
+ const ov::AnyMap& parsed_config,
const ov::SoPtr<ov::IRemoteContext>& context,
- const CacheContent& cacheContent) const {
+ const CacheContent& cache_content) const {
OV_ITT_SCOPED_TASK(ov::itt::domains::OV, "CoreImpl::compile_model_and_cache");
- ov::SoPtr<ov::ICompiledModel> compiled_model =
- context ? plugin.compile_model(model, context, parsedConfig) : plugin.compile_model(model, parsedConfig);
- if (cacheContent.m_cache_manager && device_supports_model_caching(plugin)) {
+
+ const auto get_cfg_with_shared_ctx = [](auto&& parsed_config, auto&& cache_content) {
+ auto config_with_shared_ctx = parsed_config;
+ config_with_shared_ctx.emplace(ov::internal::model_sharing_context(cache_content.m_shared_ctx->get_context()));
+ return config_with_shared_ctx;
+ };
+ const auto& cfg =
+ cache_content.m_shared_ctx && device_supports_internal_property(plugin, ov::internal::model_sharing_context)
+ ? get_cfg_with_shared_ctx(parsed_config, cache_content)
+ : parsed_config;
+
+ auto compiled_model = context ? plugin.compile_model(model, context, cfg) : plugin.compile_model(model, cfg);
+ if (cache_content.m_cache_manager && device_supports_model_caching(plugin)) {
try {
// need to export network for further import from "cache"
OV_ITT_SCOPE(FIRST_INFERENCE, ov::itt::domains::LoadTime, "Core::compile_model::Export");
@@ -1502,21 +1515,33 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model_and_cache(ov::Plugin&
compiled_model_runtime_properties =
plugin.get_property(ov::internal::compiled_model_runtime_properties.name(), {}).as<std::string>();
}
- cacheContent.m_cache_manager->write_cache_entry(cacheContent.m_blob_id, [&](std::ostream& networkStream) {
+ try {
+ if (cache_content.m_shared_ctx) {
+ auto ctx = compiled_model->get_property(ov::internal::model_sharing_context.name())
+ .as<ov::internal::WeightSharingCtxPtr>();
+ if (ctx) {
+ cache_content.m_shared_ctx->write_context(*ctx);
+ }
+ }
+ } catch (const ov::Exception&) {
+ // do nothing if compile model will not return it
+ }
+ // write compiled blob
+ cache_content.m_cache_manager->write_cache_entry(cache_content.m_blob_id, [&](std::ostream& stream) {
uint32_t header_size_alignment{};
if (device_supports_internal_property(plugin, ov::internal::cache_header_alignment.name())) {
header_size_alignment =
plugin.get_property(ov::internal::cache_header_alignment.name(), {}).as<uint32_t>();
}
- networkStream << ov::CompiledBlobHeader(ov::get_openvino_version().buildNumber,
- ov::ModelCache::calculate_file_info(cacheContent.m_model_path),
- compiled_model_runtime_properties,
- header_size_alignment);
- compiled_model->export_model(networkStream);
+ stream << ov::CompiledBlobHeader(ov::get_openvino_version().buildNumber,
+ ov::ModelCache::calculate_file_info(cache_content.m_model_path),
+ compiled_model_runtime_properties,
+ header_size_alignment);
+ compiled_model->export_model(stream);
});
} catch (...) {
- cacheContent.m_cache_manager->remove_cache_entry(cacheContent.m_blob_id);
+ cache_content.m_cache_manager->remove_cache_entry(cache_content.m_blob_id);
throw;
}
}
@@ -1524,7 +1549,7 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model_and_cache(ov::Plugin&
}
ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::load_model_from_cache(
- const CacheContent& cacheContent,
+ const CacheContent& cache_content,
ov::Plugin& plugin,
const ov::AnyMap& config,
const ov::SoPtr<ov::IRemoteContext>& context,
@@ -1532,13 +1557,12 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::load_model_from_cache(
ov::SoPtr<ov::ICompiledModel> compiled_model;
struct HeaderException {};
- OPENVINO_ASSERT(cacheContent.m_cache_manager != nullptr);
+ OPENVINO_ASSERT(cache_content.m_cache_manager != nullptr);
try {
- cacheContent.m_cache_manager->read_cache_entry(
- cacheContent.m_blob_id,
- cacheContent.m_mmap_enabled &&
- device_supports_internal_property(plugin, ov::internal::caching_with_mmap.name()),
+ cache_content.m_cache_manager->read_cache_entry(
+ cache_content.m_blob_id,
+ cache_content.m_mmap_enabled && device_supports_internal_property(plugin, ov::internal::caching_with_mmap),
[&](ICacheManager::CompiledBlobVariant& compiled_blob) {
OV_ITT_SCOPE(FIRST_INFERENCE,
ov::itt::domains::LoadTime,
@@ -1557,13 +1581,12 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::load_model_from_cache(
}};
std::visit(header_reader, compiled_blob);
- if (header.get_file_info() != ov::ModelCache::calculate_file_info(cacheContent.m_model_path)) {
+ if (header.get_file_info() != ov::ModelCache::calculate_file_info(cache_content.m_model_path)) {
// Original file is changed, don't use cache
OPENVINO_THROW("Original model file is changed");
}
- if (device_supports_internal_property(
- plugin,
- ov::internal::compiled_model_runtime_properties_supported.name())) {
+ if (device_supports_internal_property(plugin,
+ ov::internal::compiled_model_runtime_properties_supported)) {
ov::AnyMap compiled_model_runtime_properties = {
{ov::internal::compiled_model_runtime_properties.name(),
std::string(header.get_runtime_info())}};
@@ -1588,8 +1611,8 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::load_model_from_cache(
ov::AnyMap update_config = config;
update_config[ov::loaded_from_cache.name()] = true;
- if (cacheContent.model && plugin.is_property_supported(ov::hint::model.name())) {
- update_config[ov::hint::model.name()] = cacheContent.model;
+ if (cache_content.model && plugin.is_property_supported(ov::hint::model.name())) {
+ update_config[ov::hint::model.name()] = cache_content.model;
}
if (plugin.is_property_supported(ov::weights_path.name())) {
@@ -1599,7 +1622,7 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::load_model_from_cache(
path_hint != update_config.end()) {
weights_path = path_hint->second.as<std::string>();
} else if (weights_path = extract_weight_path(header.get_runtime_info()); weights_path.empty()) {
- weights_path = cacheContent.m_model_path;
+ weights_path = cache_content.m_model_path;
}
weights_path.replace_extension(".bin");
@@ -1607,6 +1630,12 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::load_model_from_cache(
update_config[ov::weights_path.name()] = util::path_to_string(weights_path);
}
}
+ if (device_supports_internal_property(plugin, ov::internal::model_sharing_context)) {
+ if (cache_content.m_shared_ctx) {
+ update_config.emplace(
+ ov::internal::model_sharing_context(cache_content.m_shared_ctx->get_context()));
+ }
+ }
ov::util::VariantVisitor model_importer{
[&](const ov::Tensor& compiled_blob) -> ov::SoPtr<ov::ICompiledModel> {
@@ -1624,9 +1653,9 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::load_model_from_cache(
});
} catch (const HeaderException&) {
// For these exceptions just remove old cache and set that import didn't work
- cacheContent.m_cache_manager->remove_cache_entry(cacheContent.m_blob_id);
+ cache_content.m_cache_manager->remove_cache_entry(cache_content.m_blob_id);
} catch (...) {
- cacheContent.m_cache_manager->remove_cache_entry(cacheContent.m_blob_id);
+ cache_content.m_cache_manager->remove_cache_entry(cache_content.m_blob_id);
// TODO: temporary disabled by #54335. In future don't throw only for new 'blob_outdated' exception
// throw;
}
@@ -1735,9 +1764,8 @@ ov::CoreConfig::CacheConfig ov::CoreConfig::get_cache_config_for_device(const ov
ov::CoreConfig::CacheConfig ov::CoreConfig::CacheConfig::create(const std::filesystem::path& dir) {
auto cfg = CacheConfig{dir, nullptr};
if (dir.extension() == ".bin") {
- OPENVINO_NOT_IMPLEMENTED;
+ cfg.m_cache_manager = std::make_shared<runtime::SingleFileStorage>(dir);
} else if (!dir.empty()) {
- ov::util::create_directory_recursive(dir);
cfg.m_cache_manager = std::make_shared<FileStorageCacheManager>(dir);
}
return cfg;
diff --git a/src/inference/src/dev/core_impl.hpp b/src/inference/src/dev/core_impl.hpp
index 8aa644cd045..49c8bed1f78 100644
--- a/src/inference/src/dev/core_impl.hpp
+++ b/src/inference/src/dev/core_impl.hpp
@@ -12,6 +12,7 @@
#include "openvino/core/so_extension.hpp"
#include "openvino/core/version.hpp"
#include "openvino/runtime/common.hpp"
+#include "openvino/runtime/icache_manager.hpp"
#include "openvino/runtime/icompiled_model.hpp"
#include "openvino/runtime/threading/executor_manager.hpp"
@@ -150,9 +151,11 @@ private:
bool mmap_enabled = false,
const std::filesystem::path model_path = {})
: m_cache_manager(cache_manager),
+ m_shared_ctx(dynamic_cast<ov::IContextStore*>(cache_manager.get())),
m_model_path(model_path),
m_mmap_enabled{mmap_enabled} {}
std::shared_ptr<ov::ICacheManager> m_cache_manager{};
+ ov::IContextStore* m_shared_ctx{};
std::string m_blob_id{};
std::filesystem::path m_model_path{};
std::shared_ptr<const ov::Model> model{};
diff --git a/src/inference/src/single_file_storage.cpp b/src/inference/src/single_file_storage.cpp
index 3ce99aa8c7d..58961aebae3 100644
--- a/src/inference/src/single_file_storage.cpp
+++ b/src/inference/src/single_file_storage.cpp
@@ -83,7 +83,10 @@ void write_padding(std::ostream& stream, uint64_t alignment) {
}
} // namespace
-SingleFileStorage::SingleFileStorage(const std::filesystem::path& path) : m_file_path{path} {
+SingleFileStorage::SingleFileStorage(const std::filesystem::path& path)
+ : m_file_path{path},
+ m_blob_index{},
+ m_shared_context{std::make_shared<wsh::Context>()} {
util::create_directory_recursive(m_file_path.parent_path());
if (!util::file_exists(m_file_path)) {
std::ofstream stream(m_file_path, std::ios::binary);
@@ -96,8 +99,8 @@ SingleFileStorage::SingleFileStorage(const std::filesystem::path& path) : m_file
if (!build_content_index(stream)) {
m_blob_index.clear();
- m_shared_context.m_cache_sources.clear();
- m_shared_context.m_weight_registry.clear();
+ m_shared_context->m_cache_sources.clear();
+ m_shared_context->m_weight_registry.clear();
}
}
}
@@ -173,9 +176,9 @@ bool SingleFileStorage::build_content_index(std::ifstream& stream) {
if (!s.good()) {
return false;
}
- m_shared_context.m_weight_registry[source_id][const_id] = {static_cast<size_t>(const_offset),
- static_cast<size_t>(const_size),
- element::Type_t{const_type}};
+ m_shared_context->m_weight_registry[source_id][const_id] = {static_cast<size_t>(const_offset),
+ static_cast<size_t>(const_size),
+ element::Type_t{const_type}};
}
return s.good();
};
@@ -200,7 +203,7 @@ bool SingleFileStorage::build_content_index(std::ifstream& stream) {
return false;
}
const auto weight_size = size - sizeof(device_id) - sizeof(source_id) - sizeof(padding_size) - padding_size;
- m_shared_context.m_cache_sources[source_id] = {};
+ m_shared_context->m_cache_sources[source_id] = {};
if (s.tellg() + static_cast<std::streamoff>(weight_size) > end_pos) {
return false;
}
@@ -282,7 +285,7 @@ void SingleFileStorage::read_cache_entry(const std::string& blob_id, bool enable
void SingleFileStorage::remove_cache_entry(const std::string& id) {}
-weight_sharing::Context SingleFileStorage::get_context() const {
+std::shared_ptr<wsh::Context> SingleFileStorage::get_context() const {
return m_shared_context;
}
@@ -293,8 +296,8 @@ void SingleFileStorage::write_context(const weight_sharing::Context& context) {
weight_sharing::WeightRegistry delta_weight_registry;
for (const auto& [source_id, const_meta_map] : context.m_weight_registry) {
for (const auto& [const_id, const_meta] : const_meta_map) {
- if (m_shared_context.m_weight_registry.count(source_id) == 0 ||
- m_shared_context.m_weight_registry[source_id].count(const_id) == 0) {
+ if (m_shared_context->m_weight_registry.count(source_id) == 0 ||
+ m_shared_context->m_weight_registry[source_id].count(const_id) == 0) {
delta_weight_registry[source_id][const_id] = const_meta;
}
}
@@ -313,7 +316,7 @@ void SingleFileStorage::write_context(const weight_sharing::Context& context) {
s.write(reinterpret_cast<const char*>(&const_size), sizeof(const_size));
s.write(reinterpret_cast<const char*>(&const_type), sizeof(const_type));
- m_shared_context.m_weight_registry[source_id][const_id] = props;
+ m_shared_context->m_weight_registry[source_id][const_id] = props;
}
};
write_tlv_record(stream, static_cast<TLVTraits::TagType>(Tag::ConstantMeta), const_meta_writer);
@@ -321,7 +324,7 @@ void SingleFileStorage::write_context(const weight_sharing::Context& context) {
weight_sharing::WeightSourceRegistry delta_cache_sources;
for (const auto& [source_id, weight_buffer] : context.m_cache_sources) {
- if (m_shared_context.m_cache_sources.count(source_id) == 0) {
+ if (m_shared_context->m_cache_sources.count(source_id) == 0) {
delta_cache_sources[source_id] = weight_buffer;
}
}
@@ -336,7 +339,7 @@ void SingleFileStorage::write_context(const weight_sharing::Context& context) {
s.write(reinterpret_cast<const char*>(buf->get_ptr()), buf->size());
}
- m_shared_context.m_cache_sources[source_id] = weight_buffer;
+ m_shared_context->m_cache_sources[source_id] = weight_buffer;
};
write_tlv_record(stream, static_cast<TLVTraits::TagType>(Tag::WeightSource), weight_source_writer);
}
diff --git a/src/inference/tests/functional/caching_test.cpp b/src/inference/tests/functional/caching_test.cpp
index 137c98d1c6b..5848945b8ca 100644
--- a/src/inference/tests/functional/caching_test.cpp
+++ b/src/inference/tests/functional/caching_test.cpp
@@ -26,6 +26,7 @@
#ifdef PROXY_PLUGIN_ENABLED
# include "openvino/proxy/properties.hpp"
#endif
+#include "openvino/core/weight_sharing_util.hpp"
#include "openvino/runtime/common.hpp"
#include "openvino/runtime/compiled_model.hpp"
#include "openvino/runtime/core.hpp"
@@ -39,6 +40,12 @@
#include "unit_test_utils/mocks/openvino/runtime/mock_icompiled_model.hpp"
#include "unit_test_utils/mocks/openvino/runtime/mock_iplugin.hpp"
+namespace ov::weight_sharing {
+inline bool operator==(const ov::wsh::WeightMetaData& lhs, const ov::wsh::WeightMetaData& rhs) {
+ return std::tie(lhs.m_offset, lhs.m_size) == std::tie(rhs.m_offset, rhs.m_size);
+}
+} // namespace ov::weight_sharing
+
using namespace ::testing;
using namespace std::placeholders;
using namespace std::chrono;
@@ -225,6 +232,9 @@ public:
.WillRepeatedly(Return(std::vector<ov::PropertyName>{ov::supported_properties.name(),
ov::optimal_number_of_infer_requests.name(),
ov::model_name.name()}));
+ EXPECT_CALL(*mock, get_property(ov::internal::model_sharing_context.name()))
+ .Times(AnyNumber())
+ .WillRepeatedly(Return(ov::Any{std::make_shared<const ov::wsh::Context>()}));
ON_CALL(*mock, export_model(_)).WillByDefault(Invoke([model](std::ostream& s) {
s << model->get_friendly_name();
s << ' ';
@@ -255,6 +265,7 @@ public:
m_model.reset();
mockPlugin.reset();
ov::test::utils::removeIRFiles(modelName, weightsName);
+ std::filesystem::remove_all(ov::util::make_path(m_cacheDir));
}
void testLoad(const std::function<void(ov::Core& core)>& func) {
@@ -3151,62 +3162,267 @@ TEST_P(CachingTest, import_from_cache_model_by_custom_model_rt_info) {
});
}
-TEST_P(CachingTest, compile_and_import_from_cache_model_by_custom_id) {
- ON_CALL(*mockPlugin, import_model(A<const ov::Tensor&>(), _))
- .WillByDefault(Invoke([&](const ov::Tensor& itensor, const ov::AnyMap& config) {
- if (m_checkConfigCb) {
- m_checkConfigCb(config);
- }
- EXPECT_EQ(config.count(ov::hint::compiled_blob.name()), 0);
- EXPECT_EQ(config.count(ov::hint::model.name()), 0);
- EXPECT_EQ(config.count(ov::weights_path.name()), 0);
+MATCHER_P(IsAnyShCtx, other, "") {
+ if (arg.first == ov::internal::model_sharing_context.name()) {
+ const auto& map = arg.second.template as<ov::internal::WeightSharingCtxPtr>();
+ if (map) {
+ return map->m_weight_registry == other.m_weight_registry;
+ } else if (!map && other.m_weight_registry.empty()) {
+ return true;
+ }
+ }
+ return false;
+}
- size_t pos = 0;
- auto name = getline_from_buffer(itensor.data<const char>(), itensor.get_byte_size(), pos);
- std::lock_guard<std::mutex> lock(mock_creation_mutex);
- return create_mock_compiled_model(m_models[name], mockPlugin);
- }));
- EXPECT_CALL(*mockPlugin, get_property(_, _)).Times(AnyNumber());
- EXPECT_CALL(*mockPlugin, query_model(_, _)).Times(AnyNumber());
+TEST_P(CachingTest, test_share_weight_create_ctx_single_core) {
+ ov::Core core;
+ m_cacheDir += ".bin";
+ core.set_property(ov::cache_dir(m_cacheDir));
+ injectPlugin(mockPlugin.get());
+ core.register_plugin(ov::util::make_plugin_library_name(ov::test::utils::getExecutableDirectory(),
+ std::string("mock_engine") + OV_BUILD_POSTFIX),
+ deviceName);
+ const auto empty_ctx = ov::wsh::Context();
+ const auto ctx1 = [] {
+ auto constants_meta = ov::wsh::WeightMetaMap{{0, ov::wsh::WeightMetaData{0, 100, ov::element::u8}}};
+ auto tmp = ov::wsh::WeightRegistry{{1, std::move(constants_meta)}};
+ return ov::wsh::Context{tmp, {}};
+ }();
+
+ EXPECT_CALL(*mockPlugin, get_property(ov::supported_properties.name(), _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::device::capability::EXPORT_IMPORT, _)).Times(AnyNumber());
EXPECT_CALL(*mockPlugin, get_property(ov::device::architecture.name(), _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::internal::supported_properties.name(), _))
+ .Times(AnyNumber())
+ .WillRepeatedly(Return(std::vector<ov::PropertyName>{ov::internal::caching_properties.name(),
+ ov::internal::model_sharing_context.name()}));
EXPECT_CALL(*mockPlugin, get_property(ov::internal::caching_properties.name(), _)).Times(AnyNumber());
- if (m_remoteContext) {
- return; // skip the remote Context test for Multi plugin
+ EXPECT_CALL(*mockPlugin, get_property(ov::device::capabilities.name(), _)).Times(AnyNumber());
+ {
+ // 1st compile
+ EXPECT_CALL(*mockPlugin, compile_model(A<const std::shared_ptr<const ov::Model>&>(), _, _))
+ .Times(m_remoteContext ? 1 : 0);
+ EXPECT_CALL(*mockPlugin,
+ compile_model(A<const std::shared_ptr<const ov::Model>&>(), Contains(IsAnyShCtx(empty_ctx))))
+ .Times(!m_remoteContext ? 1 : 0);
+ EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<const ov::Tensor&>(), _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<const ov::Tensor&>(), _)).Times(0);
+ m_post_mock_net_callbacks.emplace_back([&](MockICompiledModelImpl& net) {
+ EXPECT_CALL(net, export_model(_)).Times(1);
+ EXPECT_CALL(net, get_property(ov::internal::model_sharing_context.name()))
+ .WillOnce(Return(ov::Any(std::make_shared<const ov::wsh::Context>(ctx1))));
+ });
+ m_testFunction(core);
}
- m_post_mock_net_callbacks.emplace_back([&](MockICompiledModelImpl& net) {
- EXPECT_CALL(net, export_model(_)).Times(1);
- });
- MkDirGuard guard(m_cacheDir);
- EXPECT_CALL(*mockPlugin, compile_model(_, _, _)).Times(0);
- EXPECT_CALL(
- *mockPlugin,
- compile_model(A<const std::shared_ptr<const ov::Model>&>(), Not(Contains(Key(ov::cache_blob_id.name())))))
- .Times(1);
- EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), _, _)).Times(1);
- EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), _)).Times(2);
- EXPECT_CALL(*mockPlugin, import_model(A<const ov::Tensor&>(), _, _)).Times(0);
- EXPECT_CALL(*mockPlugin, import_model(A<const ov::Tensor&>(), _)).Times(0);
- testLoad([&](ov::Core& core) {
- const auto config = ov::AnyMap{{ov::cache_dir(m_cacheDir)}, ov::cache_blob_id(100)};
+ EXPECT_EQ(comp_models.size(), 1);
+ m_post_mock_net_callbacks.pop_back();
+ {
+ // 2nd compile try (use import)
+ EXPECT_CALL(*mockPlugin, compile_model(A<const std::shared_ptr<const ov::Model>&>(), _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, compile_model(A<const std::shared_ptr<const ov::Model>&>(), _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), _, Contains(IsAnyShCtx(ctx1))))
+ .Times(m_remoteContext ? 1 : 0);
+ EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), Contains(IsAnyShCtx(ctx1))))
+ .Times(!m_remoteContext ? 1 : 0);
- // read and load model with custom ID
- performReadAndLoad(core, config);
+ EXPECT_CALL(*mockPlugin, import_model(A<const ov::Tensor&>(), _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<const ov::Tensor&>(), _)).Times(0);
- // import model with custom ID
- performReadAndLoadWithContext(core, config);
+ EXPECT_CALL(*comp_models.back(), get_property(ov::internal::model_sharing_context.name()))
+ .WillRepeatedly(Return(std::make_shared<const ov::wsh::Context>(empty_ctx)));
- // import model with custom ID
- performReadAndLoad(core, config);
+ m_testFunction(core);
+ EXPECT_EQ(comp_models.size(), 1);
+ }
+ core.unload_plugin(deviceName);
+}
- // import model with custom ID and empty model
- auto empty_model = std::make_shared<ov::Model>(ov::ResultVector{}, ov::ParameterVector{}, "empty");
- auto compiled_model = core.compile_model(empty_model, deviceToLoad, config);
- EXPECT_TRUE(compiled_model);
- });
+TEST_P(CachingTest, test_share_weight_create_ctx) {
+ m_cacheDir += ".bin";
+
+ const auto empty_ctx = ov::wsh::Context();
+ const auto ctx1 = [] {
+ auto constants_meta = ov::wsh::WeightMetaMap{{0, ov::wsh::WeightMetaData{0, 200, ov::element::u8}}};
+ // auto c = std::make_shared<ov::wsh::Context>();
+ // c->m_constants_meta_data = {{1, std::move(constants_meta)}};
+ // return c;
+ return ov::wsh::Context{ov::wsh::WeightRegistry{{1, std::move(constants_meta)}}, {}};
+ }();
+
+ EXPECT_CALL(*mockPlugin, get_property(ov::supported_properties.name(), _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::device::capability::EXPORT_IMPORT, _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::device::architecture.name(), _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::internal::supported_properties.name(), _))
+ .Times(AnyNumber())
+ .WillRepeatedly(Return(std::vector<ov::PropertyName>{ov::internal::caching_properties.name(),
+ ov::internal::model_sharing_context.name()}));
+ EXPECT_CALL(*mockPlugin, get_property(ov::internal::caching_properties.name(), _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::device::capabilities.name(), _)).Times(AnyNumber());
+ {
+ // 1st compile
+ EXPECT_CALL(*mockPlugin, compile_model(A<const std::shared_ptr<const ov::Model>&>(), _, _))
+ .Times(m_remoteContext ? 1 : 0);
+ EXPECT_CALL(*mockPlugin,
+ compile_model(A<const std::shared_ptr<const ov::Model>&>(), Contains(IsAnyShCtx(empty_ctx))))
+ .Times(!m_remoteContext ? 1 : 0);
+ EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<const ov::Tensor&>(), _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<const ov::Tensor&>(), _)).Times(0);
+ m_post_mock_net_callbacks.emplace_back([&](MockICompiledModelImpl& net) {
+ EXPECT_CALL(net, export_model(_)).Times(1);
+ EXPECT_CALL(net, get_property(ov::internal::model_sharing_context.name()))
+ .WillOnce(Return(ov::Any(std::make_shared<const ov::wsh::Context>(ctx1))));
+ });
+ testLoad([&](ov::Core& core) {
+ core.set_property(ov::cache_dir(m_cacheDir));
+ m_testFunction(core);
+ });
+ }
+ EXPECT_EQ(comp_models.size(), 1);
+ m_post_mock_net_callbacks.pop_back();
+ {
+ // 2nd import
+ EXPECT_CALL(*mockPlugin, compile_model(A<const std::shared_ptr<const ov::Model>&>(), _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, compile_model(A<const std::shared_ptr<const ov::Model>&>(), _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), _, Contains(IsAnyShCtx(ctx1))))
+ .Times(m_remoteContext ? 1 : 0);
+ EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), Contains(IsAnyShCtx(ctx1))))
+ .Times(!m_remoteContext ? 1 : 0);
+ EXPECT_CALL(*mockPlugin, import_model(A<const ov::Tensor&>(), _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<const ov::Tensor&>(), _)).Times(0);
+
+ EXPECT_CALL(*comp_models.back(), get_property(ov::internal::model_sharing_context.name()))
+ .WillRepeatedly(Return(ov::Any(std::make_shared<const ov::wsh::Context>(empty_ctx))));
+
+ testLoad([&](ov::Core& core) {
+ core.set_property(ov::cache_dir(m_cacheDir));
+ m_testFunction(core);
+ });
+ EXPECT_EQ(comp_models.size(), 1);
+ }
}
-TEST_P(CachingTest, test_cache_path_as_file) {
- GTEST_SKIP() << "Not implemented yet";
+TEST_P(CachingTest, test_share_weight_create_ctx_multiple_compilation) {
+ // use ov::weight path as cache property to force different blob id for same model
+ m_cacheDir += ".bin";
+ const auto empty_ctx = ov::wsh::Context();
+ auto cm1 = ov::wsh::WeightMetaMap{{1, ov::wsh::WeightMetaData{0, 200, ov::element::u8}},
+ {2, ov::wsh::WeightMetaData{200, 200, ov::element::u8}}};
+ auto wt_c1 = ov::wsh::WeightRegistry{{10, cm1}};
+ auto ctx1 = ov::wsh::Context();
+ ctx1.m_weight_registry = wt_c1;
+
+ auto cm2 = ov::wsh::WeightMetaMap{{1, ov::wsh::WeightMetaData{0, 300, ov::element::u8}},
+ {2, ov::wsh::WeightMetaData{300, 300, ov::element::u8}}};
+ auto wt_c2 = ov::wsh::WeightRegistry{{20, cm2}};
+ auto ctx2 = ov::wsh::Context();
+ ctx2.m_weight_registry = wt_c2;
+
+ auto final_ctx = ov::wsh::Context();
+ final_ctx.m_weight_registry = {{10, cm1}, {20, cm2}};
+ EXPECT_CALL(*mockPlugin, get_property(ov::supported_properties.name(), _))
+ .Times(AnyNumber())
+ .WillRepeatedly(Return(std::vector<ov::PropertyName>{ov::supported_properties.name(),
+ ov::device::capabilities.name(),
+ ov::device::architecture.name(),
+ ov::weights_path.name(),
+ ov::hint::model.name()}));
+ EXPECT_CALL(*mockPlugin, get_property(ov::device::capability::EXPORT_IMPORT, _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::device::architecture.name(), _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::internal::supported_properties.name(), _))
+ .Times(AnyNumber())
+ .WillRepeatedly(Return(std::vector<ov::PropertyName>{ov::internal::caching_properties.name(),
+ ov::internal::model_sharing_context.name()}));
+ EXPECT_CALL(*mockPlugin, get_property(ov::internal::caching_properties.name(), _))
+ .Times(AnyNumber())
+ .WillRepeatedly(
+ Return(std::vector<ov::PropertyName>{ov::weights_path.name(), ov::device::architecture.name()}));
+ EXPECT_CALL(*mockPlugin, get_property(ov::device::capabilities.name(), _)).Times(AnyNumber());
+ {
+ // 1st compile
+ EXPECT_CALL(*mockPlugin, compile_model(A<const std::shared_ptr<const ov::Model>&>(), _, _))
+ .Times(m_remoteContext ? 1 : 0);
+ EXPECT_CALL(*mockPlugin,
+ compile_model(A<const std::shared_ptr<const ov::Model>&>(), Contains(IsAnyShCtx(empty_ctx))))
+ .Times(!m_remoteContext ? 1 : 0);
+ EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<const ov::Tensor&>(), _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<const ov::Tensor&>(), _)).Times(0);
+ EXPECT_CALL(*mockPlugin, get_property(ov::weights_path.name(), _)).WillOnce(Return(""));
+ m_post_mock_net_callbacks.emplace_back([&](MockICompiledModelImpl& net) {
+ EXPECT_CALL(net, export_model(_)).Times(1);
+ EXPECT_CALL(net, get_property(ov::internal::model_sharing_context.name()))
+ .WillOnce(Return(ov::Any(std::make_shared<const ov::wsh::Context>(ctx1))));
+ });
+ testLoad([&](ov::Core& core) {
+ core.set_property(ov::cache_dir(m_cacheDir));
+ m_testFunction(core);
+ });
+ EXPECT_EQ(comp_models.size(), 1);
+ m_post_mock_net_callbacks.pop_back();
+ }
+ {
+ // 2nd compile
+ EXPECT_CALL(*mockPlugin, compile_model(A<const std::shared_ptr<const ov::Model>&>(), _, _))
+ .Times(m_remoteContext ? 1 : 0);
+ EXPECT_CALL(*mockPlugin,
+ compile_model(A<const std::shared_ptr<const ov::Model>&>(), Contains(IsAnyShCtx(ctx1))))
+ .Times(!m_remoteContext ? 1 : 0);
+ EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<const ov::Tensor&>(), _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<const ov::Tensor&>(), _)).Times(0);
+
+ m_post_mock_net_callbacks.emplace_back([&](MockICompiledModelImpl& net) {
+ EXPECT_CALL(net, export_model(_)).Times(1);
+ EXPECT_CALL(net, get_property(ov::internal::model_sharing_context.name()))
+ .WillOnce(Return(ov::Any(std::make_shared<const ov::wsh::Context>(ctx2))));
+ });
+
+ testLoad([&](ov::Core& core) {
+ core.set_property(ov::cache_dir(m_cacheDir));
+ const auto config = ov::AnyMap{{ov::weights_path("test_path")}};
+ m_testFunctionWithCfg(core, config);
+ });
+ EXPECT_EQ(comp_models.size(), 2);
+ m_post_mock_net_callbacks.pop_back();
+ }
+ {
+ // import 2nd model
+ EXPECT_CALL(*mockPlugin, compile_model(A<const std::shared_ptr<const ov::Model>&>(), _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, compile_model(A<const std::shared_ptr<const ov::Model>&>(), _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), _, _)).Times(m_remoteContext ? 1 : 0);
+ EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), Contains(IsAnyShCtx(final_ctx))))
+ .Times(!m_remoteContext ? 1 : 0);
+ EXPECT_CALL(*mockPlugin, import_model(A<const ov::Tensor&>(), _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<const ov::Tensor&>(), _)).Times(0);
+ testLoad([&](ov::Core& core) {
+ core.set_property(ov::cache_dir(m_cacheDir));
+ const auto config = ov::AnyMap{{ov::weights_path("test_path")}};
+ m_testFunctionWithCfg(core, config);
+ });
+ EXPECT_EQ(comp_models.size(), 2);
+ }
+ {
+ // import 1st model
+ EXPECT_CALL(*mockPlugin, compile_model(A<const std::shared_ptr<const ov::Model>&>(), _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, compile_model(A<const std::shared_ptr<const ov::Model>&>(), _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), _, _)).Times(m_remoteContext ? 1 : 0);
+ EXPECT_CALL(*mockPlugin, import_model(A<std::istream&>(), Contains(IsAnyShCtx(final_ctx))))
+ .Times(!m_remoteContext ? 1 : 0);
+ EXPECT_CALL(*mockPlugin, import_model(A<const ov::Tensor&>(), _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(A<const ov::Tensor&>(), _)).Times(0);
+ EXPECT_CALL(*mockPlugin, get_property(ov::weights_path.name(), _)).WillOnce(Return(""));
+ testLoad([&](ov::Core& core) {
+ core.set_property(ov::cache_dir(m_cacheDir));
+ m_testFunction(core);
+ });
+ EXPECT_EQ(comp_models.size(), 2);
+ }
}
#if defined(ENABLE_OV_IR_FRONTEND)
diff --git a/src/inference/tests/unit/single_file_storage.cpp b/src/inference/tests/unit/single_file_storage.cpp
index cef9d7d076a..1c427da1888 100644
--- a/src/inference/tests/unit/single_file_storage.cpp
+++ b/src/inference/tests/unit/single_file_storage.cpp
@@ -209,12 +209,12 @@ TEST_F(SingleFileStorageTest, ContextMetaWriteRead) {
const auto meta_read_test = [&](SingleFileStorage& storage) {
auto got_context = storage.get_context();
- EXPECT_EQ(got_context.m_weight_registry.size(), 2);
+ EXPECT_EQ(got_context->m_weight_registry.size(), 2);
for (const auto& [source_id, const_meta] : test_context.m_weight_registry) {
- auto& got_meta_data = got_context.m_weight_registry;
+ auto& got_meta_data = got_context->m_weight_registry;
ASSERT_EQ(got_meta_data.count(source_id), 1);
- EXPECT_EQ(got_context.m_weight_registry[source_id].size(),
+ EXPECT_EQ(got_context->m_weight_registry[source_id].size(),
test_context.m_weight_registry[source_id].size());
for (const auto& [const_id, expected_props] : const_meta) {
ASSERT_EQ(got_meta_data[source_id].count(const_id), 1);
@@ -244,9 +244,9 @@ TEST_F(SingleFileStorageTest, ContextMetaAppendDelta) {
m_storage->write_context(test_context);
auto got_context = m_storage->get_context();
- EXPECT_EQ(got_context.m_weight_registry.size(), 2);
- EXPECT_EQ(got_context.m_weight_registry[1].size(), 2);
- EXPECT_EQ(got_context.m_weight_registry[2].size(), 1);
+ EXPECT_EQ(got_context->m_weight_registry.size(), 2);
+ EXPECT_EQ(got_context->m_weight_registry[1].size(), 2);
+ EXPECT_EQ(got_context->m_weight_registry[2].size(), 1);
m_storage.reset();
const auto file_size_after_first_write = test::utils::fileSize(m_file_path.string());
@@ -312,7 +312,7 @@ TEST_F(SingleFileStorageTest, ContextWeightSourceAppendDelta) {
test_context.m_cache_sources[11].m_weights = buffer_2;
m_storage->write_context(test_context);
- EXPECT_EQ(m_storage->get_context().m_cache_sources.size(), 2);
+ EXPECT_EQ(m_storage->get_context()->m_cache_sources.size(), 2);
m_storage.reset();
const auto file_size_after_first_write = test::utils::fileSize(m_file_path.string());