Skip to content

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

[NPU] Model marshalling: fallback to copying weights for all unhandled Constant nodes (#33068)

Razvan Apetroaie45921fac67d9
diff --git a/src/plugins/intel_npu/src/compiler_adapter/include/xml_serializer.hpp b/src/plugins/intel_npu/src/compiler_adapter/include/xml_serializer.hpp
index 263992bea22..fb6bd172b96 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/include/xml_serializer.hpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/include/xml_serializer.hpp
@@ -40,13 +40,23 @@ public:
false,
ov::element::dynamic,
false),
+ m_base_constant_writer(std::ref(constant_write_handler)),
m_weightless_constant_writer(weightless_constant_writer
? weightless_constant_writer
: std::make_shared<WeightlessWriter>(constant_write_handler)) {}
private:
+ /**
+ * @brief Toggles between the two writers.
+ */
ov::util::ConstantWriter& get_constant_write_handler() override;
+ /**
+ * @brief Overriden in order to choose which weights writer will be used based on the occurrence of the
+ * "WeightsPointerAttribute".
+ */
+ bool append_node_attributes(ov::Node& node) override;
+
std::unique_ptr<ov::util::XmlSerializer> make_visitor(pugi::xml_node& data,
const std::string& node_type_name,
ov::util::ConstantWriter& constant_write_handler,
@@ -56,7 +66,15 @@ private:
ov::element::Type,
bool) const override;
+ /**
+ * @brief The base OV writer, copies the weights in a dedicated buffer.
+ *
+ * @note Ideally, we would not require this writer at all. The current algorithm does not handle subgraphs properly,
+ * so falling back to copying a part of the weights is a temporary fix.
+ */
+ std::reference_wrapper<ov::util::ConstantWriter> m_base_constant_writer;
std::shared_ptr<WeightlessWriter> m_weightless_constant_writer = nullptr;
+ bool m_use_weightless_writer = false;
};
/**
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/xml_serializer.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/xml_serializer.cpp
index f609027f296..8024c847e11 100644
--- a/src/plugins/intel_npu/src/compiler_adapter/src/xml_serializer.cpp
+++ b/src/plugins/intel_npu/src/compiler_adapter/src/xml_serializer.cpp
@@ -10,7 +10,20 @@
namespace intel_npu {
ov::util::ConstantWriter& XmlSerializer::get_constant_write_handler() {
- return *m_weightless_constant_writer;
+ if (m_use_weightless_writer) {
+ return *m_weightless_constant_writer;
+ } else {
+ return m_base_constant_writer;
+ }
+}
+
+bool XmlSerializer::append_node_attributes(ov::Node& node) {
+ // If the "WeightsPointerAttribute" is found, then we have the metadata required to avoid copying the weights
+ // corresponding to this node.
+ m_use_weightless_writer = node.get_rt_info().count(WeightsPointerAttribute::get_type_info_static()) != 0;
+ auto result = ov::util::XmlSerializer::append_node_attributes(node);
+ m_use_weightless_writer = false;
+ return result;
}
std::unique_ptr<ov::util::XmlSerializer> XmlSerializer::make_visitor(pugi::xml_node& data,