Skip to content

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

[NPUW]Enable SWA for Gemma4-E2B/E4B by default. (#38303)

intelgaoxiong8f34dbfe204a
diff --git a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
index 702780f8e31..d1d25bcb7fa 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/llm_compiled_model.cpp
@@ -988,6 +988,27 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& m
ov::npuw::DetectAttentionMask().run_on_model(kvcache_model);
ov::npuw::log_detected_masks(kvcache_model);
+ const bool is_per_layer_inputs_model = has_per_layer_inputs(kvcache_model);
+ // Gemma-4 E2B/E4B cross-group KV sharing models benefit the most from shrinking the SWA KV
+ // cache and from hoisting the LM-head output slice through the SWA/Global boundary, so
+ // auto-enable both options for them unless the user explicitly configured it.
+ bool propagate_slice_up = m_cfg.get<::intel_npu::NPUW_LLM_PROPAGATE_SLICE_UP>();
+ if (is_per_layer_inputs_model) {
+ // SWA shrink is incompatible with prefix caching, multi-token generation (e.g. speculative
+ // decoding), and continuous prefill.
+ const bool swa_shrink_compatible =
+ !m_enable_prefix_caching && max_generation_token_len == 1 && !m_enable_continuous_prefill;
+ if (swa_shrink_compatible && !m_cfg.has<::intel_npu::NPUW_LLM_ENABLE_SWA_KV_CACHE_SHRINK>()) {
+ m_cfg.update({{"NPUW_LLM_ENABLE_SWA_KV_CACHE_SHRINK", "YES"}});
+ LOG_INFO("Gemma-4 cross-group KV model: auto-enabling NPUW_LLM_ENABLE_SWA_KV_CACHE_SHRINK");
+ }
+ if (!m_cfg.has<::intel_npu::NPUW_LLM_PROPAGATE_SLICE_UP>()) {
+ m_cfg.update({{"NPUW_LLM_PROPAGATE_SLICE_UP", "YES"}});
+ propagate_slice_up = true;
+ LOG_INFO("Gemma-4 cross-group KV model: auto-enabling NPUW_LLM_PROPAGATE_SLICE_UP");
+ }
+ }
+
// Two mutually-exclusive ways to handle sliding-window attention (SWA) layers:
// - PatchSlidingWindowMask (default): only fixes up the attention mask for correctness;
// the KV cache still keeps the full context (no memory/perf savings).
@@ -1094,22 +1115,11 @@ ov::npuw::LLMCompiledModel::LLMCompiledModel(const std::shared_ptr<ov::Model>& m
LOG_DEBUG("Encoder embedding model: skipping generate model variants (prefill-only).");
}
- const bool is_per_layer_inputs_model = has_per_layer_inputs(prefill_model);
- bool propagate_slice_up = m_cfg.get<::intel_npu::NPUW_LLM_PROPAGATE_SLICE_UP>();
-
if (lm_head_model) {
LOG_DEBUG("Shared LM head: slice the prefill output");
// KVCache model is already reshaped to [1, max_generation_token_len, embed size],
// so only apply slice to the Prefill model:
ov::npuw::SliceOutEmbeds(axes.batch, m_kvcache_desc.max_generation_token_len).run_on_model(prefill_model);
- // Gemma-4 E2B/E4B cross-group KV sharing models benefit the most from hoisting the slice
- // through the SWA/Global boundary, so auto-enable this option for them unless the user
- // explicitly configured it.
- if (is_per_layer_inputs_model && !m_cfg.has<::intel_npu::NPUW_LLM_PROPAGATE_SLICE_UP>()) {
- m_cfg.update({{"NPUW_LLM_PROPAGATE_SLICE_UP", "YES"}});
- propagate_slice_up = true;
- LOG_INFO("Gemma-4 cross-group KV model: auto-enabling NPUW_LLM_PROPAGATE_SLICE_UP");
- }
if (propagate_slice_up) {
ov::npuw::PropagateSliceUp().run_on_model(prefill_model);
}
diff --git a/src/plugins/intel_npu/tests/unit/npuw/llm_compiled_model_factory_options_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/llm_compiled_model_factory_options_test.cpp
index 4d7f7d7c5be..c91ad8f3f99 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/llm_compiled_model_factory_options_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/llm_compiled_model_factory_options_test.cpp
@@ -317,6 +317,79 @@ TEST_F(LLMCompiledModelFactoryOptionsTest, TextRerankTagKeptOutOfStageConfigs) {
expect_missing_prop(generate.props, "NPUW_TEXT_RERANK");
}
+TEST_F(LLMCompiledModelFactoryOptionsTest, PerLayerInputsModelAutoEnablesSwaShrinkAndPropagateSliceUp) {
+ RecordingFactory recorder;
+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;
+
+ ASSERT_NO_THROW(compiled =
+ create_compiled_model(ov::test::npuw::build_per_layer_inputs_probe_model(), {}, recorder));
+ ASSERT_NE(compiled, nullptr);
+
+ EXPECT_TRUE(compiled->get_property("NPUW_LLM_ENABLE_SWA_KV_CACHE_SHRINK").as<bool>());
+ EXPECT_TRUE(compiled->get_property("NPUW_LLM_PROPAGATE_SLICE_UP").as<bool>());
+}
+
+TEST_F(LLMCompiledModelFactoryOptionsTest, PerLayerInputsModelKeepsExplicitUserOverrides) {
+ RecordingFactory recorder;
+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;
+
+ ASSERT_NO_THROW(compiled = create_compiled_model(
+ ov::test::npuw::build_per_layer_inputs_probe_model(),
+ {{"NPUW_LLM_ENABLE_SWA_KV_CACHE_SHRINK", "NO"}, {"NPUW_LLM_PROPAGATE_SLICE_UP", "NO"}},
+ recorder));
+ ASSERT_NE(compiled, nullptr);
+
+ EXPECT_FALSE(compiled->get_property("NPUW_LLM_ENABLE_SWA_KV_CACHE_SHRINK").as<bool>());
+ EXPECT_FALSE(compiled->get_property("NPUW_LLM_PROPAGATE_SLICE_UP").as<bool>());
+}
+
+TEST_F(LLMCompiledModelFactoryOptionsTest, PerLayerInputsModelDoesNotOverridePrefixCaching) {
+ RecordingFactory recorder;
+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;
+
+ // Shrink must not be auto-enabled here: it is mutually exclusive with prefix caching
+ // (see the assert in the ShrinkSlidingWindowKVCache application site), so silently
+ // defaulting it to YES would turn this valid config into a compilation failure.
+ ASSERT_NO_THROW(compiled = create_compiled_model(ov::test::npuw::build_per_layer_inputs_probe_model(),
+ {{"NPUW_LLM_ENABLE_PREFIX_CACHING", "YES"}},
+ recorder));
+ ASSERT_NE(compiled, nullptr);
+
+ EXPECT_FALSE(compiled->get_property("NPUW_LLM_ENABLE_SWA_KV_CACHE_SHRINK").as<bool>());
+ EXPECT_TRUE(compiled->get_property("NPUW_LLM_PROPAGATE_SLICE_UP").as<bool>());
+}
+
+TEST_F(LLMCompiledModelFactoryOptionsTest, PerLayerInputsModelDoesNotOverrideMultiTokenGeneration) {
+ RecordingFactory recorder;
+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;
+
+ // Generating more than 1 token per inference (e.g. speculative decoding) doesn't work
+ // with a shrunk/sliding KV cache, so shrink must stay off here even though the model
+ // has consumed per_layer_inputs.
+ ASSERT_NO_THROW(compiled = create_compiled_model(ov::test::npuw::build_per_layer_inputs_probe_model(),
+ {{"NPUW_LLM_MAX_GENERATION_TOKEN_LEN", "8"}},
+ recorder));
+ ASSERT_NE(compiled, nullptr);
+
+ EXPECT_FALSE(compiled->get_property("NPUW_LLM_ENABLE_SWA_KV_CACHE_SHRINK").as<bool>());
+ EXPECT_TRUE(compiled->get_property("NPUW_LLM_PROPAGATE_SLICE_UP").as<bool>());
+}
+
+TEST_F(LLMCompiledModelFactoryOptionsTest, PerLayerInputsModelDoesNotOverrideContinuousPrefill) {
+ RecordingFactory recorder;
+ std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;
+
+ // Continuous prefill doesn't work with a shrunk/sliding KV cache either (see
+ // compute_continuous_prefill_supported()), so shrink must stay off here too.
+ ASSERT_NO_THROW(compiled = create_compiled_model(ov::test::npuw::build_per_layer_inputs_probe_model(),
+ {{"NPUW_LLM_ENABLE_CONTINUOUS_PREFILL", "YES"}},
+ recorder));
+ ASSERT_NE(compiled, nullptr);
+
+ EXPECT_FALSE(compiled->get_property("NPUW_LLM_ENABLE_SWA_KV_CACHE_SHRINK").as<bool>());
+ EXPECT_TRUE(compiled->get_property("NPUW_LLM_PROPAGATE_SLICE_UP").as<bool>());
+}
+
TEST_F(LLMCompiledModelFactoryOptionsTest, DefaultStageConfigsCarryBaselineNpuwOptions) {
RecordingFactory recorder;
std::unique_ptr<ov::npuw::LLMCompiledModel> compiled;
diff --git a/src/plugins/intel_npu/tests/unit/npuw/llm_test_helpers.hpp b/src/plugins/intel_npu/tests/unit/npuw/llm_test_helpers.hpp
index 22b7db64d18..d7d809b11ea 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/llm_test_helpers.hpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/llm_test_helpers.hpp
@@ -13,7 +13,11 @@
#include "compiled_model.hpp"
#include "llm_compiled_model.hpp"
#include "model_builder.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/constant.hpp"
#include "openvino/op/fake_convert.hpp"
+#include "openvino/op/parameter.hpp"
+#include "openvino/op/result.hpp"
#include "openvino/op/scaled_dot_product_attention.hpp"
#include "openvino/pass/stateful_to_stateless.hpp"
#include "openvino/runtime/iplugin.hpp"
@@ -247,6 +251,30 @@ inline std::shared_ptr<ov::Model> build_gemma4_moe_llm_test_model() {
return model;
}
+/// Real stateful LLM (like build_llm_test_model) plus a *consumed* per_layer_inputs
+/// parameter (non-zero, dynamic proj_dim), used to probe LLMCompiledModel's
+/// is_per_layer_inputs_model auto-enable path (Gemma-4 E2B/E4B cross-group KV sharing).
+/// A minimal standalone graph without beam_idx/KV-cache state fails
+/// StatefulToStateless (which LLMCompiledModel always runs), so this builds on the same
+/// base topology as the other test models and appends the probe input via a dedicated
+/// Add + Result, matching how build_gemma4_moe_llm_test_model appends its dangling PLE.
+inline std::shared_ptr<ov::Model> build_per_layer_inputs_probe_model() {
+ ModelBuilder mb;
+ auto model = mb.build_llm(make_test_model_config());
+
+ auto per_layer_inputs = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, -1, -1, -1});
+ per_layer_inputs->output(0).set_names({"per_layer_inputs"});
+ auto sibling = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{1, 1, 1, 1}, {0.0f});
+ auto add = std::make_shared<ov::op::v1::Add>(per_layer_inputs, sibling);
+ add->output(0).set_names({"per_layer_inputs_probe_output"});
+ auto result = std::make_shared<ov::op::v0::Result>(add);
+ result->set_friendly_name("per_layer_inputs_probe_output");
+
+ model->add_parameters({per_layer_inputs});
+ model->add_results({result});
+ return model;
+}
+
inline std::shared_ptr<ov::Model> build_sliding_window_test_model(size_t window_size = 512,
size_t sliding_to_full_ratio = 0,
const SlidingMaskFn& sliding_mask_fn = {},