diff --git a/src/plugins/intel_npu/src/plugin/include/compiled_model.hpp b/src/plugins/intel_npu/src/plugin/include/compiled_model.hpp index 7d1c7fc71a5..083e41072ac 100644 --- a/src/plugins/intel_npu/src/plugin/include/compiled_model.hpp +++ b/src/plugins/intel_npu/src/plugin/include/compiled_model.hpp @@ -15,12 +15,42 @@ namespace intel_npu { class CompiledModel final : public ICompiledModel { public: - explicit CompiledModel(const std::shared_ptr& model, - const std::shared_ptr& plugin, - const std::shared_ptr& networkDescription, - const std::shared_ptr& device, - const std::optional>& compiler, - const Config& config); + /** + * @brief The constructor used by the "Plugin::compile_model" method. + * @note The compilation step has been placed inside this constructor instead of the originating call. This choice + * was motivated by the possibility of modifying the I/O identifiers via these passes which could potentially lead + * to bugs. + * @param model The IR of the model to be compiled + * @param plugin Pointer towards the NPU plugin instance + * @param device Backend specific object through which inference requests can be created + * @param compiler Module used for compiling the IR model. + * @param profiling Flag indicating if profiling was requested. Setting this to "true" will lead to storing the + * "compiler" parameter inside the newly created "CompiledModel". + * @param config Custom configuration object + */ + CompiledModel(const std::shared_ptr& model, + const std::shared_ptr& plugin, + const std::shared_ptr& device, + const ov::SoPtr& compiler, + const bool profiling, + const Config& config); + + /** + * @brief The constructor used by the "Plugin::import_model" method. + * @param model The IR of the already compiled model + * @param plugin Pointer towards the NPU plugin instance + * @param networkDescription Object holding the compiled model within a buffer along with distinct fields for its + * metadata + * @param device Backend specific object through which inference requests can be created + * @param compiler If set, the module will be stored inside the newly created "CompiledModel" + * @param config Custom configuration object + */ + CompiledModel(const std::shared_ptr& model, + const std::shared_ptr& plugin, + const std::shared_ptr& networkDescription, + const std::shared_ptr& device, + const std::optional>& compiler, + const Config& config); CompiledModel(const CompiledModel&) = delete; @@ -49,6 +79,8 @@ private: void configure_stream_executors(); + void create_executor(); + std::shared_ptr _networkPtr; const std::shared_ptr _model; const Config _config; diff --git a/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp index d535c0117c0..3998995d638 100644 --- a/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp +++ b/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp @@ -40,6 +40,40 @@ namespace intel_npu { using intel_npu::envVarStrToBool; +CompiledModel::CompiledModel(const std::shared_ptr& model, + const std::shared_ptr& plugin, + const std::shared_ptr& device, + const ov::SoPtr& compiler, + const bool profiling, + const Config& config) + : ICompiledModel(model, plugin), + _model(model), + _config(config), + _logger("CompiledModel", config.get()), + _device(device), + _compiler(profiling ? std::optional(compiler) : std::nullopt) { + OV_ITT_SCOPED_TASK(itt::domains::NPUPlugin, "CompiledModel::CompiledModel"); + OPENVINO_ASSERT(compiler != nullptr, "NPU CompiledModel: the pointer towards the compiler object is null"); + + try { + _networkPtr = std::make_shared(compiler->compile(model, config)); + } catch (const std::exception& ex) { + OPENVINO_THROW(ex.what()); + } catch (...) { + _logger.error("Unexpected exception"); + OPENVINO_THROW("NPU CompiledModel: got an unexpected exception from compiler"); + } + + OV_ITT_TASK_CHAIN(COMPILED_MODEL, itt::domains::NPUPlugin, "CompiledModel::CompiledModel", "initialize_properties"); + initialize_properties(); + configure_stream_executors(); + + OV_ITT_TASK_NEXT(COMPILED_MODEL, "create_executor"); + create_executor(); + + OV_ITT_TASK_SKIP(COMPILED_MODEL); +} + CompiledModel::CompiledModel(const std::shared_ptr& model, const std::shared_ptr& plugin, const std::shared_ptr& networkDescription, @@ -54,32 +88,15 @@ CompiledModel::CompiledModel(const std::shared_ptr& model, _device(device), _compiler(compiler) { OV_ITT_SCOPED_TASK(itt::domains::NPUPlugin, "CompiledModel::CompiledModel"); - - if (_networkPtr == nullptr) { - OPENVINO_THROW("Network is null!"); - } + OPENVINO_ASSERT(_networkPtr != nullptr, + "NPU CompiledModel: the pointer towards the NetworkDescription object is null"); OV_ITT_TASK_CHAIN(COMPILED_MODEL, itt::domains::NPUPlugin, "CompiledModel::CompiledModel", "initialize_properties"); initialize_properties(); configure_stream_executors(); OV_ITT_TASK_NEXT(COMPILED_MODEL, "create_executor"); - const bool configCreateExecutor = _config.get(); - static const auto envVar = std::getenv("IE_NPU_CREATE_EXECUTOR"); - const bool IE_NPU_CREATE_EXECUTOR = - envVar ? envVarStrToBool("IE_NPU_CREATE_EXECUTOR", envVar) : configCreateExecutor; - - if (IE_NPU_CREATE_EXECUTOR) { - _logger.info("Creating the executor inside the \"CompiledModel\" constructor"); - - // If no device has been defined, the executor shall keep the default value of "nullptr". In this scenario, - // only export operations will be allowed - if (_device != nullptr) { - _executorPtr = _device->createExecutor(_networkPtr, _config); - } - } else { - _logger.info("Executor will not be created inside the \"CompiledModel\" constructor"); - } + create_executor(); OV_ITT_TASK_SKIP(COMPILED_MODEL); } @@ -323,4 +340,18 @@ void CompiledModel::initialize_properties() { } } +void CompiledModel::create_executor() { + if (_config.get()) { + _logger.info("Creating the executor inside the \"CompiledModel\" constructor"); + + // If no device has been defined, the executor shall keep the default value of "nullptr". In this scenario, + // only export operations will be allowed + if (_device != nullptr) { + _executorPtr = _device->createExecutor(_networkPtr, _config); + } + } else { + _logger.info("Executor will not be created inside the \"CompiledModel\" constructor"); + } +} + } // namespace intel_npu diff --git a/src/plugins/intel_npu/src/plugin/src/plugin.cpp b/src/plugins/intel_npu/src/plugin/src/plugin.cpp index dbec9c4ae69..e2e895019d1 100644 --- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp +++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp @@ -571,28 +571,16 @@ std::shared_ptr Plugin::compile_model(const std::shared_ptr< OV_ITT_TASK_NEXT(PLUGIN_COMPILE_MODEL, "compile"); - std::shared_ptr networkDescription; std::shared_ptr compiledModel; - ov::SoPtr compiler; - try { - compiler = getCompiler(localConfig); - networkDescription = std::make_shared(compiler->compile(model, localConfig)); - } catch (const std::exception& ex) { - OPENVINO_THROW(ex.what()); - } catch (...) { - _logger.error("Unexpected exception"); - OPENVINO_THROW("NPU ExecutableNetwork got unexpected exception from compiler"); - } - try { bool profiling = localConfig.get(); compiledModel = std::make_shared(model, shared_from_this(), - networkDescription, device, - profiling ? std::optional(compiler) : std::nullopt, + getCompiler(localConfig), + profiling, localConfig); } catch (const std::exception& ex) { OPENVINO_THROW(ex.what());