Skip to content

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

Enhance GroupQueryAttention handling for empty past tokens and update… (#38801)

liang89238539c065
diff --git a/src/common/transformations/src/transformations/op_conversions/group_query_attention_decomposition.cpp b/src/common/transformations/src/transformations/op_conversions/group_query_attention_decomposition.cpp
index 1e1450f8daf..abc59d8c6dc 100644
--- a/src/common/transformations/src/transformations/op_conversions/group_query_attention_decomposition.cpp
+++ b/src/common/transformations/src/transformations/op_conversions/group_query_attention_decomposition.cpp
@@ -136,8 +136,12 @@ ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(
auto Q = get_input(GQAInputs::QUERY);
auto K = get_input(GQAInputs::KEY);
auto V = get_input(GQAInputs::VALUE);
- auto past_key = get_input(GQAInputs::PAST_KEY);
- auto past_value = get_input(GQAInputs::PAST_VALUE);
+ const bool has_past = has_input(GQAInputs::PAST_KEY);
+ ov::Output<ov::Node> past_key, past_value;
+ if (has_past) {
+ past_key = get_input(GQAInputs::PAST_KEY);
+ past_value = get_input(GQAInputs::PAST_VALUE);
+ }
auto seqlens_k = get_input(GQAInputs::SEQLENS_K);
const auto metadata = create_metadata();
@@ -148,7 +152,7 @@ ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(
const auto kv_cache_bit_width = node->get_kv_cache_bit_width();
const auto k_quant_type = node->get_k_quant_type();
const auto v_quant_type = node->get_v_quant_type();
- const auto kv_cache_type = past_key.get_element_type();
+ const auto kv_cache_type = node->get_output_element_type(1);
// Get k_scale and v_scale from their actual input indices.
// Note: validate_and_infer_types() already verified these indices are valid when kv_quantized is true,
@@ -262,6 +266,9 @@ ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(
K = rotaryEmbedding(K, cos, sin, rotary_interleaved);
}
}
+ // Absent past inputs and zero-capacity caches both use current K/V as the complete cache.
+ const bool empty_past =
+ !has_past || (past_key.get_partial_shape()[2].is_static() && past_key.get_partial_shape()[2].get_length() == 0);
// Quantize-on-write: when the cache is quantized, quantize the (post-RoPE) current K/V into the cache type
// before appending them, so the assembled present cache stays quantized and the past bytes are preserved
@@ -288,6 +295,10 @@ ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(
V = past_value;
present_k = past_key;
present_v = past_value;
+ } else if (empty_past) {
+ // With no past tokens, the current K/V already are the complete present cache.
+ present_k = K;
+ present_v = V;
} else {
const auto cache = batched ? construct_batched_kvcache(node,
past_key,
@@ -327,7 +338,7 @@ ov::OutputVector ov::pass::GroupQueryAttentionDecomposition::decompose(
V = dequantize_kv(V, metadata->v_scale, kv_num_heads, kv_cache_bit_width, v_quant_type, T);
}
- const auto concat_kv_len = get_dimensions(K, {2});
+ const auto concat_kv_len = get_dimensions(register_new_node<v3::ShapeOf>(K), {2});
const auto concat_kv_len_scalar = register_new_node<v0::Squeeze>(concat_kv_len);
// Broadcast KV if grouped query attention
diff --git a/src/common/transformations/tests/op_conversions/group_query_attention_decomposition_test.cpp b/src/common/transformations/tests/op_conversions/group_query_attention_decomposition_test.cpp
index aafadde1ae7..a47ead106fe 100644
--- a/src/common/transformations/tests/op_conversions/group_query_attention_decomposition_test.cpp
+++ b/src/common/transformations/tests/op_conversions/group_query_attention_decomposition_test.cpp
@@ -47,7 +47,7 @@ using ov::op::internal::GroupQueryAttention;
// Graph-level tests for GroupQueryAttentionDecomposition: the internal op is replaced by a
// ScaledDotProductAttention-based decomposition, and each feature emits its expected structure
-// (sliding-window cache -> ScatterUpdate; smooth_softmax / head_sink -> SDPA sink input). Numerical
+// (non-empty sliding-window cache -> ScatterUpdate; smooth_softmax / head_sink -> SDPA sink input). Numerical
// parity against ONNX Runtime is covered by the end-to-end tests in onnx_import_com_microsoft.
namespace {
@@ -103,7 +103,7 @@ struct GqaParams {
local_window_size = w;
sliding_window_cache = true;
past_len = capacity;
- expects_scatter_update = true;
+ expects_scatter_update = !capacity.is_static() || capacity.get_length() > 0;
return *this;
}
GqaParams& sink_smooth() {
@@ -138,7 +138,7 @@ struct GqaParams {
GqaParams& shape(const Dimension& seq, const Dimension& past) {
seq_len = seq;
past_len = past;
- expects_scatter_update = past.is_static();
+ expects_scatter_update = past.is_static() && past.get_length() > 0;
return *this;
}
};
@@ -248,10 +248,20 @@ class GroupQueryAttentionDecompositionTest : public testing::TestWithParam<GqaPa
TEST_P(GroupQueryAttentionDecompositionTest, decomposes_to_sdpa) {
const auto& p = GetParam();
auto model = make_gqa_model(p);
+ const auto present_key_shape_before = model->get_output_partial_shape(1);
+ const auto present_value_shape_before = model->get_output_partial_shape(2);
pass::Manager manager;
manager.register_pass<pass::GroupQueryAttentionDecomposition>();
manager.run_passes(model);
+ if (p.name == "empty_past_windowed_cache") {
+ const auto expected_shape = PartialShape{1, KV_NUM_HEADS, 1, HEAD_SIZE};
+ EXPECT_EQ(present_key_shape_before, expected_shape);
+ EXPECT_EQ(present_value_shape_before, expected_shape);
+ EXPECT_EQ(model->get_output_partial_shape(1), present_key_shape_before);
+ EXPECT_EQ(model->get_output_partial_shape(2), present_value_shape_before);
+ }
+
// The internal op is always replaced by exactly one ScaledDotProductAttention.
EXPECT_EQ(count_ops_of_type<GroupQueryAttention>(model), 0u);
EXPECT_EQ(count_ops_of_type<op::v13::ScaledDotProductAttention>(model), 1u);
@@ -263,8 +273,8 @@ TEST_P(GroupQueryAttentionDecompositionTest, decomposes_to_sdpa) {
}
}
- // The windowed rolling / static-cache paths assemble the present buffer with a ScatterUpdate along the
- // sequence axis (2); other cache paths do not. (Partial rotary also emits an unrelated ScatterUpdate to
+ // The non-empty windowed rolling / static-cache paths assemble the present buffer with a ScatterUpdate
+ // along the sequence axis (2); other cache paths do not. (Partial rotary also emits an unrelated ScatterUpdate to
// re-attach pass-through channels along the last (channel) axis - filter cache writes by scatter axis.)
bool has_cache_scatter_update = false;
for (const auto& op : model->get_ordered_ops()) {
@@ -288,6 +298,7 @@ INSTANTIATE_TEST_SUITE_P(GroupQueryAttentionDecomposition,
GqaParams{"partial_rotary"}.rotary(/*interleaved*/ false, HEAD_SIZE / 2),
GqaParams{"partial_rotary_interleaved"}.rotary(/*interleaved*/ true,
HEAD_SIZE / 2),
+ GqaParams{"empty_past"}.shape(1, 0),
GqaParams{"static_past_scatter"}.shape(1, 8),
GqaParams{"static_past_scatter_bias"}.shape(1, 8).bias(5),
GqaParams{"sliding_window"}.window(2),
@@ -299,6 +310,7 @@ INSTANTIATE_TEST_SUITE_P(GroupQueryAttentionDecomposition,
GqaParams{"i4_per_tensor"}.quant(4, element::u8),
GqaParams{"f8e4m3_per_tensor"}.quant(8, element::f8e4m3),
GqaParams{"windowed_cache"}.windowed_roll(3, 4),
+ GqaParams{"empty_past_windowed_cache"}.windowed_roll(3, 0),
GqaParams{"bidirectional"}.bidirectional()),
[](const testing::TestParamInfo<GqaParams>& i) {
return i.param.name;
@@ -537,6 +549,73 @@ TEST(GroupQueryAttentionValues, shared_kv_attends_to_past_and_returns_it_as_pres
EXPECT_TRUE(params[1]->output(0).get_target_inputs().empty());
}
+TEST(GroupQueryAttentionValues, absent_past_returns_current_kv_as_present) {
+ const auto query = std::make_shared<op::v0::Parameter>(element::f32, Shape{1, 4, 3, 8});
+ const auto key = std::make_shared<op::v0::Parameter>(element::f32, Shape{1, 2, 3, 8});
+ const auto value = std::make_shared<op::v0::Parameter>(element::f32, Shape{1, 2, 3, 8});
+ const auto empty_past = op::v0::Constant::create(element::f32, Shape{1, 2, 0, 8}, {});
+ const auto seqlens_k = std::make_shared<op::v0::Parameter>(element::i32, Shape{1});
+ const auto total_length = std::make_shared<op::v0::Parameter>(element::i32, Shape{1});
+ const auto gqa = std::make_shared<GroupQueryAttention>(
+ OutputVector{query, key, value, empty_past, empty_past, seqlens_k, total_length},
+ 4,
+ 2,
+ 0.0f,
+ false,
+ false);
+ auto model = std::make_shared<Model>(gqa->outputs(), ParameterVector{query, key, value, seqlens_k, total_length});
+ decompose(model);
+
+ ASSERT_NE(find_sdpa(model), nullptr);
+ EXPECT_EQ(model->get_results()[1]->get_input_node_shared_ptr(0), key);
+ EXPECT_EQ(model->get_results()[2]->get_input_node_shared_ptr(0), value);
+}
+
+TEST(GroupQueryAttentionValues, absent_quantized_past_returns_quantized_current_kv) {
+ const auto query = std::make_shared<op::v0::Parameter>(element::f32, Shape{1, 4, 3, 8});
+ const auto key = std::make_shared<op::v0::Parameter>(element::f32, Shape{1, 2, 3, 8});
+ const auto value = std::make_shared<op::v0::Parameter>(element::f32, Shape{1, 2, 3, 8});
+ const auto empty_past = op::v0::Constant::create(element::i8, Shape{1, 2, 0, 8}, {});
+ const auto seqlens_k = std::make_shared<op::v0::Parameter>(element::i32, Shape{1});
+ const auto total_length = std::make_shared<op::v0::Parameter>(element::i32, Shape{1});
+ const auto absent = op::v0::Constant::create(element::dynamic, Shape{0}, {});
+ const auto k_scale = std::make_shared<op::v0::Parameter>(element::f32, Shape{});
+ const auto v_scale = std::make_shared<op::v0::Parameter>(element::f32, Shape{});
+ const auto per_tensor = op::internal::GroupQueryAttentionQuantType::PER_TENSOR;
+ const auto gqa = std::make_shared<GroupQueryAttention>(OutputVector{query,
+ key,
+ value,
+ empty_past,
+ empty_past,
+ seqlens_k,
+ total_length,
+ absent,
+ absent,
+ absent,
+ absent,
+ absent,
+ k_scale,
+ v_scale},
+ 4,
+ 2,
+ 0.0f,
+ false,
+ false,
+ 8,
+ per_tensor,
+ per_tensor);
+ auto model = std::make_shared<Model>(gqa->outputs(),
+ ParameterVector{query, key, value, seqlens_k, total_length, k_scale, v_scale});
+ decompose(model);
+
+ for (size_t port : {1, 2}) {
+ const auto present = model->get_results()[port]->get_input_node_shared_ptr(0);
+ EXPECT_TRUE(is_type<op::v0::Convert>(present)) << "present must be the quantized current K/V";
+ EXPECT_EQ(present->get_output_element_type(0), element::i8);
+ EXPECT_EQ(present->get_output_partial_shape(0), (PartialShape{1, 2, 3, 8}));
+ }
+}
+
TEST(GroupQueryAttentionValues, softcap_grouped_query_does_not_replicate_kv) {
auto model = make_gqa_model(GqaParams{"softcap_gqa"}.soft_cap(30.0f).shape(1, Dimension::dynamic()));
decompose(model);
diff --git a/src/core/src/op/group_query_attention.cpp b/src/core/src/op/group_query_attention.cpp
index 4d2d70c71f5..fb0a1fc73ba 100644
--- a/src/core/src/op/group_query_attention.cpp
+++ b/src/core/src/op/group_query_attention.cpp
@@ -141,12 +141,25 @@ void GroupQueryAttention::validate_and_infer_types() {
return "unknown";
};
+ const auto is_present = [&](GroupQueryAttentionInputs input) {
+ const auto pos = static_cast<size_t>(input);
+ return (pos < get_input_size()) && !ov::util::is_empty_constant_tensor(input_value(pos));
+ };
+ const auto is_typed_empty_past = [&](GroupQueryAttentionInputs input) {
+ const auto pos = static_cast<size_t>(input);
+ if (pos >= get_input_size() || !ov::util::is_empty_constant_tensor(input_value(pos))) {
+ return false;
+ }
+ const auto& shape = get_input_partial_shape(pos);
+ return shape.rank().is_static() && shape.rank().get_length() == 4 && get_input_element_type(pos).is_static();
+ };
+
const auto check_input = [&](GroupQueryAttentionInputs input,
std::initializer_list<Rank> allowed_ranks,
const std::vector<element::Type>& allowed_types,
bool required = true) {
const auto pos = static_cast<size_t>(input);
- const bool present = (pos < get_input_size()) && !ov::util::is_empty_constant_tensor(input_value(pos));
+ const bool present = is_present(input);
NODE_VALIDATION_CHECK(this,
!required || present,
"GroupQueryAttention requires ",
@@ -207,12 +220,29 @@ void GroupQueryAttention::validate_and_infer_types() {
check_input(GroupQueryAttentionInputs::QUERY, {4}, float_types);
check_input(GroupQueryAttentionInputs::KEY, {4}, float_types);
check_input(GroupQueryAttentionInputs::VALUE, {4}, float_types);
+ // An absent past (empty Constant) is an empty KV cache; past_key and past_value come as a pair.
check_input(GroupQueryAttentionInputs::PAST_KEY,
{4},
- {element::f32, element::f16, element::bf16, element::i8, element::u8, element::f8e4m3});
+ {element::f32, element::f16, element::bf16, element::i8, element::u8, element::f8e4m3},
+ false);
check_input(GroupQueryAttentionInputs::PAST_VALUE,
{4},
- {element::f32, element::f16, element::bf16, element::i8, element::u8, element::f8e4m3});
+ {element::f32, element::f16, element::bf16, element::i8, element::u8, element::f8e4m3},
+ false);
+ // RemoveEmptyKVInputs replaces a zero-sized past Parameter with a rank-4 Constant of the same type and shape.
+ // The frontend uses a rank-1 {0} Constant when ONNX omits past; infer its layout from KEY, but reject quantization
+ // because the floating-point KEY cannot identify the cache type.
+ const bool has_past = is_present(GroupQueryAttentionInputs::PAST_KEY);
+ const bool has_past_value = is_present(GroupQueryAttentionInputs::PAST_VALUE);
+ NODE_VALIDATION_CHECK(this,
+ has_past == has_past_value,
+ "GroupQueryAttention expects past_key and past_value to be both present or both absent");
+ const bool has_past_layout = has_past || is_typed_empty_past(GroupQueryAttentionInputs::PAST_KEY);
+ const bool has_past_value_layout = has_past_value || is_typed_empty_past(GroupQueryAttentionInputs::PAST_VALUE);
+ NODE_VALIDATION_CHECK(this,
+ has_past_layout == has_past_value_layout,
+ "GroupQueryAttention expects past_key and past_value to both provide cache layout "
+ "information or neither");
check_input(GroupQueryAttentionInputs::SEQLENS_K, {1, 2}, integral_types);
// output_qk is laid out over total_sequence_length (ORT), so qk_output needs it.
check_input(GroupQueryAttentionInputs::TOTAL_SEQUENCE_LENGTH,
@@ -277,8 +307,6 @@ void GroupQueryAttention::validate_and_infer_types() {
get_input_element_type(pos));
}
}
- const auto past_k_shape = get_input_partial_shape(static_cast<size_t>(GroupQueryAttentionInputs::PAST_KEY));
-
const auto& q_type = get_input_element_type(static_cast<size_t>(GroupQueryAttentionInputs::QUERY));
const auto& batch_size = q_shape[0];
@@ -367,14 +395,29 @@ void GroupQueryAttention::validate_and_infer_types() {
// The KV cache (past_key/past_value, input 3/4) may be quantized. present_key/present_value inherit the
// cache element type so a quantized (i8/u8/f8e4m3) cache round-trips from past to present, matching the ONNX spec.
- const auto& kv_cache_type = get_input_element_type(static_cast<size_t>(GroupQueryAttentionInputs::PAST_KEY));
- const auto& past_value_type = get_input_element_type(static_cast<size_t>(GroupQueryAttentionInputs::PAST_VALUE));
NODE_VALIDATION_CHECK(this,
- kv_cache_type.compatible(past_value_type),
- "GroupQueryAttention expects past_key and past_value element types to match, got ",
- kv_cache_type,
- " and ",
- past_value_type);
+ !m_sliding_window_cache || has_past || has_past_layout,
+ "GroupQueryAttention requires past_key and past_value with a known layout for a windowed "
+ "KV cache");
+ NODE_VALIDATION_CHECK(this,
+ !is_shared_kv() || has_past,
+ "GroupQueryAttention requires non-empty past_key and past_value for shared KV");
+ NODE_VALIDATION_CHECK(this,
+ has_past_layout || !is_kv_quantized(),
+ "GroupQueryAttention with a quantized KV cache and no past requires a typed rank-4 empty "
+ "past_key and past_value");
+ const auto kv_cache_type = get_input_element_type(
+ static_cast<size_t>(has_past_layout ? GroupQueryAttentionInputs::PAST_KEY : GroupQueryAttentionInputs::KEY));
+ if (has_past_layout) {
+ const auto& past_value_type =
+ get_input_element_type(static_cast<size_t>(GroupQueryAttentionInputs::PAST_VALUE));
+ NODE_VALIDATION_CHECK(this,
+ kv_cache_type.compatible(past_value_type),
+ "GroupQueryAttention expects past_key and past_value element types to match, got ",
+ kv_cache_type,
+ " and ",
+ past_value_type);
+ }
if (is_kv_quantized()) {
NODE_VALIDATION_CHECK(
this,
@@ -405,6 +448,14 @@ void GroupQueryAttention::validate_and_infer_types() {
// present_key/present_value keep the past KV layout: kv head size may differ from query head size
// when the cache is quantized (e.g. 4-bit values packed two per byte).
+ const auto& key_ps = get_input_partial_shape(static_cast<size_t>(GroupQueryAttentionInputs::KEY));
+ const auto past_key_pos = static_cast<size_t>(GroupQueryAttentionInputs::PAST_KEY);
+ const auto past_k_shape = has_past_layout
+ ? get_input_partial_shape(past_key_pos)
+ : PartialShape{Dimension::dynamic(),
+ m_kv_num_heads,
+ 0,
+ key_ps.rank().is_static() ? key_ps[3] : Dimension::dynamic()};
auto kv_shape = PartialShape{batch_size, m_kv_num_heads, past_k_shape[2], past_k_shape[3]};
auto& output_kv_len = kv_shape[2];
// A windowed KV cache keeps the past buffer's own (capacity) sequence dimension: it rolls in place
@@ -412,9 +463,9 @@ void GroupQueryAttention::validate_and_infer_types() {
// A zero-capacity past (absent/empty cache) cannot be written in place either, so present grows by the step.
// Shared KV (statically empty key, kv_sequence_length == 0) appends nothing: present is the past as is.
// A key/value of dynamic length cannot be written in place, so present grows by its own length.
- const auto& key_ps = get_input_partial_shape(static_cast<size_t>(GroupQueryAttentionInputs::KEY));
+ const bool empty_past = output_kv_len.is_static() && output_kv_len.get_length() == 0;
const bool dynamic_kv_len = key_ps.rank().is_static() && key_ps[2].is_dynamic();
- if (!m_sliding_window_cache && !is_shared_kv() && !has_fixed_kv_capacity()) {
+ if ((!m_sliding_window_cache || empty_past) && !is_shared_kv() && !has_fixed_kv_capacity()) {
output_kv_len += dynamic_kv_len ? key_ps[2] : sequence_len;
}
diff --git a/src/core/tests/type_prop/group_query_attention.cpp b/src/core/tests/type_prop/group_query_attention.cpp
index 282738f8edf..b94d0004e9a 100644
--- a/src/core/tests/type_prop/group_query_attention.cpp
+++ b/src/core/tests/type_prop/group_query_attention.cpp
@@ -149,6 +149,87 @@ TEST(type_prop, group_query_attention_dynamic_kv_len_accumulates) {
EXPECT_EQ(op->get_output_partial_shape(2), (PartialShape{1, 2, {6, 13}, 8}));
}
+TEST(type_prop, group_query_attention_absent_past_is_empty_cache) {
+ auto args = make_valid_gqa_args();
+ const auto empty = op::v0::Constant::create(element::f32, Shape{1, 2, 0, 8}, {});
+ args[static_cast<size_t>(op::internal::GroupQueryAttentionInputs::PAST_KEY)] = empty;
+ args[static_cast<size_t>(op::internal::GroupQueryAttentionInputs::PAST_VALUE)] = empty;
+
+ const auto op = std::make_shared<op::internal::GroupQueryAttention>(args, 6, 2, 1.0f, false, false);
+
+ EXPECT_EQ(op->get_output_element_type(1), element::f32);
+ EXPECT_EQ(op->get_output_partial_shape(1), (PartialShape{1, 2, 4, 8}));
+ EXPECT_EQ(op->get_output_partial_shape(2), (PartialShape{1, 2, 4, 8}));
+}
+
+TEST(type_prop, group_query_attention_absent_past_key_only) {
+ auto args = make_valid_gqa_args();
+ args[static_cast<size_t>(op::internal::GroupQueryAttentionInputs::PAST_KEY)] =
+ op::v0::Constant::create(element::f32, Shape{1, 2, 0, 8}, {});
+
+ OV_EXPECT_THROW(std::ignore = std::make_shared<op::internal::GroupQueryAttention>(args, 6, 2, 1.0f, false, false),
+ ov::NodeValidationFailure,
+ HasSubstr("both present or both absent"));
+}
+
+TEST(type_prop, group_query_attention_absent_typed_past_with_quantized_cache) {
+ auto args = make_valid_gqa_quant_args(element::i8, 8);
+ const auto empty = op::v0::Constant::create(element::i8, Shape{1, 2, 0, 8}, {});
+ args[static_cast<size_t>(op::internal::GroupQueryAttentionInputs::PAST_KEY)] = empty;
+ args[static_cast<size_t>(op::internal::GroupQueryAttentionInputs::PAST_VALUE)] = empty;
+ const auto quantize_type = op::internal::GroupQueryAttentionQuantType::PER_TENSOR;
+
+ const auto op = std::make_shared<op::internal::GroupQueryAttention>(args,
+ 6,
+ 2,
+ 1.0f,
+ false,
+ false,
+ 8,
+ quantize_type,
+ quantize_type);
+
+ EXPECT_EQ(op->get_output_element_type(1), element::i8);
+ EXPECT_EQ(op->get_output_element_type(2), element::i8);
+ EXPECT_EQ(op->get_output_partial_shape(1), (PartialShape{1, 2, 4, 8}));
+}
+
+TEST(type_prop, group_query_attention_absent_typed_past_with_4bit_cache) {
+ auto args = make_valid_gqa_quant_args(element::u8, 4);
+ const auto empty = op::v0::Constant::create(element::u8, Shape{1, 2, 0, 4}, {});
+ args[static_cast<size_t>(op::internal::GroupQueryAttentionInputs::PAST_KEY)] = empty;
+ args[static_cast<size_t>(op::internal::GroupQueryAttentionInputs::PAST_VALUE)] = empty;
+ const auto quantize_type = op::internal::GroupQueryAttentionQuantType::PER_TENSOR;
+
+ const auto op = std::make_shared<op::internal::GroupQueryAttention>(args,
+ 6,
+ 2,
+ 1.0f,
+ false,
+ false,
+ 4,
+ quantize_type,
+ quantize_type);
+
+ EXPECT_EQ(op->get_output_element_type(1), element::u8);
+ EXPECT_EQ(op->get_output_partial_shape(1), (PartialShape{1, 2, 4, 4}));
+ EXPECT_EQ(op->get_output_partial_shape(2), (PartialShape{1, 2, 4, 4}));
+}
+
+TEST(type_prop, group_query_attention_absent_unshaped_past_with_quantized_cache) {
+ auto args = make_valid_gqa_quant_args(element::i8, 8);
+ const auto empty = op::v0::Constant::create(element::i8, Shape{0}, {});
+ args[static_cast<size_t>(op::internal::GroupQueryAttentionInputs::PAST_KEY)] = empty;
+ args[static_cast<size_t>(op::internal::GroupQueryAttentionInputs::PAST_VALUE)] = empty;
+ const auto quantize_type = op::internal::GroupQueryAttentionQuantType::PER_TENSOR;
+
+ OV_EXPECT_THROW(
+ std::ignore = std::make_shared<
+ op::internal::GroupQueryAttention>(args, 6, 2, 1.0f, false, false, 8, quantize_type, quantize_type),
+ ov::NodeValidationFailure,
+ HasSubstr("typed rank-4 empty past"));
+}
+
TEST(type_prop, group_query_attention_invalid_query_rank) {
auto args = make_valid_gqa_args();
args[0] = std::make_shared<op::v0::Parameter>(element::f32, PartialShape{1, 6, 8});
@@ -457,6 +538,21 @@ TEST(type_prop, group_query_attention_shared_kv_present_is_past) {
EXPECT_EQ(op->get_output_partial_shape(1), (PartialShape{1, 2, -1, 8}));
}
+TEST(type_prop, group_query_attention_shared_kv_requires_non_empty_past) {
+ auto args = make_valid_gqa_args();
+ args[static_cast<size_t>(op::internal::GroupQueryAttentionInputs::KEY)] =
+ std::make_shared<op::v0::Parameter>(element::f32, PartialShape{1, 2, 0, 8});
+ args[static_cast<size_t>(op::internal::GroupQueryAttentionInputs::VALUE)] =
+ std::make_shared<op::v0::Parameter>(element::f32, PartialShape{1, 2, 0, 8});
+ const auto empty_past = op::v0::Constant::create(element::f32, Shape{1, 2, 0, 8}, {});
+ args[static_cast<size_t>(op::internal::GroupQueryAttentionInputs::PAST_KEY)] = empty_past;
+ args[static_cast<size_t>(op::internal::GroupQueryAttentionInputs::PAST_VALUE)] = empty_past;
+
+ OV_EXPECT_THROW(std::ignore = std::make_shared<op::internal::GroupQueryAttention>(args, 6, 2, 1.0f, false, false),
+ ov::NodeValidationFailure,
+ HasSubstr("non-empty past_key and past_value for shared KV"));
+}
+
TEST(type_prop, group_query_attention_regular_kv_is_not_shared) {
const auto op =
std::make_shared<op::internal::GroupQueryAttention>(make_valid_gqa_args(), 6, 2, 1.0f, false, false);
diff --git a/src/frontends/onnx/frontend/src/op/com.microsoft/group_query_attention.cpp b/src/frontends/onnx/frontend/src/op/com.microsoft/group_query_attention.cpp
index 0d64db277a3..61d4835f9f5 100644
--- a/src/frontends/onnx/frontend/src/op/com.microsoft/group_query_attention.cpp
+++ b/src/frontends/onnx/frontend/src/op/com.microsoft/group_query_attention.cpp
@@ -15,7 +15,6 @@
#include "openvino/op/divide.hpp"
#include "openvino/op/reshape.hpp"
#include "openvino/op/shape_of.hpp"
-#include "openvino/op/slice.hpp"
#include "openvino/op/transpose.hpp"
#include "utils/attention.hpp"
#include "utils/common.hpp"
@@ -254,12 +253,8 @@ ov::OutputVector group_query_attention(const ov::frontend::onnx::Node& node) {
FRONT_END_OP_CONVERSION_CHECK(kv_cache_bit_width == 0,
"GroupQueryAttention: a quantized KV cache (kv_cache_bit_width != 0) requires "
"past_key and past_value.");
- // Zero-length [B, kv_num_heads, 0, head_size] past, sliced from the already transposed K so it carries the
- // batch/head/head_size dims and element type without extra shape arithmetic.
- const auto zero = v0::Constant::create(ov::element::i64, ov::Shape{1}, {0});
- const auto one = v0::Constant::create(ov::element::i64, ov::Shape{1}, {1});
- const auto seq_axis = v0::Constant::create(ov::element::i64, ov::Shape{1}, {2});
- const auto empty_past = std::make_shared<v8::Slice>(ov_op_inputs[1], zero, zero, one, seq_axis)->output(0);
+ // The empty Constant marks the past as absent; its type matches the current KV tensor.
+ const auto empty_past = v0::Constant::create(ov_op_inputs[1].get_element_type(), ov::Shape{0}, {})->output(0);
ov_op_inputs.push_back(empty_past);
ov_op_inputs.push_back(empty_past);
}
diff --git a/src/plugins/intel_npu/src/plugin/npuw/embedding/remove_empty_kv_inputs.cpp b/src/plugins/intel_npu/src/plugin/npuw/embedding/remove_empty_kv_inputs.cpp
index dc712df0fe7..f138243e88c 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/embedding/remove_empty_kv_inputs.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/embedding/remove_empty_kv_inputs.cpp
@@ -6,6 +6,7 @@
#include "../util.hpp"
#include "openvino/core/graph_util.hpp"
+#include "openvino/op/group_query_attention.hpp"
#include "openvino/op/ops.hpp"
#include "openvino/op/util/node_util.hpp"
#include "openvino/opsets/opset13.hpp"
@@ -36,6 +37,14 @@ auto match_down_up_convert_subgraph_after_lpt = [](const ov::Output<ov::Node>& i
return downconvert;
};
+constexpr size_t kPastKeyInput = static_cast<size_t>(ov::op::internal::GroupQueryAttentionInputs::PAST_KEY);
+constexpr size_t kPastValueInput = static_cast<size_t>(ov::op::internal::GroupQueryAttentionInputs::PAST_VALUE);
+
+bool is_empty_param(const ov::Output<ov::Node>& output) {
+ return ov::is_type<ov::op::v0::Parameter>(output.get_node()) && output.get_partial_shape().is_static() &&
+ ov::shape_size(output.get_shape()) == 0;
+}
+
// diagnostics warnings on OPENVINO_MATCHER_PASS_RTTI() definition: visibility hidden
#ifdef __GNUC__
# pragma GCC diagnostic push
@@ -113,6 +122,49 @@ public:
}
};
+class RemoveEmptyGQAPastInputs : public ov::pass::MatcherPass {
+public:
+ OPENVINO_MATCHER_PASS_RTTI("npuw::RemoveEmptyGQAPastInputs");
+
+ using Context = RemoveEmptyKVTensors::Context;
+
+ RemoveEmptyGQAPastInputs(Context::Ref ctx) {
+ // GQA has a variable number of inputs, so the past inputs are checked by a node predicate.
+ auto gqa = opp::wrap_type<ov::op::internal::GroupQueryAttention>(opp::op::Predicate(
+ [](const ov::Output<ov::Node>& output) {
+ const auto node = output.get_node();
+ for (size_t input_pos : {kPastKeyInput, kPastValueInput}) {
+ if (is_empty_param(node->input_value(input_pos))) {
+ return true;
+ }
+ }
+ return false;
+ },
+ "has_empty_past_param"));
+
+ auto callback = [=](opp::Matcher& m) {
+ auto matched_gqa = m.get_match_root();
+ bool changed = false;
+ for (size_t input_pos : {kPastKeyInput, kPastValueInput}) {
+ const auto past_output = matched_gqa->input_value(input_pos);
+ if (!is_empty_param(past_output)) {
+ continue;
+ }
+ auto past = ov::as_type_ptr<ov::op::v0::Parameter>(past_output.get_node_shared_ptr());
+ // An empty Constant marks the GQA past as absent, i.e. an empty KV cache.
+ auto empty = std::make_shared<ov::op::v0::Constant>(past->get_element_type(), past->get_shape());
+ empty->set_friendly_name(past->get_friendly_name());
+ ov::copy_runtime_info(past, empty);
+ ov::replace_node(past, empty);
+ ctx.get().old_params.push_back(past);
+ changed = true;
+ }
+ return changed;
+ };
+ register_matcher(std::make_shared<opp::Matcher>(gqa, "RemoveEmptyGQAPastInputs"), std::move(callback));
+ }
+};
+
#ifdef __GNUC__
# pragma GCC diagnostic pop
#endif
@@ -121,6 +173,7 @@ bool remove_empty_kv_inputs(std::shared_ptr<ov::Model> model) {
ov::pass::GraphRewrite rewr;
RemoveEmptyKVTensors::Context ctx;
rewr.add_matcher<RemoveEmptyKVTensors>(std::ref(ctx));
+ rewr.add_matcher<RemoveEmptyGQAPastInputs>(std::ref(ctx));
rewr.run_on_model(model);
for (auto old_param : ctx.old_params) {
model->remove_parameter(old_param);
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 14a06e056fd..c79ae60cb11 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
@@ -2,44 +2,134 @@
// SPDX-License-Identifier: Apache-2.0
//
+#include "embedding/remove_empty_kv_inputs.hpp"
+
#include <gtest/gtest.h>
-#include "embedding/remove_empty_kv_inputs.hpp"
+#include "common_test_utils/graph_comparator.hpp"
#include "llm_pass_test_fixture.hpp"
-#include "openvino/opsets/opset13.hpp"
+#include "openvino/op/concat.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/convert.hpp"
+#include "openvino/op/group_query_attention.hpp"
+#include "openvino/op/multiply.hpp"
+#include "openvino/op/parameter.hpp"
+#include "openvino/op/result.hpp"
+#include "openvino/op/shape_of.hpp"
namespace {
+using namespace ov::op;
+
class RemoveEmptyKVInputsPassTest : public ov::test::npuw::LLMPassTestFixture {};
+TEST_F(RemoveEmptyKVInputsPassTest, GQAEmptyPastInputsBecomeConstants) {
+ auto query = std::make_shared<v0::Parameter>(ov::element::f16, ov::Shape{1, 4, 1, 16});
+ auto key = std::make_shared<v0::Parameter>(ov::element::f16, ov::Shape{1, 2, 1, 16});
+ auto value = std::make_shared<v0::Parameter>(ov::element::f16, ov::Shape{1, 2, 1, 16});
+ auto past_key = std::make_shared<v0::Parameter>(ov::element::f16, ov::Shape{1, 2, 0, 16});
+ auto past_value = std::make_shared<v0::Parameter>(ov::element::f16, ov::Shape{1, 2, 0, 16});
+ auto seqlens = std::make_shared<v0::Parameter>(ov::element::i32, ov::Shape{1});
+ auto total_length = std::make_shared<v0::Parameter>(ov::element::i32, ov::Shape{1});
+ const auto make_model = [&](const ov::Output<ov::Node>& past_key_input,
+ const ov::Output<ov::Node>& past_value_input,
+ bool include_past_parameters) {
+ auto gqa = std::make_shared<ov::op::internal::GroupQueryAttention>(
+ ov::OutputVector{query, key, value, past_key_input, past_value_input, seqlens, total_length},
+ 4,
+ 2,
+ 0.0f,
+ false,
+ false);
+ ov::ParameterVector parameters{query, key, value};
+ if (include_past_parameters) {
+ parameters.push_back(past_key);
+ parameters.push_back(past_value);
+ }
+ parameters.push_back(seqlens);
+ parameters.push_back(total_length);
+ return std::make_shared<ov::Model>(ov::ResultVector{std::make_shared<v0::Result>(gqa->output(0)),
+ std::make_shared<v0::Result>(gqa->output(1)),
+ std::make_shared<v0::Result>(gqa->output(2))},
+ parameters);
+ };
+ auto model = make_model(past_key, past_value, true);
+ const auto empty_past_key = v0::Constant::create(ov::element::f16, ov::Shape{1, 2, 0, 16}, {});
+ const auto empty_past_value = v0::Constant::create(ov::element::f16, ov::Shape{1, 2, 0, 16}, {});
+ const auto model_ref = make_model(empty_past_key, empty_past_value, false);
+
+ ov::npuw::RemoveEmptyKVInputs pass;
+ EXPECT_TRUE(pass.run_on_model(model));
+ auto comparator = FunctionsComparator::with_default();
+ comparator.enable(FunctionsComparator::CONST_VALUES);
+ comparator.enable(FunctionsComparator::ATTRIBUTES);
+ const auto comparison = comparator.compare(model, model_ref);
+ EXPECT_TRUE(comparison.valid) << comparison.message;
+ EXPECT_FALSE(pass.run_on_model(model));
+}
+
+TEST_F(RemoveEmptyKVInputsPassTest, GQAEmptyPastWindowedCacheBecomesConstants) {
+ auto query = std::make_shared<v0::Parameter>(ov::element::f16, ov::Shape{1, 4, 1, 16});
+ auto key = std::make_shared<v0::Parameter>(ov::element::f16, ov::Shape{1, 2, 1, 16});
+ auto value = std::make_shared<v0::Parameter>(ov::element::f16, ov::Shape{1, 2, 1, 16});
+ auto past_key = std::make_shared<v0::Parameter>(ov::element::f16, ov::Shape{1, 2, 0, 16});
+ auto past_value = std::make_shared<v0::Parameter>(ov::element::f16, ov::Shape{1, 2, 0, 16});
+ auto seqlens = std::make_shared<v0::Parameter>(ov::element::i32, ov::Shape{1});
+ auto total_length = std::make_shared<v0::Parameter>(ov::element::i32, ov::Shape{1});
+ auto gqa = std::make_shared<ov::op::internal::GroupQueryAttention>(
+ ov::OutputVector{query, key, value, past_key, past_value, seqlens, total_length},
+ 4,
+ 2,
+ 0.0f,
+ false,
+ false,
+ 0,
+ ov::op::internal::GroupQueryAttentionQuantType::NONE,
+ ov::op::internal::GroupQueryAttentionQuantType::NONE,
+ 3,
+ true);
+ auto model = std::make_shared<ov::Model>(
+ ov::ResultVector{std::make_shared<v0::Result>(gqa->output(0)),
+ std::make_shared<v0::Result>(gqa->output(1)),
+ std::make_shared<v0::Result>(gqa->output(2))},
+ ov::ParameterVector{query, key, value, past_key, past_value, seqlens, total_length});
+
+ ov::npuw::RemoveEmptyKVInputs pass;
+ EXPECT_TRUE(pass.run_on_model(model));
+ EXPECT_EQ(model->get_parameters().size(), 5u);
+ EXPECT_EQ(model->outputs()[1].get_partial_shape(), (ov::PartialShape{1, 2, 1, 16}));
+ EXPECT_EQ(model->outputs()[2].get_partial_shape(), (ov::PartialShape{1, 2, 1, 16}));
+ EXPECT_TRUE(ov::is_type<v0::Constant>(gqa->input_value(3).get_node()));
+ EXPECT_TRUE(ov::is_type<v0::Constant>(gqa->input_value(4).get_node()));
+ EXPECT_FALSE(pass.run_on_model(model));
+}
+
// Direct unit test for RemoveEmptyKVInputs: only the minimal past/current KV concat subgraph
// is needed. This avoids relying on LLMCompiledModel orchestration to validate the matcher.
TEST_F(RemoveEmptyKVInputsPassTest, HandlesDownUpProjSubgraph) {
- using namespace ov::opset13;
-
- auto past_k = std::make_shared<Parameter>(ov::element::f8e4m3, ov::Shape{1, 4, 0, 16});
+ auto past_k = std::make_shared<v0::Parameter>(ov::element::f8e4m3, 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 current_k = std::make_shared<Parameter>(ov::element::f8e4m3, ov::Shape{1, 4, 1, 16});
+ auto current_k = std::make_shared<v0::Parameter>(ov::element::f8e4m3, ov::Shape{1, 4, 1, 16});
current_k->set_friendly_name("current_key_values.0.key");
current_k->output(0).set_names({"current_key_values.0.key"});
- auto upconvert = std::make_shared<Convert>(past_k, ov::element::f32);
- auto upscale = Constant::create(ov::element::f32, ov::Shape{}, {1.0f});
- auto upmul = std::make_shared<Multiply>(upconvert, upscale);
- auto downscale = Constant::create(ov::element::f32, ov::Shape{}, {1.0f});
- auto downmul = std::make_shared<Multiply>(upmul, downscale);
- auto downconvert = std::make_shared<Convert>(downmul, ov::element::f8e4m3);
+ auto upconvert = std::make_shared<v0::Convert>(past_k, ov::element::f32);
+ auto upscale = v0::Constant::create(ov::element::f32, ov::Shape{}, {1.0f});
+ auto upmul = std::make_shared<v1::Multiply>(upconvert, upscale);
+ auto downscale = v0::Constant::create(ov::element::f32, ov::Shape{}, {1.0f});
+ auto downmul = std::make_shared<v1::Multiply>(upmul, downscale);
+ auto downconvert = std::make_shared<v0::Convert>(downmul, ov::element::f8e4m3);
- auto concat = std::make_shared<Concat>(ov::OutputVector{downconvert, current_k}, 2);
+ auto concat = std::make_shared<v0::Concat>(ov::OutputVector{downconvert, current_k}, 2);
concat->set_friendly_name("past_key_values.0.keypresent.0.key_concat");
// ShapeOf on the original parameter exercises the replacement-to-constant path.
- auto shapeof = std::make_shared<ShapeOf>(past_k, ov::element::i64);
- auto concat_result = std::make_shared<Result>(concat);
+ auto shapeof = std::make_shared<v3::ShapeOf>(past_k, ov::element::i64);
+ auto concat_result = std::make_shared<v0::Result>(concat);
concat_result->output(0).set_names({"present.0.key"});
- auto shape_result = std::make_shared<Result>(shapeof);
+ auto shape_result = std::make_shared<v0::Result>(shapeof);
auto model = std::make_shared<ov::Model>(ov::ResultVector{concat_result, shape_result},
ov::ParameterVector{past_k, current_k},
"remove_empty_kv_inputs_test");
@@ -69,30 +159,28 @@ TEST_F(RemoveEmptyKVInputsPassTest, HandlesDownUpProjSubgraph) {
// └── concat2(axis=2) ── result2
// current_k2[1,4,1,16]──┘
TEST_F(RemoveEmptyKVInputsPassTest, SharedParam_TwoConcats_BothEliminated) {
- using namespace ov::opset13;
-
- auto past_k = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 4, 0, 16});
+ auto past_k = std::make_shared<v0::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 current_k1 = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 4, 1, 16});
+ auto current_k1 = std::make_shared<v0::Parameter>(ov::element::f32, ov::Shape{1, 4, 1, 16});
current_k1->set_friendly_name("current_key_values.0.key");
current_k1->output(0).set_names({"current_key_values.0.key"});
- auto current_k2 = std::make_shared<Parameter>(ov::element::f32, ov::Shape{1, 4, 1, 16});
+ auto current_k2 = std::make_shared<v0::Parameter>(ov::element::f32, ov::Shape{1, 4, 1, 16});
current_k2->set_friendly_name("current_key_values.1.key");
current_k2->output(0).set_names({"current_key_values.1.key"});
// Both concat nodes share the same empty past_k (Gemma4 shared KV-cache pattern).
- auto concat1 = std::make_shared<Concat>(ov::OutputVector{past_k, current_k1}, 2);
+ auto concat1 = std::make_shared<v0::Concat>(ov::OutputVector{past_k, current_k1}, 2);
concat1->set_friendly_name("past_key_values.0.key_concat1");
- auto concat2 = std::make_shared<Concat>(ov::OutputVector{past_k, current_k2}, 2);
+ auto concat2 = std::make_shared<v0::Concat>(ov::OutputVector{past_k, current_k2}, 2);
concat2->set_friendly_name("past_key_values.0.key_concat2");
- auto result1 = std::make_shared<Result>(concat1);
+ auto result1 = std::make_shared<v0::Result>(concat1);
result1->output(0).set_names({"present.0.key"});
- auto result2 = std::make_shared<Result>(concat2);
+ auto result2 = std::make_shared<v0::Result>(concat2);
result2->output(0).set_names({"present.1.key"});
auto model = std::make_shared<ov::Model>(ov::ResultVector{result1, result2},
@@ -118,42 +206,39 @@ TEST_F(RemoveEmptyKVInputsPassTest, SharedParam_TwoConcats_BothEliminated) {
// two independent LPT down/up-convert subgraphs before each Concat (the realistic
// quantised-KV path that triggers the LPT branch of the matcher).
TEST_F(RemoveEmptyKVInputsPassTest, SharedParam_TwoConcats_LptSubgraph_BothEliminated) {
- using namespace ov::opset13;
-
- auto past_k = std::make_shared<Parameter>(ov::element::f8e4m3, ov::Shape{1, 4, 0, 16});
+ auto past_k = std::make_shared<v0::Parameter>(ov::element::f8e4m3, 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 current_k1 = std::make_shared<Parameter>(ov::element::f8e4m3, ov::Shape{1, 4, 1, 16});
+ auto current_k1 = std::make_shared<v0::Parameter>(ov::element::f8e4m3, ov::Shape{1, 4, 1, 16});
current_k1->set_friendly_name("current_key_values.0.key");
current_k1->output(0).set_names({"current_key_values.0.key"});
- auto current_k2 = std::make_shared<Parameter>(ov::element::f8e4m3, ov::Shape{1, 4, 1, 16});
+ auto current_k2 = std::make_shared<v0::Parameter>(ov::element::f8e4m3, ov::Shape{1, 4, 1, 16});
current_k2->set_friendly_name("current_key_values.1.key");
current_k2->output(0).set_names({"current_key_values.1.key"});
// Build a minimal LPT down/up-convert subgraph rooted at `src`.
auto make_lpt = [](const std::shared_ptr<ov::Node>& src) -> std::shared_ptr<ov::Node> {
- using namespace ov::opset13;
- auto upconv = std::make_shared<Convert>(src, ov::element::f32);
- auto upscale = Constant::create(ov::element::f32, ov::Shape{}, {1.0f});
- auto upmul = std::make_shared<Multiply>(upconv, upscale);
- auto downscale = Constant::create(ov::element::f32, ov::Shape{}, {1.0f});
- auto downmul = std::make_shared<Multiply>(upmul, downscale);
- return std::make_shared<Convert>(downmul, ov::element::f8e4m3);
+ auto upconv = std::make_shared<v0::Convert>(src, ov::element::f32);
+ auto upscale = v0::Constant::create(ov::element::f32, ov::Shape{}, {1.0f});
+ auto upmul = std::make_shared<v1::Multiply>(upconv, upscale);
+ auto downscale = v0::Constant::create(ov::element::f32, ov::Shape{}, {1.0f});
+ auto downmul = std::make_shared<v1::Multiply>(upmul, downscale);
+ return std::make_shared<v0::Convert>(downmul, ov::element::f8e4m3);
};
// Two independent LPT chains both starting from the same past_k.
- auto lpt1 = make_lpt(past_k);
- auto lpt2 = make_lpt(past_k);
- auto concat1 = std::make_shared<Concat>(ov::OutputVector{lpt1, current_k1}, 2);
+ auto lpt1 = make_lpt(past_k);
+ auto lpt2 = make_lpt(past_k);
+ auto concat1 = std::make_shared<v0::Concat>(ov::OutputVector{lpt1, current_k1}, 2);
concat1->set_friendly_name("past_key_values.0.key_concat1");
- auto concat2 = std::make_shared<Concat>(ov::OutputVector{lpt2, current_k2}, 2);
+ auto concat2 = std::make_shared<v0::Concat>(ov::OutputVector{lpt2, current_k2}, 2);
concat2->set_friendly_name("past_key_values.0.key_concat2");
- auto result1 = std::make_shared<Result>(concat1);
+ auto result1 = std::make_shared<v0::Result>(concat1);
result1->output(0).set_names({"present.0.key"});
- auto result2 = std::make_shared<Result>(concat2);
+ auto result2 = std::make_shared<v0::Result>(concat2);
result2->output(0).set_names({"present.1.key"});
auto model = std::make_shared<ov::Model>(ov::ResultVector{result1, result2},
@@ -178,18 +263,16 @@ TEST_F(RemoveEmptyKVInputsPassTest, SharedParam_TwoConcats_LptSubgraph_BothElimi
// 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});
+ auto past_conv = std::make_shared<v0::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});
+ auto present_conv = std::make_shared<v0::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 concat = std::make_shared<v0::Concat>(ov::OutputVector{past_conv, present_conv}, 2);
+ auto result = std::make_shared<v0::Result>(concat);
auto model = std::make_shared<ov::Model>(ov::ResultVector{result},
ov::ParameterVector{past_conv, present_conv},
@@ -203,18 +286,16 @@ TEST_F(RemoveEmptyKVInputsPassTest, LinCacheConvParamPreserved) {
// 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});
+ auto past_ssm = std::make_shared<v0::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});
+ auto present_ssm = std::make_shared<v0::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 concat = std::make_shared<v0::Concat>(ov::OutputVector{past_ssm, present_ssm}, 2);
+ auto result = std::make_shared<v0::Result>(concat);
auto model = std::make_shared<ov::Model>(ov::ResultVector{result},
ov::ParameterVector{past_ssm, present_ssm},
@@ -229,30 +310,28 @@ TEST_F(RemoveEmptyKVInputsPassTest, LinCacheSsmParamPreserved) {
// 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});
+ auto past_k = std::make_shared<v0::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});
+ auto present_k = std::make_shared<v0::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});
+ auto past_v = std::make_shared<v0::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});
+ auto present_v = std::make_shared<v0::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 concat_k = std::make_shared<v0::Concat>(ov::OutputVector{past_k, present_k}, 2);
+ auto concat_v = std::make_shared<v0::Concat>(ov::OutputVector{past_v, present_v}, 2);
- auto result_k = std::make_shared<Result>(concat_k);
+ auto result_k = std::make_shared<v0::Result>(concat_k);
result_k->output(0).set_names({"present.0.key"});
- auto result_v = std::make_shared<Result>(concat_v);
+ auto result_v = std::make_shared<v0::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},
@@ -274,19 +353,17 @@ TEST_F(RemoveEmptyKVInputsPassTest, PlainF32KeyValuePairBothRemoved) {
// 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});
+ auto past_k = std::make_shared<v0::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});
+ auto present_k = std::make_shared<v0::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);
+ auto concat = std::make_shared<v0::Concat>(ov::OutputVector{past_k, present_k}, 2);
+ auto result = std::make_shared<v0::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},
@@ -303,18 +380,16 @@ TEST_F(RemoveEmptyKVInputsPassTest, WhisperRestoredParamRemoved) {
// 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});
+ auto past_other = std::make_shared<v0::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});
+ auto present_other = std::make_shared<v0::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 concat = std::make_shared<v0::Concat>(ov::OutputVector{past_other, present_other}, 2);
+ auto result = std::make_shared<v0::Result>(concat);
auto model = std::make_shared<ov::Model>(ov::ResultVector{result},
ov::ParameterVector{past_other, present_other},
@@ -328,57 +403,55 @@ TEST_F(RemoveEmptyKVInputsPassTest, NonKVNamedParamPreserved) {
// 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});
+ auto past_k = std::make_shared<v0::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});
+ auto present_k = std::make_shared<v0::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);
+ auto concat_k = std::make_shared<v0::Concat>(ov::OutputVector{past_k, present_k}, 2);
+ auto result_k = std::make_shared<v0::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});
+ auto past_v = std::make_shared<v0::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});
+ auto present_v = std::make_shared<v0::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);
+ auto concat_v = std::make_shared<v0::Concat>(ov::OutputVector{past_v, present_v}, 2);
+ auto result_v = std::make_shared<v0::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});
+ auto past_conv = std::make_shared<v0::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});
+ auto present_conv = std::make_shared<v0::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);
+ auto concat_conv = std::make_shared<v0::Concat>(ov::OutputVector{past_conv, present_conv}, 2);
+ auto result_conv = std::make_shared<v0::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});
+ auto past_ssm = std::make_shared<v0::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});
+ auto present_ssm = std::make_shared<v0::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 concat_ssm = std::make_shared<v0::Concat>(ov::OutputVector{past_ssm, present_ssm}, 2);
+ auto result_ssm = std::make_shared<v0::Result>(concat_ssm);
auto model = std::make_shared<ov::Model>(
ov::ResultVector{result_k, result_v, result_conv, result_ssm},