Skip to content

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

[core] Add hint::compiled_blob property (#29354)

Pawel Raasz65917b24729e
diff --git a/src/common/util/include/openvino/util/variant_visitor.hpp b/src/common/util/include/openvino/util/variant_visitor.hpp
new file mode 100644
index 00000000000..7898a1144e4
--- /dev/null
+++ b/src/common/util/include/openvino/util/variant_visitor.hpp
@@ -0,0 +1,15 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+namespace ov::util {
+
+template <class... Ts>
+struct VariantVisitor : Ts... {
+ using Ts::operator()...;
+};
+
+template <class... Ts>
+VariantVisitor(Ts...) -> VariantVisitor<Ts...>;
+} // namespace ov::util
diff --git a/src/inference/dev_api/openvino/runtime/internal_properties.hpp b/src/inference/dev_api/openvino/runtime/internal_properties.hpp
index e2602462258..566a82a9790 100644
--- a/src/inference/dev_api/openvino/runtime/internal_properties.hpp
+++ b/src/inference/dev_api/openvino/runtime/internal_properties.hpp
@@ -37,12 +37,6 @@ static constexpr Property<std::vector<PropertyName>, PropertyMutability::RO> cac
*/
static constexpr Property<bool, PropertyMutability::RO> caching_with_mmap{"CACHING_WITH_MMAP"};
-/**
- * @brief Property to get a ov::AlignedBuffer with cached model
- * @ingroup ov_dev_api_plugin_api
- */
-static constexpr Property<std::shared_ptr<ov::AlignedBuffer>, PropertyMutability::RW> cached_model_buffer{"CACHED_MODEL_BUFFER"};
-
/**
* @brief Allow to create exclusive_async_requests with one executor
* @ingroup ov_dev_api_plugin_api
diff --git a/src/inference/include/openvino/runtime/properties.hpp b/src/inference/include/openvino/runtime/properties.hpp
index 76d8f1e3a6c..01ec1624344 100644
--- a/src/inference/include/openvino/runtime/properties.hpp
+++ b/src/inference/include/openvino/runtime/properties.hpp
@@ -24,6 +24,7 @@
#include "openvino/core/except.hpp"
#include "openvino/core/type/element_type.hpp"
#include "openvino/runtime/common.hpp"
+#include "openvino/runtime/tensor.hpp"
namespace ov {
@@ -607,6 +608,13 @@ static constexpr Property<element::Type, PropertyMutability::RW> kv_cache_precis
*/
static constexpr Property<float, PropertyMutability::RW> activations_scale_factor{"ACTIVATIONS_SCALE_FACTOR"};
+/** @brief Hint for device to use model compiled blob.
+ * @ingroup ov_runtime_cpp_prop_api
+ *
+ * The property is used pass compiled blob as ov::Tensor.
+ * The blob can be regular or weightless model. The `weights_path` property is hint where to look for weights.
+ */
+inline constexpr Property<Tensor, PropertyMutability::RW> compiled_blob{"COMPILED_BLOB"};
} // namespace hint
/**
diff --git a/src/inference/src/cache_manager.hpp b/src/inference/src/cache_manager.hpp
index eb5d92bed1e..d0f67529356 100644
--- a/src/inference/src/cache_manager.hpp
+++ b/src/inference/src/cache_manager.hpp
@@ -15,6 +15,7 @@
#include <string>
#include "openvino/runtime/shared_buffer.hpp"
+#include "openvino/runtime/tensor.hpp"
#include "openvino/util/file_util.hpp"
#include "openvino/util/mmap_object.hpp"
@@ -69,7 +70,7 @@ public:
/**
* @brief Function passing created input stream
*/
- using StreamReader = std::function<void(std::istream&, std::shared_ptr<ov::AlignedBuffer>)>;
+ using StreamReader = std::function<void(std::istream&, ov::Tensor&)>;
/**
* @brief Callback when OpenVINO intends to read model from cache
@@ -135,19 +136,13 @@ private:
void read_cache_entry(const std::string& id, bool enable_mmap, StreamReader reader) override {
// Fix the bug caused by pugixml, which may return unexpected results if the locale is different from "C".
ScopedLocale plocal_C(LC_ALL, "C");
- auto blob_file_name = getBlobFile(id);
+ const auto blob_file_name = getBlobFile(id);
if (ov::util::file_exists(blob_file_name)) {
- if (enable_mmap) {
- auto mmap = ov::load_mmap_object(blob_file_name);
- auto shared_buffer =
- std::make_shared<ov::SharedBuffer<std::shared_ptr<MappedMemory>>>(mmap->data(), mmap->size(), mmap);
- OwningSharedStreamBuffer buf(shared_buffer);
- std::istream stream(&buf);
- reader(stream, shared_buffer);
- } else {
- std::ifstream stream(blob_file_name, std::ios_base::binary);
- reader(stream, nullptr);
- }
+ auto compiled_blob =
+ read_tensor_data(blob_file_name, element::u8, PartialShape::dynamic(1), 0, enable_mmap);
+ SharedStreamBuffer buf{reinterpret_cast<char*>(compiled_blob.data()), compiled_blob.get_byte_size()};
+ std::istream stream(&buf);
+ reader(stream, compiled_blob);
}
}
diff --git a/src/inference/src/dev/core_impl.cpp b/src/inference/src/dev/core_impl.cpp
index 06cef1c95e8..8be365a13e1 100644
--- a/src/inference/src/dev/core_impl.cpp
+++ b/src/inference/src/dev/core_impl.cpp
@@ -5,6 +5,7 @@
#include "core_impl.hpp"
#include <memory>
+#include <variant>
#include "check_network_batchable.hpp"
#include "itt.hpp"
@@ -29,6 +30,7 @@
#include "openvino/util/common_util.hpp"
#include "openvino/util/file_util.hpp"
#include "openvino/util/shared_object.hpp"
+#include "openvino/util/variant_visitor.hpp"
#include "openvino/util/xml_parse_utils.hpp"
#include "ov_plugins.hpp"
#ifdef PROXY_PLUGIN_ENABLED
@@ -207,6 +209,64 @@ static const auto core_properties_names =
static const auto auto_batch_properties_names =
ov::util::make_array(ov::auto_batch_timeout.name(), ov::hint::allow_auto_batching.name());
+
+ov::util::Path extract_weight_path(const std::string& compiled_properties) {
+ if (auto start = compiled_properties.find(ov::weights_path.name()); start != std::string::npos) {
+ start += std::string_view{ov::weights_path.name()}.size() + 1;
+ auto length = compiled_properties.find(",", start);
+ if (length != std::string::npos) {
+ length -= start;
+ }
+ return {compiled_properties.substr(start, length)};
+ } else {
+ return {};
+ }
+}
+
+using model_hint_t = std::variant<std::shared_ptr<const ov::Model>, std::string>;
+
+ov::SoPtr<ov::ICompiledModel> import_compiled_model(const ov::Plugin& plugin,
+ const ov::SoPtr<ov::IRemoteContext>& context,
+ const ov::AnyMap& config) {
+ ov::SoPtr<ov::ICompiledModel> compiled_model;
+ if (auto blob_hint = config.find(ov::hint::compiled_blob.name()); blob_hint != config.end()) {
+ try {
+ auto compiled_blob = blob_hint->second.as<ov::Tensor>();
+ ov::SharedStreamBuffer buffer{reinterpret_cast<char*>(compiled_blob.data()), compiled_blob.get_byte_size()};
+ std::istream stream{&buffer};
+ compiled_model =
+ context ? plugin.import_model(stream, context, config) : plugin.import_model(stream, config);
+ } catch (...) {
+ }
+ }
+ return compiled_model;
+}
+
+ov::SoPtr<ov::ICompiledModel> import_compiled_model(const ov::Plugin& plugin,
+ const ov::SoPtr<ov::IRemoteContext>& context,
+ const ov::AnyMap& config,
+ const model_hint_t& model_hint) {
+ auto cfg = config;
+ const auto apply_model_hint = ov::util::VariantVisitor{
+ [&cfg, &plugin](const std::shared_ptr<const ov::Model>& model_ptr) {
+ if (model_ptr != nullptr &&
+ ov::util::contains(plugin.get_property(ov::supported_properties), ov::hint::model)) {
+ cfg[ov::hint::model.name()] = std::const_pointer_cast<ov::Model>(model_ptr);
+ }
+ },
+ [&cfg, &plugin](const std::string& model_path) {
+ if (cfg.count(ov::weights_path.name()) == 0 &&
+ ov::util::contains(plugin.get_property(ov::supported_properties), ov::weights_path)) {
+ ov::util::Path weights_path{model_path};
+ weights_path.replace_extension(".bin");
+ if (ov::util::file_exists(weights_path)) {
+ cfg[ov::weights_path.name()] = weights_path.string();
+ }
+ }
+ }};
+ std::visit(apply_model_hint, model_hint);
+ return import_compiled_model(plugin, context, cfg);
+}
} // namespace
bool ov::is_config_applicable(const std::string& user_device_name, const std::string& subprop_device_name) {
@@ -770,11 +830,13 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model(const std::shared_ptr<
auto parsed = parseDeviceNameIntoConfig(deviceName, coreConfig, config_with_batch, is_proxy_device(deviceName));
auto plugin = get_plugin(parsed._deviceName);
- ov::SoPtr<ov::ICompiledModel> res;
// will consume ov::cache_dir if plugin not support it
auto cacheManager = parsed._core_config.get_cache_config_for_device(plugin, parsed._config)._cacheManager;
+ auto res = import_compiled_model(plugin, {}, parsed._config, model);
// Skip caching for proxy plugin. HW plugin will load network from the cache
- if (cacheManager && device_supports_model_caching(plugin, parsed._config) && !is_proxy_device(plugin)) {
+ if (res) {
+ // hint::compiled_blob is set and imported skip compilation
+ } else if (cacheManager && device_supports_model_caching(plugin, parsed._config) && !is_proxy_device(plugin)) {
CacheContent cacheContent{cacheManager, parsed._core_config.get_enable_mmap()};
cacheContent.blobId = ov::ModelCache::compute_hash(model, create_compile_config(plugin, parsed._config));
cacheContent.model = std::const_pointer_cast<ov::Model>(model);
@@ -805,11 +867,13 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model(const std::shared_ptr<
auto parsed = parseDeviceNameIntoConfig(deviceName, coreConfig, config_with_batch, is_proxy_device(deviceName));
auto plugin = get_plugin(parsed._deviceName);
- ov::SoPtr<ov::ICompiledModel> res;
// will consume ov::cache_dir if plugin not support it
auto cacheManager = parsed._core_config.get_cache_config_for_device(plugin, parsed._config)._cacheManager;
+ auto res = import_compiled_model(plugin, context, parsed._config, model);
// Skip caching for proxy plugin. HW plugin will load network from the cache
- if (cacheManager && device_supports_model_caching(plugin, parsed._config) && !is_proxy_device(plugin)) {
+ if (res) {
+ // hint::compiled_blob is set and imported skip compilation
+ } else if (cacheManager && device_supports_model_caching(plugin, parsed._config) && !is_proxy_device(plugin)) {
CacheContent cacheContent{cacheManager, parsed._core_config.get_enable_mmap()};
cacheContent.blobId = ov::ModelCache::compute_hash(model, create_compile_config(plugin, parsed._config));
std::unique_ptr<CacheGuardEntry> lock = cacheGuard.get_hash_lock(cacheContent.blobId);
@@ -830,11 +894,13 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model(const std::string& mod
auto parsed = parse_device_config(device_name, coreConfig, config, false);
// in case of compile_model(file_name), we need to clear-up core-level properties
auto plugin = get_plugin(parsed._deviceName);
- ov::SoPtr<ov::ICompiledModel> compiled_model;
// will consume ov::cache_dir if plugin not support it
auto cacheManager = parsed._core_config.get_cache_config_for_device(plugin, parsed._config)._cacheManager;
+ auto compiled_model = import_compiled_model(plugin, {}, parsed._config, model_path);
- if (cacheManager && device_supports_model_caching(plugin, parsed._config) && !is_proxy_device(plugin)) {
+ if (compiled_model) {
+ // hint::compiled_blob is set and imported skip compilation
+ } else if (cacheManager && device_supports_model_caching(plugin, parsed._config) && !is_proxy_device(plugin)) {
// Skip caching for proxy plugin. HW plugin will load network from the cache
CoreConfig::remove_core_skip_cache_dir(parsed._config);
CacheContent cacheContent{cacheManager, parsed._core_config.get_enable_mmap(), model_path};
@@ -858,11 +924,13 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::compile_model(const std::string& mod
OV_ITT_SCOPED_TASK(ov::itt::domains::OV, "Core::compile_model::from_memory");
auto parsed = parseDeviceNameIntoConfig(device_name, coreConfig, config);
auto plugin = get_plugin(parsed._deviceName);
- ov::SoPtr<ov::ICompiledModel> compiled_model;
// will consume ov::cache_dir if plugin not support it
auto cacheManager = parsed._core_config.get_cache_config_for_device(plugin, parsed._config)._cacheManager;
+ auto compiled_model = import_compiled_model(plugin, {}, parsed._config);
// Skip caching for proxy plugin. HW plugin will load network from the cache
- if (cacheManager && device_supports_model_caching(plugin, parsed._config) && !is_proxy_device(plugin)) {
+ if (compiled_model) {
+ // hint::compiled_blob is set and imported skip compilation
+ } else if (cacheManager && device_supports_model_caching(plugin, parsed._config) && !is_proxy_device(plugin)) {
CacheContent cacheContent{cacheManager, parsed._core_config.get_enable_mmap()};
cacheContent.blobId =
ov::ModelCache::compute_hash(model_str, weights, create_compile_config(plugin, parsed._config));
@@ -1443,12 +1511,12 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::load_model_from_cache(
cacheContent.blobId,
cacheContent.mmap_enabled && ov::util::contains(plugin.get_property(ov::internal::supported_properties),
ov::internal::caching_with_mmap),
- [&](std::istream& networkStream, std::shared_ptr<ov::AlignedBuffer> model_buffer) {
+ [&](std::istream& networkStream, ov::Tensor& compiled_blob) {
OV_ITT_SCOPE(FIRST_INFERENCE,
ov::itt::domains::LoadTime,
"Core::load_model_from_cache::ReadStreamAndImport");
+ ov::CompiledBlobHeader header;
try {
- ov::CompiledBlobHeader header;
networkStream >> header;
if (header.get_file_info() != ov::ModelCache::calculate_file_info(cacheContent.modelPath)) {
// Original file is changed, don't use cache
@@ -1477,21 +1545,34 @@ 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 &&
+ util::contains(plugin.get_property(ov::supported_properties), ov::hint::model)) {
+ update_config[ov::hint::model.name()] = cacheContent.model;
+ }
if (util::contains(plugin.get_property(ov::supported_properties), ov::hint::model) &&
cacheContent.model) {
update_config[ov::hint::model.name()] = cacheContent.model;
}
if (util::contains(plugin.get_property(ov::supported_properties), ov::weights_path)) {
- std::filesystem::path weights_path = cacheContent.modelPath;
+ util::Path weights_path;
+
+ if (auto&& path_hint = update_config.find(ov::weights_path.name());
+ 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.modelPath;
+ weights_path.replace_extension(".bin");
+ }
weights_path.replace_extension(".bin");
if (ov::util::file_exists(weights_path)) {
update_config[ov::weights_path.name()] = weights_path.string();
}
}
- if (model_buffer) {
- update_config[ov::internal::cached_model_buffer.name()] = model_buffer;
+
+ if (compiled_blob) {
+ update_config[ov::hint::compiled_blob.name()] = compiled_blob;
}
compiled_model = context ? plugin.import_model(networkStream, context, update_config)
: plugin.import_model(networkStream, update_config);
diff --git a/src/inference/tests/functional/caching_test.cpp b/src/inference/tests/functional/caching_test.cpp
index f0ad5c26907..961f1649d0a 100644
--- a/src/inference/tests/functional/caching_test.cpp
+++ b/src/inference/tests/functional/caching_test.cpp
@@ -33,6 +33,7 @@
#include "openvino/runtime/iplugin.hpp"
#include "openvino/runtime/iremote_context.hpp"
#include "openvino/runtime/properties.hpp"
+#include "openvino/runtime/shared_buffer.hpp"
#include "unit_test_utils/mocks/openvino/runtime/mock_iasync_infer_request.hpp"
#include "unit_test_utils/mocks/openvino/runtime/mock_icompiled_model.hpp"
#include "unit_test_utils/mocks/openvino/runtime/mock_iplugin.hpp"
@@ -2429,10 +2430,11 @@ TEST_P(CachingTest, Load_mmap) {
if (m_checkConfigCb) {
m_checkConfigCb(config);
}
- std::shared_ptr<ov::AlignedBuffer> model_buffer;
- if (config.count(ov::internal::cached_model_buffer.name()))
- model_buffer = config.at(ov::internal::cached_model_buffer.name()).as<std::shared_ptr<ov::AlignedBuffer>>();
- EXPECT_TRUE(model_buffer);
+ ov::Tensor compiled_blob;
+ if (config.count(ov::hint::compiled_blob.name()))
+ compiled_blob = config.at(ov::hint::compiled_blob.name()).as<ov::Tensor>();
+
+ EXPECT_TRUE(static_cast<bool>(compiled_blob));
std::string name;
istr >> name;
@@ -2476,10 +2478,8 @@ TEST_P(CachingTest, Load_mmap_is_disabled) {
if (m_checkConfigCb) {
m_checkConfigCb(config);
}
- std::shared_ptr<ov::AlignedBuffer> model_buffer;
- if (config.count(ov::internal::cached_model_buffer.name()))
- model_buffer = config.at(ov::internal::cached_model_buffer.name()).as<std::shared_ptr<ov::AlignedBuffer>>();
- EXPECT_FALSE(model_buffer);
+
+ EXPECT_GT(config.count(ov::hint::compiled_blob.name()), 0);
std::string name;
istr >> name;
@@ -2523,10 +2523,8 @@ TEST_P(CachingTest, Load_mmap_is_not_supported_by_plugin) {
if (m_checkConfigCb) {
m_checkConfigCb(config);
}
- std::shared_ptr<ov::AlignedBuffer> model_buffer;
- if (config.count(ov::internal::cached_model_buffer.name()))
- model_buffer = config.at(ov::internal::cached_model_buffer.name()).as<std::shared_ptr<ov::AlignedBuffer>>();
- EXPECT_FALSE(model_buffer);
+
+ EXPECT_GT(config.count(ov::hint::compiled_blob.name()), 0);
std::string name;
istr >> name;
@@ -2565,10 +2563,8 @@ TEST_P(CachingTest, Load_mmap_is_disabled_local_cfg) {
if (m_checkConfigCb) {
m_checkConfigCb(config);
}
- std::shared_ptr<ov::AlignedBuffer> model_buffer;
- if (config.count(ov::internal::cached_model_buffer.name()))
- model_buffer = config.at(ov::internal::cached_model_buffer.name()).as<std::shared_ptr<ov::AlignedBuffer>>();
- EXPECT_FALSE(model_buffer);
+
+ EXPECT_GT(config.count(ov::hint::compiled_blob.name()), 0);
std::string name;
istr >> name;
@@ -2611,10 +2607,7 @@ TEST_P(CachingTest, Load_mmap_is_not_supported_by_plugin_local_cfg) {
if (m_checkConfigCb) {
m_checkConfigCb(config);
}
- std::shared_ptr<ov::AlignedBuffer> model_buffer;
- if (config.count(ov::internal::cached_model_buffer.name()))
- model_buffer = config.at(ov::internal::cached_model_buffer.name()).as<std::shared_ptr<ov::AlignedBuffer>>();
- EXPECT_FALSE(model_buffer);
+ EXPECT_GT(config.count(ov::hint::compiled_blob.name()), 0);
std::string name;
istr >> name;
@@ -2647,6 +2640,135 @@ TEST_P(CachingTest, Load_mmap_is_not_supported_by_plugin_local_cfg) {
std::cout << "Caching Load multiple threads test completed. Tried " << index << " times" << std::endl;
}
+TEST_P(CachingTest, import_from_cache_model_and_weights_path_properties_not_supported) {
+ ON_CALL(*mockPlugin, import_model(_, _)).WillByDefault(Invoke([&](std::istream& istr, const ov::AnyMap& config) {
+ if (m_checkConfigCb) {
+ m_checkConfigCb(config);
+ }
+ EXPECT_EQ(config.count(ov::hint::compiled_blob.name()), 1);
+ EXPECT_EQ(config.count(ov::hint::model.name()), 0);
+ EXPECT_EQ(config.count(ov::weights_path.name()), 0);
+
+ std::string name;
+ istr >> name;
+ char space;
+ istr.read(&space, 1);
+ 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());
+ EXPECT_CALL(*mockPlugin, get_property(ov::device::architecture.name(), _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::internal::caching_properties.name(), _)).Times(AnyNumber());
+ if (m_remoteContext) {
+ return; // skip the remote Context test for Multi plugin
+ }
+ 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>&>(), _)).Times(1);
+ EXPECT_CALL(*mockPlugin, import_model(_, _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(_, _)).Times(1);
+ testLoad([&](ov::Core& core) {
+ const auto config = ov::AnyMap{{ov::cache_dir(m_cacheDir)}};
+ m_testFunctionWithCfg(core, config);
+ // load from cache
+ m_testFunctionWithCfg(core, config);
+ });
+}
+
+TEST_P(CachingTest, import_from_cache_model_and_weights_path_properties_are_supported) {
+ ON_CALL(*mockPlugin, get_property(ov::supported_properties.name(), _))
+ .WillByDefault(Invoke([&](const std::string&, const ov::AnyMap&) {
+ return std::vector<ov::PropertyName>{
+ ov::supported_properties.name(),
+ ov::device::capabilities.name(),
+ ov::device::architecture.name(),
+ ov::hint::model.name(),
+ ov::weights_path.name(),
+ };
+ }));
+ ON_CALL(*mockPlugin, import_model(_, _)).WillByDefault(Invoke([&](std::istream& istr, const ov::AnyMap& config) {
+ if (m_checkConfigCb) {
+ m_checkConfigCb(config);
+ }
+ EXPECT_EQ(config.count(ov::hint::compiled_blob.name()), 1);
+ EXPECT_EQ(config.count(ov::hint::model.name()), m_type != TestLoadType::EModelName ? 1 : 0);
+ EXPECT_EQ(config.count(ov::weights_path.name()), m_type == TestLoadType::EModelName ? 1 : 0);
+
+ std::string name;
+ istr >> name;
+ char space;
+ istr.read(&space, 1);
+ 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());
+ EXPECT_CALL(*mockPlugin, get_property(ov::device::architecture.name(), _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::internal::caching_properties.name(), _)).Times(AnyNumber());
+ if (m_remoteContext) {
+ return; // skip the remote Context test for Multi plugin
+ }
+ 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>&>(), _)).Times(1);
+ EXPECT_CALL(*mockPlugin, import_model(_, _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(_, _)).Times(1);
+ testLoad([&](ov::Core& core) {
+ const auto config = ov::AnyMap{{ov::cache_dir(m_cacheDir)}};
+ m_testFunctionWithCfg(core, config);
+ // load from cache
+ m_testFunctionWithCfg(core, config);
+ });
+}
+
+TEST_P(CachingTest, import_from_compiled_blob_weights_path_property_is_supported) {
+ ON_CALL(*mockPlugin, get_property(ov::supported_properties.name(), _))
+ .WillByDefault(Invoke([&](const std::string&, const ov::AnyMap&) {
+ 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()};
+ }));
+ ON_CALL(*mockPlugin, import_model(_, _)).WillByDefault(Invoke([&](std::istream& istr, const ov::AnyMap& config) {
+ if (m_checkConfigCb) {
+ m_checkConfigCb(config);
+ }
+ EXPECT_EQ(config.count(ov::hint::compiled_blob.name()), 1);
+ EXPECT_EQ(config.count(ov::hint::model.name()), m_type != TestLoadType::EModelName ? 1 : 0);
+ EXPECT_EQ(config.count(ov::weights_path.name()), m_type == TestLoadType::EModelName ? 1 : 0);
+ return nullptr;
+ }));
+ EXPECT_CALL(*mockPlugin, get_property(_, _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, query_model(_, _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::device::architecture.name(), _)).Times(AnyNumber());
+ EXPECT_CALL(*mockPlugin, get_property(ov::internal::caching_properties.name(), _)).Times(AnyNumber());
+ if (m_remoteContext) {
+ return; // skip the remote Context test for Multi plugin
+ }
+ m_modelCallback = nullptr;
+ MkDirGuard guard(m_cacheDir);
+ EXPECT_CALL(*mockPlugin, compile_model(_, _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(_, _, _)).Times(0);
+ EXPECT_CALL(*mockPlugin, import_model(_, _)).Times(1);
+
+ EXPECT_CALL(*mockPlugin, compile_model(A<const std::shared_ptr<const ov::Model>&>(), _)).Times(1);
+ if (m_type == TestLoadType::EModelName) {
+ EXPECT_CALL(*mockPlugin, OnCompileModelFromFile()).Times(1);
+ }
+ testLoad([&](ov::Core& core) {
+ auto compiled_blob = ov::Tensor(ov::element::u8, ov::Shape{100});
+ const auto config = ov::AnyMap{{ov::hint::compiled_blob(compiled_blob)}};
+ m_testFunctionWithCfg(core, config);
+ });
+}
#if defined(ENABLE_OV_IR_FRONTEND)
static std::string getTestCaseName(const testing::TestParamInfo<std::tuple<TestParam, std::string>>& obj) {
diff --git a/src/plugins/auto/tests/functional/shared_tests_instances/skip_tests_config.cpp b/src/plugins/auto/tests/functional/shared_tests_instances/skip_tests_config.cpp
index 3bd0bdfae93..6425d12f0df 100644
--- a/src/plugins/auto/tests/functional/shared_tests_instances/skip_tests_config.cpp
+++ b/src/plugins/auto/tests/functional/shared_tests_instances/skip_tests_config.cpp
@@ -15,6 +15,10 @@ std::vector<std::string> disabledTestPatterns() {
R"(.*Behavior.*OVCompiledModelBaseTest.*canSetConfigToCompiledModel.*)",
R"(.*Behavior.*OVCompiledModelBaseTest.*canExportModel.*)",
R"(.*Behavior.*OVCompiledModelBaseTest.*canSetConfigToCompiledModelWithIncorrectConfig.*)",
+ // requires export_model be implemented
+ R"(.*Behavior.*OVCompiledModelBaseTest.*import_from_weightless_blob.*targetDevice=(MULTI|AUTO).*)",
+ R"(.*Behavior.*OVCompiledModelBaseTest.*compile_from.*_blob.*targetDevice=(MULTI|AUTO).*)",
+ R"(.*Behavior.*OVCompiledModelBaseTest.*use_blob_hint.*targetDevice=(MULTI|AUTO).*)",
// unsupported metrics
R"(.*smoke_AutoOVGetMetricPropsTest.*OVGetMetricPropsTest.*(AVAILABLE_DEVICES|OPTIMIZATION_CAPABILITIES|RANGE_FOR_ASYNC_INFER_REQUESTS|RANGE_FOR_STREAMS).*)",
diff --git a/src/plugins/auto_batch/tests/functional/skip_tests_config.cpp b/src/plugins/auto_batch/tests/functional/skip_tests_config.cpp
index 88a606a8a2d..d10439a4c80 100644
--- a/src/plugins/auto_batch/tests/functional/skip_tests_config.cpp
+++ b/src/plugins/auto_batch/tests/functional/skip_tests_config.cpp
@@ -25,6 +25,10 @@ std::vector<std::string> disabledTestPatterns() {
R"(.*LoadNetworkCreateDefaultExecGraphResult.*)",
// BATCH/TEMPLATE plugin doesn't support this case
R"(.*OVInferRequestPerfCountersTest.*CheckOperationInProfilingInfo.*)",
+ // requires export_model be implemented
+ R"(.*Behavior.*OVCompiledModelBaseTest.*import_from_weightless_blob.*targetDevice=(BATCH).*)",
+ R"(.*Behavior.*OVCompiledModelBaseTest.*compile_from.*_blob.*targetDevice=(BATCH).*)",
+ R"(.*Behavior.*OVCompiledModelBaseTest.*use_blob_hint.*targetDevice=(BATCH).*)",
};
return disabled_items;
diff --git a/src/plugins/hetero/tests/functional/skip_tests_config.cpp b/src/plugins/hetero/tests/functional/skip_tests_config.cpp
index 8bd802e7627..d718ad63b76 100644
--- a/src/plugins/hetero/tests/functional/skip_tests_config.cpp
+++ b/src/plugins/hetero/tests/functional/skip_tests_config.cpp
@@ -14,6 +14,12 @@ std::vector<std::string> disabledTestPatterns() {
R"(.*smoke_(Multi|Auto|Hetero)_BehaviorTests.*OVPropertiesTests.*SetCorrectProperties.*)",
R"(.*smoke_(Multi|Auto|Hetero)_BehaviorTests.*OVPropertiesTests.*canSetPropertyAndCheckGetProperty.*)",
R"(.*OVInferRequestCheckTensorPrecision.*get(Input|Output|Inputs|Outputs)From.*FunctionWith(Single|Several).*type=(u4|u1|i4|boolean).*)",
- R"(.*OVGetMetricPropsTest.*OVGetMetricPropsTest.*GetMetricAndPrintNoThrow_AVAILABLE_DEVICES.*)"};
+ R"(.*OVGetMetricPropsTest.*OVGetMetricPropsTest.*GetMetricAndPrintNoThrow_AVAILABLE_DEVICES.*)",
+ // CACHE_MODE property is not supported on NPU
+ R"(.*OVCompiledModelBaseTest.*import_from_.*_blob.*targetDevice=(HETERO.NPU).*)",
+ R"(.*OVCompiledModelBaseTest.*compile_from_.*_blob.*targetDevice=(HETERO.NPU).*)",
+ R"(.*OVCompiledModelBaseTest.*compile_from_cached_weightless_blob.*targetDevice=(HETERO.NPU).*)",
+ R"(.*OVCompiledModelBaseTest.*use_blob_hint_.*targetDevice=CPU.*)",
+ };
return retVector;
}
diff --git a/src/plugins/intel_cpu/src/plugin.cpp b/src/plugins/intel_cpu/src/plugin.cpp
index f93f61441d5..e71b6e30a9b 100644
--- a/src/plugins/intel_cpu/src/plugin.cpp
+++ b/src/plugins/intel_cpu/src/plugin.cpp
@@ -12,6 +12,7 @@
#include "openvino/runtime/intel_cpu/properties.hpp"
#include "openvino/runtime/internal_properties.hpp"
#include "openvino/runtime/properties.hpp"
+#include "openvino/runtime/shared_buffer.hpp"
#include "openvino/runtime/threading/cpu_streams_info.hpp"
#include "openvino/runtime/threading/executor_manager.hpp"
#include "transformations/transformation_pipeline.h"
@@ -585,9 +586,12 @@ std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& model_str
auto _config = config;
std::shared_ptr<ov::AlignedBuffer> model_buffer;
- if (_config.count(ov::internal::cached_model_buffer.name())) {
- model_buffer = _config.at(ov::internal::cached_model_buffer.name()).as<std::shared_ptr<ov::AlignedBuffer>>();
- _config.erase(ov::internal::cached_model_buffer.name());
+ if (auto blob_it = _config.find(ov::hint::compiled_blob.name()); blob_it != _config.end()) {
+ auto compiled_blob = blob_it->second.as<ov::Tensor>();
+ model_buffer = std::make_shared<ov::SharedBuffer<ov::Tensor>>(reinterpret_cast<char*>(compiled_blob.data()),
+ compiled_blob.get_byte_size(),
+ compiled_blob);
+ _config.erase(blob_it);
}
ModelDeserializer deserializer(
diff --git a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/skip_tests_config.cpp b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
index 64d9dfa1b37..2a70f8c68cb 100644
--- a/src/plugins/intel_cpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
+++ b/src/plugins/intel_cpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
@@ -81,6 +81,11 @@ std::vector<std::string> disabledTestPatterns() {
R"(.*Behavior.*OVCompiledModelBaseTest.*canSetConfigToCompiledModel.*)",
R"(.*Behavior.*OVCompiledModelBaseTest.*canExportModel.*)",
R"(.*Behavior.*OVCompiledModelBaseTest.*canSetConfigToCompiledModelWithIncorrectConfig.*)",
+ // requires support CACHE_MODE
+ R"(.*OVCompiledModelBaseTest.*import_from_weightless_blob.*)",
+ R"(.*OVCompiledModelBaseTest.*compile_from_weightless_blob.*)",
+ R"(.*OVCompiledModelBaseTest.*compile_from_cached_weightless_blob.*)",
+
// CPU does not support dynamic rank
// Issue: 66778
R"(.*smoke_BehaviorTests.*InferFullyDynamicNetworkWith(S|G)etTensor.*)",
diff --git a/src/plugins/intel_gpu/src/plugin/plugin.cpp b/src/plugins/intel_gpu/src/plugin/plugin.cpp
index 3f9bf4e412a..63bbf7f31bd 100644
--- a/src/plugins/intel_gpu/src/plugin/plugin.cpp
+++ b/src/plugins/intel_gpu/src/plugin/plugin.cpp
@@ -36,6 +36,7 @@
#include "openvino/runtime/performance_heuristics.hpp"
#include "openvino/runtime/plugin_config.hpp"
#include "openvino/runtime/properties.hpp"
+#include "openvino/runtime/shared_buffer.hpp"
#include "openvino/util/weights_path.hpp"
#include "transformations/common_optimizations/dimension_tracking.hpp"
#include "transformations/init_node_info.hpp"
@@ -321,9 +322,12 @@ std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& model,
}
std::shared_ptr<ov::AlignedBuffer> model_buffer;
- if (_orig_config.count(ov::internal::cached_model_buffer.name())) {
- model_buffer = _orig_config.at(ov::internal::cached_model_buffer.name()).as<std::shared_ptr<ov::AlignedBuffer>>();
- _orig_config.erase(ov::internal::cached_model_buffer.name());
+ if (auto blob_it = _orig_config.find(ov::hint::compiled_blob.name()); blob_it != _orig_config.end()) {
+ auto compiled_blob = blob_it->second.as<ov::Tensor>();
+ model_buffer = std::make_shared<ov::SharedBuffer<ov::Tensor>>(reinterpret_cast<char*>(compiled_blob.data()),
+ compiled_blob.get_byte_size(),
+ compiled_blob);
+ _orig_config.erase(blob_it);
}
ExecutionConfig config = m_configs_map.at(device_id);
diff --git a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/skip_tests_config.cpp b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
index 44d67c8a601..fde6562d342 100644
--- a/src/plugins/intel_gpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
+++ b/src/plugins/intel_gpu/tests/functional/shared_tests_instances/skip_tests_config.cpp
@@ -200,6 +200,8 @@ std::vector<std::string> disabledTestPatterns() {
R"(.*smoke_ConditionGPUTest_static/StaticConditionLayerGPUTest.CompareWithRefs/IS=\(3.6\)_netPRC=i8_ifCond=PARAM_targetDevice=GPU_.*)",
// Issue: 142900
R"(.*smoke_TestsROIAlign_.*ROIAlignV9LayerTest.*)",
+ // Use weight from model not from path hint
+ R"(.*compile_from_weightless_blob.*)",
#if defined(_WIN32)
// by calc abs_threshold with expected value
diff --git a/src/plugins/intel_npu/src/plugin/src/metadata.cpp b/src/plugins/intel_npu/src/plugin/src/metadata.cpp
index e281e0b23cb..a6a5f43d641 100644
--- a/src/plugins/intel_npu/src/plugin/src/metadata.cpp
+++ b/src/plugins/intel_npu/src/plugin/src/metadata.cpp
@@ -98,7 +98,7 @@ std::streampos MetadataBase::getFileSize(std::istream& stream) {
OPENVINO_THROW("Stream is in bad status! Please check the passed stream status!");
}
- if (dynamic_cast<ov::OwningSharedStreamBuffer*>(stream.rdbuf()) != nullptr) {
+ if (dynamic_cast<ov::SharedStreamBuffer*>(stream.rdbuf()) != nullptr) {
return stream.rdbuf()->in_avail();
}
const std::streampos streamStart = stream.tellg();
diff --git a/src/plugins/intel_npu/src/plugin/src/plugin.cpp b/src/plugins/intel_npu/src/plugin/src/plugin.cpp
index c2ef625243c..6775c2ebcdb 100644
--- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp
+++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp
@@ -26,6 +26,7 @@
#include "openvino/op/parameter.hpp"
#include "openvino/runtime/intel_npu/properties.hpp"
#include "openvino/runtime/properties.hpp"
+#include "openvino/runtime/shared_buffer.hpp"
#include "remote_context.hpp"
using namespace intel_npu;
@@ -839,12 +840,15 @@ std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& stream, c
}
std::shared_ptr<ov::AlignedBuffer> modelBuffer;
- // ov::internal::cached_model_buffer has no corresponding "Config" implementation thus we need to remove it from the
+ // ov::hint::compiled_blob has no corresponding "Config" implementation thus we need to remove it from the
// list of properties
- if (npu_plugin_properties.count(ov::internal::cached_model_buffer.name())) {
- modelBuffer =
- npu_plugin_properties.at(ov::internal::cached_model_buffer.name()).as<std::shared_ptr<ov::AlignedBuffer>>();
- npu_plugin_properties.erase(ov::internal::cached_model_buffer.name());
+ if (auto blob_it = npu_plugin_properties.find(ov::hint::compiled_blob.name());
+ blob_it != npu_plugin_properties.end()) {
+ auto compiled_blob = blob_it->second.as<ov::Tensor>();
+ modelBuffer = std::make_shared<ov::SharedBuffer<ov::Tensor>>(reinterpret_cast<char*>(compiled_blob.data()),
+ compiled_blob.get_byte_size(),
+ compiled_blob);
+ npu_plugin_properties.erase(blob_it);
}
const auto propertiesMap = any_copy(npu_plugin_properties);
diff --git a/src/plugins/intel_npu/tests/functional/shared_tests_instances/skip_tests.xml b/src/plugins/intel_npu/tests/functional/shared_tests_instances/skip_tests.xml
index e47003b3863..dcd7e47d770 100644
--- a/src/plugins/intel_npu/tests/functional/shared_tests_instances/skip_tests.xml
+++ b/src/plugins/intel_npu/tests/functional/shared_tests_instances/skip_tests.xml
@@ -1,5 +1,5 @@
<!--
-Example:
+Example:
<skip_config>
<message>skip_message_xxxxxx</message>
<enable_rules>
@@ -79,7 +79,7 @@ Example:
</skip_config>
<!-- EISW 30810 -->
- <skip_config>
+ <skip_config>
<message>Hetero plugin doesn't throw an exception in case of big device ID</message>
<filters>
<filter>.*OVClassLoadNetworkTestNPU.*LoadNetworkHETEROWithBigDeviceIDThrows.*</filter>
@@ -376,7 +376,7 @@ Example:
</skip_config>
<!-- EISW 111510 -->
- <skip_config>
+ <skip_config>
<message>Failing test for NPU device</message>
<filters>
<filter>.*OVClassImportExportTestP.*OVClassCompiledModelImportExportTestP.*ImportNetworkThrowWithDeviceName.*</filter>
@@ -551,7 +551,7 @@ Example:
<filter>.*smoke_RandomUniform/RandomLayerTest_NPU3720.SW.*</filter>
</filters>
</skip_config>
-
+
<!-- EISW ##### -->
<skip_config>
<message>GetExecGraphInfo function is not implemented for NPU plugin</message>
@@ -877,4 +877,15 @@ Example:
<filter>.*OVClassModelOptionalTestP.CompileModelCreateDefaultExecGraphResult.*</filter>
</filters>
</skip_config>
+
+ <!-- CACHE_MODE is not supported on NPU, update test with correct property to make weightless compiled model -->
+ <skip_config>
+ <message>compiled_blob test use `CACHE_MOD` which is not supported on NPU</message>
+ <filters>
+ <filter>.*OVCompiledModelBaseTest.*import_from_.*_blob.*</filter>
+ <filter>.*OVCompiledModelBaseTest.*compile_from_.*_blob.*</filter>
+ <filter>.*OVCompiledModelBaseTest.*compile_from_cached_weightless_blob.*</filter>
+ <filter>.*OVCompiledModelBaseTest.*use_blob_hint_.*</filter>
+ </filters>
+ </skip_config>
</skip_configs>
diff --git a/src/plugins/intel_npu/tests/functional/shared_tests_instances/skip_tests_config.cpp b/src/plugins/intel_npu/tests/functional/shared_tests_instances/skip_tests_config.cpp
index 20e2eff9ed6..9a6c2769bdb 100644
--- a/src/plugins/intel_npu/tests/functional/shared_tests_instances/skip_tests_config.cpp
+++ b/src/plugins/intel_npu/tests/functional/shared_tests_instances/skip_tests_config.cpp
@@ -823,7 +823,7 @@ std::vector<std::string> disabledTestPatterns() {
});
// [Tracking number: E#125086]
- _skipRegistry.addPatterns(devices.has3720() && backendName.isZero(),
+ _skipRegistry.addPatterns(devices.has3720() && backendName.isZero(),
"Failing tests after functional tests migration to OV", {
#ifdef WIN32
".*OVInferRequestPerfCountersExceptionTest.perfCountWereNotEnabledExceptionTest.*",
@@ -901,6 +901,13 @@ std::vector<std::string> disabledTestPatterns() {
_skipRegistry.addPatterns("get_runtime_model method is not supported on NPU", {
".*OVClassModelOptionalTestP.CompileModelCreateDefaultExecGraphResult.*",
});
+ // CACHE_MODE is not supported on NPU, update test with correct property to make weightless compiled model
+ _skipRegistry.addPatterns("compiled_blob test use `CACHE_MOD` which is not supported on NPU", {
+ R"(.*OVCompiledModelBaseTest.*import_from_.*_blob.*)",
+ R"(.*OVCompiledModelBaseTest.*compile_from_.*_blob.*)",
+ R"(.*OVCompiledModelBaseTest.*compile_from_cached_weightless_blob.*)",
+ R"(.*OVCompiledModelBaseTest.*use_blob_hint_.*)",
+ });
return _skipRegistry;
}();
// clang-format on
diff --git a/src/plugins/template/src/compiled_model.cpp b/src/plugins/template/src/compiled_model.cpp
index 89e532b9fa6..bd837529605 100644
--- a/src/plugins/template/src/compiled_model.cpp
+++ b/src/plugins/template/src/compiled_model.cpp
@@ -179,8 +179,10 @@ void ov::template_plugin::CompiledModel::export_model(std::ostream& model_stream
model_stream.write(reinterpret_cast<char*>(&dataSize), sizeof(dataSize));
model_stream.write(m_model.c_str(), dataSize);
- dataSize = static_cast<std::uint64_t>(m_constants.size());
- model_stream.write(reinterpret_cast<char*>(&dataSize), sizeof(dataSize));
- model_stream.write(reinterpret_cast<char*>(&m_constants[0]), dataSize);
+ if (m_cfg.cache_mode == CacheMode::OPTIMIZE_SPEED) {
+ dataSize = static_cast<std::uint64_t>(m_constants.size());
+ model_stream.write(reinterpret_cast<char*>(&dataSize), sizeof(dataSize));
+ model_stream.write(reinterpret_cast<char*>(&m_constants[0]), dataSize);
+ }
}
// ! [compiled_model:export_model]
diff --git a/src/plugins/template/src/config.cpp b/src/plugins/template/src/config.cpp
index 7ba4517edec..3b3a78e40c4 100644
--- a/src/plugins/template/src/config.cpp
+++ b/src/plugins/template/src/config.cpp
@@ -14,10 +14,7 @@ Configuration::Configuration() {}
Configuration::Configuration(const ov::AnyMap& config, const Configuration& defaultCfg, bool throwOnUnsupported) {
*this = defaultCfg;
- for (auto&& c : config) {
- const auto& key = c.first;
- const auto& value = c.second;
-
+ for (auto&& [key, value] : config) {
if (ov::template_plugin::disable_transformations == key) {
disable_transformations = value.as<bool>();
} else if (ov::internal::exclusive_async_requests == key) {
@@ -85,6 +82,13 @@ Configuration::Configuration(const ov::AnyMap& config, const Configuration& defa
model_priority = value.as<ov::hint::Priority>();
} else if (ov::cache_encryption_callbacks == key) {
encryption_callbacks = value.as<EncryptionCallbacks>();
+ } else if (ov::weights_path == key) {
+ weights_path = value.as<std::string>();
+ if (!weights_path.empty()) {
+ compiled_model_runtime_properties[ov::weights_path.name()] = weights_path.string();
+ }
+ } else if (ov::cache_mode == key) {
+ cache_mode = value.as<CacheMode>();
} else if (throwOnUnsupported) {
OPENVINO_THROW("Property was not found: ", key);
}
@@ -118,6 +122,12 @@ ov::Any Configuration::Get(const std::string& name) const {
return log_level;
} else if (name == ov::hint::model_priority) {
return model_priority;
+ } else if (name == ov::weights_path) {
+ return weights_path.string();
+ } else if (name == ov::internal::compiled_model_runtime_properties) {
+ return compiled_model_runtime_properties;
+ } else if (name == ov::cache_mode) {
+ return cache_mode;
} else {
OPENVINO_THROW("Property was not found: ", name);
}
diff --git a/src/plugins/template/src/config.hpp b/src/plugins/template/src/config.hpp
index ecdd56b1a71..c3cd8ff1913 100644
--- a/src/plugins/template/src/config.hpp
+++ b/src/plugins/template/src/config.hpp
@@ -4,6 +4,7 @@
#pragma once
+#include <filesystem>
#include <map>
#include <string>
@@ -32,7 +33,7 @@ struct Configuration {
int device_id = 0;
bool perf_count = false;
- ov::threading::IStreamsExecutor::Config streams_executor_config;
+ ov::threading::IStreamsExecutor::Config streams_executor_config{};
int streams = 1;
int threads = 0;
int threads_per_stream = 0;
@@ -48,7 +49,10 @@ struct Configuration {
ov::hint::Priority model_priority = ov::hint::Priority::DEFAULT;
- EncryptionCallbacks encryption_callbacks;
+ EncryptionCallbacks encryption_callbacks{};
+ std::filesystem::path weights_path{};
+ AnyMap compiled_model_runtime_properties{};
+ CacheMode cache_mode{CacheMode::OPTIMIZE_SPEED};
};
// ! [configuration:header]
diff --git a/src/plugins/template/src/plugin.cpp b/src/plugins/template/src/plugin.cpp
index 960d14c9470..d46339ef94c 100644
--- a/src/plugins/template/src/plugin.cpp
+++ b/src/plugins/template/src/plugin.cpp
@@ -4,6 +4,7 @@
#include "plugin.hpp"
+#include <fstream>
#include <memory>
#include "itt.hpp"
@@ -11,6 +12,9 @@
#include "openvino/pass/manager.hpp"
#include "openvino/runtime/internal_properties.hpp"
#include "openvino/runtime/properties.hpp"
+#include "openvino/runtime/shared_buffer.hpp"
+#include "openvino/util/common_util.hpp"
+#include "openvino/util/file_util.hpp"
#include "remote_context.hpp"
#include "template/properties.hpp"
#include "transformations/common_optimizations/common_optimizations.hpp"
@@ -25,6 +29,62 @@ namespace {
static constexpr const char* wait_executor_name = "TemplateWaitExecutor";
static constexpr const char* stream_executor_name = "TemplateStreamsExecutor";
static constexpr const char* template_exclusive_executor = "TemplateExecutor";
+
+uint64_t get_blob_data_size(std::istream& model) {
+ uint64_t size = 0;
+ model.read(reinterpret_cast<char*>(&size), sizeof(size));
+ return size;
+}
+
+std::string get_model_str(std::istream& model) {
+ const auto model_size = std::min<uint64_t>(model.rdbuf()->in_avail(), get_blob_data_size(model));
+ std::string xml;
+ xml.resize(model_size);
+ model.read(xml.data(), model_size);
+ return xml;
+}
+
+ov::Tensor read_weights(std::istream& model, const size_t weights_size) {
+ ov::Tensor weights(ov::element::from<uint8_t>(), ov::Shape{weights_size});
+ model.read(reinterpret_cast<char*>(weights.data()), weights_size);
+ return weights;
+}
+
+ov::Tensor get_model_weights(std::istream& model) {
+ const auto weights_size = std::min<uint64_t>(model.rdbuf()->in_avail(), get_blob_data_size(model));
+ return weights_size != 0 ? read_weights(model, weights_size) : ov::Tensor();
+}
+
+ov::Tensor get_model_weights(const ov::AnyMap& properties) {
+ ov::Tensor weights;
+ if (auto weights_path = properties.find(ov::weights_path.name()); weights_path != properties.end()) {
+ const auto w_path = std::filesystem::path(weights_path->second.as<std::string>());
+ if (auto size = ov::util::file_size(w_path); size > 0) {
+ auto f_weights = std::ifstream(w_path, std::ios::binary | std::ios::in);
+ weights = read_weights(f_weights, size);
+ }
+ }
+ return weights;
+}
+
+std::shared_ptr<ov::Model> get_ov_model_from_blob(const ov::template_plugin::Plugin& plugin,
+ ov::Tensor& weights,
+ size_t offset,
+ const ov::AnyMap& properties) {
+ if (auto blob_it = properties.find(ov::hint::compiled_blob.name()); blob_it != properties.end()) {
+ if (auto&& blob = blob_it->second.as<ov::Tensor>(); blob) {
+ ov::SharedStreamBuffer shared_buffer(reinterpret_cast<char*>(blob.data()), blob.get_byte_size());
+ std::istream blob_stream(&shared_buffer);
+ blob_stream.seekg(offset, std::ios::beg);
+ const auto model = get_model_str(blob_stream);
+ try {
+ return weights ? plugin.get_core()->read_model(model, weights) : nullptr;
+ } catch (...) {
+ }
+ }
+ }
+ return nullptr;
+};
} // namespace
// ! [plugin:ctor]
@@ -103,7 +163,15 @@ std::shared_ptr<ov::ICompiledModel> ov::template_plugin::Plugin::compile_model(
const ov::SoPtr<ov::IRemoteContext>& context) const {
OV_ITT_SCOPED_TASK(itt::domains::TemplatePlugin, "Plugin::compile_model");
- auto fullConfig = Configuration{properties, m_cfg};
+ Configuration fullConfig;
+ {
+ auto _properties = properties;
+ // remove not supported properties which are consumed by compile_model
+ _properties.erase(ov::loaded_from_cache.name());
+ _properties.erase(ov::hint::compiled_blob.name());
+ fullConfig = Configuration{_properties, m_cfg};
+ }
+
fullConfig.streams_executor_config = ov::threading::IStreamsExecutor::Config{stream_executor_name,
fullConfig.streams,
fullConfig.threads_per_stream};
@@ -112,15 +180,16 @@ std::shared_ptr<ov::ICompiledModel> ov::template_plugin::Plugin::compile_model(
fullConfig.streams = streamsExecutorConfig.get_streams();
fullConfig.threads = streamsExecutorConfig.get_threads();
fullConfig.threads_per_stream = streamsExecutorConfig.get_threads_per_stream();
- auto compiled_model = std::make_shared<CompiledModel>(
+
+ return std::make_shared<CompiledModel>(
model->clone(),
shared_from_this(),
context,
fullConfig.exclusive_async_requests
? get_executor_manager()->get_executor(template_exclusive_executor)
: get_executor_manager()->get_idle_cpu_streams_executor(streamsExecutorConfig),
- fullConfig);
- return compiled_model;
+ fullConfig,
+ false);
}
// ! [plugin:compile_model_with_remote]
@@ -146,27 +215,37 @@ std::shared_ptr<ov::ICompiledModel> ov::template_plugin::Plugin::import_model(
loaded_from_cache = it->second.as<bool>();
_properties.erase(it);
}
+ _properties.erase(ov::hint::compiled_blob.name());
auto fullConfig = Configuration{_properties, m_cfg};
fullConfig.streams_executor_config = ov::threading::IStreamsExecutor::Config{stream_executor_name,
fullConfig.streams,
fullConfig.threads_per_stream};
- // read XML content
- std::string xmlString;
- std::uint64_t dataSize = 0;
- model.read(reinterpret_cast<char*>(&dataSize), sizeof(dataSize));
- xmlString.resize(dataSize);
- model.read(const_cast<char*>(xmlString.c_str()), dataSize);
-
- // read blob content
- ov::Tensor weights;
- model.read(reinterpret_cast<char*>(&dataSize), sizeof(dataSize));
- if (0 != dataSize) {
- weights = ov::Tensor(ov::element::from<char>(), ov::Shape{static_cast<ov::Shape::size_type>(dataSize)});
- model.read(weights.data<char>(), dataSize);
+ auto weights = get_model_weights(properties);
+ if (!weights) {
+ if (auto model_hint = properties.find(ov::hint::model.name()); model_hint != properties.end()) {
+ if (auto m = model_hint->second.as<std::shared_ptr<ov::Model>>()) {
+ if (m->has_rt_info("__weights_path")) {
+ AnyMap rt_info;
+ auto p = m->get_rt_info<std::string>("__weights_path");
+ rt_info[ov::weights_path.name()] = m->get_rt_info<ov::Any>("__weights_path");
+ weights = get_model_weights(rt_info);
+ }
+ }
+ }
}
+ auto ov_model = get_ov_model_from_blob(*this, weights, model.tellg(), properties);
+ if (!ov_model) {
+ // read XML content
+ std::string xmlString = get_model_str(model);
- auto ov_model = get_core()->read_model(xmlString, weights);
+ // read blob content
+ if (!weights) {
+ weights = get_model_weights(model);
+ }
+
+ ov_model = get_core()->read_model(xmlString, weights);
+ }
auto streamsExecutorConfig =
ov::threading::IStreamsExecutor::Config::make_default_multi_threaded(fullConfig.streams_executor_config);
fullConfig.streams = streamsExecutorConfig.get_streams();
@@ -268,6 +347,8 @@ ov::Any ov::template_plugin::Plugin::get_property(const std::string& name, const
ov::template_plugin::disable_transformations,
ov::log::level,
ov::hint::model_priority,
+ ov::weights_path,
+ ov::cache_mode,
};
return rw_properties;
};
@@ -285,7 +366,8 @@ ov::Any ov::template_plugin::Plugin::get_property(const std::string& name, const
ov::PropertyName{ov::internal::caching_properties.name(), ov::PropertyMutability::RO},
ov::PropertyName{ov::internal::exclusive_async_requests.name(), ov::PropertyMutability::RW},
ov::PropertyName{ov::inference_num_threads.name(), ov::PropertyMutability::RW},
- ov::PropertyName{ov::internal::threads_per_stream.name(), ov::PropertyMutability::RW}};
+ ov::PropertyName{ov::internal::threads_per_stream.name(), ov::PropertyMutability::RW},
+ ov::PropertyName{ov::internal::compiled_model_runtime_properties.name(), ov::PropertyMutability::RO}};
} else if (ov::available_devices == name) {
// TODO: fill list of available devices
return decltype(ov::available_devices)::value_type{{""}};
diff --git a/src/plugins/template/tests/functional/shared_tests_instances/behavior/ov_executable_network/exec_network_base.cpp b/src/plugins/template/tests/functional/shared_tests_instances/behavior/ov_executable_network/exec_network_base.cpp
index b34f825e701..c93b31f4faf 100644
--- a/src/plugins/template/tests/functional/shared_tests_instances/behavior/ov_executable_network/exec_network_base.cpp
+++ b/src/plugins/template/tests/functional/shared_tests_instances/behavior/ov_executable_network/exec_network_base.cpp
@@ -3,9 +3,10 @@
//
#include "behavior/compiled_model/compiled_model_base.hpp"
+#include "common_test_utils/ov_tensor_utils.hpp"
+#include "openvino/core/model_util.hpp"
-using namespace ov::test::behavior;
-namespace {
+namespace ov::test::behavior {
const std::vector<ov::AnyMap> configs = {
{},
@@ -24,4 +25,4 @@ INSTANTIATE_TEST_SUITE_P(smoke_BehaviorTests,
::testing::ValuesIn(configs)),
OVCompiledModelBaseTestOptional::getTestCaseName);
-} // namespace
+} // namespace ov::test::behavior
diff --git a/src/tests/functional/plugin/conformance/test_runner/conformance_infra/src/main.cpp b/src/tests/functional/plugin/conformance/test_runner/conformance_infra/src/main.cpp
index 90a6ebdc684..62dc0764d0d 100644
--- a/src/tests/functional/plugin/conformance/test_runner/conformance_infra/src/main.cpp
+++ b/src/tests/functional/plugin/conformance/test_runner/conformance_infra/src/main.cpp
@@ -78,7 +78,7 @@ int main(int argc, char* argv[]) {
}
ov::test::utils::is_print_rel_influence_coef = true;
- ov::test::utils::disable_tests_skipping = true;
+ ov::test::utils::disable_tests_skipping = false;
ov::test::utils::OpSummary::setExtendReport(FLAGS_extend_report);
ov::test::utils::OpSummary::setExtractBody(FLAGS_extract_body);
ov::test::utils::OpSummary::setSaveReportWithUniqueName(FLAGS_report_unique_name);
diff --git a/src/tests/functional/plugin/conformance/test_runner/conformance_infra/src/skip_tests_config.cpp b/src/tests/functional/plugin/conformance/test_runner/conformance_infra/src/skip_tests_config.cpp
index 37328e0198d..b9ca6e405fa 100644
--- a/src/tests/functional/plugin/conformance/test_runner/conformance_infra/src/skip_tests_config.cpp
+++ b/src/tests/functional/plugin/conformance/test_runner/conformance_infra/src/skip_tests_config.cpp
@@ -18,7 +18,12 @@ const char *targetPluginName = "";
const char *refCachePath = "";
std::vector<std::string> IRFolderPaths = {};
-std::vector<std::string> disabledTests = {};
+std::vector<std::string> disabledTests = {
+ R"(.*OVCompiledModelBaseTest.*import_from_.*_blob.*targetDevice=(MULTI|AUTO|CPU).*)",
+ R"(.*OVCompiledModelBaseTest.*compile_from_.*_blob.*targetDevice=(MULTI|AUTO|CPU).*)",
+ R"(.*OVCompiledModelBaseTest.*compile_from_cached_weightless_blob.*targetDevice=(MULTI|AUTO|CPU).*)",
+ R"(.*OVCompiledModelBaseTest.*use_blob_hint_.*targetDevice=CPU.*)",
+};
ShapeMode shapeMode = ov::test::conformance::ShapeMode::BOTH;
diff --git a/src/tests/functional/plugin/shared/include/behavior/compiled_model/compiled_model_base.hpp b/src/tests/functional/plugin/shared/include/behavior/compiled_model/compiled_model_base.hpp
index ffbec42fb1f..ee443d5603f 100644
--- a/src/tests/functional/plugin/shared/include/behavior/compiled_model/compiled_model_base.hpp
+++ b/src/tests/functional/plugin/shared/include/behavior/compiled_model/compiled_model_base.hpp
@@ -9,6 +9,7 @@
#include "base/ov_behavior_test_utils.hpp"
#include "common_test_utils/file_utils.hpp"
+#include "common_test_utils/ov_tensor_utils.hpp"
#include "common_test_utils/ov_test_utils.hpp"
#include "common_test_utils/subgraph_builders/concat_with_params.hpp"
#include "common_test_utils/subgraph_builders/conv_pool_relu.hpp"
@@ -16,13 +17,58 @@
#include "common_test_utils/subgraph_builders/single_concat_with_constant.hpp"
#include "common_test_utils/subgraph_builders/single_split.hpp"
#include "common_test_utils/subgraph_builders/split_concat.hpp"
+#include "openvino/core/model_util.hpp"
#include "openvino/op/concat.hpp"
#include "openvino/runtime/exec_model_info.hpp"
#include "openvino/runtime/tensor.hpp"
+#include "openvino/util/file_util.hpp"
-namespace ov {
-namespace test {
-namespace behavior {
+namespace ov::test::behavior {
+namespace {
+using op::v0::Parameter, op::v0::Constant, op::v1::Add;
+
+Tensor from_stream(std::istream& stream, const size_t size) {
+ ov::Tensor t(element::from<char>(), Shape{size});
+ stream.read(t.data<char>(), size);
+ return t;
+}
+
+std::shared_ptr<Model> make_model_with_weights(const ov::Tensor& w) {
+ constexpr auto precision = element::f32;
+
+ auto weights = std::make_shared<Constant>(w);
+ auto input = std::make_shared<Parameter>(precision, Shape{1});
+ auto add = std::make_shared<Add>(input, weights);
+
+ weights->set_friendly_name("weights");
+ input->set_friendly_name("input");
+ add->set_friendly_name("add");
+
+ auto model = std::make_shared<Model>(OutputVector{add}, ParameterVector{input}, "Simple with weights");
+ util::set_tensors_names(AUTO, *model, {}, {{0, {"add"}}});
+ return model;
+}
+
+std::shared_ptr<Model> make_model_with_weights() {
+ constexpr auto precision = element::f32;
+
+ auto weights = std::make_shared<Constant>(element::f32, Shape{5}, std::vector<float>{1.0f});
+ auto input = std::make_shared<Parameter>(precision, Shape{1});
+ auto add = std::make_shared<Add>(input, weights);
+
+ weights->set_friendly_name("weights");
+ input->set_friendly_name("input");
+ add->set_friendly_name("add");
+
+ auto model = std::make_shared<Model>(OutputVector{add}, ParameterVector{input}, "Simple with weights");
+ util::set_tensors_names(AUTO, *model, {}, {{0, {"add"}}});
+ return model;
+}
+} // namespace
+
+using testing::_;
+
+namespace utils = ::ov::test::utils;
class OVCompiledModelBaseTest : public testing::WithParamInterface<InferRequestParams>,
public OVCompiledNetworkTestBase {
@@ -715,6 +761,417 @@ TEST_P(CompiledModelSetType, canSetInputOutputTypeAndCompileModel) {
model = ppp.build();
OV_ASSERT_NO_THROW(core.compile_model(model, target_device, configuration));
}
-} // namespace behavior
-} // namespace test
-} // namespace ov
+
+TEST_P(OVCompiledModelBaseTest, import_from_weightless_blob) {
+ const auto w_file_path =
+ ov::util::path_join({utils::getCurrentWorkingDir(), utils::generateTestFilePrefix() + "_weights.bin"});
+
+ std::stringstream export_stream;
+ {
+ configuration.emplace(ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE));
+ auto model = make_model_with_weights();
+ auto compiled_model = core->compile_model(model, target_device, configuration);
+ ASSERT_TRUE(compiled_model);
+ compiled_model.export_model(export_stream);
+ if (target_device != utils::DEVICE_HETERO) {
+ EXPECT_EQ(ov::CacheMode::OPTIMIZE_SIZE, compiled_model.get_property(ov::cache_mode));
+ }
+ }
+ OV_EXPECT_THROW(core->import_model(export_stream, target_device), ov::Exception, _);
+
+ utils::removeFile(w_file_path.string());
+}
+
+TEST_P(OVCompiledModelBaseTest, compile_from_regular_blob) {
+ auto compiled_model_ref = core->compile_model(make_model_with_weights(), target_device, configuration);
+ ASSERT_TRUE(compiled_model_ref);
+ if (target_device != utils::DEVICE_HETERO) {
+ EXPECT_EQ(ov::CacheMode::OPTIMIZE_SPEED, compiled_model_ref.get_property(ov::cache_mode));
+ }
+ Tensor exported_model;
+ {
+ std::stringstream export_stream;
+ compiled_model_ref.export_model(export_stream);
+ exported_model = from_stream(export_stream, export_stream.str().size());
+ }
+
+ ov::Tensor expected, output;
+ auto input = utils::create_tensor(element::f32, Shape{1}, std::vector<float>{2.0f});
+ // Infer reference model
+ {
+ auto infer_request = compiled_model_ref.create_infer_request();
+ infer_request.set_tensor("input", input);
+ infer_request.infer();
+ expected = infer_request.get_tensor("add");
+ }
+ // Infer compiled from stream
+ {
+ configuration.emplace(ov::hint::compiled_blob(exported_model));
+ auto empty_model = std::make_shared<Model>(OutputVector{}, ParameterVector{}, "Empty model");
+ auto model_from_stream = core->compile_model(empty_model, target_device, configuration);
+ auto infer_request_import = model_from_stream.create_infer_request();
+ infer_request_import.set_tensor("input", input);
+ infer_request_import.infer();
+ output = infer_request_import.get_tensor("add");
+ }
+
+ OV_ASSERT_NO_THROW(utils::compare(expected, output));
+}
+
+TEST_P(OVCompiledModelBaseTest, compile_from_weightless_blob) {
+ auto weights = utils::create_tensor(element::f32, Shape{5}, std::vector<float>{1.0f, 1.0f, 1.0f, 1.0f, 1.0f});
+ auto w_file_path =
+ ov::util::path_join({utils::getCurrentWorkingDir(), utils::generateTestFilePrefix() + "_weights.bin"});
+
+ Tensor exported_model;
+ configuration.emplace(ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE));
+ {
+ auto model = make_model_with_weights(weights);
+ std::stringstream export_stream;
+ auto export_cfg = configuration;
+ if (target_device == utils::DEVICE_GPU) {
+ export_cfg.emplace(ov::hint::model(model));
+ export_cfg.emplace(ov::weights_path(w_file_path.string()));
+ }
+ auto compiled_model_ref = core->compile_model(model, target_device, export_cfg);
+ ASSERT_TRUE(compiled_model_ref);
+ compiled_model_ref.export_model(export_stream);
+ exported_model = from_stream(export_stream, export_stream.str().size());
+ }
+
+ auto expected = utils::create_tensor(element::f32, Shape{5}, std::vector<float>{3.0f, 5.0f, 3.0f, 3.0f, 3.0f});
+ {
+ // store weights in file, not same as in original model model
+ auto w = utils::create_tensor(element::f32, Shape{5}, std::vector<float>{1.0f, 3.0f, 1.0f, 1.0f, 1.0f});
+ auto w_file = std::ofstream(w_file_path, std::ios::binary);
+ w_file.write(reinterpret_cast<const char*>(w.data()), w.get_byte_size());
+ }
+
+ ov::Tensor output;
+ auto input = utils::create_tensor(element::f32, Shape{1}, std::vector<float>{2.0f});
+ // Infer compiled from weightless stream
+ {
+ configuration.emplace(ov::weights_path(w_file_path.string()));
+ configuration.emplace(ov::hint::compiled_blob(exported_model));
+ auto empty_model = std::make_shared<Model>(OutputVector{}, ParameterVector{}, "Empty model");
+ auto import_model = core->compile_model(empty_model, target_device, configuration);
+ auto infer_request_import = import_model.create_infer_request();
+ infer_request_import.set_tensor("input", input);
+ infer_request_import.infer();
+ output = infer_request_import.get_tensor("add");
+ }
+
+ utils::removeFile(w_file_path.string());
+ OV_ASSERT_NO_THROW(utils::compare(expected, output));
+}
+
+TEST_P(OVCompiledModelBaseTest, compile_from_weightless_blob_but_no_weights) {
+ Tensor exported_model;
+ auto w_file_path =
+ ov::util::path_join({utils::getCurrentWorkingDir(), utils::generateTestFilePrefix() + "_weights.bin"});
+
+ auto model = make_model_with_weights();
+ {
+ auto export_cfg = configuration;
+ export_cfg.emplace(ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE));
+ if (target_device == utils::DEVICE_GPU) {
+ export_cfg.emplace(ov::hint::model(model));
+ export_cfg.emplace(ov::weights_path(w_file_path.string()));
+ }
+ auto compiled_model_ref = core->compile_model(model, target_device, export_cfg);
+ std::stringstream export_stream;
+ ASSERT_TRUE(compiled_model_ref);
+ compiled_model_ref.export_model(export_stream);
+ exported_model = from_stream(export_stream, export_stream.str().size());
+ if (target_device != utils::DEVICE_HETERO) {
+ EXPECT_EQ(ov::CacheMode::OPTIMIZE_SIZE, compiled_model_ref.get_property(ov::cache_mode));
+ }
+ }
+
+ auto expected = utils::create_tensor(element::f32, Shape{5}, std::vector<float>{3.0f, 3.0f, 3.0f, 3.0f, 3.0f});
+ ov::Tensor output;
+ auto input = utils::create_tensor(element::f32, Shape{1}, std::vector<float>{2.0f});
+ // Infer compiled from weightless stream and no weights use original model
+ {
+ configuration.emplace(ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE));
+ configuration.emplace(ov::hint::compiled_blob(exported_model));
+ configuration.emplace(ov::weights_path(w_file_path.string()));
+ auto import_model = core->compile_model(model, target_device, configuration);
+ auto infer_request_import = import_model.create_infer_request();
+ infer_request_import.set_tensor("input", input);
+ infer_request_import.infer();
+ output = infer_request_import.get_tensor("add");
+ }
+
+ utils::removeFile(w_file_path.string());
+ OV_ASSERT_NO_THROW(utils::compare(expected, output));
+}
+
+TEST_P(OVCompiledModelBaseTest, compile_from_cached_weightless_blob_use_weight_hint) {
+ auto cache_dir = ov::util::path_join({utils::getCurrentWorkingDir(), utils::generateTestFilePrefix() + "cache"});
+ auto w_file_path = ov::util::path_join({cache_dir, "weights.bin"});
+ {
+ // store weights in file, not same as in original model model
+ std::filesystem::create_directories(cache_dir);
+ auto w = utils::create_tensor(element::f32, Shape{5}, std::vector<float>{1.0f, 3.0f, 1.0f, 1.0f, 1.0f});
+ auto w_file = std::ofstream(w_file_path, std::ios::binary);
+ w_file.write(reinterpret_cast<const char*>(w.data()), w.get_byte_size());
+ }
+
+ configuration.emplace(ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE));
+ configuration.emplace(ov::weights_path(w_file_path.string()));
+ configuration.emplace(ov::cache_dir(cache_dir.string()));
+ auto model = make_model_with_weights();
+
+ {
+ auto compiled_model = core->compile_model(model, target_device, configuration);
+ ASSERT_TRUE(compiled_model);
+ EXPECT_FALSE(compiled_model.get_property(ov::loaded_from_cache));
+ }
+ {
+ auto compiled_model = core->compile_model(model, target_device, configuration);
+ ASSERT_TRUE(compiled_model);
+ EXPECT_TRUE(compiled_model.get_property(ov::loaded_from_cache));
+ }
+
+ std::error_code ec;
+ std::filesystem::remove_all(cache_dir, ec);
+}
+
+TEST_P(OVCompiledModelBaseTest, compile_from_cached_weightless_blob_no_hint) {
+ auto cache_dir = ov::util::path_join({utils::getCurrentWorkingDir(), utils::generateTestFilePrefix() + "cache"});
+ auto w_file_path = ov::util::path_join({cache_dir, "weights.bin"});
+ {
+ // store weights in file, not same as in original model model
+ std::filesystem::create_directories(cache_dir);
+ auto w = utils::create_tensor(element::f32, Shape{5}, std::vector<float>{1.0f, 3.0f, 1.0f, 1.0f, 1.0f});
+ auto w_file = std::ofstream(w_file_path, std::ios::binary);
+ w_file.write(reinterpret_cast<const char*>(w.data()), w.get_byte_size());
+ }
+
+ configuration.emplace(ov::cache_dir(cache_dir.string()));
+ auto model = make_model_with_weights();
+
+ {
+ auto export_cfg = configuration;
+ export_cfg.emplace(ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE));
+ if (target_device == utils::DEVICE_GPU) {
+ export_cfg.emplace(ov::hint::model(model));
+ export_cfg.emplace(ov::weights_path(w_file_path.string()));
+ }
+ auto compiled_model = core->compile_model(model, target_device, export_cfg);
+ ASSERT_TRUE(compiled_model);
+ EXPECT_FALSE(compiled_model.get_property(ov::loaded_from_cache));
+ }
+ {
+ configuration.emplace(ov::weights_path(w_file_path.string()));
+ if (target_device == utils::DEVICE_GPU) {
+ configuration.emplace(ov::hint::model(model));
+ configuration.emplace(ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE));
+ }
+ auto compiled_model = core->compile_model(model, target_device, configuration);
+ ASSERT_TRUE(compiled_model);
+ EXPECT_TRUE(compiled_model.get_property(ov::loaded_from_cache));
+ }
+
+ std::error_code ec;
+ std::filesystem::remove_all(cache_dir, ec);
+}
+
+TEST_P(OVCompiledModelBaseTest, use_blob_hint_has_priority_over_cache) {
+ auto cache_dir = ov::util::path_join({utils::getCurrentWorkingDir(), utils::generateTestFilePrefix() + "cache"});
+ auto w_file_path = ov::util::path_join({cache_dir, "weights.bin"});
+ auto blob_file_path = cache_dir / "export.blob";
+
+ {
+ // store weights in file, not same as in original model model
+ std::filesystem::create_directories(cache_dir);
+ auto w = utils::create_tensor(element::f32, Shape{5}, std::vector<float>{1.0f, 3.0f, 1.0f, 1.0f, 1.0f});
+ auto w_file = std::ofstream(w_file_path, std::ios::binary);
+ w_file.write(reinterpret_cast<const char*>(w.data()), w.get_byte_size());
+ }
+
+ configuration.emplace(ov::cache_dir(cache_dir.string()));
+ auto model = make_model_with_weights();
+
+ {
+ auto export_cfg = configuration;
+ export_cfg.emplace(ov::weights_path(w_file_path.string()));
+ export_cfg.emplace(ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE));
+ if (target_device == utils::DEVICE_GPU) {
+ export_cfg.emplace(ov::hint::model(model));
+ export_cfg.emplace(ov::weights_path(w_file_path.string()));
+ }
+ auto compiled_model = core->compile_model(model, target_device, export_cfg);
+ ASSERT_TRUE(compiled_model);
+ EXPECT_FALSE(compiled_model.get_property(ov::loaded_from_cache));
+ std::ofstream blob_file(blob_file_path, std::ios::binary);
+ compiled_model.export_model(blob_file);
+ }
+ {
+ configuration.emplace(ov::hint::compiled_blob(ov::read_tensor_data(blob_file_path)));
+ if (target_device == utils::DEVICE_GPU) {
+ configuration.emplace(ov::hint::model(model));
+ configuration.emplace(ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE));
+ }
+ auto compiled_model = core->compile_model(model, target_device, configuration);
+ ASSERT_TRUE(compiled_model);
+ EXPECT_FALSE(compiled_model.get_property(ov::loaded_from_cache));
+ }
+
+ std::error_code ec;
+ std::filesystem::remove_all(cache_dir, ec);
+}
+
+TEST_P(OVCompiledModelBaseTest, use_blob_hint_has_priority_over_cache_but_weights_bind_from_model_hint) {
+ auto cache_dir = ov::util::path_join({utils::getCurrentWorkingDir(), utils::generateTestFilePrefix() + "cache"});
+ auto w_file_path = ov::util::path_join({cache_dir, "weights.bin"});
+ auto blob_file_path = cache_dir / "export.blob";
+ std::filesystem::create_directories(cache_dir);
+
+ {
+ // store weights in file, not same as in original model model
+ auto w = utils::create_tensor(element::f32, Shape{5}, std::vector<float>{1.0f, 3.0f, 1.0f, 1.0f, 1.0f});
+ auto w_file = std::ofstream(w_file_path, std::ios::binary);
+ w_file.write(reinterpret_cast<const char*>(w.data()), w.get_byte_size());
+ }
+
+ configuration.emplace(ov::cache_dir(cache_dir.string()));
+ auto model = make_model_with_weights();
+ {
+ auto export_cfg = configuration;
+ export_cfg.emplace(ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE));
+ if (target_device == utils::DEVICE_GPU) {
+ export_cfg.emplace(ov::hint::model(model));
+ export_cfg.emplace(ov::weights_path(w_file_path.string()));
+ }
+ auto compiled_model = core->compile_model(model, target_device, export_cfg);
+ ASSERT_TRUE(compiled_model);
+ EXPECT_FALSE(compiled_model.get_property(ov::loaded_from_cache));
+ auto blob_file = std::ofstream(blob_file_path, std::ios::binary);
+ compiled_model.export_model(blob_file);
+ }
+ {
+ model->get_rt_info()["__weights_path"] = w_file_path.string();
+ configuration.emplace(ov::hint::compiled_blob(ov::read_tensor_data(blob_file_path)));
+ if (target_device == utils::DEVICE_GPU) {
+ configuration.emplace(ov::hint::model(model));
+ configuration.emplace(ov::weights_path(w_file_path.string()));
+ configuration.emplace(ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE));
+ }
+ auto compiled_model = core->compile_model(model, target_device, configuration);
+ ASSERT_TRUE(compiled_model);
+ EXPECT_FALSE(compiled_model.get_property(ov::loaded_from_cache));
+ }
+
+ std::error_code ec;
+ std::filesystem::remove_all(cache_dir, ec);
+}
+
+TEST_P(OVCompiledModelBaseTest, use_blob_hint_has_priority_over_cache_but_weights_from_model_path) {
+ auto cache_dir = ov::util::path_join({utils::getCurrentWorkingDir(), utils::generateTestFilePrefix() + "cache"});
+ auto model_file_path = cache_dir / "model.xml";
+ auto w_file_path = model_file_path;
+ w_file_path.replace_extension(".bin");
+ auto blob_file_path = cache_dir / "export.blob";
+ std::filesystem::create_directories(cache_dir);
+
+ auto model = make_model_with_weights();
+ ov::save_model(model, model_file_path, false);
+
+ configuration.emplace(ov::cache_dir(cache_dir.string()));
+ {
+ auto export_cfg = configuration;
+ export_cfg.emplace(ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE));
+ if (target_device == utils::DEVICE_GPU) {
+ export_cfg.emplace(ov::hint::model(model));
+ export_cfg.emplace(ov::weights_path(w_file_path.string()));
+ }
+ auto compiled_model = core->compile_model(model, target_device, export_cfg);
+ ASSERT_TRUE(compiled_model);
+ EXPECT_FALSE(compiled_model.get_property(ov::loaded_from_cache));
+ auto blob_file = std::ofstream(blob_file_path, std::ios::binary);
+ compiled_model.export_model(blob_file);
+ }
+ {
+ configuration.emplace(ov::hint::compiled_blob(ov::read_tensor_data(blob_file_path)));
+ if (target_device == utils::DEVICE_GPU) {
+ configuration.emplace(ov::hint::model(model));
+ configuration.emplace(ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE));
+ }
+ auto compiled_model = core->compile_model(model_file_path, target_device, configuration);
+ ASSERT_TRUE(compiled_model);
+ EXPECT_FALSE(compiled_model.get_property(ov::loaded_from_cache));
+ }
+
+ std::error_code ec;
+ std::filesystem::remove_all(cache_dir, ec);
+}
+
+TEST_P(OVCompiledModelBaseTest, use_blob_hint_which_fails_load_from_cache) {
+ auto cache_dir = ov::util::path_join({utils::getCurrentWorkingDir(), utils::generateTestFilePrefix() + "cache"});
+ auto w_file_path = ov::util::path_join({cache_dir, "weights.bin"});
+
+ {
+ // store weights in file, not same as in original model model
+ std::filesystem::create_directories(cache_dir);
+ auto w = utils::create_tensor(element::f32, Shape{5}, std::vector<float>{1.0f, 3.0f, 1.0f, 1.0f, 1.0f});
+ auto w_file = std::ofstream(w_file_path, std::ios::binary);
+ w_file.write(reinterpret_cast<const char*>(w.data()), w.get_byte_size());
+ }
+
+ configuration.emplace(ov::cache_dir(cache_dir.string()));
+ auto model = make_model_with_weights();
+
+ {
+ auto export_cfg = configuration;
+ export_cfg.emplace(ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE));
+ if (target_device == utils::DEVICE_GPU) {
+ export_cfg.emplace(ov::hint::model(model));
+ export_cfg.emplace(ov::weights_path(w_file_path.string()));
+ }
+ auto compiled_model = core->compile_model(model, target_device, export_cfg);
+ ASSERT_TRUE(compiled_model);
+ EXPECT_FALSE(compiled_model.get_property(ov::loaded_from_cache));
+ }
+ {
+ configuration.emplace(ov::weights_path(w_file_path.string()));
+ configuration.emplace(ov::hint::compiled_blob(Tensor{}));
+ if (target_device == utils::DEVICE_GPU) {
+ configuration.emplace(ov::hint::model(model));
+ configuration.emplace(ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE));
+ }
+ auto compiled_model = core->compile_model(model, target_device, configuration);
+ ASSERT_TRUE(compiled_model);
+ EXPECT_TRUE(compiled_model.get_property(ov::loaded_from_cache));
+ }
+
+ std::error_code ec;
+ std::filesystem::remove_all(cache_dir, ec);
+}
+
+TEST_P(OVCompiledModelBaseTest, compile_from_cached_weightless_blob_but_no_weights) {
+ auto cache_dir = ov::util::Path(utils::getCurrentWorkingDir()) / (utils::generateTestFilePrefix() + "cache");
+ auto w_file_path = cache_dir / "weights.bin";
+ std::filesystem::create_directories(cache_dir);
+
+ configuration.emplace(ov::cache_mode(ov::CacheMode::OPTIMIZE_SIZE));
+ configuration.emplace(ov::cache_dir(cache_dir.string()));
+ auto model = make_model_with_weights();
+
+ {
+ auto compiled_model = core->compile_model(model, target_device, configuration);
+ ASSERT_TRUE(compiled_model);
+ EXPECT_FALSE(compiled_model.get_property(ov::loaded_from_cache));
+ }
+ {
+ // model not loaded from cache as no weights on path
+ auto compiled_model = core->compile_model(model, target_device, configuration);
+ ASSERT_TRUE(compiled_model);
+ EXPECT_FALSE(compiled_model.get_property(ov::loaded_from_cache));
+ }
+
+ std::error_code ec;
+ std::filesystem::remove_all(cache_dir, ec);
+}
+} // namespace ov::test::behavior