-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathplugin.cpp
More file actions
1021 lines (867 loc) · 46.3 KB
/
Copy pathplugin.cpp
File metadata and controls
1021 lines (867 loc) · 46.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "plugin.hpp"
#include <fstream>
#include <numeric>
#include <optional>
#include "compiled_model.hpp"
#include "intel_npu/common/compiler_adapter_factory.hpp"
#include "intel_npu/common/device_helpers.hpp"
#include "intel_npu/common/filtered_config.hpp"
#include "intel_npu/common/icompiler_adapter.hpp"
#include "intel_npu/common/igraph.hpp"
#include "intel_npu/common/itt.hpp"
#include "intel_npu/common/parser_factory.hpp"
#include "intel_npu/config/npuw.hpp"
#include "intel_npu/config/options.hpp"
#include "intel_npu/utils/utils.hpp"
#include "metrics.hpp"
#include "npuw/compiled_model.hpp"
#include "npuw/gqa_compiled_model.hpp"
#include "npuw/llm_compiled_model.hpp"
#include "npuw/orc/schema_npuw.hpp"
#include "npuw/serialization.hpp"
#include "openvino/core/rt_info/weightless_caching_attributes.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/parameter.hpp"
#include "openvino/runtime/intel_npu/properties.hpp"
#include "openvino/runtime/properties.hpp"
#include "openvino/runtime/shared_buffer.hpp"
#include "openvino/util/file_util.hpp"
#include "plugin_property_manager.hpp"
#include "remote_context.hpp"
#include "transformations.hpp"
namespace {
using namespace intel_npu;
const std::vector<size_t> CONSTANT_NODE_DUMMY_SHAPE{1};
const char* NPU_PLUGIN_LIB_NAME = "openvino_intel_npu_plugin";
constexpr std::string_view WEIGHTS_IR_EXTENSION = ".bin";
constexpr std::string_view ONNX_EXTENSION = ".onnx";
/**
* @brief Creates an "ov::Model" object which contains only the given "parameter" and "result" nodes.
* @details Using an "ov::Model" object to create the "CompiledModel" is the preferred way of using the OV API.
* This path allows making use of the already written functions/attributes for handling the I/O information.
*
* Note that a stored compiled model does not hold the original IR model within it. The only related information
* which may be extracted is the original model's "parameter"/"result" nodes. Thus, we need to build a dummy model
* starting from these fields in order to satisfy the API.
*
* @param inputDescriptors Describes the input nodes.
* @param outputDescriptors Describes the output nodes.
* @returns The dummy "ov::Model" composed of "parameter" and "result" nodes built using the given descriptors.
*/
std::shared_ptr<ov::Model> create_dummy_model(const std::vector<IODescriptor>& inputDescriptors,
const std::vector<IODescriptor>& outputDescriptors,
const std::optional<int64_t> batchSize,
const std::optional<std::vector<ov::Layout>>& inputLayouts,
const std::optional<std::vector<ov::Layout>>& outputLayouts) {
ov::ParameterVector parameters;
ov::ResultVector results;
for (size_t inputIndex = 0; inputIndex < inputDescriptors.size(); ++inputIndex) {
const IODescriptor& inputDescriptor = inputDescriptors.at(inputIndex);
if (inputDescriptor.isStateInput || inputDescriptor.isStateOutput || inputDescriptor.isShapeTensor ||
inputDescriptor.isInitInputWeights || inputDescriptor.isMainInputWeights) {
continue;
}
auto shape = inputDescriptor.shapeFromIRModel.has_value() ? *inputDescriptor.shapeFromIRModel
: inputDescriptor.shapeFromCompiler;
if (batchSize.has_value()) {
shape[intel_npu::utils::BATCH_AXIS] = ov::Dimension(batchSize.value());
}
std::shared_ptr<ov::op::v0::Parameter> parameter =
std::make_shared<ov::op::v0::Parameter>(inputDescriptor.precision, shape);
parameter->set_friendly_name(inputDescriptor.nodeFriendlyName);
parameter->output(0).get_tensor().set_names(inputDescriptor.outputTensorNames);
if (inputLayouts.has_value()) {
parameter->set_layout(inputLayouts->at(inputIndex));
}
parameters.push_back(std::move(parameter));
}
// The "result" nodes require a parent node in order to satisfy the API conventions. Additionally, a dummy shape for
// the "Constant" node was required since the specific constructor does not accept "ov::PartialShape" values (a
// constant can't have dynamic shape). The dummy tensor was also brought in order to register the correct,
// potentially dynamic, output shape.
for (size_t outputIndex = 0; outputIndex < outputDescriptors.size(); ++outputIndex) {
const IODescriptor& outputDescriptor = outputDescriptors.at(outputIndex);
if (outputDescriptor.isStateInput || outputDescriptor.isStateOutput || outputDescriptor.isShapeTensor ||
outputDescriptor.isInitOutputWeights) {
continue;
}
std::shared_ptr<ov::Node> constantDummy =
std::make_shared<ov::op::v0::Constant>(outputDescriptor.precision, CONSTANT_NODE_DUMMY_SHAPE);
auto shape = outputDescriptor.shapeFromIRModel.has_value() ? *outputDescriptor.shapeFromIRModel
: outputDescriptor.shapeFromCompiler;
if (batchSize.has_value()) {
shape[intel_npu::utils::BATCH_AXIS] = ov::Dimension(batchSize.value());
}
const std::shared_ptr<ov::descriptor::Tensor>& tensorDummy =
std::make_shared<ov::descriptor::Tensor>(outputDescriptor.precision,
shape,
outputDescriptor.outputTensorNames);
auto& result = results.emplace_back(std::make_shared<ov::op::v0::Result>(constantDummy));
result->output(0).set_tensor_ptr(tensorDummy);
if (outputLayouts.has_value()) {
result->set_layout(outputLayouts->at(outputIndex));
}
result->set_friendly_name(outputDescriptor.nodeFriendlyName);
}
return std::make_shared<ov::Model>(results, parameters);
}
/**
* @brief Just checks if there is any "WeightlessCacheAttribute" present in the model. In the negative case, an error is
* thrown. The weights separation flow in its current state cannot work without this attribuite.
*/
void check_weightless_cache_attribute_occurrence(const std::shared_ptr<const ov::Model>& model) {
if (!model) {
return;
}
for (const auto& ov_node : model->get_ordered_ops()) {
if (!ov::is_type<ov::op::v0::Constant>(ov_node)) {
continue;
}
if (auto it = ov_node->get_rt_info().find(ov::WeightlessCacheAttribute::get_type_info_static());
it != ov_node->get_rt_info().end()) {
return;
}
}
OPENVINO_THROW("No \"WeightlessCacheAttribute\" has been found in any of the model's Constant nodes. This "
"attribute is required for running the \"weights separation\" flow.");
}
std::shared_ptr<ov::ICompiledModel> import_model_npuw(std::istream& stream,
ov::AnyMap& properties,
std::shared_ptr<const ov::IPlugin> pluginSO) {
if (const auto header = ov::npuw::orc::is_orc(stream);
header.has_value() && header->schema_uuid == ov::npuw::orc::schema_npuw::NPUW_ORC_PARTITIONED_SCHEMA) {
return ov::npuw::CompiledModel::import_model(stream, pluginSO, properties);
}
// If was exported via NPUW
auto stream_start_pos = stream.tellg();
ov::npuw::s11n::IndicatorType serialization_indicator;
if (ov::npuw::orc::try_read_bytes(stream, serialization_indicator.data(), serialization_indicator.size()) &&
serialization_indicator == NPUW_SERIALIZATION_INDICATOR) {
ov::npuw::s11n::IndicatorType compiled_model_indicator;
if (ov::npuw::orc::try_read_bytes(stream, compiled_model_indicator.data(), compiled_model_indicator.size())) {
stream.clear();
stream.seekg(stream_start_pos);
if (compiled_model_indicator == NPUW_GQA_COMPILED_MODEL_INDICATOR) {
return ov::npuw::GQACompiledModel::import_model(stream, pluginSO, properties);
} else if (compiled_model_indicator == NPUW_LLM_COMPILED_MODEL_INDICATOR) {
// Properties are required for ov::weights_path
return ov::npuw::LLMCompiledModel::import_model(stream, pluginSO, properties);
} else if (compiled_model_indicator == NPUW_COMPILED_MODEL_INDICATOR) {
OPENVINO_THROW("Legacy flat NPUW CompiledModel blobs are no longer supported. Re-export the model with "
"the current ORC serializer.");
} else {
OPENVINO_THROW("Couldn't deserialize NPUW blob - fatal error!");
}
}
}
stream.clear();
stream.seekg(stream_start_pos);
// Drop NPUW properties if there are any
for (auto it = properties.begin(); it != properties.end(); ++it) {
if (it->first.find("NPUW") != it->first.npos) {
properties.erase(it->first);
}
}
return nullptr;
}
std::shared_ptr<const ov::Model> get_model_ptr_from_map(ov::AnyMap& properties) {
if (properties.count(ov::hint::model.name())) {
try {
return properties.at(ov::hint::model.name()).as<std::shared_ptr<const ov::Model>>();
} catch (const ov::Exception&) {
try {
return std::const_pointer_cast<const ov::Model>(
properties.at(ov::hint::model.name()).as<std::shared_ptr<ov::Model>>());
} catch (const ov::Exception&) {
OPENVINO_THROW("The value of the \"ov::hint::model\" configuration option (\"MODEL_PTR\") has the "
"wrong data type. Expected: std::shared_ptr<const ov::Model>.");
}
}
}
return nullptr;
}
void init_config(const IEngineBackend* backend, OptionsDesc& options, FilteredConfig& config) {
// Initialize (note: it will reset registered options)
options.reset();
#define REGISTER_OPTION(OPT_TYPE) \
do { \
auto dummyopt = details::makeOptionModel<OPT_TYPE>(); \
std::string o_name = dummyopt.key().data(); \
options.add<OPT_TYPE>(); \
config.enable(std::move(o_name), false); \
} while (0)
REGISTER_OPTION(LOG_LEVEL);
REGISTER_OPTION(CACHE_DIR);
REGISTER_OPTION(CACHE_MODE);
REGISTER_OPTION(COMPILED_BLOB);
REGISTER_OPTION(DEVICE_ID);
REGISTER_OPTION(NUM_STREAMS);
REGISTER_OPTION(PERF_COUNT);
REGISTER_OPTION(LOADED_FROM_CACHE);
REGISTER_OPTION(COMPILATION_NUM_THREADS);
REGISTER_OPTION(PERFORMANCE_HINT);
REGISTER_OPTION(EXECUTION_MODE_HINT);
REGISTER_OPTION(PERFORMANCE_HINT_NUM_REQUESTS);
OPENVINO_SUPPRESS_DEPRECATED_START
REGISTER_OPTION(ENABLE_CPU_PINNING);
OPENVINO_SUPPRESS_DEPRECATED_END
REGISTER_OPTION(INFERENCE_PRECISION_HINT);
REGISTER_OPTION(MODEL_PRIORITY);
REGISTER_OPTION(COMPILATION_MODE_PARAMS);
REGISTER_OPTION(DMA_ENGINES);
REGISTER_OPTION(TILES);
REGISTER_OPTION(COMPILATION_MODE);
REGISTER_OPTION(COMPILER_TYPE);
REGISTER_OPTION(COMPILER_VERSION);
REGISTER_OPTION(PLATFORM);
REGISTER_OPTION(CREATE_EXECUTOR);
REGISTER_OPTION(DYNAMIC_SHAPE_TO_STATIC);
REGISTER_OPTION(PROFILING_TYPE);
REGISTER_OPTION(BACKEND_COMPILATION_PARAMS);
REGISTER_OPTION(BATCH_MODE);
REGISTER_OPTION(BYPASS_UMD_CACHING);
REGISTER_OPTION(DEFER_WEIGHTS_LOAD);
REGISTER_OPTION(WEIGHTS_PATH);
REGISTER_OPTION(RUN_INFERENCES_SEQUENTIALLY);
REGISTER_OPTION(COMPILER_DYNAMIC_QUANTIZATION);
REGISTER_OPTION(QDQ_OPTIMIZATION);
REGISTER_OPTION(QDQ_OPTIMIZATION_AGGRESSIVE);
REGISTER_OPTION(STEPPING);
REGISTER_OPTION(DISABLE_VERSION_CHECK);
REGISTER_OPTION(EXPORT_RAW_BLOB);
REGISTER_OPTION(IMPORT_RAW_BLOB);
REGISTER_OPTION(BATCH_COMPILER_MODE_SETTINGS);
REGISTER_OPTION(TURBO);
REGISTER_OPTION(ENABLE_WEIGHTLESS);
REGISTER_OPTION(SEPARATE_WEIGHTS_VERSION);
REGISTER_OPTION(WS_COMPILE_CALL_NUMBER);
REGISTER_OPTION(MODEL_SERIALIZER_VERSION);
REGISTER_OPTION(ENABLE_STRIDES_FOR);
REGISTER_OPTION(SHARED_COMMON_QUEUE);
REGISTER_OPTION(CACHE_ENCRYPTION_CALLBACKS);
REGISTER_OPTION(RUNTIME_REQUIREMENTS);
REGISTER_OPTION(COMPATIBILITY_CHECK);
if (backend) {
// Options registered only if drivers is present and supports the corresponding extension
REGISTER_OPTION(MAX_TILES);
if (backend->isCommandQueueExtSupported()) {
REGISTER_OPTION(WORKLOAD_TYPE);
}
if (backend->isContextExtSupported()) {
REGISTER_OPTION(DISABLE_IDLE_MEMORY_PRUNING);
}
}
// parse again env_variables to update registered configs which have env vars set
config.parseEnvVars();
// NPUW properties are requested by OV Core during caching and have no effect on the NPU plugin. But we still need
// to enable those for OV Core to query. Note: do this last to not filter them out. register npuw caching properties
for_each_exposed_npuw_option([&](auto tag) {
using Opt = typename decltype(tag)::type;
REGISTER_OPTION(Opt);
});
config.enableRuntimeOptions();
// Special cases - options with OptionMode::Both must be enabled for the plugin even if the compiler does not
// support them, because they may be used by the plugin itself or by the driver.
// We still check compiler support to decide whether these options should be removed from the config string.
// NPU_TURBO might be supported by the driver
if (backend && backend->isCommandQueueExtSupported()) {
config.enable(ov::intel_npu::turbo.name(), true);
}
// LOG_LEVEL, PERFORMANCE_HINT and PERF_COUNT are needed by runtime options
config.enable(ov::log::level.name(), true);
config.enable(ov::hint::performance_mode.name(), true);
config.enable(ov::enable_profiling.name(), true);
if (config.get<COMPILER_TYPE>() == ov::intel_npu::CompilerType::PREFER_PLUGIN && backend != nullptr) {
auto device = backend->getDevice();
if (device) {
auto platformName = device->getName();
CompilerAdapterFactory compilerFactory;
auto compileType = compilerFactory.determineAppropriateCompilerTypeBasedOnPlatform(platformName);
if (compileType == ov::intel_npu::CompilerType::DRIVER) {
config.update({{ov::intel_npu::compiler_type.name(), COMPILER_TYPE::toString(compileType)}});
}
}
}
}
std::optional<ov::log::Level> read_log_level(const ov::AnyMap& properties) {
const auto it = properties.find(ov::log::level.name());
if (it == properties.end()) {
return std::nullopt;
}
return it->second.as<ov::log::Level>();
}
} // namespace
namespace intel_npu {
Plugin::Plugin() : _logger("NPUPlugin", Logger::global().level()) {
OV_ITT_SCOPED_TASK(itt::domains::NPUPlugin, "Plugin::Plugin");
set_device_name("NPU");
std::shared_ptr<OptionsDesc> options = std::make_shared<OptionsDesc>();
// parse env_variables to get LOG_LEVEL if needed
options->add<LOG_LEVEL>();
FilteredConfig config(options);
config.parseEnvVars();
Logger::global().setLevel(config.get<LOG_LEVEL>());
_logger.setLevel(config.get<LOG_LEVEL>());
OV_ITT_TASK_CHAIN(PLUGIN, itt::domains::NPUPlugin, "Plugin::Plugin", "GetBackend");
// backend registry shall be created after configs are updated
_backendsRegistry = std::make_unique<BackendsRegistry>();
_backend = _backendsRegistry->getEngineBackend();
OV_ITT_TASK_NEXT(PLUGIN, "InitConfig");
init_config(_backend._ptr.get(), *options, config);
if (_backend) {
OV_ITT_TASK_NEXT(PLUGIN, "RegisterBackendOptions");
_backend->registerOptions(*options);
}
OV_ITT_TASK_NEXT(PLUGIN, "CreateMetrics");
auto metrics = std::make_shared<Metrics>(_backend);
/// Init and register properties
OV_ITT_TASK_NEXT(PLUGIN, "RegisterProperties");
_propertiesManager = std::make_unique<PluginPropertyManager>(config, metrics, _backend, _logger);
}
void Plugin::set_property(const ov::AnyMap& properties) {
if (properties.empty()) {
return;
}
update_log_level(properties);
if (_backend != nullptr) {
_backend->updateInfo(properties);
}
_propertiesManager->setProperty(properties);
}
ov::Any Plugin::get_property(const std::string& name, const ov::AnyMap& arguments) const {
return _propertiesManager->getProperty(name, arguments);
}
bool Plugin::is_property_supported(const std::string& name, const ov::AnyMap& arguments) const {
return _propertiesManager->isPropertySupported(name, arguments);
}
std::shared_ptr<ov::ICompiledModel> Plugin::compile_model(const std::shared_ptr<const ov::Model>& model,
const ov::AnyMap& properties) const {
OV_ITT_SCOPED_TASK(itt::domains::NPUPlugin, "Plugin::compile_model");
LogLevelScope logScope(properties, _logger);
// Before going any further: if
// ... 1 - NPUW mode is activated
// ... 2 - this request is NOT coming from NPUW,
// activate the NPUW path
auto useNpuwKey = ov::intel_npu::use_npuw.name();
ov::AnyMap localProperties = properties;
if (localProperties.count(useNpuwKey)) {
if (localProperties.at(useNpuwKey).as<bool>() == true) {
return ov::npuw::ICompiledModel::create(model->clone(), shared_from_this(), localProperties);
} else {
// NPUW is disabled, remove the key from the properties
localProperties.erase(useNpuwKey);
}
}
if (_backend != nullptr) {
_backend->updateInfo(localProperties);
}
// Resolving the requested compiler type based on local and global properties.
// It can still remain PREFER_PLUGIN even after this point
ov::intel_npu::CompilerType compilerType = _propertiesManager->determineCompilerType(localProperties);
auto deviceId = _propertiesManager->determineDeviceId(localProperties);
// DEVICE_ID can be passed both as an index and as a platform name.
// Identify the right device object to be taken into account when the target compilation platform is determined
std::shared_ptr<IDevice> device = utils::getDeviceById(_backend, deviceId);
// Determine the final compilation target based on NPU_PLATFORM, determined device name (if any) and the list of
// available devices (if any)
const auto compilationPlatform =
utils::getCompilationPlatform(_propertiesManager->determinePlatform(localProperties),
device == nullptr ? std::move(deviceId) : device->getName(),
_backend == nullptr ? std::vector<std::string>() : _backend->getDeviceNames());
CompilerAdapterFactory factory;
auto compiler = factory.getCompiler(_backend, compilerType, compilationPlatform);
localProperties[ov::intel_npu::compiler_type.name()] = compilerType;
if (!compilationPlatform.empty()) {
localProperties[ov::intel_npu::platform.name()] = compilationPlatform;
}
OV_ITT_TASK_CHAIN(PLUGIN_COMPILE_MODEL, itt::domains::NPUPlugin, "Plugin::compile_model", "fork_local_config");
FilteredConfig localConfig = _propertiesManager->getConfigForSpecificCompiler(localProperties, compiler.get());
localConfig.update({{ov::intel_npu::compiler_version.name(), std::to_string(compiler->get_version())}});
auto updateBatchMode = [&](ov::intel_npu::BatchMode mode) {
std::stringstream strStream;
strStream << mode;
_logger.info("Setting batching mode to %s.", strStream.str().c_str());
localConfig.update({{ov::intel_npu::batch_mode.name(), strStream.str()}});
};
// Handle batch mode configuration
std::optional<ov::Dimension> originalBatch = std::nullopt;
std::shared_ptr<ov::Model> batchedModel;
bool shouldHandleBatching = false;
bool successfullyDebatched = false;
if (localConfig.isAvailable(ov::intel_npu::batch_mode.name())) {
// Set default batch mode if not configured
if (!localConfig.has(ov::intel_npu::batch_mode.name())) {
updateBatchMode(ov::intel_npu::BatchMode::AUTO);
}
// Handle models with variables (states)
if (!model->get_variables().empty()) {
if (localConfig.get<BATCH_MODE>() == ov::intel_npu::BatchMode::PLUGIN) {
OPENVINO_THROW(
"This model contains states, thus it is not supported when handling batching on the plugin");
}
updateBatchMode(ov::intel_npu::BatchMode::COMPILER);
}
shouldHandleBatching = true;
} else {
// If the model contains states, it is not supported when handling batching on the plugin
shouldHandleBatching = model->get_variables().empty();
}
if (shouldHandleBatching) {
// Process batching
std::tie(batchedModel, successfullyDebatched) =
intel_npu::batch_helpers::handlePluginBatching(model, localConfig, updateBatchMode, originalBatch, _logger);
}
if (localConfig.has(ov::intel_npu::enable_strides_for.name())) {
if (model->is_dynamic()) {
OPENVINO_ASSERT(
!intel_npu::batch_helpers::checkModelDynamicDims(model),
"Dynamic shape tensors are not supported with the dynamic strides feature (ENABLE_STRIDES_FOR).");
OPENVINO_ASSERT(successfullyDebatched || !localConfig.isAvailable(ov::intel_npu::batch_mode.name()) ||
localConfig.get<BATCH_MODE>() != ov::intel_npu::BatchMode::COMPILER,
"Dynamic batching is not supported with the dynamic strides feature (ENABLE_STRIDES_FOR).");
}
}
// Update stepping w/ information from driver, unless provided by user or we are off-device
// Ignore if compilation was requested for a platform that is different from the current one
if (!localConfig.has<STEPPING>() && device != nullptr && device->getName() == compilationPlatform) {
try {
localConfig.update({{ov::intel_npu::stepping.name(), std::to_string(device->getSubDevId())}});
} catch (...) {
_logger.warning("Stepping information not implemented by selected backend. Skipping. Please provide "
"NPU_STEPPING if required.");
}
}
// Update max_tiles w/ information from driver, unless provided by user or we are off-device
// Ignore if compilation was requested for a platform that is different from the current one
if (!localConfig.has<MAX_TILES>() && device != nullptr && device->getName() == compilationPlatform) {
try {
localConfig.update({{ov::intel_npu::max_tiles.name(), std::to_string(device->getMaxNumSlices())}});
} catch (...) {
_logger.warning("Max tiles information not implemented by selected backend. Default value will be used.");
}
}
OV_ITT_TASK_NEXT(PLUGIN_COMPILE_MODEL, "compile");
if (localConfig.isAvailable(ov::enable_weightless.name()) && !localConfig.get<CACHE_DIR>().empty()) {
// If OV caching is enabled, then weights separation is performed only if the user opted for optimizing the
// size of the binary object
const bool cacheModeOptimizeSize = (localConfig.get<CACHE_MODE>() == ov::CacheMode::OPTIMIZE_SIZE);
if (localConfig.get<ENABLE_WEIGHTLESS>() && !cacheModeOptimizeSize) {
_logger.warning(
"The cache mode was not set to \"optimize size\" but the \"ENABLE_WEIGHTLESS\" configuration option "
"was set to true. Weights separation WILL NOT be performed in this case.");
} else if (!localConfig.get<ENABLE_WEIGHTLESS>() && cacheModeOptimizeSize) {
_logger.warning(
"The cache mode was set to \"optimize size\" but the \"ENABLE_WEIGHTLESS\" configuration option "
"was set to false. Weights separation WILL be performed in this case.");
}
localConfig.update({{ov::enable_weightless.name(), cacheModeOptimizeSize ? "YES" : "NO"}});
}
std::shared_ptr<intel_npu::IGraph> graph;
auto compileWithConfig = [&](auto&& modelToCompile, const auto& config) {
if (!localConfig.get<ENABLE_WEIGHTLESS>()) {
return compiler->compile(modelToCompile, config);
} else {
check_weightless_cache_attribute_occurrence(model);
return compiler->compileWS(std::move(modelToCompile), config);
}
};
try {
_logger.debug("performing compile");
// Determine which model to use
auto modelToCompile = successfullyDebatched ? std::move(batchedModel) : model->clone();
const bool performanceHintSetByUser = localConfig.has(ov::hint::performance_mode.name());
const bool shouldForceThroughput = successfullyDebatched && !performanceHintSetByUser;
const bool shouldWarnAboutLatency = successfullyDebatched && performanceHintSetByUser &&
localConfig.get<PERFORMANCE_HINT>() == ov::hint::PerformanceMode::LATENCY;
if (shouldWarnAboutLatency) {
_logger.warning("PERFORMANCE_HINT is explicitly set to LATENCY mode, but batch dimension (N) is "
"detected in the model. The NPU Plugin will reshape the model to batch size 1 and "
"process each batch slice separately.");
_logger.warning("For optimal performance with batched models, THROUGHPUT mode is highly recommended, "
"as LATENCY mode prevents parallel batch processing.");
_logger.warning("If batch detection appears incorrect, verify that the input and output layouts are "
"configured properly.");
}
if (shouldForceThroughput) {
_logger.info("Setting performance mode to THROUGHPUT for batched model compilation.");
auto modifiedConfig = localConfig; // Copy only when needed
std::stringstream strStream;
strStream << ov::hint::PerformanceMode::THROUGHPUT;
modifiedConfig.update({{ov::hint::performance_mode.name(), strStream.str()}});
graph = compileWithConfig(std::move(modelToCompile), modifiedConfig);
} else {
graph = compileWithConfig(std::move(modelToCompile), localConfig);
}
} catch (const std::exception& ex) {
OPENVINO_THROW(ex.what());
} catch (...) {
_logger.error("Unexpected exception");
OPENVINO_THROW("NPU plugin: got an unexpected exception from compiler");
}
std::optional<int64_t> batch = std::nullopt;
if (originalBatch.has_value() && successfullyDebatched) {
batch = originalBatch.value().is_static() ? originalBatch.value().get_length() : -1;
if (batch > 0) {
// Initial batch setup for static cases
graph->set_batch_size(batch.value());
}
}
if (localConfig.has(CACHE_ENCRYPTION_CALLBACKS::key().data()) &&
!localConfig.get<CACHE_ENCRYPTION_CALLBACKS>().encrypt) {
_logger.warning("Encryption callbacks were provided for compiled model creation, but the encrypt "
"callback is null. Proceeding with unencrypted compilation; encrypted blob export "
"will be disabled.");
}
std::shared_ptr<ov::ICompiledModel> compiledModel;
try {
compiledModel = std::make_shared<CompiledModel>(model, shared_from_this(), device, graph, localConfig, batch);
} catch (const std::exception& ex) {
OPENVINO_THROW(ex.what());
} catch (...) {
OPENVINO_THROW("Unexpected exception thrown upon attempting to create the \"CompiledModel\" object");
}
++_compiledModelLoadCounter;
OV_ITT_TASK_SKIP(PLUGIN_COMPILE_MODEL);
return compiledModel;
}
std::shared_ptr<ov::ICompiledModel> Plugin::compile_model(const std::shared_ptr<const ov::Model>& model,
const ov::AnyMap& properties,
const ov::SoPtr<ov::IRemoteContext>& context) const {
auto casted = std::dynamic_pointer_cast<RemoteContextImpl>(context._ptr);
if (casted == nullptr) {
OPENVINO_THROW("Invalid remote context type. Can't cast to ov::intel_npu::RemoteContext type");
}
return compile_model(model, properties);
}
ov::SoPtr<ov::IRemoteContext> Plugin::create_context(const ov::AnyMap& remoteProperties) const {
return std::make_shared<RemoteContextImpl>(_backend, remoteProperties);
}
ov::SoPtr<ov::IRemoteContext> Plugin::get_default_context(const ov::AnyMap&) const {
return std::make_shared<RemoteContextImpl>(_backend);
}
std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& stream, const ov::AnyMap& properties) const {
OV_ITT_SCOPED_TASK(itt::domains::NPUPlugin, "Plugin::import_model");
LogLevelScope logScope(properties, _logger);
if (properties.find(ov::hint::compiled_blob.name()) != properties.end()) {
_logger.warning("ov::hint::compiled_blob is no longer supported for import_model(stream) API! Please use new "
"import_model(tensor) API instead.");
}
auto npuPluginProperties = properties;
// NPUW properties from npuPluginProperties will be erased if import_model_npuw returns nullptr
auto compiledModel = import_model_npuw(stream, npuPluginProperties, shared_from_this());
if (compiledModel) {
return compiledModel;
}
if (_backend != nullptr) {
_backend->updateInfo(npuPluginProperties);
}
try {
const bool skipCompatibility =
(npuPluginProperties.find(DISABLE_VERSION_CHECK::key().data()) != npuPluginProperties.end())
? npuPluginProperties[DISABLE_VERSION_CHECK::key().data()].as<bool>()
: _propertiesManager->getConfig().get<DISABLE_VERSION_CHECK>();
const bool importRawBlob =
(npuPluginProperties.find(IMPORT_RAW_BLOB::key().data()) != npuPluginProperties.end())
? npuPluginProperties[IMPORT_RAW_BLOB::key().data()].as<bool>()
: _propertiesManager->getConfig().get<IMPORT_RAW_BLOB>();
std::unique_ptr<MetadataBase> metadata = nullptr;
size_t blobSize = MetadataBase::getFileSize(stream);
if (!importRawBlob && !skipCompatibility) {
// Read only metadata from the stream and check if blob is compatible. Load blob into memory only in case it
// passes compatibility checks.
metadata = read_metadata_from(stream);
blobSize = metadata->get_blob_size();
} else {
_logger.info("Blob compatibility check skipped.");
}
OPENVINO_ASSERT(blobSize > 0, "Parsed blob size is empty from the given stream!");
ov::Allocator customAllocator{utils::AlignedAllocator{utils::STANDARD_PAGE_SIZE}};
ov::Tensor tensor(ov::element::u8, ov::Shape{blobSize}, customAllocator);
if (blobSize > static_cast<decltype(blobSize)>(std::numeric_limits<std::streamsize>::max())) {
OPENVINO_THROW("Blob size is too large to be represented on a std::streamsize!");
}
stream.read(tensor.data<char>(), static_cast<std::streamsize>(blobSize));
return parse(tensor, std::move(metadata), npuPluginProperties);
} catch (const std::exception& ex) {
OPENVINO_THROW("Can't import network: ", ex.what());
} catch (...) {
OPENVINO_THROW("NPU import_model got unexpected exception from CompiledModel");
}
}
std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& stream,
const ov::SoPtr<ov::IRemoteContext>& context,
const ov::AnyMap& properties) const {
auto casted = std::dynamic_pointer_cast<RemoteContextImpl>(context._ptr);
if (casted == nullptr) {
OPENVINO_THROW("Invalid remote context type. Can't cast to ov::intel_npu::RemoteContext type");
}
return import_model(stream, properties);
}
std::shared_ptr<ov::ICompiledModel> Plugin::import_model(const ov::Tensor& compiledBlob,
const ov::AnyMap& properties) const {
OV_ITT_SCOPED_TASK(itt::domains::NPUPlugin, "Plugin::import_model");
LogLevelScope logScope(properties, _logger);
// Need to create intermediate istream for NPUW
ov::SharedStreamBuffer buffer{compiledBlob.data(), compiledBlob.get_byte_size()};
std::istream stream{&buffer};
auto npuPluginProperties = properties;
// NPUW properties from npuPluginProperties will be erased if import_model_npuw returns nullptr
auto compiledModel = import_model_npuw(stream, npuPluginProperties, shared_from_this());
if (compiledModel) {
return compiledModel;
}
if (_backend != nullptr) {
_backend->updateInfo(npuPluginProperties);
}
try {
const bool skipCompatibility =
(npuPluginProperties.find(DISABLE_VERSION_CHECK::key().data()) != npuPluginProperties.end())
? npuPluginProperties[DISABLE_VERSION_CHECK::key().data()].as<bool>()
: _propertiesManager->getConfig().get<DISABLE_VERSION_CHECK>();
const bool importRawBlob =
(npuPluginProperties.find(IMPORT_RAW_BLOB::key().data()) != npuPluginProperties.end())
? npuPluginProperties[IMPORT_RAW_BLOB::key().data()].as<bool>()
: _propertiesManager->getConfig().get<IMPORT_RAW_BLOB>();
std::unique_ptr<MetadataBase> metadata = nullptr;
size_t blobSize = compiledBlob.get_byte_size();
if (!importRawBlob && !skipCompatibility) {
metadata = read_metadata_from(compiledBlob);
blobSize = metadata->get_blob_size();
} else {
_logger.info("Blob compatibility check skipped.");
}
OPENVINO_ASSERT(blobSize > 0, "Parsed blob size is empty from the given buffer!");
const ov::Tensor roiTensor(compiledBlob,
ov::Coordinate{0},
ov::Coordinate{blobSize}); // ROI tensor to skip NPU plugin metadata
return parse(roiTensor, std::move(metadata), npuPluginProperties);
} catch (const std::exception& ex) {
OPENVINO_THROW("Can't import network: ", ex.what());
} catch (...) {
OPENVINO_THROW("NPU import_model got unexpected exception from CompiledModel");
}
}
std::shared_ptr<ov::ICompiledModel> Plugin::import_model(const ov::Tensor& compiledBlob,
const ov::SoPtr<ov::IRemoteContext>& context,
const ov::AnyMap& properties) const {
auto casted = std::dynamic_pointer_cast<RemoteContextImpl>(context._ptr);
if (casted == nullptr) {
OPENVINO_THROW("Invalid remote context type. Can't cast to ov::intel_npu::RemoteContext type");
}
return import_model(compiledBlob, properties);
}
ov::SupportedOpsMap Plugin::query_model(const std::shared_ptr<const ov::Model>& model,
const ov::AnyMap& properties) const {
OV_ITT_SCOPED_TASK(itt::domains::NPUPlugin, "Plugin::query_model");
LogLevelScope logScope(properties, _logger);
auto localProperties = properties;
if (_backend != nullptr) {
_backend->updateInfo(localProperties);
}
ov::intel_npu::CompilerType compilerType = _propertiesManager->determineCompilerType(localProperties);
auto deviceId = _propertiesManager->determineDeviceId(localProperties);
std::shared_ptr<IDevice> device = utils::getDeviceById(_backend, deviceId);
const auto compilationPlatform =
utils::getCompilationPlatform(_propertiesManager->determinePlatform(localProperties),
device == nullptr ? std::move(deviceId) : device->getName(),
_backend == nullptr ? std::vector<std::string>() : _backend->getDeviceNames());
CompilerAdapterFactory factory;
auto compiler = factory.getCompiler(_backend, compilerType, compilationPlatform);
localProperties[ov::intel_npu::compiler_type.name()] = compilerType;
if (!compilationPlatform.empty()) {
localProperties[ov::intel_npu::platform.name()] = compilationPlatform;
}
FilteredConfig localConfig = _propertiesManager->getConfigForSpecificCompiler(localProperties, compiler.get());
ov::SupportedOpsMap supportedOpsMap;
try {
supportedOpsMap = compiler->query(model->clone(), localConfig);
} catch (const std::runtime_error& e) {
OPENVINO_THROW(e.what());
} catch (...) {
OPENVINO_THROW("NPU query_model got unexpected error from compiler");
}
return supportedOpsMap;
}
std::shared_ptr<ov::ICompiledModel> Plugin::parse(const ov::Tensor& tensorBig,
std::unique_ptr<MetadataBase> metadata,
const ov::AnyMap& properties) const {
OV_ITT_SCOPED_TASK(itt::domains::NPUPlugin, "Plugin::parse");
auto localProperties = properties;
auto originalModel = get_model_ptr_from_map(localProperties);
std::shared_ptr<IDevice> device =
utils::getDeviceById(_backend, _propertiesManager->determineDeviceId(localProperties));
if (_backend == nullptr || device == nullptr) {
OPENVINO_THROW("Device not found.");
}
OV_ITT_TASK_CHAIN(PLUGIN_PARSE_MODEL, itt::domains::NPUPlugin, "Plugin::parse", "fork_local_config");
FilteredConfig localConfig = _propertiesManager->getConfigWithCompilerPropertiesDisabled(localProperties);
const auto loadedFromCache = localConfig.get<LOADED_FROM_CACHE>();
if (!loadedFromCache) {
_logger.warning(
"The usage of a compiled model can lead to undefined behavior. Please use OpenVINO IR instead!");
}
const bool isNotNullDecryption = localConfig.has(CACHE_ENCRYPTION_CALLBACKS::key().data()) &&
localConfig.get<CACHE_ENCRYPTION_CALLBACKS>().decrypt != nullptr;
if (!metadata && isNotNullDecryption) {
_logger.warning(
"Received decryption callback, but metadata parsing is skipped and cannot determine if blob was "
"encrypted or not.");
}
ov::Tensor tensor = tensorBig;
if (isNotNullDecryption &&
(metadata == nullptr || (metadata != nullptr && metadata->is_encrypted_blob().value_or(false)))) {
{
std::string decryptedBlobStr;
{
std::string encryptedBlobStr(tensor.data<const char>(), tensor.get_byte_size()); // +1x blob size
decryptedBlobStr =
localConfig.get<CACHE_ENCRYPTION_CALLBACKS>().decrypt(encryptedBlobStr); // +2x blob size
} // -1x blob size when deallocating temporary encrypted blob string
ov::Allocator customAllocator{utils::AlignedAllocator{utils::STANDARD_PAGE_SIZE}};
size_t alignedSize = utils::align_size_to_standard_page_size(decryptedBlobStr.size());
size_t paddingSize = alignedSize - decryptedBlobStr.size();
tensor = ov::Tensor(ov::element::u8, ov::Shape{alignedSize},
customAllocator); // +1x blob size
std::memcpy(tensor.data<char>(), decryptedBlobStr.c_str(), decryptedBlobStr.size());
if (paddingSize > 0) {
// If user altered in some way initial blob during encryption, check if its size is still paged aligned
_logger.warning("Decrypted blob size was not page aligned, additional %zu bytes padding will be added",
paddingSize);
std::memset(tensor.data<char>() + decryptedBlobStr.size(), 0, paddingSize);
}
} // -1x blob size when deallocating decrypted blob string
}
uint64_t mainSize = tensor.get_byte_size();
std::optional<std::vector<uint64_t>> initSizes;
std::optional<int64_t> batchSize = std::nullopt;
if (metadata) {
if (metadata->is_encrypted_blob().value_or(false) && !isNotNullDecryption) {
OPENVINO_THROW("Blob is encrypted, but no decryption callback was provided!");
}
size_t accumulator = 0;
initSizes = metadata->get_init_sizes();
mainSize = initSizes.has_value()
? metadata->get_blob_size() - std::accumulate(initSizes->begin(), initSizes->end(), accumulator)
: metadata->get_blob_size();
batchSize = metadata->get_batch_size();
std::optional<uint32_t> compilerVersion = metadata->get_compiler_version();
if (compilerVersion.has_value()) {
localConfig.update({{ov::intel_npu::compiler_version.name(), std::to_string(compilerVersion.value())}});
_logger.debug("Imported model was compiled with compiler version: %u.%u",
ONEAPI_VERSION_MAJOR(compilerVersion.value()),
ONEAPI_VERSION_MINOR(compilerVersion.value()));
}
} else {
_logger.warning(
"Metadata parsing is skipped, if this is a weightless blob, init schedules cannot be parsed from it!");
}
const ov::Tensor tensorMain(tensor,
ov::Coordinate{0},
ov::Coordinate{mainSize}); // ROI tensor to skip NPU plugin metadata
std::vector<ov::Tensor> tensorsInits;
const bool weightsSeparationEnabled = initSizes.has_value();
if (weightsSeparationEnabled) {
// Read the init compiled models as well
size_t cursorPosition = mainSize;
for (uint64_t initSize : initSizes.value()) {
const ov::Tensor tensorInit(tensor,
ov::Coordinate{cursorPosition},
ov::Coordinate{cursorPosition + initSize});
tensorsInits.push_back(tensorInit);
cursorPosition += initSize;
}
// Retrieve the ov::Model used for compilation. This is required for extracting and matching the weights
if (!originalModel) {
if (!localConfig.get<WEIGHTS_PATH>().empty()) {
const std::string weightsPath = localConfig.get<WEIGHTS_PATH>();
auto ext = ov::util::path_to_string(ov::util::make_path(weightsPath).extension());
if (ext == ONNX_EXTENSION) {
originalModel = get_core()->read_model(weightsPath, weightsPath, properties);
} else if (ext == WEIGHTS_IR_EXTENSION) {
// constants will be populated in parser
} else {
OPENVINO_THROW("Invalid path to the weights: ",
weightsPath,
". A \".bin\" or \".onnx\" extension was expected.");
}
} else {
OPENVINO_THROW("Attempted to load a weightless compiled model, but no weights have been provided");
}
}
check_weightless_cache_attribute_occurrence(originalModel);
}
const std::optional<std::vector<ov::Tensor>> initBlobs =
weightsSeparationEnabled ? std::make_optional(std::move(tensorsInits)) : std::nullopt;
// Special case for PERF_COUNT as it requires compiler_type detection in case it is still set to PREFER_PLUGIN
if (localConfig.has<PERF_COUNT>() && localConfig.get<PERF_COUNT>() &&
localConfig.get<COMPILER_TYPE>() == ov::intel_npu::CompilerType::PREFER_PLUGIN) {
ov::intel_npu::CompilerType compilerType = localConfig.get<COMPILER_TYPE>();
CompilerAdapterFactory factory;
(void)factory.getCompiler(_backend, compilerType, device->getName());
localConfig.update({{ov::intel_npu::compiler_type.name(), COMPILER_TYPE::toString(compilerType)}});
}
ParserFactory parserFactory;
auto parser = parserFactory.getParser(_backend->getInitStructs());
// Convert descriptor to an owning string before metadata is potentially destroyed.
std::optional<std::string> compatibilityDescriptor = std::nullopt;
if (metadata) {
if (const auto descriptorView = metadata->get_compatibility_descriptor(); descriptorView.has_value()) {
compatibilityDescriptor = std::string(descriptorView.value());
}
}
auto graph = parser->parse(tensorMain,
localConfig,
initBlobs,
weightsSeparationEnabled && originalModel != nullptr
? std::make_optional(std::move(originalModel))
: std::nullopt,
compatibilityDescriptor);
graph->update_network_name("net" + std::to_string(_compiledModelLoadCounter++));
const std::shared_ptr<ov::Model> modelDummy =
create_dummy_model(graph->get_metadata().inputs,
graph->get_metadata().outputs,
batchSize,
metadata ? metadata->get_input_layouts() : std::nullopt,
metadata ? metadata->get_output_layouts() : std::nullopt);
if (batchSize.has_value()) {
if (batchSize.value() > 0) {
// Initial batch setup for static cases
graph->set_batch_size(batchSize.value());
}
}
OV_ITT_TASK_NEXT(PLUGIN_PARSE_MODEL, "parse");
return std::make_shared<CompiledModel>(modelDummy, shared_from_this(), device, graph, localConfig, batchSize);
}
Plugin::LogLevelScope::LogLevelScope(const ov::AnyMap& props, Logger& instanceLogger)
: _instanceLogger(instanceLogger) {
const auto lvl = read_log_level(props);
if (!lvl) {
return;
}
_prevGlobal = Logger::global().level();
_prevInstance = _instanceLogger.level();
Logger::global().setLevel(*lvl);
_instanceLogger.setLevel(*lvl);
}
Plugin::LogLevelScope::~LogLevelScope() {
if (!_prevGlobal) {