OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[TRANSFORMATIONS] PagedGatedDeltaNetFusion (#35532)
Mikhail Ryzhov954f917ca434
diff --git a/src/common/transformations/include/transformations/paged_attention/paged_gated_delta_net_fusion.hpp b/src/common/transformations/include/transformations/paged_attention/paged_gated_delta_net_fusion.hppnew file mode 100644index 00000000000..2fc8ec1a41e--- /dev/null+++ b/src/common/transformations/include/transformations/paged_attention/paged_gated_delta_net_fusion.hpp@@ -0,0 +1,67 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include <unordered_set>++#include "openvino/pass/matcher_pass.hpp"+#include "openvino/pass/sdpa_to_paged_attention.hpp"+#include "transformations_visibility.hpp"++namespace ov {+namespace pass {++/**+ * @ingroup ov_transformation_common_api+ * @brief Fuses GatedDeltaNet into internal::PagedGatedDeltaNet with paged attention.+ *+ * For example, the following graph+ *+ * +------------------++ * | recurrent_state |+ * | (Parameter) |+ * +------------------++ * |+ * +---+---+-+--+---++ * | | | | |+ * v v v v v+ * +------------------++ * | GatedDeltaNet |+ * | (internal op) |+ * +------------------++ * | |+ * output0| |output1 (state)+ * | |+ * v v+ * ... [optional Result/Assign for state writeback]+ *+ * is transformed to:+ *+ * gated_delta_state_table.N -----------++ * gate, beta (flattened) -----+ |+ * query, key, value (flat) ---+------> | internal::PagedGatedDeltaNet+ * subsequence_begins ----------+ |+ * la.block_indices -----------+ |+ * la.* (runtime params) ------+ |+ * v+ * +------------------++ * |Reshape for output|+ * +------------------++ * |+ * v+ * ...+ */+class TRANSFORMATIONS_API PagedGatedDeltaNetFusion : public ov::pass::MatcherPass {+public:+ OPENVINO_MATCHER_PASS_RTTI("PagedGatedDeltaNetFusion");+ PagedGatedDeltaNetFusion(ov::pass::paged_attention::PaParams& pa_params,+ std::unordered_set<std::string>& var_ids_to_remove);++private:+ size_t m_layer_index = 0;+};++} // namespace pass+} // namespace ovdiff --git a/src/common/transformations/src/transformations/paged_attention/paged_gated_delta_net_fusion.cpp b/src/common/transformations/src/transformations/paged_attention/paged_gated_delta_net_fusion.cppnew file mode 100644index 00000000000..cc9cb9d3d07--- /dev/null+++ b/src/common/transformations/src/transformations/paged_attention/paged_gated_delta_net_fusion.cpp@@ -0,0 +1,176 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "transformations/paged_attention/paged_gated_delta_net_fusion.hpp"++#include <memory>+#include <string>+#include <unordered_set>++#include "itt.hpp"+#include "openvino/core/graph_util.hpp"+#include "openvino/core/rt_info.hpp"+#include "openvino/op/concat.hpp"+#include "openvino/op/constant.hpp"+#include "openvino/op/convert.hpp"+#include "openvino/op/gated_delta_net.hpp"+#include "openvino/op/gather.hpp"+#include "openvino/op/paged_gated_delta_net.hpp"+#include "openvino/op/parameter.hpp"+#include "openvino/op/reshape.hpp"+#include "openvino/op/result.hpp"+#include "openvino/op/shape_of.hpp"+#include "openvino/op/slice.hpp"+#include "openvino/op/transpose.hpp"+#include "openvino/op/unsqueeze.hpp"+#include "openvino/op/util/read_value_base.hpp"+#include "openvino/pass/pattern/matcher.hpp"+#include "openvino/pass/pattern/op/optional.hpp"+#include "openvino/pass/pattern/op/wrap_type.hpp"+#include "transformations/rt_info/keep_const_precision.hpp"++using ov::pass::pattern::any_input;+using ov::pass::pattern::wrap_type;++namespace v0 = ov::op::v0;++namespace {++constexpr const char* GATED_DELTA_STATE_TABLE_PREFIX = "gated_delta_state_table.";++std::string make_gated_delta_state_table_name(const size_t layer_index) {+ return std::string(GATED_DELTA_STATE_TABLE_PREFIX) + std::to_string(layer_index);+}++ov::PartialShape make_gated_delta_state_table_shape(const ov::PartialShape& state_shape) {+ // GDN input state shape is [B, H, D_k, D_v].+ // PagedGatedDeltaNet state table expects [?, H, D_v, D_k] — swap last two dims.+ if (state_shape.rank().is_static() && state_shape.rank().get_length() == 4) {+ return ov::PartialShape{ov::Dimension::dynamic(), state_shape[1], state_shape[3], state_shape[2]};+ }+ return ov::PartialShape::dynamic(4);+}++ov::Output<ov::Node> flatten_batch_length(const ov::Output<ov::Node>& input,+ const std::vector<int64_t>& tail_dim_indices) {+ // Flattens [B, L, ...tail_dims] to [B*L, ...tail_dims] by preserving tail_dim_indices.+ const auto shape_of = std::make_shared<ov::op::v3::ShapeOf>(input, ov::element::i64);+ const auto idx_const = v0::Constant::create(ov::element::i64, ov::Shape{tail_dim_indices.size()}, tail_dim_indices);+ const auto axis_0 = v0::Constant::create(ov::element::i64, ov::Shape{}, {0});+ const auto tail_dims = std::make_shared<ov::op::v8::Gather>(shape_of, idx_const, axis_0);+ const auto flat_dim = v0::Constant::create(ov::element::i64, ov::Shape{1}, {-1});+ const auto flat_shape = std::make_shared<v0::Concat>(ov::OutputVector{flat_dim, tail_dims}, 0);+ const auto reshaped = std::make_shared<ov::op::v1::Reshape>(input, flat_shape, false);++ ov::copy_runtime_info(input.get_node_shared_ptr(), {shape_of, tail_dims, flat_shape, reshaped});++ return reshaped;+}+} // namespace++namespace ov::pass {++PagedGatedDeltaNetFusion::PagedGatedDeltaNetFusion(ov::pass::paged_attention::PaParams& pa_params,+ std::unordered_set<std::string>& var_ids_to_remove) {+ auto query = any_input();+ auto key = any_input();+ auto value = any_input();+ auto gate = any_input();+ auto beta = any_input();++ // recurrent_state must come from ReadValue(cache_param) directly or via Gather(ReadValue, beam_idx, axis).+ auto cache_param = any_input();+ auto read_value = wrap_type<ov::op::util::ReadValueBase>({cache_param});+ auto gathered_state = ov::pass::pattern::optional<ov::op::v8::Gather>({read_value, any_input(), any_input()});+ auto gdn = wrap_type<ov::op::internal::GatedDeltaNet>({query, key, value, gathered_state, gate, beta});++ ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS, &pa_params, &var_ids_to_remove](+ ov::pass::pattern::Matcher& m) {+ if (transformation_callback(m.get_match_root())) {+ return false;+ }++ const auto& pm = m.get_pattern_value_map();+ const auto gdn_node = ov::as_type_ptr<ov::op::internal::GatedDeltaNet>(pm.at(gdn).get_node_shared_ptr());+ if (!gdn_node || gdn_node->get_output_size() != 2) {+ return false;+ }++ // Add la.* params lazily on first match — PaParams::add is idempotent.+ pa_params.add("subsequence_begins", ov::element::i32, ov::PartialShape{-1});+ pa_params.add("la.block_indices", ov::element::i32, ov::PartialShape{-1});+ pa_params.add("la.block_indices_begins", ov::element::i32, ov::PartialShape{-1});+ pa_params.add("la.past_lens", ov::element::i32, ov::PartialShape{-1});+ pa_params.add("la.cache_interval", ov::element::i32, ov::PartialShape{-1});++ const auto state_consumers = gdn_node->output(1).get_target_inputs();+ const auto& state_out = pm.at(read_value);++ const auto state_table_param = pa_params.add(make_gated_delta_state_table_name(m_layer_index++),+ state_out.get_element_type(),+ make_gated_delta_state_table_shape(state_out.get_partial_shape()));+ enable_keep_const_precision(state_table_param);++ const auto rv = ov::as_type_ptr<ov::op::util::ReadValueBase>(pm.at(read_value).get_node_shared_ptr());+ OPENVINO_ASSERT(rv, "Matched cache node is expected to be ReadValue");+ var_ids_to_remove.insert(rv->get_variable_id());++ const auto query_flat = flatten_batch_length(pm.at(query), {2, 3});+ const auto key_flat = flatten_batch_length(pm.at(key), {2, 3});+ const auto value_flat = flatten_batch_length(pm.at(value), {2, 3});+ const auto gate_flat = flatten_batch_length(pm.at(gate), {2});+ const auto beta_flat = flatten_batch_length(pm.at(beta), {2});++ // Inputs 0-5 are flattened from matched GatedDeltaNet [B,L,H,*] to PagedGDN [B*L,H,*].+ const auto paged_gdn =+ std::make_shared<ov::op::internal::PagedGatedDeltaNet>(query_flat,+ key_flat,+ value_flat,+ state_table_param->output(0),+ gate_flat,+ beta_flat,+ pa_params["subsequence_begins"],+ pa_params["la.block_indices"],+ pa_params["la.block_indices_begins"],+ pa_params["la.past_lens"],+ pa_params["la.cache_interval"],+ gdn_node->get_fuse_qk_l2norm(),+ gdn_node->get_q_l2_norm_eps(),+ gdn_node->get_k_l2_norm_eps());++ paged_gdn->set_friendly_name(gdn_node->get_friendly_name() + "/PagedGatedDeltaNet");+ const auto query_shape = std::make_shared<ov::op::v3::ShapeOf>(pm.at(query), ov::element::i64);+ const auto value_shape = std::make_shared<ov::op::v3::ShapeOf>(pm.at(value), ov::element::i64);+ const auto axis_0 = v0::Constant::create(ov::element::i64, ov::Shape{}, {0});+ const auto idx_q = v0::Constant::create(ov::element::i64, ov::Shape{3}, {0, 1, 2});+ const auto idx_v = v0::Constant::create(ov::element::i64, ov::Shape{1}, {3});+ const auto q_dims = std::make_shared<ov::op::v8::Gather>(query_shape, idx_q, axis_0);+ const auto v_dim = std::make_shared<ov::op::v8::Gather>(value_shape, idx_v, axis_0);+ const auto out0_shape = std::make_shared<v0::Concat>(ov::OutputVector{q_dims, v_dim}, 0);+ const auto paged_gdn_out = std::make_shared<ov::op::v1::Reshape>(paged_gdn, out0_shape, false);+ paged_gdn_out->set_friendly_name(gdn_node->get_friendly_name());++ ov::copy_runtime_info(gdn_node,+ {paged_gdn, query_shape, value_shape, q_dims, v_dim, out0_shape, paged_gdn_out});++ // Disconnect GDN state output consumers; cleanup is driven by ReadValue variable ids.+ for (const auto& state_consumer : state_consumers) {+ // Reconnect consumer to the original state source so it becomes a dead branch.+ state_consumer.replace_source_output(gdn_node->input_value(3));+ }++ if (!ov::replace_output_update_name(gdn_node->output(0), paged_gdn_out->output(0))) {+ gdn_node->output(0).replace(paged_gdn_out->output(0));+ }++ register_new_node(paged_gdn_out);+ register_new_node(paged_gdn);+ return true;+ };++ const auto matcher = std::make_shared<ov::pass::pattern::Matcher>(gdn, "PagedGatedDeltaNetFusion");+ register_matcher(matcher, callback);+}++} // namespace ov::passdiff --git a/src/common/transformations/tests/common_optimizations/paged_gated_delta_net_fusion.cpp b/src/common/transformations/tests/common_optimizations/paged_gated_delta_net_fusion.cppnew file mode 100644index 00000000000..3f4334a3021--- /dev/null+++ b/src/common/transformations/tests/common_optimizations/paged_gated_delta_net_fusion.cpp@@ -0,0 +1,407 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "transformations/paged_attention/paged_gated_delta_net_fusion.hpp"++#include <gtest/gtest.h>++#include <cstdlib>+#include <string>+#include <unordered_set>++#include "common_test_utils/ov_test_utils.hpp"+#include "openvino/op/add.hpp"+#include "openvino/op/concat.hpp"+#include "openvino/op/constant.hpp"+#include "openvino/op/gated_delta_net.hpp"+#include "openvino/op/gather.hpp"+#include "openvino/op/paged_gated_delta_net.hpp"+#include "openvino/op/parameter.hpp"+#include "openvino/op/read_value.hpp"+#include "openvino/op/reshape.hpp"+#include "openvino/op/result.hpp"+#include "openvino/op/shape_of.hpp"+#include "openvino/pass/manager.hpp"+#include "openvino/pass/sdpa_to_paged_attention.hpp"+#include "openvino/runtime/core.hpp"++namespace {++using namespace ov;+namespace v0 = ov::op::v0;+namespace v1 = ov::op::v1;+namespace v3 = ov::op::v3;+namespace v8 = ov::op::v8;+namespace internal = ov::op::internal;++std::shared_ptr<v0::Parameter> make_f32_param(const std::string& name, const Shape& shape) {+ auto p = std::make_shared<v0::Parameter>(element::f32, shape);+ p->set_friendly_name(name);+ p->get_output_tensor(0).set_names({name});+ return p;+}++std::shared_ptr<ov::Model> build_fusable_model() {+ auto query = make_f32_param("query", Shape{2, 3, 4, 8});+ auto key = make_f32_param("key", Shape{2, 3, 4, 8});+ auto value = make_f32_param("value", Shape{2, 3, 4, 6});++ auto recurrent_state = make_f32_param("past_recurrent_state", Shape{2, 4, 8, 6});+ recurrent_state->get_output_tensor(0).set_names({"cache_params.past.recurrent_state.0"});+ auto read_value = std::make_shared<ov::op::v3::ReadValue>(recurrent_state->output(0), "cache_param_0");++ auto gate = make_f32_param("gate", Shape{2, 3, 4});+ auto beta = make_f32_param("beta", Shape{2, 3, 4});++ auto gdn = std::make_shared<internal::GatedDeltaNet>(query, key, value, read_value, gate, beta);++ auto out = std::make_shared<v0::Result>(gdn->output(0));+ auto present_state = std::make_shared<v0::Result>(gdn->output(1));+ present_state->get_output_tensor(0).set_names({"cache_params.present.recurrent_state.0"});++ ParameterVector params{query, key, value, recurrent_state, gate, beta};++ return std::make_shared<ov::Model>(ResultVector{out, present_state}, params);+}++std::shared_ptr<ov::Model> build_non_fusable_model() {+ auto query = make_f32_param("query", Shape{2, 3, 4, 8});+ auto key = make_f32_param("key", Shape{2, 3, 4, 8});+ auto value = make_f32_param("value", Shape{2, 3, 4, 6});++ auto recurrent_state = make_f32_param("past_recurrent_state", Shape{2, 4, 8, 6});+ recurrent_state->get_output_tensor(0).set_names({"cache_params.past.recurrent_state.0"});+ auto read_value = std::make_shared<ov::op::v3::ReadValue>(recurrent_state->output(0), "cache_param_0");++ auto gate = make_f32_param("gate", Shape{2, 3, 4});+ auto beta = make_f32_param("beta", Shape{2, 3, 4});++ auto gdn = std::make_shared<internal::GatedDeltaNet>(query, key, value, read_value, gate, beta);+ auto add_rhs = make_f32_param("state_add_rhs", Shape{2, 4, 8, 6});+ auto state_add = std::make_shared<v1::Add>(gdn->output(1), add_rhs);++ auto out = std::make_shared<v0::Result>(gdn->output(0));+ auto present_state = std::make_shared<v0::Result>(state_add);+ present_state->get_output_tensor(0).set_names({"cache_params.present.recurrent_state.0"});++ ParameterVector params{query, key, value, recurrent_state, gate, beta, add_rhs};+ return std::make_shared<ov::Model>(ResultVector{out, present_state}, params);+}++std::shared_ptr<ov::Model> build_fusable_model_with_gathered_state() {+ auto query = make_f32_param("query", Shape{2, 3, 4, 8});+ auto key = make_f32_param("key", Shape{2, 3, 4, 8});+ auto value = make_f32_param("value", Shape{2, 3, 4, 6});++ auto recurrent_state = make_f32_param("past_recurrent_state", Shape{2, 4, 8, 6});+ recurrent_state->get_output_tensor(0).set_names({"cache_params.past.recurrent_state.0"});+ auto read_value = std::make_shared<ov::op::v3::ReadValue>(recurrent_state->output(0), "cache_param_0");+ auto beam_idx = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});+ beam_idx->set_friendly_name("beam_idx");+ beam_idx->get_output_tensor(0).set_names({"beam_idx"});+ auto gather_axis = v0::Constant::create(element::i64, Shape{}, {0});+ auto gathered_state = std::make_shared<ov::op::v8::Gather>(read_value, beam_idx, gather_axis);++ auto gate = make_f32_param("gate", Shape{2, 3, 4});+ auto beta = make_f32_param("beta", Shape{2, 3, 4});++ auto gdn = std::make_shared<internal::GatedDeltaNet>(query, key, value, gathered_state, gate, beta);++ auto out = std::make_shared<v0::Result>(gdn->output(0));+ auto present_state = std::make_shared<v0::Result>(gdn->output(1));+ present_state->get_output_tensor(0).set_names({"cache_params.present.recurrent_state.0"});++ ParameterVector params{query, key, value, recurrent_state, beam_idx, gate, beta};+ return std::make_shared<ov::Model>(ResultVector{out, present_state}, params);+}++std::shared_ptr<v0::Parameter> make_pa_param(const std::string& name,+ ov::element::Type et,+ const ov::PartialShape& shape) {+ auto p = std::make_shared<v0::Parameter>(et, shape);+ p->set_friendly_name(name);+ p->get_output_tensor(0).set_names({name});+ return p;+}++// Mirrors flatten_blhd_to_thd from paged_gated_delta_net_fusion.cpp.+ov::Output<ov::Node> ref_flatten_blhd_to_thd(const ov::Output<ov::Node>& input) {+ const auto shape_of = std::make_shared<v3::ShapeOf>(input, element::i64);+ const auto idx_hd = v0::Constant::create(element::i64, Shape{2}, {2, 3});+ const auto axis_0 = v0::Constant::create(element::i64, Shape{}, {0});+ const auto dims_hd = std::make_shared<v8::Gather>(shape_of, idx_hd, axis_0);+ const auto flat_dim = v0::Constant::create(element::i64, Shape{1}, {-1});+ const auto flat_shape = std::make_shared<v0::Concat>(OutputVector{flat_dim, dims_hd}, 0);+ return std::make_shared<v1::Reshape>(input, flat_shape, false);+}++// Mirrors flatten_blh_to_th from paged_gated_delta_net_fusion.cpp.+ov::Output<ov::Node> ref_flatten_blh_to_th(const ov::Output<ov::Node>& input) {+ const auto shape_of = std::make_shared<v3::ShapeOf>(input, element::i64);+ const auto idx_h = v0::Constant::create(element::i64, Shape{1}, {2});+ const auto axis_0 = v0::Constant::create(element::i64, Shape{}, {0});+ const auto dim_h = std::make_shared<v8::Gather>(shape_of, idx_h, axis_0);+ const auto flat_dim = v0::Constant::create(element::i64, Shape{1}, {-1});+ const auto flat_shape = std::make_shared<v0::Concat>(OutputVector{flat_dim, dim_h}, 0);+ return std::make_shared<v1::Reshape>(input, flat_shape, false);+}++// Builds the PagedGDN block + output reshape that replaces GDN in the fused graph.+// Returns {paged_gdn_out, paged_gdn} where paged_gdn_out is the final reshaped output.+ov::Output<ov::Node> build_paged_gdn_block(const std::shared_ptr<v0::Parameter>& query,+ const std::shared_ptr<v0::Parameter>& key,+ const std::shared_ptr<v0::Parameter>& value,+ const std::shared_ptr<v0::Parameter>& gate,+ const std::shared_ptr<v0::Parameter>& beta,+ const std::shared_ptr<v0::Parameter>& state_table,+ const std::shared_ptr<v0::Parameter>& subseq_begins,+ const std::shared_ptr<v0::Parameter>& block_indices,+ const std::shared_ptr<v0::Parameter>& block_indices_begins,+ const std::shared_ptr<v0::Parameter>& past_lens,+ const std::shared_ptr<v0::Parameter>& cache_interval,+ const std::string& gdn_friendly_name) {+ const auto query_flat = ref_flatten_blhd_to_thd(query);+ const auto key_flat = ref_flatten_blhd_to_thd(key);+ const auto value_flat = ref_flatten_blhd_to_thd(value);+ const auto gate_flat = ref_flatten_blh_to_th(gate);+ const auto beta_flat = ref_flatten_blh_to_th(beta);++ auto paged_gdn = std::make_shared<internal::PagedGatedDeltaNet>(query_flat,+ key_flat,+ value_flat,+ state_table->output(0),+ gate_flat,+ beta_flat,+ subseq_begins->output(0),+ block_indices->output(0),+ block_indices_begins->output(0),+ past_lens->output(0),+ cache_interval->output(0));+ paged_gdn->set_friendly_name(gdn_friendly_name + "/PagedGatedDeltaNet");++ const auto q_shape = std::make_shared<v3::ShapeOf>(query, element::i64);+ const auto v_shape = std::make_shared<v3::ShapeOf>(value, element::i64);+ const auto axis_0 = v0::Constant::create(element::i64, Shape{}, {0});+ const auto idx_q = v0::Constant::create(element::i64, Shape{3}, {0, 1, 2});+ const auto idx_v = v0::Constant::create(element::i64, Shape{1}, {3});+ const auto q_dims = std::make_shared<v8::Gather>(q_shape, idx_q, axis_0);+ const auto v_dim = std::make_shared<v8::Gather>(v_shape, idx_v, axis_0);+ const auto out_shape = std::make_shared<v0::Concat>(OutputVector{q_dims, v_dim}, 0);+ auto paged_gdn_out = std::make_shared<v1::Reshape>(paged_gdn, out_shape, false);+ paged_gdn_out->set_friendly_name(gdn_friendly_name);+ return paged_gdn_out->output(0);+}++// Reference graph for build_fusable_model() after PagedGatedDeltaNetFusion.+// GDN is replaced by PagedGDN; state Result reconnected to ReadValue.+std::shared_ptr<ov::Model> build_reference_fused_model() {+ auto query = make_f32_param("query", Shape{2, 3, 4, 8});+ auto key = make_f32_param("key", Shape{2, 3, 4, 8});+ auto value = make_f32_param("value", Shape{2, 3, 4, 6});+ auto recurrent_state = make_f32_param("past_recurrent_state", Shape{2, 4, 8, 6});+ recurrent_state->get_output_tensor(0).set_names({"cache_params.past.recurrent_state.0"});+ auto gate = make_f32_param("gate", Shape{2, 3, 4});+ auto beta = make_f32_param("beta", Shape{2, 3, 4});++ // ReadValue remains as the reconnected source for the state Result (dead branch).+ const auto read_value = std::make_shared<v3::ReadValue>(recurrent_state->output(0), "cache_param_0");++ // New PA params added by the pass (in creation order).+ // State input shape [2,4,8,6] = [B,H,D_k,D_v] → table shape [?,H,D_v,D_k] = [?,4,6,8].+ auto subseq_begins = make_pa_param("subsequence_begins", element::i32, PartialShape{-1});+ auto block_indices = make_pa_param("la.block_indices", element::i32, PartialShape{-1});+ auto block_indices_begins = make_pa_param("la.block_indices_begins", element::i32, PartialShape{-1});+ auto past_lens = make_pa_param("la.past_lens", element::i32, PartialShape{-1});+ auto cache_interval = make_pa_param("la.cache_interval", element::i32, PartialShape{-1});+ auto state_table =+ make_pa_param("gated_delta_state_table.0", element::f32, PartialShape{Dimension::dynamic(), 4, 6, 8});++ const auto paged_gdn_out = build_paged_gdn_block(query,+ key,+ value,+ gate,+ beta,+ state_table,+ subseq_begins,+ block_indices,+ block_indices_begins,+ past_lens,+ cache_interval,+ "GatedDeltaNet");++ auto out = std::make_shared<v0::Result>(paged_gdn_out);+ auto present_state = std::make_shared<v0::Result>(read_value->output(0));+ present_state->get_output_tensor(0).set_names({"cache_params.present.recurrent_state.0"});++ ParameterVector params{query,+ key,+ value,+ recurrent_state,+ gate,+ beta,+ subseq_begins,+ block_indices,+ block_indices_begins,+ past_lens,+ cache_interval,+ state_table};+ return std::make_shared<ov::Model>(ResultVector{out, present_state}, params);+}++// Reference graph for build_non_fusable_model() after PagedGatedDeltaNetFusion.+// GDN is replaced by PagedGDN; the Add consumer of the state output is reconnected to ReadValue.+std::shared_ptr<ov::Model> build_reference_fused_non_fusable_model() {+ auto query = make_f32_param("query", Shape{2, 3, 4, 8});+ auto key = make_f32_param("key", Shape{2, 3, 4, 8});+ auto value = make_f32_param("value", Shape{2, 3, 4, 6});+ auto recurrent_state = make_f32_param("past_recurrent_state", Shape{2, 4, 8, 6});+ recurrent_state->get_output_tensor(0).set_names({"cache_params.past.recurrent_state.0"});+ auto gate = make_f32_param("gate", Shape{2, 3, 4});+ auto beta = make_f32_param("beta", Shape{2, 3, 4});+ auto add_rhs = make_f32_param("state_add_rhs", Shape{2, 4, 8, 6});++ const auto read_value = std::make_shared<v3::ReadValue>(recurrent_state->output(0), "cache_param_0");++ auto subseq_begins = make_pa_param("subsequence_begins", element::i32, PartialShape{-1});+ auto block_indices = make_pa_param("la.block_indices", element::i32, PartialShape{-1});+ auto block_indices_begins = make_pa_param("la.block_indices_begins", element::i32, PartialShape{-1});+ auto past_lens = make_pa_param("la.past_lens", element::i32, PartialShape{-1});+ auto cache_interval = make_pa_param("la.cache_interval", element::i32, PartialShape{-1});+ auto state_table =+ make_pa_param("gated_delta_state_table.0", element::f32, PartialShape{Dimension::dynamic(), 4, 6, 8});++ const auto paged_gdn_out = build_paged_gdn_block(query,+ key,+ value,+ gate,+ beta,+ state_table,+ subseq_begins,+ block_indices,+ block_indices_begins,+ past_lens,+ cache_interval,+ "GatedDeltaNet");++ // The Add consumer of GDN state output is reconnected to ReadValue.+ const auto state_add = std::make_shared<v1::Add>(read_value->output(0), add_rhs);+ auto out = std::make_shared<v0::Result>(paged_gdn_out);+ auto present_state = std::make_shared<v0::Result>(state_add);+ present_state->get_output_tensor(0).set_names({"cache_params.present.recurrent_state.0"});++ ParameterVector params{query,+ key,+ value,+ recurrent_state,+ gate,+ beta,+ add_rhs,+ subseq_begins,+ block_indices,+ block_indices_begins,+ past_lens,+ cache_interval,+ state_table};+ return std::make_shared<ov::Model>(ResultVector{out, present_state}, params);+}++// Reference graph for build_fusable_model_with_gathered_state() after PagedGatedDeltaNetFusion.+// GDN is replaced by PagedGDN; state Result reconnected to Gather(ReadValue, beam_idx, axis).+std::shared_ptr<ov::Model> build_reference_fused_model_with_gathered_state() {+ auto query = make_f32_param("query", Shape{2, 3, 4, 8});+ auto key = make_f32_param("key", Shape{2, 3, 4, 8});+ auto value = make_f32_param("value", Shape{2, 3, 4, 6});+ auto recurrent_state = make_f32_param("past_recurrent_state", Shape{2, 4, 8, 6});+ recurrent_state->get_output_tensor(0).set_names({"cache_params.past.recurrent_state.0"});+ auto beam_idx = std::make_shared<v0::Parameter>(element::i32, PartialShape{-1});+ beam_idx->set_friendly_name("beam_idx");+ beam_idx->get_output_tensor(0).set_names({"beam_idx"});+ auto gate = make_f32_param("gate", Shape{2, 3, 4});+ auto beta = make_f32_param("beta", Shape{2, 3, 4});++ const auto read_value = std::make_shared<v3::ReadValue>(recurrent_state->output(0), "cache_param_0");+ const auto gather_axis = v0::Constant::create(element::i64, Shape{}, {0});+ const auto gathered_state = std::make_shared<v8::Gather>(read_value, beam_idx, gather_axis);++ auto subseq_begins = make_pa_param("subsequence_begins", element::i32, PartialShape{-1});+ auto block_indices = make_pa_param("la.block_indices", element::i32, PartialShape{-1});+ auto block_indices_begins = make_pa_param("la.block_indices_begins", element::i32, PartialShape{-1});+ auto past_lens = make_pa_param("la.past_lens", element::i32, PartialShape{-1});+ auto cache_interval = make_pa_param("la.cache_interval", element::i32, PartialShape{-1});+ // The pattern matches on read_value (not gathered_state), so state shape comes from ReadValue output: [2,4,8,6].+ auto state_table =+ make_pa_param("gated_delta_state_table.0", element::f32, PartialShape{Dimension::dynamic(), 4, 6, 8});++ const auto paged_gdn_out = build_paged_gdn_block(query,+ key,+ value,+ gate,+ beta,+ state_table,+ subseq_begins,+ block_indices,+ block_indices_begins,+ past_lens,+ cache_interval,+ "GatedDeltaNet");++ auto out = std::make_shared<v0::Result>(paged_gdn_out);+ // State Result reconnected to gathered_state (gdn_node->input_value(3)).+ auto present_state = std::make_shared<v0::Result>(gathered_state->output(0));+ present_state->get_output_tensor(0).set_names({"cache_params.present.recurrent_state.0"});++ ParameterVector params{query,+ key,+ value,+ recurrent_state,+ beam_idx,+ gate,+ beta,+ subseq_begins,+ block_indices,+ block_indices_begins,+ past_lens,+ cache_interval,+ state_table};+ return std::make_shared<ov::Model>(ResultVector{out, present_state}, params);+}++} // namespace++class PagedGatedDeltaNetFusionTest : public ::TransformationTestsF {};++void run_paged_gated_delta_net_fusion(const std::shared_ptr<ov::Model>& model) {+ ov::pass::paged_attention::PaParams pa_params{model->get_parameters()};+ std::unordered_set<std::string> var_ids_to_remove;++ ov::pass::Manager manager;+ manager.set_per_pass_validation(false);+ manager.register_pass<ov::pass::PagedGatedDeltaNetFusion>(pa_params, var_ids_to_remove);+ manager.run_passes(model);+ model->add_parameters(pa_params.items());+ model->validate_nodes_and_infer_types();+}++TEST_F(PagedGatedDeltaNetFusionTest, FusesWhenStateOutputIsOnlyResultConsumer) {+ model = build_fusable_model();+ model_ref = build_reference_fused_model();+ disable_rt_info_check();+ comparator.disable(FunctionsComparator::PRECISIONS);+ comparator.disable(FunctionsComparator::TENSOR_NAMES);++ run_paged_gated_delta_net_fusion(model);+}++TEST_F(PagedGatedDeltaNetFusionTest, FusesWhenStateOutputHasNonResultConsumer) {+ model = build_non_fusable_model();+ model_ref = build_reference_fused_non_fusable_model();++ run_paged_gated_delta_net_fusion(model);+}++TEST_F(PagedGatedDeltaNetFusionTest, FusesWhenStateInputIsGatherFromReadValue) {+ model = build_fusable_model_with_gathered_state();+ model_ref = build_reference_fused_model_with_gathered_state();+ run_paged_gated_delta_net_fusion(model);+}diff --git a/src/common/transformations/tests/op_conversions/sdpa_to_paged_attention_test.cpp b/src/common/transformations/tests/op_conversions/sdpa_to_paged_attention_test.cppindex 8bcdd29741f..ffc22161b95 100644--- a/src/common/transformations/tests/op_conversions/sdpa_to_paged_attention_test.cpp+++ b/src/common/transformations/tests/op_conversions/sdpa_to_paged_attention_test.cpp@@ -3581,7 +3581,8 @@ TEST_F(SDPAToPATest, SDPAToPA_LFM2) {auto MatMul11 = makeOP<v0::MatMul>({Convert21, Convert22}, {{"transpose_a", false}, {"transpose_b", false}});auto Transpose10 = makeOP<v1::Transpose>({MatMul11, {0, 2, 1}});auto Concat8 = makeOP<v0::Concat>({Transpose10, Transpose10}, {{"axis", -1}});- auto Cos0 = makeOP<v0::Cos>({Concat8});+ auto Transpose11_rope = makeOP<v1::Transpose>({Concat8, {1, 0, 2}});+ auto Cos0 = makeOP<v0::Cos>({Transpose11_rope});auto Unsqueeze7 = makeOP<v0::Unsqueeze>({Cos0, 1});auto Multiply20 = makeOP<v1::Multiply>({Transpose9, Unsqueeze7}, {{"auto_broadcast", "numpy"}});auto Slice4 = makeOP<v8::Slice>({Transpose9, {2}, {LLONG_MAX}, {1}, {3}});@@ -3589,7 +3590,7 @@ TEST_F(SDPAToPATest, SDPAToPA_LFM2) {auto Multiply21 = makeOP<v1::Multiply>({Slice4, Convert23}, {{"auto_broadcast", "numpy"}});auto Slice5 = makeOP<v8::Slice>({Transpose9, {0}, {2}, {1}, {3}});auto Concat9 = makeOP<v0::Concat>({Multiply21, Slice5}, {{"axis", -1}});- auto Sin0 = makeOP<v0::Sin>({Concat8});+ auto Sin0 = makeOP<v0::Sin>({Transpose11_rope});auto Unsqueeze8 = makeOP<v0::Unsqueeze>({Sin0, 1});auto Multiply22 = makeOP<v1::Multiply>({Concat9, Unsqueeze8}, {{"auto_broadcast", "numpy"}});auto Add18 = makeOP<v1::Add>({Multiply20, Multiply22}, {{"auto_broadcast", "numpy"}});@@ -4392,7 +4393,8 @@ TEST_F(SDPAToPATest, SDPAToPA_LFM2) {auto MatMul11 = makeOP<v0::MatMul>({Convert23, Convert24}, {{"transpose_a", false}, {"transpose_b", false}});auto Transpose10 = makeOP<v1::Transpose>({MatMul11, {0, 2, 1}});auto Concat7 = makeOP<v0::Concat>({Transpose10, Transpose10}, {{"axis", -1}});- auto Cos0 = makeOP<v0::Cos>({Concat7});+ auto Transpose10_rope = makeOP<v1::Transpose>({Concat7, {1, 0, 2}});+ auto Cos0 = makeOP<v0::Cos>({Transpose10_rope});auto Unsqueeze8 = makeOP<v0::Unsqueeze>({Cos0, 1});auto Multiply20 = makeOP<v1::Multiply>({Transpose9, Unsqueeze8}, {{"auto_broadcast", "numpy"}});auto Slice4 = makeOP<v8::Slice>({Transpose9, {2}, {LLONG_MAX}, {1}, {3}});@@ -4400,7 +4402,7 @@ TEST_F(SDPAToPATest, SDPAToPA_LFM2) {auto Multiply21 = makeOP<v1::Multiply>({Slice4, Convert25}, {{"auto_broadcast", "numpy"}});auto Slice5 = makeOP<v8::Slice>({Transpose9, {0}, {2}, {1}, {3}});auto Concat8 = makeOP<v0::Concat>({Multiply21, Slice5}, {{"axis", -1}});- auto Sin0 = makeOP<v0::Sin>({Concat7});+ auto Sin0 = makeOP<v0::Sin>({Transpose10_rope});auto Unsqueeze9 = makeOP<v0::Unsqueeze>({Sin0, 1});auto Multiply22 = makeOP<v1::Multiply>({Concat8, Unsqueeze9}, {{"auto_broadcast", "numpy"}});auto Add18 = makeOP<v1::Add>({Multiply20, Multiply22}, {{"auto_broadcast", "numpy"}});diff --git a/src/core/src/pass/sdpa_to_paged_attention.cpp b/src/core/src/pass/sdpa_to_paged_attention.cppindex a36b7a0d4a9..40ec6e83f9d 100644--- a/src/core/src/pass/sdpa_to_paged_attention.cpp+++ b/src/core/src/pass/sdpa_to_paged_attention.cpp@@ -14,8 +14,10 @@#include "openvino/op/subtract.hpp"#include "openvino/op/unsqueeze.hpp"#include "openvino/pass/manager.hpp"+#include "transformations/common_optimizations/fuse_gated_delta_net.hpp"#include "transformations/common_optimizations/sdpa_fusion.hpp"#include "transformations/op_conversions/convert_slice_to_strided_slice.hpp"+#include "transformations/paged_attention/paged_gated_delta_net_fusion.hpp"#include "transformations/paged_attention/position_ids_replacer.hpp"#include "transformations/paged_attention/prev_sequence_length_pattern.hpp"#include "transformations/paged_attention/state_management_pattern.hpp"@@ -140,7 +142,11 @@ bool ov::pass::SDPAToPagedAttention::run_on_model(const std::shared_ptr<ov::Modeov::pass::Manager manager("SDPA to PA");manager.set_per_pass_validation(false);+ manager.register_pass<ov::pass::GatedDeltaNetFusion>(); // This pass is required to ensure that all GatedDeltaNet+ // nodes are in the expected form before running+ // PagedGatedDeltaNetFusion.manager.register_pass<StateManagementPattern>(m_params, m_results, m_options, var_ids_to_remove);+ manager.register_pass<PagedGatedDeltaNetFusion>(m_params, var_ids_to_remove);manager.register_pass<PrevSequenceLengthPattern>(processed_input_ids, max_context_len, position_ids);manager.register_pass<TotalSequenceLengthPattern>(max_context_len);manager.register_pass<TotalSequenceLengthPatternQwen>(max_context_len);@@ -148,6 +154,7 @@ bool ov::pass::SDPAToPagedAttention::run_on_model(const std::shared_ptr<ov::Modemanager.register_pass<PositionIDsReplacer>(unsqueezed_position_ids);manager.register_pass<PositionIDsReplacerQwen>(unsqueezed_position_ids);manager.register_pass<PositionIDsReplacerCodeGen2>(position_ids);+ manager.register_pass<PositionIDsReplacerLFM2>(position_ids);manager.run_passes(model);{