diff --git a/src/plugins/intel_npu/src/common/include/intel_npu/common/blob_container.hpp b/src/plugins/intel_npu/src/common/include/intel_npu/common/blob_container.hpp index 13f833ddfccc88..ed5bb92f473002 100644 --- a/src/plugins/intel_npu/src/common/include/intel_npu/common/blob_container.hpp +++ b/src/plugins/intel_npu/src/common/include/intel_npu/common/blob_container.hpp @@ -47,19 +47,17 @@ class BlobContainerVector : public BlobContainer { class BlobContainerAlignedBuffer : public BlobContainer { public: BlobContainerAlignedBuffer(const std::shared_ptr& blobSO, - size_t ovHeaderOffset, - size_t metadataSize) - : _ownershipBlob(blobSO), + size_t ovHeaderOffset, uint64_t blobSize) + : _blobSize(blobSize), _ovHeaderOffset(ovHeaderOffset), - _metadataSize(metadataSize) {} + _ownershipBlob(blobSO) {} void* get_ptr() override { return _ownershipBlob->get_ptr(_ovHeaderOffset); } size_t size() const override { - // remove OV header offset and metadata from blob size - return _ownershipBlob->size() - _ovHeaderOffset - _metadataSize; + return _blobSize; } bool release_from_memory() override { @@ -67,9 +65,9 @@ class BlobContainerAlignedBuffer : public BlobContainer { } private: - std::shared_ptr _ownershipBlob; + uint64_t _blobSize; size_t _ovHeaderOffset; - size_t _metadataSize; + std::shared_ptr _ownershipBlob; }; } // namespace intel_npu diff --git a/src/plugins/intel_npu/src/common/include/intel_npu/common/igraph.hpp b/src/plugins/intel_npu/src/common/include/intel_npu/common/igraph.hpp index 37447859e42b27..86d6902a79bff8 100644 --- a/src/plugins/intel_npu/src/common/include/intel_npu/common/igraph.hpp +++ b/src/plugins/intel_npu/src/common/include/intel_npu/common/igraph.hpp @@ -24,7 +24,7 @@ class IGraph : public std::enable_shared_from_this { const Config& config, std::unique_ptr blobPtr); - virtual void export_blob(std::ostream& stream) const = 0; + virtual size_t export_blob(std::ostream& stream) const = 0; virtual std::vector process_profiling_output(const std::vector& profData, const Config& config) const = 0; diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/driver_graph.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/driver_graph.hpp index 25af83a82f2490..2c845227e8aac7 100644 --- a/src/plugins/intel_npu/src/compiler_adapter/include/driver_graph.hpp +++ b/src/plugins/intel_npu/src/compiler_adapter/include/driver_graph.hpp @@ -23,7 +23,7 @@ class DriverGraph final : public IGraph { const Config& config, std::unique_ptr blob); - void export_blob(std::ostream& stream) const override; + size_t export_blob(std::ostream& stream) const override; std::vector process_profiling_output(const std::vector& profData, const Config& config) const override; diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/plugin_graph.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/plugin_graph.hpp index 5d0ab241bcd9c8..0d006f820c199d 100644 --- a/src/plugins/intel_npu/src/compiler_adapter/include/plugin_graph.hpp +++ b/src/plugins/intel_npu/src/compiler_adapter/include/plugin_graph.hpp @@ -26,7 +26,7 @@ class PluginGraph final : public IGraph { std::unique_ptr blobPtr, const Config& config); - void export_blob(std::ostream& stream) const override; + size_t export_blob(std::ostream& stream) const override; std::vector process_profiling_output(const std::vector& profData, const Config& config) const override; diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/driver_graph.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/driver_graph.cpp index 5ce5cc9f1b39ff..20c38a73a67fdf 100644 --- a/src/plugins/intel_npu/src/compiler_adapter/src/driver_graph.cpp +++ b/src/plugins/intel_npu/src/compiler_adapter/src/driver_graph.cpp @@ -32,9 +32,9 @@ DriverGraph::DriverGraph(const std::shared_ptr& zeGraphExt, initialize(config); } -void DriverGraph::export_blob(std::ostream& stream) const { +size_t DriverGraph::export_blob(std::ostream& stream) const { const uint8_t* blobPtr = nullptr; - size_t blobSize = -1; + size_t blobSize; std::vector blob; if (_blobIsReleased) { @@ -47,7 +47,7 @@ void DriverGraph::export_blob(std::ostream& stream) const { if (!stream) { _logger.error("Write blob to stream failed. Blob is broken!"); - return; + return 0; } if (_logger.level() >= ov::log::Level::INFO) { @@ -61,6 +61,7 @@ void DriverGraph::export_blob(std::ostream& stream) const { _logger.info(str.str().c_str()); } _logger.info("Write blob to stream successfully."); + return blobSize; } std::vector DriverGraph::process_profiling_output(const std::vector& profData, diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/plugin_graph.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/plugin_graph.cpp index 4d48dbe89efaa3..90e531d5b940c7 100644 --- a/src/plugins/intel_npu/src/compiler_adapter/src/plugin_graph.cpp +++ b/src/plugins/intel_npu/src/compiler_adapter/src/plugin_graph.cpp @@ -30,12 +30,12 @@ PluginGraph::PluginGraph(const std::shared_ptr& zeGraphExt, initialize(config); } -void PluginGraph::export_blob(std::ostream& stream) const { +size_t PluginGraph::export_blob(std::ostream& stream) const { stream.write(reinterpret_cast(_blobPtr->get_ptr()), _blobPtr->size()); if (!stream) { _logger.error("Write blob to stream failed. Blob is broken!"); - return; + return 0; } if (_logger.level() >= ov::log::Level::INFO) { @@ -51,6 +51,7 @@ void PluginGraph::export_blob(std::ostream& stream) const { _logger.info(str.str().c_str()); } _logger.info("Write blob to stream successfully."); + return _blobPtr->size(); } std::vector PluginGraph::process_profiling_output(const std::vector& profData, diff --git a/src/plugins/intel_npu/src/plugin/include/metadata.hpp b/src/plugins/intel_npu/src/plugin/include/metadata.hpp new file mode 100644 index 00000000000000..78af3f9e5c5bef --- /dev/null +++ b/src/plugins/intel_npu/src/plugin/include/metadata.hpp @@ -0,0 +1,178 @@ +// Copyright (C) 2018-2024 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#pragma once + +#include + +#include +#include +#include + +#include + +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(version >> 16); +} + +/** + * @brief Gets the minor version. + * + * @return Minor version. + */ +constexpr uint16_t get_minor(uint32_t version) { + return static_cast(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 +struct Metadata : public MetadataBase {}; + +/** + * @brief Template specialization for metadata version 1.0. + */ +template <> +struct Metadata : 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 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 read_metadata_from(std::istream& stream); + +std::unique_ptr read_metadata_from(std::istream& stream, const std::shared_ptr& modelBuffer); +} // namespace intel_npu diff --git a/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp index 506502c819d72e..7d46e14db8a980 100644 --- a/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp +++ b/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp @@ -13,6 +13,7 @@ #include "intel_npu/config/compiler.hpp" #include "intel_npu/config/config.hpp" #include "intel_npu/config/runtime.hpp" +#include "metadata.hpp" #include "openvino/pass/constant_folding.hpp" #include "openvino/pass/manager.hpp" #include "openvino/runtime/properties.hpp" @@ -72,7 +73,12 @@ std::shared_ptr CompiledModel::create_sync_infer_request( void CompiledModel::export_model(std::ostream& stream) const { _logger.debug("CompiledModel::export_model"); - _graph->export_blob(stream); + size_t blobSizeBeforeVersioning = _graph->export_blob(stream); + + auto meta = Metadata(); + meta.write(stream); + stream.write(reinterpret_cast(&blobSizeBeforeVersioning), sizeof(blobSizeBeforeVersioning)); + stream.write(MAGIC_BYTES.data(), MAGIC_BYTES.size()); } std::shared_ptr CompiledModel::get_runtime_model() const { diff --git a/src/plugins/intel_npu/src/plugin/src/metadata.cpp b/src/plugins/intel_npu/src/plugin/src/metadata.cpp index 175df59b42e713..a4dc76fb84fc18 100644 --- a/src/plugins/intel_npu/src/plugin/src/metadata.cpp +++ b/src/plugins/intel_npu/src/plugin/src/metadata.cpp @@ -10,6 +10,34 @@ #include "intel_npu/config/config.hpp" #include "intel_npu/utils/logger/logger.hpp" #include "openvino/core/version.hpp" +#include "openvino/runtime/shared_buffer.hpp" +namespace { + +size_t getFileSize(std::istream& stream) { + auto log = intel_npu::Logger::global().clone("getFileSize"); + if (!stream) { + OPENVINO_THROW("Stream is in bad status! Please check the passed stream status!"); + } + + const size_t streamStart = stream.tellg(); + stream.seekg(0, std::ios_base::end); + const size_t streamEnd = stream.tellg(); + stream.seekg(streamStart, std::ios_base::beg); + + log.debug("Read blob size: streamStart=%zu, streamEnd=%zu", streamStart, streamEnd); + + if (streamEnd < streamStart) { + OPENVINO_THROW("Invalid stream size: streamEnd (", + streamEnd, + ") is not larger than streamStart (", + streamStart, + ")!"); + } + + return streamEnd - streamStart; +} + +} // namespace namespace intel_npu { @@ -24,7 +52,15 @@ void OpenvinoVersion::read(std::istream& stream) { } Metadata::Metadata() - : _version{METADATA_VERSION_1_0}, + : _blobDataSize{0}, + _version{METADATA_VERSION_1_0}, + _ovHeaderOffset{0}, + _ovVersion{ov::get_openvino_version().buildNumber} {} + +Metadata::Metadata(size_t ovHeaderOffset, uint64_t blobDataSize) + : _blobDataSize{blobDataSize}, + _version{METADATA_VERSION_1_0}, + _ovHeaderOffset{ovHeaderOffset}, _ovVersion{ov::get_openvino_version().buildNumber} {} void Metadata::read(std::istream& stream) { @@ -41,13 +77,13 @@ void Metadata::write(std::ostream& stream) { stream.write(_ovVersion.get_version().data(), _ovVersion.get_version().size()); } -std::unique_ptr create_metadata(uint32_t version) { +std::unique_ptr create_metadata(uint32_t version, size_t ovHeaderOffset, uint64_t blobDataSize) { switch (version) { case METADATA_VERSION_1_0: - return std::make_unique>(); + return std::make_unique>(ovHeaderOffset, blobDataSize); default: - OPENVINO_THROW("Invalid metadata version!"); + return nullptr; } } @@ -75,35 +111,69 @@ bool Metadata::is_compatible() { return true; } -std::unique_ptr read_metadata_from(const std::vector& blob) { +std::unique_ptr read_metadata_from(std::istream& stream) { 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); + size_t currentStreamPos = stream.tellg(); + size_t streamSize = getFileSize(stream); + stream.seekg(streamSize - magicBytesSize, std::ios::beg); + stream.read(blobMagicBytes.data(), magicBytesSize); if (MAGIC_BYTES != blobMagicBytes) { - OPENVINO_THROW("Blob is missing NPU metadata!"); + logger.error("Blob is missing NPU metadata!"); + return nullptr; } uint64_t blobDataSize; - metadataIterator -= sizeof(blobDataSize); - std::memcpy(&blobDataSize, &(*metadataIterator), sizeof(blobDataSize)); - metadataIterator = blob.begin() + blobDataSize; + stream.seekg(streamSize - magicBytesSize - sizeof(blobDataSize), std::ios::beg); + stream.read(reinterpret_cast(&blobDataSize), sizeof(blobDataSize)); + stream.seekg(currentStreamPos + blobDataSize, std::ios::beg); + + uint32_t metaVersion; + stream.read(reinterpret_cast(&metaVersion), sizeof(metaVersion)); + + auto storedMeta = create_metadata(metaVersion, currentStreamPos, blobDataSize); + if (storedMeta != nullptr) { + storedMeta->read(stream); + } else { + 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)); + } + stream.seekg(currentStreamPos, std::ios::beg); + return storedMeta; +} + +std::unique_ptr read_metadata_from(std::istream& stream, const std::shared_ptr& modelBuffer) { + Logger logger("NPUPlugin", Logger::global().level()); + size_t magicBytesSize = MAGIC_BYTES.size(); + std::string blobMagicBytes; + blobMagicBytes.resize(magicBytesSize); - std::stringstream metadataStream; - metadataStream.write(reinterpret_cast(&(*metadataIterator)), - blob.end() - metadataIterator - sizeof(blobDataSize)); + size_t currentStreamPos = stream.tellg(); + size_t streamSize = modelBuffer->size(); + + blobMagicBytes.assign(reinterpret_cast(modelBuffer->get_ptr(streamSize - magicBytesSize)), magicBytesSize); + if (MAGIC_BYTES != blobMagicBytes) { + logger.error("Blob is missing NPU metadata!"); + return nullptr; + } + + uint64_t blobDataSize; + blobDataSize = *reinterpret_cast(modelBuffer->get_ptr(streamSize - magicBytesSize - sizeof(blobDataSize))); uint32_t metaVersion; - metadataStream.read(reinterpret_cast(&metaVersion), sizeof(metaVersion)); + metaVersion = *reinterpret_cast(modelBuffer->get_ptr(currentStreamPos + blobDataSize)); - std::unique_ptr storedMeta; - try { - storedMeta = create_metadata(metaVersion); - storedMeta->read(metadataStream); - } catch(...) { + auto storedMeta = create_metadata(metaVersion, currentStreamPos, blobDataSize); + stream.seekg(blobDataSize + sizeof(metaVersion), std::ios::cur); + if (storedMeta != nullptr) { + storedMeta->read(stream); + } else { logger.warning("Imported blob metadata version: %d.%d, but the current version is: %d.%d", get_major(metaVersion), get_minor(metaVersion), @@ -121,4 +191,12 @@ void Metadata::set_ov_version(const OpenvinoVersion& newVe _ovVersion = newVersion; } +uint64_t Metadata::get_blob_size() const { + return _blobDataSize; +} + +size_t Metadata::get_ov_header_offset() const { + return _ovHeaderOffset; +} + } // namespace intel_npu diff --git a/src/plugins/intel_npu/src/plugin/src/plugin.cpp b/src/plugins/intel_npu/src/plugin/src/plugin.cpp index 52ff5063829355..68a1c4579629a1 100644 --- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp +++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp @@ -7,9 +7,9 @@ #include #include "compiled_model.hpp" -#include "npuw/compiled_model.hpp" -#include "driver_compiler_adapter.hpp" +#include "compiler_adapter_factory.hpp" #include "intel_npu/common/device_helpers.hpp" +#include "intel_npu/common/icompiler_adapter.hpp" #include "intel_npu/common/igraph.hpp" #include "intel_npu/common/itt.hpp" #include "intel_npu/config/common.hpp" @@ -18,13 +18,12 @@ #include "intel_npu/config/runtime.hpp" #include "intel_npu/utils/zero/zero_init.hpp" #include "metadata.hpp" +#include "npuw/compiled_model.hpp" #include "openvino/op/constant.hpp" #include "openvino/op/parameter.hpp" #include "openvino/runtime/intel_npu/properties.hpp" #include "openvino/runtime/properties.hpp" -#include "plugin_compiler_adapter.hpp" #include "remote_context.hpp" -#include "zero_backend.hpp" using namespace intel_npu; @@ -133,30 +132,6 @@ std::map any_copy(const ov::AnyMap& params) { return result; } -size_t getFileSize(std::istream& stream) { - auto log = Logger::global().clone("getFileSize"); - if (!stream) { - OPENVINO_THROW("Stream is in bad status! Please check the passed stream status!"); - } - - const size_t streamStart = stream.tellg(); - stream.seekg(0, std::ios_base::end); - const size_t streamEnd = stream.tellg(); - stream.seekg(streamStart, std::ios_base::beg); - - log.debug("Read blob size: streamStart=%zu, streamEnd=%zu", streamStart, streamEnd); - - if (streamEnd < streamStart) { - OPENVINO_THROW("Invalid stream size: streamEnd (", - streamEnd, - ") is not larger than streamStart (", - streamStart, - ")!"); - } - - return streamEnd - streamStart; -} - void update_log_level(const std::map& propertiesMap) { auto it = propertiesMap.find(std::string(LOG_LEVEL::key())); if (it != propertiesMap.end()) { @@ -700,8 +675,9 @@ std::shared_ptr Plugin::compile_model(const std::shared_ptr< } } - auto original_model = model->clone(); - auto compiler = getCompiler(localConfig); + auto originalModel = model->clone(); + CompilerAdapterFactory compilerAdapterFactory; + auto compiler = compilerAdapterFactory.getCompiler(_backends->getIEngineBackend(), localConfig); OV_ITT_TASK_NEXT(PLUGIN_COMPILE_MODEL, "compile"); std::shared_ptr graph; @@ -717,7 +693,7 @@ std::shared_ptr Plugin::compile_model(const std::shared_ptr< std::shared_ptr compiledModel; try { - compiledModel = std::make_shared(original_model, shared_from_this(), device, graph, localConfig); + compiledModel = std::make_shared(originalModel, shared_from_this(), device, graph, localConfig); } catch (const std::exception& ex) { OPENVINO_THROW(ex.what()); } catch (...) { @@ -753,7 +729,14 @@ std::shared_ptr Plugin::import_model(std::istream& stream, c OV_ITT_SCOPED_TASK(itt::domains::NPUPlugin, "Plugin::import_model"); OV_ITT_TASK_CHAIN(PLUGIN_IMPORT_MODEL, itt::domains::NPUPlugin, "Plugin::import_model", "merge_configs"); - const std::map propertiesMap = any_copy(properties); + auto _properties = properties; + std::shared_ptr modelBuffer; + if (_properties.count(ov::internal::cached_model_buffer.name())) { + modelBuffer = _properties.at(ov::internal::cached_model_buffer.name()).as>(); + _properties.erase(ov::internal::cached_model_buffer.name()); + } + + const auto propertiesMap = any_copy(_properties); auto localConfig = merge_configs(_globalConfig, propertiesMap, OptionMode::RunTime); _logger.setLevel(localConfig.get()); const auto platform = _backends->getCompilationPlatform(localConfig.get(), localConfig.get()); @@ -773,23 +756,39 @@ std::shared_ptr Plugin::import_model(std::istream& stream, c std::shared_ptr compiledModel; try { - auto compiler = getCompiler(localConfig); + CompilerAdapterFactory compilerAdapterFactory; + auto compiler = compilerAdapterFactory.getCompiler(_backends->getIEngineBackend(), localConfig); - auto graphSize = getFileSize(stream); + std::unique_ptr blobPtr; + std::unique_ptr storedMeta; - std::vector blob(graphSize); - stream.read(reinterpret_cast(blob.data()), graphSize); - if (!stream) { - OPENVINO_THROW("Failed to read data from stream!"); + if (dynamic_cast(stream.rdbuf())) { + storedMeta = read_metadata_from(stream, dynamic_cast(stream.rdbuf())->get_buffer()); + } else { + storedMeta = read_metadata_from(stream); } - _logger.debug("Successfully read %zu bytes into blob.", graphSize); - - auto storedMeta = read_metadata_from(blob); - if (!storedMeta->is_compatible()) { + + if (storedMeta == nullptr) { + OPENVINO_THROW("Could not read metadata!"); + } else if (!storedMeta->is_compatible()) { OPENVINO_THROW("Incompatible blob version!"); } + auto graphSize = storedMeta->get_blob_size(); + + if (modelBuffer == nullptr) { + std::vector blob(graphSize); + stream.read(reinterpret_cast(blob.data()), graphSize); + if (!stream) { + OPENVINO_THROW("Failed to read data from stream!"); + } + _logger.debug("Successfully read %zu bytes into blob.", graphSize); + + blobPtr = std::make_unique(std::move(blob)); + } else { + blobPtr = std::make_unique(modelBuffer, storedMeta->get_ov_header_offset(), graphSize); + } - auto graph = compiler->parse(std::move(blob), localConfig); + auto graph = compiler->parse(std::move(blobPtr), localConfig); graph->update_network_name("net" + std::to_string(_compiledModelLoadCounter++)); const std::shared_ptr modelDummy = @@ -827,7 +826,8 @@ ov::SupportedOpsMap Plugin::query_model(const std::shared_ptr& const auto platform = _backends->getCompilationPlatform(localConfig.get(), localConfig.get()); localConfig.update({{ov::intel_npu::platform.name(), platform}}); - auto compiler = getCompiler(localConfig); + CompilerAdapterFactory compilerAdapterFactory; + auto compiler = compilerAdapterFactory.getCompiler(_backends->getIEngineBackend(), localConfig); ov::SupportedOpsMap supportedOpsMap; try { supportedOpsMap = compiler->query(model, localConfig); @@ -840,40 +840,6 @@ ov::SupportedOpsMap Plugin::query_model(const std::shared_ptr& return supportedOpsMap; } -std::unique_ptr Plugin::getCompiler(const Config& config) const { - auto compilerType = config.get(); - _logger.debug("performing createCompiler"); - - switch (compilerType) { - case ov::intel_npu::CompilerType::MLIR: { - if (_backends->getBackendName() != "LEVEL0") { - return std::make_unique(nullptr); - } - - auto zeroBackend = std::dynamic_pointer_cast(_backends->getIEngineBackend()._ptr); - if (zeroBackend == nullptr) { - return std::make_unique(nullptr); - } - - return std::make_unique(zeroBackend->getInitStruct()); - } - case ov::intel_npu::CompilerType::DRIVER: { - if (_backends->getBackendName() != "LEVEL0") { - OPENVINO_THROW("NPU Compiler Adapter must be used with LEVEL0 backend"); - } - - auto zeroBackend = std::dynamic_pointer_cast(_backends->getIEngineBackend()._ptr); - if (!zeroBackend) { - OPENVINO_THROW("Failed to cast zeroBackend, zeroBackend is a nullptr"); - } - - return std::make_unique(zeroBackend->getInitStruct()); - } - default: - OPENVINO_THROW("Invalid NPU_COMPILER_TYPE"); - } -} - std::atomic Plugin::_compiledModelLoadCounter{1}; static const ov::Version version = {CI_BUILD_NUMBER, NPU_PLUGIN_LIB_NAME}; diff --git a/src/plugins/intel_npu/tests/unit/CMakeLists.txt b/src/plugins/intel_npu/tests/unit/CMakeLists.txt index f4e8a64ecea92b..5c9044eb543906 100644 --- a/src/plugins/intel_npu/tests/unit/CMakeLists.txt +++ b/src/plugins/intel_npu/tests/unit/CMakeLists.txt @@ -29,6 +29,8 @@ ov_add_test_target( ${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/utils/include ${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/include ${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/al/include + OBJECT_FILES + ${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/src/metadata.cpp LINK_LIBRARIES ${MANDATORY_UNIT_TESTS_LIBS} LABELS diff --git a/src/plugins/intel_npu/tests/unit/npu/metadata_version.cpp b/src/plugins/intel_npu/tests/unit/npu/metadata_version.cpp index 6bc8e2d6436c5d..f3b637e643101b 100644 --- a/src/plugins/intel_npu/tests/unit/npu/metadata_version.cpp +++ b/src/plugins/intel_npu/tests/unit/npu/metadata_version.cpp @@ -11,38 +11,23 @@ using namespace intel_npu; using MetadataUnitTests = ::testing::Test; -struct MetadataTest : Metadata { - void set_version(uint32_t newVersion) { - _version = newVersion; - } - - void set_ov_version(const OpenvinoVersion& newVersion) { - _ovVersion = newVersion; - } -}; - TEST_F(MetadataUnitTests, readUnversionedBlob) { - std::vector blob(50, 68); + std::stringstream stream(" ELF"); - std::unique_ptr storedMeta; - ASSERT_ANY_THROW(storedMeta = read_metadata_from(blob)); + auto storedMeta = read_metadata_from(stream); ASSERT_EQ(storedMeta, nullptr); } TEST_F(MetadataUnitTests, writeAndReadMetadataFromBlob) { std::stringstream stream; size_t blobSize = 0; - auto meta = MetadataTest(); + auto meta = Metadata(stream.tellg(), blobSize); OV_ASSERT_NO_THROW(meta.write(stream)); OV_ASSERT_NO_THROW(stream.write(reinterpret_cast(&blobSize), sizeof(blobSize))); OV_ASSERT_NO_THROW(stream.write(MAGIC_BYTES.data(), MAGIC_BYTES.size())); - blobSize = stream.str().length(); - - std::vector blob(blobSize); - OV_ASSERT_NO_THROW(stream.read(reinterpret_cast(blob.data()), blobSize)); - auto storedMeta = read_metadata_from(blob); + auto storedMeta = read_metadata_from(stream); ASSERT_NE(storedMeta, nullptr); ASSERT_TRUE(storedMeta->is_compatible()); } @@ -50,7 +35,7 @@ TEST_F(MetadataUnitTests, writeAndReadMetadataFromBlob) { TEST_F(MetadataUnitTests, writeAndReadInvalidOpenvinoVersion) { size_t blobSize = 0; std::stringstream stream; - auto meta = MetadataTest(); + auto meta = Metadata(stream.tellg(), blobSize); OpenvinoVersion badOvVersion("just_some_wrong_ov_version"); meta.set_ov_version(badOvVersion); @@ -59,11 +44,7 @@ TEST_F(MetadataUnitTests, writeAndReadInvalidOpenvinoVersion) { OV_ASSERT_NO_THROW(stream.write(reinterpret_cast(&blobSize), sizeof(blobSize))); OV_ASSERT_NO_THROW(stream.write(MAGIC_BYTES.data(), MAGIC_BYTES.size())); - blobSize = stream.str().length(); - - std::vector blob(blobSize); - OV_ASSERT_NO_THROW(stream.read(reinterpret_cast(blob.data()), blobSize)); - auto storedMeta = read_metadata_from(blob); + auto storedMeta = read_metadata_from(stream); ASSERT_NE(storedMeta, nullptr); ASSERT_FALSE(storedMeta->is_compatible()); } @@ -71,7 +52,7 @@ TEST_F(MetadataUnitTests, writeAndReadInvalidOpenvinoVersion) { TEST_F(MetadataUnitTests, writeAndReadInvalidMetadataVersion) { size_t blobSize = 0; std::stringstream stream; - auto meta = Metadata(); + auto meta = Metadata(stream.tellg(), blobSize); constexpr uint32_t dummy_version = make_version(0x00007E57, 0x0000AC3D); meta.set_version(dummy_version); @@ -80,10 +61,6 @@ TEST_F(MetadataUnitTests, writeAndReadInvalidMetadataVersion) { OV_ASSERT_NO_THROW(stream.write(reinterpret_cast(&blobSize), sizeof(blobSize))); OV_ASSERT_NO_THROW(stream.write(MAGIC_BYTES.data(), MAGIC_BYTES.size())); - blobSize = stream.str().length(); - - std::vector blob(blobSize); - OV_ASSERT_NO_THROW(stream.read(reinterpret_cast(blob.data()), blobSize)); - auto storedMeta = read_metadata_from(blob); + auto storedMeta = read_metadata_from(stream); ASSERT_EQ(storedMeta, nullptr); }