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
[NPU] Extract compilation mode configuration logic (#37722)
Dan Liu99030810b3d2
diff --git a/src/plugins/intel_npu/src/plugin/include/transformations.hpp b/src/plugins/intel_npu/src/plugin/include/transformations.hppindex 6349b89389e..79a1ff0a5ff 100644--- a/src/plugins/intel_npu/src/plugin/include/transformations.hpp+++ b/src/plugins/intel_npu/src/plugin/include/transformations.hpp@@ -7,8 +7,6 @@#include <functional>#include <memory>#include <optional>-#include <set>-#include <string>#include "intel_npu/config/config.hpp"#include "intel_npu/config/options.hpp"@@ -17,6 +15,22 @@#include "openvino/runtime/intel_npu/properties.hpp"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.+ * @param model The OpenVINO model to inspect+ * @param compilerType The resolved compiler type+ * @param compilationModeSet Whether NPU_COMPILATION_MODE was already provided+ * @param dynamicShapeToStatic Whether the dynamic-shape-to-static conversion is requested+ * @return true if 'HostCompile_Interpreter' should be selected, false otherwise+ */+bool should_use_host_compile_interpreter(const std::shared_ptr<const ov::Model>& model,+ ov::intel_npu::CompilerType compilerType,+ bool compilationModeSet,+ bool dynamicShapeToStatic);+namespace batch_helpers {/**diff --git a/src/plugins/intel_npu/src/plugin/src/plugin.cpp b/src/plugins/intel_npu/src/plugin/src/plugin.cppindex fd5374c2126..0dd6cb61111 100644--- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp+++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp@@ -291,43 +291,13 @@ std::shared_ptr<ov::ICompiledModel> Plugin::compile_model(const std::shared_ptr<localConfig.update(ov::intel_npu::compiler_version.name(), compiler->get_version());// Resolve HostCompile before batching so the selected mode controls subsequent model and batch handling.- if (compilerType == ov::intel_npu::CompilerType::PLUGIN && !localConfig.has<COMPILATION_MODE>() &&- !localConfig.get<DYNAMIC_SHAPE_TO_STATIC>()) {- // 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();- });- };-- // 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);- };-- 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 (inputsDynamic && outputsDynamic && allPortsHaveFiniteUpperBounds) {- _logger.info("NPU_COMPILATION_MODE not set; selecting 'HostCompile_Interpreter' "- "for fully-dynamic model (inputs and outputs both dynamic)");- localConfig.update(ov::intel_npu::compilation_mode.name(), "HostCompile_Interpreter");- }+ if (should_use_host_compile_interpreter(model,+ 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)");+ localConfig.update(ov::intel_npu::compilation_mode.name(), "HostCompile_Interpreter");}// Read the default or explicit compilation mode so automatic and user-selected HostCompile take the same path.diff --git a/src/plugins/intel_npu/src/plugin/src/transformations.cpp b/src/plugins/intel_npu/src/plugin/src/transformations.cppindex e8b45172dcf..e0b82dd00b2 100644--- a/src/plugins/intel_npu/src/plugin/src/transformations.cpp+++ b/src/plugins/intel_npu/src/plugin/src/transformations.cpp@@ -4,6 +4,7 @@#include "transformations.hpp"+#include <algorithm>#include <map>#include <sstream>@@ -13,6 +14,48 @@#include "openvino/op/result.hpp"namespace intel_npu {++bool should_use_host_compile_interpreter(const std::shared_ptr<const ov::Model>& model,+ ov::intel_npu::CompilerType compilerType,+ bool compilationModeSet,+ bool dynamicShapeToStatic) {+ if (compilerType != ov::intel_npu::CompilerType::PLUGIN || compilationModeSet || dynamicShapeToStatic) {+ 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();+ });+ };++ // 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);+ };++ 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);++ return inputsDynamic && outputsDynamic && allPortsHaveFiniteUpperBounds;+}+namespace batch_helpers {bool hasOtherDynamicDims(const ov::PartialShape& shape) {diff --git a/src/plugins/intel_npu/tests/unit/CMakeLists.txt b/src/plugins/intel_npu/tests/unit/CMakeLists.txtindex 9be62e6123e..787b5495e3b 100644--- a/src/plugins/intel_npu/tests/unit/CMakeLists.txt+++ b/src/plugins/intel_npu/tests/unit/CMakeLists.txt@@ -47,6 +47,7 @@ ov_add_test_target(${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/src/blob_format_importers.cpp${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/src/blob_source.cpp${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/src/compiler_option_support_helper.cpp+ ${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/src/transformations.cpp# npuw core${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/compiled_model.cpp${OpenVINO_SOURCE_DIR}/src/plugins/intel_npu/src/plugin/npuw/flux2_compiled_model.cpp@@ -169,6 +170,7 @@ install(TARGETS ${TARGET_NAME}target_sources(${TARGET_NAME} PRIVATE${CMAKE_CURRENT_SOURCE_DIR}/npu/executor_test.cpp+ ${CMAKE_CURRENT_SOURCE_DIR}/npu/should_use_host_compile_interpreter_test.cpp${CMAKE_CURRENT_SOURCE_DIR}/npu/compiler_option_support_helper_test.cpp${CMAKE_CURRENT_SOURCE_DIR}/npu/option_support_cache_test.cpp${CMAKE_CURRENT_SOURCE_DIR}/npu/state_tensor_binding_test.cppdiff --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.cppnew file mode 100644index 00000000000..41ff4690e5e--- /dev/null+++ b/src/plugins/intel_npu/tests/unit/npu/should_use_host_compile_interpreter_test.cpp@@ -0,0 +1,173 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include <gtest/gtest.h>++#include <memory>++#include "intel_npu/config/config.hpp"+#include "intel_npu/config/options.hpp"+#include "openvino/op/constant.hpp"+#include "openvino/op/parameter.hpp"+#include "openvino/op/relu.hpp"+#include "openvino/op/result.hpp"+#include "openvino/op/shape_of.hpp"+#include "openvino/op/slice.hpp"+#include "openvino/runtime/intel_npu/properties.hpp"+#include "transformations.hpp"++using namespace intel_npu;++namespace {++std::shared_ptr<ov::Model> make_relu_model(const ov::PartialShape& shape) {+ auto param = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, shape);+ auto relu = std::make_shared<ov::op::v0::Relu>(param);+ auto result = std::make_shared<ov::op::v0::Result>(relu);+ return std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{param}, "relu_model");+}++std::shared_ptr<ov::Model> make_dynamic_input_static_output_model(const ov::PartialShape& shape) {+ auto param = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, shape);+ auto shapeOf = std::make_shared<ov::op::v3::ShapeOf>(param);+ auto result = std::make_shared<ov::op::v0::Result>(shapeOf);+ return std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{param}, "shape_of_model");+}++std::shared_ptr<ov::Model> make_two_input_relu_model(const ov::PartialShape& shape0, const ov::PartialShape& shape1) {+ auto param0 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, shape0);+ auto relu0 = std::make_shared<ov::op::v0::Relu>(param0);+ auto result0 = std::make_shared<ov::op::v0::Result>(relu0);++ auto param1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, shape1);+ auto relu1 = std::make_shared<ov::op::v0::Relu>(param1);+ auto result1 = std::make_shared<ov::op::v0::Result>(relu1);++ return std::make_shared<ov::Model>(ov::ResultVector{result0, result1},+ ov::ParameterVector{param0, param1},+ "two_input_relu_model");+}++// Slice with a runtime (Parameter) stop keeps the static input while producing a bounded dynamic 4D output.+std::shared_ptr<ov::Model> make_static_input_dynamic_output_model(const ov::PartialShape& shape) {+ auto data = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, shape);+ auto start = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{4}, {0, 0, 0, 0});+ auto step = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{4}, {1, 1, 1, 1});+ auto stop = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{4});+ auto slice = std::make_shared<ov::op::v8::Slice>(data, start, stop, step);+ auto result = std::make_shared<ov::op::v0::Result>(slice);+ return std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{data, stop}, "slice_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);+ return std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{}, "no_input_model");+}++class ShouldUseHostCompileInterpreterTest : public ::testing::Test {+protected:+ ShouldUseHostCompileInterpreterTest() {+ auto desc = std::make_shared<OptionsDesc>();+ desc->add<COMPILER_TYPE>();+ desc->add<COMPILATION_MODE>();+ desc->add<DYNAMIC_SHAPE_TO_STATIC>();+ config = std::make_unique<Config>(desc);+ config->update({{ov::intel_npu::compiler_type.name(), "PLUGIN"}});+ }++ bool run(const std::shared_ptr<const ov::Model>& model) {+ return intel_npu::should_use_host_compile_interpreter(model,+ config->get<COMPILER_TYPE>(),+ config->has<COMPILATION_MODE>(),+ config->get<DYNAMIC_SHAPE_TO_STATIC>());+ }++ std::unique_ptr<Config> config;+};++constexpr int64_t UPPER_BOUND = 224;++ov::Dimension bounded() {+ return ov::Dimension(1, UPPER_BOUND);+}++ov::Dimension unbounded() {+ return ov::Dimension::dynamic();+}++TEST_F(ShouldUseHostCompileInterpreterTest, BoundedDynamicFourDimensionalInputAndOutputEnableHostCompile) {+ EXPECT_TRUE(run(make_relu_model({1, bounded(), 16, 32})));+}++TEST_F(ShouldUseHostCompileInterpreterTest, DynamicSpatialDimensionsEnableHostCompile) {+ EXPECT_TRUE(run(make_relu_model({1, 3, bounded(), bounded()})));+}++TEST_F(ShouldUseHostCompileInterpreterTest, NonPluginCompilerDoesNotEnableHostCompile) {+ config->update({{ov::intel_npu::compiler_type.name(), "DRIVER"}});++ EXPECT_FALSE(run(make_relu_model({1, bounded(), 16, 32})));+}++TEST_F(ShouldUseHostCompileInterpreterTest, ExplicitCompilationModeIsNotOverridden) {+ config->update({{ov::intel_npu::compilation_mode.name(), "ReferenceSW"}});++ EXPECT_FALSE(run(make_relu_model({1, bounded(), 16, 32})));+ EXPECT_EQ(config->get<COMPILATION_MODE>(), "ReferenceSW");+}++TEST_F(ShouldUseHostCompileInterpreterTest, DynamicShapeToStaticDoesNotEnableHostCompile) {+ config->update({{ov::intel_npu::dynamic_shape_to_static.name(), "YES"}});++ EXPECT_FALSE(run(make_relu_model({1, bounded(), 16, 32})));+}++TEST_F(ShouldUseHostCompileInterpreterTest, StaticModelDoesNotEnableHostCompile) {+ EXPECT_FALSE(run(make_relu_model({1, 3, 16, 32})));+}++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, DynamicBatchDoesNotEnableHostCompile) {+ 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, UnboundedDimensionDoesNotEnableHostCompile) {+ EXPECT_FALSE(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, StaticInputDynamicOutputDoesNotEnableHostCompile) {+ EXPECT_FALSE(run(make_static_input_dynamic_output_model({1, 3, 16, 32})));+}++TEST_F(ShouldUseHostCompileInterpreterTest, UnboundedAdditionalPortDoesNotEnableHostCompile) {+ const auto model = make_two_input_relu_model({1, bounded(), 16, 32}, {1, unbounded(), 16, 32});++ EXPECT_FALSE(run(model));+}++TEST_F(ShouldUseHostCompileInterpreterTest, MultipleDynamicOutputsEnableHostCompile) {+ EXPECT_TRUE(run(make_two_input_relu_model({1, bounded(), 16, 32}, {1, 3, bounded(), bounded()})));+}++TEST_F(ShouldUseHostCompileInterpreterTest, NoInputModelDoesNotEnableHostCompile) {+ EXPECT_FALSE(run(make_no_input_model()));+}++} // namespace