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
[NPU] Add InitWeights checks to import path (#39833)
Sorin Butnariuddf0d31205c0
diff --git a/src/plugins/intel_npu/src/backend/src/zero_infer_request.cpp b/src/plugins/intel_npu/src/backend/src/zero_infer_request.cppindex e4ce1e05fdd..8d8d4456577 100644--- a/src/plugins/intel_npu/src/backend/src/zero_infer_request.cpp+++ b/src/plugins/intel_npu/src/backend/src/zero_infer_request.cpp@@ -347,6 +347,12 @@ std::vector<ov::SoPtr<ov::IVariableState>> ZeroInferRequest::query_state() constvoid ZeroInferRequest::setup_pipeline() {_logger.debug("setup_pipeline - started");+ // Checked before allocating any buffer+ for (const IODescriptor& inputDescriptor : _metadata.inputs) {+ OPENVINO_ASSERT(!inputDescriptor.isInitInputWeights,+ "This path should not be used for running inferences for the \"init\" model");+ }+std::optional<size_t> batchSize;if (_graph->get_kind() != GraphKind::Dynamic) {batchSize = _graph->get_batch_size();@@ -1022,9 +1028,6 @@ void ZeroInferRequest::prepare_inputs() {for (const auto& userTensor : _userInputTensors) {const IODescriptor& inputDescriptor = _metadata.inputs.at(inputIndex);- OPENVINO_ASSERT(!inputDescriptor.isInitInputWeights,- "This path should not be used for running inferences for the \"init\" model");-if (inputDescriptor.isMainInputWeights) {// These values were set while running the "WeightlessGraph::init" methodcontinue;diff --git a/src/plugins/intel_npu/src/plugin/include/blob_format_importers.hpp b/src/plugins/intel_npu/src/plugin/include/blob_format_importers.hppindex a919994e1c6..192fea5a366 100644--- a/src/plugins/intel_npu/src/plugin/include/blob_format_importers.hpp+++ b/src/plugins/intel_npu/src/plugin/include/blob_format_importers.hpp@@ -135,4 +135,19 @@ std::unique_ptr<IBlobFormatImporter> create(BlobSource& npu_formatted_blob,} // namespace blob_format_importer_factory+namespace blob_format_importer_utils {++/**+ * @brief Checks that the I/O descriptors of the main schedule don't describe init schedule weights.+ * @details The init schedule weights descriptors belong to the init schedules only. Their flags are derived from the+ * argument names found in the blob, so an argument of the main schedule using such a name must be rejected. Otherwise,+ * it would be dropped from the imported model and treated as an init schedule buffer by the inference request.+ *+ * @param metadata The I/O descriptors of the main schedule.+ * @throws ov::Exception if at least one descriptor is marked as an init schedule weights descriptor.+ */+void validate_main_schedule_descriptors(const NetworkMetadata& metadata);++} // namespace blob_format_importer_utils+} // namespace intel_npudiff --git a/src/plugins/intel_npu/src/plugin/src/blob_format_importers.cpp b/src/plugins/intel_npu/src/plugin/src/blob_format_importers.cppindex 231d2ff3089..d32b228e454 100644--- a/src/plugins/intel_npu/src/plugin/src/blob_format_importers.cpp+++ b/src/plugins/intel_npu/src/plugin/src/blob_format_importers.cpp@@ -435,6 +435,7 @@ std::shared_ptr<IGraph> IBlobFormatImporter::create_graph(const ov::SoPtr<IEnginextract_blob_type());m_graph->update_network_name(network_name);+ blob_format_importer_utils::validate_main_schedule_descriptors(m_graph->get_metadata());if (m_batch_size.has_value() && m_batch_size.value() > 0) {// Initial batch setup for static casesm_graph->set_batch_size(m_batch_size.value());@@ -496,5 +497,22 @@ std::unique_ptr<IBlobFormatImporter> create(BlobSource& npu_formatted_blob,}} // namespace blob_format_importer_factory+namespace blob_format_importer_utils {++void validate_main_schedule_descriptors(const NetworkMetadata& metadata) {+ auto check_descriptors = [](const std::vector<IODescriptor>& descriptors) {+ for (const IODescriptor& descriptor : descriptors) {+ OPENVINO_ASSERT(!descriptor.isInitInputWeights && !descriptor.isInitOutputWeights,+ "The I/O descriptor \"",+ descriptor.nameFromCompiler,+ "\" of the main schedule is marked as an init schedule weights descriptor");+ }+ };++ check_descriptors(metadata.inputs);+ check_descriptors(metadata.outputs);+}++} // namespace blob_format_importer_utils} // namespace intel_npudiff --git a/src/plugins/intel_npu/tests/unit/npu/blob_format_importers.cpp b/src/plugins/intel_npu/tests/unit/npu/blob_format_importers.cppindex 2647878d547..824f1b07dda 100644--- a/src/plugins/intel_npu/tests/unit/npu/blob_format_importers.cpp+++ b/src/plugins/intel_npu/tests/unit/npu/blob_format_importers.cpp@@ -154,3 +154,41 @@ TEST_F(BlobFormatImportersTest, CannotCreateModelBeforeGraph) {OV_ASSERT_NO_THROW(importer = blob_format_importer_factory::create(source, true, nullptr, config));OV_EXPECT_THROW(importer->create_dummy_model(), ov::Exception, _);}++using blob_format_importer_utils::validate_main_schedule_descriptors;++namespace {++IODescriptor create_descriptor(const std::string& name, const ov::PartialShape& shape) {+ IODescriptor descriptor;+ descriptor.nameFromCompiler = name;+ descriptor.precision = ov::element::f32;+ descriptor.shapeFromCompiler = shape;+ return descriptor;+}++} // namespace+/**+ * @brief The init weights descriptors belong to the init schedules only, the main schedule must not contain any of+ * these regardless of their shapes or of the batch size+ */+TEST_F(BlobFormatImportersTest, MainScheduleRejectsInitWeightsDescriptors) {+ NetworkMetadata metadata;+ metadata.inputs.push_back(create_descriptor("input", ov::Shape{1, 3}));+ metadata.outputs.push_back(create_descriptor("output", ov::Shape{1, 3}));+ IODescriptor main_weights = create_descriptor("main_weights", ov::Shape{});+ main_weights.isMainInputWeights = true;+ metadata.inputs.push_back(std::move(main_weights));+ OV_ASSERT_NO_THROW(validate_main_schedule_descriptors(metadata));++ IODescriptor init_input = create_descriptor("init_input", ov::Shape{1, 3});+ init_input.isInitInputWeights = true;+ metadata.inputs.push_back(std::move(init_input));+ OV_EXPECT_THROW_HAS_SUBSTRING(validate_main_schedule_descriptors(metadata), ov::Exception, "init_input");++ metadata.inputs.pop_back();+ IODescriptor init_output = create_descriptor("init_output", ov::Shape{1, 3});+ init_output.isInitOutputWeights = true;+ metadata.outputs.push_back(std::move(init_output));+ OV_EXPECT_THROW_HAS_SUBSTRING(validate_main_schedule_descriptors(metadata), ov::Exception, "init_output");+}