Skip to content

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

[NPUW] Support CompiledModel export/import (#30360)

Alexey Smirnovb9e7c4cd55dd
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
index 0124c8fd9d8..797993e205b 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp
@@ -141,12 +141,13 @@ std::shared_ptr<ov::npuw::ICompiledModel> ov::npuw::ICompiledModel::create(
compiled_model = std::make_shared<ov::npuw::LLMCompiledModel>(model, plugin, config);
} else {
LOG_INFO("ov::npuw::CompiledModel will be created.");
- // CACHE_DIR isn't supported with NPU_USE_NPUW
- if (properties.count(ov::cache_dir.name())) {
- OPENVINO_THROW("Option 'CACHE_DIR' is not supported with configuration: NPU_USE_NPUW : YES, NPUW_LLM : NO");
- }
+ // Drop CACHE_DIR from the config
+ // If it's present we will be utilizing LLMCompiledModel's import
+ // and not the underlying models and submodels
+ auto config = properties;
+ config.erase(ov::cache_dir.name());
pre_load_transform(model, properties);
- compiled_model = std::make_shared<ov::npuw::CompiledModel>(model, plugin, properties);
+ compiled_model = std::make_shared<ov::npuw::CompiledModel>(model, plugin, config);
}
LOG_INFO("Done");
return compiled_model;
@@ -653,79 +654,78 @@ void ov::npuw::CompiledModel::CompiledModelDesc::deserialize(std::istream& strea
LOG_DEBUG("DONE.");
}
-void ov::npuw::CompiledModel::serialize(std::ostream& stream) const {
- LOG_INFO("Serializing CompiledModel...");
- LOG_BLOCK();
-
+void ov::npuw::CompiledModel::export_model(std::ostream& stream) const {
using namespace ov::npuw::s11n;
- // Serialize name
- write(stream, m_name);
-
- // Serialize inputs and outputs
- write(stream, inputs());
- write(stream, outputs());
-
- // Serialize meta
- write(stream, m_inputs_to_submodels_inputs);
- write(stream, m_outputs_to_submodels_outputs);
- write(stream, m_param_subscribers);
- write(stream, m_submodels_input_to_prev_output);
-
- // Write device list
- write(stream, m_dev_list);
-
- // Write config
- write(stream, m_cfg);
- // FIXME: utilize overload instead
- write(stream, m_non_npuw_props.size());
- for (const auto& p : m_non_npuw_props) {
- // Skip properties which don't need to/can't be serialized
- // FIXME: extend the logic
- if (p.first == ov::cache_encryption_callbacks.name()) {
- write(stream, false);
- continue;
- }
- write(stream, true);
- write(stream, p.first);
- write_any(stream, p.second);
+ // Identify encryption flow
+ bool encryption_required = false;
+ EncryptionCallbacks enc_callbacks;
+ if (auto it = m_non_npuw_props.find(ov::cache_encryption_callbacks.name());
+ it != m_non_npuw_props.end() && it->second.as<EncryptionCallbacks>().encrypt) {
+ LOG_INFO("Encryption will be done via the function provided.");
+ encryption_required = true;
+ enc_callbacks.encrypt = it->second.as<EncryptionCallbacks>().encrypt;
}
- // Write flow identifier
+ // Identify either full flow or weightless
bool is_weightless = true;
- if (m_non_npuw_props.count(ov::cache_mode.name()) &&
- m_non_npuw_props.at(ov::cache_mode.name()).as<CacheMode>() == CacheMode::OPTIMIZE_SPEED) {
+ if (auto it = m_non_npuw_props.find(ov::cache_mode.name());
+ it != m_non_npuw_props.end() && it->second.as<CacheMode>() == CacheMode::OPTIMIZE_SPEED) {
+ LOG_INFO("Serialization will be done via flow with weights.");
is_weightless = false;
}
+
+ // Write header regardless of encryption requirement - to identify NPUW serializated blobs
+ // Serialize magic number first
+ write(stream, NPUW_SERIALIZATION_INDICATOR);
+ // Serilize CompiledModel identifier
+ write(stream, NPUW_COMPILED_MODEL_INDICATOR);
+ // Serialize general meta info
+ write(stream, OPENVINO_VERSION_MAJOR);
+ write(stream, OPENVINO_VERSION_MINOR);
+ write(stream, OPENVINO_VERSION_PATCH);
+ write(stream, std::string(NPUW_SERIALIZATION_VERSION));
+ // Serialize encrypted flag
+ write(stream, encryption_required);
+ // Write flow identifier
write(stream, is_weightless);
- // Create weightless context
- WeightsContext ctx(is_weightless, m_const_to_offset);
-
- // Serialize compiled submodels
- write(stream, m_compiled_submodels.size());
- for (const auto& subm : m_compiled_submodels) {
- // Write device idx
- std::size_t device_idx = subm.device_it - m_dev_list.begin();
- write(stream, device_idx);
- // Write ICompiledModel if it's there
- if (subm.compiled_model) {
- write(stream, true);
- // FIXME: workaround for import/export model since import model seem to reset the file pointer
- std::stringstream ss;
- subm.compiled_model->export_model(ss);
- write(stream, ss.str());
- } else {
- write(stream, false);
+ if (!encryption_required) {
+ EncryptContext ctx(false, nullptr, nullptr);
+ serialize(stream, ctx);
+
+ write(stream, m_weights_bank->get_name());
+ if (!is_weightless) {
+ // Serialize weights bank
+ // Note: no need to encrypt weights in full flow
+ m_weights_bank->serialize(stream);
}
- // Write the rest of the submodel desc
- subm.serialize(stream, ctx);
+ return;
}
- LOG_INFO("Done.");
+ // In case of weightless flow the whole blob will be encrypted on NPUW side.
+ std::stringstream non_encrypted_stream;
+ if (is_weightless) {
+ non_encrypted_stream.copyfmt(stream);
+ EncryptContext ctx(false, nullptr, nullptr);
+ serialize(non_encrypted_stream, ctx);
+ std::string encrypted = enc_callbacks.encrypt(non_encrypted_stream.str());
+ write(stream, encrypted);
+ } else {
+ // In case of blob with weights only encrypt XML part of the model
+ EncryptContext ctx(true, enc_callbacks.encrypt, nullptr);
+ serialize(stream, ctx);
+ }
+
+ write(stream, m_weights_bank->get_name());
+ if (!is_weightless) {
+ // Serialize weights bank
+ // Note: no need to encrypt weights in full flow
+ m_weights_bank->serialize(stream);
+ }
}
-std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::deserialize(
+std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::import_model(
std::istream& stream,
const std::shared_ptr<const ov::IPlugin>& plugin,
const ov::AnyMap& properties) {
@@ -734,131 +734,341 @@ std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::deserialize(
using namespace ov::npuw::s11n;
- // Deserialize model name first
- std::string model_name;
- read(stream, model_name);
-
- // Create a dummy CompiledModel with an empty ov::Model - this will skip the constructor flow
- // to continue deserialization
- ov::ParameterVector parameters;
- ov::NodeVector results;
-
- read(stream, parameters);
- read(stream, results);
-
- auto ov_model = std::make_shared<ov::Model>(ov::as_output_vector(results), parameters, model_name);
-
- auto compiled = std::make_shared<ov::npuw::CompiledModel>(ov_model, plugin, true);
-
- // Deserialize meta
- compiled->m_name = model_name;
- read(stream, compiled->m_inputs_to_submodels_inputs);
- read(stream, compiled->m_outputs_to_submodels_outputs);
- read(stream, compiled->m_param_subscribers);
- read(stream, compiled->m_submodels_input_to_prev_output);
-
- // Deserialize device list
- read(stream, compiled->m_dev_list);
-
- // Deserialize config
- read(stream, compiled->m_cfg);
- compiled->m_cfg.parseEnvVars();
- // FIXME: utilize overload instead
- std::size_t props_size;
- read(stream, props_size);
- for (std::size_t i = 0; i < props_size; ++i) {
- bool should_read = true;
- read(stream, should_read);
- // Skip properties which don't need to/can't be deserialized
- // FIXME: extend the logic
- if (!should_read) {
- continue;
- }
- std::string key;
- read(stream, key);
- ov::Any val;
- read_any(stream, val);
- compiled->m_non_npuw_props[key] = std::move(val);
+ // Sanity check magic number
+ ov::npuw::s11n::IndicatorType serialization_indicator;
+ read(stream, serialization_indicator);
+ NPUW_ASSERT(serialization_indicator == NPUW_SERIALIZATION_INDICATOR && "This blob wasn't serialized via NPUW!");
+
+ ov::npuw::s11n::IndicatorType compiled_indicator;
+ read(stream, compiled_indicator);
+ NPUW_ASSERT(compiled_indicator == NPUW_COMPILED_MODEL_INDICATOR &&
+ "This blob wasn't serialized via CompiledModel!");
+
+ // Deserialize general meta info
+ int vmajor, vminor, vpatch;
+ std::string s11n_version;
+ read(stream, vmajor);
+ read(stream, vminor);
+ read(stream, vpatch);
+ read(stream, s11n_version);
+
+ if (vmajor != OPENVINO_VERSION_MAJOR || vminor != OPENVINO_VERSION_MINOR || vpatch != OPENVINO_VERSION_PATCH ||
+ s11n_version != std::string(NPUW_SERIALIZATION_VERSION)) {
+ OPENVINO_THROW("This blobs was serialized with different OV version!",
+ "\nSerialized by OV ",
+ vmajor,
+ '.',
+ vminor,
+ '.',
+ vpatch,
+ "\nCurrent OV version ",
+ OPENVINO_VERSION_MAJOR,
+ '.',
+ OPENVINO_VERSION_MINOR,
+ '.',
+ OPENVINO_VERSION_PATCH,
+ "\nNPUW serialized by version ",
+ s11n_version,
+ "\nNPUW current serialization version ",
+ NPUW_SERIALIZATION_VERSION);
}
- compiled->implement_properties();
- // Read flow identifier
- bool is_weightless = false;
+ bool encrypted = false;
+ read(stream, encrypted);
+ bool is_weightless = true;
read(stream, is_weightless);
- // Initialize weights stream if weightless flow
- std::string weights_path;
- std::shared_ptr<const ov::Model> model_ptr;
- // Cache model's constants
- WeightsContext::ConstsCache consts_cache;
- if (is_weightless) {
- if (properties.find(ov::weights_path.name()) != properties.end()) {
- weights_path = properties.at(ov::weights_path.name()).as<std::string>();
- NPUW_ASSERT(!weights_path.empty() &&
- "Empty weights_path. Please provide WEIGHTS_PATH or MODEL_PTR in the configuration.");
- } else if (properties.find(ov::hint::model.name()) != properties.end()) {
- model_ptr = properties.at(ov::hint::model.name()).as<std::shared_ptr<const ov::Model>>();
- NPUW_ASSERT(
- model_ptr &&
- "Empty model passed in MODEL_PTR. Please provide WEIGHTS_PATH or MODEL_PTR in the configuration.");
- // Fill the cache
- for (const auto& node : model_ptr->get_ordered_ops()) {
- if (ov::op::util::is_constant(node)) {
- const auto& c = std::static_pointer_cast<ov::op::v0::Constant>(node);
- auto rt_info = c->get_rt_info();
- auto weightless_cache_attr = rt_info.find(ov::WeightlessCacheAttribute::get_type_info_static());
- if (weightless_cache_attr == rt_info.end()) {
- continue;
- }
- std::size_t offset = weightless_cache_attr->second.as<ov::WeightlessCacheAttribute>().bin_offset;
- std::size_t size = c->get_byte_size();
- consts_cache[{offset, size}] = node;
- }
- }
+ auto read_and_finalize_bank = [&](std::istream& model_stream,
+ const std::shared_ptr<ov::npuw::CompiledModel>& compiled) {
+ // Deserialize weights bank name
+ std::string bank_name;
+ read(model_stream, bank_name);
+
+ if (is_weightless) {
+ auto bank = ov::npuw::weights::bank(bank_name, compiled->get_plugin()->get_core(), "");
+ compiled->m_weights_bank = bank;
+ compiled->finalize_weights_bank();
} else {
- NPUW_ASSERT(false && "Blob is weightless but no WEIGHTS_PATH nor MODEL_PTR property is provided!");
+ auto bank =
+ ov::npuw::weights::Bank::deserialize(model_stream, compiled->get_plugin()->get_core(), bank_name);
+ compiled->m_weights_bank = bank;
+ compiled->reconstruct_closure();
}
+ };
+
+ if (!encrypted) {
+ EncryptContext ctx(false, nullptr, nullptr);
+ auto compiled_model = ov::npuw::CompiledModel::deserialize(stream, plugin, properties, ctx);
+ NPUW_ASSERT(compiled_model && "Couldn't import NPUW compiled model!");
+ read_and_finalize_bank(stream, compiled_model);
+ LOG_INFO("Done.");
+ return compiled_model;
}
- ov::npuw::s11n::Weights weights = nullptr;
+ EncryptionCallbacks enc_callbacks;
+ NPUW_ASSERT(properties.count(ov::cache_encryption_callbacks.name()) &&
+ properties.at(ov::cache_encryption_callbacks.name()).as<EncryptionCallbacks>().decrypt &&
+ "Model is encrypted but no decrypt function was provided!");
+ enc_callbacks.decrypt = properties.at(ov::cache_encryption_callbacks.name()).as<EncryptionCallbacks>().decrypt;
+
+ LOG_INFO("Decryption will be done via the function provided.");
+
+ std::shared_ptr<ov::npuw::CompiledModel> compiled_model = nullptr;
+
+ // Model is encrypted
if (is_weightless) {
- if (!weights_path.empty()) {
- auto mapped_memory = ov::load_mmap_object(weights_path);
- weights = std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::MappedMemory>>>(mapped_memory->data(),
- mapped_memory->size(),
- mapped_memory);
- }
+ std::string encrypted_str;
+ read(stream, encrypted_str);
+ std::istringstream decrypted_stream(std::move(enc_callbacks.decrypt(encrypted_str)));
+ EncryptContext ctx(false, nullptr, nullptr);
+ compiled_model = ov::npuw::CompiledModel::deserialize(decrypted_stream, plugin, properties, ctx);
+ } else {
+ EncryptContext ctx(true, nullptr, enc_callbacks.decrypt);
+ compiled_model = ov::npuw::CompiledModel::deserialize(stream, plugin, properties, ctx);
}
- WeightsContext ctx(weights, consts_cache);
-
- // Deserialize compiled submodels
- std::size_t subm_size = 0;
- read(stream, subm_size);
- compiled->m_compiled_submodels.resize(subm_size);
- for (std::size_t i = 0; i < subm_size; ++i) {
- std::size_t device_idx = 0;
- read(stream, device_idx);
-
- bool has_compiled_model = false;
- read(stream, has_compiled_model);
- if (has_compiled_model) {
- // Import model from the plugin
- // FIXME: workaround for import/export model since import model seems to reset the file pointer
- std::string buf;
- read(stream, buf);
- std::stringstream buffer(buf);
- compiled->m_compiled_submodels[i].compiled_model =
- plugin->get_core()->import_model(buffer, compiled->m_dev_list[device_idx]);
+ NPUW_ASSERT(compiled_model && "Couldn't import NPUW compiled model!");
+ read_and_finalize_bank(stream, compiled_model);
+
+ LOG_INFO("Done.");
+ return compiled_model;
+}
+
+void ov::npuw::CompiledModel::serialize(std::ostream& stream, const ov::npuw::s11n::EncryptContext& enc_ctx) const {
+ LOG_INFO("Serializing CompiledModel...");
+ LOG_BLOCK();
+
+ using namespace ov::npuw::s11n;
+
+ auto write_model = [&](std::ostream& model_stream) {
+ // Serialize name
+ write(model_stream, m_name);
+
+ // Serialize inputs and outputs
+ write(model_stream, inputs());
+ write(model_stream, outputs());
+
+ // Serialize meta
+ write(model_stream, m_inputs_to_submodels_inputs);
+ write(model_stream, m_outputs_to_submodels_outputs);
+ write(model_stream, m_param_subscribers);
+ write(model_stream, m_submodels_input_to_prev_output);
+
+ // Write device list
+ write(model_stream, m_dev_list);
+
+ // Write config
+ write(model_stream, m_cfg);
+ // FIXME: utilize overload instead
+ write(model_stream, m_non_npuw_props.size());
+ for (const auto& p : m_non_npuw_props) {
+ // Skip properties which don't need to/can't be serialized
+ // FIXME: extend the logic
+ if (p.first == ov::cache_encryption_callbacks.name()) {
+ write(model_stream, false);
+ continue;
+ }
+ write(model_stream, true);
+ write(model_stream, p.first);
+ write_any(model_stream, p.second);
}
- compiled->m_compiled_submodels[i].device_it = compiled->m_dev_list.begin() + device_idx;
- compiled->m_compiled_submodels[i].deserialize(stream, ctx);
- }
- compiled->implement_properties();
- compiled->report_io();
+ // Write flow identifier
+ bool is_weightless = true;
+ if (m_non_npuw_props.count(ov::cache_mode.name()) &&
+ m_non_npuw_props.at(ov::cache_mode.name()).as<CacheMode>() == CacheMode::OPTIMIZE_SPEED) {
+ is_weightless = false;
+ }
+ write(model_stream, is_weightless);
+
+ // Create weightless context
+ WeightsContext ctx(is_weightless, m_const_to_offset);
+
+ // Serialize compiled submodels
+ write(model_stream, m_compiled_submodels.size());
+ for (const auto& subm : m_compiled_submodels) {
+ // Write device idx
+ std::size_t device_idx = subm.device_it - m_dev_list.begin();
+ write(model_stream, device_idx);
+ // Write ICompiledModel if it's there
+ if (subm.compiled_model) {
+ write(model_stream, true);
+ // FIXME: workaround for import/export model since import model seem to reset the file pointer
+ std::stringstream ss;
+ subm.compiled_model->export_model(ss);
+ write(model_stream, ss.str());
+ } else {
+ write(model_stream, false);
+ }
+ // Write the rest of the submodel desc
+ subm.serialize(model_stream, ctx);
+ }
+ };
+
+ std::stringstream non_encrypted_stream;
+ if (enc_ctx.encrypted) {
+ NPUW_ASSERT(enc_ctx.encrypt && "Encryption function isn't provided!");
+ non_encrypted_stream.copyfmt(stream);
+ write_model(non_encrypted_stream);
+ std::string encrypted_str = enc_ctx.encrypt(non_encrypted_stream.str());
+ write(stream, encrypted_str);
+ } else {
+ write_model(stream);
+ }
LOG_INFO("Done.");
+}
+
+std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::deserialize(
+ std::istream& stream,
+ const std::shared_ptr<const ov::IPlugin>& plugin,
+ const ov::AnyMap& properties,
+ const ov::npuw::s11n::EncryptContext& enc_ctx) {
+ LOG_INFO("Deserializing CompiledModel...");
+ LOG_BLOCK();
+
+ using namespace ov::npuw::s11n;
+
+ auto read_model = [&](std::istream& model_stream) {
+ // Deserialize model name first
+ std::string model_name;
+ read(stream, model_name);
+
+ // Create a dummy CompiledModel with an empty ov::Model - this will skip the constructor flow
+ // to continue deserialization
+ ov::ParameterVector parameters;
+ ov::NodeVector results;
+
+ read(stream, parameters);
+ read(stream, results);
+
+ auto ov_model = std::make_shared<ov::Model>(ov::as_output_vector(results), parameters, model_name);
+
+ auto compiled = std::make_shared<ov::npuw::CompiledModel>(ov_model, plugin, true);
+
+ // Deserialize meta
+ compiled->m_name = model_name;
+ read(stream, compiled->m_inputs_to_submodels_inputs);
+ read(stream, compiled->m_outputs_to_submodels_outputs);
+ read(stream, compiled->m_param_subscribers);
+ read(stream, compiled->m_submodels_input_to_prev_output);
+
+ // Deserialize device list
+ read(stream, compiled->m_dev_list);
+
+ // Deserialize config
+ read(stream, compiled->m_cfg);
+ compiled->m_cfg.parseEnvVars();
+ // FIXME: utilize overload instead
+ std::size_t props_size;
+ read(stream, props_size);
+ for (std::size_t i = 0; i < props_size; ++i) {
+ bool should_read = true;
+ read(stream, should_read);
+ // Skip properties which don't need to/can't be deserialized
+ // FIXME: extend the logic
+ if (!should_read) {
+ continue;
+ }
+ std::string key;
+ read(stream, key);
+ ov::Any val;
+ read_any(stream, val);
+ compiled->m_non_npuw_props[key] = std::move(val);
+ }
+ compiled->implement_properties();
+
+ // Read flow identifier
+ bool is_weightless = false;
+ read(stream, is_weightless);
+
+ // Initialize weights stream if weightless flow
+ std::string weights_path;
+ std::shared_ptr<const ov::Model> model_ptr;
+ // Cache model's constants
+ WeightsContext::ConstsCache consts_cache;
+ if (is_weightless) {
+ if (properties.find(ov::weights_path.name()) != properties.end()) {
+ weights_path = properties.at(ov::weights_path.name()).as<std::string>();
+ NPUW_ASSERT(!weights_path.empty() &&
+ "Empty weights_path. Please provide WEIGHTS_PATH or MODEL_PTR in the configuration.");
+ } else if (properties.find(ov::hint::model.name()) != properties.end()) {
+ model_ptr = properties.at(ov::hint::model.name()).as<std::shared_ptr<const ov::Model>>();
+ NPUW_ASSERT(
+ model_ptr &&
+ "Empty model passed in MODEL_PTR. Please provide WEIGHTS_PATH or MODEL_PTR in the configuration.");
+ // Fill the cache
+ for (const auto& node : model_ptr->get_ordered_ops()) {
+ if (ov::op::util::is_constant(node)) {
+ const auto& c = std::static_pointer_cast<ov::op::v0::Constant>(node);
+ auto rt_info = c->get_rt_info();
+ auto weightless_cache_attr = rt_info.find(ov::WeightlessCacheAttribute::get_type_info_static());
+ if (weightless_cache_attr == rt_info.end()) {
+ continue;
+ }
+ std::size_t offset =
+ weightless_cache_attr->second.as<ov::WeightlessCacheAttribute>().bin_offset;
+ std::size_t size = c->get_byte_size();
+ consts_cache[{offset, size}] = node;
+ }
+ }
+ } else {
+ NPUW_ASSERT(false && "Blob is weightless but no WEIGHTS_PATH nor MODEL_PTR property is provided!");
+ }
+ }
+
+ ov::npuw::s11n::Weights weights = nullptr;
+ if (is_weightless) {
+ if (!weights_path.empty()) {
+ auto mapped_memory = ov::load_mmap_object(weights_path);
+ weights = std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::MappedMemory>>>(mapped_memory->data(),
+ mapped_memory->size(),
+ mapped_memory);
+ }
+ }
+
+ WeightsContext ctx(weights, consts_cache);
+
+ // Deserialize compiled submodels
+ std::size_t subm_size = 0;
+ read(stream, subm_size);
+ compiled->m_compiled_submodels.resize(subm_size);
+ for (std::size_t i = 0; i < subm_size; ++i) {
+ std::size_t device_idx = 0;
+ read(stream, device_idx);
+
+ bool has_compiled_model = false;
+ read(stream, has_compiled_model);
+ if (has_compiled_model) {
+ // Import model from the plugin
+ // FIXME: workaround for import/export model since import model seems to reset the file pointer
+ std::string buf;
+ read(stream, buf);
+ std::stringstream buffer(buf);
+ compiled->m_compiled_submodels[i].compiled_model =
+ plugin->get_core()->import_model(buffer, compiled->m_dev_list[device_idx]);
+ }
+ compiled->m_compiled_submodels[i].device_it = compiled->m_dev_list.begin() + device_idx;
+ compiled->m_compiled_submodels[i].deserialize(stream, ctx);
+ }
+
+ compiled->implement_properties();
+ compiled->report_io();
+ LOG_INFO("Done.");
+ return compiled;
+ };
+
+ std::shared_ptr<ov::npuw::CompiledModel> compiled = nullptr;
+ if (enc_ctx.encrypted) {
+ std::string encrypted_string;
+ read(stream, encrypted_string);
+ std::istringstream decrypted_stream(std::move(enc_ctx.decrypt(encrypted_string)));
+ compiled = read_model(decrypted_stream);
+ } else {
+ compiled = read_model(stream);
+ }
+
+ NPUW_ASSERT(compiled && "Couldn't create NPUW compiled model!");
return compiled;
}
@@ -1256,10 +1466,6 @@ ov::Any ov::npuw::CompiledModel::get_property(const std::string& name) const {
OPENVINO_SUPPRESS_DEPRECATED_END
}
-void ov::npuw::CompiledModel::export_model(std::ostream& model_stream) const {
- OPENVINO_NOT_IMPLEMENTED;
-}
-
std::string ov::npuw::CompiledModel::submodel_device(const std::size_t idx) const {
std::size_t real_idx = m_compiled_submodels[idx].replaced_by.value_or(idx);
const auto& comp_subm_desc = m_compiled_submodels[real_idx];
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
index 0b9392351f0..4e3ab9e363b 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp
@@ -48,6 +48,9 @@ public:
const bool serialized);
void export_model(std::ostream& model) const override;
+ static std::shared_ptr<CompiledModel> import_model(std::istream& stream,
+ const std::shared_ptr<const ov::IPlugin>& plugin,
+ const ov::AnyMap& properties);
std::shared_ptr<const ov::Model> get_runtime_model() const override;
void set_property(const ov::AnyMap& properties) override;
@@ -73,10 +76,11 @@ private:
void report_io() const;
- void serialize(std::ostream& stream) const;
+ void serialize(std::ostream& stream, const ov::npuw::s11n::EncryptContext& ctx) const;
static std::shared_ptr<CompiledModel> deserialize(std::istream& stream,
const std::shared_ptr<const ov::IPlugin>& plugin,
- const ov::AnyMap& properties);
+ const ov::AnyMap& properties,
+ const ov::npuw::s11n::EncryptContext& ctx);
// This is used for removing too long output tensor names to fix some compilation issues
// NB: These two methods has nothing to do with this particular class and should be
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
index 48e4469000d..6d35a8bb37d 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
@@ -731,6 +731,8 @@ void ov::npuw::LLMCompiledModel::export_model(std::ostream& stream) const {
// Write header regardless of encryption requirement - to identify NPUW serializated blobs
// Serialize magic number first
write(stream, NPUW_SERIALIZATION_INDICATOR);
+ // Serilize LLMCompiledModel identifier
+ write(stream, NPUW_LLM_COMPILED_MODEL_INDICATOR);
// Serialize general meta info
write(stream, OPENVINO_VERSION_MAJOR);
write(stream, OPENVINO_VERSION_MINOR);
@@ -794,8 +796,10 @@ void ov::npuw::LLMCompiledModel::serialize(std::ostream& stream, const ov::npuw:
write(model_stream, m_cfg);
// Serialize CompiledModels
- m_kvcache_compiled->serialize(model_stream);
- m_prefill_compiled->serialize(model_stream);
+ // Note: no need to pass any encryption here as it's done in export_model()
+ EncryptContext enc_ctx(false, nullptr, nullptr);
+ m_kvcache_compiled->serialize(model_stream, enc_ctx);
+ m_prefill_compiled->serialize(model_stream, enc_ctx);
};
std::stringstream non_encrypted_stream;
@@ -838,6 +842,11 @@ std::shared_ptr<ov::npuw::LLMCompiledModel> ov::npuw::LLMCompiledModel::import_m
read(stream, serialization_indicator);
NPUW_ASSERT(serialization_indicator == NPUW_SERIALIZATION_INDICATOR && "This blob wasn't serialized via NPUW!");
+ ov::npuw::s11n::IndicatorType llm_compiled_indicator;
+ read(stream, llm_compiled_indicator);
+ NPUW_ASSERT(llm_compiled_indicator == NPUW_LLM_COMPILED_MODEL_INDICATOR &&
+ "This blob wasn't serialized via LLMCompiledModel!");
+
// Deserialize general meta info
int vmajor, vminor, vpatch;
std::string s11n_version;
@@ -973,8 +982,10 @@ std::shared_ptr<ov::npuw::LLMCompiledModel> ov::npuw::LLMCompiledModel::deserial
compiled->implement_properties();
// Deserialize CompiledModels
- compiled->m_kvcache_compiled = ov::npuw::CompiledModel::deserialize(model_stream, plugin, properties);
- compiled->m_prefill_compiled = ov::npuw::CompiledModel::deserialize(model_stream, plugin, properties);
+ // Note: no need to pass any encryption here as it's done in import_model()
+ EncryptContext enc_ctx(false, nullptr, nullptr);
+ compiled->m_kvcache_compiled = ov::npuw::CompiledModel::deserialize(model_stream, plugin, properties, enc_ctx);
+ compiled->m_prefill_compiled = ov::npuw::CompiledModel::deserialize(model_stream, plugin, properties, enc_ctx);
return compiled;
};
diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp
index eabd919c342..071b48c156f 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp
@@ -95,7 +95,8 @@ enum class AnyType : int {
BOOL,
CACHE_MODE,
ELEMENT_TYPE,
- ANYMAP
+ ANYMAP,
+ PERFMODE
};
void ov::npuw::s11n::write_any(std::ostream& stream, const ov::Any& var) {
@@ -137,6 +138,9 @@ void ov::npuw::s11n::write_any(std::ostream& stream, const ov::Any& var) {
} else if (var.is<ov::AnyMap>()) {
write(stream, static_cast<int>(AnyType::ANYMAP));
write(stream, var.as<ov::AnyMap>());
+ } else if (var.is<ov::hint::PerformanceMode>()) {
+ write(stream, static_cast<int>(AnyType::PERFMODE));
+ write(stream, var.as<ov::hint::PerformanceMode>());
} else {
NPUW_ASSERT(false && "Unsupported type");
}
@@ -154,6 +158,10 @@ void ov::npuw::s11n::write(std::ostream& stream, const ov::element::Type& var) {
stream.write(reinterpret_cast<const char*>(&var), sizeof var);
}
+void ov::npuw::s11n::write(std::ostream& stream, const ov::hint::PerformanceMode& var) {
+ stream.write(reinterpret_cast<const char*>(&var), sizeof var);
+}
+
void ov::npuw::s11n::write(std::ostream& stream, const ov::AnyMap& var) {
write(stream, var.size());
for (const auto& el : var) {
@@ -315,6 +323,10 @@ void ov::npuw::s11n::read_any(std::istream& stream, ov::Any& var) {
ov::AnyMap val;
read(stream, val);
var = val;
+ } else if (type == AnyType::PERFMODE) {
+ ov::hint::PerformanceMode val;
+ read(stream, val);
+ var = val;
} else {
NPUW_ASSERT(false && "Unsupported type");
}
@@ -332,6 +344,10 @@ void ov::npuw::s11n::read(std::istream& stream, ov::element::Type& var) {
stream.read(reinterpret_cast<char*>(&var), sizeof var);
}
+void ov::npuw::s11n::read(std::istream& stream, ov::hint::PerformanceMode& var) {
+ stream.read(reinterpret_cast<char*>(&var), sizeof var);
+}
+
void ov::npuw::s11n::read(std::istream& stream, ov::AnyMap& var) {
std::size_t var_size = 0;
read(stream, var_size);
diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
index 7fcf7b4e22b..1022e6c8b02 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp
@@ -28,7 +28,13 @@ using IndicatorType = std::array<uint8_t, 6>;
const constexpr ov::npuw::s11n::IndicatorType NPUW_SERIALIZATION_INDICATOR =
{char{0x13}, char{0x37}, char{0x6e}, char{0x70}, char{0x75}, char{0x77}};
-const constexpr char* NPUW_SERIALIZATION_VERSION = "0.2";
+const constexpr ov::npuw::s11n::IndicatorType NPUW_COMPILED_MODEL_INDICATOR =
+ {char{0x43}, char{0x4f}, char{0x4d}, char{0x50}, char{0x4d}, char{0x4f}};
+
+const constexpr ov::npuw::s11n::IndicatorType NPUW_LLM_COMPILED_MODEL_INDICATOR =
+ {char{0x4c}, char{0x4c}, char{0x4d}, char{0x43}, char{0x4d}, char{0x4f}};
+
+const constexpr char* NPUW_SERIALIZATION_VERSION = "0.3";
// Forward declaration
namespace intel_npu {
@@ -51,6 +57,9 @@ enum class CacheMode;
namespace element {
class Type;
}
+namespace hint {
+enum class PerformanceMode;
+}
// Forward declaration
namespace op {
@@ -121,6 +130,7 @@ void write(std::ostream& stream, const ov::npuw::weights::LazyTensor& var);
void write(std::ostream& stream, const ov::CacheMode& var);
void write(std::ostream& stream, const ov::element::Type& var);
void write(std::ostream& stream, const std::map<std::string, Any>& var);
+void write(std::ostream& stream, const ov::hint::PerformanceMode& var);
void read(std::istream& stream, std::streampos& var);
void read(std::istream& stream, std::string& var);
@@ -136,6 +146,7 @@ void read(std::istream& stream, ov::npuw::weights::LazyTensor& var);
void read(std::istream& stream, ov::CacheMode& var);
void read(std::istream& stream, ov::element::Type& var);
void read(std::istream& stream, std::map<std::string, Any>& var);
+void read(std::istream& stream, ov::hint::PerformanceMode& var);
// Weightless utils
void write_weightless(std::ostream& stream, const std::vector<ov::Tensor>& var, const WeightsContext& ctx);
diff --git a/src/plugins/intel_npu/src/plugin/src/plugin.cpp b/src/plugins/intel_npu/src/plugin/src/plugin.cpp
index 47e324ad4db..b285e52e62c 100644
--- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp
+++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp
@@ -550,9 +550,19 @@ std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& origStrea
ov::npuw::s11n::IndicatorType serialization_indicator;
ov::npuw::s11n::read(stream, serialization_indicator);
if (serialization_indicator == NPUW_SERIALIZATION_INDICATOR) {
+ ov::npuw::s11n::IndicatorType compiled_model_indicator;
+ ov::npuw::s11n::read(stream, compiled_model_indicator);
stream.seekg(-stream.tellg() + stream_start_pos, std::ios::cur);
- // Properties are required for ov::weights_path
- return ov::npuw::LLMCompiledModel::import_model(stream, shared_from_this(), properties);
+
+ if (compiled_model_indicator == NPUW_LLM_COMPILED_MODEL_INDICATOR) {
+ // Properties are required for ov::weights_path
+ return ov::npuw::LLMCompiledModel::import_model(stream, shared_from_this(), properties);
+ } else if (compiled_model_indicator == NPUW_COMPILED_MODEL_INDICATOR) {
+ // Properties are required for ov::weights_path
+ return ov::npuw::CompiledModel::import_model(stream, shared_from_this(), properties);
+ } else {
+ OPENVINO_THROW("Couldn't deserialize NPUW blob - fatal error!");
+ }
}
stream.seekg(-stream.tellg() + stream_start_pos, std::ios::cur);