Skip to content

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

[NPU] Model marshalling without weights copies: handle submodels as well (#33097)

Razvan Apetroaiedb52b1d5489c
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 6f01ce10fe5..4ce927461da 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
@@ -14,6 +14,7 @@
#include "intel_npu/common/filtered_config.hpp"
#include "intel_npu/config/options.hpp"
#include "intel_npu/weights_pointer_attribute.hpp"
+#include "openvino/op/util/multi_subgraph_base.hpp"
#include "openvino/pass/serialize.hpp"
#include "transformations/common_optimizations/nop_elimination.hpp"
#include "transformations/op_conversions/convert_interpolate11_downgrade.hpp"
@@ -152,32 +153,18 @@ std::string rankToLegacyLayoutString(const size_t rank) {
*/
void storeWeightsPointerAttribute(const std::shared_ptr<ov::Model>& model) {
for (auto&& node : model->get_ops()) {
- if (!ov::is_type<ov::op::v0::Constant>(node)) {
- continue;
- }
-
- auto constantNode = std::static_pointer_cast<ov::op::v0::Constant>(node);
- ov::RTMap& runtimeInfoMap = constantNode->get_rt_info();
- runtimeInfoMap[intel_npu::WeightsPointerAttribute::get_type_info_static()] =
- intel_npu::WeightsPointerAttribute(constantNode->get_data_ptr(), constantNode->get_byte_size());
- }
-}
-
-/**
- * @brief Removes the attributes stored by "storeWeightsPointerAttribute" in order to restore the model to its original
- * state.
- * @see storeWeightsPointerAttribute for details.
- */
-void removeWeightsPointerAttribute(const std::shared_ptr<ov::Model>& model) {
- for (auto&& node : model->get_ops()) {
- if (!ov::is_type<ov::op::v0::Constant>(node)) {
+ if (auto subgraphNode = ov::as_type_ptr<ov::op::util::MultiSubGraphOp>(node)) {
+ // "Models within models"
+ for (const std::shared_ptr<ov::Model>& submodel : subgraphNode->get_functions()) {
+ storeWeightsPointerAttribute(submodel);
+ }
continue;
}
- ov::RTMap& runtimeInfoMap = node->get_rt_info();
- const auto& resultIt = runtimeInfoMap.find(intel_npu::WeightsPointerAttribute::get_type_info_static());
- if (resultIt != runtimeInfoMap.end()) {
- runtimeInfoMap.erase(resultIt);
+ if (auto constantNode = ov::as_type_ptr<ov::op::v0::Constant>(node)) {
+ ov::RTMap& runtimeInfoMap = constantNode->get_rt_info();
+ runtimeInfoMap[intel_npu::WeightsPointerAttribute::get_type_info_static()] =
+ intel_npu::WeightsPointerAttribute(constantNode->get_data_ptr(), constantNode->get_byte_size());
}
}
}
@@ -469,7 +456,6 @@ SerializedIR serializeIR(const std::shared_ptr<const ov::Model>& model,
SerializedIR serializedIR =
VCLSerializerWithoutWeightsCopy(model, compilerVersion, supportedOpsetVersion).serialize();
- removeWeightsPointerAttribute(nonConstantModel);
return serializedIR;
}
return VCLSerializerWithWeightsCopy(model, compilerVersion, supportedOpsetVersion).serialize();
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 3bc1ec916a6..29207922e60 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
@@ -9,6 +9,7 @@
#include "common_test_utils/node_builders/constant.hpp"
#include "intel_npu/config/options.hpp"
#include "openvino/opsets/opset11.hpp"
+#include "openvino/pass/serialize.hpp"
#include "shared_test_classes/base/ov_behavior_test_utils.hpp"
#include "vcl_serializer.hpp"
@@ -16,6 +17,32 @@ using CompilationParams = std::tuple<std::string, // Device name
ov::AnyMap // Config
>;
+namespace {
+
+// As of writing this, the "all weights copy" solution serializes the model below using ~500KB. The "no weights copy"
+// uses ~100KB. If sizes change significantly, then investigation may be required.
+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() {
+ 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);
+
+ // 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});
+ add = std::make_shared<ov::opset11::Add>(add, add_constant);
+ }
+ auto res = std::make_shared<ov::opset11::Result>(add);
+
+ return std::make_shared<ov::Model>(ov::ResultVector{std::move(res)}, ov::ParameterVector{std::move(data)});
+}
+
+} // namespace
+
namespace ov::test::behavior {
class DriverCompilerAdapterCustomStreamTestNPU : public ov::test::behavior::OVPluginTestBase,
@@ -57,16 +84,29 @@ protected:
};
TEST_P(DriverCompilerAdapterCustomStreamTestNPU, TestLargeModelWeightsCopy) {
- auto model = createModelWithLargeSize();
+ auto model = createModelWithLargeWeights();
const ze_graph_compiler_version_info_t dummyCompilerVersion{0, 0};
- EXPECT_NO_THROW(::intel_npu::driver_compiler_utils::serializeIR(model, dummyCompilerVersion, 11, true));
+
+ ::intel_npu::SerializedIR serializedModel;
+ 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);
}
TEST_P(DriverCompilerAdapterCustomStreamTestNPU, TestLargeModelNoWeightsCopy) {
- auto model = createModelWithLargeSize();
+ auto model = createModelWithLargeWeights();
const ze_graph_compiler_version_info_t dummyCompilerVersion{0, 0};
- EXPECT_NO_THROW(::intel_npu::driver_compiler_utils::serializeIR(model, dummyCompilerVersion, 11, false));
+ ::intel_npu::SerializedIR serializedModel;
+ 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);
+
+ ov::pass::StreamSerialize::DataHeader dataHeader;
+ memcpy(&dataHeader, serializedModel.second.get(), sizeof(dataHeader));
+ ASSERT_TRUE(dataHeader.consts_size == 0);
}
const std::vector<ov::AnyMap> configs = {
diff --git a/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/compile_and_infer.cpp b/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/compile_and_infer.cpp
index c3705a13411..fd8c7e0075d 100644
--- a/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/compile_and_infer.cpp
+++ b/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/compile_and_infer.cpp
@@ -32,7 +32,7 @@ INSTANTIATE_TEST_SUITE_P(compatibility_smoke_BehaviorTests,
INSTANTIATE_TEST_SUITE_P(compatibility_smoke_BehaviorTests,
OVCompileAndInferRequestSerializers,
- ::testing::Combine(::testing::Values(getConstantGraph(ov::element::f32)),
+ ::testing::Combine(::testing::Values(createModelContainingSubgraph()),
::testing::Values(ov::test::utils::DEVICE_NPU),
::testing::ValuesIn(std::vector<ov::AnyMap>{
{ov::intel_npu::use_base_model_serializer(true)},
diff --git a/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/compile_and_infer.hpp b/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/compile_and_infer.hpp
index b70e47efc27..fed424bd6c1 100644
--- a/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/compile_and_infer.hpp
+++ b/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/compile_and_infer.hpp
@@ -35,6 +35,41 @@ inline std::shared_ptr<ov::Model> getConstantGraph(element::Type type) {
return std::make_shared<Model>(results, params);
}
+inline std::shared_ptr<ov::Model> createSimpleModel() {
+ auto data = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1});
+ auto add_constant = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{1}, {1});
+ auto add = std::make_shared<ov::op::v1::Add>(data, add_constant);
+
+ return std::make_shared<ov::Model>(ov::OutputVector{add}, ov::ParameterVector{data});
+}
+
+inline std::shared_ptr<ov::Model> createModelContainingSubgraph() {
+ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1});
+ input->get_output_tensor(0).set_names({"tensor_input"});
+
+ // Simple subgraph that adds 1 to the input
+ std::shared_ptr<ov::Model> body = createSimpleModel();
+ auto tensor_iterator = std::make_shared<ov::op::v0::TensorIterator>();
+ tensor_iterator->set_body(body);
+ tensor_iterator->set_sliced_input(
+ std::dynamic_pointer_cast<ov::op::v0::Parameter>(body->input().get_node_shared_ptr()),
+ input,
+ 0,
+ 1,
+ 1,
+ -1,
+ 0);
+
+ // Outside the subgraph, add 1 to the output
+ auto add_constant = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{1}, {1});
+ auto add = std::make_shared<ov::op::v1::Add>(tensor_iterator->get_iter_value(body->output(), -1), add_constant);
+
+ auto result = std::make_shared<ov::op::v0::Result>(add);
+ result->get_output_tensor(0).set_names({"tensor_output"});
+
+ return std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{input});
+}
+
inline bool isCommandQueueExtSupported() {
return std::make_shared<::intel_npu::ZeroInitStructsHolder>()->getCommandQueueDdiTable().version() > 0;
}
@@ -249,10 +284,13 @@ TEST_P(OVCompileAndInferRequestSerializers, AccurateResults) {
execNet = core->compile_model(function, target_device, configuration);
ov::InferRequest inference_request;
OV_ASSERT_NO_THROW(inference_request = execNet.create_infer_request());
+
+ const ov::Tensor input = utils::create_tensor(ov::element::f32, ov::Shape{1}, std::vector<float>{1.0f});
+ inference_request.set_input_tensor(input);
OV_ASSERT_NO_THROW(inference_request.infer());
const ov::Tensor output = inference_request.get_tensor("tensor_output");
- const ov::Tensor expected = utils::create_tensor(ov::element::f32, ov::Shape{1}, std::vector<float>{2.0f});
+ const ov::Tensor expected = utils::create_tensor(ov::element::f32, ov::Shape{1}, std::vector<float>{3.0f});
OV_ASSERT_NO_THROW(utils::compare(expected, output));
} catch (const ov::Exception& exception) {
ASSERT_STR_CONTAINS(