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] Enable FuseGroupedQueryIntoGDN for statically-shaped models (#35789)
Mikhail Ryzhov37451e0b9b20
diff --git a/src/common/transformations/src/transformations/common_optimizations/fuse_gated_delta_net.cpp b/src/common/transformations/src/transformations/common_optimizations/fuse_gated_delta_net.cppindex d5fc5ef6348..0eeddccc31a 100644--- a/src/common/transformations/src/transformations/common_optimizations/fuse_gated_delta_net.cpp+++ b/src/common/transformations/src/transformations/common_optimizations/fuse_gated_delta_net.cpp@@ -402,66 +402,57 @@ ov::Output<ov::Node> find_qk_anchor(const ov::Output<ov::Node>& start) {return {};}-ov::Output<ov::Node> align_to_reference_shape(const ov::Output<ov::Node>& src,- const ov::Output<ov::Node>& reference,- ov::pass::MatcherPass* pass) {+ov::Output<ov::Node> align_to_reference_shape(const ov::Output<ov::Node>& src, const ov::Output<ov::Node>& reference) {const auto& src_ps = src.get_partial_shape();const auto& ref_ps = reference.get_partial_shape();- if (src_ps.compatible(ref_ps)) {- return src;- }- if (src_ps.is_static() && ref_ps.is_static()) {- const auto src_shape = src_ps.to_shape();- const auto ref_shape = ref_ps.to_shape();- if (ov::shape_size(src_shape) != ov::shape_size(ref_shape)) {- return {};- }+ if (src_ps == ref_ps) {+ return src;}- // Accumulate product of all static source dimensions: n.- uint64_t n = 1;- bool has_static_dims = false;- for (const auto& dim : src_ps) {- if (!dim.is_static()) {- continue;- }- has_static_dims = true;- n *= static_cast<uint64_t>(dim.get_length());+ if (src_ps.compatible(ref_ps)) {+ auto ref_shape = std::make_shared<ov::op::v3::ShapeOf>(reference);+ auto reshaped = std::make_shared<v1::Reshape>(src, ref_shape, false);+ ov::copy_runtime_info(src.get_node_shared_ptr(), {ref_shape, reshaped});+ return reshaped;}+ const auto src_rank = src_ps.rank();const auto ref_rank = ref_ps.rank();- if (has_static_dims && ref_rank.is_static() && ref_rank.get_length() >= 2) {- const auto head_size_idx = ref_rank.get_length() - 1;- const auto head_num_idx = ref_rank.get_length() - 2;- const auto& head_size_dim = ref_ps[head_size_idx];-- if (head_size_dim.is_static()) {- const auto head_size = static_cast<uint64_t>(head_size_dim.get_length());- if (head_size == 0 || n % head_size != 0) {- return {};- }- const auto head_num = static_cast<int64_t>(n / head_size);- auto ref_shape = std::make_shared<ov::op::v3::ShapeOf>(reference);- auto indices = v0::Constant::create(ov::element::i64, {1}, {head_num_idx});- auto updates = v0::Constant::create(ov::element::i64, {1}, {head_num});- auto axis = v0::Constant::create(ov::element::i64, {}, {0});- auto updated_shape = std::make_shared<ov::op::v3::ScatterUpdate>(ref_shape, indices, updates, axis);- auto reshaped = std::make_shared<v1::Reshape>(src, updated_shape, false);-- pass->register_new_node(ref_shape);- pass->register_new_node(updated_shape);- pass->register_new_node(reshaped);- return reshaped;+ // Reference is always 4D per GatedDeltaNet spec. Source can be 2D, 3D, or 4D.+ // Calculate num_heads = src_head_dim / ref_head_dim and reshape src to 4D.+ if (src_rank.is_static() && src_rank.get_length() >= 2 && src_rank.get_length() <= 4 && ref_rank.is_static() &&+ ref_rank.get_length() == 4) {+ const auto src_head_dim = src_ps[src_rank.get_length() - 1];+ const auto ref_head_dim = ref_ps[ref_rank.get_length() - 1];++ if (src_head_dim.is_static() && ref_head_dim.is_static()) {+ const auto src_head_size = src_head_dim.get_length();+ const auto ref_head_size = ref_head_dim.get_length();++ if (ref_head_size > 0 && src_head_size % ref_head_size == 0) {+ const int64_t num_heads = src_head_size / ref_head_size;++ // Create ref_shape and update num_heads at index 2: {ref[0], ref[1], num_heads, ref[3]}.+ auto ref_shape = std::make_shared<ov::op::v3::ShapeOf>(reference);+ auto indices = v0::Constant::create(ov::element::i64, {1}, {2});+ auto updates = v0::Constant::create(ov::element::i64, {1}, {num_heads});+ auto axis = v0::Constant::create(ov::element::i64, {}, {0});+ auto updated_shape = std::make_shared<ov::op::v3::ScatterUpdate>(ref_shape, indices, updates, axis);++ auto reshaped = std::make_shared<v1::Reshape>(src, updated_shape, false);+ if (!reshaped->get_output_partial_shape(0).compatible(ref_ps)) {+ return {};+ }++ ov::copy_runtime_info(src.get_node_shared_ptr(), {ref_shape, updated_shape, reshaped});+ return reshaped;+ }}}- auto ref_shape = std::make_shared<ov::op::v3::ShapeOf>(reference);- auto reshaped = std::make_shared<v1::Reshape>(src, ref_shape, false);- pass->register_new_node(ref_shape);- pass->register_new_node(reshaped);- return reshaped;+ return {};}} // namespace@@ -493,9 +484,9 @@ ov::pass::FuseGroupedQueryIntoGDN::FuseGroupedQueryIntoGDN() {return false;}- auto q_aligned = align_to_reference_shape(q_anchor, gdn_node->input_value(0), this);- auto k_aligned = align_to_reference_shape(k_anchor, gdn_node->input_value(1), this);- auto v_aligned = align_to_reference_shape(v_anchor, gdn_node->input_value(2), this);+ auto q_aligned = align_to_reference_shape(q_anchor, gdn_node->input_value(0));+ auto k_aligned = align_to_reference_shape(k_anchor, gdn_node->input_value(1));+ auto v_aligned = align_to_reference_shape(v_anchor, gdn_node->input_value(2));if (!q_aligned.get_node_shared_ptr() || !k_aligned.get_node_shared_ptr() || !v_aligned.get_node_shared_ptr()) {return false;diff --git a/src/common/transformations/tests/common_optimizations/fuse_gated_delta_net.cpp b/src/common/transformations/tests/common_optimizations/fuse_gated_delta_net.cppindex 8e2acf69204..6628ed5c8c5 100644--- a/src/common/transformations/tests/common_optimizations/fuse_gated_delta_net.cpp+++ b/src/common/transformations/tests/common_optimizations/fuse_gated_delta_net.cpp@@ -631,12 +631,137 @@ std::shared_ptr<ov::Model> build_grouped_query_gdn_different_anchors() {ov::ParameterVector{src_q, src_k, src_v, state, gate, beta});}+// Build a model where the shared Concat anchor is 3D, while GDN consumes a 4D tensor.+// This forces the grouped-query fusion path to rebuild the reshape into a 4D shape.+std::shared_ptr<ov::Model> build_grouped_query_gdn_rank3_before() {+ auto src0 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 4, 8});+ auto src1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 4, 8});+ auto state = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 2, 8, 8});+ auto gate = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 4, 2});+ auto beta = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 4, 2});++ auto src = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{src0, src1}, 2);++ auto reshape_shape = ov::op::v0::Constant::create(ov::element::i64, {4}, {1, 4, 2, 8});+ auto q = std::make_shared<ov::op::v1::Reshape>(src, reshape_shape, false);+ auto k = std::make_shared<ov::op::v1::Reshape>(src, reshape_shape, false);+ auto v = std::make_shared<ov::op::v1::Reshape>(src, reshape_shape, false);++ auto gdn = std::make_shared<ov::op::internal::GatedDeltaNet>(q, k, v, state, gate, beta);+ return std::make_shared<ov::Model>(ov::ResultVector{std::make_shared<ov::op::v0::Result>(gdn->output(0)),+ std::make_shared<ov::op::v0::Result>(gdn->output(1))},+ ov::ParameterVector{src0, src1, state, gate, beta});+}++std::shared_ptr<ov::Model> build_grouped_query_gdn_rank3_after() {+ auto src0 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 4, 8});+ auto src1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 4, 8});+ auto state = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 2, 8, 8});+ auto gate = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 4, 2});+ auto beta = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 4, 2});++ auto src = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{src0, src1}, 2);++ auto q_ref =+ std::make_shared<ov::op::v1::Reshape>(src,+ ov::op::v0::Constant::create(ov::element::i64, {4}, {1, 4, 2, 8}),+ false);+ auto k_ref =+ std::make_shared<ov::op::v1::Reshape>(src,+ ov::op::v0::Constant::create(ov::element::i64, {4}, {1, 4, 2, 8}),+ false);+ auto v_ref =+ std::make_shared<ov::op::v1::Reshape>(src,+ ov::op::v0::Constant::create(ov::element::i64, {4}, {1, 4, 2, 8}),+ false);++ auto q_ref_shape = std::make_shared<ov::op::v3::ShapeOf>(q_ref);+ auto k_ref_shape = std::make_shared<ov::op::v3::ShapeOf>(k_ref);+ auto v_ref_shape = std::make_shared<ov::op::v3::ShapeOf>(v_ref);+ auto indices = ov::op::v0::Constant::create(ov::element::i64, {1}, {2});+ auto axis = ov::op::v0::Constant::create(ov::element::i64, {}, {0});+ auto updates = ov::op::v0::Constant::create(ov::element::i64, {1}, {2});+ auto q_shape = std::make_shared<ov::op::v3::ScatterUpdate>(q_ref_shape, indices, updates, axis);+ auto k_shape = std::make_shared<ov::op::v3::ScatterUpdate>(k_ref_shape, indices, updates, axis);+ auto v_shape = std::make_shared<ov::op::v3::ScatterUpdate>(v_ref_shape, indices, updates, axis);++ auto q = std::make_shared<ov::op::v1::Reshape>(src, q_shape, false);+ auto k = std::make_shared<ov::op::v1::Reshape>(src, k_shape, false);+ auto v = std::make_shared<ov::op::v1::Reshape>(src, v_shape, false);++ auto gdn = std::make_shared<ov::op::internal::GatedDeltaNet>(q, k, v, state, gate, beta);+ return std::make_shared<ov::Model>(ov::ResultVector{std::make_shared<ov::op::v0::Result>(gdn->output(0)),+ std::make_shared<ov::op::v0::Result>(gdn->output(1))},+ ov::ParameterVector{src0, src1, state, gate, beta});+}++// Same reshape regression, but with batch > 1 and dynamic sequence length.+std::shared_ptr<ov::Model> build_grouped_query_gdn_rank3_dynamic_before() {+ auto src0 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{2, -1, 8});+ auto src1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{2, -1, 8});+ auto state = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{2, 2, 8, 8});+ auto gate = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{2, -1, 2});+ auto beta = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{2, -1, 2});++ auto src = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{src0, src1}, 2);++ auto reshape_shape = ov::op::v0::Constant::create(ov::element::i64, {4}, {2, -1, 2, 8});+ auto q = std::make_shared<ov::op::v1::Reshape>(src, reshape_shape, false);+ auto k = std::make_shared<ov::op::v1::Reshape>(src, reshape_shape, false);+ auto v = std::make_shared<ov::op::v1::Reshape>(src, reshape_shape, false);++ auto gdn = std::make_shared<ov::op::internal::GatedDeltaNet>(q, k, v, state, gate, beta);+ return std::make_shared<ov::Model>(ov::ResultVector{std::make_shared<ov::op::v0::Result>(gdn->output(0)),+ std::make_shared<ov::op::v0::Result>(gdn->output(1))},+ ov::ParameterVector{src0, src1, state, gate, beta});+}++std::shared_ptr<ov::Model> build_grouped_query_gdn_rank3_dynamic_after() {+ auto src0 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{2, -1, 8});+ auto src1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{2, -1, 8});+ auto state = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{2, 2, 8, 8});+ auto gate = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{2, -1, 2});+ auto beta = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{2, -1, 2});++ auto src = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{src0, src1}, 2);++ auto q_ref =+ std::make_shared<ov::op::v1::Reshape>(src,+ ov::op::v0::Constant::create(ov::element::i64, {4}, {2, -1, 2, 8}),+ false);+ auto k_ref =+ std::make_shared<ov::op::v1::Reshape>(src,+ ov::op::v0::Constant::create(ov::element::i64, {4}, {2, -1, 2, 8}),+ false);+ auto v_ref =+ std::make_shared<ov::op::v1::Reshape>(src,+ ov::op::v0::Constant::create(ov::element::i64, {4}, {2, -1, 2, 8}),+ false);++ auto q_ref_shape = std::make_shared<ov::op::v3::ShapeOf>(q_ref);+ auto k_ref_shape = std::make_shared<ov::op::v3::ShapeOf>(k_ref);+ auto v_ref_shape = std::make_shared<ov::op::v3::ShapeOf>(v_ref);+ auto indices = ov::op::v0::Constant::create(ov::element::i64, {1}, {2});+ auto axis = ov::op::v0::Constant::create(ov::element::i64, {}, {0});+ auto updates = ov::op::v0::Constant::create(ov::element::i64, {1}, {2});+ auto q_shape = std::make_shared<ov::op::v3::ScatterUpdate>(q_ref_shape, indices, updates, axis);+ auto k_shape = std::make_shared<ov::op::v3::ScatterUpdate>(k_ref_shape, indices, updates, axis);+ auto v_shape = std::make_shared<ov::op::v3::ScatterUpdate>(v_ref_shape, indices, updates, axis);++ auto q = std::make_shared<ov::op::v1::Reshape>(src, q_shape, false);+ auto k = std::make_shared<ov::op::v1::Reshape>(src, k_shape, false);+ auto v = std::make_shared<ov::op::v1::Reshape>(src, v_shape, false);++ auto gdn = std::make_shared<ov::op::internal::GatedDeltaNet>(q, k, v, state, gate, beta);+ return std::make_shared<ov::Model>(ov::ResultVector{std::make_shared<ov::op::v0::Result>(gdn->output(0)),+ std::make_shared<ov::op::v0::Result>(gdn->output(1))},+ ov::ParameterVector{src0, src1, state, gate, beta});+}+} // namespace// Positive: all Q/K/V share one Split anchor via a Transpose each → transformation fuses them.TEST_F(TransformationTestsF, FuseGroupedQueryIntoGDN_SharedAnchor_Applied) {- disable_rt_info_check();- disable_result_friendly_names_check();model = build_grouped_query_gdn_before();manager.register_pass<ov::pass::FuseGroupedQueryIntoGDN>();model_ref = build_grouped_query_gdn_after();@@ -644,11 +769,21 @@ TEST_F(TransformationTestsF, FuseGroupedQueryIntoGDN_SharedAnchor_Applied) {// Negative: Q, K, V each come from a distinct Split node → anchors mismatch → transformation skipped.TEST_F(TransformationTestsF, FuseGroupedQueryIntoGDN_DifferentAnchors_NotApplied) {- disable_rt_info_check();- disable_result_friendly_names_check();model = build_grouped_query_gdn_different_anchors();manager.register_pass<ov::pass::FuseGroupedQueryIntoGDN>();model_ref = build_grouped_query_gdn_different_anchors();}+TEST_F(TransformationTestsF, FuseGroupedQueryIntoGDN_ReshapeTo4D_Applied) {+ model = build_grouped_query_gdn_rank3_before();+ manager.register_pass<ov::pass::FuseGroupedQueryIntoGDN>();+ model_ref = build_grouped_query_gdn_rank3_after();+}++TEST_F(TransformationTestsF, FuseGroupedQueryIntoGDN_ReshapeTo4D_DynamicSeqLen_Applied) {+ model = build_grouped_query_gdn_rank3_dynamic_before();+ manager.register_pass<ov::pass::FuseGroupedQueryIntoGDN>();+ model_ref = build_grouped_query_gdn_rank3_dynamic_after();+}+} // namespace ov::test\ No newline at end of file