-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alexandru Enache <[email protected]> Full versions read from vector Signed-off-by: Alexandru Enache <[email protected]> Update with the progress Update again Write from metadata serialized vector works Fixed reading functions and metadata_v3 is now functional Move versioning stuff to separate files check_blob_version is now a standalone function Remove usage of designated initializers Investigating about header mismatch throw Add null terminator for blobVersionHeader Fix header comparation Solution with template and iterator+memcpy Minor tweaks Use stream instead of memcpy Remove unused stream argument for check_blob_version Fix checks for header and size Added factory function which enables template usage with runtime vars Move back logger_debug print Working on the compatibility function Fixed openvino commit read from blob Split and rename check_blob_version into read_metadata_from + individual code in import_model Clang format and lib includes separation isCompatible tweaks and moved .data() into .write() Added switch case for minor version Comments, logger messages update Change appended layout to blob Signed-off-by: Alexandru Enache <[email protected]>
- Loading branch information
1 parent
090da7b
commit aaba4a6
Showing
4 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
src/plugins/intel_npu/src/plugin/include/model_version.hpp
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,60 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include <stdint.h> | ||
|
||
#include <memory> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace intel_npu { | ||
|
||
constexpr std::string_view DELIMITER = "OVNPU"; | ||
|
||
constexpr int CURRENT_METAVERSION_MAJOR = 1; | ||
constexpr int CURRENT_METAVERSION_MINOR = 0; | ||
|
||
struct MetadataVersion { | ||
uint32_t major; | ||
uint32_t minor; | ||
}; | ||
|
||
struct OpenvinoVersion { | ||
std::string version; | ||
uint32_t size; | ||
|
||
OpenvinoVersion(const std::string& version); | ||
|
||
void read(std::istream& stream); | ||
}; | ||
|
||
struct MetadataBase { | ||
virtual void read(std::istream& stream) = 0; | ||
virtual void write(std::ostream& stream) = 0; | ||
virtual bool isCompatible() = 0; | ||
virtual ~MetadataBase() = default; | ||
}; | ||
|
||
template <int Major, int Minor> | ||
struct Metadata : public MetadataBase {}; | ||
|
||
template <> | ||
struct Metadata<1, 0> : public MetadataBase { | ||
MetadataVersion version; | ||
OpenvinoVersion ovVersion; | ||
|
||
Metadata(); | ||
|
||
void read(std::istream& stream) override; | ||
|
||
void write(std::ostream& stream) override; | ||
|
||
bool isCompatible() override; | ||
}; | ||
|
||
std::unique_ptr<MetadataBase> createMetadata(int major, int minor); | ||
|
||
std::unique_ptr<MetadataBase> read_metadata_from(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,97 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "model_version.hpp" | ||
|
||
#include <cstring> | ||
#include <sstream> | ||
|
||
#include "intel_npu/utils/logger/logger.hpp" | ||
#include "openvino/core/version.hpp" | ||
|
||
namespace intel_npu { | ||
|
||
OpenvinoVersion::OpenvinoVersion(const std::string& version) { | ||
this->version = version; | ||
this->size = static_cast<uint32_t>(version.size()); | ||
} | ||
|
||
void OpenvinoVersion::read(std::istream& stream) { | ||
stream.read(reinterpret_cast<char*>(&size), sizeof(size)); | ||
stream.read(&version[0], size); | ||
} | ||
|
||
Metadata<1, 0>::Metadata() : version{1, 0}, ovVersion{ov::get_openvino_version().buildNumber} {} | ||
|
||
void Metadata<1, 0>::read(std::istream& stream) { | ||
ovVersion.read(stream); | ||
} | ||
|
||
void Metadata<1, 0>::write(std::ostream& stream) { | ||
stream.write(reinterpret_cast<const char*>(&version.major), sizeof(version.major)); | ||
stream.write(reinterpret_cast<const char*>(&version.minor), sizeof(version.minor)); | ||
|
||
stream.write(reinterpret_cast<const char*>(&ovVersion.size), sizeof(ovVersion.size)); | ||
stream.write(ovVersion.version.c_str(), ovVersion.version.size()); | ||
} | ||
|
||
std::unique_ptr<MetadataBase> createMetadata(int major, int minor) { | ||
switch (major) { | ||
case 1: | ||
switch (minor) { | ||
case 0: | ||
return std::make_unique<Metadata<1, 0>>(); | ||
|
||
default: | ||
return nullptr; | ||
} | ||
|
||
default: | ||
return nullptr; | ||
} | ||
} | ||
|
||
bool Metadata<1, 0>::isCompatible() { | ||
// checking if we still support the format | ||
// but is checking `Major` redundant since it's checked in createMetadata? | ||
if (version.major != CURRENT_METAVERSION_MAJOR || version.minor != CURRENT_METAVERSION_MINOR) { | ||
return false; | ||
} | ||
// checking if we can import the blob | ||
return ovVersion.version == ov::get_openvino_version().buildNumber; | ||
} | ||
|
||
std::unique_ptr<MetadataBase> read_metadata_from(std::vector<uint8_t>& blob) { | ||
Logger _logger("NPUPlugin", Logger::global().level()); | ||
size_t delimiterSize = DELIMITER.size(); | ||
std::string blobVersionHeader(delimiterSize, '\0'); | ||
|
||
auto metadataIterator = blob.end() - delimiterSize; | ||
memcpy(blobVersionHeader.data(), &(*metadataIterator), delimiterSize); | ||
if (DELIMITER != blobVersionHeader) { | ||
_logger.error("Blob is not versioned"); | ||
return nullptr; | ||
} | ||
|
||
size_t blobDataSize; | ||
metadataIterator -= sizeof(blobDataSize); | ||
memcpy(&blobDataSize, &(*metadataIterator), sizeof(blobDataSize)); | ||
|
||
metadataIterator = blob.begin() + blobDataSize; | ||
std::stringstream metadataStream; | ||
metadataStream.write(reinterpret_cast<const char*>(&(*metadataIterator)), | ||
blob.end() - metadataIterator - sizeof(blobDataSize)); | ||
|
||
MetadataVersion metaVersion; | ||
metadataStream.read(reinterpret_cast<char*>(&metaVersion.major), sizeof(metaVersion.major)); | ||
metadataStream.read(reinterpret_cast<char*>(&metaVersion.minor), sizeof(metaVersion.minor)); | ||
|
||
auto storedMeta = createMetadata(metaVersion.major, metaVersion.minor); | ||
if (storedMeta != nullptr) { | ||
storedMeta->read(metadataStream); | ||
} | ||
return storedMeta; | ||
} | ||
|
||
} // 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