Skip to content

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

[NPUW] Cover new LFM2 IR with the tests (#36876)

Anastasiya(Asya) Pronina86874ae24ea7
diff --git a/src/core/src/pass/stateful_to_stateless.cpp b/src/core/src/pass/stateful_to_stateless.cpp
index c13a4507501..5bc3705f9f8 100644
--- a/src/core/src/pass/stateful_to_stateless.cpp
+++ b/src/core/src/pass/stateful_to_stateless.cpp
@@ -154,7 +154,7 @@ bool ov::pass::StatefulToStateless::run_on_model(const std::shared_ptr<ov::Model
"Stateful models without `beam_idx` input are not supported in StatefulToStateless transformation");
}
- // Process ReadValues that are NOT connected via beam_idx: Conv and SSM caches in models with Linear Attention.
+ // Process ReadValues that are NOT connected via beam_idx: Conv caches in old LFM2 IRs.
for (const auto& op : model->get_ops()) {
if (auto read_value = ov::as_type_ptr<op::util::ReadValueBase>(op)) {
auto variable_name = read_value->get_variable_id();
@@ -162,7 +162,7 @@ bool ov::pass::StatefulToStateless::run_on_model(const std::shared_ptr<ov::Model
if ((processed_variable_ids.find(variable_name) == processed_variable_ids.end()) &&
std::regex_match(variable_name, match, context.lin_cache_naming_convention)) {
variables.push_back(Variable(context, variable_name));
- // For models with Linear Attention, ReadValue for Conv and SSM caches connects directly to the useful
+ // For old LFM2 IR, ReadValue for Conv caches connects directly to the useful
// Ops after.
future_params[variable_name] = read_value;
processed_variable_ids.insert(std::move(variable_name));
diff --git a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/add_position_ids_param.cpp b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/add_position_ids_param.cpp
index 9604baf8408..eec22d1fa7e 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/add_position_ids_param.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/add_position_ids_param.cpp
@@ -35,7 +35,7 @@ public:
OPENVINO_MATCHER_PASS_RTTI("ov::npuw::HardcodedPositionIdsMatcher");
explicit HardcodedPositionIdsMatcher(ov::ParameterVector& new_params) {
// The oldest exports shared one Range between RoPE, Causal Mask and the Gated Short
- // Convolution Block, and offset it inside Range itself:
+ // Convolution Block indexing, and offset it inside Range itself:
//
// -> Convert -> RoPE
// |
@@ -70,14 +70,15 @@ public:
auto sin = opp::wrap_type<ov::op::v0::Sin>(concat);
ov::pass::MultiMatcher::Callback callback = [=, &new_params](const auto& m) {
- // NOTE: Range that mimics `position_ids` is consumed by RoPE operation as well as by Causal Mask creation
- // (LessEqual operation) and Gated Short Convolution Block's ScattedNDUpdate operation.
+ // NOTE: Range that mimics `position_ids` is consumed by RoPE operation, but might as well be used for
+ // Causal Mask creation (via LessEqual, ex.: transformers==5.0.0) and Gated Short Convolution Block's
+ // ScatterNDUpdate operation (ex.: transformers==4.57.6). The rewrite below only rewires the RoPE
+ // path and ScatterNDUpdate path (if exists) to use new `position_ids` parameter.
// For static shapes case, it is not right to use actual `position_ids` for the second argument of
- // LessEqual operation (=Q range), because causal triangular mask will only allow positions from
- // the left till the real current positions in the sequence (inclusively), while our current items
- // are lied at the right end of the static `input_ids` after a window of padding.
- // Thus, the Range is preserved for Causal Mask creation, while added `position_ids` parameter is
- // used only for RoPE and ScatterNDUpdate in Gated Short Convolution Block.
+ // LessEqual operation (=Q range) in creation of the causal mask. This setup will only allow positions
+ // from the left till the real current positions in the sequence (inclusively), while our current
+ // items are lied at the right end of the static `input_ids` after a whole window of padding. Thus,
+ // the Range should be preserved for Causal Mask creation.
auto& pattern_to_output = m.at(cos).front();
auto range_node = pattern_to_output.at(range).get_node_shared_ptr();
@@ -95,7 +96,7 @@ public:
convert_node->input(0).replace_source_output(unsqueeze1_node_copy->output(0));
// FIXME: For Gated Short Convolution Block, there is ScatterNDUpdate that also consumes generated
- // positions.
+ // positions in old IRs.
// It seems to right to use the newly created `position_ids` for it as well, however, real tests show
// no difference against usage of hardcoded QRange: both are similarly accurate.
auto position_ids_squeezed = std::make_shared<ov::op::v0::Squeeze>(
@@ -104,8 +105,7 @@ public:
OPENVINO_ASSERT(range_node->get_output_size() == 1, "Range node should have exactly one output");
auto range_consumers = range_node->get_output_target_inputs(0);
for (auto&& consumer : range_consumers) {
- // Only the Clamp path is rewired: the Range is preserved for Causal Mask creation,
- // and newer models have no Clamp to begin with.
+ // Only the Clamp path is remained to rewire.
if (consumer.get_node()->get_type_name() == std::string("Clamp")) {
consumer.replace_source_output(position_ids_squeezed->output(0));
}
diff --git a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/right_align_mask_slice_for_conv.cpp b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/right_align_mask_slice_for_conv.cpp
index 403d91afa92..09732a297ad 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/right_align_mask_slice_for_conv.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/npuw_transformations/right_align_mask_slice_for_conv.cpp
@@ -28,10 +28,9 @@ public:
// the Slice operation is inserted to extract the current tokens from that `attention_mask`
// and pass to the Conv subgraph.
// This is done to allow skipping processing of some tokens in the prefill stage.
- // Slice is done from zero till the number of current tokens, assuming that in most cases
- // tokens from the right are not the useful ones.
- // However, in NPUW static-shape mode, we place useful current tokens exactly to the right end
- // in the prefill stage.
+ // Slice is applied from the zero till the current length of the sequence.
+ // However, in NPUW static-shape mode, we place useful current tokens to the right end
+ // of `input_ids` after a fixed amount of padding tokens.
// Therefore, Slice operation will extract the left part of mask, that in case of chunked
// prefill will be padded either with zeroes (in the first iteration) or with ones
// (in subsequent iterations) but for past tokens.
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
index 78cbc1f7170..7edac2bd904 100644
--- 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
@@ -27,11 +27,32 @@
#include "openvino/op/unsqueeze.hpp"
namespace {
-// offset_positions selects the transformers>=5.4 shape, where Range starts from zero and the past
+// Shared builder for LFM2-like RoPE / causal-mask patterns.
+//
+// Before transformers==5.0.0, the pattern looked like this:
+// Range -> Unsqueeze(0) -> Unsqueeze(1) -> Convert -> MatMul(inv_freq, .)
+// -> Transpose -> Concat(.,.) -> {Cos, Sin}
+// `-> Unsqueeze(2) -> LessEqual (causal mask)
+//
+// And, there was an additional Range consumer for Gated Short Convolution indexing attached:
+// Range -> Clamp -> Add -> Mod -> Unsqueeze -> ScatterNDUpdate
+//
+// From transformers>=5.0.0, the pattern is simplified to the first part only (no Clamp consumer).
+//
+//
+// From transformers>=5.4, pattern doesn't use the same subgraph for causal mask and
+// RoPE anymore as well as it doesn't utilize Gated Short Convolution Indexing path too.
+// So, only the RoPE chain is expected to be matched to feed `position_ids` into.
+//
+//
+// `offset_positions` selects the shape, where Range starts from zero and the past
// length is added on top of it rather than being folded into Range's start.
-// with_clamp controls the Gated Short Convolution Block's Clamp consumer on Range, which newer
-// exports drop altogether.
-std::shared_ptr<ov::Model> build_model_with_lfm2_like_pattern(bool offset_positions = false, bool with_clamp = true) {
+// `with_range_clamp_consumer` selects whether to attach the additional Range consumer,
+// which full pattern is simplified to Range -> Clamp -> Result. It defaults to true, so that a
+// no-argument call still builds the pre-5.0.0 IR the older tests below are written against.
+std::shared_ptr<ov::Model> build_model_with_lfm2_like_pattern(bool offset_positions = false,
+ bool with_range_clamp_consumer = true,
+ const std::string& model_name = "lfm2_like_model") {
// 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});
@@ -53,16 +74,16 @@ std::shared_ptr<ov::Model> build_model_with_lfm2_like_pattern(bool offset_positi
auto unsqueeze = std::make_shared<ov::op::v0::Unsqueeze>(positions, unsqueeze_axes);
unsqueeze->set_friendly_name("unsqueeze_batch");
- // Unsqueeze1: add feature dim [1, seq_len] → [1, 1, seq_len]
+ // 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)
+ // Convert (always present in real LFM2 models)
auto convert = std::make_shared<ov::op::v0::Convert>(unsqueeze1, ov::element::f32);
convert->set_friendly_name("convert");
- // MatMul: [inv_freq] × [positions] → freqs
+ // MatMul: [inv_freq] x [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");
@@ -70,12 +91,12 @@ std::shared_ptr<ov::Model> build_model_with_lfm2_like_pattern(bool offset_positi
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]
+ // 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
+ // 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");
@@ -106,9 +127,9 @@ std::shared_ptr<ov::Model> build_model_with_lfm2_like_pattern(bool offset_positi
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
- if (with_clamp) {
+ if (with_range_clamp_consumer) {
+ // Old LFM2 IR only: Range -> Clamp (stand-in for Range -> Clamp -> Add -> Mod ->
+ // Unsqueeze -> ScatterNDUpdate). The updated LFM2 IR does not have this consumer.
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);
@@ -116,7 +137,16 @@ std::shared_ptr<ov::Model> build_model_with_lfm2_like_pattern(bool offset_positi
results.push_back(clamp_result);
}
- return std::make_shared<ov::Model>(results, params, "model_with_lfm2_like_pattern");
+ return std::make_shared<ov::Model>(results, params, model_name);
+}
+
+// Updated LFM2 IR: RoPE + causal mask only. The Conv cache update no longer touches
+// Range (it uses in-layer Slice + Broadcast instead), so Range has just two
+// consumers.
+std::shared_ptr<ov::Model> build_model_with_lfm2_v2_like_pattern() {
+ return build_model_with_lfm2_like_pattern(/*offset_positions=*/false,
+ /*with_range_clamp_consumer=*/false,
+ "model_with_lfm2_v2_like_pattern");
}
bool has_parameter_named(const std::shared_ptr<ov::Model>& model, const std::string& name) {
@@ -259,7 +289,7 @@ TEST(AddPositionIdsParamTest, NoOpWhenPatternDoesNotMatch) {
}
// --- Test: Clamp consumer on Range gets rewired to position_ids ---
-// In real LFM2 models, Range feeds a Clamp for Gated Short Convolution indexing.
+// In old 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();
@@ -299,11 +329,115 @@ TEST(AddPositionIdsParamTest, ReapplyDoesNotModifyGraph) {
<< "Second pass should not add duplicate operations";
}
+// ===================== LFM2 v2 (updated IR) tests =====================
+// The updated LFM2 IR no longer routes `Range` through the Range -> Clamp -> Add ->
+// Mod -> Unsqueeze -> ScatterNDUpdate path. The Conv cache is now updated in-layer
+// via Slice(last 3) + Broadcast, entirely decoupled from `Range`.
+// Therefore `Range` has only two consumers:
+// Branch A: Range -> Unsqueeze -> Unsqueeze -> Convert -> MatMul (RoPE)
+// Branch B: Range -> Unsqueeze -> Unsqueeze -> Unsqueeze -> LessEqual (causal mask)
+// The AddPositionIdsParam pass must still add `position_ids`, rewire the RoPE branch
+// to it, and preserve `Range` for the causal-mask branch.
+
+// After the pass: `position_ids` parameter is added with i64 / {?, ?}.
+TEST(AddPositionIdsParamTest, LFM2v2_AddsPositionIdsParameter) {
+ auto model = build_model_with_lfm2_v2_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"));
+
+ 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";
+}
+
+// After the pass: the RoPE branch (MatMul) is fed by position_ids, not by Range.
+TEST(AddPositionIdsParamTest, LFM2v2_RopePathUsesPositionIds) {
+ auto model = build_model_with_lfm2_v2_like_pattern();
+ ov::npuw::AddPositionIdsParam().run_on_model(model);
+
+ 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: 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) {
+ 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";
+}
+
+// After the pass: Range is still present and still feeds the causal-mask LessEqual.
+TEST(AddPositionIdsParamTest, LFM2v2_RangePreservedForCausalMask) {
+ auto model = build_model_with_lfm2_v2_like_pattern();
+ ov::npuw::AddPositionIdsParam().run_on_model(model);
+
+ EXPECT_GE(count_ops_of_type(model, "Range"), 1u) << "Range node should be preserved for causal mask";
+
+ bool found_range_as_le_input = false;
+ for (const auto& op : model->get_ops()) {
+ if (op->get_type_name() != std::string("LessEqual")) {
+ continue;
+ }
+ 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";
+}
+
+
// --- transformers>=5.4: Range -> Add(past_len) -> Unsqueeze -> Unsqueeze -> Convert -> RoPE ---
// The offset moved out of Range, so the pass matches through an optional Add. These models also
// give the causal mask its own Range and have no Clamp path.
TEST(AddPositionIdsParamTest, OffsetAddRopePathUsesPositionIds) {
- auto model = build_model_with_lfm2_like_pattern(true /*offset_positions*/, false /*with_clamp*/);
+ auto model = build_model_with_lfm2_like_pattern(true /*offset_positions*/, false /*with_range_clamp_consumer*/);
EXPECT_FALSE(has_parameter_named(model, "position_ids"));
ASSERT_NO_THROW(ov::npuw::AddPositionIdsParam().run_on_model(model));
@@ -337,7 +471,7 @@ TEST(AddPositionIdsParamTest, OffsetAddRopePathUsesPositionIds) {
// The Range must survive for Causal Mask creation even though the RoPE consumer it used to be
// identified by is now the Add rather than the Unsqueeze.
TEST(AddPositionIdsParamTest, OffsetAddPreservesRangeForCausalMask) {
- auto model = build_model_with_lfm2_like_pattern(true /*offset_positions*/, false /*with_clamp*/);
+ auto model = build_model_with_lfm2_like_pattern(true /*offset_positions*/, false /*with_range_clamp_consumer*/);
ASSERT_NO_THROW(ov::npuw::AddPositionIdsParam().run_on_model(model));
EXPECT_GE(count_ops_of_type(model, "Range"), 1u) << "Range node should be preserved for causal mask";
@@ -373,7 +507,7 @@ TEST(AddPositionIdsParamTest, OffsetAddPreservesRangeForCausalMask) {
// Clamp rewiring now keys off the Clamp itself rather than off skipping the RoPE consumer, so it
// has to keep working when the positions reach RoPE through an Add.
TEST(AddPositionIdsParamTest, OffsetAddWithClampReplacesClampInput) {
- auto model = build_model_with_lfm2_like_pattern(true /*offset_positions*/, true /*with_clamp*/);
+ auto model = build_model_with_lfm2_like_pattern(true /*offset_positions*/, true /*with_range_clamp_consumer*/);
ASSERT_NO_THROW(ov::npuw::AddPositionIdsParam().run_on_model(model));
EXPECT_TRUE(has_parameter_named(model, "position_ids"));
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
index 992aa180cbf..eeeb71ff179 100644
--- 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
@@ -267,6 +267,41 @@ std::shared_ptr<ov::Model> build_model_with_qwen35_like_cache(size_t num_layers
"model_with_qwen35_like_cache");
}
+// Builds a model matching the updated LFM2-1.2B IR (scaled down to 2 layers):
+// - 2 KV-cache layers: past_key_values.<idx>.key/value (f32, {?,8,?,64}) via beam_idx
+// - 2 Gated Short Convolution (conv) layers: cache_params.past.conv.<idx> (f32, {?,2048,3})
+// The distinction vs. the v1 builder is that the conv cache is now connected via
+// beam_idx Gather (matching the updated exporter behavior).
+std::shared_ptr<ov::Model> build_model_with_lfm2_v2_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) {
+ // Standard KV cache (via beam_idx Gather)
+ auto key_block = make_past_key_values_kv_state(
+ beam_idx, layer, "key", ov::element::f32, ov::PartialShape{-1, 8, -1, 64}, {1, 8, 0, 64});
+ auto value_block = make_past_key_values_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);
+
+ // Conv cache now connected via beam_idx Gather (updated IR behavior).
+ auto conv_block = make_cache_params_lin_state(
+ beam_idx, layer, "conv", ov::element::f32, ov::PartialShape{-1, 2048, 3}, {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_v2_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()) {
@@ -627,4 +662,59 @@ TEST(StatefulToStatelessTest, Qwen35_ShapesAndElementTypesPreserved) {
}
}
}
+
+// ===================== LFM2 v2 (updated IR) tests =====================
+// The updated LFM2 IR now connects the Conv cache via beam_idx Gather (the v1
+// LFM2 IR connected it directly to ReadValue). For StatefulToStateless this is
+// the only structural change that matters -- the pass must pick up the new conv
+// cache through the same beam_idx traversal as the KV caches, remove the sinks
+// and variables, and produce stateless inputs/outputs with the expected names.
+
+TEST(StatefulToStatelessTest, LFM2v2_ConvertsToStateless) {
+ auto model = build_model_with_lfm2_v2_like_cache();
+
+ // 2 layers * (key + value + conv) = 6 sinks / variables
+ 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);
+ EXPECT_FALSE(has_input_with_name(model, "beam_idx"));
+
+ 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"));
+
+ EXPECT_TRUE(has_input_with_name(model, "cache_params.past.conv." + idx));
+ EXPECT_TRUE(has_output_with_name(model, "cache_params.present.conv." + idx));
+ }
+}
+
+// Shapes and element type of the new past.conv Parameter must be preserved from
+// the original variable info: {?, 2048, 3}, f32.
+TEST(StatefulToStatelessTest, LFM2v2_ConvCacheShapeAndTypePreserved) {
+ auto model = build_model_with_lfm2_v2_like_cache(1);
+ ASSERT_NO_THROW(ov::pass::StatefulToStateless().run_on_model(model));
+
+ bool checked = false;
+ for (const auto& input : model->inputs()) {
+ const auto& name = input.get_any_name();
+ if (name.find("cache_params.past.conv") == std::string::npos) {
+ continue;
+ }
+ const auto& shape = input.get_partial_shape();
+ ASSERT_EQ(shape.rank().get_length(), 3) << "Conv input " << name << " should be rank 3";
+ EXPECT_TRUE(shape[0].is_dynamic()) << "Batch dim should be dynamic";
+ EXPECT_EQ(shape[1].get_length(), 2048) << "Conv 1st dim mismatch";
+ EXPECT_EQ(shape[2].get_length(), 3) << "Conv kernel size mismatch";
+ EXPECT_EQ(input.get_element_type(), ov::element::f32) << "Element type mismatch";
+ checked = true;
+ }
+ EXPECT_TRUE(checked) << "cache_params.past.conv.0 input not found";
+}
} // namespace