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.
Cherry-pick from
alexandruenache1111:blob_stream_commit
- Loading branch information
1 parent
99b823b
commit 7bfc852
Showing
11 changed files
with
403 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include <stdint.h> | ||
|
||
#include <memory> | ||
#include <string> | ||
#include <vector> | ||
|
||
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 ~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 { | ||
protected: | ||
uint32_t _version; | ||
OpenvinoVersion _ovVersion; | ||
|
||
public: | ||
Metadata(); | ||
|
||
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); | ||
}; | ||
|
||
/** | ||
* @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); | ||
|
||
/** | ||
* @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(const std::vector<uint8_t>& blob); | ||
|
||
} // 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
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,124 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "metadata.hpp" | ||
|
||
#include <cstring> | ||
#include <sstream> | ||
|
||
#include "intel_npu/config/config.hpp" | ||
#include "intel_npu/utils/logger/logger.hpp" | ||
#include "openvino/core/version.hpp" | ||
|
||
namespace intel_npu { | ||
|
||
OpenvinoVersion::OpenvinoVersion(std::string_view version) | ||
: _version(version), | ||
_size(static_cast<uint32_t>(version.size())) {} | ||
|
||
void OpenvinoVersion::read(std::istream& stream) { | ||
stream.read(reinterpret_cast<char*>(&_size), sizeof(_size)); | ||
_version.resize(_size); | ||
stream.read(_version.data(), _size); | ||
} | ||
|
||
Metadata<METADATA_VERSION_1_0>::Metadata() | ||
: _version{METADATA_VERSION_1_0}, | ||
_ovVersion{ov::get_openvino_version().buildNumber} {} | ||
|
||
void Metadata<METADATA_VERSION_1_0>::read(std::istream& stream) { | ||
_ovVersion.read(stream); | ||
} | ||
|
||
uint32_t& OpenvinoVersion::get_size() { | ||
return _size; | ||
} | ||
|
||
void Metadata<METADATA_VERSION_1_0>::write(std::ostream& stream) { | ||
stream.write(reinterpret_cast<const char*>(&_version), sizeof(_version)); | ||
stream.write(reinterpret_cast<const char*>(&_ovVersion.get_size()), sizeof(_ovVersion.get_size())); | ||
stream.write(_ovVersion.get_version().data(), _ovVersion.get_version().size()); | ||
} | ||
|
||
std::unique_ptr<MetadataBase> create_metadata(uint32_t version) { | ||
switch (version) { | ||
case METADATA_VERSION_1_0: | ||
return std::make_unique<Metadata<METADATA_VERSION_1_0>>(); | ||
|
||
default: | ||
OPENVINO_THROW("Invalid metadata version!"); | ||
} | ||
} | ||
|
||
std::string OpenvinoVersion::get_version() { | ||
return _version; | ||
} | ||
|
||
bool Metadata<METADATA_VERSION_1_0>::is_compatible() { | ||
Logger logger("NPUPlugin", Logger::global().level()); | ||
// checking if we can import the blob | ||
if (_ovVersion.get_version() != ov::get_openvino_version().buildNumber) { | ||
logger.warning("Imported blob OpenVINO version: %s, but the current OpenVINO version is: %s", | ||
_ovVersion.get_version().c_str(), | ||
ov::get_openvino_version().buildNumber); | ||
|
||
#ifdef NPU_PLUGIN_DEVELOPER_BUILD | ||
if (auto envVar = std::getenv("NPU_DISABLE_VERSION_CHECK")) { | ||
if (envVarStrToBool("NPU_DISABLE_VERSION_CHECK", envVar)) { | ||
return true; | ||
} | ||
} | ||
#endif | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
std::unique_ptr<MetadataBase> read_metadata_from(const std::vector<uint8_t>& blob) { | ||
Logger logger("NPUPlugin", Logger::global().level()); | ||
size_t magicBytesSize = MAGIC_BYTES.size(); | ||
std::string blobMagicBytes; | ||
blobMagicBytes.resize(magicBytesSize); | ||
|
||
auto metadataIterator = blob.end() - magicBytesSize; | ||
std::memcpy(blobMagicBytes.data(), &(*metadataIterator), magicBytesSize); | ||
if (MAGIC_BYTES != blobMagicBytes) { | ||
OPENVINO_THROW("Blob is missing NPU metadata!"); | ||
} | ||
|
||
uint64_t blobDataSize; | ||
metadataIterator -= sizeof(blobDataSize); | ||
std::memcpy(&blobDataSize, &(*metadataIterator), sizeof(blobDataSize)); | ||
metadataIterator = blob.begin() + blobDataSize; | ||
|
||
std::stringstream metadataStream; | ||
metadataStream.write(reinterpret_cast<const char*>(&(*metadataIterator)), | ||
blob.end() - metadataIterator - sizeof(blobDataSize)); | ||
|
||
uint32_t metaVersion; | ||
metadataStream.read(reinterpret_cast<char*>(&metaVersion), sizeof(metaVersion)); | ||
|
||
std::unique_ptr<MetadataBase> storedMeta; | ||
try { | ||
storedMeta = create_metadata(metaVersion); | ||
storedMeta->read(metadataStream); | ||
} catch(...) { | ||
logger.warning("Imported blob metadata version: %d.%d, but the current version is: %d.%d", | ||
get_major(metaVersion), | ||
get_minor(metaVersion), | ||
get_major(CURRENT_METADATA_VERSION), | ||
get_minor(CURRENT_METADATA_VERSION)); | ||
} | ||
return storedMeta; | ||
} | ||
|
||
void Metadata<METADATA_VERSION_1_0>::set_version(uint32_t newVersion) { | ||
_version = newVersion; | ||
} | ||
|
||
void Metadata<METADATA_VERSION_1_0>::set_ov_version(const OpenvinoVersion& newVersion) { | ||
_ovVersion = newVersion; | ||
} | ||
|
||
} // namespace intel_npu |
Oops, something went wrong.