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] Small refactoring: CompiledModel::get_runtime_model() (#31908)
Razvan Apetroaiec67f69393675
diff --git a/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/src/compiled_model.cppindex 4e29dce910f..8475ebd6b2f 100644--- a/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp+++ b/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp@@ -18,12 +18,6 @@#include "openvino/runtime/threading/executor_manager.hpp"#include "transformations/utils/utils.hpp"-namespace {--const std::vector<size_t> CONSTANT_NODE_DUMMY_SHAPE{1};--}-namespace intel_npu {using intel_npu::envVarStrToBool;@@ -97,47 +91,46 @@ void CompiledModel::export_model(std::ostream& stream) const {std::shared_ptr<const ov::Model> CompiledModel::get_runtime_model() const {ov::ParameterVector parameters;ov::ResultVector results;-- for (const IODescriptor& inputDescriptor : _graph->get_metadata().inputs) {- if (inputDescriptor.isStateInput || inputDescriptor.isStateOutput || inputDescriptor.isShapeTensor) {- continue;+ std::shared_ptr<const ov::Model> dummyModel;++ try {+ for (const ov::Output<const ov::Node>& nodeOutput : inputs()) {+ std::shared_ptr<ov::Node> clonedParameter =+ std::dynamic_pointer_cast<const ov::op::v0::Parameter>(nodeOutput.get_node_shared_ptr())+ ->clone_with_new_inputs({});+ parameters.push_back(std::dynamic_pointer_cast<ov::op::v0::Parameter>(clonedParameter));}- std::shared_ptr<ov::op::v0::Parameter> parameter =- std::make_shared<ov::op::v0::Parameter>(inputDescriptor.precision, inputDescriptor.shapeFromCompiler);-- parameter->set_friendly_name(inputDescriptor.nodeFriendlyName);- parameter->output(0).get_tensor().set_names(inputDescriptor.outputTensorNames);- 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 (const IODescriptor& outputDescriptor : _graph->get_metadata().outputs) {- if (outputDescriptor.isStateInput || outputDescriptor.isStateOutput || outputDescriptor.isShapeTensor) {- continue;+ for (const ov::Output<const ov::Node>& nodeOutput : outputs()) {+ const auto resultOriginal =+ std::dynamic_pointer_cast<const ov::op::v0::Result>(nodeOutput.get_node_shared_ptr());++ // A dummy node is required for constructing and populating the Result node. A Constant one is perhaps the+ // most fitting choice here.+ std::shared_ptr<ov::Node> constantDummy =+ std::make_shared<ov::op::v0::Constant>(nodeOutput.get_element_type(),+ nodeOutput.get_partial_shape().get_max_shape());+ // Attached to the Result node as output tensor in order to provide the correct tensor names. Additionally,+ // the dummy Constant node could use only static shapes. If the shape is dynamic, this construct can provide+ // the correct shape to the Result node.+ const std::shared_ptr<ov::descriptor::Tensor>& tensorDummy =+ std::make_shared<ov::descriptor::Tensor>(nodeOutput.get_element_type(),+ nodeOutput.get_partial_shape(),+ nodeOutput.get_names());++ auto& resultCopy = results.emplace_back(std::make_shared<ov::op::v0::Result>(constantDummy));+ resultCopy->output(0).set_tensor_ptr(tensorDummy);+ resultCopy->set_friendly_name(resultOriginal->get_friendly_name());++ dummyModel = std::make_shared<ov::Model>(results, parameters);}-- std::shared_ptr<ov::Node> constantDummy = std::make_shared<ov::op::v0::Constant>(- outputDescriptor.precision,- outputDescriptor.shapeFromCompiler.to_shape().empty() ? CONSTANT_NODE_DUMMY_SHAPE- : outputDescriptor.shapeFromCompiler.to_shape());-- const std::shared_ptr<ov::descriptor::Tensor>& tensorDummy =- std::make_shared<ov::descriptor::Tensor>(outputDescriptor.precision,- outputDescriptor.shapeFromCompiler,- outputDescriptor.outputTensorNames);-- auto& result = results.emplace_back(std::make_shared<ov::op::v0::Result>(constantDummy));- result->output(0).set_tensor_ptr(tensorDummy);- result->set_friendly_name(outputDescriptor.nodeFriendlyName);+ } catch (const std::exception& e) {+ OPENVINO_THROW("Failed to construct a dummy ov::Model object as runtime model. ", e.what());}_logger.warning("Returning a dummy ov::Model object that contains only the given parameter and result nodes");- return std::make_shared<ov::Model>(results, parameters);+ return dummyModel;}void CompiledModel::set_property(const ov::AnyMap& properties) {