Skip to content

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

[NPU] Move graph property queries into ZeGraphExtWrappers (#38601)

Patrik Stepaned4493b1a4ef
diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/ze_graph_ext_wrappers.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/ze_graph_ext_wrappers.hpp
index c5f963ac7ca..927fd95effa 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/include/ze_graph_ext_wrappers.hpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/include/ze_graph_ext_wrappers.hpp
@@ -88,6 +88,20 @@ public:
bool isBlobDataImported(const GraphDescriptor& graphDescriptor) const;
+ /**
+ * @brief Tells whether the graph is known to require the initialize stage.
+ * @return `true` only when the driver states the requirement. Anything that leaves it
+ * unestablished - no graph handle, a driver too old to express it, or a failed
+ * query - reports `false`.
+ */
+ bool isInitStageRequired(const GraphDescriptor& graphDescriptor) const;
+
+ /**
+ * @brief Tells whether the graph was compiled with layer profiling enabled.
+ * @return `std::nullopt` if the driver is too old to answer.
+ */
+ std::optional<bool> isProfilingEnabled(const GraphDescriptor& graphDescriptor) const;
+
void evict_memory(const GraphDescriptor& graphDescriptor) const;
/**
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp
index f5df09744d0..c5854470883 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/graph.cpp
@@ -10,7 +10,6 @@
#include "intel_npu/config/options.hpp"
#include "intel_npu/utils/utils.hpp"
#include "intel_npu/utils/vcl/vcl_api.hpp"
-#include "intel_npu/utils/zero/zero_api.hpp"
#include "intel_npu/utils/zero/zero_cmd_queue_pool.hpp"
#include "intel_npu/utils/zero/zero_utils.hpp"
#include "openvino/runtime/make_tensor.hpp"
@@ -242,17 +241,8 @@ void Graph::initialize_impl(const Config& config) {
}
bool Graph::release_blob(const Config& config) {
- if ((_zeGraphExt != nullptr && _zeGraphExt->isBlobDataImported(_graphDesc)) || _blobIsPersistent ||
- _blob == std::nullopt || _zeroInitStruct->getGraphDdiTable().version() < ZE_MAKE_VERSION(1, 8) ||
- config.get<PERF_COUNT>()) {
- return false;
- }
-
- ze_graph_properties_2_t properties = {};
- properties.stype = ZE_STRUCTURE_TYPE_GRAPH_PROPERTIES_2;
- _zeroInitStruct->getGraphDdiTable().pfnGetProperties2(_graphDesc._handle, &properties);
-
- if (~properties.initStageRequired & ZE_GRAPH_STAGE_INITIALIZE) {
+ if (_blobIsPersistent || _blob == std::nullopt || config.get<PERF_COUNT>() || _zeGraphExt == nullptr ||
+ _zeGraphExt->isBlobDataImported(_graphDesc) || !_zeGraphExt->isInitStageRequired(_graphDesc)) {
return false;
}
@@ -295,18 +285,17 @@ std::optional<std::string_view> Graph::get_compatibility_descriptor() const {
}
std::optional<bool> Graph::is_profiling_blob() const {
- if (_zeroInitStruct->getGraphDdiTable().version() < ZE_MAKE_VERSION(1, 16)) {
- _logger.debug("Cannot determine if the blob was compiled for profiling");
- return std::nullopt;
+ std::optional<bool> profilingEnabled;
+
+ if (_zeGraphExt != nullptr) {
+ profilingEnabled = _zeGraphExt->isProfilingEnabled(_graphDesc);
}
- ze_graph_properties_3_t graphProperties = {};
- graphProperties.stype = ZE_STRUCTURE_TYPE_GRAPH_PROPERTIES_3;
- auto result = _zeroInitStruct->getGraphDdiTable().pfnGetProperties3(static_cast<ze_graph_handle_t>(get_handle()),
- &graphProperties);
- THROW_ON_FAIL_FOR_LEVELZERO_EXT("pfnGetArgumentProperties3", result, _zeroInitStruct->getGraphDdiTable());
+ if (!profilingEnabled.has_value()) {
+ _logger.debug("Cannot determine if the blob was compiled for profiling");
+ }
- return graphProperties.flags & ZE_GRAPH_PROPERTIES_FLAG_PROFILING_ENABLED;
+ return profilingEnabled;
}
std::optional<size_t> Graph::determine_batch_size() {
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/weightless_graph.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/weightless_graph.cpp
index 7fc98fc6ccb..426cd91c1e4 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/weightless_graph.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/weightless_graph.cpp
@@ -664,16 +664,9 @@ void WeightlessGraph::set_weights_inputs() {
}
void WeightlessGraph::release_init_blob(const size_t initIndex) {
- if ((_zeGraphExt != nullptr && _zeGraphExt->isBlobDataImported(_graphDesc)) || _blobIsPersistent ||
- _initBlobs == std::nullopt || _zeroInitStruct->getGraphDdiTable().version() < ZE_MAKE_VERSION(1, 8)) {
- return;
- }
-
- ze_graph_properties_2_t properties = {};
- properties.stype = ZE_STRUCTURE_TYPE_GRAPH_PROPERTIES_2;
- _zeroInitStruct->getGraphDdiTable().pfnGetProperties2(_initsGraphDesc.at(initIndex)._handle, &properties);
-
- if (~properties.initStageRequired & ZE_GRAPH_STAGE_INITIALIZE) {
+ if (_blobIsPersistent || _initBlobs == std::nullopt || _zeGraphExt == nullptr ||
+ _zeGraphExt->isBlobDataImported(_graphDesc) ||
+ !_zeGraphExt->isInitStageRequired(_initsGraphDesc.at(initIndex))) {
return;
}
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp
index 467233f6183..3b1e7374048 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp
@@ -552,6 +552,51 @@ bool ZeGraphExtWrappers::isBlobDataImported(const GraphDescriptor& graphDescript
return graphDescriptor._memoryPersistent;
}
+bool ZeGraphExtWrappers::isInitStageRequired(const GraphDescriptor& graphDescriptor) const {
+ // Every path below reports false for the same reason: the requirement is something only the
+ // driver can establish, and a requirement that was never established is not asserted.
+
+ // ze_graph_properties_2_t, and with it initStageRequired, exists only from 1.8 on. An older
+ // driver has no way to express the requirement, so it cannot have stated one.
+ if (_graphExtVersion < ZE_MAKE_VERSION(1, 8)) {
+ return false;
+ }
+
+ // There is no graph to state anything about.
+ if (graphDescriptor._handle == nullptr) {
+ return false;
+ }
+
+ ze_graph_properties_2_t properties = {};
+ properties.stype = ZE_STRUCTURE_TYPE_GRAPH_PROPERTIES_2;
+ const ze_result_t result =
+ _zeroInitStruct->getGraphDdiTable().pfnGetProperties2(graphDescriptor._handle, &properties);
+ if (result != ZE_RESULT_SUCCESS) {
+ // Degrade rather than throw: the query is an optimisation hint, not a correctness
+ // requirement, and a driver that failed to answer has stated nothing either.
+ _logger.warning("pfnGetProperties2 returned error: 0x%x", static_cast<uint32_t>(result));
+ return false;
+ }
+
+ return (properties.initStageRequired & ZE_GRAPH_STAGE_INITIALIZE) != 0;
+}
+
+std::optional<bool> ZeGraphExtWrappers::isProfilingEnabled(const GraphDescriptor& graphDescriptor) const {
+ if (_graphExtVersion < ZE_MAKE_VERSION(1, 16)) {
+ _logger.debug("Reporting whether a graph was compiled for profiling is not supported by the current driver "
+ "version.");
+ return std::nullopt;
+ }
+
+ ze_graph_properties_3_t graphProperties = {};
+ graphProperties.stype = ZE_STRUCTURE_TYPE_GRAPH_PROPERTIES_3;
+
+ auto result = _zeroInitStruct->getGraphDdiTable().pfnGetProperties3(graphDescriptor._handle, &graphProperties);
+ THROW_ON_FAIL_FOR_LEVELZERO_EXT("pfnGetProperties3", result, _zeroInitStruct->getGraphDdiTable());
+
+ return (graphProperties.flags & ZE_GRAPH_PROPERTIES_FLAG_PROFILING_ENABLED) != 0;
+}
+
void ZeGraphExtWrappers::getMetadata(ze_graph_handle_t graphHandle,
uint32_t indexUsedByDriver,
std::vector<IODescriptor>& inputs,