OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[NPUW] Support blob encryption (#29250)
Alexey Smirnove5c966fe779a
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cppindex 150bc6b6d38..7536b3bcc42 100644--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp@@ -680,6 +680,13 @@ void ov::npuw::CompiledModel::serialize(std::ostream& stream) const {// FIXME: utilize overload insteadwrite(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);}@@ -760,6 +767,13 @@ std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::deserialize(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;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.cppindex 638aa33face..b12449a02ee 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@@ -708,49 +708,106 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& m}void ov::npuw::LLMCompiledModel::export_model(std::ostream& stream) const {- LOG_INFO("Serializing LLMCompiledModel...");- LOG_BLOCK();-using namespace ov::npuw::s11n;+ // Identify encryption flow+ bool encryption_required = false;+ EncryptionCallbacks enc_callbacks;+ if (auto it = m_non_llm_props.find(ov::cache_encryption_callbacks.name());+ it != m_non_llm_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;+ }++ // Identify either full flow or weightless+ bool is_weightless = true;+ if (auto it = m_non_llm_props.find(ov::cache_mode.name());+ it != m_non_llm_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 firstwrite(stream, NPUW_SERIALIZATION_INDICATOR);-// Serialize general meta infowrite(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);- // Serialize name- write(stream, m_name);+ if (!encryption_required) {+ LLMSerializeContext ctx(false, nullptr);+ return serialize(stream, ctx);+ }- // Serialize inputs and outputs- write(stream, inputs());- write(stream, outputs());+ // 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);+ LLMSerializeContext ctx(false, 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+ LLMSerializeContext ctx(true, enc_callbacks.encrypt);+ serialize(stream, ctx);+ }+}- // Serialize LLMCompiledModel-specific data- write(stream, m_kvcache_desc.max_prompt_size);- write(stream, m_kvcache_desc.total_size);- write(stream, m_kvcache_desc.num_stored_tokens);- write(stream, m_kvcache_desc.dim);- write(stream, m_kvcache_desc.v_tensors_transposed);+void ov::npuw::LLMCompiledModel::serialize(std::ostream& stream, const ov::npuw::s11n::LLMSerializeContext& ctx) const {+ LOG_INFO("Serializing LLMCompiledModel...");+ LOG_BLOCK();- // Write config- write(stream, m_cfg);+ using namespace ov::npuw::s11n;// Identify either full flow or weightlessbool is_weightless = true;- if (m_non_llm_props.count(ov::cache_mode.name()) &&- m_non_llm_props.at(ov::cache_mode.name()).as<CacheMode>() == CacheMode::OPTIMIZE_SPEED) {+ if (auto it = m_non_llm_props.find(ov::cache_mode.name());+ it != m_non_llm_props.end() && it->second.as<CacheMode>() == CacheMode::OPTIMIZE_SPEED) {LOG_INFO("Serialization will be done via flow with weights.");is_weightless = false;}- write(stream, is_weightless);- // Serialize CompiledModels- m_kvcache_compiled->serialize(stream);- m_prefill_compiled->serialize(stream);+ auto write_model_meta = [&](std::ostream& model_stream) {+ // Serialize name+ write(model_stream, m_name);++ // Serialize inputs and outputs+ write(model_stream, inputs());+ write(model_stream, outputs());++ // Serialize LLMCompiledModel-specific data+ write(model_stream, m_kvcache_desc.max_prompt_size);+ write(model_stream, m_kvcache_desc.total_size);+ write(model_stream, m_kvcache_desc.num_stored_tokens);+ write(model_stream, m_kvcache_desc.dim);+ write(model_stream, m_kvcache_desc.v_tensors_transposed);++ // Write config+ write(model_stream, m_cfg);++ // Serialize CompiledModels+ m_kvcache_compiled->serialize(model_stream);+ m_prefill_compiled->serialize(model_stream);+ };++ std::stringstream non_encrypted_stream;+ if (ctx.encrypted) {+ NPUW_ASSERT(ctx.encrypt && "Encryption function isn't provided!");+ non_encrypted_stream.copyfmt(stream);+ write_model_meta(non_encrypted_stream);+ std::string encrypted_str = ctx.encrypt(non_encrypted_stream.str());+ write(stream, encrypted_str);+ } else {+ write_model_meta(stream);+ }// Serialize bank nameconst auto& kv_bank = m_kvcache_compiled->m_weights_bank;@@ -760,13 +817,14 @@ void ov::npuw::LLMCompiledModel::export_model(std::ostream& stream) const {if (!is_weightless) {// Serialize weights bank+ // Note: no need to encrypt weights in full flowkv_bank->serialize(stream);}LOG_INFO("Done.");}-std::shared_ptr<ov::npuw::LLMCompiledModel> ov::npuw::LLMCompiledModel::deserialize(+std::shared_ptr<ov::npuw::LLMCompiledModel> ov::npuw::LLMCompiledModel::import_model(std::istream& stream,const std::shared_ptr<const ov::IPlugin>& plugin,const ov::AnyMap& properties) {@@ -809,64 +867,130 @@ std::shared_ptr<ov::npuw::LLMCompiledModel> ov::npuw::LLMCompiledModel::deserialNPUW_SERIALIZATION_VERSION);}- // Deserialize model name first- std::string model_name;- read(stream, model_name);+ bool encrypted = false;+ read(stream, encrypted);+ bool is_weightless = true;+ read(stream, is_weightless);- // Create a dummy CompiledModel with an empty ov::Model - this will skip the constructor flow- // to continue deserialization- ov::ParameterVector parameters;- ov::NodeVector results;+ auto read_and_finalize_banks = [&](std::istream& model_stream,+ const std::shared_ptr<ov::npuw::LLMCompiledModel>& compiled) {+ // Deserialize weights bank name+ std::string bank_name;+ read(model_stream, bank_name);- read(stream, parameters);- read(stream, results);+ if (is_weightless) {+ auto bank = ov::npuw::weights::bank(bank_name, compiled->get_plugin()->get_core(), "");- auto ov_model = std::make_shared<ov::Model>(results, parameters, model_name);+ compiled->m_kvcache_compiled->m_weights_bank = bank;+ compiled->m_prefill_compiled->m_weights_bank = bank;++ compiled->m_kvcache_compiled->finalize_weights_bank();+ compiled->m_prefill_compiled->finalize_weights_bank();+ } else {+ auto bank =+ ov::npuw::weights::Bank::deserialize(model_stream, compiled->get_plugin()->get_core(), bank_name);- auto compiled = std::make_shared<ov::npuw::LLMCompiledModel>(ov_model, plugin, true);+ compiled->m_kvcache_compiled->m_weights_bank = bank;+ compiled->m_prefill_compiled->m_weights_bank = bank;- // Deserialize LLMCompiledModel-specific data- read(stream, compiled->m_kvcache_desc.max_prompt_size);- read(stream, compiled->m_kvcache_desc.total_size);- read(stream, compiled->m_kvcache_desc.num_stored_tokens);- read(stream, compiled->m_kvcache_desc.dim);- read(stream, compiled->m_kvcache_desc.v_tensors_transposed);+ compiled->m_kvcache_compiled->reconstruct_closure();+ compiled->m_prefill_compiled->reconstruct_closure();+ }+ };- // Deserialize config- read(stream, compiled->m_cfg);- compiled->implement_properties();+ if (!encrypted) {+ LLMDeserializeContext ctx(false, nullptr);+ auto compiled_model = ov::npuw::LLMCompiledModel::deserialize(stream, plugin, properties, ctx);+ NPUW_ASSERT(compiled_model && "Couldn't import NPUW compiled model!");+ read_and_finalize_banks(stream, compiled_model);+ LOG_INFO("Done.");+ return compiled_model;+ }- // Deserialize flow indicator- bool is_weightless = false;- read(stream, is_weightless);+ 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;- // Deserialize CompiledModels- compiled->m_kvcache_compiled = ov::npuw::CompiledModel::deserialize(stream, plugin, properties);- compiled->m_prefill_compiled = ov::npuw::CompiledModel::deserialize(stream, plugin, properties);+ LOG_INFO("Decryption will be done via the function provided.");- // Deserialize weights bank name- std::string bank_name;- read(stream, bank_name);+ std::shared_ptr<ov::npuw::LLMCompiledModel> compiled_model = nullptr;+ // Model is encryptedif (is_weightless) {- auto bank = ov::npuw::weights::bank(bank_name, compiled->get_plugin()->get_core(), "");+ std::string encrypted_str;+ read(stream, encrypted_str);+ std::istringstream decrypted_stream(std::move(enc_callbacks.decrypt(encrypted_str)));+ LLMDeserializeContext ctx(false, nullptr);+ compiled_model = ov::npuw::LLMCompiledModel::deserialize(decrypted_stream, plugin, properties, ctx);+ } else {+ LLMDeserializeContext ctx(true, enc_callbacks.decrypt);+ compiled_model = ov::npuw::LLMCompiledModel::deserialize(stream, plugin, properties, ctx);+ }- compiled->m_kvcache_compiled->m_weights_bank = bank;- compiled->m_prefill_compiled->m_weights_bank = bank;+ NPUW_ASSERT(compiled_model && "Couldn't import NPUW compiled model!");+ read_and_finalize_banks(stream, compiled_model);- compiled->m_kvcache_compiled->finalize_weights_bank();- compiled->m_prefill_compiled->finalize_weights_bank();- } else {- auto bank = ov::npuw::weights::Bank::deserialize(stream, compiled->get_plugin()->get_core(), bank_name);+ LOG_INFO("Done.");- compiled->m_kvcache_compiled->m_weights_bank = bank;- compiled->m_prefill_compiled->m_weights_bank = bank;+ return compiled_model;+}- compiled->m_kvcache_compiled->reconstruct_closure();- compiled->m_prefill_compiled->reconstruct_closure();+std::shared_ptr<ov::npuw::LLMCompiledModel> ov::npuw::LLMCompiledModel::deserialize(+ std::istream& stream,+ const std::shared_ptr<const ov::IPlugin>& plugin,+ const ov::AnyMap& properties,+ const ov::npuw::s11n::LLMDeserializeContext& ctx) {+ using namespace ov::npuw::s11n;++ auto read_model_meta = [&](std::istream& model_stream) {+ // Deserialize model name first+ std::string model_name;+ read(model_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(model_stream, parameters);+ read(model_stream, results);++ auto ov_model = std::make_shared<ov::Model>(results, parameters, model_name);++ auto compiled = std::make_shared<ov::npuw::LLMCompiledModel>(ov_model, plugin, true);++ // Deserialize LLMCompiledModel-specific data+ read(model_stream, compiled->m_kvcache_desc.max_prompt_size);+ read(model_stream, compiled->m_kvcache_desc.total_size);+ read(model_stream, compiled->m_kvcache_desc.num_stored_tokens);+ read(model_stream, compiled->m_kvcache_desc.dim);+ read(model_stream, compiled->m_kvcache_desc.v_tensors_transposed);++ // Deserialize config+ read(model_stream, compiled->m_cfg);+ 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);++ return compiled;+ };++ std::shared_ptr<ov::npuw::LLMCompiledModel> compiled = nullptr;+ if (ctx.encrypted) {+ std::string encrypted_string;+ read(stream, encrypted_string);+ std::istringstream decrypted_stream(std::move(ctx.decrypt(encrypted_string)));+ compiled = read_model_meta(decrypted_stream);+ } else {+ compiled = read_model_meta(stream);}- LOG_INFO("Done.");+ NPUW_ASSERT(compiled && "Couldn't create NPUW compiled model!");+return compiled;}diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.hpp b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.hppindex a15215bba57..aa9a2e8c616 100644--- a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.hpp@@ -34,9 +34,9 @@ public:LLMCompiledModel() = delete;void export_model(std::ostream& model) const override;- static std::shared_ptr<LLMCompiledModel> deserialize(std::istream& stream,- const std::shared_ptr<const ov::IPlugin>& plugin,- const ov::AnyMap& properties);+ static std::shared_ptr<LLMCompiledModel> 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;@@ -50,6 +50,12 @@ private:std::shared_ptr<ov::ISyncInferRequest> create_sync_infer_request() const override;void implement_properties();+ void serialize(std::ostream& stream, const ov::npuw::s11n::LLMSerializeContext& ctx) const;+ static std::shared_ptr<LLMCompiledModel> deserialize(std::istream& stream,+ const std::shared_ptr<const ov::IPlugin>& plugin,+ const ov::AnyMap& properties,+ const ov::npuw::s11n::LLMDeserializeContext& ctx);+std::string m_name;std::shared_ptr<::intel_npu::OptionsDesc> m_options_desc;::intel_npu::Config m_cfg;diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.hppindex 544bce67361..8514457bb5d 100644--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.hpp@@ -5,6 +5,7 @@#pragma once#include <array>+#include <functional>#include <iostream>#include <map>#include <memory>@@ -26,7 +27,7 @@ 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.1";+const constexpr char* NPUW_SERIALIZATION_VERSION = "0.2";// Forward declarationnamespace intel_npu {@@ -76,6 +77,22 @@ struct Context {const std::unordered_map<const void*, std::size_t>& const_to_offset;};+struct LLMSerializeContext {+ explicit LLMSerializeContext(bool _encrypted, std::function<std::string(const std::string&)> _encrypt)+ : encrypted(_encrypted),+ encrypt(_encrypt) {}+ bool encrypted = false;+ std::function<std::string(const std::string&)> encrypt = nullptr;+};++struct LLMDeserializeContext {+ explicit LLMDeserializeContext(bool _encrypted, std::function<std::string(const std::string&)> _decrypt)+ : encrypted(_encrypted),+ decrypt(_decrypt) {}+ bool encrypted = false;+ std::function<std::string(const std::string&)> decrypt = nullptr;+};+using Weights = std::shared_ptr<ov::SharedBuffer<std::shared_ptr<ov::MappedMemory>>>;// Specific type overloadsdiff --git a/src/plugins/intel_npu/src/plugin/src/plugin.cpp b/src/plugins/intel_npu/src/plugin/src/plugin.cppindex db26cdac165..490f08d3396 100644--- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp+++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp@@ -820,7 +820,7 @@ std::shared_ptr<ov::ICompiledModel> Plugin::import_model(std::istream& stream, cif (serialization_indicator == NPUW_SERIALIZATION_INDICATOR) {stream.seekg(-stream.tellg() + stream_start_pos, std::ios::cur);// Properties are required for ov::weights_path- return ov::npuw::LLMCompiledModel::deserialize(stream, shared_from_this(), properties);+ return ov::npuw::LLMCompiledModel::import_model(stream, shared_from_this(), properties);}stream.seekg(-stream.tellg() + stream_start_pos, std::ios::cur);