Skip to content

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

[NPU] Select HostCompile_Interpreter mode for models with dynamic I/O (#38573)

Maxim Kurin0774c3e5ad01
diff --git a/src/plugins/intel_npu/src/plugin/include/transformations.hpp b/src/plugins/intel_npu/src/plugin/include/transformations.hpp
index 79a1ff0a5ff..66e333c9872 100644
--- a/src/plugins/intel_npu/src/plugin/include/transformations.hpp
+++ b/src/plugins/intel_npu/src/plugin/include/transformations.hpp
@@ -17,9 +17,11 @@
namespace intel_npu {
/**
- * @brief Decides whether the bounded dynamic 4D I/O model should be compiled with the
- * 'HostCompile_Interpreter' compilation mode. This is a pure predicate: it does not read from or
- * mutate any Config. The caller is responsible for updating the config when this returns true.
+ * @brief Decides whether a model with at least one dynamic non-batch dimension (bounded or unbounded) in its inputs
+ * should be compiled with the 'HostCompile_Interpreter' compilation mode. Only the inputs are inspected, since the
+ * output shapes of some models cannot be inferred. Models whose only dynamic input dimension is the batch, and models
+ * with any dynamic-rank input, are rejected. This is a pure predicate: it does not read from or mutate any Config. The
+ * caller is responsible for updating the config when this returns true.
* @param model The OpenVINO model to inspect
* @param compilerType The resolved compiler type
* @param compilationModeSet Whether NPU_COMPILATION_MODE was already provided
diff --git a/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp b/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp
index c89b2f4bfd5..6647c191443 100644
--- a/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/src/compiled_model.cpp
@@ -176,10 +176,18 @@ std::shared_ptr<const ov::Model> CompiledModel::get_runtime_model() const {
std::dynamic_pointer_cast<const ov::op::v0::Result>(nodeOutput.get_node_shared_ptr());
// A dummy node is required for constructing and populating the Result node. A Constant one is perhaps the
- // most fitting choice here.
+ // most fitting choice here. A dimension without an upper bound has no size to allocate, so it is left
+ // empty.
+ ov::Shape constantShape;
+ const auto& partialShape = nodeOutput.get_partial_shape();
+ if (partialShape.rank().is_static()) {
+ for (const ov::Dimension& dimension : partialShape) {
+ constantShape.push_back(dimension.get_interval().has_upper_bound() ? dimension.get_max_length()
+ : 0);
+ }
+ }
std::shared_ptr<ov::Node> constantDummy =
- std::make_shared<ov::op::v0::Constant>(nodeOutput.get_element_type(),
- nodeOutput.get_partial_shape().get_max_shape());
+ std::make_shared<ov::op::v0::Constant>(nodeOutput.get_element_type(), constantShape);
// Attached to the Result node as output tensor in order to provide the correct tensor names. Additionally,
// the dummy Constant node could use only static shapes. If the shape is dynamic, this construct can provide
// the correct shape to the Result node.
diff --git a/src/plugins/intel_npu/src/plugin/src/plugin.cpp b/src/plugins/intel_npu/src/plugin/src/plugin.cpp
index 7ff83c19d5a..c85da2a6d8d 100644
--- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp
+++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp
@@ -295,8 +295,9 @@ std::shared_ptr<ov::ICompiledModel> Plugin::compile_model(const std::shared_ptr<
compilerType,
localConfig.has<COMPILATION_MODE>(),
localConfig.get<DYNAMIC_SHAPE_TO_STATIC>())) {
- _logger.info("NPU_COMPILATION_MODE not set; selecting 'HostCompile_Interpreter' for bounded dynamic 4D I/O "
- "model (inputs and outputs both dynamic, static batch, other dimensions dynamic)");
+ _logger.info(
+ "NPU_COMPILATION_MODE not set; selecting 'HostCompile_Interpreter' "
+ "for dynamic model (at least one input has a dynamic non-batch dimension, all input ranks static)");
localConfig.update(ov::intel_npu::compilation_mode.name(), "HostCompile_Interpreter");
}
diff --git a/src/plugins/intel_npu/src/plugin/src/transformations.cpp b/src/plugins/intel_npu/src/plugin/src/transformations.cpp
index e0b82dd00b2..008d038509a 100644
--- a/src/plugins/intel_npu/src/plugin/src/transformations.cpp
+++ b/src/plugins/intel_npu/src/plugin/src/transformations.cpp
@@ -23,37 +23,26 @@ bool should_use_host_compile_interpreter(const std::shared_ptr<const ov::Model>&
return false;
}
- // HostCompile allocates dynamic buffers from I/O upper bounds, so every dynamic dimension must be bounded.
- const auto hasFiniteUpperBounds = [](const auto& port) {
- const auto& shape = port.get_partial_shape();
- const auto rank = shape.rank();
- return rank.is_static() && std::all_of(shape.begin(), shape.end(), [](const ov::Dimension& dimension) {
- return dimension.get_interval().has_upper_bound();
- });
+ // HostCompile supports dynamic dimensions only; a single dynamic-rank input rules the model out.
+ const auto hasStaticRank = [](const auto& port) {
+ return port.get_partial_shape().rank().is_static();
};
- // Detect a bounded dynamic 4D I/O port that makes the model a HostCompile candidate.
- const auto isDynamicHostCompilePort = [&hasFiniteUpperBounds](const auto& port) {
- const auto& shape = port.get_partial_shape();
- const auto rank = shape.rank();
- // Keep batch static to avoid failures in ConvertBatchedLayerTo1N and AdjustScaleShiftForDWConv, because reshape
- // operations in these passes do not support dynamic batch shapes.
- return shape.is_dynamic() && rank.is_static() && rank.get_length() == 4 && shape[0].is_static() &&
- hasFiniteUpperBounds(port);
+ // Any dynamic dimension other than batch, bounded or unbounded, makes the model a HostCompile model: HostCompile
+ // sizes dynamic buffers at run time from the bound tensor or the predicted output shape. A dynamic batch alone is
+ // not supported well by HostCompile yet, so such models are left to plugin batching.
+ const auto isDynamicPort = [](const auto& port) {
+ return batch_helpers::hasOtherDynamicDims(port.get_partial_shape());
};
+ // Only the inputs decide: shape inference does not reach the outputs of some models, which then look fully
+ // dynamic even though the model is a HostCompile one.
const auto& modelInputs = model->inputs();
- const auto& modelOutputs = model->outputs();
- const bool inputsDynamic = std::any_of(modelInputs.begin(), modelInputs.end(), isDynamicHostCompilePort);
- const bool outputsDynamic = std::any_of(modelOutputs.begin(), modelOutputs.end(), isDynamicHostCompilePort);
-
- // Candidate detection above uses any_of; validate every I/O separately because one unrelated unbounded port
- // still prevents HostCompile from allocating all dynamic buffers.
- const bool allPortsHaveFiniteUpperBounds =
- std::all_of(modelInputs.begin(), modelInputs.end(), hasFiniteUpperBounds) &&
- std::all_of(modelOutputs.begin(), modelOutputs.end(), hasFiniteUpperBounds);
+ if (!std::all_of(modelInputs.begin(), modelInputs.end(), hasStaticRank)) {
+ return false;
+ }
- return inputsDynamic && outputsDynamic && allPortsHaveFiniteUpperBounds;
+ return std::any_of(modelInputs.begin(), modelInputs.end(), isDynamicPort);
}
namespace batch_helpers {
diff --git a/src/plugins/intel_npu/tests/unit/npu/should_use_host_compile_interpreter_test.cpp b/src/plugins/intel_npu/tests/unit/npu/should_use_host_compile_interpreter_test.cpp
index 41ff4690e5e..08b60e49748 100644
--- a/src/plugins/intel_npu/tests/unit/npu/should_use_host_compile_interpreter_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npu/should_use_host_compile_interpreter_test.cpp
@@ -11,6 +11,7 @@
#include "openvino/op/constant.hpp"
#include "openvino/op/parameter.hpp"
#include "openvino/op/relu.hpp"
+#include "openvino/op/reshape.hpp"
#include "openvino/op/result.hpp"
#include "openvino/op/shape_of.hpp"
#include "openvino/op/slice.hpp"
@@ -60,6 +61,16 @@ std::shared_ptr<ov::Model> make_static_input_dynamic_output_model(const ov::Part
return std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{data, stop}, "slice_model");
}
+// Reshape to a runtime pattern of unknown length leaves the output rank dynamic.
+std::shared_ptr<ov::Model> make_dynamic_rank_output_model(const ov::PartialShape& shape) {
+ auto data = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, shape);
+ auto pattern =
+ std::make_shared<ov::op::v0::Parameter>(ov::element::i64, ov::PartialShape{ov::Dimension::dynamic()});
+ auto reshape = std::make_shared<ov::op::v1::Reshape>(data, pattern, false);
+ auto result = std::make_shared<ov::op::v0::Result>(reshape);
+ return std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{data, pattern}, "reshape_model");
+}
+
std::shared_ptr<ov::Model> make_no_input_model() {
auto constant = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{1, 3, 16, 32}, {0.0f});
auto result = std::make_shared<ov::op::v0::Result>(constant);
@@ -132,34 +143,53 @@ TEST_F(ShouldUseHostCompileInterpreterTest, DynamicRankDoesNotEnableHostCompile)
EXPECT_FALSE(run(make_relu_model(ov::PartialShape::dynamic())));
}
-TEST_F(ShouldUseHostCompileInterpreterTest, NonFourDimensionalModelDoesNotEnableHostCompile) {
- EXPECT_FALSE(run(make_relu_model({1, bounded(), 16})));
+TEST_F(ShouldUseHostCompileInterpreterTest, DynamicRankAdditionalPortDoesNotEnableHostCompile) {
+ EXPECT_FALSE(run(make_two_input_relu_model({1, bounded(), 16, 32}, ov::PartialShape::dynamic())));
}
-TEST_F(ShouldUseHostCompileInterpreterTest, DynamicBatchDoesNotEnableHostCompile) {
+TEST_F(ShouldUseHostCompileInterpreterTest, NonFourDimensionalModelEnablesHostCompile) {
+ EXPECT_TRUE(run(make_relu_model({1, bounded(), 16})));
+}
+
+TEST_F(ShouldUseHostCompileInterpreterTest, DynamicBatchOnlyDoesNotEnableHostCompile) {
EXPECT_FALSE(run(make_relu_model({bounded(), 3, 16, 32})));
}
-TEST_F(ShouldUseHostCompileInterpreterTest, DynamicBatchWithDynamicSpatialDoesNotEnableHostCompile) {
- EXPECT_FALSE(run(make_relu_model({bounded(), 3, bounded(), 32})));
+TEST_F(ShouldUseHostCompileInterpreterTest, UnboundedBatchOnlyDoesNotEnableHostCompile) {
+ EXPECT_FALSE(run(make_relu_model({unbounded(), 3, 16, 32})));
+}
+
+TEST_F(ShouldUseHostCompileInterpreterTest, DynamicBatchOnlyOnEveryPortDoesNotEnableHostCompile) {
+ EXPECT_FALSE(run(make_two_input_relu_model({bounded(), 3, 16, 32}, {unbounded(), 3, 16, 32})));
+}
+
+TEST_F(ShouldUseHostCompileInterpreterTest, DynamicBatchWithDynamicSpatialEnablesHostCompile) {
+ EXPECT_TRUE(run(make_relu_model({bounded(), 3, bounded(), 32})));
}
-TEST_F(ShouldUseHostCompileInterpreterTest, UnboundedDimensionDoesNotEnableHostCompile) {
- EXPECT_FALSE(run(make_relu_model({1, unbounded(), 16, 32})));
+TEST_F(ShouldUseHostCompileInterpreterTest, UnboundedDimensionEnablesHostCompile) {
+ EXPECT_TRUE(run(make_relu_model({1, unbounded(), 16, 32})));
}
-TEST_F(ShouldUseHostCompileInterpreterTest, StaticOutputDoesNotEnableHostCompile) {
- EXPECT_FALSE(run(make_dynamic_input_static_output_model({1, bounded(), 16, 32})));
+TEST_F(ShouldUseHostCompileInterpreterTest, StaticOutputEnablesHostCompile) {
+ EXPECT_TRUE(run(make_dynamic_input_static_output_model({1, bounded(), 16, 32})));
}
TEST_F(ShouldUseHostCompileInterpreterTest, StaticInputDynamicOutputDoesNotEnableHostCompile) {
EXPECT_FALSE(run(make_static_input_dynamic_output_model({1, 3, 16, 32})));
}
-TEST_F(ShouldUseHostCompileInterpreterTest, UnboundedAdditionalPortDoesNotEnableHostCompile) {
+TEST_F(ShouldUseHostCompileInterpreterTest, DynamicRankOutputEnablesHostCompile) {
+ const auto model = make_dynamic_rank_output_model({1, bounded(), 16, 32});
+ ASSERT_TRUE(model->output(0).get_partial_shape().rank().is_dynamic());
+
+ EXPECT_TRUE(run(model));
+}
+
+TEST_F(ShouldUseHostCompileInterpreterTest, UnboundedAdditionalPortEnablesHostCompile) {
const auto model = make_two_input_relu_model({1, bounded(), 16, 32}, {1, unbounded(), 16, 32});
- EXPECT_FALSE(run(model));
+ EXPECT_TRUE(run(model));
}
TEST_F(ShouldUseHostCompileInterpreterTest, MultipleDynamicOutputsEnableHostCompile) {