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] Human readable plugin metadata (#35571)
Alexandru Enache13797f655b68
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/compat_string_parser.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/compat_string_parser.hppnew file mode 100644index 00000000000..46d54790c07--- /dev/null+++ b/src/plugins/intel_npu/src/al/include/intel_npu/compat_string_parser.hpp@@ -0,0 +1,371 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++/*+ This file implements a parser for compatibility strings used in the NPU+ software stack. The compatibility string encodes requirements of a compiled+ blob and metadata such as a target platform, minimum runtime and compiler+ version. The string consists of a human-readable list of name=value pairs+ called attributes. Basic encapsulation across SW layers is supported via+ nested lists. Optional attributes are supported with brace-enclosed+ attribute list.++ The grammar for the compatibility string is defined as follows:++ string ::= expr (';' expr)*+ expr ::= attr | '{' string '}'+ attr ::= name '=' value | name '=' list+ list ::= '[' string ('|' string)* ']'+ name ::= [a-z][_a-z0-9]*+ value ::= [A-Z0-9][_A-Z0-9\.]*++ No whitespaces are allowed.+ Attribute names are lower case. Attribute values are upper case.+++ Example usage:++ std::string_view compatibilityString = "name=A;nested=[key=X;ver=1.2|key=Y;ver=1.3];{optional=V1.4}";+ std::array legalAttributes = {"name", "nested"};+ Parser parser(compatibilityString, legalAttributes);+ parser.getAttribute("name"); // returns "A"+ auto nested = parser.getAttribute("nested"); // returns "[key=X;ver=1.2|key=Y;ver=1.3]"+ Parser::splitList(nested); // returns ["key=X;ver=1.2", "key=Y;ver=1.3"]+ // to be parsed separately+ parser.getOptionalAttributes(); // returns { "optional": "V1.4"} as map+++ Compatibility string guidelines:++ The compatibility string represents the blob's runtime requirements in a+ concise form: the target hardware and the minimum version of required+ SW/FW runtime components. The compatibility string MUST be sufficient to+ determine whether the blob will run correctly in the current environment without+ accessing the blob content.+ The string is human-readable but is not intended to be interpreted by the+ end user nor edited. The string consists of attributes which primarily+ represent HW target requirements, runtime component versions and may include+ feature flags.++ The string should include the version of the components contributing to the+ blob's generation (such as compiler). While not strictly required to+ evaluate the runtime requirements a component version may be used to reject+ known bad blobs in future releases or for debugging purposes.+ Each component SHOULD serialize its own version as an attribute in the+ compatibility string. The component encapsulating a blob in a binary+ container MUST serialize the inner blob compatibility string in one of its+ attributes as a list (e.g. `inner=[string]`). The component MUST NOT repeat+ the information contained in the inner blob attributes.++ Attribute names should be short and may use abbreviations. For clarity+ attribute names SHOULD be unique across components, e.g. `version` is not a+ good attribute name. As attributes normally represent versions, the `_ver`+ suffix is not recommended either. For consistency, versions SHOULD be+ encoded as dot-separated integers, e.g. `compiler=8.1`.+ In case of boolean flags the attribute should be set to `1` for true. False+ values SHOULD NOT be serialized. The absence of a flag MUST be treated as+ feature not being required.++ A component MUST reject the string if an attribute name is not recognized,+ so a new requirement is automatically rejected by prior SW releases that did+ not support given feature.+ An unknown optional attribute (enclosed in braces) MUST be ignored. This allows+ for augmenting metadata without breaking compatibility with earlier SW+ versions.++ Version history:+ 2026-05-06 Initial version.++*/++#pragma once++#include <algorithm>+#include <array>+#include <cassert>+#include <cctype>+#include <functional>+#include <map>+#include <stdexcept>+#include <string>+#include <string_view>+#include <vector>++namespace intel_npu::compat {++class Parser {+public:+ using attr_map_type = std::map<std::string, std::string, std::less<>>;++ Parser() = default;++ template <typename Iterable>+ Parser(std::string_view input, const Iterable& legalAttributeNames);++ template <typename Iterable>+ void parse(std::string_view input, const Iterable& legalAttributeNames);++ const std::string& getAttribute(const std::string& name) const;+ const attr_map_type& getAttributes() const {+ return _attributes;+ }+ const attr_map_type& getOptionalAttributes() const {+ return _optional_attributes;+ }++ template <typename Iterable>+ void validateAttributes(const Iterable& legalAttributeNames) const;++ // Breaks down a list of nested compatibility strings+ static std::vector<std::string> splitList(std::string_view list);++private:+ enum class CaptureMode { ATTR, STRING };++ char peek() const;+ char nextc();+ void expect(char c);+ void reset(std::string_view input, CaptureMode mode = CaptureMode::ATTR);+ void startCapture(CaptureMode mode);+ std::string_view stopCapture(CaptureMode mode);+ std::string_view parseString();+ void parseExpr();+ void parseAttr();+ std::vector<std::string_view> parseList();+ template <typename F>+ std::string_view readChars(F condition);+ std::string_view parseName();+ std::string_view parseValue();+ std::runtime_error errorAt(std::string_view message) const;+ void setAttribute(std::string_view name, std::string_view value);++ // Safe ctype helpers to avoid undefined behavior on signed char+ // clang-format off+ static bool isLower(char c) { return std::islower(static_cast<unsigned char>(c)); }+ static bool isUpper(char c) { return std::isupper(static_cast<unsigned char>(c)); }+ static bool isDigit(char c) { return std::isdigit(static_cast<unsigned char>(c)); }+ static bool isGraph(char c) { return std::isgraph(static_cast<unsigned char>(c)); }+ // clang-format on++ std::string_view::iterator _current;+ std::string_view::iterator _end;++ CaptureMode _captureMode = CaptureMode::ATTR;+ std::string_view::iterator _captureStart;+ std::string_view::iterator _captureEnd;+ int _nesting = 0; // Nesting level for current capture mode+ int _optional = 0; // Tracks top-level optional attributes++ attr_map_type _attributes;+ attr_map_type _optional_attributes;+};++template <typename Iterable>+Parser::Parser(std::string_view input, const Iterable& legalAttributeNames) {+ parse(input, legalAttributeNames);+}++template <typename Iterable>+void Parser::parse(std::string_view input, const Iterable& legalAttributeNames) {+ reset(input);+ _attributes.clear();+ _optional_attributes.clear();+ parseString();+ if (peek() != 0) {+ throw errorAt("at the end of the string");+ }+ validateAttributes(legalAttributeNames);+}++inline const std::string& Parser::getAttribute(const std::string& name) const {+ auto it = _attributes.find(name);+ if (it != _attributes.end()) {+ return it->second;+ }+ throw std::runtime_error("Attribute not found: " + name);+}++template <typename Iterable>+void Parser::validateAttributes(const Iterable& legalAttributeNames) const {+ if (legalAttributeNames.size() == 0) {+ return; // No validation if no legal attribute names provided+ }+ for (const auto& entry : _attributes) {+ auto& name = entry.first;+ if (std::find(legalAttributeNames.begin(), legalAttributeNames.end(), name) == legalAttributeNames.end()) {+ throw std::runtime_error("Illegal attribute: " + name);+ }+ }+}++inline std::vector<std::string> Parser::splitList(std::string_view list) {+ Parser parser;+ parser.reset(list, CaptureMode::STRING);+ auto items = parser.parseList();+ if (parser.peek() != 0) {+ throw parser.errorAt("at the end of the list");+ }+ return std::vector<std::string>(items.begin(), items.end());+}++inline char Parser::peek() const {+ return _current != _end ? *_current : 0;+}++inline char Parser::nextc() {+ assert(_current != _end);+ return *_current++;+}++inline void Parser::expect(char c) {+ if (peek() != c) {+ throw errorAt(std::string("expected '") + c + "'");+ }+ nextc();+}++inline void Parser::reset(std::string_view input, CaptureMode mode) {+ _current = input.begin();+ _end = input.end();+ _optional = 0;+ _nesting = 0;+ _captureMode = mode;+ _captureStart = _end;+ _captureEnd = _end;+}++inline void Parser::startCapture(CaptureMode mode) {+ if (_captureMode == mode) {+ if (_nesting++ == 0) {+ _captureStart = _current;+ }+ }+}++inline std::string_view Parser::stopCapture(CaptureMode mode) {+ if (_captureMode == mode) {+ if (--_nesting == 0) {+ _captureEnd = _current;+ assert(_captureEnd != _captureStart);+ // return std::string_view(_captureStart, _captureEnd); // C++20+ return std::string_view(&*_captureStart, _captureEnd - _captureStart);+ }+ }+ return {};+}++// string ::= expr (';' expr)*+inline std::string_view Parser::parseString() {+ startCapture(CaptureMode::STRING);+ parseExpr();+ while (peek() == ';') {+ nextc(); // ';'+ parseExpr();+ }+ return stopCapture(CaptureMode::STRING);+}++// expr ::= attr | '{' string '}'+inline void Parser::parseExpr() {+ if (peek() == '{') {+ ++_optional;+ nextc(); // '{'+ parseString();+ expect('}');+ --_optional;+ } else {+ parseAttr();+ }+}++// attr ::= name '=' value | name '=' list+inline void Parser::parseAttr() {+ auto name = parseName();+ expect('=');+ if (peek() == '[') {+ startCapture(CaptureMode::ATTR);+ parseList();+ auto list = stopCapture(CaptureMode::ATTR);+ if (!list.empty()) {+ setAttribute(name, list);+ }+ return;+ }+ auto value = parseValue();+ if (_captureMode == CaptureMode::ATTR && _nesting == 0) {+ setAttribute(name, value);+ }+}++// list ::= '[' string ('|' string)* ']'+inline std::vector<std::string_view> Parser::parseList() {+ std::vector<std::string_view> items;+ expect('[');+ items.push_back(parseString());+ while (peek() == '|') {+ nextc(); // '|'+ items.push_back(parseString());+ }+ expect(']');+ return items;+}++template <typename F>+std::string_view Parser::readChars(F condition) {+ auto start = _current;+ while (peek() != 0 && condition(peek())) {+ nextc();+ }+ // return std::string_view(start, _current); // C++20+ return std::string_view(&*start, _current - start);+}++// name ::= [a-z][_a-z0-9]*+inline std::string_view Parser::parseName() {+ if (!isLower(peek())) {+ throw errorAt("expected attribute name");+ }+ return readChars([](char c) {+ return isLower(c) || isDigit(c) || c == '_';+ });+}++// value ::= [A-Z0-9][_A-Z0-9\.]*+inline std::string_view Parser::parseValue() {+ if (!isUpper(peek()) && !isDigit(peek())) {+ throw errorAt("expected attribute value");+ }+ return readChars([](char c) {+ return isUpper(c) || isDigit(c) || c == '.' || c == '_';+ });+}++inline std::runtime_error Parser::errorAt(std::string_view message) const {+ if (peek() == 0) {+ return std::runtime_error(std::string(message) + " at the end of the string");+ } else {+ std::string msg = "Unexpected character ";+ char c = peek();+ if (isGraph(c)) { // Printable non-space character+ msg += "'";+ msg += c;+ msg += "': ";+ }+ msg += message;+ return std::runtime_error(std::move(msg));+ }+}++inline void Parser::setAttribute(std::string_view name, std::string_view value) {+ if (_attributes.count(name) != 0 || _optional_attributes.count(name) != 0) {+ throw std::runtime_error("Duplicate attribute: " + std::string(name));+ }+ if (_optional > 0) {+ _optional_attributes.emplace(name, value);+ } else {+ _attributes.emplace(name, value);+ }+}++} // namespace intel_npu::compatdiff --git a/src/plugins/intel_npu/src/plugin/include/metadata.hpp b/src/plugins/intel_npu/src/plugin/include/metadata.hppindex 8b109d3b336..626be644068 100644--- a/src/plugins/intel_npu/src/plugin/include/metadata.hpp+++ b/src/plugins/intel_npu/src/plugin/include/metadata.hpp@@ -4,9 +4,13 @@#pragma once+#include <array>#include <cstdint>+#include <map>#include <memory>#include <optional>+#include <string>+#include <string_view>#include <variant>#include <vector>@@ -16,7 +20,6 @@#include "openvino/runtime/tensor.hpp"namespace intel_npu {-class MetadataBase {public:MetadataBase(uint32_t version, uint64_t blobDataSize);@@ -35,13 +38,29 @@ public:*/void read(const ov::Tensor& tensor);+ /**+ * @brief Populates this object from a pre-parsed human-readable metadata attribute map.+ */+ void read_as_text(std::map<std::string, std::string, std::less<>> attrs);+virtual void read() = 0;+ /**+ * @note Layouts and encryption are intentionally omitted from the human-readable compatibility+ * string. They are internal implementation details that don't affect cross-version compatibility and would only add+ * noise for consumers.+ * Compiler version is already contained within the compiler requirements field.+ *+ */+ virtual void read_as_text() = 0;+/*** @brief Writes metadata to a stream.*/virtual void write(std::ostream& stream) = 0;+ virtual void write_as_text(std::ostream& stream) = 0;+virtual uint64_t get_blob_size() const;/**@@ -60,14 +79,12 @@ public:virtual std::optional<uint32_t> get_compiler_version() const;- virtual bool is_encrypted_blob() const;+ virtual std::optional<bool> is_encrypted_blob() const;virtual ~MetadataBase() = default;static std::streampos getFileSize(std::istream& stream);- virtual size_t get_metadata_size() const = 0;-/*** @brief Returns a uint32_t value which represents two uint16_t values concatenated.* @details Convention for bumping the metadata version:@@ -98,7 +115,8 @@ public:protected:/**- * @brief Reads data from the source containing the metadata. The implementation depends on the type of source.+ * @brief Reads data from the source containing the binary metadata. The implementation depends on the type of+ * source.*/void read_data_from_source(char* destination, const size_t size);@@ -115,6 +133,11 @@ protected:uint64_t _blobDataSize;Logger _logger;+ /**+ * @brief Parsed key-value attributes from the human-readable metadata string.+ */+ std::map<std::string, std::string, std::less<>> _textAttrs;+/*** @brief Where the metadata is read from. The type can be a stream, an OpenVINO tensor or "uninitialized_source".* @details Stored as attribute in order to avoid repeatedly passing the same arguments to some methods.@@ -133,6 +156,24 @@ protected:*/constexpr std::string_view MAGIC_BYTES = "OVNPU";+/**+ * @brief Keys used in the metadata text format.+ */+namespace MetadataTextKeys {+constexpr std::string_view META = "meta";+constexpr std::string_view OV = "ov";+constexpr std::string_view WS_INITS = "ws_inits";+constexpr std::string_view BATCH = "batch";+} // namespace MetadataTextKeys++/**+ * @brief List of known attributes in the human-readable metadata format.+ */+inline constexpr std::array metadataTextAttributes = {MetadataTextKeys::META,+ MetadataTextKeys::OV,+ MetadataTextKeys::WS_INITS,+ MetadataTextKeys::BATCH};+/*** @brief List of supported version formats.*/@@ -223,6 +264,8 @@ public:void read() override;+ void read_as_text() override;+/*** @attention It's a must to first write metadata version in any metadata specialization.*@@ -232,7 +275,7 @@ public:*/void write(std::ostream& stream) override;- size_t get_metadata_size() const override;+ void write_as_text(std::ostream& stream) override;protected:OpenvinoVersion _ovVersion;@@ -240,6 +283,8 @@ protected:/*** @brief The version that adds support for init schedules (weights separation).+ *+ * @note The text format defines WS enablement as a boolean flag; actual sizes are not preserved*/template <>class Metadata<METADATA_VERSION_2_1> : public Metadata<METADATA_VERSION_2_0> {@@ -254,15 +299,17 @@ public:*/void read() override;+ void read_as_text() override;+/*** @details The number of init schedules, along with the size of each init binary object are written in addition to* the information registered by the previous metadata versions.*/void write(std::ostream& stream) override;- std::optional<std::vector<uint64_t>> get_init_sizes() const override;+ void write_as_text(std::ostream& stream) override;- size_t get_metadata_size() const override;+ std::optional<std::vector<uint64_t>> get_init_sizes() const override;private:std::optional<std::vector<uint64_t>> _initSizes;@@ -282,11 +329,13 @@ public:void read() override;+ void read_as_text() override;+void write(std::ostream& stream) override;- std::optional<int64_t> get_batch_size() const override;+ void write_as_text(std::ostream& stream) override;- size_t get_metadata_size() const override;+ std::optional<int64_t> get_batch_size() const override;private:std::optional<int64_t> _batchSize;@@ -310,8 +359,6 @@ public:void write(std::ostream& stream) override;- size_t get_metadata_size() const override;-std::optional<std::vector<ov::Layout>> get_input_layouts() const override;std::optional<std::vector<ov::Layout>> get_output_layouts() const override;@@ -339,8 +386,6 @@ public:void write(std::ostream& stream) override;- size_t get_metadata_size() const override;-std::optional<uint32_t> get_compiler_version() const override;private:@@ -367,12 +412,10 @@ public:void write(std::ostream& stream) override;- size_t get_metadata_size() const override;-- bool is_encrypted_blob() const override;+ std::optional<bool> is_encrypted_blob() const override;private:- bool _isEncryptedBlob;+ std::optional<bool> _isEncryptedBlob;};/**@@ -402,4 +445,6 @@ std::unique_ptr<MetadataBase> read_metadata_from(std::istream& stream);*/std::unique_ptr<MetadataBase> read_metadata_from(const ov::Tensor& tensor);+std::unique_ptr<MetadataBase> read_as_text(std::string_view input);+} // namespace intel_npudiff --git a/src/plugins/intel_npu/src/plugin/src/metadata.cpp b/src/plugins/intel_npu/src/plugin/src/metadata.cppindex 68ad662fed6..06341ebb50b 100644--- a/src/plugins/intel_npu/src/plugin/src/metadata.cpp+++ b/src/plugins/intel_npu/src/plugin/src/metadata.cpp@@ -4,13 +4,56 @@#include "metadata.hpp"+#include <algorithm>+#include <cctype>#include <cstring>-#include <iterator>#include <optional>+#include <string>-#include "intel_npu/utils/utils.hpp"+#include "intel_npu/compat_string_parser.hpp"#include "openvino/runtime/shared_buffer.hpp"+namespace {++template <typename T>+void write_text_field(std::ostream& stream, std::string_view key, const T& value) {+ if (stream.tellp() != std::streampos(0)) {+ stream << ';';+ }+ stream << key << '=' << value;+}++std::vector<uint16_t> parse_version(std::string_view sv) {+ const auto hasOnlyDigits = [](std::string_view sv) {+ return !sv.empty() && std::all_of(sv.begin(), sv.end(), [](unsigned char c) {+ return std::isdigit(c);+ });+ };++ std::vector<uint16_t> parts;+ std::string_view remaining = sv;+ while (true) {+ const size_t dot = remaining.find('.');+ const std::string_view part = remaining.substr(0, dot);+ if (!hasOnlyDigits(part)) {+ OPENVINO_THROW("Invalid version '",+ sv,+ "': version must meet the format MAJOR.MINOR.PATCH with numeric components");+ }+ parts.push_back(static_cast<uint16_t>(std::stoul(std::string(part))));+ if (dot == std::string_view::npos) {+ break;+ }+ remaining = remaining.substr(dot + 1);+ if (remaining.empty()) {+ OPENVINO_THROW("Invalid version '", sv, "': trailing dot");+ }+ }+ return parts;+}++} // namespace+namespace intel_npu {uint16_t OpenvinoVersion::get_major() const {@@ -136,12 +179,28 @@ void MetadataBase::read(const ov::Tensor& tensor) {read();}+void MetadataBase::read_as_text(std::map<std::string, std::string, std::less<>> attrs) {+ _textAttrs = std::move(attrs);+ read_as_text();+}+void MetadataBase::read_data_from_source(char* destination, const size_t size) {if (const std::reference_wrapper<std::istream>* stream =std::get_if<std::reference_wrapper<std::istream>>(&_source)) {stream->get().read(destination, size);} else if (const std::reference_wrapper<const ov::Tensor>* tensor =std::get_if<std::reference_wrapper<const ov::Tensor>>(&_source)) {+ const size_t available = tensor->get().get_byte_size();+ const size_t remaining = (_cursorOffset <= available) ? available - _cursorOffset : 0;+ if (size > remaining) {+ OPENVINO_THROW("NPU metadata: attempted to read ",+ size,+ " bytes at offset ",+ _cursorOffset,+ " but only ",+ remaining,+ " bytes remain in the metadata buffer.");+ }std::memcpy(destination, tensor->get().data<const char>() + _cursorOffset, size);_cursorOffset += size;} else {@@ -248,6 +307,44 @@ void Metadata<METADATA_VERSION_2_5>::read() {_isEncryptedBlob = isEncryptedBlob;}+void Metadata<METADATA_VERSION_2_0>::read_as_text() {+ const auto it = _textAttrs.find(MetadataTextKeys::OV);+ if (it == _textAttrs.end()) {+ OPENVINO_THROW("Human-readable metadata missing '" + std::string(MetadataTextKeys::OV) + "' field.");+ }+ const auto ovParts = parse_version(it->second);+ if (ovParts.size() != 3) {+ OPENVINO_THROW("Human-readable metadata: '" + std::string(MetadataTextKeys::OV) ++ "' is not in MAJOR.MINOR.PATCH format: " + it->second);+ }+ _ovVersion = OpenvinoVersion(ovParts[0], ovParts[1], ovParts[2]);+}++void Metadata<METADATA_VERSION_2_1>::read_as_text() {+ Metadata<METADATA_VERSION_2_0>::read_as_text();++ const auto it = _textAttrs.find(MetadataTextKeys::WS_INITS);+ if (it == _textAttrs.end()) {+ return;+ }+ if (it->second != "1") {+ OPENVINO_THROW("Human-readable metadata: '" + std::string(MetadataTextKeys::WS_INITS) ++ "' must be '1' when present; got: " + it->second);+ }+ _initSizes = std::vector<uint64_t>{};+}++void Metadata<METADATA_VERSION_2_2>::read_as_text() {+ Metadata<METADATA_VERSION_2_1>::read_as_text();++ const auto it = _textAttrs.find(MetadataTextKeys::BATCH);+ if (it == _textAttrs.end()) {+ return;+ }+ const int64_t batchValue = std::stoll(it->second);+ _batchSize = batchValue != 0 ? std::optional<int64_t>(batchValue) : std::nullopt;+}+void Metadata<METADATA_VERSION_2_0>::write(std::ostream& stream) {stream.write(reinterpret_cast<const char*>(&_version), sizeof(_version));_ovVersion.write(stream);@@ -306,12 +403,38 @@ void Metadata<METADATA_VERSION_2_4>::write(std::ostream& stream) {void Metadata<METADATA_VERSION_2_5>::write(std::ostream& stream) {Metadata<METADATA_VERSION_2_4>::write(stream);- const uint8_t isEncryptedBlob = _isEncryptedBlob;+ const uint8_t isEncryptedBlob = _isEncryptedBlob.value_or(false);stream.write(reinterpret_cast<const char*>(&isEncryptedBlob), sizeof(isEncryptedBlob));append_blob_size_and_magic(stream);}+void Metadata<METADATA_VERSION_2_0>::write_as_text(std::ostream& stream) {+ const uint16_t meta_major = MetadataBase::get_major(_version);+ const uint16_t meta_minor = MetadataBase::get_minor(_version);+ write_text_field(stream, MetadataTextKeys::META, std::to_string(meta_major) + "." + std::to_string(meta_minor));+ write_text_field(stream,+ MetadataTextKeys::OV,+ std::to_string(OPENVINO_VERSION_MAJOR) + "." + std::to_string(OPENVINO_VERSION_MINOR) + "." ++ std::to_string(OPENVINO_VERSION_PATCH));+}++void Metadata<METADATA_VERSION_2_1>::write_as_text(std::ostream& stream) {+ Metadata<METADATA_VERSION_2_0>::write_as_text(stream);++ if (_initSizes.has_value() && !_initSizes->empty()) {+ write_text_field(stream, MetadataTextKeys::WS_INITS, "1");+ }+}++void Metadata<METADATA_VERSION_2_2>::write_as_text(std::ostream& stream) {+ Metadata<METADATA_VERSION_2_1>::write_as_text(stream);++ if (_batchSize.has_value() && _batchSize.value() != 0) {+ write_text_field(stream, MetadataTextKeys::BATCH, _batchSize.value());+ }+}+std::unique_ptr<MetadataBase> create_metadata(uint32_t version, uint64_t blobSize) {uint16_t major = MetadataBase::get_major(version), minor = MetadataBase::get_minor(version);if (major != CURRENT_METADATA_MAJOR_VERSION || minor > CURRENT_METADATA_MINOR_VERSION) {@@ -437,6 +560,36 @@ std::unique_ptr<MetadataBase> read_metadata_from(const ov::Tensor& tensor) {return storedMeta;}+std::unique_ptr<MetadataBase> read_as_text(std::string_view input) {+ std::string versionStr;+ compat::Parser::attr_map_type attrs;+ try {+ compat::Parser parser(input, metadataTextAttributes);+ versionStr = parser.getAttribute(std::string(MetadataTextKeys::META));+ attrs = parser.getAttributes();+ } catch (const std::exception& ex) {+ OPENVINO_THROW("NPU compatibility string is malformed: ", ex.what());+ }++ const auto metaParts = parse_version(versionStr);+ if (metaParts.size() != 2) {+ OPENVINO_THROW("NPU compatibility string is malformed: 'meta' must be in MAJOR.MINOR format: ", versionStr);+ }+ const uint32_t metaVersion = MetadataBase::make_version(metaParts[0], metaParts[1]);++ std::unique_ptr<MetadataBase> storedMeta;+ try {+ storedMeta = create_metadata(metaVersion, 0);+ storedMeta->read_as_text(std::move(attrs));+ } catch (const std::exception& ex) {+ OPENVINO_THROW("Can't read NPU human-readable metadata: ", ex.what());+ } catch (...) {+ OPENVINO_THROW("Unexpected exception while reading NPU human-readable metadata");+ }++ return storedMeta;+}+uint64_t MetadataBase::get_blob_size() const {return _blobDataSize;}@@ -461,8 +614,8 @@ std::optional<uint32_t> MetadataBase::get_compiler_version() const {return std::nullopt;}-bool MetadataBase::is_encrypted_blob() const {- return false;+std::optional<bool> MetadataBase::is_encrypted_blob() const {+ return std::nullopt;}std::optional<std::vector<uint64_t>> Metadata<METADATA_VERSION_2_1>::get_init_sizes() const {@@ -485,60 +638,8 @@ std::optional<uint32_t> Metadata<METADATA_VERSION_2_4>::get_compiler_version() creturn _compilerVersion;}-bool Metadata<METADATA_VERSION_2_5>::is_encrypted_blob() const {+std::optional<bool> Metadata<METADATA_VERSION_2_5>::is_encrypted_blob() const {return _isEncryptedBlob;}-size_t Metadata<METADATA_VERSION_2_0>::get_metadata_size() const {- return sizeof(_version) + _ovVersion.get_openvino_version_size();-}--size_t Metadata<METADATA_VERSION_2_1>::get_metadata_size() const {- size_t metadataSize = Metadata<METADATA_VERSION_2_0>::get_metadata_size() + sizeof(_numberOfInits);-- if (_initSizes.has_value()) {- metadataSize += _initSizes->size() * sizeof(uint64_t);- }-- return metadataSize;-}--size_t Metadata<METADATA_VERSION_2_2>::get_metadata_size() const {- size_t metadataSize = Metadata<METADATA_VERSION_2_1>::get_metadata_size() + sizeof(int64_t);-- return metadataSize;-}--size_t Metadata<METADATA_VERSION_2_3>::get_metadata_size() const {- size_t metadataSize = Metadata<METADATA_VERSION_2_2>::get_metadata_size();- // Number of input layouts & number of output layouts- metadataSize += 2 * sizeof(uint64_t);-- if (_inputLayouts.has_value()) {- for (const ov::Layout& layout : _inputLayouts.value()) {- // Length followed by the layout value as string- metadataSize += sizeof(uint16_t) + layout.to_string().size();- }- }- if (_outputLayouts.has_value()) {- for (const ov::Layout& layout : _outputLayouts.value()) {- metadataSize += sizeof(uint16_t) + layout.to_string().size();- }- }-- return metadataSize;-}--size_t Metadata<METADATA_VERSION_2_4>::get_metadata_size() const {- return Metadata<METADATA_VERSION_2_3>::get_metadata_size() + sizeof(_compilerVersion.value());-}--size_t Metadata<METADATA_VERSION_2_5>::get_metadata_size() const {- size_t metadataSize = Metadata<METADATA_VERSION_2_4>::get_metadata_size() +- sizeof(uint8_t); // intermediate uint8_t will be used to write bool due to possible platform- // different sizes of bool-- return metadataSize;-}-} // namespace intel_npudiff --git a/src/plugins/intel_npu/src/plugin/src/plugin.cpp b/src/plugins/intel_npu/src/plugin/src/plugin.cppindex c29be097fb1..d939accff0c 100644--- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp+++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp@@ -858,7 +858,8 @@ std::shared_ptr<ov::ICompiledModel> Plugin::parse(const ov::Tensor& tensorBig,}ov::Tensor tensor = tensorBig;- if (isNotNullDecryption && (metadata == nullptr || (metadata != nullptr && metadata->is_encrypted_blob()))) {+ if (isNotNullDecryption &&+ (metadata == nullptr || (metadata != nullptr && metadata->is_encrypted_blob().value_or(false)))) {{std::string decryptedBlobStr;{@@ -886,7 +887,7 @@ std::shared_ptr<ov::ICompiledModel> Plugin::parse(const ov::Tensor& tensorBig,std::optional<int64_t> batchSize = std::nullopt;if (metadata) {- if (metadata->is_encrypted_blob() && !isNotNullDecryption) {+ if (metadata->is_encrypted_blob().value_or(false) && !isNotNullDecryption) {OPENVINO_THROW("Blob is encrypted, but no decryption callback was provided!");}diff --git a/src/plugins/intel_npu/tests/unit/npu/metadata/metadata_text.cpp b/src/plugins/intel_npu/tests/unit/npu/metadata/metadata_text.cppnew file mode 100644index 00000000000..fac295f660e--- /dev/null+++ b/src/plugins/intel_npu/tests/unit/npu/metadata/metadata_text.cpp@@ -0,0 +1,98 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "metadata_text.hpp"++namespace {++const auto make_key_value_field = [](std::string_view key, std::string_view val) -> std::string {+ return std::string(key) + '=' + std::string(val);+};++const std::vector<MetadataTextTest::ParamType> inputs = {+ {make_key_value_field(MetadataTextKeys::META, "2.0") + ";" + make_key_value_field(MetadataTextKeys::OV, "2026.1.0"),+ true},+ {make_key_value_field(MetadataTextKeys::META, "2.1") + ";" ++ make_key_value_field(MetadataTextKeys::OV, "2026.1.0") + ";" ++ make_key_value_field(MetadataTextKeys::WS_INITS, "1"),+ true},+ {make_key_value_field(MetadataTextKeys::META, "2.2") + ";" ++ make_key_value_field(MetadataTextKeys::OV, "2026.1.0") + ";" ++ make_key_value_field(MetadataTextKeys::BATCH, "4"),+ true},+ {make_key_value_field(MetadataTextKeys::META, "2.2") + ";" ++ make_key_value_field(MetadataTextKeys::OV, "2026.1.0") + ";" ++ make_key_value_field(MetadataTextKeys::BATCH, "4") + ";" ++ make_key_value_field(MetadataTextKeys::WS_INITS, "1"),+ true},+ {make_key_value_field(MetadataTextKeys::META, "2.2") + ";" ++ make_key_value_field(MetadataTextKeys::OV, "2026.1.0") + ";" ++ make_key_value_field(MetadataTextKeys::BATCH, "-1"),+ false},+ // order independent: metadata version field is not first+ {make_key_value_field(MetadataTextKeys::OV, "2026.1.0") + ";" + make_key_value_field(MetadataTextKeys::META, "2.0"),+ true},+ // optional (unknown) fields are ignored+ {make_key_value_field(MetadataTextKeys::META, "2.0") + ";" ++ make_key_value_field(MetadataTextKeys::OV, "2026.1.0") + ";{future_field=FUTURE_VAL}",+ true},+ {make_key_value_field(MetadataTextKeys::META, "2.0") + ";" ++ make_key_value_field(MetadataTextKeys::OV, "2026.1.0") + ";future_field=FUTURE_VAL",+ false},+ {"", false},+ {make_key_value_field(MetadataTextKeys::OV, "2026.1.0"), false},+ // metadata version malformed+ {make_key_value_field(MetadataTextKeys::META, "20") + ";" + make_key_value_field(MetadataTextKeys::OV, "2026.1.0"),+ false},+ {make_key_value_field(MetadataTextKeys::META, "2X.0") + ";" ++ make_key_value_field(MetadataTextKeys::OV, "2026.1.0"),+ false},+ {make_key_value_field(MetadataTextKeys::META, "2.0X") + ";" ++ make_key_value_field(MetadataTextKeys::OV, "2026.1.0"),+ false},+ {make_key_value_field(MetadataTextKeys::META, "2.0.1") + ";" ++ make_key_value_field(MetadataTextKeys::OV, "2026.1.0"),+ false},+ // unsupported metadata major version+ {make_key_value_field(MetadataTextKeys::META, std::to_string(CURRENT_METADATA_MAJOR_VERSION + 1) + ".0") + ";" ++ make_key_value_field(MetadataTextKeys::OV, "2026.1.0"),+ false},+ // minor version exceeds current maximum+ {make_key_value_field(MetadataTextKeys::META, "2.99") + ";" ++ make_key_value_field(MetadataTextKeys::OV, "2026.1.0"),+ false},+ {make_key_value_field(MetadataTextKeys::META, "2.0"), false},+ {make_key_value_field(MetadataTextKeys::META, "2.1") + ";" ++ make_key_value_field(MetadataTextKeys::OV, "2026.1.0") + ";" ++ make_key_value_field(MetadataTextKeys::WS_INITS, "0"),+ false},+ // unmatched closing bracket in value+ {make_key_value_field(MetadataTextKeys::META, "2.0") + ";" ++ make_key_value_field(MetadataTextKeys::OV, "2026.1.0") + ";bad=]VALUE",+ false},+ // unclosed opening bracket in value+ {make_key_value_field(MetadataTextKeys::META, "2.0") + ";" ++ make_key_value_field(MetadataTextKeys::OV, "2026.1.0") + ";bad=[VALUE",+ false},+ {make_key_value_field(MetadataTextKeys::META, "2.0") + ";" + make_key_value_field(MetadataTextKeys::OV, "2026.1"),+ false},+ {make_key_value_field(MetadataTextKeys::META, "2.0") + ";" + make_key_value_field(MetadataTextKeys::OV, "20261"),+ false},+ // too many ov version components+ {make_key_value_field(MetadataTextKeys::META, "2.0") + ";" ++ make_key_value_field(MetadataTextKeys::OV, "2026.1.0.0"),+ false},+ {make_key_value_field(MetadataTextKeys::META, "2.6") + ";;" ++ make_key_value_field(MetadataTextKeys::OV, "2026.1.0") + ";",+ false},+ {"key=VALUE;key=VALUE2", false},+ {"notavalidformat", false},+};++} // namespace++INSTANTIATE_TEST_SUITE_P(FormatValidation,+ MetadataTextTest,+ ::testing::ValuesIn(inputs),+ MetadataTextTest::getTestCaseName);diff --git a/src/plugins/intel_npu/tests/unit/npu/metadata/metadata_text.hpp b/src/plugins/intel_npu/tests/unit/npu/metadata/metadata_text.hppnew file mode 100644index 00000000000..62ec9884b54--- /dev/null+++ b/src/plugins/intel_npu/tests/unit/npu/metadata/metadata_text.hpp@@ -0,0 +1,123 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include <gtest/gtest.h>++#include <sstream>++#include "common_test_utils/test_assertions.hpp"+#include "metadata.hpp"+#include "metadata_wrappers.hpp"++using namespace intel_npu;++using MetadataUnitTests = ::testing::Test;++namespace {++std::string make_text_string(MetadataBase& meta) {+ std::ostringstream stream;+ meta.write_as_text(stream);+ return stream.str();+}++} // namespace++using MetadataHumanReadableTests = ::testing::Test;++TEST_F(MetadataHumanReadableTests, minimalMetadata) {+ auto meta = Metadata<METADATA_VERSION_2_0>(0, CURRENT_OPENVINO_VERSION);+ const auto text = make_text_string(meta);++ std::unique_ptr<MetadataBase> storedMeta;+ OV_ASSERT_NO_THROW(storedMeta = read_as_text(text));++ ASSERT_FALSE(storedMeta->get_init_sizes().has_value());+ ASSERT_FALSE(storedMeta->get_batch_size().has_value());+ ASSERT_FALSE(storedMeta->get_input_layouts().has_value());+ ASSERT_FALSE(storedMeta->get_output_layouts().has_value());+ ASSERT_FALSE(storedMeta->get_compiler_version().has_value());+}++TEST_F(MetadataHumanReadableTests, initSizes) {+ const std::vector<uint64_t> initSizes{16, 32, 64};+ auto meta = Metadata<METADATA_VERSION_2_1>(0, CURRENT_OPENVINO_VERSION, initSizes);+ const auto text = make_text_string(meta);++ std::unique_ptr<MetadataBase> storedMeta;+ OV_ASSERT_NO_THROW(storedMeta = read_as_text(text));++ ASSERT_TRUE(storedMeta->get_init_sizes().has_value());+}++TEST_F(MetadataHumanReadableTests, emptyInitSizes) {+ auto meta = Metadata<METADATA_VERSION_2_1>(0, CURRENT_OPENVINO_VERSION, std::vector<uint64_t>{});+ const auto text = make_text_string(meta);++ std::unique_ptr<MetadataBase> storedMeta;+ OV_ASSERT_NO_THROW(storedMeta = read_as_text(text));++ ASSERT_FALSE(storedMeta->get_init_sizes().has_value());+}++TEST_F(MetadataHumanReadableTests, batchSize) {+ const int64_t batchSize = 4;+ auto meta = Metadata<METADATA_VERSION_2_2>(0, CURRENT_OPENVINO_VERSION, std::nullopt, batchSize);+ const auto text = make_text_string(meta);++ std::unique_ptr<MetadataBase> storedMeta;+ OV_ASSERT_NO_THROW(storedMeta = read_as_text(text));++ ASSERT_TRUE(storedMeta->get_batch_size().has_value());+ EXPECT_EQ(storedMeta->get_batch_size().value(), batchSize);+}++TEST_F(MetadataHumanReadableTests, zeroBatchSize) {+ auto meta = Metadata<METADATA_VERSION_2_2>(0, CURRENT_OPENVINO_VERSION, std::nullopt, std::nullopt);+ const auto text = make_text_string(meta);++ std::unique_ptr<MetadataBase> storedMeta;+ OV_ASSERT_NO_THROW(storedMeta = read_as_text(text));++ ASSERT_FALSE(storedMeta->get_batch_size().has_value());+}++TEST_F(MetadataHumanReadableTests, noLayouts) {+ auto meta = Metadata<METADATA_VERSION_2_3>(0, CURRENT_OPENVINO_VERSION, std::nullopt, std::nullopt, std::nullopt);+ const auto text = make_text_string(meta);++ std::unique_ptr<MetadataBase> storedMeta;+ OV_ASSERT_NO_THROW(storedMeta = read_as_text(text));++ ASSERT_FALSE(storedMeta->get_input_layouts().has_value());+ ASSERT_FALSE(storedMeta->get_output_layouts().has_value());+}++TEST_F(MetadataHumanReadableTests, compilerVersion) {+ const uint32_t compilerVersion = 0xCAFECAFE;+ auto meta = Metadata<METADATA_VERSION_2_4>(0,+ CURRENT_OPENVINO_VERSION,+ std::nullopt,+ std::nullopt,+ std::nullopt,+ std::nullopt,+ compilerVersion);+ const auto text = make_text_string(meta);++ std::unique_ptr<MetadataBase> storedMeta;+ OV_ASSERT_NO_THROW(storedMeta = read_as_text(text));++ ASSERT_FALSE(storedMeta->get_compiler_version().has_value());+}++TEST_P(MetadataTextTest, Format) {+ std::unique_ptr<MetadataBase> meta;+ if (isValid) {+ OV_ASSERT_NO_THROW(meta = ::read_as_text(compatibilityString));+ } else {+ ASSERT_ANY_THROW(meta = ::read_as_text(compatibilityString));+ }+}diff --git a/src/plugins/intel_npu/tests/unit/npu/metadata_version.cpp b/src/plugins/intel_npu/tests/unit/npu/metadata/metadata_version.cppsimilarity index 88%rename from src/plugins/intel_npu/tests/unit/npu/metadata_version.cpprename to src/plugins/intel_npu/tests/unit/npu/metadata/metadata_version.cppindex b7a099bf58c..e59ef128fd6 100644--- a/src/plugins/intel_npu/tests/unit/npu/metadata_version.cpp+++ b/src/plugins/intel_npu/tests/unit/npu/metadata/metadata_version.cpp@@ -6,26 +6,12 @@#include "common_test_utils/test_assertions.hpp"#include "metadata.hpp"-#include "openvino/core/version.hpp"+#include "metadata_wrappers.hpp"using namespace intel_npu;using MetadataUnitTests = ::testing::Test;-struct MetadataTest : Metadata<CURRENT_METADATA_VERSION> {- MetadataTest(uint64_t blobSize,- const std::optional<OpenvinoVersion>& ovVersion,- const std::optional<std::vector<uint64_t>>& initSizes = std::nullopt,- const std::optional<int64_t> batchSize = std::nullopt,- const std::optional<std::vector<ov::Layout>>& inputLayouts = std::nullopt,- const std::optional<std::vector<ov::Layout>>& outputLayouts = std::nullopt)- : Metadata<CURRENT_METADATA_VERSION>(blobSize, ovVersion, initSizes, batchSize, inputLayouts, outputLayouts) {}-- void set_version(uint32_t newVersion) {- _version = newVersion;- }-};-TEST_F(MetadataUnitTests, readUnversionedBlob) {std::stringstream blob("this_is an_unversioned bl0b");@@ -192,32 +178,6 @@ TEST_F(MetadataUnitTests, writeAndReadMetadataWithNewerMinorVersion) {ASSERT_ANY_THROW(storedMeta = read_metadata_from(tensor));}-struct MetadataVersionTestFixture : Metadata<CURRENT_METADATA_VERSION>, ::testing::TestWithParam<uint32_t> {-public:- std::stringstream blob;-- void set_version(uint32_t newVersion) {- _version = newVersion;- }-- MetadataVersionTestFixture() : Metadata<CURRENT_METADATA_VERSION>(0, std::nullopt) {}-- MetadataVersionTestFixture(uint64_t blobSize, std::optional<OpenvinoVersion> ovVersion)- : Metadata<CURRENT_METADATA_VERSION>(blobSize, ovVersion) {}-- void TestBody() override {}-- static std::string getTestCaseName(const testing::TestParamInfo<MetadataVersionTestFixture::ParamType>& info);-};--std::string MetadataVersionTestFixture::getTestCaseName(- const testing::TestParamInfo<MetadataVersionTestFixture::ParamType>& info) {- std::ostringstream result;- result << "major version=" << MetadataBase::get_major(info.param)- << ", minor version=" << MetadataBase::get_minor(info.param);- return result.str();-}-TEST_P(MetadataVersionTestFixture, writeAndReadInvalidMetadataVersion) {uint32_t metaVersion = GetParam();if (CURRENT_METADATA_MAJOR_VERSION == MetadataBase::get_major(metaVersion) && CURRENT_METADATA_MINOR_VERSION == 0) {diff --git a/src/plugins/intel_npu/tests/unit/npu/metadata/metadata_wrappers.hpp b/src/plugins/intel_npu/tests/unit/npu/metadata/metadata_wrappers.hppnew file mode 100644index 00000000000..651ec108a8e--- /dev/null+++ b/src/plugins/intel_npu/tests/unit/npu/metadata/metadata_wrappers.hpp@@ -0,0 +1,75 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include <gtest/gtest.h>++#include "metadata.hpp"++using namespace intel_npu;++struct MetadataTest : Metadata<CURRENT_METADATA_VERSION> {+ MetadataTest(uint64_t blobSize,+ const std::optional<OpenvinoVersion>& ovVersion,+ const std::optional<std::vector<uint64_t>>& initSizes = std::nullopt,+ const std::optional<int64_t>& batchSize = std::nullopt,+ const std::optional<std::vector<ov::Layout>>& inputLayouts = std::nullopt,+ const std::optional<std::vector<ov::Layout>>& outputLayouts = std::nullopt,+ const std::optional<uint32_t>& compilerVersion = std::nullopt,+ const std::optional<uint64_t>& blobSizeAfterEncryption = std::nullopt)+ : Metadata<CURRENT_METADATA_VERSION>(blobSize,+ ovVersion,+ initSizes,+ batchSize,+ inputLayouts,+ outputLayouts,+ compilerVersion,+ blobSizeAfterEncryption) {}++ void set_version(uint32_t newVersion) {+ _version = newVersion;+ }+};++struct MetadataVersionTestFixture : Metadata<CURRENT_METADATA_VERSION>, ::testing::TestWithParam<uint32_t> {+public:+ void set_version(uint32_t newVersion) {+ _version = newVersion;+ }++ MetadataVersionTestFixture() : Metadata<CURRENT_METADATA_VERSION>(0, std::nullopt) {}++ MetadataVersionTestFixture(uint64_t blobSize, std::optional<OpenvinoVersion> ovVersion)+ : Metadata<CURRENT_METADATA_VERSION>(blobSize, ovVersion) {}++ void TestBody() override {}++ static std::string getTestCaseName(const testing::TestParamInfo<MetadataVersionTestFixture::ParamType>& info) {+ std::ostringstream result;+ result << "majorVersion=" << MetadataBase::get_major(info.param)+ << "_minorVersion=" << MetadataBase::get_minor(info.param);+ return result.str();+ }++ std::stringstream blob;+};++struct MetadataTextTest : Metadata<CURRENT_METADATA_VERSION>, ::testing::TestWithParam<std::tuple<std::string, bool>> {+public:+ MetadataTextTest() : Metadata<CURRENT_METADATA_VERSION>(0, std::nullopt) {}++ void SetUp() override {+ isValid = std::get<1>(GetParam());+ compatibilityString = std::get<0>(GetParam());+ }++ static std::string getTestCaseName(const testing::TestParamInfo<MetadataTextTest::ParamType>& info) {+ return "compatibilityString=\"" + std::get<0>(info.param) ++ "\"_isValid=" + (std::get<1>(info.param) ? "true" : "false");+ }++ std::string compatibilityString;+ bool isValid;+};