5
5
#include " core_impl.hpp"
6
6
7
7
#include < memory>
8
+ #include < variant>
8
9
9
10
#include " check_network_batchable.hpp"
10
11
#include " itt.hpp"
29
30
#include " openvino/util/common_util.hpp"
30
31
#include " openvino/util/file_util.hpp"
31
32
#include " openvino/util/shared_object.hpp"
33
+ #include " openvino/util/variant_visitor.hpp"
32
34
#include " openvino/util/xml_parse_utils.hpp"
33
35
#include " ov_plugins.hpp"
34
36
#ifdef PROXY_PLUGIN_ENABLED
@@ -207,6 +209,64 @@ static const auto core_properties_names =
207
209
208
210
static const auto auto_batch_properties_names =
209
211
ov::util::make_array (ov::auto_batch_timeout.name(), ov::hint::allow_auto_batching.name());
212
+
213
+ ov::util::Path extract_weight_path (const std::string& compiled_properties) {
214
+ if (auto start = compiled_properties.find (ov::weights_path.name ()); start != std::string::npos) {
215
+ start += std::string_view{ov::weights_path.name ()}.size () + 1 ;
216
+ auto length = compiled_properties.find (" ," , start);
217
+ if (length != std::string::npos) {
218
+ length -= start;
219
+ }
220
+ return {compiled_properties.substr (start, length)};
221
+ } else {
222
+ return {};
223
+ }
224
+ }
225
+
226
+ using model_hint_t = std::variant<std::shared_ptr<const ov::Model>, std::string>;
227
+
228
+ ov::SoPtr<ov::ICompiledModel> import_compiled_model (const ov::Plugin& plugin,
229
+ const ov::SoPtr<ov::IRemoteContext>& context,
230
+ const ov::AnyMap& config) {
231
+ ov::SoPtr<ov::ICompiledModel> compiled_model;
232
+ if (auto blob_hint = config.find (ov::hint::compiled_blob.name ()); blob_hint != config.end ()) {
233
+ try {
234
+ auto compiled_blob = blob_hint->second .as <ov::Tensor>();
235
+ ov::SharedStreamBuffer buffer{reinterpret_cast <char *>(compiled_blob.data ()), compiled_blob.get_byte_size ()};
236
+ std::istream stream{&buffer};
237
+ compiled_model =
238
+ context ? plugin.import_model (stream, context, config) : plugin.import_model (stream, config);
239
+ } catch (...) {
240
+ }
241
+ }
242
+ return compiled_model;
243
+ }
244
+
245
+ ov::SoPtr<ov::ICompiledModel> import_compiled_model (const ov::Plugin& plugin,
246
+ const ov::SoPtr<ov::IRemoteContext>& context,
247
+ const ov::AnyMap& config,
248
+ const model_hint_t & model_hint) {
249
+ auto cfg = config;
250
+ const auto apply_model_hint = ov::util::VariantVisitor{
251
+ [&cfg, &plugin](const std::shared_ptr<const ov::Model>& model_ptr) {
252
+ if (model_ptr != nullptr &&
253
+ ov::util::contains (plugin.get_property (ov::supported_properties), ov::hint::model)) {
254
+ cfg[ov::hint::model.name ()] = std::const_pointer_cast<ov::Model>(model_ptr);
255
+ }
256
+ },
257
+ [&cfg, &plugin](const std::string& model_path) {
258
+ if (cfg.count (ov::weights_path.name ()) == 0 &&
259
+ ov::util::contains (plugin.get_property (ov::supported_properties), ov::weights_path)) {
260
+ ov::util::Path weights_path{model_path};
261
+ weights_path.replace_extension (" .bin" );
262
+ if (ov::util::file_exists (weights_path)) {
263
+ cfg[ov::weights_path.name ()] = weights_path.string ();
264
+ }
265
+ }
266
+ }};
267
+ std::visit (apply_model_hint, model_hint);
268
+ return import_compiled_model (plugin, context, cfg);
269
+ }
210
270
} // namespace
211
271
212
272
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<
770
830
771
831
auto parsed = parseDeviceNameIntoConfig (deviceName, coreConfig, config_with_batch, is_proxy_device (deviceName));
772
832
auto plugin = get_plugin (parsed._deviceName );
773
- ov::SoPtr<ov::ICompiledModel> res;
774
833
// will consume ov::cache_dir if plugin not support it
775
834
auto cacheManager = parsed._core_config .get_cache_config_for_device (plugin, parsed._config )._cacheManager ;
835
+ auto res = import_compiled_model (plugin, {}, parsed._config , model);
776
836
// Skip caching for proxy plugin. HW plugin will load network from the cache
777
- if (cacheManager && device_supports_model_caching (plugin, parsed._config ) && !is_proxy_device (plugin)) {
837
+ if (res) {
838
+ // hint::compiled_blob is set and imported skip compilation
839
+ } else if (cacheManager && device_supports_model_caching (plugin, parsed._config ) && !is_proxy_device (plugin)) {
778
840
CacheContent cacheContent{cacheManager, parsed._core_config .get_enable_mmap ()};
779
841
cacheContent.blobId = ov::ModelCache::compute_hash (model, create_compile_config (plugin, parsed._config ));
780
842
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<
805
867
806
868
auto parsed = parseDeviceNameIntoConfig (deviceName, coreConfig, config_with_batch, is_proxy_device (deviceName));
807
869
auto plugin = get_plugin (parsed._deviceName );
808
- ov::SoPtr<ov::ICompiledModel> res;
809
870
// will consume ov::cache_dir if plugin not support it
810
871
auto cacheManager = parsed._core_config .get_cache_config_for_device (plugin, parsed._config )._cacheManager ;
872
+ auto res = import_compiled_model (plugin, context, parsed._config , model);
811
873
// Skip caching for proxy plugin. HW plugin will load network from the cache
812
- if (cacheManager && device_supports_model_caching (plugin, parsed._config ) && !is_proxy_device (plugin)) {
874
+ if (res) {
875
+ // hint::compiled_blob is set and imported skip compilation
876
+ } else if (cacheManager && device_supports_model_caching (plugin, parsed._config ) && !is_proxy_device (plugin)) {
813
877
CacheContent cacheContent{cacheManager, parsed._core_config .get_enable_mmap ()};
814
878
cacheContent.blobId = ov::ModelCache::compute_hash (model, create_compile_config (plugin, parsed._config ));
815
879
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
830
894
auto parsed = parse_device_config (device_name, coreConfig, config, false );
831
895
// in case of compile_model(file_name), we need to clear-up core-level properties
832
896
auto plugin = get_plugin (parsed._deviceName );
833
- ov::SoPtr<ov::ICompiledModel> compiled_model;
834
897
// will consume ov::cache_dir if plugin not support it
835
898
auto cacheManager = parsed._core_config .get_cache_config_for_device (plugin, parsed._config )._cacheManager ;
899
+ auto compiled_model = import_compiled_model (plugin, {}, parsed._config , model_path);
836
900
837
- if (cacheManager && device_supports_model_caching (plugin, parsed._config ) && !is_proxy_device (plugin)) {
901
+ if (compiled_model) {
902
+ // hint::compiled_blob is set and imported skip compilation
903
+ } else if (cacheManager && device_supports_model_caching (plugin, parsed._config ) && !is_proxy_device (plugin)) {
838
904
// Skip caching for proxy plugin. HW plugin will load network from the cache
839
905
CoreConfig::remove_core_skip_cache_dir (parsed._config );
840
906
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
858
924
OV_ITT_SCOPED_TASK (ov::itt::domains::OV, " Core::compile_model::from_memory" );
859
925
auto parsed = parseDeviceNameIntoConfig (device_name, coreConfig, config);
860
926
auto plugin = get_plugin (parsed._deviceName );
861
- ov::SoPtr<ov::ICompiledModel> compiled_model;
862
927
// will consume ov::cache_dir if plugin not support it
863
928
auto cacheManager = parsed._core_config .get_cache_config_for_device (plugin, parsed._config )._cacheManager ;
929
+ auto compiled_model = import_compiled_model (plugin, {}, parsed._config );
864
930
// Skip caching for proxy plugin. HW plugin will load network from the cache
865
- if (cacheManager && device_supports_model_caching (plugin, parsed._config ) && !is_proxy_device (plugin)) {
931
+ if (compiled_model) {
932
+ // hint::compiled_blob is set and imported skip compilation
933
+ } else if (cacheManager && device_supports_model_caching (plugin, parsed._config ) && !is_proxy_device (plugin)) {
866
934
CacheContent cacheContent{cacheManager, parsed._core_config .get_enable_mmap ()};
867
935
cacheContent.blobId =
868
936
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(
1443
1511
cacheContent.blobId ,
1444
1512
cacheContent.mmap_enabled && ov::util::contains (plugin.get_property (ov::internal::supported_properties),
1445
1513
ov::internal::caching_with_mmap),
1446
- [&](std::istream& networkStream, std::shared_ptr< ov::AlignedBuffer> model_buffer ) {
1514
+ [&](std::istream& networkStream, ov::Tensor& compiled_blob ) {
1447
1515
OV_ITT_SCOPE (FIRST_INFERENCE,
1448
1516
ov::itt::domains::LoadTime,
1449
1517
" Core::load_model_from_cache::ReadStreamAndImport" );
1518
+ ov::CompiledBlobHeader header;
1450
1519
try {
1451
- ov::CompiledBlobHeader header;
1452
1520
networkStream >> header;
1453
1521
if (header.get_file_info () != ov::ModelCache::calculate_file_info (cacheContent.modelPath )) {
1454
1522
// Original file is changed, don't use cache
@@ -1477,21 +1545,34 @@ ov::SoPtr<ov::ICompiledModel> ov::CoreImpl::load_model_from_cache(
1477
1545
1478
1546
ov::AnyMap update_config = config;
1479
1547
update_config[ov::loaded_from_cache.name ()] = true ;
1548
+ if (cacheContent.model &&
1549
+ util::contains (plugin.get_property (ov::supported_properties), ov::hint::model)) {
1550
+ update_config[ov::hint::model.name ()] = cacheContent.model ;
1551
+ }
1480
1552
1481
1553
if (util::contains (plugin.get_property (ov::supported_properties), ov::hint::model) &&
1482
1554
cacheContent.model ) {
1483
1555
update_config[ov::hint::model.name ()] = cacheContent.model ;
1484
1556
}
1485
1557
if (util::contains (plugin.get_property (ov::supported_properties), ov::weights_path)) {
1486
- std::filesystem::path weights_path = cacheContent.modelPath ;
1558
+ util::Path weights_path;
1559
+
1560
+ if (auto && path_hint = update_config.find (ov::weights_path.name ());
1561
+ path_hint != update_config.end ()) {
1562
+ weights_path = path_hint->second .as <std::string>();
1563
+ } else if (weights_path = extract_weight_path (header.get_runtime_info ()); weights_path.empty ()) {
1564
+ weights_path = cacheContent.modelPath ;
1565
+ weights_path.replace_extension (" .bin" );
1566
+ }
1487
1567
weights_path.replace_extension (" .bin" );
1488
1568
1489
1569
if (ov::util::file_exists (weights_path)) {
1490
1570
update_config[ov::weights_path.name ()] = weights_path.string ();
1491
1571
}
1492
1572
}
1493
- if (model_buffer) {
1494
- update_config[ov::internal::cached_model_buffer.name ()] = model_buffer;
1573
+
1574
+ if (compiled_blob) {
1575
+ update_config[ov::hint::compiled_blob.name ()] = compiled_blob;
1495
1576
}
1496
1577
compiled_model = context ? plugin.import_model (networkStream, context, update_config)
1497
1578
: plugin.import_model (networkStream, update_config);
0 commit comments