Skip to content

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

[NPUW] Covered Linear Attention support with tests (#35748)

Anastasiya(Asya) Proninadfce999d5ab9
diff --git a/src/plugins/intel_npu/tests/unit/CMakeLists.txt b/src/plugins/intel_npu/tests/unit/CMakeLists.txt
index c0bd129129d..a7cefdc68cf 100644
--- a/src/plugins/intel_npu/tests/unit/CMakeLists.txt
+++ b/src/plugins/intel_npu/tests/unit/CMakeLists.txt
@@ -127,11 +127,15 @@ install(TARGETS ${TARGET_NAME}
target_sources(${TARGET_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/npuw/llm_compiled_model_graph_options_test.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/npuw/stored_tokens_state_test.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/npuw/lincache_utils_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/partitioning_options_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/npuw_options_smoke_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/orc.cpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/sdpa_pattern_nodes_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/subgraph_pipeline.cpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/subgraph_behavior_infer_test.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/npuw/pipeline_passes/add_position_ids_param_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/npuw/pipeline_passes/remove_empty_kv_inputs_test.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/npuw/pipeline_passes/stateful_to_stateless_test.cpp
)
diff --git a/src/plugins/intel_npu/tests/unit/npuw/lincache_utils_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/lincache_utils_test.cpp
new file mode 100644
index 00000000000..e0576ea7240
--- /dev/null
+++ b/src/plugins/intel_npu/tests/unit/npuw/lincache_utils_test.cpp
@@ -0,0 +1,119 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <gtest/gtest.h>
+
+#include <string>
+#include <vector>
+
+#include "util.hpp"
+
+namespace {
+// ===================== matchLinCacheString tests =====================
+
+TEST(MatchLinCacheStringTest, MatchesPastConvCache) {
+ EXPECT_TRUE(ov::npuw::util::matchLinCacheString("cache_params.past.conv.0", "past"));
+ EXPECT_TRUE(ov::npuw::util::matchLinCacheString("cache_params.past.conv.99", "past"));
+}
+
+TEST(MatchLinCacheStringTest, MatchesPresentConvCache) {
+ EXPECT_TRUE(ov::npuw::util::matchLinCacheString("cache_params.present.conv.0", "present"));
+ EXPECT_TRUE(ov::npuw::util::matchLinCacheString("cache_params.present.conv.99", "present"));
+}
+
+TEST(MatchLinCacheStringTest, MatchesPastSsmCache) {
+ EXPECT_TRUE(ov::npuw::util::matchLinCacheString("cache_params.past.ssm.0", "past"));
+ EXPECT_TRUE(ov::npuw::util::matchLinCacheString("cache_params.past.ssm.5", "past"));
+}
+
+TEST(MatchLinCacheStringTest, MatchesPresentSsmCache) {
+ EXPECT_TRUE(ov::npuw::util::matchLinCacheString("cache_params.present.ssm.0", "present"));
+ EXPECT_TRUE(ov::npuw::util::matchLinCacheString("cache_params.present.ssm.5", "present"));
+}
+
+TEST(MatchLinCacheStringTest, DoesNotMatchKVCacheNames) {
+ EXPECT_FALSE(ov::npuw::util::matchLinCacheString("past_key_values.0.key", "past"));
+ EXPECT_FALSE(ov::npuw::util::matchLinCacheString("past_key_values.0.value", "past"));
+ EXPECT_FALSE(ov::npuw::util::matchLinCacheString("present.0.key", "present"));
+ EXPECT_FALSE(ov::npuw::util::matchLinCacheString("present.0.value", "present"));
+ EXPECT_FALSE(ov::npuw::util::matchLinCacheString("cache_params.past.key.0", "past"));
+ EXPECT_FALSE(ov::npuw::util::matchLinCacheString("cache_params.past.value.0", "past"));
+ EXPECT_FALSE(ov::npuw::util::matchLinCacheString("cache_params.present.key.0", "present"));
+ EXPECT_FALSE(ov::npuw::util::matchLinCacheString("cache_params.present.value.0", "present"));
+}
+
+TEST(MatchLinCacheStringTest, PastDoesNotMatchPresentAndViceVersa) {
+ EXPECT_FALSE(ov::npuw::util::matchLinCacheString("cache_params.present.conv.0", "past"));
+ EXPECT_FALSE(ov::npuw::util::matchLinCacheString("cache_params.past.conv.0", "present"));
+}
+
+TEST(MatchLinCacheStringTest, DoesNotMatchInvalidStrings) {
+ EXPECT_FALSE(ov::npuw::util::matchLinCacheString("", "past"));
+ EXPECT_FALSE(ov::npuw::util::matchLinCacheString("cache_params.past.conv", "past")); // no index
+ EXPECT_FALSE(ov::npuw::util::matchLinCacheString("cache_params.conv.0", "past")); // missing past/present
+ EXPECT_FALSE(ov::npuw::util::matchLinCacheString("random_string", "past"));
+}
+
+TEST(MatchLinCacheStringTest, DefaultParameterIsPast) {
+ EXPECT_TRUE(ov::npuw::util::matchLinCacheString("cache_params.past.conv.0")); // default = "past"
+ EXPECT_FALSE(ov::npuw::util::matchLinCacheString("cache_params.present.conv.0")); // default = "past"
+}
+
+// ===================== starts_with_past_lincache tests =====================
+
+TEST(StartsWithPastLincacheTest, DetectsConvCacheNames) {
+ EXPECT_TRUE(ov::npuw::util::starts_with_past_lincache("cache_params.past.conv.0"));
+ EXPECT_TRUE(ov::npuw::util::starts_with_past_lincache("cache_params.past.conv.15"));
+}
+
+TEST(StartsWithPastLincacheTest, DetectsSsmCacheNames) {
+ EXPECT_TRUE(ov::npuw::util::starts_with_past_lincache("cache_params.past.ssm.0"));
+ EXPECT_TRUE(ov::npuw::util::starts_with_past_lincache("cache_params.past.ssm.7"));
+}
+
+TEST(StartsWithPastLincacheTest, KVCacheNotDetected) {
+ EXPECT_FALSE(ov::npuw::util::starts_with_past_lincache("past_key_values.0.key"));
+ EXPECT_FALSE(ov::npuw::util::starts_with_past_lincache("past_key_values.0.value"));
+}
+
+TEST(StartsWithPastLincacheTest, PresentNamesNotDetected) {
+ EXPECT_FALSE(ov::npuw::util::starts_with_past_lincache("cache_params.present.conv.0"));
+ EXPECT_FALSE(ov::npuw::util::starts_with_past_lincache("cache_params.present.ssm.0"));
+}
+
+TEST(StartsWithPastLincacheTest, NonCacheNamesNotDetected) {
+ EXPECT_FALSE(ov::npuw::util::starts_with_past_lincache("input_ids"));
+ EXPECT_FALSE(ov::npuw::util::starts_with_past_lincache("attention_mask"));
+ EXPECT_FALSE(ov::npuw::util::starts_with_past_lincache(""));
+}
+
+TEST(StartsWithPastLincacheTest, ClassifiesInputNamesCorrectly) {
+ std::vector<std::string> kvcache_names;
+ std::vector<std::string> lincache_names;
+
+ std::vector<std::string> all_inputs = {
+ "past_key_values.0.key",
+ "past_key_values.0.value",
+ "past_key_values.1.key",
+ "past_key_values.1.value",
+ "cache_params.past.conv.0",
+ "cache_params.past.conv.1",
+ "cache_params.past.ssm.0",
+ "cache_params.past.ssm.1",
+ "input_ids",
+ "attention_mask"
+ };
+
+ for (const auto& name : all_inputs) {
+ if (ov::npuw::util::starts_with(name, "past_key_values")) {
+ kvcache_names.push_back(name);
+ } else if (ov::npuw::util::starts_with_past_lincache(name)) {
+ lincache_names.push_back(name);
+ }
+ }
+
+ EXPECT_EQ(kvcache_names.size(), 4u); // 2 keys + 2 values from layer 1
+ EXPECT_EQ(lincache_names.size(), 4u); // conv.0/1, ssm.0/1
+}
+} // namespace
diff --git a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/add_position_ids_param_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/add_position_ids_param_test.cpp
new file mode 100644
index 00000000000..69cbc43778a
--- /dev/null
+++ b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/add_position_ids_param_test.cpp
@@ -0,0 +1,285 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <gtest/gtest.h>
+
+#include <queue>
+#include <set>
+#include <string>
+#include <vector>
+
+#include "npuw_transformations/add_position_ids_param.hpp"
+#include "openvino/op/clamp.hpp"
+#include "openvino/op/concat.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/convert.hpp"
+#include "openvino/op/cos.hpp"
+#include "openvino/op/less_eq.hpp"
+#include "openvino/op/matmul.hpp"
+#include "openvino/op/parameter.hpp"
+#include "openvino/op/range.hpp"
+#include "openvino/op/result.hpp"
+#include "openvino/op/sin.hpp"
+#include "openvino/op/squeeze.hpp"
+#include "openvino/op/transpose.hpp"
+#include "openvino/op/unsqueeze.hpp"
+
+namespace {
+std::shared_ptr<ov::Model> build_model_with_lfm2_like_pattern() {
+ // Range: start=0, stop=seq_len, step=1 (mimics position_ids generation)
+ auto start = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {0});
+ auto stop = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {128});
+ auto step = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {1});
+ auto range = std::make_shared<ov::op::v4::Range>(start, stop, step, ov::element::i64);
+ range->set_friendly_name("range");
+
+ // Unsqueeze: add batch dim [seq_len] → [1, seq_len]
+ auto unsqueeze_axes = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {0});
+ auto unsqueeze = std::make_shared<ov::op::v0::Unsqueeze>(range, unsqueeze_axes);
+ unsqueeze->set_friendly_name("unsqueeze_batch");
+
+ // Unsqueeze1: add feature dim [1, seq_len] → [1, 1, seq_len]
+ auto unsqueeze1_axes = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {1});
+ auto unsqueeze1 = std::make_shared<ov::op::v0::Unsqueeze>(unsqueeze, unsqueeze1_axes);
+ unsqueeze1->set_friendly_name("unsqueeze_feature");
+
+ // Convert (always present in real LFM-2 models)
+ auto convert = std::make_shared<ov::op::v0::Convert>(unsqueeze1, ov::element::f32);
+ convert->set_friendly_name("convert");
+
+ // MatMul: [inv_freq] × [positions] → freqs
+ auto inv_freq = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 8, 1});
+ inv_freq->output(0).set_names({"inv_freq"});
+ inv_freq->set_friendly_name("inv_freq");
+
+ auto matmul = std::make_shared<ov::op::v0::MatMul>(inv_freq, convert);
+ matmul->set_friendly_name("matmul_rope");
+
+ // Transpose: [1, 8, 128] → [1, 128, 8]
+ auto transpose_order = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{3}, {0, 2, 1});
+ auto transpose = std::make_shared<ov::op::v1::Transpose>(matmul, transpose_order);
+ transpose->set_friendly_name("transpose_rope");
+
+ // Concat(transpose, transpose) → simulate theta doubling
+ auto concat = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{transpose, transpose}, 2);
+ concat->set_friendly_name("concat_theta");
+
+ // Cos / Sin
+ auto cos = std::make_shared<ov::op::v0::Cos>(concat);
+ cos->set_friendly_name("cos");
+ auto sin = std::make_shared<ov::op::v0::Sin>(concat);
+ sin->set_friendly_name("sin");
+
+ auto cos_result = std::make_shared<ov::op::v0::Result>(cos);
+ cos_result->set_friendly_name("cos_result");
+ auto sin_result = std::make_shared<ov::op::v0::Result>(sin);
+ sin_result->set_friendly_name("sin_result");
+
+ ov::ResultVector results = {cos_result, sin_result};
+ ov::ParameterVector params = {inv_freq};
+
+ // Causal mask consumer:
+ // Real LFM2 path: Range -> Unsqueeze -> Unsqueeze -> Unsqueeze -> LessEqual
+ auto unsqueeze_causal_axes = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1}, {2});
+ auto unsqueeze_causal = std::make_shared<ov::op::v0::Unsqueeze>(unsqueeze1, unsqueeze_causal_axes);
+ unsqueeze_causal->set_friendly_name("unsqueeze_causal");
+
+ auto stub_k_range_as_const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{1, 1, 1}, {0});
+ auto less_equal = std::make_shared<ov::op::v1::LessEqual>(stub_k_range_as_const, unsqueeze_causal);
+ less_equal->set_friendly_name("causal_mask_less_equal");
+ auto mask_result = std::make_shared<ov::op::v0::Result>(less_equal);
+ mask_result->set_friendly_name("mask_result");
+ results.push_back(mask_result);
+
+ // Optional Clamp consumer on Range (simulates Gated Short Convolution indexing in LFM2).
+ // Real LFM2 path: Range -> Clamp -> Add -> Mod -> Unsqueeze -> ScatterNDUpdate
+ auto clamp = std::make_shared<ov::op::v0::Clamp>(range, 0, 2);
+ clamp->set_friendly_name("conv_clamp");
+ auto clamp_result = std::make_shared<ov::op::v0::Result>(clamp);
+ clamp_result->set_friendly_name("clamp_result");
+ results.push_back(clamp_result);
+
+ return std::make_shared<ov::Model>(results, params, "model_with_lfm2_like_pattern");
+}
+
+bool has_parameter_named(const std::shared_ptr<ov::Model>& model, const std::string& name) {
+ for (const auto& p : model->get_parameters()) {
+ for (const auto& n : p->output(0).get_names()) {
+ if (n == name) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+size_t count_ops_of_type(const std::shared_ptr<ov::Model>& model, const std::string& type_name) {
+ size_t count = 0;
+ for (const auto& op : model->get_ops()) {
+ if (op->get_type_name() == type_name) {
+ ++count;
+ }
+ }
+ return count;
+}
+
+// ===================== TESTS =====================
+TEST(AddPositionIdsParamTest, AddsPositionIdsParameter) {
+ auto model = build_model_with_lfm2_like_pattern();
+
+ EXPECT_FALSE(has_parameter_named(model, "position_ids"));
+ ASSERT_NO_THROW(ov::npuw::AddPositionIdsParam().run_on_model(model));
+ EXPECT_TRUE(has_parameter_named(model, "position_ids"));
+}
+
+TEST(AddPositionIdsParamTest, PositionIdsHasCorrectShapeAndType) {
+ auto model = build_model_with_lfm2_like_pattern();
+ ov::npuw::AddPositionIdsParam().run_on_model(model);
+
+ for (const auto& p : model->get_parameters()) {
+ for (const auto& n : p->output(0).get_names()) {
+ if (n == "position_ids") {
+ EXPECT_EQ(p->get_element_type(), ov::element::i64);
+ const auto& shape = p->get_partial_shape();
+ ASSERT_EQ(shape.rank().get_length(), 2);
+ EXPECT_TRUE(shape[0].is_dynamic());
+ EXPECT_TRUE(shape[1].is_dynamic());
+ return;
+ }
+ }
+ }
+ FAIL() << "position_ids parameter not found";
+}
+
+// --- Test: RoPE path uses position_ids, not Range ---
+// After the pass, the MatMul in the RoPE path should ultimately be fed by position_ids
+// (through the new Unsqueeze), not by the original Range.
+TEST(AddPositionIdsParamTest, RopePathUsesPositionIds) {
+ auto model = build_model_with_lfm2_like_pattern();
+ ov::npuw::AddPositionIdsParam().run_on_model(model);
+
+ // Find the MatMul node
+ std::shared_ptr<ov::Node> matmul_node;
+ for (const auto& op : model->get_ops()) {
+ if (op->get_type_name() == std::string("MatMul")) {
+ matmul_node = op;
+ break;
+ }
+ }
+ ASSERT_NE(matmul_node, nullptr) << "MatMul not found in model";
+
+ // Walk backwards from MatMul's input 1 to find what feeds it
+ // The chain should be: position_ids → Unsqueeze → Convert → MatMul
+ auto walk = matmul_node->input_value(1).get_node_shared_ptr();
+ bool found_position_ids = false;
+ int depth = 0;
+ while (depth < 5) { // limit walk depth
+ if (walk->get_type_name() == std::string("Parameter")) {
+ const auto& names = walk->output(0).get_names();
+ if (names.count("position_ids") > 0) {
+ found_position_ids = true;
+ }
+ break;
+ }
+ if (walk->get_input_size() == 0) {
+ break;
+ }
+ walk = walk->input_value(0).get_node_shared_ptr();
+ ++depth;
+ }
+ EXPECT_TRUE(found_position_ids) << "MatMul (RoPE path) should be fed by position_ids parameter";
+}
+
+TEST(AddPositionIdsParamTest, RangePreservedForCausalMaskConsumer) {
+ auto model = build_model_with_lfm2_like_pattern();
+ ov::npuw::AddPositionIdsParam().run_on_model(model);
+
+ // Range should still exist in the graph
+ EXPECT_GE(count_ops_of_type(model, "Range"), 1u) << "Range node should be preserved for causal mask";
+
+ // Find the LessEqual node -- Range should still feed it via the Unsqueeze chain
+ bool found_range_as_le_input = false;
+ for (const auto& op : model->get_ops()) {
+ if (op->get_type_name() == std::string("LessEqual")) {
+ // BFS from all LessEqual inputs to find Range
+ std::set<ov::Node*> visited;
+ std::queue<ov::Node*> to_visit;
+ for (size_t inp = 0; inp < op->get_input_size(); ++inp) {
+ to_visit.push(op->input_value(inp).get_node());
+ }
+ while (!to_visit.empty()) {
+ auto* n = to_visit.front();
+ to_visit.pop();
+ if (!visited.insert(n).second) {
+ continue;
+ }
+ if (std::string(n->get_type_name()) == "Range") {
+ found_range_as_le_input = true;
+ break;
+ }
+ for (size_t i = 0; i < n->get_input_size(); ++i) {
+ to_visit.push(n->input_value(i).get_node());
+ }
+ }
+ }
+ }
+ EXPECT_TRUE(found_range_as_le_input) << "Range should still feed the causal mask LessEqual";
+}
+
+TEST(AddPositionIdsParamTest, NoOpWhenPatternDoesNotMatch) {
+ // Build a model without the RoPE pattern
+ auto input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, -1, 64});
+ input->output(0).set_names({"input"});
+ auto result = std::make_shared<ov::op::v0::Result>(input);
+ auto model = std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{input}, "no_rope_model");
+
+ size_t params_before = model->get_parameters().size();
+
+ ASSERT_NO_THROW(ov::npuw::AddPositionIdsParam().run_on_model(model));
+
+ EXPECT_EQ(model->get_parameters().size(), params_before)
+ << "No new parameters should be added when pattern doesn't match";
+}
+
+// --- Test: Clamp consumer on Range gets rewired to position_ids ---
+// In real LFM2 models, Range feeds a Clamp for Gated Short Convolution indexing.
+// The pass should replace Range->Clamp with Squeeze(position_ids)->Clamp.
+TEST(AddPositionIdsParamTest, ClampInputIsReplacedToPositionIds) {
+ auto model = build_model_with_lfm2_like_pattern();
+
+ ASSERT_NO_THROW(ov::npuw::AddPositionIdsParam().run_on_model(model));
+ EXPECT_TRUE(has_parameter_named(model, "position_ids"));
+
+ // Clamp's input should now come from Squeeze(position_ids), not Range
+ for (const auto& op : model->get_ops()) {
+ if (op->get_type_name() != std::string("Clamp")) {
+ continue;
+ }
+ auto producer = op->input_value(0).get_node_shared_ptr();
+ ASSERT_EQ(std::string(producer->get_type_name()), "Squeeze")
+ << "Clamp should be fed by Squeeze(position_ids), not " << producer->get_type_name();
+
+ auto squeeze_input = producer->input_value(0).get_node_shared_ptr();
+ ASSERT_EQ(std::string(squeeze_input->get_type_name()), "Parameter");
+ EXPECT_TRUE(squeeze_input->output(0).get_names().count("position_ids") > 0)
+ << "Squeeze should be fed by position_ids parameter";
+ }
+}
+
+// Running the pass a second time must not alter the graph (no duplicate position_ids).
+TEST(AddPositionIdsParamTest, ReapplyDoesNotModifyGraph) {
+ auto model = build_model_with_lfm2_like_pattern();
+ ov::npuw::AddPositionIdsParam().run_on_model(model);
+
+ const size_t params_after_first = model->get_parameters().size();
+ const size_t ops_after_first = model->get_ops().size();
+
+ ov::npuw::AddPositionIdsParam().run_on_model(model);
+
+ EXPECT_EQ(model->get_parameters().size(), params_after_first)
+ << "Second pass should not add duplicate parameters";
+ EXPECT_EQ(model->get_ops().size(), ops_after_first)
+ << "Second pass should not add duplicate operations";
+}
+} // namespace
diff --git a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/remove_empty_kv_inputs_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/remove_empty_kv_inputs_test.cpp
index b86b1795640..14a06e056fd 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/remove_empty_kv_inputs_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/remove_empty_kv_inputs_test.cpp
@@ -171,4 +171,240 @@ TEST_F(RemoveEmptyKVInputsPassTest, SharedParam_TwoConcats_LptSubgraph_BothElimi
EXPECT_EQ(outputs[1].get_partial_shape(), ov::PartialShape({1, 4, 1, 16}));
}
+// --- Tests for the name-based filter (PR #35221 change) ---
+// The pass should only remove parameters whose name matches KV-cache naming conventions.
+// Linear cache parameters (cache_params.past.conv/ssm) must be preserved even though
+// they structurally match the Parameter->Concat pattern.
+
+// Linear cache conv parameter should NOT be removed by the pass.
+TEST_F(RemoveEmptyKVInputsPassTest, LinCacheConvParamPreserved) {
+ using namespace ov::opset13;
+
+ auto past_conv = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 2048, 0});
+ past_conv->set_friendly_name("cache_params.past.conv.0");
+ past_conv->output(0).set_names({"cache_params.past.conv.0"});
+
+ auto present_conv = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 2048, 3});
+ present_conv->set_friendly_name("cache_params.present.0.conv");
+ present_conv->output(0).set_names({"cache_params.present.0.conv"});
+
+ auto concat = std::make_shared<Concat>(ov::OutputVector{past_conv, present_conv}, 2);
+ auto result = std::make_shared<Result>(concat);
+
+ auto model = std::make_shared<ov::Model>(ov::ResultVector{result},
+ ov::ParameterVector{past_conv, present_conv},
+ "lin_cache_conv_preserved_test");
+
+ ov::npuw::RemoveEmptyKVInputs pass;
+ EXPECT_FALSE(pass.run_on_model(model)) << "Pass should not match linear cache conv parameters";
+ EXPECT_EQ(model->get_parameters().size(), 2u) << "Both parameters should remain";
+ EXPECT_EQ(count_ops<ov::op::v0::Concat>(model), 1u) << "Concat should remain";
+}
+
+// Linear cache SSM parameter should NOT be removed by the pass.
+TEST_F(RemoveEmptyKVInputsPassTest, LinCacheSsmParamPreserved) {
+ using namespace ov::opset13;
+
+ auto past_ssm = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 16, 0, 128});
+ past_ssm->set_friendly_name("cache_params.past.ssm.0");
+ past_ssm->output(0).set_names({"cache_params.past.ssm.0"});
+
+ auto present_ssm = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 16, 128, 128});
+ present_ssm->set_friendly_name("cache_params.present.0.ssm");
+ present_ssm->output(0).set_names({"cache_params.present.0.ssm"});
+
+ auto concat = std::make_shared<Concat>(ov::OutputVector{past_ssm, present_ssm}, 2);
+ auto result = std::make_shared<Result>(concat);
+
+ auto model = std::make_shared<ov::Model>(ov::ResultVector{result},
+ ov::ParameterVector{past_ssm, present_ssm},
+ "lin_cache_ssm_preserved_test");
+
+ ov::npuw::RemoveEmptyKVInputs pass;
+ EXPECT_FALSE(pass.run_on_model(model)) << "Pass should not match linear cache SSM parameters";
+ EXPECT_EQ(model->get_parameters().size(), 2u) << "Both parameters should remain";
+ EXPECT_EQ(count_ops<ov::op::v0::Concat>(model), 1u) << "Concat should remain";
+}
+
+// Plain f32 key+value pair: both should be removed.
+// Also verifies 'value' naming (existing tests only use 'key').
+TEST_F(RemoveEmptyKVInputsPassTest, PlainF32KeyValuePairBothRemoved) {
+ using namespace ov::opset13;
+
+ auto past_k = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 4, 0, 16});
+ past_k->set_friendly_name("past_key_values.0.key");
+ past_k->output(0).set_names({"past_key_values.0.key"});
+
+ auto present_k = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 4, 1, 16});
+ present_k->set_friendly_name("present.0.key");
+ present_k->output(0).set_names({"present.0.key"});
+
+ auto past_v = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 4, 0, 16});
+ past_v->set_friendly_name("past_key_values.0.value");
+ past_v->output(0).set_names({"past_key_values.0.value"});
+
+ auto present_v = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 4, 1, 16});
+ present_v->set_friendly_name("present.0.value");
+ present_v->output(0).set_names({"present.0.value"});
+
+ auto concat_k = std::make_shared<Concat>(ov::OutputVector{past_k, present_k}, 2);
+ auto concat_v = std::make_shared<Concat>(ov::OutputVector{past_v, present_v}, 2);
+
+ auto result_k = std::make_shared<Result>(concat_k);
+ result_k->output(0).set_names({"present.0.key"});
+ auto result_v = std::make_shared<Result>(concat_v);
+ result_v->output(0).set_names({"present.0.value"});
+
+ auto model = std::make_shared<ov::Model>(ov::ResultVector{result_k, result_v},
+ ov::ParameterVector{past_k, present_k, past_v, present_v},
+ "plain_f32_key_value_pair_test");
+
+ ov::npuw::RemoveEmptyKVInputs pass;
+ EXPECT_TRUE(pass.run_on_model(model));
+
+ // Both past_k and past_v removed; current_k and current_v remain
+ EXPECT_EQ(model->get_parameters().size(), 2u);
+ EXPECT_EQ(count_ops<ov::op::v0::Concat>(model), 0u);
+
+ const auto outputs = model->outputs();
+ ASSERT_EQ(outputs.size(), 2u);
+ EXPECT_EQ(outputs[0].get_partial_shape(), ov::PartialShape({1, 4, 1, 16}));
+ EXPECT_EQ(outputs[1].get_partial_shape(), ov::PartialShape({1, 4, 1, 16}));
+}
+
+// Whisper-style "input_restored." naming should be matched and removed.
+TEST_F(RemoveEmptyKVInputsPassTest, WhisperRestoredParamRemoved) {
+ using namespace ov::opset13;
+
+ const std::string var_name = "input_restored.past_key_values.0.decoder.keypresent.0.decoder.key";
+ auto past_k = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 8, 0, 64});
+ past_k->set_friendly_name(var_name);
+ past_k->output(0).set_names({var_name});
+
+ auto present_k = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 8, 1, 64});
+ present_k->set_friendly_name("present.0.key");
+ present_k->output(0).set_names({"present.0.key"});
+
+ auto concat = std::make_shared<Concat>(ov::OutputVector{past_k, present_k}, 2);
+ auto result = std::make_shared<Result>(concat);
+ result->output(0).set_names({"output_restored.past_key_values.0.decoder.key"});
+
+ auto model = std::make_shared<ov::Model>(ov::ResultVector{result},
+ ov::ParameterVector{past_k, present_k},
+ "whisper_restored_param_test");
+
+ ov::npuw::RemoveEmptyKVInputs pass;
+ EXPECT_TRUE(pass.run_on_model(model));
+
+ EXPECT_EQ(model->get_parameters().size(), 1u);
+ EXPECT_EQ(model->get_parameters().front()->get_friendly_name(), "present.0.key");
+ EXPECT_EQ(count_ops<ov::op::v0::Concat>(model), 0u);
+}
+
+// Arbitrary non-KV parameter name: should NOT be removed.
+TEST_F(RemoveEmptyKVInputsPassTest, NonKVNamedParamPreserved) {
+ using namespace ov::opset13;
+
+ auto past_other = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 4, 0, 16});
+ past_other->set_friendly_name("some_other_param");
+ past_other->output(0).set_names({"some_other_param"});
+
+ auto present_other = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 4, 1, 16});
+ present_other->set_friendly_name("present_other");
+ present_other->output(0).set_names({"present_other"});
+
+ auto concat = std::make_shared<Concat>(ov::OutputVector{past_other, present_other}, 2);
+ auto result = std::make_shared<Result>(concat);
+
+ auto model = std::make_shared<ov::Model>(ov::ResultVector{result},
+ ov::ParameterVector{past_other, present_other},
+ "non_kv_named_param_test");
+
+ ov::npuw::RemoveEmptyKVInputs pass;
+ EXPECT_FALSE(pass.run_on_model(model)) << "Pass should not match non-KV named parameters";
+ EXPECT_EQ(model->get_parameters().size(), 2u) << "Both parameters should remain";
+ EXPECT_EQ(count_ops<ov::op::v0::Concat>(model), 1u) << "Concat should remain";
+}
+
+// Hybrid cache model: standard KV key+value (should be removed) + linear cache conv/ssm (should be preserved).
+TEST_F(RemoveEmptyKVInputsPassTest, HybridCacheKVRemovedLinCachePreserved) {
+ using namespace ov::opset13;
+
+ // Standard KV cache key – should be removed
+ auto past_k = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 4, 0, 16});
+ past_k->set_friendly_name("past_key_values.0.key");
+ past_k->output(0).set_names({"past_key_values.0.key"});
+
+ auto present_k = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 4, 1, 16});
+ present_k->set_friendly_name("present.0.key");
+ present_k->output(0).set_names({"present.0.key"});
+
+ auto concat_k = std::make_shared<Concat>(ov::OutputVector{past_k, present_k}, 2);
+ auto result_k = std::make_shared<Result>(concat_k);
+ result_k->output(0).set_names({"present.0.key"});
+
+ // Standard KV cache value – should be removed
+ auto past_v = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 4, 0, 16});
+ past_v->set_friendly_name("past_key_values.0.value");
+ past_v->output(0).set_names({"past_key_values.0.value"});
+
+ auto present_v = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 4, 1, 16});
+ present_v->set_friendly_name("present.0.value");
+ present_v->output(0).set_names({"present.0.value"});
+
+ auto concat_v = std::make_shared<Concat>(ov::OutputVector{past_v, present_v}, 2);
+ auto result_v = std::make_shared<Result>(concat_v);
+ result_v->output(0).set_names({"present.0.value"});
+
+ // Linear cache conv – should be preserved
+ auto past_conv = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 2048, 0});
+ past_conv->set_friendly_name("cache_params.past.conv.0");
+ past_conv->output(0).set_names({"cache_params.past.conv.0"});
+
+ auto present_conv = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 2048, 3});
+ present_conv->set_friendly_name("cache_params.present.conv.0");
+ present_conv->output(0).set_names({"cache_params.present.conv.0"});
+
+ auto concat_conv = std::make_shared<Concat>(ov::OutputVector{past_conv, present_conv}, 2);
+ auto result_conv = std::make_shared<Result>(concat_conv);
+
+ // Linear cache ssm – should be preserved
+ auto past_ssm = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 16, 0, 128});
+ past_ssm->set_friendly_name("cache_params.past.ssm.0");
+ past_ssm->output(0).set_names({"cache_params.past.ssm.0"});
+
+ auto present_ssm = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 16, 128, 128});
+ present_ssm->set_friendly_name("cache_params.present.ssm.0");
+ present_ssm->output(0).set_names({"cache_params.present.ssm.0"});
+
+ auto concat_ssm = std::make_shared<Concat>(ov::OutputVector{past_ssm, present_ssm}, 2);
+ auto result_ssm = std::make_shared<Result>(concat_ssm);
+
+ auto model = std::make_shared<ov::Model>(
+ ov::ResultVector{result_k, result_v, result_conv, result_ssm},
+ ov::ParameterVector{past_k, present_k, past_v, present_v, past_conv, present_conv, past_ssm, present_ssm},
+ "hybrid_kv_and_lincache_test");
+
+ ov::npuw::RemoveEmptyKVInputs pass;
+ EXPECT_TRUE(pass.run_on_model(model));
+
+ // past_k and past_v removed; present_k, present_v, conv pair, ssm pair remain
+ EXPECT_EQ(model->get_parameters().size(), 6u) << "Only past_k and past_v should be removed";
+
+ // KV Concats eliminated, linear cache Concats remain
+ EXPECT_EQ(count_ops<ov::op::v0::Concat>(model), 2u) << "Only KV Concats should be removed";
+
+ // Verify linear cache parameters are still present
+ auto params = model->get_parameters();
+ auto has_param = [&](const std::string& name) {
+ return std::any_of(params.begin(), params.end(), [&](const auto& p) {
+ return p->output(0).get_names().count(name) > 0;
+ });
+ };
+ EXPECT_TRUE(has_param("cache_params.past.conv.0")) << "Conv past should be preserved";
+ EXPECT_TRUE(has_param("cache_params.present.conv.0")) << "Conv present should be preserved";
+ EXPECT_TRUE(has_param("cache_params.past.ssm.0")) << "SSM past should be preserved";
+ EXPECT_TRUE(has_param("cache_params.present.ssm.0")) << "SSM present should be preserved";
+}
+
} // namespace
diff --git a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/reshape_to_static_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/reshape_to_static_test.cpp
index 3acb04f3e5a..9c96b5bc9b8 100644
--- a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/reshape_to_static_test.cpp
+++ b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/reshape_to_static_test.cpp
@@ -199,4 +199,74 @@ TEST_F(ReshapeToStaticPassTest, PerLayerInputsResolvedToStaticForPrefillAndGener
EXPECT_EQ(*generate_per_layer_shape, (ov::Shape{1, 1, 42, 256}));
}
+// Builds a stateless model with standard LLM inputs plus linear cache inputs
+// (cache_params.past.conv.N, cache_params.past.ssm.N) to test the matchLinCacheString
+// branch of ReshapeToStatic independently.
+std::shared_ptr<ov::Model> build_model_with_lincache_inputs() {
+ auto input_ids = std::make_shared<ov::op::v0::Parameter>(ov::element::i64, ov::PartialShape{1, -1});
+ input_ids->output(0).set_names({"input_ids"});
+
+ auto attention_mask = std::make_shared<ov::op::v0::Parameter>(ov::element::i64, ov::PartialShape{1, -1});
+ attention_mask->output(0).set_names({"attention_mask"});
+
+ auto position_ids = std::make_shared<ov::op::v0::Parameter>(ov::element::i64, ov::PartialShape{1, -1});
+ position_ids->output(0).set_names({"position_ids"});
+
+ // Standard KV cache input
+ auto kv_key = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{-1, 4, -1, 64});
+ kv_key->output(0).set_names({"past_key_values.0.key"});
+ auto kv_value = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{-1, 4, -1, 64});
+ kv_value->output(0).set_names({"past_key_values.0.value"});
+
+ // Linear cache: conv (e.g. Gated Short Convolution in LFM2/Qwen3.5)
+ auto conv = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{-1, 2048, 3});
+ conv->output(0).set_names({"cache_params.past.conv.0"});
+
+ // Linear cache: ssm (e.g. GatedDeltaNet in Qwen3.5)
+ auto ssm = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{-1, 16, 128, 128});
+ ssm->output(0).set_names({"cache_params.past.ssm.0"});
+
+ auto result = std::make_shared<ov::op::v0::Result>(input_ids);
+ return std::make_shared<ov::Model>(ov::ResultVector{result},
+ ov::ParameterVector{input_ids, attention_mask, position_ids,
+ kv_key, kv_value, conv, ssm},
+ "lincache_reshape_model");
+}
+
+// --- Test 6 -------------------------------------------------------------------------
+// Linear cache inputs (cache_params.past.conv.N, cache_params.past.ssm.N) should only
+// have their batch dimension set to 1 — all other dimensions must be preserved as-is.
+// This contrasts with standard KV cache inputs where seq_len = kvcache_size - input_size.
+TEST_F(ReshapeToStaticPassTest, LinCacheInputsOnlyBatchIsReshaped) {
+ const ov::npuw::KVAxesPosition kv_axes_position{0u, 2u};
+
+ auto model = build_model_with_lincache_inputs();
+ ASSERT_TRUE(ov::npuw::ReshapeToStatic(/*input_size=*/1,
+ /*kvcache_size=*/192,
+ kv_axes_position,
+ /*lora_rank=*/0)
+ .run_on_model(model));
+
+ EXPECT_TRUE(all_inputs_static(model))
+ << "At least one input still has a dynamic dimension after ReshapeToStatic";
+
+ // Conv linear cache: batch=1, other dims unchanged
+ const auto conv_shape = input_shape(model, "cache_params.past.conv.0");
+ ASSERT_TRUE(conv_shape.has_value()) << "cache_params.past.conv.0 not found";
+ EXPECT_EQ(*conv_shape, (ov::Shape{1, 2048, 3}));
+
+ // SSM linear cache: batch=1, other dims unchanged
+ const auto ssm_shape = input_shape(model, "cache_params.past.ssm.0");
+ ASSERT_TRUE(ssm_shape.has_value()) << "cache_params.past.ssm.0 not found";
+ EXPECT_EQ(*ssm_shape, (ov::Shape{1, 16, 128, 128}));
+
+ // Standard KV cache: batch=1, seq_len = kvcache_size(192) - input_size(1) = 191
+ const auto kv_key_shape = input_shape(model, "past_key_values.0.key");
+ ASSERT_TRUE(kv_key_shape.has_value()) << "past_key_values.0.key not found";
+ EXPECT_EQ(*kv_key_shape, (ov::Shape{1, 4, 191, 64}));
+
+ const auto kv_value_shape = input_shape(model, "past_key_values.0.value");
+ ASSERT_TRUE(kv_value_shape.has_value()) << "past_key_values.0.value not found";
+ EXPECT_EQ(*kv_value_shape, (ov::Shape{1, 4, 191, 64}));
+}
} // namespace
diff --git a/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/stateful_to_stateless_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/stateful_to_stateless_test.cpp
new file mode 100644
index 00000000000..992aa180cbf
--- /dev/null
+++ b/src/plugins/intel_npu/tests/unit/npuw/pipeline_passes/stateful_to_stateless_test.cpp
@@ -0,0 +1,630 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <gtest/gtest.h>
+
+#include <string>
+#include <vector>
+
+#include "openvino/op/assign.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/gather.hpp"
+#include "openvino/op/matmul.hpp"
+#include "openvino/op/parameter.hpp"
+#include "openvino/op/read_value.hpp"
+#include "openvino/op/result.hpp"
+#include "openvino/pass/stateful_to_stateless.hpp"
+
+namespace {
+struct CacheStateBlock {
+ std::shared_ptr<ov::op::v6::ReadValue> read_value;
+ std::shared_ptr<ov::op::v6::Assign> assign;
+ ov::Output<ov::Node> output;
+};
+
+// Creates a cache block connected to gather with beam_idx, using an explicit variable_id.
+CacheStateBlock make_cache_state(const ov::Output<ov::Node>& beam_idx,
+ const std::string& var_name,
+ ov::element::Type precision,
+ const ov::PartialShape& var_shape,
+ const ov::Shape& init_shape) {
+ auto variable =
+ std::make_shared<ov::op::util::Variable>(ov::op::util::VariableInfo{var_shape, precision, var_name});
+
+ auto num_elements = ov::shape_size(init_shape);
+ auto init = ov::op::v0::Constant::create(precision, init_shape, std::vector<float>(num_elements, 0.0f));
+ auto read_value = std::make_shared<ov::op::v6::ReadValue>(init, variable);
+
+ auto gather_axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {0});
+ auto gather = std::make_shared<ov::op::v8::Gather>(read_value, beam_idx, gather_axis);
+
+ auto assign = std::make_shared<ov::op::v6::Assign>(gather, variable);
+
+ return {read_value, assign, gather->output(0)};
+}
+
+// Creates a cache block NOT connected to beam_idx, using an explicit variable_id.
+CacheStateBlock make_cache_state(const std::string& var_name,
+ ov::element::Type precision,
+ const ov::PartialShape& var_shape,
+ const ov::Shape& init_shape) {
+ auto variable =
+ std::make_shared<ov::op::util::Variable>(ov::op::util::VariableInfo{var_shape, precision, var_name});
+
+ auto init = ov::op::v0::Constant::create(precision, init_shape, std::vector<float>(ov::shape_size(init_shape), 0.0f));
+ auto read_value = std::make_shared<ov::op::v6::ReadValue>(init, variable);
+
+ auto assign = std::make_shared<ov::op::v6::Assign>(read_value, variable);
+
+ return {read_value, assign, read_value->output(0)};
+}
+
+// Standard LLM KV cache: variable_id = "past_key_values.N.keypresent.N.key"
+CacheStateBlock make_past_key_values_kv_state(const ov::Output<ov::Node>& beam_idx,
+ size_t layer_idx,
+ const std::string& key_or_value,
+ ov::element::Type precision,
+ const ov::PartialShape& var_shape,
+ const ov::Shape& init_shape) {
+ const std::string idx = std::to_string(layer_idx);
+ const std::string var_name = "past_key_values." + idx + "." + key_or_value +
+ "present." + idx + "." + key_or_value;
+ return make_cache_state(beam_idx, var_name, precision, var_shape, init_shape);
+}
+
+// Whisper decoder KV cache: variable_id = "past_key_values.N.decoder.keypresent.N.decoder.key"
+CacheStateBlock make_whisper_decoder_kv_state(const ov::Output<ov::Node>& beam_idx,
+ size_t layer_idx,
+ const std::string& key_or_value,
+ ov::element::Type precision,
+ const ov::PartialShape& var_shape,
+ const ov::Shape& init_shape) {
+ const std::string idx = std::to_string(layer_idx);
+ const std::string var_name = "past_key_values." + idx + ".decoder." + key_or_value +
+ "present." + idx + ".decoder." + key_or_value;
+ return make_cache_state(beam_idx, var_name, precision, var_shape, init_shape);
+}
+
+// Whisper encoder KV cache: numeric variable_id, NOT connected to beam_idx.
+CacheStateBlock make_whisper_encoder_kv_state(const std::string& numeric_id,
+ ov::element::Type precision,
+ const ov::PartialShape& var_shape,
+ const ov::Shape& init_shape) {
+ return make_cache_state(numeric_id, precision, var_shape, init_shape);
+}
+
+CacheStateBlock make_cache_params_kv_state(const ov::Output<ov::Node>& beam_idx,
+ size_t layer_idx,
+ const std::string& key_or_value,
+ ov::element::Type precision,
+ const ov::PartialShape& var_shape,
+ const ov::Shape& init_shape) {
+ const std::string var_name = "cache_params.past." + key_or_value + "." + std::to_string(layer_idx) +
+ "cache_params.present." + key_or_value + "." + std::to_string(layer_idx);
+ return make_cache_state(beam_idx, var_name, precision, var_shape, init_shape);
+}
+
+// Creates a linear cache block (conv) NOT connected to beam_idx.
+CacheStateBlock make_cache_params_gated_short_conv_state(size_t layer_idx,
+ ov::element::Type precision,
+ const ov::Shape& shape) {
+ const std::string var_name = "cache_params.past.conv." + std::to_string(layer_idx) +
+ "cache_params.present.conv." + std::to_string(layer_idx);
+ return make_cache_state(var_name, precision, shape, shape);
+}
+
+// Creates a linear cache block (conv, ssm) connected to beam_idx via Gather.
+CacheStateBlock make_cache_params_lin_state(const ov::Output<ov::Node>& beam_idx,
+ size_t layer_idx,
+ const std::string& cache_type,
+ ov::element::Type precision,
+ const ov::PartialShape& var_shape,
+ const ov::Shape& init_shape) {
+ const std::string var_name = "cache_params.past." + cache_type + "." + std::to_string(layer_idx) +
+ "cache_params.present." + cache_type + "." + std::to_string(layer_idx);
+ return make_cache_state(beam_idx, var_name, precision, var_shape, init_shape);
+}
+
+// Builds a model matching TinyLlama-1.1B caches (scaled down to 2 layers):
+// - Standard KV cache naming: past_key_values.N.key/value (f32, {?,4,?,64})
+// - All connected via beam_idx.
+std::shared_ptr<ov::Model> build_model_with_tinyllama_like_cache(size_t num_layers = 2) {
+ auto stub_input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{-1, 64});
+ stub_input->output(0).set_names({"stub_input"});
+
+ auto beam_idx = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{-1});
+ beam_idx->output(0).set_names({"beam_idx"});
+
+ ov::SinkVector sinks;
+ for (size_t layer = 0; layer < num_layers; ++layer) {
+ auto key_block = make_past_key_values_kv_state(
+ beam_idx, layer, "key", ov::element::f32, ov::PartialShape{-1, 4, -1, 64}, {1, 4, 0, 64});
+ auto value_block = make_past_key_values_kv_state(
+ beam_idx, layer, "value", ov::element::f32, ov::PartialShape{-1, 4, -1, 64}, {1, 4, 0, 64});
+ sinks.push_back(key_block.assign);
+ sinks.push_back(value_block.assign);
+ }
+
+ auto result = std::make_shared<ov::op::v0::Result>(stub_input);
+ return std::make_shared<ov::Model>(ov::ResultVector{result},
+ sinks,
+ ov::ParameterVector{stub_input, beam_idx},
+ "model_with_tinyllama_like_cache");
+}
+
+// Builds a model matching Whisper decoder caches (scaled down to 2 layers):
+// - 2 decoder KV layers: past_key_values.N.decoder.key/value (f32, {?,8,?,64}), connected to beam_idx
+// - 2 encoder KV layers: numeric variable_ids (e.g. "242", "244"), NOT connected to beam_idx
+// The decoder naming doesn't match the StatefulToStateless regex, so "input_restored." prefix is expected.
+// The encoder numeric ids don't match and they also are NOT connected to beam_idx, so they remain unchanged..
+std::shared_ptr<ov::Model> build_model_with_whisper_decoder_like_cache(size_t num_layers = 2) {
+ auto stub_input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{-1, 64});
+ stub_input->output(0).set_names({"stub_input"});
+
+ auto beam_idx = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{-1});
+ beam_idx->output(0).set_names({"beam_idx"});
+
+ ov::SinkVector sinks;
+ size_t encoder_id_counter = 242;
+ for (size_t layer = 0; layer < num_layers; ++layer) {
+ // Decoder KV cache (connected to beam_idx, uses "decoder" in variable_id)
+ auto dec_key = make_whisper_decoder_kv_state(
+ beam_idx, layer, "key", ov::element::f32, ov::PartialShape{-1, 8, -1, 64}, {1, 8, 0, 64});
+ auto dec_value = make_whisper_decoder_kv_state(
+ beam_idx, layer, "value", ov::element::f32, ov::PartialShape{-1, 8, -1, 64}, {1, 8, 0, 64});
+ sinks.push_back(dec_key.assign);
+ sinks.push_back(dec_value.assign);
+
+ // Encoder KV cache (NOT connected to beam_idx, numeric variable_ids)
+ auto enc_key = make_whisper_encoder_kv_state(
+ std::to_string(encoder_id_counter++), ov::element::f32, ov::PartialShape{-1, 8, -1, 64}, {1, 8, 0, 64});
+ auto enc_value = make_whisper_encoder_kv_state(
+ std::to_string(encoder_id_counter++), ov::element::f32, ov::PartialShape{-1, 8, -1, 64}, {1, 8, 0, 64});
+ sinks.push_back(enc_key.assign);
+ sinks.push_back(enc_value.assign);
+ }
+
+ auto result = std::make_shared<ov::op::v0::Result>(stub_input);
+ return std::make_shared<ov::Model>(ov::ResultVector{result},
+ sinks,
+ ov::ParameterVector{stub_input, beam_idx},
+ "model_with_whisper_decoder_like_cache");
+}
+
+// Builds a model matching LFM2-1.2B caches (scaled down to 2 layers):
+// - 2 KV-cache layers: cache_params.past.key.<idx> / value.<idx> (f32, {?,8,?,64})
+// - 2 Gated Short Convolution (conv) layers: cache_params.past.conv.<idx> (f32, {?,2048,3})
+// KV caches are connected via beam_idx; conv caches are not.
+std::shared_ptr<ov::Model> build_model_with_lfm2_like_cache(size_t num_layers = 2) {
+ auto stub_input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{-1, 64});
+ stub_input->output(0).set_names({"stub_input"});
+
+ auto beam_idx = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{-1});
+ beam_idx->output(0).set_names({"beam_idx"});
+
+ ov::SinkVector sinks;
+ for (size_t layer = 0; layer < num_layers; ++layer) {
+ // KV cache (connected to beam_idx)
+ auto key_block =
+ make_cache_params_kv_state(beam_idx, layer, "key", ov::element::f32,
+ ov::PartialShape{-1, 8, -1, 64}, {1, 8, 0, 64});
+ auto value_block =
+ make_cache_params_kv_state(beam_idx, layer, "value", ov::element::f32,
+ ov::PartialShape{-1, 8, -1, 64}, {1, 8, 0, 64});
+ sinks.push_back(key_block.assign);
+ sinks.push_back(value_block.assign);
+
+ // Gated Short Convolution cache (NOT connected to beam_idx)
+ auto conv_block = make_cache_params_gated_short_conv_state(layer, ov::element::f32, {1, 2048, 3});
+ sinks.push_back(conv_block.assign);
+ }
+
+ auto result = std::make_shared<ov::op::v0::Result>(stub_input);
+ return std::make_shared<ov::Model>(ov::ResultVector{result},
+ sinks,
+ ov::ParameterVector{stub_input, beam_idx},
+ "model_with_lfm2_like_cache");
+}
+
+// Builds a model matching Qwen3.5-2B caches (scaled down to 2 layers):
+// - 2 KV-cache layers: cache_params.past.key.<idx> / value.<idx> (f32, {?,2,?,256})
+// - 2 GatedDeltaNet (ssm) layers: cache_params.past.ssm.<idx> (f32, {?,16,128,128})
+// - 2 Convolution1D (conv) layers: cache_params.past.conv.<idx> (f32, {?,6144,4})
+// All cache types are connected via beam_idx Gather in Qwen3.5-2B.
+std::shared_ptr<ov::Model> build_model_with_qwen35_like_cache(size_t num_layers = 2) {
+ auto stub_input = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{-1, 64});
+ stub_input->output(0).set_names({"stub_input"});
+
+ auto beam_idx = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{-1});
+ beam_idx->output(0).set_names({"beam_idx"});
+
+ ov::SinkVector sinks;
+ for (size_t layer = 0; layer < num_layers; ++layer) {
+ // KV cache
+ auto key_block =
+ make_cache_params_kv_state(beam_idx, layer, "key", ov::element::f32, ov::PartialShape{-1, 2, -1, 256}, {1, 2, 0, 256});
+ auto value_block =
+ make_cache_params_kv_state(beam_idx, layer, "value", ov::element::f32, ov::PartialShape{-1, 2, -1, 256}, {1, 2, 0, 256});
+ sinks.push_back(key_block.assign);
+ sinks.push_back(value_block.assign);
+
+ // GatedDeltaNet SSM cache
+ auto ssm_block =
+ make_cache_params_lin_state(beam_idx, layer, "ssm", ov::element::f32, ov::PartialShape{-1, 16, 128, 128}, {1, 16, 128, 128});
+ sinks.push_back(ssm_block.assign);
+
+ // Conv1D cache
+ auto conv_block =
+ make_cache_params_lin_state(beam_idx, layer, "conv", ov::element::f32, ov::PartialShape{-1, 6144, 4}, {1, 6144, 4});
+ sinks.push_back(conv_block.assign);
+ }
+
+ auto result = std::make_shared<ov::op::v0::Result>(stub_input);
+ return std::make_shared<ov::Model>(ov::ResultVector{result},
+ sinks,
+ ov::ParameterVector{stub_input, beam_idx},
+ "model_with_qwen35_like_cache");
+}
+
+bool has_input_with_name(const std::shared_ptr<ov::Model>& model, const std::string& name) {
+ for (const auto& input : model->inputs()) {
+ for (const auto& input_name : input.get_names()) {
+ if (input_name.find(name) != std::string::npos) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+bool has_output_with_name(const std::shared_ptr<ov::Model>& model, const std::string& name) {
+ for (const auto& output : model->outputs()) {
+ for (const auto& output_name : output.get_names()) {
+ if (output_name.find(name) != std::string::npos) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+std::vector<std::string> collect_input_names(const std::shared_ptr<ov::Model>& model) {
+ std::vector<std::string> names;
+ for (const auto& input : model->inputs()) {
+ names.push_back(input.get_any_name());
+ }
+ return names;
+}
+
+std::vector<std::string> collect_output_names(const std::shared_ptr<ov::Model>& model) {
+ std::vector<std::string> names;
+ for (const auto& output : model->outputs()) {
+ names.push_back(output.get_any_name());
+ }
+ return names;
+}
+
+TEST(StatefulToStatelessTest, TinyLlama_ConvertsToStateless) {
+ auto model = build_model_with_tinyllama_like_cache();
+
+ ASSERT_EQ(model->get_sinks().size(), 4u);
+ ASSERT_EQ(model->get_variables().size(), 4u);
+
+ ASSERT_NO_THROW(ov::pass::StatefulToStateless().run_on_model(model));
+
+ EXPECT_EQ(model->get_sinks().size(), 0u);
+ EXPECT_EQ(model->get_variables().size(), 0u);
+
+ for (size_t layer = 0; layer < 2; ++layer) {
+ const auto idx = std::to_string(layer);
+ EXPECT_TRUE(has_input_with_name(model, "past_key_values." + idx + ".key"));
+ EXPECT_TRUE(has_input_with_name(model, "past_key_values." + idx + ".value"));
+ EXPECT_TRUE(has_output_with_name(model, "present." + idx + ".key"));
+ EXPECT_TRUE(has_output_with_name(model, "present." + idx + ".value"));
+ }
+
+ // Verify ordering: key(0), value(0), key(1), value(1)
+ auto input_names = collect_input_names(model);
+ std::vector<std::string> kv_inputs;
+ for (const auto& name : input_names) {
+ if (name.find("past_key_values") != std::string::npos) {
+ kv_inputs.push_back(name);
+ }
+ }
+ ASSERT_EQ(kv_inputs.size(), 4u);
+ EXPECT_NE(kv_inputs[0].find("0.key"), std::string::npos);
+ EXPECT_NE(kv_inputs[1].find("0.value"), std::string::npos);
+ EXPECT_NE(kv_inputs[2].find("1.key"), std::string::npos);
+ EXPECT_NE(kv_inputs[3].find("1.value"), std::string::npos);
+
+ // Verify output ordering: present.0.key, present.0.value, present.1.key, present.1.value
+ auto output_names = collect_output_names(model);
+ std::vector<std::string> kv_outputs;
+ for (const auto& name : output_names) {
+ if (name.find("present") != std::string::npos) {
+ kv_outputs.push_back(name);
+ }
+ }
+ ASSERT_EQ(kv_outputs.size(), 4u);
+ EXPECT_NE(kv_outputs[0].find("0.key"), std::string::npos);
+ EXPECT_NE(kv_outputs[1].find("0.value"), std::string::npos);
+ EXPECT_NE(kv_outputs[2].find("1.key"), std::string::npos);
+ EXPECT_NE(kv_outputs[3].find("1.value"), std::string::npos);
+}
+
+// Whisper decoder naming ("past_key_values.N.decoder.key") doesn't match the
+// StatefulToStateless regex, so converted parameters get "input_restored." prefix.
+// Encoder states (numeric variable_ids) are not connected to beam_idx and don't match
+// any naming convention, so they remain unchanged.
+TEST(StatefulToStatelessTest, WhisperDecoder_DecoderStatesGetRestoredPrefix) {
+ auto model = build_model_with_whisper_decoder_like_cache();
+
+ // 2 layers * (dec_key + dec_value + enc_key + enc_value) = 8 sinks
+ ASSERT_EQ(model->get_sinks().size(), 8u);
+ ASSERT_EQ(model->get_variables().size(), 8u);
+
+ ASSERT_NO_THROW(ov::pass::StatefulToStateless().run_on_model(model));
+
+ // Decoder states (connected to beam_idx) get converted with "input_restored." prefix
+ for (size_t layer = 0; layer < 2; ++layer) {
+ const auto idx = std::to_string(layer);
+ const auto decoder_key_var =
+ "past_key_values." + idx + ".decoder.keypresent." + idx + ".decoder.key";
+ const auto decoder_value_var =
+ "past_key_values." + idx + ".decoder.valuepresent." + idx + ".decoder.value";
+
+ EXPECT_TRUE(has_input_with_name(model, "input_restored." + decoder_key_var))
+ << "Decoder key layer " << layer << " should get input_restored prefix";
+ EXPECT_TRUE(has_input_with_name(model, "input_restored." + decoder_value_var))
+ << "Decoder value layer " << layer << " should get input_restored prefix";
+ EXPECT_TRUE(has_output_with_name(model, "output_restored." + decoder_key_var))
+ << "Decoder key layer " << layer << " should get output_restored prefix";
+ EXPECT_TRUE(has_output_with_name(model, "output_restored." + decoder_value_var))
+ << "Decoder value layer " << layer << " should get output_restored prefix";
+ }
+
+ // Encoder states (numeric ids, not connected to beam_idx) remain as sinks/variables
+ EXPECT_EQ(model->get_sinks().size(), 4u) << "Encoder states should remain as sinks";
+ EXPECT_EQ(model->get_variables().size(), 4u) << "Encoder variables should remain";
+}
+
+
+// Verifies full conversion: sinks/variables removed, beam_idx removed,
+// KV inputs -> past_key_values naming, conv inputs -> cache_params naming.
+TEST(StatefulToStatelessTest, LFM2_ConvertsToStateless) {
+ auto model = build_model_with_lfm2_like_cache();
+
+ // 2 layers x (key + value + conv) = 6 sinks
+ ASSERT_EQ(model->get_sinks().size(), 6u);
+ ASSERT_EQ(model->get_variables().size(), 6u);
+
+ ASSERT_NO_THROW(ov::pass::StatefulToStateless().run_on_model(model));
+
+ EXPECT_EQ(model->get_sinks().size(), 0u);
+ EXPECT_EQ(model->get_variables().size(), 0u);
+
+ // KV cache key/value -> mapped to standard "past_key_values" / "present" naming
+ for (size_t layer = 0; layer < 2; ++layer) {
+ const auto idx = std::to_string(layer);
+ EXPECT_TRUE(has_input_with_name(model, "past_key_values." + idx + ".key"));
+ EXPECT_TRUE(has_input_with_name(model, "past_key_values." + idx + ".value"));
+ EXPECT_TRUE(has_output_with_name(model, "present." + idx + ".key"));
+ EXPECT_TRUE(has_output_with_name(model, "present." + idx + ".value"));
+ }
+
+ // Conv cache -> keeps cache_params naming
+ for (size_t layer = 0; layer < 2; ++layer) {
+ const auto idx = std::to_string(layer);
+ EXPECT_TRUE(has_input_with_name(model, "cache_params.past.conv." + idx));
+ EXPECT_TRUE(has_output_with_name(model, "cache_params.present.conv." + idx));
+ }
+}
+
+// Verifies ordering: KV caches first (key before value, layer 0 before 1), then conv in natural order.
+TEST(StatefulToStatelessTest, LFM2_KVCacheBeforeConv) {
+ auto model = build_model_with_lfm2_like_cache();
+ ov::pass::StatefulToStateless().run_on_model(model);
+
+ auto input_names = collect_input_names(model);
+
+ std::vector<std::string> cache_inputs;
+ for (const auto& name : input_names) {
+ if (name.find("past_key_values") != std::string::npos ||
+ name.find("cache_params") != std::string::npos) {
+ cache_inputs.push_back(name);
+ }
+ }
+
+ // 2 layers: key(0), value(0), key(1), value(1), conv(0), conv(1)
+ ASSERT_EQ(cache_inputs.size(), 6u);
+ EXPECT_NE(cache_inputs[0].find("0.key"), std::string::npos);
+ EXPECT_NE(cache_inputs[1].find("0.value"), std::string::npos);
+ EXPECT_NE(cache_inputs[2].find("1.key"), std::string::npos);
+ EXPECT_NE(cache_inputs[3].find("1.value"), std::string::npos);
+
+ // Conv caches after KV
+ EXPECT_NE(cache_inputs[4].find("conv"), std::string::npos);
+ EXPECT_NE(cache_inputs[5].find("conv"), std::string::npos);
+
+ // Verify same ordering for outputs: present.0.key, present.0.value, ..., cache_params.present.conv.0, ...
+ auto output_names = collect_output_names(model);
+ std::vector<std::string> cache_outputs;
+ for (const auto& name : output_names) {
+ if (name.find("present") != std::string::npos) {
+ cache_outputs.push_back(name);
+ }
+ }
+ ASSERT_EQ(cache_outputs.size(), 6u);
+ EXPECT_NE(cache_outputs[0].find("0.key"), std::string::npos);
+ EXPECT_NE(cache_outputs[1].find("0.value"), std::string::npos);
+ EXPECT_NE(cache_outputs[2].find("1.key"), std::string::npos);
+ EXPECT_NE(cache_outputs[3].find("1.value"), std::string::npos);
+ EXPECT_NE(cache_outputs[4].find("conv"), std::string::npos);
+ EXPECT_NE(cache_outputs[5].find("conv"), std::string::npos);
+}
+
+// Verifies that shapes from the real model are preserved through conversion.
+TEST(StatefulToStatelessTest, LFM2_ShapesAndElementsTypeArePreserved) {
+ auto model = build_model_with_lfm2_like_cache(1); // 1 layer for simplicity
+ ov::pass::StatefulToStateless().run_on_model(model);
+
+ for (const auto& input : model->inputs()) {
+ const auto& name = input.get_any_name();
+ const auto& shape = input.get_partial_shape();
+
+ if (name.find("past_key_values") != std::string::npos) {
+ // KV cache: {?,8,?,64}, f32
+ ASSERT_EQ(shape.rank().get_length(), 4) << "KV input " << name << " should be rank 4";
+ EXPECT_EQ(shape[1].get_length(), 8) << "Num heads mismatch for " << name;
+ EXPECT_EQ(shape[3].get_length(), 64) << "Head dim mismatch for " << name;
+ EXPECT_EQ(input.get_element_type(), ov::element::f32) << "Element type mismatch for " << name;
+ } else if (name.find("cache_params.past.conv") != std::string::npos) {
+ // Conv cache: {1, 2048, 3}, f32
+ ASSERT_EQ(shape.rank().get_length(), 3) << "Conv input " << name << " should be rank 3";
+ EXPECT_EQ(shape[1].get_length(), 2048) << "Conv 1st dim mismatch for " << name;
+ EXPECT_EQ(shape[2].get_length(), 3) << "Conv kernel size mismatch for " << name;
+ EXPECT_EQ(input.get_element_type(), ov::element::f32) << "Element type mismatch for " << name;
+ }
+ }
+}
+
+// Verifies full conversion with all three cache types.
+TEST(StatefulToStatelessTest, Qwen35_ConvertsToStateless) {
+ auto model = build_model_with_qwen35_like_cache();
+
+ // 2 layers x (key + value + ssm + conv) = 8 sinks
+ ASSERT_EQ(model->get_sinks().size(), 8u);
+ ASSERT_EQ(model->get_variables().size(), 8u);
+
+ ASSERT_NO_THROW(ov::pass::StatefulToStateless().run_on_model(model));
+
+ EXPECT_EQ(model->get_sinks().size(), 0u);
+ EXPECT_EQ(model->get_variables().size(), 0u);
+
+ for (size_t layer = 0; layer < 2; ++layer) {
+ const auto idx = std::to_string(layer);
+ // KV -> standard naming
+ EXPECT_TRUE(has_input_with_name(model, "past_key_values." + idx + ".key"));
+ EXPECT_TRUE(has_input_with_name(model, "past_key_values." + idx + ".value"));
+ EXPECT_TRUE(has_output_with_name(model, "present." + idx + ".key"));
+ EXPECT_TRUE(has_output_with_name(model, "present." + idx + ".value"));
+
+ // SSM -> cache_params naming
+ EXPECT_TRUE(has_input_with_name(model, "cache_params.past.ssm." + idx));
+ EXPECT_TRUE(has_output_with_name(model, "cache_params.present.ssm." + idx));
+
+ // Conv -> cache_params naming
+ EXPECT_TRUE(has_input_with_name(model, "cache_params.past.conv." + idx));
+ EXPECT_TRUE(has_output_with_name(model, "cache_params.present.conv." + idx));
+ }
+}
+
+// Verifies ordering: KV first (key before value), then ssm/conv in natural order.
+TEST(StatefulToStatelessTest, Qwen35_KVCacheBeforeLinearCaches) {
+ auto model = build_model_with_qwen35_like_cache();
+ ov::pass::StatefulToStateless().run_on_model(model);
+
+ auto input_names = collect_input_names(model);
+
+ int kv_last_pos = -1;
+ int lin_first_pos = static_cast<int>(input_names.size());
+
+ for (int i = 0; i < static_cast<int>(input_names.size()); ++i) {
+ if (input_names[i].find("past_key_values") != std::string::npos) {
+ kv_last_pos = i;
+ }
+ if (input_names[i].find("cache_params") != std::string::npos) {
+ lin_first_pos = std::min(lin_first_pos, i);
+ }
+ }
+
+ EXPECT_LT(kv_last_pos, lin_first_pos)
+ << "KV cache parameters should appear before linear cache parameters";
+
+ // Same check for outputs
+ auto output_names = collect_output_names(model);
+ int kv_out_last_pos = -1;
+ int lin_out_first_pos = static_cast<int>(output_names.size());
+ for (int i = 0; i < static_cast<int>(output_names.size()); ++i) {
+ if (output_names[i].find("present") != std::string::npos &&
+ output_names[i].find("cache_params") == std::string::npos) {
+ kv_out_last_pos = i;
+ }
+ if (output_names[i].find("cache_params.present") != std::string::npos) {
+ lin_out_first_pos = std::min(lin_out_first_pos, i);
+ }
+ }
+ EXPECT_LT(kv_out_last_pos, lin_out_first_pos)
+ << "KV cache outputs should appear before linear cache outputs";
+}
+
+// Verifies KV key-before-value ordering within each layer.
+TEST(StatefulToStatelessTest, Qwen35_KeyBeforeValueOrdering) {
+ auto model = build_model_with_qwen35_like_cache();
+ ov::pass::StatefulToStateless().run_on_model(model);
+
+ auto input_names = collect_input_names(model);
+
+ std::vector<std::string> kv_inputs;
+ for (const auto& name : input_names) {
+ if (name.find("past_key_values") != std::string::npos) {
+ kv_inputs.push_back(name);
+ }
+ }
+
+ // Expected: key(0), value(0), key(1), value(1)
+ ASSERT_EQ(kv_inputs.size(), 4u);
+ EXPECT_NE(kv_inputs[0].find("0.key"), std::string::npos);
+ EXPECT_NE(kv_inputs[1].find("0.value"), std::string::npos);
+ EXPECT_NE(kv_inputs[2].find("1.key"), std::string::npos);
+ EXPECT_NE(kv_inputs[3].find("1.value"), std::string::npos);
+
+ // Same ordering for outputs
+ auto output_names = collect_output_names(model);
+ std::vector<std::string> kv_outputs;
+ for (const auto& name : output_names) {
+ if (name.find("present") != std::string::npos &&
+ name.find("cache_params") == std::string::npos) {
+ kv_outputs.push_back(name);
+ }
+ }
+ ASSERT_EQ(kv_outputs.size(), 4u);
+ EXPECT_NE(kv_outputs[0].find("0.key"), std::string::npos);
+ EXPECT_NE(kv_outputs[1].find("0.value"), std::string::npos);
+ EXPECT_NE(kv_outputs[2].find("1.key"), std::string::npos);
+ EXPECT_NE(kv_outputs[3].find("1.value"), std::string::npos);
+}
+
+// Verifies shapes and element types from the real Qwen3.5 model are preserved.
+TEST(StatefulToStatelessTest, Qwen35_ShapesAndElementTypesPreserved) {
+ auto model = build_model_with_qwen35_like_cache(1);
+ ov::pass::StatefulToStateless().run_on_model(model);
+
+ for (const auto& input : model->inputs()) {
+ const auto& name = input.get_any_name();
+ const auto& shape = input.get_partial_shape();
+
+ if (name.find("past_key_values") != std::string::npos) {
+ // KV cache: {?,2,?,256}, f32
+ ASSERT_EQ(shape.rank().get_length(), 4) << "KV input " << name << " should be rank 4";
+ EXPECT_EQ(shape[1].get_length(), 2) << "Num heads mismatch for " << name;
+ EXPECT_EQ(shape[3].get_length(), 256) << "Head dim mismatch for " << name;
+ EXPECT_EQ(input.get_element_type(), ov::element::f32) << "Element type mismatch for " << name;
+ } else if (name.find("cache_params.past.ssm") != std::string::npos) {
+ // SSM cache: {1, 16, 128, 128}, f32
+ ASSERT_EQ(shape.rank().get_length(), 4) << "SSM input " << name << " should be rank 4";
+ EXPECT_EQ(shape[1].get_length(), 16);
+ EXPECT_EQ(shape[2].get_length(), 128);
+ EXPECT_EQ(shape[3].get_length(), 128);
+ EXPECT_EQ(input.get_element_type(), ov::element::f32) << "Element type mismatch for " << name;
+ } else if (name.find("cache_params.past.conv") != std::string::npos) {
+ // Conv cache: {1, 6144, 4}, f32
+ ASSERT_EQ(shape.rank().get_length(), 3) << "Conv input " << name << " should be rank 3";
+ EXPECT_EQ(shape[1].get_length(), 6144);
+ EXPECT_EQ(shape[2].get_length(), 4);
+ EXPECT_EQ(input.get_element_type(), ov::element::f32) << "Element type mismatch for " << name;
+ }
+ }
+}
+} // namespace
diff --git a/src/plugins/intel_npu/tests/unit/npuw/stored_tokens_state_test.cpp b/src/plugins/intel_npu/tests/unit/npuw/stored_tokens_state_test.cpp
new file mode 100644
index 00000000000..66cff261d89
--- /dev/null
+++ b/src/plugins/intel_npu/tests/unit/npuw/stored_tokens_state_test.cpp
@@ -0,0 +1,61 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <gtest/gtest.h>
+
+#include <string>
+
+// Forward-declare LLMInferRequest so the friend declaration in StoredTokensState resolves.
+namespace ov { namespace npuw { class LLMInferRequest; } }
+
+#include "llm_stored_tokens_state.hpp"
+
+namespace {
+int64_t read_stored_tokens(const ov::npuw::StoredTokensState& state) {
+ auto tensor = state.get_state();
+ return tensor->data<int64_t>()[0];
+}
+
+TEST(StoredTokensStateTest, InitialValueIsZero) {
+ ov::npuw::StoredTokensState state;
+ EXPECT_EQ(read_stored_tokens(state), 0);
+}
+
+TEST(StoredTokensStateTest, StateNameIsCorrect) {
+ ov::npuw::StoredTokensState state;
+ EXPECT_EQ(state.get_name(), "npuw_stored_tokens_state");
+}
+
+TEST(StoredTokensStateTest, ResetState) {
+ ov::npuw::StoredTokensState state;
+ state.reset();
+ EXPECT_EQ(read_stored_tokens(state), 0);
+}
+
+// --- set_state(...) should not be called externally ---
+TEST(StoredTokensStateTest, SetStateThrows) {
+ ov::npuw::StoredTokensState state;
+ auto tensor = ov::Tensor(ov::element::i64, ov::Shape{1});
+ EXPECT_ANY_THROW(state.set_state(ov::get_tensor_impl(tensor)));
+}
+
+TEST(StoredTokensStateTest, GetStateReturnsTensor) {
+ ov::npuw::StoredTokensState state;
+ auto tensor = state.get_state();
+ ASSERT_NE(tensor, nullptr);
+ EXPECT_EQ(tensor->get_element_type(), ov::element::i64);
+ EXPECT_EQ(tensor->get_shape(), ov::Shape{1});
+}
+
+// --- get_state() returns a copy, so external mutation does not affect internal state ---
+TEST(StoredTokensStateTest, GetStateReturnsCopyNotReference) {
+ ov::npuw::StoredTokensState state;
+ EXPECT_EQ(read_stored_tokens(state), 0);
+
+ auto tensor = state.get_state();
+ tensor->data<int64_t>()[0] = 42;
+
+ EXPECT_EQ(read_stored_tokens(state), 0);
+}
+} // namespace