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] Reject inconsistent Level Zero graph metadata ranks (#37942)
Alexis Menard4496d8702d44
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.hppindex 78c50b9f24d..c5f963ac7ca 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@@ -114,6 +114,20 @@ private:Logger _logger;};+/**+ * @brief Extracts the I/O metadata from Level Zero specific structures and converts them into OpenVINO specific+ * ones.+ *+ * @param arg The main Level Zero structure from which most metadata will be extracted.+ * @param metadata The secondary Level Zero structure from which metadata will be extracted. More specifically, the+ * argument is used for populating "shapeFromIRModel". Not providing this argument will lead to an empty value for+ * the referenced attribute.+ * @returns A descriptor object containing the metadata converted in OpenVINO specific structures.+ */+IODescriptor createIODescriptorFromLevelZero(uint32_t indexUsedByDriver,+ const ze_graph_argument_properties_3_t& arg,+ const std::optional<ze_graph_argument_metadata_t>& metadata);+// Parse the result string of query from format <name_0><name_1><name_2> to unordered_set of stringstd::unordered_set<std::string> parseQueryResult(std::vector<char>& data);diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/dynamic_graph.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/dynamic_graph.cppindex 7f5c9ff1752..c70cb1ad47e 100644--- a/src/plugins/intel_npu/src/compiler_adapter/src/dynamic_graph.cpp+++ b/src/plugins/intel_npu/src/compiler_adapter/src/dynamic_graph.cpp@@ -15,6 +15,7 @@#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 "ze_graph_ext_wrappers.hpp"namespace intel_npu {@@ -65,96 +66,6 @@ void DynamicGraph::create_execution_engine(const Config& config) {}}-/**- * @brief Extracts the I/O metadata from Level Zero specific structures and converts them into OpenVINO specific- * ones.- *- * @param arg The main Level Zero structure from which most metadata will be extracted.- * @param metadata The secondary Level Zero structure from which metadata will be extracted. More specifically, the- * argument is used for populating "shapeFromIRModel". Not providing this argument will lead to an empty value for- * the referenced attribute.- * @returns A descriptor object containing the metadata converted in OpenVINO specific structures.- */-static IODescriptor getIODescriptor(const ze_graph_argument_properties_3_t& arg,- const std::optional<ze_graph_argument_metadata_t>& metadata) {- auto logger = Logger::global().clone("getIODescriptor");- ov::element::Type_t precision = zeroUtils::toOVElementType(arg.devicePrecision);- ov::Shape shapeFromCompiler;- ov::PartialShape shapeFromIRModel;- std::unordered_set<std::string> outputTensorNames;-- for (uint32_t id = 0; id < arg.associated_tensor_names_count; id++) {- outputTensorNames.insert(arg.associated_tensor_names[id]);- }- for (uint32_t id = 0; id < arg.dims_count; id++) {- shapeFromCompiler.push_back(arg.dims[id]);- }- if (metadata.has_value()) {- const auto dynamicDim = std::numeric_limits<uint64_t>::max();- shapeFromIRModel.reserve(metadata->shape_size);- for (uint32_t id = 0; id < metadata->shape_size; id++) {- if (metadata->shape[id] != dynamicDim) {- shapeFromIRModel.push_back(metadata->shape[id]);- } else {- // lower bound is ignored, so we set it to 1 just to satisfy the Dimension constructor,- // upper bound is set to the value from shapeFromCompiler as it is filled with upper bounds- // in case of dynamic dimensions- if (id == utils::BATCH_AXIS && shapeFromCompiler[id] == utils::DEFAULT_BATCH_SIZE) {- logger.info("Ignore dynamic batch size upper limit, but keep the dimension dynamic as a metadata "- "from compiler has been lost.");- // We need to keep batch dimension dynamic- shapeFromIRModel.push_back(ov::Dimension(1, dynamicDim));- } else {- shapeFromIRModel.push_back(ov::Dimension(1, shapeFromCompiler[id]));- }- }- }- }-- // Flags will be used instead of indices for informing the type of the current entry- std::string nameFromCompiler = arg.name;- const bool isInput = (arg.type == ZE_GRAPH_ARGUMENT_TYPE_INPUT);- bool isStateInput = false;- bool isStateOutput = false;- bool isShapeTensor = false;- bool isInitInputWeights = false;- bool isInitOutputWeights = false;- bool isMainInputWeights = false;- if (isInput && isStateInputName(nameFromCompiler)) {- nameFromCompiler = nameFromCompiler.substr(READVALUE_PREFIX.length());- isStateInput = true;- } else if (!isInput && isStateOutputName(nameFromCompiler)) {- nameFromCompiler = nameFromCompiler.substr(ASSIGN_PREFIX.length());- isStateOutput = true;- } else if (isShapeTensorName(nameFromCompiler)) {- nameFromCompiler = nameFromCompiler.substr(SHAPE_TENSOR_PREFIX.length());- isShapeTensor = true;- } else if (isInput && isInitInputWeightsName(nameFromCompiler)) {- nameFromCompiler = nameFromCompiler.substr(INIT_INPUT_WEIGHTS_PREFIX.length());- isInitInputWeights = true;- } else if (!isInput && isInitOutputWeightsName(nameFromCompiler)) {- nameFromCompiler = nameFromCompiler.substr(INIT_OUTPUT_WEIGHTS_PREFIX.length());- isInitOutputWeights = true;- } else if (isInput && isMainInputWeightsName(nameFromCompiler)) {- nameFromCompiler = nameFromCompiler.substr(MAIN_INPUT_WEIGHTS_PREFIX.length());- isMainInputWeights = true;- }-- return {std::move(nameFromCompiler),- precision,- shapeFromCompiler,- isStateInput,- isStateOutput,- isShapeTensor,- isInitInputWeights,- isInitOutputWeights,- isMainInputWeights,- std::nullopt,- arg.debug_friendly_name,- std::move(outputTensorNames),- metadata.has_value() ? std::optional(shapeFromIRModel) : std::nullopt};-}-void DynamicGraph::prepare_metadata() {_metadata.inputs.clear();_metadata.outputs.clear();@@ -166,9 +77,7 @@ void DynamicGraph::prepare_metadata() {if (npuVMRuntimeGetMetadata(_engine, i, &arg, &meta, upperBound.data()) != NPU_VM_RUNTIME_RESULT_SUCCESS) {OPENVINO_THROW("Failed to get VM runtime metadata");}- IODescriptor ioDesc = getIODescriptor(arg, meta);- // TODO: Once runtime returns right value, can remove change on index and layout- ioDesc.indexUsedByDriver = i;+ IODescriptor ioDesc = createIODescriptorFromLevelZero(i, arg, meta);ioDesc.supportsStridedLayout = true;switch (arg.type) {case ZE_GRAPH_ARGUMENT_TYPE_INPUT: {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.cppindex 95c43a24709..7d45a09cda1 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@@ -16,22 +16,12 @@#include "openvino/core/dimension.hpp"#include "openvino/core/partial_shape.hpp"-namespace {-using namespace intel_npu;-/**- * @brief Extracts the I/O metadata from Level Zero specific structures and converts them into OpenVINO specific- * ones.- *- * @param arg The main Level Zero structure from which most metadata will be extracted.- * @param metadata The secondary Level Zero structure from which metadata will be extracted. More specifically, the- * argument is used for populating "shapeFromIRModel". Not providing this argument will lead to an empty value for- * the referenced attribute.- * @returns A descriptor object containing the metadata converted in OpenVINO specific structures.- */-static IODescriptor getIODescriptor(const uint32_t indexUsedByDriver,- const ze_graph_argument_properties_3_t& arg,- const std::optional<ze_graph_argument_metadata_t>& metadata) {- auto logger = Logger::global().clone("getIODescriptor");+namespace intel_npu {++IODescriptor createIODescriptorFromLevelZero(const uint32_t indexUsedByDriver,+ const ze_graph_argument_properties_3_t& arg,+ const std::optional<ze_graph_argument_metadata_t>& metadata) {+ auto logger = Logger::global().clone("createIODescriptorFromLevelZero");ov::element::Type_t precision = zeroUtils::toOVElementType(arg.devicePrecision);ov::Shape shapeFromCompiler;ov::PartialShape shapeFromIRModel;@@ -40,10 +30,34 @@ static IODescriptor getIODescriptor(const uint32_t indexUsedByDriver,for (uint32_t id = 0; id < arg.associated_tensor_names_count; id++) {outputTensorNames.insert(arg.associated_tensor_names[id]);}+ if (arg.dims_count > ZE_MAX_GRAPH_ARGUMENT_DIMENSIONS_SIZE) {+ OPENVINO_THROW("Invalid Level Zero graph argument metadata for argument index ",+ indexUsedByDriver,+ ": dims_count ",+ arg.dims_count,+ " exceeds ABI limit ",+ ZE_MAX_GRAPH_ARGUMENT_DIMENSIONS_SIZE);+ }for (uint32_t id = 0; id < arg.dims_count; id++) {shapeFromCompiler.push_back(arg.dims[id]);}if (metadata.has_value()) {+ if (metadata->shape_size > ZE_MAX_GRAPH_TENSOR_REF_DIMS) {+ OPENVINO_THROW("Invalid Level Zero graph argument metadata for argument index ",+ indexUsedByDriver,+ ": metadata shape_size ",+ metadata->shape_size,+ " exceeds ABI limit ",+ ZE_MAX_GRAPH_TENSOR_REF_DIMS);+ }+ if (metadata->shape_size != arg.dims_count) {+ OPENVINO_THROW("Invalid Level Zero graph argument metadata for argument index ",+ indexUsedByDriver,+ ": metadata shape_size ",+ metadata->shape_size,+ " does not match dims_count ",+ arg.dims_count);+ }const auto dynamicDim = std::numeric_limits<uint64_t>::max();shapeFromIRModel.reserve(metadata->shape_size);for (uint32_t id = 0; id < metadata->shape_size; id++) {@@ -119,9 +133,6 @@ static IODescriptor getIODescriptor(const uint32_t indexUsedByDriver,indexUsedByDriver,supportsStridedLayout};}-} // namespace--namespace intel_npu {GraphDescriptor::GraphDescriptor(ze_graph_handle_t handle, bool memoryPersistent): _handle(handle),@@ -504,10 +515,10 @@ void ZeGraphExtWrappers::getMetadata(ze_graph_handle_t graphHandle,switch (arg.type) {case ZE_GRAPH_ARGUMENT_TYPE_INPUT: {- inputs.push_back(getIODescriptor(indexUsedByDriver, arg, std::nullopt));+ inputs.push_back(createIODescriptorFromLevelZero(indexUsedByDriver, arg, std::nullopt));} break;case ZE_GRAPH_ARGUMENT_TYPE_OUTPUT: {- outputs.push_back(getIODescriptor(indexUsedByDriver, arg, std::nullopt));+ outputs.push_back(createIODescriptorFromLevelZero(indexUsedByDriver, arg, std::nullopt));} break;default: {OPENVINO_THROW("Invalid ze_graph_argument_type_t found in ze_graph_argument_properties_3_t object: ",@@ -546,10 +557,10 @@ void ZeGraphExtWrappers::getMetadata(ze_graph_handle_t graphHandle,switch (arg.type) {case ZE_GRAPH_ARGUMENT_TYPE_INPUT: {- inputs.push_back(getIODescriptor(indexUsedByDriver, arg, optionalMetadata));+ inputs.push_back(createIODescriptorFromLevelZero(indexUsedByDriver, arg, optionalMetadata));} break;case ZE_GRAPH_ARGUMENT_TYPE_OUTPUT: {- outputs.push_back(getIODescriptor(indexUsedByDriver, arg, optionalMetadata));+ outputs.push_back(createIODescriptorFromLevelZero(indexUsedByDriver, arg, optionalMetadata));} break;default: {OPENVINO_THROW("Invalid ze_graph_argument_type_t found in ze_graph_argument_properties_3_t object: ",diff --git a/src/plugins/intel_npu/tests/unit/npu/ze_graph_ext_wrappers_test.cpp b/src/plugins/intel_npu/tests/unit/npu/ze_graph_ext_wrappers_test.cppnew file mode 100644index 00000000000..b7015624613--- /dev/null+++ b/src/plugins/intel_npu/tests/unit/npu/ze_graph_ext_wrappers_test.cpp@@ -0,0 +1,99 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "ze_graph_ext_wrappers.hpp"++#include <gtest/gtest.h>++#include <algorithm>+#include <cstring>+#include <limits>+#include <optional>+#include <string>++#include "common_test_utils/test_assertions.hpp"++namespace {++constexpr uint32_t kArgumentIndex = 7;++ze_graph_argument_properties_3_t makeArgument(uint32_t dimsCount) {+ ze_graph_argument_properties_3_t arg = {};+ arg.stype = ZE_STRUCTURE_TYPE_GRAPH_ARGUMENT_PROPERTIES_3;+ arg.type = ZE_GRAPH_ARGUMENT_TYPE_INPUT;+ arg.devicePrecision = ZE_GRAPH_ARGUMENT_PRECISION_FP32;+ arg.dims_count = dimsCount;+ std::strncpy(arg.name, "input", sizeof(arg.name) - 1);+ std::strncpy(arg.debug_friendly_name, "input_node", sizeof(arg.debug_friendly_name) - 1);++ const uint32_t initializedDims = std::min<uint32_t>(dimsCount, ZE_MAX_GRAPH_ARGUMENT_DIMENSIONS_SIZE);+ for (uint32_t dim = 0; dim < initializedDims; ++dim) {+ arg.dims[dim] = dim == 0 ? 4 : 32;+ }++ return arg;+}++ze_graph_argument_metadata_t makeMetadata(uint32_t shapeSize) {+ ze_graph_argument_metadata_t metadata = {};+ metadata.stype = ZE_STRUCTURE_TYPE_GRAPH_ARGUMENT_METADATA;+ metadata.type = ZE_GRAPH_ARGUMENT_TYPE_INPUT;+ metadata.shape_size = shapeSize;++ const uint32_t initializedDims = std::min<uint32_t>(shapeSize, ZE_MAX_GRAPH_TENSOR_REF_DIMS);+ for (uint32_t dim = 0; dim < initializedDims; ++dim) {+ metadata.shape[dim] = dim == 0 ? 4 : 32;+ }++ return metadata;+}++} // namespace++TEST(ZeGraphExtWrappersTest, RejectsMetadataRankGreaterThanArgumentRank) {+ constexpr uint32_t argRank = 1;+ constexpr uint32_t metadataRank = 2;+ auto arg = makeArgument(argRank);+ auto metadata = makeMetadata(metadataRank);+ metadata.shape[1] = std::numeric_limits<uint64_t>::max();++ OV_EXPECT_THROW_HAS_SUBSTRING(intel_npu::createIODescriptorFromLevelZero(kArgumentIndex, arg, metadata),+ ov::Exception,+ "metadata shape_size " + std::to_string(metadataRank) ++ " does not match dims_count " + std::to_string(argRank));+}++TEST(ZeGraphExtWrappersTest, RejectsArgumentRankAboveAbiLimit) {+ const uint32_t aboveAbiLimit = ZE_MAX_GRAPH_ARGUMENT_DIMENSIONS_SIZE + 1;+ auto arg = makeArgument(aboveAbiLimit);++ OV_EXPECT_THROW_HAS_SUBSTRING(intel_npu::createIODescriptorFromLevelZero(kArgumentIndex, arg, std::nullopt),+ ov::Exception,+ "dims_count " + std::to_string(aboveAbiLimit) ++ " exceeds ABI limit " + std::to_string(ZE_MAX_GRAPH_ARGUMENT_DIMENSIONS_SIZE));+}++TEST(ZeGraphExtWrappersTest, RejectsMetadataRankAboveAbiLimit) {+ const uint32_t aboveAbiLimit = ZE_MAX_GRAPH_TENSOR_REF_DIMS + 1;+ auto arg = makeArgument(1);+ auto metadata = makeMetadata(aboveAbiLimit);++ OV_EXPECT_THROW_HAS_SUBSTRING(intel_npu::createIODescriptorFromLevelZero(kArgumentIndex, arg, metadata),+ ov::Exception,+ "metadata shape_size " + std::to_string(aboveAbiLimit) ++ " exceeds ABI limit " + std::to_string(ZE_MAX_GRAPH_TENSOR_REF_DIMS));+}++TEST(ZeGraphExtWrappersTest, AcceptsMatchingDynamicMetadataRank) {+ auto arg = makeArgument(2);+ auto metadata = makeMetadata(2);+ metadata.shape[1] = std::numeric_limits<uint64_t>::max();++ const auto descriptor = intel_npu::createIODescriptorFromLevelZero(kArgumentIndex, arg, metadata);++ EXPECT_EQ(descriptor.indexUsedByDriver, kArgumentIndex);+ ASSERT_TRUE(descriptor.shapeFromIRModel.has_value());+ EXPECT_EQ(descriptor.shapeFromCompiler, ov::PartialShape({4, 32}));+ EXPECT_EQ(*descriptor.shapeFromIRModel, ov::PartialShape({4, ov::Dimension(1, 32)}));+}