OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[`intel_npu`] [`EXPORT_MODEL`] [`RAW_BLOB`] Throw when `NPU_EXPORT_RAW_BLOB` property is set for irregular blobs (#37814)
Mircea-Aurelian Dan63ed92cea55d
diff --git a/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/src/compiled_model.cppindex bfae76a6775..7d47f7439f5 100644--- a/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp+++ b/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp@@ -96,6 +96,12 @@ std::shared_ptr<ov::ISyncInferRequest> CompiledModel::create_sync_infer_request(void CompiledModel::export_model(std::ostream& stream) const {_logger.debug("CompiledModel::export_model");+ const bool rawBlobExportRequested = _propertiesManager->getConfig().get<EXPORT_RAW_BLOB>();+ if (rawBlobExportRequested) {+ OPENVINO_ASSERT(_graph->get_blob_type() == BlobType::ELF && _graph->get_kind() == GraphKind::Weightful,+ "Requested raw blob export, but the graph is not a weightful ELF one.");+ }+uint64_t blobSizesBeforeVersioning;std::optional<uint64_t> blobSizeAfterEncryption = std::nullopt;std::optional<std::vector<uint64_t>> initBlobSizes;@@ -122,36 +128,38 @@ void CompiledModel::export_model(std::ostream& stream) const {std::tie(blobSizesBeforeVersioning, initBlobSizes) = _graph->export_blob(stream);}- if (!_propertiesManager->getConfig().get<EXPORT_RAW_BLOB>()) {- std::optional<std::vector<ov::Layout>> inputLayouts = std::vector<ov::Layout>();- std::optional<std::vector<ov::Layout>> outputLayouts = std::vector<ov::Layout>();+ if (rawBlobExportRequested) {+ return;+ }- for (const ov::Output<const ov::Node>& nodeOutput : inputs()) {- inputLayouts->push_back(- std::dynamic_pointer_cast<const ov::op::v0::Parameter>(nodeOutput.get_node_shared_ptr())->get_layout());- }- for (const ov::Output<const ov::Node>& nodeOutput : outputs()) {- outputLayouts->push_back(- std::dynamic_pointer_cast<const ov::op::v0::Result>(nodeOutput.get_node_shared_ptr())->get_layout());- }+ std::optional<std::vector<ov::Layout>> inputLayouts = std::vector<ov::Layout>();+ std::optional<std::vector<ov::Layout>> outputLayouts = std::vector<ov::Layout>();- std::optional<uint32_t> compilerVersion = std::nullopt;- if (_propertiesManager->getConfig().has(ov::intel_npu::compiler_version.name())) {- compilerVersion = _propertiesManager->getConfig().get<COMPILER_VERSION>();- }+ for (const ov::Output<const ov::Node>& nodeOutput : inputs()) {+ inputLayouts->push_back(+ std::dynamic_pointer_cast<const ov::op::v0::Parameter>(nodeOutput.get_node_shared_ptr())->get_layout());+ }+ for (const ov::Output<const ov::Node>& nodeOutput : outputs()) {+ outputLayouts->push_back(+ std::dynamic_pointer_cast<const ov::op::v0::Result>(nodeOutput.get_node_shared_ptr())->get_layout());+ }- Metadata<CURRENT_METADATA_VERSION>(blobSizesBeforeVersioning,- CURRENT_OPENVINO_VERSION,- initBlobSizes,- _batchSize,- inputLayouts,- outputLayouts,- compilerVersion,- blobSizeAfterEncryption,- _graph->get_compatibility_descriptor(),- _graph->get_blob_type())- .write(stream);+ std::optional<uint32_t> compilerVersion = std::nullopt;+ if (_propertiesManager->getConfig().has(ov::intel_npu::compiler_version.name())) {+ compilerVersion = _propertiesManager->getConfig().get<COMPILER_VERSION>();}++ Metadata<CURRENT_METADATA_VERSION>(blobSizesBeforeVersioning,+ CURRENT_OPENVINO_VERSION,+ initBlobSizes,+ _batchSize,+ inputLayouts,+ outputLayouts,+ compilerVersion,+ blobSizeAfterEncryption,+ _graph->get_compatibility_descriptor(),+ _graph->get_blob_type())+ .write(stream);}std::shared_ptr<const ov::Model> CompiledModel::get_runtime_model() const {diff --git a/src/plugins/intel_npu/tests/functional/behavior/compiled_model/import_export.cpp b/src/plugins/intel_npu/tests/functional/behavior/compiled_model/import_export.cppindex f16f7ebed12..4ac5b80817c 100644--- a/src/plugins/intel_npu/tests/functional/behavior/compiled_model/import_export.cpp+++ b/src/plugins/intel_npu/tests/functional/behavior/compiled_model/import_export.cpp@@ -12,6 +12,7 @@#include "common/utils.hpp"#include "common_test_utils/subgraph_builders/conv_pool_relu.hpp"#include "intel_npu/npu_private_properties.hpp"+#include "openvino/opsets/opset6.hpp"#include "openvino/runtime/make_tensor.hpp"#include "openvino/util/codec_xor.hpp"@@ -21,6 +22,96 @@ namespace test {namespace behavior {+inline constexpr std::string_view HostCompile_Interpreter = "HostCompile_Interpreter";++inline std::shared_ptr<ov::Model> createCustomNetModel(bool dynamicBatch = false) {+ const ov::Dimension batchDimension = dynamicBatch ? ov::Dimension(1, 10) : ov::Dimension(1);+ const ov::PartialShape inputShape{batchDimension, 16, ov::Dimension(1, 1080), ov::Dimension(10, 1920)};+ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, inputShape);+ input->set_friendly_name("Parameter_59");++ auto make_conv_add = [](const ov::Output<ov::Node>& data,+ const std::string& convName,+ const std::string& addName,+ float weightValue,+ float biasValue) -> ov::Output<ov::Node> {+ const std::vector<float> weightValues(16 * 16, weightValue);+ const std::vector<float> biasValues(16, biasValue);++ auto weights = ov::op::v0::Constant::create(ov::element::f16, ov::Shape{16, 16, 1, 1}, weightValues);+ auto conv = std::make_shared<ov::op::v1::Convolution>(data,+ weights,+ ov::Strides{1, 1},+ ov::CoordinateDiff{0, 0},+ ov::CoordinateDiff{0, 0},+ ov::Strides{1, 1},+ ov::op::PadType::EXPLICIT);+ conv->set_friendly_name(convName);++ auto bias = ov::op::v0::Constant::create(ov::element::f16, ov::Shape{1, 16, 1, 1}, biasValues);+ auto add = std::make_shared<ov::op::v1::Add>(conv, bias);+ add->set_friendly_name(addName);+ return add;+ };++ auto x = make_conv_add(input, "Convolution_61", "Add_63", 0.01f, 0.001f);+ x = make_conv_add(x, "Convolution_65", "Add_67", 0.011f, 0.001f);++ auto relu68 = std::make_shared<ov::op::v0::Relu>(x);+ relu68->set_friendly_name("Relu_68");+ x = relu68;++ x = make_conv_add(x, "Convolution_70", "Add_72", 0.012f, 0.001f);+ auto relu73 = std::make_shared<ov::op::v0::Relu>(x);+ relu73->set_friendly_name("Relu_73");+ x = relu73;++ x = make_conv_add(x, "Convolution_75", "Add_77", 0.013f, 0.001f);+ auto relu78 = std::make_shared<ov::op::v0::Relu>(x);+ relu78->set_friendly_name("Relu_78");+ x = relu78;++ x = make_conv_add(x, "Convolution_82", "Add_84", 0.014f, 0.001f);+ auto relu85 = std::make_shared<ov::op::v0::Relu>(x);+ relu85->set_friendly_name("Relu_85");+ x = relu85;++ x = make_conv_add(x, "Convolution_87", "Add_89", 0.015f, 0.001f);+ auto relu90 = std::make_shared<ov::op::v0::Relu>(x);+ relu90->set_friendly_name("Relu_90");+ x = relu90;++ x = make_conv_add(x, "Convolution_92", "Add_94", 0.016f, 0.001f);+ auto relu95 = std::make_shared<ov::op::v0::Relu>(x);+ relu95->set_friendly_name("Relu_95");+ x = relu95;++ auto multiplyScale = ov::op::v0::Constant::create(ov::element::f16, ov::Shape{1, 16, 1, 1}, {0.5f});+ auto multiply97 = std::make_shared<ov::op::v1::Multiply>(x, multiplyScale);+ multiply97->set_friendly_name("Multiply_97");++ auto add98 = std::make_shared<ov::op::v1::Add>(multiply97, multiply97);+ add98->set_friendly_name("Add_98");++ x = make_conv_add(add98, "Convolution_100", "Add_102", 0.017f, 0.001f);++ auto result = std::make_shared<ov::op::v0::Result>(x);+ result->set_friendly_name("Result_104");++ auto model = std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{input}, "CustomNet");++ // making input and output to be NHWC+ auto preProc = ov::preprocess::PrePostProcessor(model);+ preProc.input(0).tensor().set_layout("NHWC");+ preProc.input(0).model().set_layout("NCHW");+ preProc.output(0).tensor().set_layout("NHWC");+ preProc.output(0).model().set_layout("NCHW");++ model = preProc.build();++ return model;+}+using OVCompiledGraphImportExportTestNPU = OVCompiledGraphImportExportTest;TEST_P(OVCompiledGraphImportExportTestNPU, CanImportModelWithApplicationHeaderAndTensorAPI) {@@ -89,6 +180,44 @@ TEST_P(OVCompiledGraphImportExportTestNPU, CheckSizeOfRawBlobIfMultipleOfPageSizASSERT_TRUE(size % 4096 == 0) << "Size of the blob should be multiple of 4096";}+TEST_P(OVCompiledGraphImportExportTestNPU, NonELFBlobExportThrows) {+ ov::Core core;+ std::stringstream sstream;++ auto rawBlobConfig = configuration;+ rawBlobConfig.emplace(ov::intel_npu::export_raw_blob(true));+ rawBlobConfig.emplace(ov::intel_npu::compiler_type(ov::intel_npu::CompilerType::PLUGIN));++ {+ // WS+ std::stringstream modelIR, weights;+ ov::pass::Serialize serialzePass(+ modelIR,+ weights); // serialization needed to re-read the model with WeightlessCacheAttribute set+ auto model = ov::test::utils::make_conv_pool_relu();+ serialzePass.run_on_model(model);+ const auto& weightsStr = weights.str();+ ov::Tensor weightsTensor(ov::element::u8, ov::Shape{weightsStr.size()}, weightsStr.c_str());+ model = core.read_model(modelIR.str(), weightsTensor);++ rawBlobConfig.emplace(ov::enable_weightless(true));+ OV_EXPECT_THROW(core.compile_model(model, target_device, rawBlobConfig).export_model(sstream),+ ov::Exception,+ testing::HasSubstr("Requested raw blob export, but the graph is not a weightful ELF one."));+ rawBlobConfig.erase(ov::enable_weightless.name());+ }++ if (ov::intel_npu::Platform::standardize(ov::test::utils::getTestPlatform()) != ov::intel_npu::Platform::NPU3720) {+ // HostCompile+ auto model = createCustomNetModel();+ rawBlobConfig.emplace(ov::intel_npu::compilation_mode(HostCompile_Interpreter));+ OV_EXPECT_THROW(core.compile_model(model, target_device, rawBlobConfig).export_model(sstream),+ ov::Exception,+ testing::HasSubstr("Requested raw blob export, but the graph is not a weightful ELF one."));+ rawBlobConfig.erase(ov::intel_npu::compilation_mode.name());+ }+}+TEST_P(OVCompiledGraphImportExportTestNPU, CheckSizeOfExportedModelIfMultipleOfPageSize) {ov::Core core;std::stringstream sstream;