Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[NPU] Merge driver and plugin graphs into one graph (#30324)

Bogdan Pereanuabb27e6578fe
diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/driver_graph.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/graph.hpp
similarity index 67%
rename from src/plugins/intel_npu/src/compiler_adapter/include/driver_graph.hpp
rename to src/plugins/intel_npu/src/compiler_adapter/include/graph.hpp
index ac89a790291..272204ea7b1 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/include/driver_graph.hpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/include/graph.hpp
@@ -9,19 +9,22 @@
#include <ze_graph_ext.h>
#include "intel_npu/common/igraph.hpp"
+#include "intel_npu/icompiler.hpp"
#include "intel_npu/utils/zero/zero_init.hpp"
+#include "openvino/runtime/so_ptr.hpp"
#include "ze_graph_ext_wrappers.hpp"
namespace intel_npu {
-class DriverGraph final : public IGraph {
+class Graph final : public IGraph {
public:
- DriverGraph(const std::shared_ptr<ZeGraphExtWrappers>& zeGraphExt,
- const std::shared_ptr<ZeroInitStructsHolder>& zeroInitStruct,
- ze_graph_handle_t graphHandle,
- NetworkMetadata metadata,
- const Config& config,
- std::unique_ptr<BlobContainer> blobPtr);
+ Graph(const std::shared_ptr<ZeGraphExtWrappers>& zeGraphExt,
+ const std::shared_ptr<ZeroInitStructsHolder>& zeroInitStruct,
+ ze_graph_handle_t graphHandle,
+ NetworkMetadata metadata,
+ std::unique_ptr<BlobContainer> blobPtr,
+ const Config& config,
+ const ov::SoPtr<ICompiler>& compiler = {nullptr});
size_t export_blob(std::ostream& stream) const override;
@@ -32,7 +35,7 @@ public:
void initialize(const Config& config) override;
- ~DriverGraph() override;
+ ~Graph() override;
private:
bool release_blob(const Config& config);
@@ -40,6 +43,8 @@ private:
std::shared_ptr<ZeGraphExtWrappers> _zeGraphExt;
std::shared_ptr<ZeroInitStructsHolder> _zeroInitStruct;
+ const ov::SoPtr<ICompiler> _compiler;
+
Logger _logger;
// In the case of the import path, the blob is released after graph initialization so it can not be any longer
diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/plugin_graph.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/plugin_graph.hpp
deleted file mode 100644
index 61d4a6ed866..00000000000
--- a/src/plugins/intel_npu/src/compiler_adapter/include/plugin_graph.hpp
+++ /dev/null
@@ -1,49 +0,0 @@
-// Copyright (C) 2018-2025 Intel Corporation
-// SPDX-License-Identifier: Apache-2.0
-//
-
-// Compiler Interface
-
-#pragma once
-
-#include <ze_graph_ext.h>
-
-#include "intel_npu/common/igraph.hpp"
-#include "intel_npu/icompiler.hpp"
-#include "intel_npu/utils/zero/zero_init.hpp"
-#include "openvino/runtime/so_ptr.hpp"
-#include "ze_graph_ext_wrappers.hpp"
-
-namespace intel_npu {
-
-class PluginGraph final : public IGraph {
-public:
- PluginGraph(const std::shared_ptr<ZeGraphExtWrappers>& zeGraphExt,
- const ov::SoPtr<ICompiler>& compiler,
- const std::shared_ptr<ZeroInitStructsHolder>& zeroInitStruct,
- ze_graph_handle_t graphHandle,
- NetworkMetadata metadata,
- std::unique_ptr<BlobContainer> blobPtr,
- const Config& config);
-
- size_t export_blob(std::ostream& stream) const override;
-
- std::vector<ov::ProfilingInfo> process_profiling_output(const std::vector<uint8_t>& profData,
- const Config& config) const override;
-
- void set_argument_value(uint32_t argi, const void* argv) const override;
-
- void initialize(const Config& config) override;
-
- ~PluginGraph() override;
-
-private:
- std::shared_ptr<ZeGraphExtWrappers> _zeGraphExt;
- std::shared_ptr<ZeroInitStructsHolder> _zeroInitStruct;
-
- const ov::SoPtr<ICompiler> _compiler;
-
- Logger _logger;
-};
-
-} // namespace intel_npu
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/driver_compiler_adapter.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/driver_compiler_adapter.cpp
index 6c7825b585c..12c810f6ce4 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/driver_compiler_adapter.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/driver_compiler_adapter.cpp
@@ -7,7 +7,7 @@
#include <regex>
#include <string_view>
-#include "driver_graph.hpp"
+#include "graph.hpp"
#include "intel_npu/common/filtered_config.hpp"
#include "intel_npu/common/itt.hpp"
#include "intel_npu/config/options.hpp"
@@ -191,12 +191,7 @@ std::shared_ptr<IGraph> DriverCompilerAdapter::compile(const std::shared_ptr<con
auto networkMeta = _zeGraphExt->getNetworkMeta(graphHandle);
networkMeta.name = model->get_friendly_name();
- return std::make_shared<DriverGraph>(_zeGraphExt,
- _zeroInitStruct,
- graphHandle,
- std::move(networkMeta),
- config,
- nullptr);
+ return std::make_shared<Graph>(_zeGraphExt, _zeroInitStruct, graphHandle, std::move(networkMeta), nullptr, config);
}
std::shared_ptr<IGraph> DriverCompilerAdapter::parse(std::unique_ptr<BlobContainer> blobPtr,
@@ -211,12 +206,12 @@ std::shared_ptr<IGraph> DriverCompilerAdapter::parse(std::unique_ptr<BlobContain
OV_ITT_TASK_NEXT(PARSE_BLOB, "getNetworkMeta");
auto networkMeta = _zeGraphExt->getNetworkMeta(graphHandle);
- return std::make_shared<DriverGraph>(_zeGraphExt,
- _zeroInitStruct,
- graphHandle,
- std::move(networkMeta),
- config,
- std::move(blobPtr));
+ return std::make_shared<Graph>(_zeGraphExt,
+ _zeroInitStruct,
+ graphHandle,
+ std::move(networkMeta),
+ std::move(blobPtr),
+ config);
}
ov::SupportedOpsMap DriverCompilerAdapter::query(const std::shared_ptr<const ov::Model>& model,
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/driver_graph.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp
similarity index 77%
rename from src/plugins/intel_npu/src/compiler_adapter/src/driver_graph.cpp
rename to src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp
index fc2fe028af8..b3ac3409ea4 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/driver_graph.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp
@@ -2,27 +2,25 @@
// SPDX-License-Identifier: Apache-2.0
//
-#include "driver_graph.hpp"
+#include "graph.hpp"
#include "intel_npu/config/options.hpp"
#include "intel_npu/utils/zero/zero_api.hpp"
namespace intel_npu {
-DriverGraph::DriverGraph(const std::shared_ptr<ZeGraphExtWrappers>& zeGraphExt,
- const std::shared_ptr<ZeroInitStructsHolder>& zeroInitStruct,
- ze_graph_handle_t graphHandle,
- NetworkMetadata metadata,
- const Config& config,
- std::unique_ptr<BlobContainer> blobPtr)
+Graph::Graph(const std::shared_ptr<ZeGraphExtWrappers>& zeGraphExt,
+ const std::shared_ptr<ZeroInitStructsHolder>& zeroInitStruct,
+ ze_graph_handle_t graphHandle,
+ NetworkMetadata metadata,
+ std::unique_ptr<BlobContainer> blobPtr,
+ const Config& config,
+ const ov::SoPtr<ICompiler>& compiler)
: IGraph(graphHandle, std::move(metadata), config, std::move(blobPtr)),
_zeGraphExt(zeGraphExt),
_zeroInitStruct(zeroInitStruct),
- _logger("DriverGraph", config.get<LOG_LEVEL>()) {
- if (_zeGraphExt == nullptr) {
- OPENVINO_THROW("Zero compiler adapter wasn't initialized");
- }
-
+ _compiler(compiler),
+ _logger("Graph", config.get<LOG_LEVEL>()) {
if (!config.get<CREATE_EXECUTOR>() || config.get<DEFER_WEIGHTS_LOAD>()) {
_logger.info("Graph initialize is deferred from the \"Graph\" constructor");
return;
@@ -31,7 +29,7 @@ DriverGraph::DriverGraph(const std::shared_ptr<ZeGraphExtWrappers>& zeGraphExt,
initialize(config);
}
-size_t DriverGraph::export_blob(std::ostream& stream) const {
+size_t Graph::export_blob(std::ostream& stream) const {
const uint8_t* blobPtr = nullptr;
size_t blobSize;
std::vector<uint8_t> blob;
@@ -40,7 +38,12 @@ size_t DriverGraph::export_blob(std::ostream& stream) const {
OPENVINO_THROW("Model was imported (not compiled) by the plugin. Model export is forbidden in this case!");
}
- _zeGraphExt->getGraphBinary(_handle, blob, blobPtr, blobSize);
+ if (_blobPtr == nullptr) { // when compiling the model using Compiler in Driver, the blob is handled by the driver
+ _zeGraphExt->getGraphBinary(_handle, blob, blobPtr, blobSize);
+ } else { // in all other cases, the blob is handled by the plugin
+ blobPtr = static_cast<const uint8_t*>(_blobPtr->get_ptr());
+ blobSize = _blobPtr->size();
+ }
stream.write(reinterpret_cast<const char*>(blobPtr), blobSize);
@@ -63,18 +66,32 @@ size_t DriverGraph::export_blob(std::ostream& stream) const {
return blobSize;
}
-std::vector<ov::ProfilingInfo> DriverGraph::process_profiling_output(const std::vector<uint8_t>& profData,
- const Config& config) const {
- OPENVINO_THROW("Profiling post-processing is not supported.");
+std::vector<ov::ProfilingInfo> Graph::process_profiling_output(const std::vector<uint8_t>& profData,
+ const Config& config) const {
+ if (_compiler == nullptr) {
+ OPENVINO_THROW("Profiling post-processing is not supported.");
+ }
+
+ std::vector<uint8_t> blob(_blobPtr->size());
+ blob.assign(reinterpret_cast<const uint8_t*>(_blobPtr->get_ptr()),
+ reinterpret_cast<const uint8_t*>(_blobPtr->get_ptr()) + _blobPtr->size());
+ return _compiler->process_profiling_output(profData, blob, config);
}
-void DriverGraph::set_argument_value(uint32_t argi, const void* argv) const {
+void Graph::set_argument_value(uint32_t argi, const void* argv) const {
+ if (_zeGraphExt == nullptr) {
+ OPENVINO_THROW("Zero compiler adapter wasn't initialized");
+ }
_zeGraphExt->setGraphArgumentValue(_handle, argi, argv);
}
-void DriverGraph::initialize(const Config& config) {
+void Graph::initialize(const Config& config) {
_logger.debug("Graph initialize start");
+ if (_zeGraphExt == nullptr || _handle == nullptr) {
+ return;
+ }
+
_logger.debug("performing pfnGetProperties");
ze_graph_properties_t props{};
props.stype = ZE_STRUCTURE_TYPE_GRAPH_PROPERTIES;
@@ -144,7 +161,7 @@ void DriverGraph::initialize(const Config& config) {
}
}
-bool DriverGraph::release_blob(const Config& config) {
+bool Graph::release_blob(const Config& config) {
if (_blobPtr == nullptr || _zeroInitStruct->getGraphDdiTable().version() < ZE_GRAPH_EXT_VERSION_1_8 ||
config.get<PERF_COUNT>()) {
return false;
@@ -167,7 +184,7 @@ bool DriverGraph::release_blob(const Config& config) {
return true;
};
-DriverGraph::~DriverGraph() {
+Graph::~Graph() {
// make sure all the context-dependent components are destroyed before the zero context is destroyed
if (_handle != nullptr) {
auto result = _zeGraphExt->destroyGraph(_handle);
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/plugin_compiler_adapter.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/plugin_compiler_adapter.cpp
index 44cdf882344..0dc23d261d5 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/plugin_compiler_adapter.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/plugin_compiler_adapter.cpp
@@ -7,6 +7,7 @@
#include <memory>
#include <string>
+#include "graph.hpp"
#include "intel_npu/common/device_helpers.hpp"
#include "intel_npu/common/itt.hpp"
#include "intel_npu/config/options.hpp"
@@ -14,9 +15,9 @@
#include "intel_npu/utils/logger/logger.hpp"
#include "intel_npu/utils/zero/zero_api.hpp"
#include "intel_npu/utils/zero/zero_result.hpp"
+#include "openvino/runtime/make_tensor.hpp"
#include "openvino/util/file_util.hpp"
#include "openvino/util/shared_object.hpp"
-#include "plugin_graph.hpp"
namespace {
std::shared_ptr<void> loadLibrary(const std::string& libpath) {
@@ -80,13 +81,27 @@ std::shared_ptr<IGraph> PluginCompilerAdapter::compile(const std::shared_ptr<con
_logger.debug("compile start");
auto networkDesc = _compiler->compile(model, config);
- auto blobPtr = std::make_unique<BlobContainerVector>(std::move(networkDesc.compiledNetwork));
_logger.debug("compile end");
+ auto tensor =
+ ov::Tensor(ov::element::u8, ov::Shape{networkDesc.compiledNetwork.size()}, networkDesc.compiledNetwork.data());
+ auto impl = ov::get_tensor_impl(tensor);
+ std::shared_ptr<std::vector<uint8_t>> sharedCompiledNetwork =
+ std::make_shared<std::vector<uint8_t>>(std::move(networkDesc.compiledNetwork));
+ impl._so = std::move(sharedCompiledNetwork);
+ tensor = ov::make_tensor(impl);
+
+ auto modelBuffer = std::make_shared<ov::SharedBuffer<ov::Tensor>>(reinterpret_cast<char*>(tensor.data()),
+ tensor.get_byte_size(),
+ tensor);
+
+ auto blobPtr = std::make_unique<BlobContainerAlignedBuffer>(modelBuffer, 0, tensor.get_byte_size());
+
ze_graph_handle_t graphHandle = nullptr;
if (_zeGraphExt) {
- // Depending on the config, we may get an error when trying to get the graph handle from the compiled network
+ // Depending on the config, we may get an error when trying to get the graph handle from the compiled
+ // network
try {
graphHandle =
_zeGraphExt->getGraphHandle(*reinterpret_cast<const uint8_t*>(blobPtr->get_ptr()), blobPtr->size());
@@ -96,13 +111,13 @@ std::shared_ptr<IGraph> PluginCompilerAdapter::compile(const std::shared_ptr<con
}
}
- return std::make_shared<PluginGraph>(_zeGraphExt,
- _compiler,
- _zeroInitStruct,
- graphHandle,
- std::move(networkDesc.metadata),
- std::move(blobPtr),
- config);
+ return std::make_shared<Graph>(_zeGraphExt,
+ _zeroInitStruct,
+ graphHandle,
+ std::move(networkDesc.metadata),
+ std::move(blobPtr),
+ config,
+ _compiler);
}
std::shared_ptr<IGraph> PluginCompilerAdapter::parse(std::unique_ptr<BlobContainer> blobPtr,
@@ -125,13 +140,13 @@ std::shared_ptr<IGraph> PluginCompilerAdapter::parse(std::unique_ptr<BlobContain
_zeGraphExt->getGraphHandle(*reinterpret_cast<const uint8_t*>(blobPtr->get_ptr()), blobPtr->size());
}
- return std::make_shared<PluginGraph>(_zeGraphExt,
- _compiler,
- _zeroInitStruct,
- graphHandle,
- std::move(networkMeta),
- std::move(blobPtr),
- config);
+ return std::make_shared<Graph>(_zeGraphExt,
+ _zeroInitStruct,
+ graphHandle,
+ std::move(networkMeta),
+ std::move(blobPtr),
+ config,
+ _compiler);
}
ov::SupportedOpsMap PluginCompilerAdapter::query(const std::shared_ptr<const ov::Model>& model,
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/plugin_graph.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/plugin_graph.cpp
deleted file mode 100644
index 56139f6a4c6..00000000000
--- a/src/plugins/intel_npu/src/compiler_adapter/src/plugin_graph.cpp
+++ /dev/null
@@ -1,161 +0,0 @@
-// Copyright (C) 2018-2025 Intel Corporation
-// SPDX-License-Identifier: Apache-2.0
-//
-
-#include "plugin_graph.hpp"
-
-#include "intel_npu/config/options.hpp"
-#include "intel_npu/utils/zero/zero_api.hpp"
-
-namespace intel_npu {
-
-PluginGraph::PluginGraph(const std::shared_ptr<ZeGraphExtWrappers>& zeGraphExt,
- const ov::SoPtr<ICompiler>& compiler,
- const std::shared_ptr<ZeroInitStructsHolder>& zeroInitStruct,
- ze_graph_handle_t graphHandle,
- NetworkMetadata metadata,
- std::unique_ptr<BlobContainer> blobPtr,
- const Config& config)
- : IGraph(graphHandle, std::move(metadata), config, std::move(blobPtr)),
- _zeGraphExt(zeGraphExt),
- _zeroInitStruct(zeroInitStruct),
- _compiler(compiler),
- _logger("PluginGraph", config.get<LOG_LEVEL>()) {
- if (!config.get<CREATE_EXECUTOR>() || config.get<DEFER_WEIGHTS_LOAD>()) {
- _logger.info("Graph initialize is deferred from the \"Graph\" constructor");
- return;
- }
-
- initialize(config);
-}
-
-size_t PluginGraph::export_blob(std::ostream& stream) const {
- stream.write(reinterpret_cast<const char*>(_blobPtr->get_ptr()), _blobPtr->size());
-
- if (!stream) {
- _logger.error("Write blob to stream failed. Blob is broken!");
- return 0;
- }
-
- if (_logger.level() >= ov::log::Level::INFO) {
- std::uint32_t result = 1171117u;
- for (const uint8_t* it = reinterpret_cast<const uint8_t*>(_blobPtr->get_ptr());
- it != reinterpret_cast<const uint8_t*>(_blobPtr->get_ptr()) + _blobPtr->size();
- ++it) {
- result = ((result << 7) + result) + static_cast<uint32_t>(*it);
- }
-
- std::stringstream str;
- str << "Blob size: " << _blobPtr->size() << ", hash: " << std::hex << result;
- _logger.info(str.str().c_str());
- }
- _logger.info("Write blob to stream successfully.");
- return _blobPtr->size();
-}
-
-std::vector<ov::ProfilingInfo> PluginGraph::process_profiling_output(const std::vector<uint8_t>& profData,
- const Config& config) const {
- std::vector<uint8_t> blob(_blobPtr->size());
- blob.assign(reinterpret_cast<const uint8_t*>(_blobPtr->get_ptr()),
- reinterpret_cast<const uint8_t*>(_blobPtr->get_ptr()) + _blobPtr->size());
- return _compiler->process_profiling_output(profData, blob, config);
-}
-
-void PluginGraph::set_argument_value(uint32_t argi, const void* argv) const {
- if (_zeGraphExt == nullptr) {
- OPENVINO_THROW("Zero compiler adapter wasn't initialized");
- }
- _zeGraphExt->setGraphArgumentValue(_handle, argi, argv);
-}
-
-void PluginGraph::initialize(const Config& config) {
- if (_zeGraphExt == nullptr || _handle == nullptr) {
- return;
- }
-
- _logger.debug("Graph initialize start");
-
- _logger.debug("performing pfnGetProperties");
- ze_graph_properties_t props{};
- props.stype = ZE_STRUCTURE_TYPE_GRAPH_PROPERTIES;
- auto result = _zeroInitStruct->getGraphDdiTable().pfnGetProperties(_handle, &props);
- THROW_ON_FAIL_FOR_LEVELZERO_EXT("pfnGetProperties", result, _zeroInitStruct->getGraphDdiTable());
-
- _logger.debug("performing pfnGetArgumentProperties3");
- for (uint32_t index = 0; index < props.numGraphArgs; ++index) {
- ze_graph_argument_properties_3_t arg3{};
- arg3.stype = ZE_STRUCTURE_TYPE_GRAPH_ARGUMENT_PROPERTIES;
- auto result = _zeroInitStruct->getGraphDdiTable().pfnGetArgumentProperties3(_handle, index, &arg3);
- THROW_ON_FAIL_FOR_LEVELZERO_EXT("pfnGetArgumentProperties3", result, _zeroInitStruct->getGraphDdiTable());
-
- if (arg3.type == ZE_GRAPH_ARGUMENT_TYPE_INPUT) {
- _input_descriptors.push_back(ArgumentDescriptor{arg3, index});
- } else {
- _output_descriptors.push_back(ArgumentDescriptor{arg3, index});
- }
- }
-
- _input_descriptors.shrink_to_fit();
- _output_descriptors.shrink_to_fit();
-
- _command_queue_group_ordinal =
- zeroUtils::findCommandQueueGroupOrdinal(_zeroInitStruct->getDevice(),
- ZE_COMMAND_QUEUE_GROUP_PROPERTY_FLAG_COMPUTE);
-
- uint32_t command_queue_options = 0;
-
- if (config.has<TURBO>() && config.get<TURBO>()) {
- if (_zeroInitStruct->getCommandQueueDdiTable().version() < ZE_MAKE_VERSION(1, 0)) {
- OPENVINO_THROW("Turbo is not supported by the current driver");
- }
- command_queue_options = command_queue_options | ZE_NPU_COMMAND_QUEUE_OPTION_TURBO;
- }
-
- if (_zeroInitStruct->getCommandQueueDdiTable().version() >= ZE_MAKE_VERSION(1, 1) &&
- config.has<RUN_INFERENCES_SEQUENTIALLY>() && config.get<RUN_INFERENCES_SEQUENTIALLY>()) {
- command_queue_options = command_queue_options | ZE_NPU_COMMAND_QUEUE_OPTION_DEVICE_SYNC;
- }
-
- _command_queue = std::make_shared<CommandQueue>(_zeroInitStruct,
- zeroUtils::toZeQueuePriority(config.get<MODEL_PRIORITY>()),
- _command_queue_group_ordinal,
- command_queue_options);
-
- if (config.has<WORKLOAD_TYPE>()) {
- set_workload_type(config.get<WORKLOAD_TYPE>());
- }
-
- _zeGraphExt->initializeGraph(_handle, _command_queue_group_ordinal);
-
- _batch_size = get_batch_size(_metadata);
-
- if (_zeroInitStruct->getCommandQueueDdiTable().version() < ZE_MAKE_VERSION(1, 1) &&
- config.get<RUN_INFERENCES_SEQUENTIALLY>()) {
- auto number_of_command_lists = _batch_size.has_value() ? *_batch_size : 1;
-
- _last_submitted_event.resize(number_of_command_lists);
- }
-
- _logger.debug("Graph initialize finish");
-}
-
-PluginGraph::~PluginGraph() {
- // make sure all the context-dependent components are destroyed before the zero context is destroyed
- if (_handle != nullptr) {
- auto result = _zeGraphExt->destroyGraph(_handle);
-
- if (ZE_RESULT_SUCCESS == result) {
- _handle = nullptr;
- }
- }
-
- if (_last_submitted_event.size()) {
- _last_submitted_event.clear();
- }
-
- if (_command_queue != nullptr) {
- _command_queue.reset();
- }
-}
-
-} // namespace intel_npu