Skip to content

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

[GPU] Enable post-op fusion for eltwise with FC/MatMul/Transpose when reshape place in middle (#36879)

Chon Ming Leeb1b41ebdd849
diff --git a/src/common/transformations/include/transformations/common_optimizations/move_eltwise_up_data_movement.hpp b/src/common/transformations/include/transformations/common_optimizations/move_eltwise_up_data_movement.hpp
index 694f7d09f49..3eb9a8b2557 100644
--- a/src/common/transformations/include/transformations/common_optimizations/move_eltwise_up_data_movement.hpp
+++ b/src/common/transformations/include/transformations/common_optimizations/move_eltwise_up_data_movement.hpp
@@ -54,6 +54,36 @@ public:
MoveEltwiseUpThroughDataMovPerChannel();
};
+/// This transformation moves a binary element-wise op below a data movement op (Unsqueeze or
+/// rank-increasing Reshape) that inserts a single unit dimension, when the producer of that op can
+/// fuse the eltwise as a post-op (e.g. FullyConnected, MatMul, Transpose). The unit dimension of the
+/// other eltwise input is squeezed instead, so that the eltwise is computed in the lower rank:
+/// ┌───────────┐ ┌──────────────┐ ┌───────────┐ ┌────────────┐
+/// │ FusableOp │ │ TargetShape │ │ FusableOp │ │ OtherInput │
+/// └─────┬─────┘ └──────┬───────┘ └─────┬─────┘ └─────┬──────┘
+/// │ │ │ │
+/// ┌───────┴───────────────┴───────┐ │ ┌────────┴───────┐
+/// │ Reshape / Unsqueeze │ => │ │ Squeeze │
+/// └───────┬───────────────────────┘ │ └────────┬───────┘
+/// │ ┌─────┴───────────────────────┴─────┐
+/// ┌───────┴────────┐ ┌────────────┐ │ Element-Wise │
+/// │ Element-Wise ├───┤ OtherInput │ └────────────────┬──────────────────┘
+/// └────────────────┘ └────────────┘ ┌───────┴────────┐
+/// │ Unsqueeze │
+/// └────────────────┘
+/// The producer may be followed by a bias Add, which belongs to it because the bias is folded into the
+/// producer's kernel. The data movement op has to be the first input of a non-commutative eltwise,
+/// because the eltwise is fused into the producer as `producer <op> peer` post-op, which would reverse
+/// the operands otherwise. A Reshape is accepted only when its shapes prove a pure dimension insertion,
+/// which excludes more than one dynamic dimension.
+class TRANSFORMATIONS_API MoveEltwiseUpThroughDataMovFusableProducer : public ov::pass::MatcherPass {
+public:
+ OPENVINO_MATCHER_PASS_RTTI("MoveEltwiseUpThroughDataMovFusableProducer");
+ /// @param fusable_producer_types Types of the ops that can fuse an eltwise as a post-op. Passed as
+ /// runtime type infos because callers may pass plugin-private ops, e.g. GPU FullyConnected.
+ MoveEltwiseUpThroughDataMovFusableProducer(std::vector<DiscreteTypeInfo> fusable_producer_types);
+};
+
class TRANSFORMATIONS_API MoveEltwiseUpThroughDataMov : public ov::pass::GraphRewrite {
public:
OPENVINO_GRAPH_REWRITE_RTTI("MoveEltwiseUpThroughDataMov");
diff --git a/src/common/transformations/include/transformations/utils/utils.hpp b/src/common/transformations/include/transformations/utils/utils.hpp
index d3a033a719a..ce69c1ef686 100644
--- a/src/common/transformations/include/transformations/utils/utils.hpp
+++ b/src/common/transformations/include/transformations/utils/utils.hpp
@@ -290,6 +290,16 @@ TRANSFORMATIONS_API bool can_eliminate_eltwise_node(const std::shared_ptr<Node>&
TRANSFORMATIONS_API bool is_constant_and_all_values_equal_int(const Output<Node>& output, const int64_t& v);
+/// Returns axes of unit dimensions of `out_shape` which are not present in `data_shape`,
+/// i.e. axes of dimensions inserted by Unsqueeze-like shape change
+TRANSFORMATIONS_API std::vector<int64_t> get_unsqueeze_axes(const PartialShape& data_shape,
+ const PartialShape& out_shape);
+
+/// Returns axes of unit dimensions of `data_shape` which are not present in `out_shape`,
+/// i.e. axes of dimensions removed by Squeeze-like shape change
+TRANSFORMATIONS_API std::vector<int64_t> get_squeeze_axes(const PartialShape& data_shape,
+ const PartialShape& out_shape);
+
template <typename... AllowedTypes>
bool is_on_path(const ov::Output<ov::Node>& output) {
auto status = true;
diff --git a/src/common/transformations/src/transformations/common_optimizations/move_eltwise_up_data_movement.cpp b/src/common/transformations/src/transformations/common_optimizations/move_eltwise_up_data_movement.cpp
index 9d63e38d7e8..9619925d73b 100644
--- a/src/common/transformations/src/transformations/common_optimizations/move_eltwise_up_data_movement.cpp
+++ b/src/common/transformations/src/transformations/common_optimizations/move_eltwise_up_data_movement.cpp
@@ -16,11 +16,13 @@
#include "openvino/core/rt_info.hpp"
#include "openvino/core/type.hpp"
#include "openvino/core/validation_util.hpp"
+#include "openvino/op/add.hpp"
#include "openvino/op/batch_to_space.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/depth_to_space.hpp"
#include "openvino/op/fake_quantize.hpp"
#include "openvino/op/reshape.hpp"
+#include "openvino/op/result.hpp"
#include "openvino/op/reverse_sequence.hpp"
#include "openvino/op/roll.hpp"
#include "openvino/op/shuffle_channels.hpp"
@@ -30,6 +32,7 @@
#include "openvino/op/util/binary_elementwise_arithmetic.hpp"
#include "openvino/op/util/broadcast_base.hpp"
#include "openvino/op/util/gather_base.hpp"
+#include "openvino/pass/pattern/op/optional.hpp"
#include "openvino/pass/pattern/op/wrap_type.hpp"
#include "transformations/symbolic_transformations/utils.hpp"
#include "transformations/utils/utils.hpp"
@@ -424,3 +427,73 @@ ov::pass::MoveEltwiseUpThroughDataMovPerChannel::MoveEltwiseUpThroughDataMovPerC
auto m = std::make_shared<Matcher>(eltwise_pattern, matcher_name);
register_matcher(m, callback);
}
+
+ov::pass::MoveEltwiseUpThroughDataMovFusableProducer::MoveEltwiseUpThroughDataMovFusableProducer(
+ std::vector<DiscreteTypeInfo> fusable_producer_types) {
+ MATCHER_SCOPE(MoveEltwiseUpThroughDataMovFusableProducer);
+
+ const auto fusable_op = std::make_shared<ov::pass::pattern::op::WrapType>(fusable_producer_types);
+ const auto fusable_producer = ov::pass::pattern::optional<v1::Add>({fusable_op, ov::pass::pattern::any_input()});
+
+ const auto rank_increase_by_one = [](const ov::Output<ov::Node>& output) {
+ const auto& in_rank = output.get_node()->get_input_partial_shape(0).rank();
+ const auto& out_rank = output.get_partial_shape().rank();
+ return in_rank.is_static() && out_rank.is_static() && out_rank.get_length() == in_rank.get_length() + 1;
+ };
+ const auto data_mov =
+ wrap_type<v1::Reshape, v0::Unsqueeze>({fusable_producer, ov::pass::pattern::any_input()},
+ rank_increase_by_one && ov::pass::pattern::consumers_count(1));
+
+ const auto other_input = ov::pass::pattern::any_input();
+ const auto eltwise_pattern =
+ wrap_type<op_util::BinaryElementwiseArithmetic>({data_mov, other_input}, ov::pass::pattern::has_static_rank());
+
+ ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](Matcher& m) {
+ const auto& pattern_map = m.get_pattern_value_map();
+
+ const auto eltwise = m.get_match_root();
+ if (transformation_callback(eltwise))
+ return false;
+
+ const auto rn = pattern_map.at(data_mov).get_node_shared_ptr();
+ const auto other = pattern_map.at(other_input);
+
+ const auto& in_ps = rn->get_input_partial_shape(0);
+ const auto& out_ps = rn->get_output_partial_shape(0);
+ const auto axes = op_util::get_unsqueeze_axes(in_ps, out_ps);
+ if (axes.size() != 1)
+ return false;
+ const auto axis = axes.front();
+ // The data movement output has to be the producer's shape with a unit dimension inserted at `axis`
+ auto expected = in_ps;
+ expected.insert(expected.begin() + axis, ov::Dimension(1));
+ if (!expected.same_scheme(out_ps))
+ return false;
+ // Reshape keeps the element count, so it can't reinterpret a single dynamic dimension, but may reorder several
+ if (ov::is_type<v1::Reshape>(rn)) {
+ const auto is_dynamic = [](const ov::Dimension& dim) {
+ return dim.is_dynamic();
+ };
+ if (std::count_if(in_ps.begin(), in_ps.end(), is_dynamic) > 1)
+ return false;
+ }
+
+ // The other input has to have the same rank and a unit dimension at `axis` to be squeezed
+ const auto& other_ps = other.get_partial_shape();
+ if (other_ps.rank() != out_ps.rank() || other_ps[axis] != 1)
+ return false;
+
+ // Eltwise(Reshape(P, axis), other) => Unsqueeze(Eltwise(P, Squeeze(other, axis)), axis)
+ const auto axis_const = v0::Constant::create(ov::element::i64, ov::Shape{1}, {axis});
+ const auto squeezed = std::make_shared<v0::Squeeze>(other, axis_const);
+ const auto eltwise_low = eltwise->clone_with_new_inputs({rn->input_value(0), squeezed});
+ const auto unsqueezed =
+ std::make_shared<v0::Unsqueeze>(eltwise_low, v0::Constant::create(ov::element::i64, ov::Shape{1}, {axis}));
+
+ ov::copy_runtime_info({eltwise, rn}, {squeezed, eltwise_low, unsqueezed});
+ return ov::replace_output_update_name(eltwise->output(0), unsqueezed->output(0));
+ };
+
+ const auto m = std::make_shared<Matcher>(eltwise_pattern, matcher_name);
+ register_matcher(m, callback);
+}
diff --git a/src/common/transformations/src/transformations/common_optimizations/nop_elimination.cpp b/src/common/transformations/src/transformations/common_optimizations/nop_elimination.cpp
index 06ff8426206..73c20390bf6 100644
--- a/src/common/transformations/src/transformations/common_optimizations/nop_elimination.cpp
+++ b/src/common/transformations/src/transformations/common_optimizations/nop_elimination.cpp
@@ -280,36 +280,6 @@ static bool replace_squeeze_unsqueeze(const std::shared_ptr<Node>& node) {
}
}
-static std::vector<int64_t> get_unsqueeze_axes(const PartialShape& data_shape, const PartialShape& out_shape) {
- std::vector<int64_t> axes;
- int64_t i = 0;
- for (auto o = 0; o < out_shape.rank().get_length(); o++) {
- if (i < data_shape.rank().get_length() && data_shape[i].same_scheme(out_shape[o])) {
- i += 1;
- continue;
- }
- if (out_shape[o].is_static() && out_shape[o] == 1) {
- axes.push_back(o);
- }
- }
- return axes;
-}
-
-static std::vector<int64_t> get_squeeze_axes(const PartialShape& data_shape, const PartialShape& out_shape) {
- std::vector<int64_t> axes;
- int64_t out_i = 0;
- for (auto i = 0; i < data_shape.rank().get_length(); i++) {
- if (out_i < out_shape.rank().get_length() && data_shape[i].same_scheme(out_shape[out_i])) {
- out_i += 1;
- continue;
- }
- if (data_shape[i].is_static() && data_shape[i] == 1) {
- axes.push_back(i);
- }
- }
- return axes;
-}
-
static bool eliminate_unsqueeze(const std::shared_ptr<Node>& node) {
auto out_shape = node->get_output_partial_shape(0);
// try to replace all squeeze/unsqueeze with reshape
@@ -342,14 +312,14 @@ static bool eliminate_unsqueeze(const std::shared_ptr<Node>& node) {
}
if (out_shape.rank().get_length() > data_shape.rank().get_length()) {
// check if single unsqueeze can handle this
- auto axes = get_unsqueeze_axes(data_shape, out_shape);
+ auto axes = op_util::get_unsqueeze_axes(data_shape, out_shape);
if (static_cast<int64_t>(axes.size()) + data_shape.rank().get_length() == out_shape.rank().get_length()) {
return replace_unsqueeze_only(axes);
}
}
if (out_shape.rank().get_length() < data_shape.rank().get_length()) {
// check if single squeeze can handle this
- auto axes = get_squeeze_axes(data_shape, out_shape);
+ auto axes = op_util::get_squeeze_axes(data_shape, out_shape);
if (data_shape.rank().get_length() - static_cast<int64_t>(axes.size()) == out_shape.rank().get_length()) {
auto axes_const = v0::Constant::create<int64_t>(element::i64, Shape{axes.size()}, axes);
auto new_sq = std::make_shared<v0::Squeeze>(input->input_value(0), axes_const);
@@ -368,7 +338,7 @@ static bool eliminate_unsqueeze(const std::shared_ptr<Node>& node) {
if (data_shape.rank().is_dynamic() || out_shape.rank().is_dynamic()) {
return false;
}
- auto axes = get_unsqueeze_axes(data_shape, out_shape);
+ auto axes = op_util::get_unsqueeze_axes(data_shape, out_shape);
return replace_unsqueeze_only(axes);
}
@@ -834,7 +804,7 @@ EliminateSqueeze::EliminateSqueeze() {
}
if (out_shape.rank().get_length() < data_shape.rank().get_length()) {
// check if single squeeze can handle this
- auto axes = get_squeeze_axes(data_shape, out_shape);
+ auto axes = op_util::get_squeeze_axes(data_shape, out_shape);
if (data_shape.rank().get_length() ==
out_shape.rank().get_length() + static_cast<int64_t>(axes.size())) {
return replace_squeeze_only(axes);
@@ -842,7 +812,7 @@ EliminateSqueeze::EliminateSqueeze() {
}
if (out_shape.rank().get_length() > data_shape.rank().get_length()) {
// check if single unsqueeze can handle this
- auto axes = get_unsqueeze_axes(data_shape, out_shape);
+ auto axes = op_util::get_unsqueeze_axes(data_shape, out_shape);
if (data_shape.rank().get_length() + static_cast<int64_t>(axes.size()) ==
out_shape.rank().get_length()) {
auto axes_const = v0::Constant::create<int64_t>(element::i64, Shape{axes.size()}, axes);
@@ -866,7 +836,7 @@ EliminateSqueeze::EliminateSqueeze() {
if (data_shape.rank().is_dynamic() || out_shape.rank().is_dynamic()) {
return false;
}
- auto axes = get_squeeze_axes(data_shape, out_shape);
+ auto axes = op_util::get_squeeze_axes(data_shape, out_shape);
return replace_squeeze_only(axes);
}
return false;
diff --git a/src/common/transformations/src/transformations/utils/utils.cpp b/src/common/transformations/src/transformations/utils/utils.cpp
index e94a8cb1194..43e1e77c7e2 100644
--- a/src/common/transformations/src/transformations/utils/utils.cpp
+++ b/src/common/transformations/src/transformations/utils/utils.cpp
@@ -628,6 +628,36 @@ bool is_constant_and_all_values_equal_int(const Output<Node>& output, const int6
return false;
}
+std::vector<int64_t> get_unsqueeze_axes(const PartialShape& data_shape, const PartialShape& out_shape) {
+ std::vector<int64_t> axes;
+ int64_t i = 0;
+ for (auto o = 0; o < out_shape.rank().get_length(); o++) {
+ if (i < data_shape.rank().get_length() && data_shape[i].same_scheme(out_shape[o])) {
+ i += 1;
+ continue;
+ }
+ if (out_shape[o].is_static() && out_shape[o] == 1) {
+ axes.push_back(o);
+ }
+ }
+ return axes;
+}
+
+std::vector<int64_t> get_squeeze_axes(const PartialShape& data_shape, const PartialShape& out_shape) {
+ std::vector<int64_t> axes;
+ int64_t out_i = 0;
+ for (auto i = 0; i < data_shape.rank().get_length(); i++) {
+ if (out_i < out_shape.rank().get_length() && data_shape[i].same_scheme(out_shape[out_i])) {
+ out_i += 1;
+ continue;
+ }
+ if (data_shape[i].is_static() && data_shape[i] == 1) {
+ axes.push_back(i);
+ }
+ }
+ return axes;
+}
+
bool process_subgraph(ov::pass::ModelPass& model_pass, const std::shared_ptr<Node>& node) {
bool changed = false;
diff --git a/src/common/transformations/tests/common_optimizations/move_eltwise_up_data_movement_test.cpp b/src/common/transformations/tests/common_optimizations/move_eltwise_up_data_movement_test.cpp
index 277fc45d95b..08c0888a141 100644
--- a/src/common/transformations/tests/common_optimizations/move_eltwise_up_data_movement_test.cpp
+++ b/src/common/transformations/tests/common_optimizations/move_eltwise_up_data_movement_test.cpp
@@ -8,13 +8,18 @@
#include <openvino/core/model.hpp>
#include <openvino/pass/manager.hpp>
+#include <optional>
+#include <string>
+#include <vector>
#include "common_test_utils/ov_test_utils.hpp"
+#include "openvino/core/except.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/clamp.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/fake_quantize.hpp"
#include "openvino/op/matmul.hpp"
+#include "openvino/op/maximum.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/parameter.hpp"
#include "openvino/op/relu.hpp"
@@ -22,6 +27,7 @@
#include "openvino/op/sigmoid.hpp"
#include "openvino/op/squeeze.hpp"
#include "openvino/op/strided_slice.hpp"
+#include "openvino/op/subtract.hpp"
#include "openvino/op/transpose.hpp"
#include "openvino/op/unsqueeze.hpp"
#include "ov_ops/type_relaxed.hpp"
@@ -818,3 +824,181 @@ TEST_F(MoveEltwiseUpThroughDataMovTest, PerChannelSqueezePDPDAxisOneReject) {
comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES);
comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
}
+
+// ---- Fusable-producer tests (non-constant second input) ----
+
+namespace {
+
+enum class Producer { MatMul, MatMulBias, Transpose };
+enum class DataMov { Reshape, Unsqueeze };
+enum class Eltwise { Add, Multiply, Subtract, Maximum };
+
+std::shared_ptr<v0::Constant> axis_const(int64_t axis) {
+ return v0::Constant::create(ov::element::i64, ov::Shape{1}, {axis});
+}
+
+std::shared_ptr<ov::Node> make_producer(Producer type, const ov::Output<ov::Node>& input) {
+ const auto make_matmul = [&]() {
+ return std::make_shared<v0::MatMul>(input, v0::Constant::create(ov::element::f16, ov::Shape{8, 16}, {0.1f}));
+ };
+ switch (type) {
+ case Producer::MatMul:
+ return make_matmul();
+ case Producer::MatMulBias:
+ return std::make_shared<v1::Add>(make_matmul(),
+ v0::Constant::create(ov::element::f16, ov::Shape{1, 1, 16}, {0.5f}));
+ case Producer::Transpose:
+ return std::make_shared<v1::Transpose>(input, v0::Constant::create(ov::element::i32, ov::Shape{3}, {0, 2, 1}));
+ }
+ OPENVINO_THROW("Unexpected producer type");
+}
+
+// `target` is the Reshape target shape (special_zero = true) or the Unsqueeze axes
+std::shared_ptr<ov::Node> make_data_mov(DataMov type,
+ const ov::Output<ov::Node>& input,
+ const std::vector<int64_t>& target) {
+ const auto target_const = v0::Constant::create(ov::element::i64, ov::Shape{target.size()}, target);
+ switch (type) {
+ case DataMov::Reshape:
+ return std::make_shared<v1::Reshape>(input, target_const, true);
+ case DataMov::Unsqueeze:
+ return std::make_shared<v0::Unsqueeze>(input, target_const);
+ }
+ OPENVINO_THROW("Unexpected data movement type");
+}
+
+std::shared_ptr<ov::Node> make_eltwise(Eltwise type, const ov::Output<ov::Node>& in0, const ov::Output<ov::Node>& in1) {
+ switch (type) {
+ case Eltwise::Add:
+ return std::make_shared<v1::Add>(in0, in1);
+ case Eltwise::Multiply:
+ return std::make_shared<v1::Multiply>(in0, in1);
+ case Eltwise::Subtract:
+ return std::make_shared<v1::Subtract>(in0, in1);
+ case Eltwise::Maximum:
+ return std::make_shared<v1::Maximum>(in0, in1);
+ }
+ OPENVINO_THROW("Unexpected eltwise type");
+}
+
+// Helper to register only the fusable-producer matcher. The matcher synthesizes the Squeeze and
+// Unsqueeze axis constants, so enable comparison of constant payloads as well.
+void register_fusable_pass(TransformationTestsF& test) {
+ test.manager.register_pass<ov::pass::MoveEltwiseUpThroughDataMovFusableProducer>(std::vector<ov::DiscreteTypeInfo>{
+ ov::op::v0::MatMul::get_type_info_static(),
+ ov::op::v1::Transpose::get_type_info_static(),
+ });
+ test.comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
+}
+
+struct FusableProducerParams {
+ std::string name;
+ Producer producer;
+ ov::PartialShape input_shape;
+ DataMov data_mov;
+ std::vector<int64_t> data_mov_target;
+ Eltwise eltwise;
+ size_t data_mov_idx;
+ ov::PartialShape other_shape;
+ std::optional<int64_t> expected_axis;
+};
+
+class MoveEltwiseUpThroughDataMovFusableTest : public testing::WithParamInterface<FusableProducerParams>,
+ public TransformationTestsF {
+public:
+ static std::string get_test_name(const testing::TestParamInfo<FusableProducerParams>& obj) {
+ return obj.param.name;
+ }
+};
+
+TEST_P(MoveEltwiseUpThroughDataMovFusableTest, CompareFunctions) {
+ const auto& p = GetParam();
+ {
+ auto input = std::make_shared<v0::Parameter>(ov::element::f16, p.input_shape);
+ auto other = std::make_shared<v0::Parameter>(ov::element::f16, p.other_shape);
+ auto data_mov = make_data_mov(p.data_mov, make_producer(p.producer, input), p.data_mov_target);
+ auto eltwise =
+ p.data_mov_idx == 0 ? make_eltwise(p.eltwise, data_mov, other) : make_eltwise(p.eltwise, other, data_mov);
+
+ model = std::make_shared<ov::Model>(ov::OutputVector{eltwise}, ov::ParameterVector{input, other});
+ register_fusable_pass(*this);
+ }
+ if (p.expected_axis) {
+ auto input = std::make_shared<v0::Parameter>(ov::element::f16, p.input_shape);
+ auto other = std::make_shared<v0::Parameter>(ov::element::f16, p.other_shape);
+ auto squeezed = std::make_shared<v0::Squeeze>(other, axis_const(*p.expected_axis));
+ auto eltwise = make_eltwise(p.eltwise, make_producer(p.producer, input), squeezed);
+ auto unsqueezed = std::make_shared<v0::Unsqueeze>(eltwise, axis_const(*p.expected_axis));
+
+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{unsqueezed}, ov::ParameterVector{input, other});
+ }
+}
+
+// clang-format off
+const std::vector<FusableProducerParams> fusable_producer_params = {
+ // name producer input_shape data_mov data_mov_target eltwise idx other_shape expected_axis
+ {"MatMulReshapeAdd", Producer::MatMul, {2, 4, 8}, DataMov::Reshape, {2, 1, 4, 16}, Eltwise::Add, 0, {2, 1, 4, 16}, 1},
+ {"TransposeReshapeAdd", Producer::Transpose, {2, 4, 16}, DataMov::Reshape, {2, 1, 16, 4}, Eltwise::Add, 0, {2, 1, 16, 4}, 1},
+ {"BiasAddReshapeAdd", Producer::MatMulBias, {2, 4, 8}, DataMov::Reshape, {2, 1, 4, 16}, Eltwise::Add, 0, {2, 1, 4, 16}, 1},
+ {"BiasAddReshapeAddDynamicBatch", Producer::MatMulBias, {-1, 4, 8}, DataMov::Reshape, {-1, 1, 4, 16}, Eltwise::Add, 0, {-1, 1, 4, 16}, 1},
+ {"MatMulUnsqueezeAdd", Producer::MatMul, {2, 4, 8}, DataMov::Unsqueeze, {1}, Eltwise::Add, 0, {2, 1, 4, 16}, 1},
+ {"MatMulUnsqueezeAxisZero", Producer::MatMul, {2, 4, 8}, DataMov::Unsqueeze, {0}, Eltwise::Add, 0, {1, 2, 4, 16}, 0},
+ {"MatMulUnsqueezeNegativeAxis", Producer::MatMul, {2, 4, 8}, DataMov::Unsqueeze, {-1}, Eltwise::Add, 0, {2, 4, 16, 1}, 3},
+ {"MatMulUnsqueezeTwoDynamicDims", Producer::MatMul, {-1, -1, 8}, DataMov::Unsqueeze, {1}, Eltwise::Add, 0, {-1, 1, -1, 16}, 1},
+ {"MatMulReshapeMultiply", Producer::MatMul, {2, 4, 8}, DataMov::Reshape, {2, 1, 4, 16}, Eltwise::Multiply, 0, {2, 1, 4, 16}, 1},
+ {"MatMulReshapeSubtract", Producer::MatMul, {2, 4, 8}, DataMov::Reshape, {2, 1, 4, 16}, Eltwise::Subtract, 0, {2, 1, 4, 16}, 1},
+ {"MatMulReshapeMaximumDataMovOnInput1", Producer::MatMul, {2, 4, 8}, DataMov::Reshape, {2, 1, 4, 16}, Eltwise::Maximum, 1, {2, 1, 4, 16}, 1},
+ {"MatMulReshapeSubtractDataMovOnInput1NoOp", Producer::MatMul, {2, 4, 8}, DataMov::Reshape, {2, 1, 4, 16}, Eltwise::Subtract, 1, {2, 1, 4, 16}, std::nullopt},
+ {"ReshapeSameRankNoOp", Producer::MatMul, {2, 4, 8}, DataMov::Reshape, {2, 2, 32}, Eltwise::Add, 0, {2, 2, 32}, std::nullopt},
+ {"ReshapeRankIncreaseWithSplitNoOp", Producer::MatMul, {2, 4, 8}, DataMov::Reshape, {2, 1, 2, 32}, Eltwise::Add, 0, {2, 1, 2, 32}, std::nullopt},
+ {"ReshapeRankIncreaseWithoutUnitDimNoOp", Producer::MatMul, {2, 4, 8}, DataMov::Reshape, {2, 2, 2, 16}, Eltwise::Add, 0, {2, 2, 2, 16}, std::nullopt},
+ // The Reshape inserts a unit dim but also reinterprets the dynamic dims: not an Unsqueeze-like change
+ {"ReshapeReorderedDynamicDimsNoOp", Producer::Transpose, {-1, -1, -1}, DataMov::Reshape, {0, 1, 0, -1}, Eltwise::Add, 0, {-1, 1, -1, -1}, std::nullopt},
+ // A Reshape that only appends a unit dim is rejected with >1 dynamic dim: the shapes can't prove the data is not reinterpreted
+ {"ReshapeTwoDynamicDimsNoOp", Producer::Transpose, {-1, -1, -1}, DataMov::Reshape, {0, 0, 0, 1}, Eltwise::Add, 0, {-1, -1, -1, 1}, std::nullopt},
+};
+// clang-format on
+
+INSTANTIATE_TEST_SUITE_P(MoveEltwiseUpThroughDataMovFusable,
+ MoveEltwiseUpThroughDataMovFusableTest,
+ testing::ValuesIn(fusable_producer_params),
+ MoveEltwiseUpThroughDataMovFusableTest::get_test_name);
+} // namespace
+
+// The eltwise feeds more than one op, which is the usual shape of a residual add, so it is still fused
+TEST_F(MoveEltwiseUpThroughDataMovTest, FusableEltwiseMultiConsumer) {
+ {
+ auto input = std::make_shared<v0::Parameter>(ov::element::f16, ov::PartialShape{2, 4, 8});
+ auto other = std::make_shared<v0::Parameter>(ov::element::f16, ov::PartialShape{2, 1, 4, 16});
+ auto reshape = make_data_mov(DataMov::Reshape, make_producer(Producer::MatMul, input), {2, 1, 4, 16});
+ auto add = std::make_shared<v1::Add>(reshape, other);
+ auto relu = std::make_shared<v0::Relu>(add);
+ auto sigmoid = std::make_shared<v0::Sigmoid>(add);
+
+ model = std::make_shared<ov::Model>(ov::OutputVector{relu, sigmoid}, ov::ParameterVector{input, other});
+ register_fusable_pass(*this);
+ }
+ {
+ auto input = std::make_shared<v0::Parameter>(ov::element::f16, ov::PartialShape{2, 4, 8});
+ auto other = std::make_shared<v0::Parameter>(ov::element::f16, ov::PartialShape{2, 1, 4, 16});
+ auto squeezed = std::make_shared<v0::Squeeze>(other, axis_const(1));
+ auto add = std::make_shared<v1::Add>(make_producer(Producer::MatMul, input), squeezed);
+ auto unsqueezed = std::make_shared<v0::Unsqueeze>(add, axis_const(1));
+ auto relu = std::make_shared<v0::Relu>(unsqueezed);
+ auto sigmoid = std::make_shared<v0::Sigmoid>(unsqueezed);
+
+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{relu, sigmoid}, ov::ParameterVector{input, other});
+ }
+}
+
+// The data movement op feeds more than one op, so the producer cannot fuse the eltwise as a post-op
+TEST_F(MoveEltwiseUpThroughDataMovTest, FusableReshapeMultiConsumerNoOp) {
+ auto input = std::make_shared<v0::Parameter>(ov::element::f16, ov::PartialShape{2, 4, 8});
+ auto other = std::make_shared<v0::Parameter>(ov::element::f16, ov::PartialShape{2, 1, 4, 16});
+ auto reshape = make_data_mov(DataMov::Reshape, make_producer(Producer::MatMul, input), {2, 1, 4, 16});
+ auto add = std::make_shared<v1::Add>(reshape, other);
+ auto relu = std::make_shared<v0::Relu>(reshape);
+
+ model = std::make_shared<ov::Model>(ov::OutputVector{add, relu}, ov::ParameterVector{input, other});
+ register_fusable_pass(*this);
+}
diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
index c64f49c84f1..19e61d4bf8f 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -1855,6 +1855,11 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
// This pass should be done after asymmetric quantization matching as it can move zp subtraction upper in the graph
manager.register_pass<ov::pass::MoveEltwiseUpThroughDataMovPerChannel>();
+ manager.register_pass<ov::pass::MoveEltwiseUpThroughDataMovFusableProducer>(std::vector<ov::DiscreteTypeInfo>{
+ ov::intel_gpu::op::FullyConnected::get_type_info_static(),
+ ov::op::v0::MatMul::get_type_info_static(),
+ ov::op::v1::Transpose::get_type_info_static(),
+ });
manager.register_pass<ov::intel_gpu::ConvertStridedSlicesToVariadicSplit>();