Skip to content

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

support gguf frontend stateful node (#39269)

Wenzeng Chen569fe8d7e523
diff --git a/src/core/src/pass/stateful_to_stateless.cpp b/src/core/src/pass/stateful_to_stateless.cpp
index 5bc3705f9f8..0e2f1845147 100644
--- a/src/core/src/pass/stateful_to_stateless.cpp
+++ b/src/core/src/pass/stateful_to_stateless.cpp
@@ -37,6 +37,8 @@ struct Variable {
// cache_params.past.key.0cache_params.present.key.0)
const std::regex lin_cache_naming_convention = std::regex(
R"((cache_params)\.past\.(key|value|conv|ssm)\.(\d+)cache_params\.present\.(key|value|conv|ssm)\.(\d+))");
+ // Pattern 3: GGUF frontend variable ids, e.g. cache_k_l3, cache_v_l3, conv_state_l0, ssm_state_l0
+ const std::regex gguf_naming_convention = std::regex(R"((cache_k|cache_v|conv_state|ssm_state)_l(\d+))");
};
Variable(const Context& context, const std::string& variable_name) : variable_name(variable_name) {
@@ -87,6 +89,22 @@ struct Variable {
} else {
index = -1;
}
+ } else if (std::regex_match(variable_name, match, context.gguf_naming_convention)) {
+ const std::string type = match[1].str();
+ const std::string layer_idx = match[2].str();
+ if (type == "cache_k" || type == "cache_v") {
+ const std::string key_or_value = type == "cache_k" ? "key" : "value";
+ input_name = "past_key_values." + layer_idx + "." + key_or_value;
+ output_name = "present." + layer_idx + "." + key_or_value;
+ index = layer_idx.length() <= std::numeric_limits<int>::digits10
+ ? std::stoi(layer_idx) * 2 + int(type == "cache_v") // order key before value
+ : -1;
+ } else {
+ const std::string state_type = type == "conv_state" ? "conv" : "ssm";
+ input_name = "cache_params.past." + state_type + "." + layer_idx;
+ output_name = "cache_params.present." + state_type + "." + layer_idx;
+ index = -1;
+ }
} else {
// Variable name doesn't follow the expected naming convention. It doens't prevent forming
// a correct stateless model but doesn't give a way to restore all names and inputs/outputs ordering
@@ -154,13 +172,15 @@ 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 caches in old LFM2 IRs.
+ // Process ReadValues that are NOT connected via beam_idx: Conv caches in old LFM2 IRs and
+ // recurrent states of GGUF models without beam reordering.
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();
std::smatch match;
if ((processed_variable_ids.find(variable_name) == processed_variable_ids.end()) &&
- std::regex_match(variable_name, match, context.lin_cache_naming_convention)) {
+ (std::regex_match(variable_name, match, context.lin_cache_naming_convention) ||
+ std::regex_match(variable_name, match, context.gguf_naming_convention))) {
variables.push_back(Variable(context, variable_name));
// For old LFM2 IR, ReadValue for Conv caches connects directly to the useful
// Ops after.
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 eeeb71ff179..a0d2cb8b1a2 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
@@ -4,6 +4,7 @@
#include <gtest/gtest.h>
+#include <algorithm>
#include <string>
#include <vector>
@@ -555,6 +556,107 @@ TEST(StatefulToStatelessTest, Qwen35_ConvertsToStateless) {
}
}
+// GGUF frontend variable ids (cache_k_lN, cache_v_lN, conv_state_lN, ssm_state_lN) are restored to optimum naming.
+TEST(StatefulToStatelessTest, GGUFFrontendNames_RestoredToOptimumNaming) {
+ 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 (const size_t layer : {3u, 11u}) {
+ const auto idx = std::to_string(layer);
+ sinks.push_back(make_cache_state(beam_idx,
+ "ssm_state_l" + idx,
+ ov::element::f32,
+ ov::PartialShape{-1, 48, 128, 128},
+ {1, 48, 128, 128})
+ .assign);
+ sinks.push_back(make_cache_state(beam_idx,
+ "conv_state_l" + idx,
+ ov::element::f32,
+ ov::PartialShape{-1, 10240, 4},
+ {1, 10240, 4})
+ .assign);
+ sinks.push_back(make_cache_state(beam_idx,
+ "cache_v_l" + idx,
+ ov::element::f32,
+ ov::PartialShape{-1, 4, -1, 256},
+ {1, 4, 0, 256})
+ .assign);
+ sinks.push_back(make_cache_state(beam_idx,
+ "cache_k_l" + idx,
+ ov::element::f32,
+ ov::PartialShape{-1, 4, -1, 256},
+ {1, 4, 0, 256})
+ .assign);
+ }
+ auto model = std::make_shared<ov::Model>(ov::ResultVector{std::make_shared<ov::op::v0::Result>(stub_input)},
+ sinks,
+ ov::ParameterVector{stub_input, beam_idx},
+ "model_with_gguf_like_cache");
+
+ ASSERT_NO_THROW(ov::pass::StatefulToStateless().run_on_model(model));
+
+ for (const std::string idx : {"3", "11"}) {
+ 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.ssm." + idx));
+ EXPECT_TRUE(has_output_with_name(model, "cache_params.present.ssm." + idx));
+ EXPECT_TRUE(has_input_with_name(model, "cache_params.past.conv." + idx));
+ EXPECT_TRUE(has_output_with_name(model, "cache_params.present.conv." + idx));
+ }
+
+ const auto input_names = collect_input_names(model);
+ const auto position = [&](const std::string& name) {
+ return std::find(input_names.begin(), input_names.end(), name) - input_names.begin();
+ };
+ EXPECT_LT(position("past_key_values.3.key"), position("past_key_values.3.value"));
+ EXPECT_LT(position("past_key_values.3.value"), position("past_key_values.11.key"));
+ EXPECT_LT(position("past_key_values.11.value"), position("cache_params.past.ssm.3"));
+}
+
+// GGUF recurrent states that bypass the beam_idx Gather are converted as well.
+TEST(StatefulToStatelessTest, GGUFFrontendNames_StatesWithoutBeamGatherAreConverted) {
+ 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;
+ sinks.push_back(make_cache_state(beam_idx,
+ "cache_k_l3",
+ ov::element::f32,
+ ov::PartialShape{-1, 4, -1, 256},
+ {1, 4, 0, 256})
+ .assign);
+ for (const size_t layer : {0u, 1u}) {
+ const auto idx = std::to_string(layer);
+ sinks.push_back(
+ make_cache_state("ssm_state_l" + idx, ov::element::f32, {1, 48, 128, 128}, {1, 48, 128, 128}).assign);
+ sinks.push_back(
+ make_cache_state("conv_state_l" + idx, ov::element::f32, {1, 10240, 4}, {1, 10240, 4}).assign);
+ }
+ auto model = std::make_shared<ov::Model>(ov::ResultVector{std::make_shared<ov::op::v0::Result>(stub_input)},
+ sinks,
+ ov::ParameterVector{stub_input, beam_idx},
+ "model_with_gguf_states_without_beam_gather");
+
+ ASSERT_NO_THROW(ov::pass::StatefulToStateless().run_on_model(model));
+
+ EXPECT_TRUE(model->get_sinks().empty());
+ EXPECT_TRUE(model->get_variables().empty());
+ EXPECT_TRUE(has_input_with_name(model, "past_key_values.3.key"));
+ for (const std::string idx : {"0", "1"}) {
+ EXPECT_TRUE(has_input_with_name(model, "cache_params.past.ssm." + idx));
+ EXPECT_TRUE(has_output_with_name(model, "cache_params.present.ssm." + idx));
+ 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();