Skip to content

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

[Plugin batch] Avoid unconditional model clone in plugin batching path to reduce memory usage (#34531)

Daria Mityaginaef938ca1ba70
diff --git a/src/plugins/intel_npu/src/plugin/src/transformations.cpp b/src/plugins/intel_npu/src/plugin/src/transformations.cpp
index 1ee096c1747..f776f25af67 100644
--- a/src/plugins/intel_npu/src/plugin/src/transformations.cpp
+++ b/src/plugins/intel_npu/src/plugin/src/transformations.cpp
@@ -189,7 +189,10 @@ std::tuple<std::shared_ptr<ov::Model>, bool> handlePluginBatching(
const std::function<void(ov::intel_npu::BatchMode)>& updateBatchMode,
std::optional<ov::Dimension>& originalBatch,
Logger logger) {
- auto reshapedModel = model->clone();
+ auto originalModel = std::const_pointer_cast<ov::Model>(model);
+ // Keep the original model for all no-op/early-return paths.
+ // A mutable clone is created only when plugin batching is actually about to be applied.
+ auto reshapedModel = originalModel;
auto successfullyDebatched = false;
auto batchModeIsAvailable = localConfig.isAvailable(ov::intel_npu::batch_mode.name());
@@ -208,7 +211,7 @@ std::tuple<std::shared_ptr<ov::Model>, bool> handlePluginBatching(
}
try {
- const auto pluginBatchingIsSupported = validateModelBatch(reshapedModel, logger);
+ const auto pluginBatchingIsSupported = validateModelBatch(model, logger);
if (!pluginBatchingIsSupported) {
if (batchModeIsAvailable && batchMode == ov::intel_npu::BatchMode::AUTO) {
@@ -219,6 +222,8 @@ std::tuple<std::shared_ptr<ov::Model>, bool> handlePluginBatching(
}
logger.info("Attempting to handle batching on the plugin side.");
+ // Clone right before mutation to avoid extra memory usage when batching is skipped.
+ reshapedModel = model->clone();
try {
originalBatch = ov::get_batch(reshapedModel);
@@ -241,6 +246,8 @@ std::tuple<std::shared_ptr<ov::Model>, bool> handlePluginBatching(
updateBatchMode(ov::intel_npu::BatchMode::COMPILER);
}
} catch (const std::exception& ex) {
+ // If plugin-side transformation failed, keep the original model and drop the clone
+ reshapedModel = originalModel;
if (batchMode == ov::intel_npu::BatchMode::AUTO) {
logger.info("Couldn't validate and reshape the model. Batching will be handled by compiler. Error: %s",
ex.what());