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: Fix BF16 serialization for E2E LLM Case (#35817)
Dmitry Matveev8f15986565b9
diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cppindex d1ecd675b12..8aa35535b2c 100644--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp@@ -1049,10 +1049,10 @@ void ov::npuw::CompiledModel::serialize_orc(std::ostream& stream) const {serialize_orc_container(stream, true, get_encrypt_callback(m_non_npuw_props));}-void ov::npuw::CompiledModel::serialize_orc_container(- std::ostream& stream,- bool include_weights_bank,- const std::function<std::string(const std::string&)>& encrypt) const {+void ov::npuw::CompiledModel::serialize_orc_container(std::ostream& stream,+ bool include_weights_bank,+ const std::function<std::string(const std::string&)>& encrypt,+ const ov::npuw::s11n::BF16Cache* bf16_consts) const {using namespace ov::npuw;if (m_cfg.get<::intel_npu::NPUW_ENSURE_COMPATIBILITY>()) {@@ -1065,6 +1065,11 @@ void ov::npuw::CompiledModel::serialize_orc_container(bool is_weightless = should_use_weightless_flow(m_non_npuw_props, m_cfg, m_const_to_offset);LOG_INFO("Serialization will be done via " << (is_weightless ? "weightless" : "flow with weights") << ".");+ // For top-level CompiledModel export we serialize this model's own BF16 cache.+ // For nested export (e.g. inside LLMCompiledModel) the parent may provide a+ // cache collected before graph splitting / BF16->FP16 conversion, and that+ // propagated view must win so weightless import can reconstruct tensors correctly.+ const auto& bf16_cache = bf16_consts != nullptr ? *bf16_consts : m_bf16_consts;ov::AnyMap serializable_props = m_non_npuw_props;serializable_props.erase(ov::cache_encryption_callbacks.name());@@ -1108,7 +1113,10 @@ void ov::npuw::CompiledModel::serialize_orc_container(meta_stream& const_cast<::intel_npu::Config&>(m_cfg);meta_stream & serializable_props;meta_stream & is_weightless;- meta_stream& const_cast<ov::npuw::s11n::BF16Cache&>(m_bf16_consts);+ // Persist the BF16 interpretation map that weightless import later feeds+ // into LazyTensor::read_weight() when it decides whether raw bytes should+ // be read as FP16 directly or converted from BF16 source storage.+ meta_stream& const_cast<ov::npuw::s11n::BF16Cache&>(bf16_cache);if (encrypt) {std::stringstream payload_stream(std::ios::in | std::ios::out | std::ios::binary);write_children(payload_stream);@@ -1267,9 +1275,13 @@ void ov::npuw::CompiledModel::serialize(std::ostream& stream, const ov::npuw::s1if (enc_ctx.encrypted) {NPUW_ASSERT(enc_ctx.encrypt && "Encryption function isn't provided!");}+ // Preserve the caller-provided BF16 cache for nested serialization.+ // LLMCompiledModel relies on this to pass original-model BF16 metadata down+ // into child CompiledModel blobs.serialize_orc_container(stream,false,- enc_ctx.encrypted ? enc_ctx.encrypt : std::function<std::string(const std::string&)>{});+ enc_ctx.encrypted ? enc_ctx.encrypt : std::function<std::string(const std::string&)>{},+ &enc_ctx.bf16_consts);}std::shared_ptr<ov::npuw::CompiledModel> ov::npuw::CompiledModel::deserialize(diff --git a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hppindex 174d35ce4d1..3a75490d986 100644--- a/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp+++ b/src/plugins/intel_npu/src/plugin/npuw/compiled_model.hpp@@ -169,7 +169,11 @@ private:const std::function<std::string(const std::string&)>& decrypt);void serialize_orc_container(std::ostream& stream,bool include_weights_bank,- const std::function<std::string(const std::string&)>& encrypt) const;+ const std::function<std::string(const std::string&)>& encrypt,+ // Nested serializers may need to preserve BF16 metadata+ // collected by a parent model (e.g. LLMCompiledModel)+ // rather than this object's local snapshot.+ const ov::npuw::s11n::BF16Cache* bf16_consts = nullptr) const;void ensure_phase0_compatibility() const;// This is used for removing too long output tensor names to fix some compilation issuesdiff --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 81384ff7e46..295510f63f1 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@@ -1257,6 +1257,9 @@ void ov::npuw::LLMCompiledModel::serialize(std::ostream& raw_stream, const ov::n// Serialize CompiledModels// Note: no need to pass any encryption here as it's done in export_model()+ // This cache is collected on the original LLM graph before BF16->FP16+ // conversion and submodel splitting. Child CompiledModels must serialize+ // this propagated view, not just their local post-transform snapshot.CompiledContext enc_ctx(false, nullptr, nullptr, m_bf16_consts);// Serialize all generate variants