Skip to content

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

Skip StatefulSDPAFusion for models with shared KV-cache (#35323)

Evgeny Kotovbcbe69a8803f
diff --git a/src/plugins/intel_cpu/src/transformations/cpu_opset/common/pass/stateful_sdpa_fusion.cpp b/src/plugins/intel_cpu/src/transformations/cpu_opset/common/pass/stateful_sdpa_fusion.cpp
index cb1c4d445db..bf95da2eddf 100644
--- a/src/plugins/intel_cpu/src/transformations/cpu_opset/common/pass/stateful_sdpa_fusion.cpp
+++ b/src/plugins/intel_cpu/src/transformations/cpu_opset/common/pass/stateful_sdpa_fusion.cpp
@@ -11,6 +11,7 @@
#include <cstdint>
#include <memory>
#include <tuple>
+#include <unordered_set>
#include <vector>
#include "openvino/cc/pass/itt.hpp"
@@ -158,6 +159,32 @@ StatefulSDPAFusion::StatefulSDPAFusion() {
const auto sdp_node = ov::as_type_ptr<ov::op::v13::ScaledDotProductAttention>(root);
const auto past_k_node = ov::as_type_ptr<ov::op::v6::ReadValue>(pattern_map.at(past_k).get_node_shared_ptr());
const auto past_v_node = ov::as_type_ptr<ov::op::v6::ReadValue>(pattern_map.at(past_v).get_node_shared_ptr());
+ // Skip SDPAs whose KV-cache Variable is shared with another SDPA: the fused
+ // ScaledDotProductAttentionWithKVCache kernel does not support shared KV-cache and
+ // partial fusion leaves the model in an inconsistent state.
+ // Walk forward from past_k / past_v, stopping at SDPA boundaries, and count
+ // how many SDPAs are reachable. Anchoring on the ReadValue (rather than on
+ // direct input node pointers) is robust to intermediate Transpose/Reshape/
+ // Convert/Gather/Broadcast ops between the cache source and the SDPA blocks.
+ auto count_reachable_sdpas = [](ov::Node* start) {
+ size_t cnt = 0;
+ std::unordered_set<ov::Node*> visited;
+ ov::op::util::visit_path_forward(
+ start,
+ visited,
+ [](ov::Node*) {},
+ [&](ov::Node* n) {
+ if (ov::is_type<ov::op::v13::ScaledDotProductAttention>(n)) {
+ ++cnt;
+ return true;
+ }
+ return false;
+ });
+ return cnt;
+ };
+ if (count_reachable_sdpas(past_k_node.get()) > 1 || count_reachable_sdpas(past_v_node.get()) > 1) {
+ return false;
+ }
if (!check_valid_children_type(past_k_node) || !check_valid_children_type(past_v_node)) {
return false;
}
diff --git a/src/plugins/intel_cpu/tests/unit/transformations/state_concat_sdpa.cpp b/src/plugins/intel_cpu/tests/unit/transformations/state_concat_sdpa.cpp
index 7995fbcf664..f3e291f2bb0 100644
--- a/src/plugins/intel_cpu/tests/unit/transformations/state_concat_sdpa.cpp
+++ b/src/plugins/intel_cpu/tests/unit/transformations/state_concat_sdpa.cpp
@@ -19,9 +19,12 @@
#include "transformations/utils/print_model.hpp"
#include "openvino/op/abs.hpp"
#include "openvino/op/add.hpp"
+#include "openvino/op/assign.hpp"
#include "openvino/op/broadcast.hpp"
#include "openvino/op/concat.hpp"
+#include "openvino/op/convert.hpp"
#include "openvino/op/gather.hpp"
+#include "openvino/op/read_value.hpp"
#include "openvino/op/reshape.hpp"
#include "openvino/op/scaled_dot_product_attention.hpp"
#include "openvino/op/shape_of.hpp"
@@ -237,3 +240,170 @@ TEST(TransformationTests, StateConcatSDPAWithExtraNode) {
}
}
}
+
+// Build a model with two SDPA blocks sharing the same KV-cache Variables.
+// One ReadValue per Variable fans out to two independent Gather -> Concat -> SDPA paths,
+// mirroring the shared-KV-cache pattern seen in Gemma3n/Gemma4 exports.
+static std::shared_ptr<ov::Model> makeSharedKVModel(const ov::PartialShape& inputShape) {
+ auto q1 = std::make_shared<ov::op::v0::Parameter>(element::f32, inputShape);
+ auto k1 = std::make_shared<ov::op::v0::Parameter>(element::f32, inputShape);
+ auto v1 = std::make_shared<ov::op::v0::Parameter>(element::f32, inputShape);
+ auto q2 = std::make_shared<ov::op::v0::Parameter>(element::f32, inputShape);
+ auto k2 = std::make_shared<ov::op::v0::Parameter>(element::f32, inputShape);
+ auto v2 = std::make_shared<ov::op::v0::Parameter>(element::f32, inputShape);
+ auto init_k = std::make_shared<ov::op::v0::Parameter>(element::f32, inputShape);
+ auto init_v = std::make_shared<ov::op::v0::Parameter>(element::f32, inputShape);
+ auto beam_idx = std::make_shared<ov::op::v0::Parameter>(element::i32, ov::PartialShape{-1});
+
+ // Shared variables for KV-cache (single ReadValue per Variable, fanning out).
+ auto var_k = std::make_shared<ov::op::util::Variable>(
+ ov::op::util::VariableInfo{inputShape, element::f32, "shared_pastk"});
+ auto var_v = std::make_shared<ov::op::util::Variable>(
+ ov::op::util::VariableInfo{inputShape, element::f32, "shared_pastv"});
+ auto pastk = std::make_shared<ov::op::v6::ReadValue>(init_k, var_k);
+ auto pastv = std::make_shared<ov::op::v6::ReadValue>(init_v, var_v);
+
+ // Path 1
+ auto gather_k1 = std::make_shared<ov::op::v8::Gather>(
+ pastk, beam_idx, op::v0::Constant::create(element::i32, {1}, {0}));
+ auto gather_v1 = std::make_shared<ov::op::v8::Gather>(
+ pastv, beam_idx, op::v0::Constant::create(element::i32, {1}, {0}));
+ auto concat_k1 = std::make_shared<ov::op::v0::Concat>(OutputVector{gather_k1, k1}, 2);
+ auto concat_v1 = std::make_shared<ov::op::v0::Concat>(OutputVector{gather_v1, v1}, 2);
+ auto sdpa1 = std::make_shared<ov::opset13::ScaledDotProductAttention>(q1, concat_k1, concat_v1, false);
+ auto add1 = std::make_shared<op::v1::Add>(sdpa1, op::v0::Constant::create(element::f32, {1}, {1.0f}));
+
+ // Path 2 (fans out from the same ReadValue as path 1)
+ auto gather_k2 = std::make_shared<ov::op::v8::Gather>(
+ pastk, beam_idx, op::v0::Constant::create(element::i32, {1}, {0}));
+ auto gather_v2 = std::make_shared<ov::op::v8::Gather>(
+ pastv, beam_idx, op::v0::Constant::create(element::i32, {1}, {0}));
+ auto concat_k2 = std::make_shared<ov::op::v0::Concat>(OutputVector{gather_k2, k2}, 2);
+ auto concat_v2 = std::make_shared<ov::op::v0::Concat>(OutputVector{gather_v2, v2}, 2);
+ auto sdpa2 = std::make_shared<ov::opset13::ScaledDotProductAttention>(q2, concat_k2, concat_v2, false);
+ auto add2 = std::make_shared<op::v1::Add>(sdpa2, op::v0::Constant::create(element::f32, {1}, {1.0f}));
+
+ // Single pair of Assigns for the shared Variables (from path 1's Concat).
+ auto assign_k = std::make_shared<op::v6::Assign>(concat_k1, var_k);
+ auto assign_v = std::make_shared<op::v6::Assign>(concat_v1, var_v);
+
+ ResultVector results{std::make_shared<ov::op::v0::Result>(add1),
+ std::make_shared<ov::op::v0::Result>(add2)};
+ SinkVector sinks{assign_k, assign_v};
+ return std::make_shared<Model>(results, sinks,
+ ParameterVector{q1, k1, v1, q2, k2, v2, init_k, init_v, beam_idx}, "SharedKVModel");
+}
+
+TEST_F(TransformationTestsF, StateConcatSDPASharedKVCache) {
+#if defined(OPENVINO_ARCH_X86_64) && (defined(__ANDROID__) || defined(ANDROID))
+ test_skipped = true;
+ GTEST_SKIP() << "Skipping StateConcatSDPASharedKVCache test on Android X64";
+#endif
+ // When KV-cache is shared between multiple SDPA blocks, StatefulSDPAFusion must NOT apply:
+ // leaving model_ref unset lets the fixture compare against a clone of the input model.
+ auto inputShape = ov::PartialShape{-1, 8, -1, 64};
+ model = makeSharedKVModel(inputShape);
+ manager.register_pass<SDPASubgraphFusion>();
+}
+
+// Mix of SDPAs in one model:
+// - "shared" part: one pair of Variables feeds two SDPAs (SDPA_s1, SDPA_s2)
+// - "exclusive" part: another pair of Variables feeds a single SDPA (SDPA_e)
+// After SDPASubgraphFusion, only SDPA_e must be fused to ScaledDotProductAttentionWithKVCache;
+// SDPA_s1 and SDPA_s2 must remain as plain ScaledDotProductAttention.
+static std::shared_ptr<ov::Model> makeMixedSharedAndExclusiveKVModel(const ov::PartialShape& inputShape,
+ bool isRef = false) {
+ auto make_param = [&](element::Type t, const ov::PartialShape& s) {
+ return std::make_shared<ov::op::v0::Parameter>(t, s);
+ };
+ auto beam_idx = make_param(element::i32, ov::PartialShape{-1});
+
+ // Shared part
+ auto q_s1 = make_param(element::f32, inputShape);
+ auto k_s1 = make_param(element::f32, inputShape);
+ auto v_s1 = make_param(element::f32, inputShape);
+ auto q_s2 = make_param(element::f32, inputShape);
+ auto k_s2 = make_param(element::f32, inputShape);
+ auto v_s2 = make_param(element::f32, inputShape);
+ auto init_ks = make_param(element::f32, inputShape);
+ auto init_vs = make_param(element::f32, inputShape);
+ auto var_ks = std::make_shared<ov::op::util::Variable>(
+ ov::op::util::VariableInfo{inputShape, element::f32, "shared_pastk"});
+ auto var_vs = std::make_shared<ov::op::util::Variable>(
+ ov::op::util::VariableInfo{inputShape, element::f32, "shared_pastv"});
+ // Single ReadValue per shared Variable, fanning out to two SDPA paths.
+ auto rv_ks = std::make_shared<ov::op::v6::ReadValue>(init_ks, var_ks);
+ auto rv_vs = std::make_shared<ov::op::v6::ReadValue>(init_vs, var_vs);
+
+ auto g_ks1 = std::make_shared<ov::op::v8::Gather>(rv_ks, beam_idx, op::v0::Constant::create(element::i32, {1}, {0}));
+ auto g_vs1 = std::make_shared<ov::op::v8::Gather>(rv_vs, beam_idx, op::v0::Constant::create(element::i32, {1}, {0}));
+ auto c_ks1 = std::make_shared<ov::op::v0::Concat>(OutputVector{g_ks1, k_s1}, 2);
+ auto c_vs1 = std::make_shared<ov::op::v0::Concat>(OutputVector{g_vs1, v_s1}, 2);
+ auto sdpa_s1 = std::make_shared<ov::opset13::ScaledDotProductAttention>(q_s1, c_ks1, c_vs1, false);
+ auto add_s1 = std::make_shared<op::v1::Add>(sdpa_s1, op::v0::Constant::create(element::f32, {1}, {1.0f}));
+
+ auto g_ks2 = std::make_shared<ov::op::v8::Gather>(rv_ks, beam_idx, op::v0::Constant::create(element::i32, {1}, {0}));
+ auto g_vs2 = std::make_shared<ov::op::v8::Gather>(rv_vs, beam_idx, op::v0::Constant::create(element::i32, {1}, {0}));
+ auto c_ks2 = std::make_shared<ov::op::v0::Concat>(OutputVector{g_ks2, k_s2}, 2);
+ auto c_vs2 = std::make_shared<ov::op::v0::Concat>(OutputVector{g_vs2, v_s2}, 2);
+ auto sdpa_s2 = std::make_shared<ov::opset13::ScaledDotProductAttention>(q_s2, c_ks2, c_vs2, false);
+ auto add_s2 = std::make_shared<op::v1::Add>(sdpa_s2, op::v0::Constant::create(element::f32, {1}, {1.0f}));
+
+ auto assign_ks = std::make_shared<op::v6::Assign>(c_ks1, var_ks);
+ auto assign_vs = std::make_shared<op::v6::Assign>(c_vs1, var_vs);
+
+ // Exclusive part (single SDPA on its own Variables — eligible for fusion)
+ auto q_e = make_param(element::f32, inputShape);
+ auto k_e = make_param(element::f32, inputShape);
+ auto v_e = make_param(element::f32, inputShape);
+ auto init_ke = make_param(element::f32, inputShape);
+ auto init_ve = make_param(element::f32, inputShape);
+ auto var_ke = std::make_shared<ov::op::util::Variable>(
+ ov::op::util::VariableInfo{inputShape, element::f32, "excl_pastk"});
+ auto var_ve = std::make_shared<ov::op::util::Variable>(
+ ov::op::util::VariableInfo{inputShape, element::f32, "excl_pastv"});
+ auto rv_ke = std::make_shared<ov::op::v6::ReadValue>(init_ke, var_ke);
+ auto rv_ve = std::make_shared<ov::op::v6::ReadValue>(init_ve, var_ve);
+ Output<ov::Node> sdp_e, concat_ke, concat_ve;
+ if (isRef) {
+ ov::intel_cpu::ScaledDotProductAttentionWithKVCache::Config config;
+ config.fuse_concat = true;
+ auto fused = std::make_shared<ov::intel_cpu::ScaledDotProductAttentionWithKVCache>(
+ OutputVector{q_e, k_e, v_e, beam_idx, rv_ke, rv_ve}, config);
+ sdp_e = fused->output(0);
+ concat_ke = fused->output(1);
+ concat_ve = fused->output(2);
+ } else {
+ auto g_ke =
+ std::make_shared<ov::op::v8::Gather>(rv_ke, beam_idx, op::v0::Constant::create(element::i32, {1}, {0}));
+ auto g_ve =
+ std::make_shared<ov::op::v8::Gather>(rv_ve, beam_idx, op::v0::Constant::create(element::i32, {1}, {0}));
+ concat_ke = std::make_shared<ov::op::v0::Concat>(OutputVector{g_ke, k_e}, 2);
+ concat_ve = std::make_shared<ov::op::v0::Concat>(OutputVector{g_ve, v_e}, 2);
+ sdp_e = std::make_shared<ov::opset13::ScaledDotProductAttention>(q_e, concat_ke, concat_ve, false);
+ }
+ auto add_e = std::make_shared<op::v1::Add>(sdp_e, op::v0::Constant::create(element::f32, {1}, {1.0f}));
+ auto assign_ke = std::make_shared<op::v6::Assign>(concat_ke, var_ke);
+ auto assign_ve = std::make_shared<op::v6::Assign>(concat_ve, var_ve);
+
+ ResultVector results{std::make_shared<ov::op::v0::Result>(add_s1),
+ std::make_shared<ov::op::v0::Result>(add_s2),
+ std::make_shared<ov::op::v0::Result>(add_e)};
+ SinkVector sinks{assign_ks, assign_vs, assign_ke, assign_ve};
+ ParameterVector params{q_s1, k_s1, v_s1, q_s2, k_s2, v_s2, init_ks, init_vs,
+ q_e, k_e, v_e, init_ke, init_ve, beam_idx};
+ return std::make_shared<Model>(results, sinks, params, "MixedSharedExclusiveKV");
+}
+
+TEST_F(TransformationTestsF, StateConcatSDPAMixedSharedAndExclusive) {
+#if defined(OPENVINO_ARCH_X86_64) && (defined(__ANDROID__) || defined(ANDROID))
+ test_skipped = true;
+ GTEST_SKIP() << "Skipping StateConcatSDPAMixedSharedAndExclusive test on Android X64";
+#endif
+ // Exclusive-cache SDPA must still be fused; shared-cache SDPAs must be left alone.
+ auto inputShape = ov::PartialShape{-1, 8, -1, 64};
+ model = makeMixedSharedAndExclusiveKVModel(inputShape);
+ model_ref = makeMixedSharedAndExclusiveKVModel(inputShape, true);
+ manager.register_pass<SDPASubgraphFusion>();
+}
+