forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eea8d49
commit 6304471
Showing
12 changed files
with
353 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include <stdint.h> | ||
|
||
#include <memory> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include <openvino/runtime/aligned_buffer.hpp> | ||
|
||
namespace intel_npu { | ||
|
||
/** | ||
* @brief Magic bytes used for identifying NPU blobs. | ||
*/ | ||
constexpr std::string_view MAGIC_BYTES = "OVNPU"; | ||
|
||
/** | ||
* @brief Returns a uint32_t value which represents two uint16_t values concatenated. | ||
* @details Convention for bumping the metadata version: | ||
* - Increment Major in case of: removing a current field OR adding a new field in between fields. | ||
* - Increment Minor in case of: adding a new field at the end. | ||
* | ||
* @return Major and minor versions concatenated into a single uint32_t value. | ||
*/ | ||
constexpr uint32_t make_version(uint16_t major, uint16_t minor) { | ||
return major << 16 | (minor & 0x0000ffff); | ||
} | ||
|
||
/** | ||
* @brief Gets the major version. | ||
* | ||
* @return Major version. | ||
*/ | ||
constexpr uint16_t get_major(uint32_t version) { | ||
return static_cast<uint16_t>(version >> 16); | ||
} | ||
|
||
/** | ||
* @brief Gets the minor version. | ||
* | ||
* @return Minor version. | ||
*/ | ||
constexpr uint16_t get_minor(uint32_t version) { | ||
return static_cast<uint16_t>(version); | ||
} | ||
|
||
/** | ||
* @brief List of supported version formats. | ||
*/ | ||
constexpr uint32_t METADATA_VERSION_1_0{make_version(1, 0)}; | ||
|
||
/** | ||
* @brief Current metadata version. | ||
*/ | ||
constexpr uint32_t CURRENT_METADATA_VERSION{METADATA_VERSION_1_0}; | ||
|
||
struct OpenvinoVersion { | ||
private: | ||
std::string _version; | ||
uint32_t _size; | ||
|
||
public: | ||
OpenvinoVersion(std::string_view version); | ||
|
||
/** | ||
* @brief Reads version data from a stream. | ||
*/ | ||
void read(std::istream& stream); | ||
|
||
/** | ||
* @brief Gets the version string. | ||
*/ | ||
std::string get_version(); | ||
|
||
/** | ||
* @brief Gets the size of version string as reference. | ||
* @return Reference to the size. | ||
* | ||
* @note Needed as reference for reading its binary representation in memory. | ||
* @see Metadata::write() | ||
*/ | ||
uint32_t& get_size(); | ||
}; | ||
|
||
struct MetadataBase { | ||
/** | ||
* @brief Reads metadata from a stream. | ||
*/ | ||
virtual void read(std::istream& stream) = 0; | ||
|
||
/** | ||
* @brief Writes metadata to a stream. | ||
*/ | ||
virtual void write(std::ostream& stream) = 0; | ||
|
||
virtual bool is_compatible() = 0; | ||
|
||
virtual uint64_t get_blob_size() const = 0; | ||
|
||
virtual size_t get_ov_header_offset() const = 0; | ||
|
||
virtual ~MetadataBase() = default; | ||
}; | ||
|
||
/** | ||
* @brief Template for metadata class handling. | ||
* | ||
* @attention It's a must to have metadata version as first field in any metadata specialization. | ||
*/ | ||
template <uint32_t version> | ||
struct Metadata : public MetadataBase {}; | ||
|
||
/** | ||
* @brief Template specialization for metadata version 1.0. | ||
*/ | ||
template <> | ||
struct Metadata<METADATA_VERSION_1_0> : public MetadataBase { | ||
private: | ||
uint64_t _blobDataSize; | ||
uint32_t _version; | ||
size_t _ovHeaderOffset; | ||
OpenvinoVersion _ovVersion; | ||
|
||
public: | ||
Metadata(); | ||
|
||
Metadata(size_t ovHeaderOffset, uint64_t blobDataSize); | ||
|
||
void read(std::istream& stream) override; | ||
|
||
void write(std::ostream& stream) override; | ||
|
||
/** | ||
* @brief Checks if metadata is supported. | ||
* | ||
* @return Returns: | ||
* - false: | ||
* - if blob metadata does not match current metadata. | ||
* - if blob OpenVINO version does not match current one. | ||
* | ||
* - true: if all versions match. | ||
* | ||
* @note The version check can be disabled if the "NPU_DISABLE_VERSION_CHECK" environment variable is set to '1'. | ||
*/ | ||
bool is_compatible() override; | ||
|
||
void set_version(uint32_t newVersion); | ||
|
||
void set_ov_version(const OpenvinoVersion& newVersion); | ||
|
||
uint64_t get_blob_size() const override; | ||
|
||
size_t get_ov_header_offset() const override; | ||
}; | ||
|
||
/** | ||
* @brief Creates a Metadata object. | ||
* | ||
* @return Unique pointer to the created MetadataBase object if the major version is supported; otherwise, returns | ||
* 'nullptr'. | ||
*/ | ||
std::unique_ptr<MetadataBase> create_metadata(uint32_t version, size_t ovHeaderOffset, uint64_t blobDataSize); | ||
|
||
/** | ||
* @brief Reads metadata from a blob. | ||
* | ||
* @return If the blob is versioned and its major version is supported, returns an unique pointer to the read | ||
* MetadataBase object; otherwise, returns 'nullptr'. | ||
*/ | ||
std::unique_ptr<MetadataBase> read_metadata_from(std::istream& stream); | ||
|
||
std::unique_ptr<MetadataBase> read_metadata_from(std::istream& stream, const std::shared_ptr<ov::AlignedBuffer>& modelBuffer); | ||
} // namespace intel_npu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.