Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[NPU] UMD caching: pass the model hash to the driver (#33046)

Razvan Apetroaie62ba741ee8c2
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/config/options.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/config/options.hpp
index a17fe93ac28..2d8f634b4d9 100644
--- a/src/plugins/intel_npu/src/al/include/intel_npu/config/options.hpp
+++ b/src/plugins/intel_npu/src/al/include/intel_npu/config/options.hpp
@@ -1377,6 +1377,8 @@ struct SEPARATE_WEIGHTS_VERSION final : OptionBase<SEPARATE_WEIGHTS_VERSION, ov:
}
static ov::intel_npu::WSVersion defaultValue() {
+ // Note: if the compiler-in-plugin is used (intel_npu::compiler_type = intel_npu::CompilerType::PLUGIN), then
+ // the default is actually WSVersion::ONE_SHOT
return ov::intel_npu::WSVersion::ITERATIVE;
}
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/npu_private_properties.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/npu_private_properties.hpp
index 9c406540d25..248875e4b68 100644
--- a/src/plugins/intel_npu/src/al/include/intel_npu/npu_private_properties.hpp
+++ b/src/plugins/intel_npu/src/al/include/intel_npu/npu_private_properties.hpp
@@ -375,7 +375,8 @@ static constexpr ov::Property<BatchMode> batch_mode{"NPU_BATCH_MODE"};
/**
* @brief [Experimental, only for NPU Plugin]
- * Type: enum. Default is "ITERATIVE".
+ * Type: enum. Default is "ITERATIVE". If the compiler-in-plugin is used (intel_npu::compiler_type =
+ * intel_npu::CompilerType::PLUGIN), then the default becomes "ONE_SHOT".
*
* The value stored in this entry indicates which implementation of the "weights separation" feature will be used.
* Note: NPU_COMPILER_TYPE = DRIVER & NPU_SEPARATE_WEIGHTS_VERSION = ONE_SHOT are not compatible.
diff --git a/src/plugins/intel_npu/src/al/include/intel_npu/weights_pointer_attribute.hpp b/src/plugins/intel_npu/src/al/include/intel_npu/weights_pointer_attribute.hpp
index ff61286c71b..61a770abebc 100644
--- a/src/plugins/intel_npu/src/al/include/intel_npu/weights_pointer_attribute.hpp
+++ b/src/plugins/intel_npu/src/al/include/intel_npu/weights_pointer_attribute.hpp
@@ -38,6 +38,10 @@ public:
return true;
}
+ bool is_deterministic() const override {
+ return false;
+ }
+
size_t memory_pointer;
size_t byte_size;
};
diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/compiler_impl.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/compiler_impl.hpp
index df64274031d..1f69ab886f0 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/include/compiler_impl.hpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/include/compiler_impl.hpp
@@ -46,6 +46,15 @@ public:
std::shared_ptr<void> getLinkedLibrary() const;
private:
+ /**
+ * @brief Compiles the given model according to the given configuration. During the model serialization step, the
+ * "WeightlessCacheAttribute" may be stored within the serialized model if requested.
+ * @note Storing the "WeightlessCacheAttribute" is necessary if the "weights separation" flow is being used.
+ */
+ NetworkDescription compile(const std::shared_ptr<const ov::Model>& model,
+ const Config& config,
+ const bool storeWeightlessCacheAttributeFlag) const;
+
vcl_log_handle_t _logHandle = nullptr;
vcl_compiler_handle_t _compilerHandle = nullptr;
vcl_compiler_properties_t _compilerProperties;
diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/vcl_serializer.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/vcl_serializer.hpp
index 74bb2c1c0a3..62901da963d 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/include/vcl_serializer.hpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/include/vcl_serializer.hpp
@@ -16,7 +16,11 @@
namespace intel_npu {
-using SerializedIR = std::pair<size_t, std::shared_ptr<uint8_t>>;
+struct SerializedIR {
+ std::shared_ptr<uint8_t> buffer = nullptr;
+ size_t size = 0;
+ std::optional<uint64_t> hash = std::nullopt;
+};
/**
* @brief Contain all required transformation on OpenVINO model in case for external compiler usage and
@@ -31,11 +35,19 @@ namespace driver_compiler_utils {
* @param supportedOpsetVersion The last operators set version supported by the compiler.
* @param useBaseModelSerializer "true" means the legacy serializer will be used (weights will be copied), "false" means
* the optimized one is used instead (weights pointers are stored instead).
+ * @param computeModelHash If true, a hash of the model will also be returned.
+ * @param storeWeightlessCacheAttribute If true, the returned serialized model will also contain within its runtime
+ * information the WeightlessCacheAttributes stored using a custom format. This format can be interpreted by the
+ * driver-compiler adapter in order to properly handle the "weights separation" feature.
+ *
+ * @returns The serialized model, along with its size and hash
*/
SerializedIR serializeIR(const std::shared_ptr<const ov::Model>& model,
ze_graph_compiler_version_info_t compilerVersion,
- const uint32_t supportedOpsetVersion,
- const bool useBaseModelSerializer = true);
+ const uint32_t supportedOpsetVersion = 11,
+ const bool useBaseModelSerializer = true,
+ const bool computeModelHash = false,
+ const bool storeWeightlessCacheAttribute = false);
/**
* @brief Serialize input / output information to string format.
diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/weightless_utils.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/weightless_utils.hpp
index 367348ffef6..ead8483f01c 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/include/weightless_utils.hpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/include/weightless_utils.hpp
@@ -5,24 +5,9 @@
#pragma once
#include "intel_npu/network_metadata.hpp"
-#include "openvino/core/model.hpp"
namespace intel_npu {
bool isInitMetadata(const NetworkMetadata& networkMetadata);
-/**
- * @brief Stores the information within the "WeightlessCacheAttribute" as runtime fields that persist upon
- * serialization.
- * @details Constant nodes (weights) may contain as medatadata the "WeightlessCacheAttribute", that is information
- * regarding the offset of the weights within the binary file, as well as the original size and precision. This
- * information is required within the "weights separation" flow, therefore this function is here to store it.
- * @note Not calling this function in the weights separation flow would lead to this information being lost upon
- * serialization. The "WeightlessCacheAttribute" information that is populated upon de-serialization would represent
- * metadata corresponding to the serialized stream, not the original weights file. Therefore the compiler would be
- * misinformed and lookups of weights offsets could fail.
- *
- * @param model Both source and target.
- */
-void storeWeightlessCacheAttribute(const std::shared_ptr<ov::Model>& model);
} // namespace intel_npu
diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/ze_graph_ext_wrappers.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/ze_graph_ext_wrappers.hpp
index 322c696b0dc..7fdf7044216 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/include/ze_graph_ext_wrappers.hpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/include/ze_graph_ext_wrappers.hpp
@@ -48,6 +48,12 @@ public:
bool isOptionSupported(std::string optName, std::optional<std::string> optValue = std::nullopt) const;
bool isTurboOptionSupported(const ze_graph_compiler_version_info_t& compilerVersion) const;
+ /**
+ * @brief Tells us whether or not the driver is able to receive and take into account a hash of the model instead of
+ * computing its own within the UMD.
+ */
+ bool isPluginModelHashSupported() const;
+
void getGraphBinary(const GraphDescriptor& graphDescriptor,
std::vector<uint8_t>& blob,
const uint8_t*& blobPtr,
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/compiler_impl.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/compiler_impl.cpp
index 559e655795b..3205c970910 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/compiler_impl.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/compiler_impl.cpp
@@ -353,6 +353,12 @@ struct vcl_allocator_malloc {
};
NetworkDescription VCLCompilerImpl::compile(const std::shared_ptr<const ov::Model>& model, const Config& config) const {
+ return compile(model, config, false);
+}
+
+NetworkDescription VCLCompilerImpl::compile(const std::shared_ptr<const ov::Model>& model,
+ const Config& config,
+ const bool storeWeightlessCacheAttributeFlag) const {
_logger.debug("compile start");
/// Check the linked vcl version whether supported in plugin
@@ -376,8 +382,12 @@ NetworkDescription VCLCompilerImpl::compile(const std::shared_ptr<const ov::Mode
useBaseModelSerializer = isUseBaseModelSerializer(usedVersion, updatedConfig);
_logger.debug("serialize IR method is %s",
useBaseModelSerializer ? "base vcl serializer" : "vcl serializer (not copy weights)");
- auto serializedIR =
- driver_compiler_utils::serializeIR(model, compilerVersion, maxOpsetVersion, useBaseModelSerializer);
+ auto serializedIR = driver_compiler_utils::serializeIR(model,
+ compilerVersion,
+ maxOpsetVersion,
+ useBaseModelSerializer,
+ false,
+ storeWeightlessCacheAttributeFlag);
std::string buildFlags;
_logger.debug("create build flags");
@@ -386,8 +396,8 @@ NetworkDescription VCLCompilerImpl::compile(const std::shared_ptr<const ov::Mode
buildFlags += driver_compiler_utils::serializeConfig(updatedConfig, compilerVersion);
_logger.debug("final build flags to compiler: %s", buildFlags.c_str());
- vcl_executable_desc_t exeDesc = {serializedIR.second.get(),
- serializedIR.first,
+ vcl_executable_desc_t exeDesc = {serializedIR.buffer.get(),
+ serializedIR.size,
buildFlags.c_str(),
buildFlags.size()};
@@ -422,7 +432,6 @@ std::vector<std::shared_ptr<NetworkDescription>> VCLCompilerImpl::compileWsOneSh
const std::shared_ptr<ov::Model>& model,
const Config& config) const {
_logger.debug("compileWsOneShot start");
- storeWeightlessCacheAttribute(model);
const auto maxOpsetVersion = _compilerProperties.supportedOpsets;
_logger.info("getSupportedOpsetVersion Max supported version of opset in CiD: %d", maxOpsetVersion);
@@ -441,8 +450,12 @@ std::vector<std::shared_ptr<NetworkDescription>> VCLCompilerImpl::compileWsOneSh
useBaseModelSerializer = isUseBaseModelSerializer({7, 5}, updatedConfig);
_logger.debug("serialize IR method is %s",
useBaseModelSerializer ? "base vcl serializer" : "vcl serializer (not copy weights)");
- auto serializedIR =
- driver_compiler_utils::serializeIR(model, compilerVersion, maxOpsetVersion, useBaseModelSerializer);
+ auto serializedIR = driver_compiler_utils::serializeIR(model,
+ compilerVersion,
+ maxOpsetVersion,
+ useBaseModelSerializer,
+ false,
+ true);
std::string buildFlags;
_logger.debug("create build flags");
@@ -451,8 +464,8 @@ std::vector<std::shared_ptr<NetworkDescription>> VCLCompilerImpl::compileWsOneSh
buildFlags += driver_compiler_utils::serializeConfig(updatedConfig, compilerVersion);
_logger.debug("final build flags to compiler: %s", buildFlags.c_str());
- vcl_executable_desc_t exeDesc = {serializedIR.second.get(),
- serializedIR.first,
+ vcl_executable_desc_t exeDesc = {serializedIR.buffer.get(),
+ serializedIR.size,
buildFlags.c_str(),
buildFlags.size()};
_logger.debug("compiler vcl version: %d.%d", _vclVersion.major, _vclVersion.minor);
@@ -481,14 +494,13 @@ NetworkDescription VCLCompilerImpl::compileWsIterative(const std::shared_ptr<ov:
const Config& config,
size_t callNumber) const {
_logger.debug("compileWsIterative start");
- storeWeightlessCacheAttribute(model);
const FilteredConfig* filteredConfig = dynamic_cast<const FilteredConfig*>(&config);
if (filteredConfig == nullptr) {
OPENVINO_THROW("config is not FilteredConfig");
}
FilteredConfig updatedConfig = *filteredConfig;
updatedConfig.update({{ov::intel_npu::ws_compile_call_number.name(), std::to_string(callNumber)}});
- return compile(model, updatedConfig);
+ return compile(model, updatedConfig, true);
}
intel_npu::NetworkMetadata VCLCompilerImpl::parse(const std::vector<uint8_t>& network, const Config& config) const {
@@ -576,7 +588,7 @@ ov::SupportedOpsMap VCLCompilerImpl::query(const std::shared_ptr<const ov::Model
_logger.debug("queryImpl build flags : %s", buildFlags.c_str());
vcl_query_handle_t queryHandle;
- vcl_query_desc_t queryDesc = {serializedIR.second.get(), serializedIR.first, buildFlags.c_str(), buildFlags.size()};
+ vcl_query_desc_t queryDesc = {serializedIR.buffer.get(), serializedIR.size, buildFlags.c_str(), buildFlags.size()};
THROW_ON_FAIL_FOR_VCL("vclQueryNetworkCreate",
vclQueryNetworkCreate(_compilerHandle, queryDesc, &queryHandle),
_logHandle);
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/driver_compiler_adapter.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/driver_compiler_adapter.cpp
index 3c2fa6b84f7..2525a076766 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/driver_compiler_adapter.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/driver_compiler_adapter.cpp
@@ -66,8 +66,11 @@ std::shared_ptr<IGraph> DriverCompilerAdapter::compile(const std::shared_ptr<con
_logger.debug("serialize IR");
- auto serializedIR =
- driver_compiler_utils::serializeIR(model, compilerVersion, maxOpsetVersion, useBaseModelSerializer(config));
+ auto serializedIR = driver_compiler_utils::serializeIR(model,
+ compilerVersion,
+ maxOpsetVersion,
+ useBaseModelSerializer(config),
+ _zeGraphExt->isPluginModelHashSupported());
std::string buildFlags;
const bool useIndices = !((compilerVersion.major < 5) || (compilerVersion.major == 5 && compilerVersion.minor < 9));
@@ -103,8 +106,6 @@ std::shared_ptr<IGraph> DriverCompilerAdapter::compileWS(const std::shared_ptr<o
const FilteredConfig& config) const {
OV_ITT_TASK_CHAIN(COMPILE_BLOB, itt::domains::NPUPlugin, "DriverCompilerAdapter", "compileWS");
- storeWeightlessCacheAttribute(model);
-
const ze_graph_compiler_version_info_t& compilerVersion = _compilerProperties.compilerVersion;
if ((compilerVersion.major < 6) || (compilerVersion.major == 6 && compilerVersion.minor < 3)) {
OPENVINO_THROW("Minimum compiler version required for weights separation: 6.3. Found: ",
@@ -123,8 +124,12 @@ std::shared_ptr<IGraph> DriverCompilerAdapter::compileWS(const std::shared_ptr<o
}
_logger.debug("serialize IR");
- auto serializedIR =
- driver_compiler_utils::serializeIR(model, compilerVersion, maxOpsetVersion, useBaseModelSerializer(config));
+ auto serializedIR = driver_compiler_utils::serializeIR(model,
+ compilerVersion,
+ maxOpsetVersion,
+ useBaseModelSerializer(config),
+ true,
+ _zeGraphExt->isPluginModelHashSupported());
std::string buildFlags;
const bool useIndices = !((compilerVersion.major < 5) || (compilerVersion.major == 5 && compilerVersion.minor < 9));
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/vcl_serializer.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/vcl_serializer.cpp
index 4ce927461da..782f55d5c5f 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/vcl_serializer.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/vcl_serializer.cpp
@@ -6,7 +6,6 @@
#include <cstdint>
#include <istream>
-#include <mutex>
#include <regex>
#include <streambuf>
@@ -14,9 +13,12 @@
#include "intel_npu/common/filtered_config.hpp"
#include "intel_npu/config/options.hpp"
#include "intel_npu/weights_pointer_attribute.hpp"
+#include "openvino/core/rt_info/weightless_caching_attributes.hpp"
+#include "openvino/op/constant.hpp"
#include "openvino/op/util/multi_subgraph_base.hpp"
#include "openvino/pass/serialize.hpp"
#include "transformations/common_optimizations/nop_elimination.hpp"
+#include "transformations/hash.hpp"
#include "transformations/op_conversions/convert_interpolate11_downgrade.hpp"
#include "xml_serializer.hpp"
@@ -169,6 +171,38 @@ void storeWeightsPointerAttribute(const std::shared_ptr<ov::Model>& model) {
}
}
+/**
+ * @brief Stores the information within the "WeightlessCacheAttribute" as runtime fields that persist upon
+ * serialization.
+ * @details Constant nodes (weights) may contain as metadata the "WeightlessCacheAttribute", that is information
+ * regarding the offset of the weights within the binary file, as well as the original size and precision. This
+ * information is required within the "weights separation" flow, therefore this function is here to store it.
+ * @note Not calling this function in the weights separation flow would lead to this information being lost upon
+ * serialization. The "WeightlessCacheAttribute" information that is populated upon de-serialization would represent
+ * metadata corresponding to the serialized stream, not the original weights file. Therefore the compiler would be
+ * misinformed and lookups of weights offsets could fail.
+ *
+ * @param model Both source and target.
+ */
+void storeWeightlessCacheAttribute(const std::shared_ptr<ov::Model>& model) {
+ size_t constantId = 0;
+ for (auto&& node : model->get_ordered_ops()) {
+ if (ov::is_type<ov::op::v0::Constant>(node)) {
+ ov::RTMap& runtimeInfoMap = node->get_rt_info();
+ const auto& weightlessCacheAttrIt =
+ runtimeInfoMap.find(ov::WeightlessCacheAttribute::get_type_info_static());
+
+ const std::string constantIdString = std::to_string(constantId++);
+ if (weightlessCacheAttrIt != runtimeInfoMap.end()) {
+ auto& weightlessCacheAttr = weightlessCacheAttrIt->second.as<ov::WeightlessCacheAttribute>();
+ model->set_rt_info(weightlessCacheAttr.bin_offset, "ws_bin_offset_" + constantIdString);
+ model->set_rt_info(weightlessCacheAttr.original_size, "ws_original_size_" + constantIdString);
+ model->set_rt_info(weightlessCacheAttr.original_dtype, "ws_original_dtype_" + constantIdString);
+ }
+ }
+ }
+}
+
} // namespace
namespace intel_npu::driver_compiler_utils {
@@ -180,81 +214,86 @@ namespace intel_npu::driver_compiler_utils {
*/
class VCLSerializerBase {
public:
- VCLSerializerBase(const std::shared_ptr<const ov::Model>& origModel,
- const ze_graph_compiler_version_info_t compilerVersion,
- const uint32_t supportedOpset = 11)
+ VCLSerializerBase(const ze_graph_compiler_version_info_t compilerVersion, const uint32_t supportedOpset)
: _logger("VCLSerializerBase", Logger::global().level()),
_compilerVersion(compilerVersion),
- _supportedOpset(supportedOpset) {
- // There is no const variant of run_passes so use const_cast here
- // as model serialization does not mutate the model
- _model = std::const_pointer_cast<ov::Model>(origModel);
-
- if (supportedOpset < 11) {
- // Need to clone to modify the model and remain thread safe
- _model = _model->clone();
- _logger.info("Clone model for offset smaller than 11");
- }
- }
+ _supportedOpset(supportedOpset) {}
- virtual SerializedIR serialize() = 0;
+ virtual SerializedIR serialize(const std::shared_ptr<ov::Model>& model,
+ const bool computeModelHash,
+ const bool storeWeightlessCacheAttributeFlag) = 0;
virtual ~VCLSerializerBase() = default;
protected:
/**
- * @brief Prepares the model for serialization and calls the provided function to serialize it.
+ * @brief Model preprocessing steps common to all serializers.
+ * @details These steps should include operator conversions and the storage of additional runtime information that
+ * the driver-compiler adapter may use.
*
- * @param register_serialization_pass A function that receives the pass manager. This function is supposed to
- * register the serialization pass using the provided manager.
+ * @param storeWeightlessCacheAttributeFlag If true, the WeightlessCacheAttributes will also be stored as runtime
+ * information using a custom format. This is necessary if the "weights separation" flow is used.
*/
- void serialize_model_to_stream(const std::function<void(ov::pass::Manager&)>& register_serialization_pass) {
- _logger.debug("serialize_model_to_stream");
- const auto passConfig = std::make_shared<ov::pass::PassConfig>();
- ov::pass::Manager manager(std::move(passConfig), "NPU:serialize_model_to_stream");
-
+ void run_common_pipeline(const std::shared_ptr<ov::Model>& model, const bool storeWeightlessCacheAttributeFlag) {
+ // Step 1: run compiler compatibility passes.
+ // It is possible some of these passes will modify WeightlessCacheAttributes. Therefore, we should run them
+ // before storing these attributes.
+ ov::pass::Manager manager(std::make_shared<ov::pass::PassConfig>(), "NPU:compiler_compatibility_passes");
if (_supportedOpset < 11) {
// Downgrade to opset10
manager.register_pass<ov::pass::ConvertInterpolate11ToInterpolate4>();
_logger.info("Downgrade op for opset smaller than 11");
}
-
if ((_compilerVersion.major < 7) || (_compilerVersion.major == 7 && _compilerVersion.minor <= 26)) {
manager.register_pass<ov::pass::EliminateIdentity>();
}
+ manager.run_passes(model);
- register_serialization_pass(manager);
-
- // We modify the original model object here therefore a mutex is required
- static std::mutex rtInfoMutex;
+ // Step 2: store the WeightlessCacheAttributes if requested
+ // Note: since these attributes contain information w.r.t. the binary file, this information is deterministic.
+ if (storeWeightlessCacheAttributeFlag) {
+ storeWeightlessCacheAttribute(model);
+ }
- {
- std::lock_guard<std::mutex> lock(rtInfoMutex);
+ // Step 3: store any other runtime information the VCL needs to know about.
+ // Warning: do not store any non-deterministic field here.
+
+ // Depending on the driver version, the compiler attached to it may request this information as an indicator
+ // of the precision/layout preprocessing requirement. We are setting this value to "true" since the OV API v1
+ // has been removed.
+ model->set_rt_info(true, "is_new_api");
+ // Flag used to indicate an NPU plugin version that switched the I/O identification convention from
+ // names to indices. The flag is needed to inform the driver-compiler adapter to expect indices
+ // when attempting to deserialize the I/O metadata.
+ model->set_rt_info(true, "use_indices_for_io_metadata");
+ }
- // Depending on the driver version, the compiler attached to it may request this information as an indicator
- // of the precision/layout preprocessing requirement. We are setting this value to "true" since the API
- // version is no longer a cause for altering the metadata. This is due to the preprocessing performed in the
- // OpenVINO framework's implementaion, the "ov::Model" object is preprocessed before reaching the NPU
- // plugin.
- _model->set_rt_info(true, "is_new_api");
- // Flag used for indicating an NPU plugin version which switched the I/O identification convention from
- // names to indices. The flag is required in order to inform the driver-compiler adapter to expect indices
- // when attempting to deserialize the I/O metadata.
- _model->set_rt_info(true, "use_indices_for_io_metadata");
+ /**
+ * @brief Calls the provided function to serialize the model and computes its hash if requested.
+ *
+ * @param register_serialization_pass A function that receives the pass manager. This function is supposed to
+ * register the serialization pass using the provided manager.
+ * @param hash The hash will be stored here if a value is provided.
+ */
+ void serialize_model_to_stream(const std::shared_ptr<ov::Model>& model,
+ const std::function<void(ov::pass::Manager&)>& register_serialization_pass,
+ std::optional<uint64_t>& hash) {
+ _logger.debug("serialize_model_to_stream");
- manager.run_passes(_model);
+ ov::pass::Manager manager(std::make_shared<ov::pass::PassConfig>(), "NPU:serialize_model_to_stream");
+ register_serialization_pass(manager);
- auto& rtInfo = _model->get_rt_info();
- rtInfo.erase("is_new_api");
- rtInfo.erase("use_indices_for_io_metadata");
+ if (hash.has_value()) {
+ manager.register_pass<ov::pass::Hash>(hash.value());
}
+ manager.run_passes(model);
+
_logger.debug("serialize_model_to_stream end");
}
Logger _logger;
- std::shared_ptr<ov::Model> _model = nullptr;
ze_graph_compiler_version_info_t _compilerVersion;
- uint32_t _supportedOpset = 11;
+ uint32_t _supportedOpset;
};
/**
@@ -262,15 +301,16 @@ protected:
*/
class VCLSerializerWithWeightsCopy : public VCLSerializerBase {
public:
- VCLSerializerWithWeightsCopy(const std::shared_ptr<const ov::Model>& origModel,
- const ze_graph_compiler_version_info_t compilerVersion,
- const uint32_t supportedOpset = 11)
- : VCLSerializerBase(origModel, compilerVersion, supportedOpset) {
+ VCLSerializerWithWeightsCopy(const ze_graph_compiler_version_info_t compilerVersion, const uint32_t supportedOpset)
+ : VCLSerializerBase(compilerVersion, supportedOpset) {
_logger.setName("VCLSerializerWithWeightsCopy");
};
- SerializedIR serialize() override {
- count_model_size();
+ SerializedIR serialize(const std::shared_ptr<ov::Model>& model,
+ const bool computeModelHash,
+ const bool storeWeightlessCacheAttributeFlag) override {
+ run_common_pipeline(model, storeWeightlessCacheAttributeFlag);
+ const auto [xmlSize, weightsSize] = count_model_size(model);
// Contract between adapter and compiler in driver
const uint32_t maxNumberOfElements = 10;
@@ -278,8 +318,6 @@ public:
const uint64_t maxSizeOfWeights = maxSizeOfXML * 2;
const uint32_t numberOfInputData = 2;
- const uint64_t xmlSize = static_cast<uint64_t>(_xmlSize);
- const uint64_t weightsSize = static_cast<uint64_t>(_weightsSize);
OPENVINO_ASSERT(numberOfInputData < maxNumberOfElements);
if (xmlSize >= maxSizeOfXML) {
@@ -319,18 +357,22 @@ public:
uint64_t weightsOffset = offset;
offset += weightsSize;
- serialize_model_to_buffer(serializedIR + xmlOffset, serializedIR + weightsOffset);
+ std::optional<uint64_t> hash = computeModelHash ? std::make_optional<uint64_t>(0) : std::nullopt;
+ serialize_model_to_buffer(model, serializedIR + xmlOffset, serializedIR + weightsOffset, hash);
OPENVINO_ASSERT(offset == sizeOfSerializedIR);
- return std::make_pair(sizeOfSerializedIR, buffer);
+ return {buffer, sizeOfSerializedIR, hash};
}
private:
/**
* @brief Serialize OpenVINO model to target buffer
*/
- void serialize_model_to_buffer(uint8_t* xml, uint8_t* weights) {
+ void serialize_model_to_buffer(const std::shared_ptr<ov::Model>& model,
+ uint8_t* xml,
+ uint8_t* weights,
+ std::optional<uint64_t>& hash) {
_logger.debug("serialize_model_to_buffer");
writer_streambuf xmlStreamBuf(xml);
@@ -338,7 +380,7 @@ private:
std::ostream xmlStream(&xmlStreamBuf);
std::ostream weightsStream(&weightsStreamBuf);
- serialize_model_to_stream(xmlStream, weightsStream);
+ serialize_model_to_stream(model, xmlStream, weightsStream, hash);
_logger.debug("serialize_model_to_buffer end");
}
@@ -346,17 +388,20 @@ private:
/**
* @brief Serialize OpenVINO model to target stream
*/
- void serialize_model_to_stream(std::ostream& xml, std::ostream& weights) {
+ void serialize_model_to_stream(const std::shared_ptr<ov::Model>& model,
+ std::ostream& xml,
+ std::ostream& weights,
+ std::optional<uint64_t>& hash) {
const std::function<void(ov::pass::Manager&)>& register_serialization_pass = [&](ov::pass::Manager& manager) {
manager.register_pass<ov::pass::Serialize>(xml, weights);
};
- VCLSerializerBase::serialize_model_to_stream(register_serialization_pass);
+ VCLSerializerBase::serialize_model_to_stream(model, register_serialization_pass, hash);
}
/**
* @brief Get size of xml and weights from model
*/
- void count_model_size() {
+ std::pair<uint64_t, uint64_t> count_model_size(const std::shared_ptr<ov::Model>& model) {
_logger.debug("count_model_size");
counter_streambuf xmlStreamBuf;
@@ -364,16 +409,14 @@ private:
std::ostream xmlStream(&xmlStreamBuf);
std::ostream weightsStream(&weightsStreamBuf);
- serialize_model_to_stream(xmlStream, weightsStream);
-
- _xmlSize = xmlStreamBuf.size();
- _weightsSize = weightsStreamBuf.size();
+ std::optional<uint64_t> hash = std::nullopt;
+ serialize_model_to_stream(model, xmlStream, weightsStream, hash);
- _logger.debug("count_model_size completed, xml size: %d, weights size: %d", _xmlSize, _weightsSize);
+ _logger.debug("count_model_size completed, xml size: %d, weights size: %d",
+ xmlStreamBuf.size(),
+ weightsStreamBuf.size());
+ return std::make_pair<uint64_t, uint64_t>(xmlStreamBuf.size(), weightsStreamBuf.size());
}
-
- size_t _xmlSize = 0;
- size_t _weightsSize = 0;
};
/**
@@ -382,83 +425,82 @@ private:
*/
class VCLSerializerWithoutWeightsCopy : public VCLSerializerBase {
public:
- VCLSerializerWithoutWeightsCopy(const std::shared_ptr<const ov::Model>& origModel,
- const ze_graph_compiler_version_info_t compilerVersion,
- const uint32_t supportedOpset = 11)
- : VCLSerializerBase(origModel, compilerVersion, supportedOpset) {
+ VCLSerializerWithoutWeightsCopy(const ze_graph_compiler_version_info_t compilerVersion,
+ const uint32_t supportedOpset)
+ : VCLSerializerBase(compilerVersion, supportedOpset) {
_logger.setName("VCLSerializerWithoutWeightsCopy");
};
- SerializedIR serialize() override {
- count_model_size();
+ SerializedIR serialize(const std::shared_ptr<ov::Model>& model,
+ const bool computeModelHash,
+ const bool storeWeightlessCacheAttributeFlag) override {
+ run_common_pipeline(model, storeWeightlessCacheAttributeFlag);
+ storeWeightsPointerAttribute(model);
- if (_serializedModelSize >= std::numeric_limits<uint64_t>::max()) {
- OPENVINO_THROW("The serialized model is too big to process. Size: ",
- _serializedModelSize,
- " >= ",
- std::numeric_limits<uint64_t>::max());
- }
+ uint64_t serializedModelSize = count_model_size(model);
// use array to avoid vector's memory zero-ing overhead
- std::shared_ptr<uint8_t> buffer(new uint8_t[_serializedModelSize], std::default_delete<uint8_t[]>());
- serialize_model_to_buffer(buffer.get());
+ std::shared_ptr<uint8_t> buffer(new uint8_t[serializedModelSize], std::default_delete<uint8_t[]>());
+ std::optional<uint64_t> hash = computeModelHash ? std::make_optional<uint64_t>(0) : std::nullopt;
+ serialize_model_to_buffer(model, buffer.get(), hash);
- return SerializedIR(_serializedModelSize, buffer);
+ return {buffer, serializedModelSize, hash};
}
private:
- void serialize_model_to_buffer(uint8_t* buffer) {
+ void serialize_model_to_buffer(const std::shared_ptr<ov::Model>& model,
+ uint8_t* buffer,
+ std::optional<uint64_t>& hash) {
_logger.debug("serialize_model_to_buffer");
writer_streambuf streamBuf(buffer);
std::ostream stream(&streamBuf);
- serialize_model_to_stream(stream);
+ serialize_model_to_stream(model, stream, hash);
_logger.debug("serialize_model_to_buffer end");
}
- void serialize_model_to_stream(std::ostream& stream) {
+ void serialize_model_to_stream(const std::shared_ptr<ov::Model>& model,
+ std::ostream& stream,
+ std::optional<uint64_t>& hash) {
const std::function<void(std::ostream&)>& compiler_version_serializer = [&](std::ostream& stream) {
stream.write(reinterpret_cast<const char*>(&_compilerVersion), sizeof(_compilerVersion));
};
const std::function<void(ov::pass::Manager&)>& register_serialization_pass = [&](ov::pass::Manager& manager) {
manager.register_pass<StreamSerialize>(stream, compiler_version_serializer);
};
- VCLSerializerBase::serialize_model_to_stream(register_serialization_pass);
+ VCLSerializerBase::serialize_model_to_stream(model, register_serialization_pass, hash);
}
- void count_model_size() {
+ uint64_t count_model_size(const std::shared_ptr<ov::Model>& model) {
_logger.debug("count_model_size");
counter_streambuf streamBuf;
std::ostream stream(&streamBuf);
- serialize_model_to_stream(stream);
+ std::optional<uint64_t> hash = std::nullopt;
+ serialize_model_to_stream(model, stream, hash);
- _serializedModelSize = streamBuf.size();
-
- _logger.debug("count_model_size completed, serialized model size: %d", _serializedModelSize);
+ _logger.debug("count_model_size completed, serialized model size: %d", streamBuf.size());
+ return streamBuf.size();
}
-
- uint64_t _serializedModelSize = 0;
};
SerializedIR serializeIR(const std::shared_ptr<const ov::Model>& model,
const ze_graph_compiler_version_info_t compilerVersion,
const uint32_t supportedOpsetVersion,
- const bool useBaseModelSerializer) {
+ const bool useBaseModelSerializer,
+ const bool computeModelHash,
+ const bool storeWeightlessCacheAttributeFlag) {
+ // The current instance is already a clone (or should be one), we are not modifying the original model
+ const std::shared_ptr<ov::Model> nonConstantModel = std::const_pointer_cast<ov::Model>(model);
if (!useBaseModelSerializer) {
- // Non-constness required for adding & removing weights pointer attributes. The current instance is already a
- // clone (or should be one), we are not modifying the original model.
- const std::shared_ptr<ov::Model> nonConstantModel = std::const_pointer_cast<ov::Model>(model);
- storeWeightsPointerAttribute(nonConstantModel);
-
- SerializedIR serializedIR =
- VCLSerializerWithoutWeightsCopy(model, compilerVersion, supportedOpsetVersion).serialize();
- return serializedIR;
+ return VCLSerializerWithoutWeightsCopy(compilerVersion, supportedOpsetVersion)
+ .serialize(nonConstantModel, computeModelHash, storeWeightlessCacheAttributeFlag);
}
- return VCLSerializerWithWeightsCopy(model, compilerVersion, supportedOpsetVersion).serialize();
+ return VCLSerializerWithWeightsCopy(compilerVersion, supportedOpsetVersion)
+ .serialize(nonConstantModel, computeModelHash, storeWeightlessCacheAttributeFlag);
}
std::string serializeIOInfo(const std::shared_ptr<const ov::Model>& model, const bool useIndices) {
@@ -607,9 +649,9 @@ std::string serializeConfig(const Config& config,
}
}
- // As a consequence of complying to the conventions established in the 2.0 OV API, the set of values corresponding
- // to the "model priority" key has been modified cpu_pinning property is not supported in compilers < v5.2 - need to
- // remove it
+ // As a consequence of complying to the conventions established in the 2.0 OV API, the set of values
+ // corresponding to the "model priority" key has been modified cpu_pinning property is not supported in
+ // compilers < v5.2 - need to remove it
if ((compilerVersion.major < 5) || (compilerVersion.major == 5 && compilerVersion.minor < 2)) {
const auto& getTargetRegex = [](const ov::hint::Priority& priorityValue) -> std::regex {
std::ostringstream result;
@@ -637,9 +679,9 @@ std::string serializeConfig(const Config& config,
}
// Special case for compiler Turbo
- // NPU_TURBO is a special option in the sense that by default it is a driver-setting, but certain compilers support
- // and make use of it too If we have turbo in the config string, we check if compiler supports it. If it doesn't
- // support it, we remove it
+ // NPU_TURBO is a special option in the sense that by default it is a driver-setting, but certain compilers
+ // support and make use of it too If we have turbo in the config string, we check if compiler supports it. If it
+ // doesn't support it, we remove it
if (std::regex_search(content, std::regex("NPU_TURBO"))) {
if (!turboSupported) {
std::ostringstream turbostr;
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/weightless_utils.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/weightless_utils.cpp
index 1ad44bacec1..97007903b1d 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/weightless_utils.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/weightless_utils.cpp
@@ -4,9 +4,6 @@
#include "weightless_utils.hpp"
-#include "openvino/core/rt_info/weightless_caching_attributes.hpp"
-#include "openvino/op/constant.hpp"
-
namespace intel_npu {
bool isInitMetadata(const NetworkMetadata& networkMetadata) {
@@ -16,35 +13,4 @@ bool isInitMetadata(const NetworkMetadata& networkMetadata) {
return networkMetadata.inputs.at(0).isInitInputWeights;
}
-/**
- * @brief Stores the information within the "WeightlessCacheAttribute" as runtime fields that persist upon
- * serialization.
- * @details Constant nodes (weights) may contain as mesdatadata the "WeightlessCacheAttribute", that is information
- * regarding the offset of the weights within the binary file, as well as the original size and precision. This
- * information is required within the "weights separation" flow, therefore this function is here to store it.
- * @note Not calling this function in the weights separation flow would lead to this information being lost upon
- * serialization. The "WeightlessCacheAttribute" information that is populated upon de-serialization would represent
- * metadata corresponding to the serialized stream, not the original weights file. Therefore the compiler would be
- * misinformed and lookups of weights offsets could fail.
- *
- * @param model Both source and target.
- */
-void storeWeightlessCacheAttribute(const std::shared_ptr<ov::Model>& model) {
- size_t constantId = 0;
- for (auto&& node : model->get_ordered_ops()) {
- if (ov::is_type<ov::op::v0::Constant>(node)) {
- ov::RTMap& runtimeInfoMap = node->get_rt_info();
- const auto& weightlessCacheAttrIt =
- runtimeInfoMap.find(ov::WeightlessCacheAttribute::get_type_info_static());
-
- const std::string constantIdString = std::to_string(constantId++);
- if (weightlessCacheAttrIt != runtimeInfoMap.end()) {
- auto& weightlessCacheAttr = weightlessCacheAttrIt->second.as<ov::WeightlessCacheAttribute>();
- model->set_rt_info(weightlessCacheAttr.bin_offset, "ws_bin_offset_" + constantIdString);
- model->set_rt_info(weightlessCacheAttr.original_size, "ws_original_size_" + constantIdString);
- model->set_rt_info(weightlessCacheAttr.original_dtype, "ws_original_dtype_" + constantIdString);
- }
- }
- }
-}
} // namespace intel_npu
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp
index 019003c61e6..00a0f154427 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp
@@ -314,8 +314,8 @@ std::unordered_set<std::string> ZeGraphExtWrappers::queryGraph(SerializedIR seri
_graphExtVersion < ZE_MAKE_VERSION(1, 16) ? ZE_STRUCTURE_TYPE_GRAPH_DESC : ZE_STRUCTURE_TYPE_GRAPH_DESC_2,
nullptr,
ZE_GRAPH_FORMAT_NGRAPH_LITE,
- serializedIR.first,
- serializedIR.second.get(),
+ serializedIR.size,
+ serializedIR.buffer.get(),
buildFlags.c_str(),
ZE_GRAPH_FLAG_NONE};
@@ -376,6 +376,12 @@ GraphDescriptor ZeGraphExtWrappers::getGraphDescriptor(SerializedIR serializedIR
const std::string& buildFlags,
const bool bypassUmdCache) const {
ze_graph_handle_t graphHandle = nullptr;
+ void* pNext = nullptr;
+ ze_graph_input_hash_t modelHash;
+ if (serializedIR.hash.has_value()) {
+ modelHash = {ZE_STRUCTURE_TYPE_GRAPH_INPUT_HASH, nullptr, serializedIR.hash.value()};
+ pNext = &modelHash;
+ }
uint32_t flags = ZE_GRAPH_FLAG_NONE;
if (bypassUmdCache) {
@@ -384,10 +390,10 @@ GraphDescriptor ZeGraphExtWrappers::getGraphDescriptor(SerializedIR serializedIR
}
ze_graph_desc_2_t desc = {ZE_STRUCTURE_TYPE_GRAPH_DESC_2,
- nullptr,
+ pNext,
ZE_GRAPH_FORMAT_NGRAPH_LITE,
- serializedIR.first,
- serializedIR.second.get(),
+ serializedIR.size,
+ serializedIR.buffer.get(),
buildFlags.c_str(),
flags};
@@ -667,4 +673,8 @@ bool ZeGraphExtWrappers::isTurboOptionSupported(const ze_graph_compiler_version_
return is_supported;
}
+bool ZeGraphExtWrappers::isPluginModelHashSupported() const {
+ return _graphExtVersion > ZE_MAKE_VERSION(1, 13);
+}
+
} // 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 bb1072f602b..9fdab1d0967 100644
--- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp
+++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp
@@ -1070,7 +1070,7 @@ ov::SupportedOpsMap Plugin::query_model(const std::shared_ptr<const ov::Model>&
ov::SupportedOpsMap supportedOpsMap;
try {
- supportedOpsMap = compiler->query(model, localConfig);
+ supportedOpsMap = compiler->query(model->clone(), localConfig);
} catch (const std::runtime_error& e) {
OPENVINO_THROW(e.what());
} catch (...) {
diff --git a/src/plugins/intel_npu/tests/functional/behavior/npu_driver_compiler_adapter/custom_stream.cpp b/src/plugins/intel_npu/tests/functional/behavior/npu_driver_compiler_adapter/custom_stream.cpp
index 29207922e60..eac16909865 100644
--- a/src/plugins/intel_npu/tests/functional/behavior/npu_driver_compiler_adapter/custom_stream.cpp
+++ b/src/plugins/intel_npu/tests/functional/behavior/npu_driver_compiler_adapter/custom_stream.cpp
@@ -8,6 +8,8 @@
#include "common/npu_test_env_cfg.hpp"
#include "common_test_utils/node_builders/constant.hpp"
#include "intel_npu/config/options.hpp"
+#include "openvino/core/rt_info/weightless_caching_attributes.hpp"
+#include "openvino/core/runtime_attribute.hpp"
#include "openvino/opsets/opset11.hpp"
#include "openvino/pass/serialize.hpp"
#include "shared_test_classes/base/ov_behavior_test_utils.hpp"
@@ -24,13 +26,18 @@ namespace {
constexpr size_t SERIALIZED_MODEL_THRESHOLD_ALL_WEIGHTS_COPY = 300000;
constexpr size_t SERIALIZED_MODEL_THRESHOLD_NO_WEIGHTS_COPY = 200000;
-std::shared_ptr<ov::Model> createModelWithLargeWeights() {
+std::shared_ptr<ov::Model> createModelWithLargeWeights(const bool placeOneWeightlessCacheAttribute = false) {
auto data = std::make_shared<ov::opset11::Parameter>(ov::element::f32, ov::Shape{100000});
auto mul_constant = ov::opset11::Constant::create(ov::element::f32, ov::Shape{1}, {1.5});
auto mul = std::make_shared<ov::opset11::Multiply>(data, mul_constant);
auto add_constant = ov::opset11::Constant::create(ov::element::f32, ov::Shape{1}, {0.5});
auto add = std::make_shared<ov::opset11::Add>(mul, add_constant);
+ if (placeOneWeightlessCacheAttribute) {
+ add_constant->get_rt_info()[ov::WeightlessCacheAttribute::get_type_info_static()] =
+ ov::WeightlessCacheAttribute(add_constant->get_byte_size(), 0, add_constant->get_element_type());
+ }
+
// Just a sample model here, large iteration to make the model large
for (int i = 0; i < 100; i++) {
add_constant = ov::opset11::Constant::create(ov::element::f32, ov::Shape{100000}, {0.5});
@@ -41,6 +48,32 @@ std::shared_ptr<ov::Model> createModelWithLargeWeights() {
return std::make_shared<ov::Model>(ov::ResultVector{std::move(res)}, ov::ParameterVector{std::move(data)});
}
+class TestNonDeterministicAttribute : public ov::RuntimeAttribute {
+public:
+ bool visit_attributes(ov::AttributeVisitor& visitor) override {
+ int test_value = 0;
+ visitor.on_attribute("test123", test_value);
+ return true;
+ }
+
+ bool is_deterministic() const override {
+ return false;
+ }
+};
+
+class TestDeterministicAttribute : public ov::RuntimeAttribute {
+public:
+ bool visit_attributes(ov::AttributeVisitor& visitor) override {
+ int test_value = 0;
+ visitor.on_attribute("test234", test_value);
+ return true;
+ }
+
+ bool is_deterministic() const override {
+ return true;
+ }
+};
+
} // namespace
namespace ov::test::behavior {
@@ -91,7 +124,7 @@ TEST_P(DriverCompilerAdapterCustomStreamTestNPU, TestLargeModelWeightsCopy) {
EXPECT_NO_THROW(serializedModel =
::intel_npu::driver_compiler_utils::serializeIR(model, dummyCompilerVersion, 11, true));
// If the size changes significantly, then investigation may be required
- ASSERT_TRUE(serializedModel.first > SERIALIZED_MODEL_THRESHOLD_ALL_WEIGHTS_COPY);
+ ASSERT_TRUE(serializedModel.size > SERIALIZED_MODEL_THRESHOLD_ALL_WEIGHTS_COPY);
}
TEST_P(DriverCompilerAdapterCustomStreamTestNPU, TestLargeModelNoWeightsCopy) {
@@ -102,13 +135,131 @@ TEST_P(DriverCompilerAdapterCustomStreamTestNPU, TestLargeModelNoWeightsCopy) {
EXPECT_NO_THROW(serializedModel =
::intel_npu::driver_compiler_utils::serializeIR(model, dummyCompilerVersion, 11, false));
// If the size changes significantly, then investigation may be required
- ASSERT_TRUE(serializedModel.first < SERIALIZED_MODEL_THRESHOLD_NO_WEIGHTS_COPY);
+ ASSERT_TRUE(serializedModel.size < SERIALIZED_MODEL_THRESHOLD_NO_WEIGHTS_COPY);
ov::pass::StreamSerialize::DataHeader dataHeader;
- memcpy(&dataHeader, serializedModel.second.get(), sizeof(dataHeader));
+ memcpy(&dataHeader, serializedModel.buffer.get(), sizeof(dataHeader));
ASSERT_TRUE(dataHeader.consts_size == 0);
}
+TEST_P(DriverCompilerAdapterCustomStreamTestNPU, CheckHashPresence) {
+ auto model = createModelWithLargeWeights();
+ const ze_graph_compiler_version_info_t dummyCompilerVersion{0, 0};
+
+ ::intel_npu::SerializedIR serializedModel;
+ EXPECT_NO_THROW(serializedModel =
+ ::intel_npu::driver_compiler_utils::serializeIR(model, dummyCompilerVersion, 11, false));
+ ASSERT_FALSE(serializedModel.hash.has_value());
+
+ EXPECT_NO_THROW(serializedModel =
+ ::intel_npu::driver_compiler_utils::serializeIR(model, dummyCompilerVersion, 11, false, true));
+ ASSERT_TRUE(serializedModel.hash.has_value());
+}
+
+/**
+ * @brief The serialization function is able to store the WeightlessCacheAttribute using the predetermined contract
+ * expected by the driver-compiler adapter.
+ */
+TEST_P(DriverCompilerAdapterCustomStreamTestNPU, CheckWeightlessCacheAttributePresence) {
+ auto model = createModelWithLargeWeights(true);
+ const ze_graph_compiler_version_info_t dummyCompilerVersion{0, 0};
+
+ ::intel_npu::SerializedIR serializedModel;
+ EXPECT_NO_THROW(
+ serializedModel =
+ ::intel_npu::driver_compiler_utils::serializeIR(model->clone(), dummyCompilerVersion, 11, false));
+ ASSERT_FALSE(model->has_rt_info("ws_bin_offset_1"));
+
+ EXPECT_NO_THROW(
+ serializedModel =
+ ::intel_npu::driver_compiler_utils::serializeIR(model, dummyCompilerVersion, 11, false, false, true));
+ // Follows the contract established with the driver-compiler adapter. Predefined prefix + a topological ID of the
+ // Constant node
+ ASSERT_TRUE(model->has_rt_info("ws_bin_offset_1"));
+}
+
+/**
+ * @brief The custom format used by the plugin to store the WeightlessCacheAttribute changes the value of the hash.
+ */
+TEST_P(DriverCompilerAdapterCustomStreamTestNPU, CheckWeightlessCacheAttributeChangesHash) {
+ auto model = createModelWithLargeWeights(false);
+ const ze_graph_compiler_version_info_t dummyCompilerVersion{0, 0};
+
+ ::intel_npu::SerializedIR serializedModel;
+ EXPECT_NO_THROW(
+ serializedModel =
+ ::intel_npu::driver_compiler_utils::serializeIR(model, dummyCompilerVersion, 11, false, true, true));
+ ASSERT_TRUE(serializedModel.hash.has_value());
+ const uint64_t hashNoWCA = serializedModel.hash.value();
+
+ model = createModelWithLargeWeights(true);
+ EXPECT_NO_THROW(
+ serializedModel =
+ ::intel_npu::driver_compiler_utils::serializeIR(model, dummyCompilerVersion, 11, false, true, true));
+
+ ASSERT_FALSE(hashNoWCA == serializedModel.hash.value());
+}
+
+/**
+ * @brief The hash produced by the serialization function should ignore only non-deterministic runtime attributes.
+ */
+TEST_P(DriverCompilerAdapterCustomStreamTestNPU, CheckPluginHashIgnoresOnlyNondeterministicField) {
+ auto model = createModelWithLargeWeights();
+ const ze_graph_compiler_version_info_t dummyCompilerVersion{0, 0};
+
+ ::intel_npu::SerializedIR serializedModel;
+ EXPECT_NO_THROW(serializedModel = ::intel_npu::driver_compiler_utils::serializeIR(model->clone(),
+ dummyCompilerVersion,
+ 11,
+ false,
+ true,
+ false));
+ ASSERT_TRUE(serializedModel.hash.has_value());
+ const uint64_t hashNoAttribute = serializedModel.hash.value();
+
+ model->input().get_node()->get_rt_info()[TestNonDeterministicAttribute::get_type_info_static()] =
+ TestNonDeterministicAttribute();
+ EXPECT_NO_THROW(serializedModel = ::intel_npu::driver_compiler_utils::serializeIR(model->clone(),
+ dummyCompilerVersion,
+ 11,
+ false,
+ true,
+ false));
+ ASSERT_TRUE(hashNoAttribute == serializedModel.hash.value());
+
+ model->input().get_node()->get_rt_info()[TestDeterministicAttribute::get_type_info_static()] =
+ TestDeterministicAttribute();
+ EXPECT_NO_THROW(
+ serializedModel =
+ ::intel_npu::driver_compiler_utils::serializeIR(model, dummyCompilerVersion, 11, false, true, false));
+ ASSERT_FALSE(hashNoAttribute == serializedModel.hash.value());
+}
+
+/**
+ * @brief The plugin should produce the same hash for the same model, different instances.
+ */
+TEST_P(DriverCompilerAdapterCustomStreamTestNPU, CheckSameModelDifferentInstancesSamePluginHash) {
+ auto model = createModelWithLargeWeights();
+ const ze_graph_compiler_version_info_t dummyCompilerVersion{0, 0};
+
+ ::intel_npu::SerializedIR serializedModel;
+ EXPECT_NO_THROW(serializedModel =
+ ::intel_npu::driver_compiler_utils::serializeIR(model,
+ dummyCompilerVersion,
+ 10, // Triggers an additional pass
+ false,
+ true,
+ false));
+ ASSERT_TRUE(serializedModel.hash.has_value());
+ const uint64_t hashFirstInstance = serializedModel.hash.value();
+
+ model = createModelWithLargeWeights();
+ EXPECT_NO_THROW(
+ serializedModel =
+ ::intel_npu::driver_compiler_utils::serializeIR(model, dummyCompilerVersion, 10, false, true, false));
+ ASSERT_TRUE(hashFirstInstance == serializedModel.hash.value());
+}
+
const std::vector<ov::AnyMap> configs = {
{{ov::intel_npu::compiler_type(ov::intel_npu::CompilerType::DRIVER)}},
};
@@ -118,4 +269,5 @@ INSTANTIATE_TEST_SUITE_P(smoke_BehaviorTest,
::testing::Combine(::testing::Values(ov::test::utils::DEVICE_NPU),
::testing::ValuesIn(configs)),
DriverCompilerAdapterCustomStreamTestNPU::getTestCaseName);
+
} // namespace ov::test::behavior