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] Extended ReduceMerge transformation (#33648)
Mikhail Ryzhov6696f96a3db3
diff --git a/src/common/transformations/src/transformations/common_optimizations/reduce_merge.cpp b/src/common/transformations/src/transformations/common_optimizations/reduce_merge.cppindex 8feecd00941..bf8eff5c18a 100644--- a/src/common/transformations/src/transformations/common_optimizations/reduce_merge.cpp+++ b/src/common/transformations/src/transformations/common_optimizations/reduce_merge.cpp@@ -5,10 +5,13 @@#include "transformations/common_optimizations/reduce_merge.hpp"#include <memory>+#include <set>+#include <vector>#include "itt.hpp"#include "openvino/core/graph_util.hpp"#include "openvino/core/rt_info.hpp"+#include "openvino/core/shape_util.hpp"#include "openvino/core/validation_util.hpp"#include "openvino/op/concat.hpp"#include "openvino/op/convert.hpp"@@ -33,9 +36,66 @@ namespace op_util = ov::op::util;namespace ov::pass {namespace {++/**+ * @brief Remaps reduction axes of a "bottom" Reduce operation through the axes removed by a preceding "top" Reduce.+ *+ * Given:+ * - @p axes_top : axes reduced (removed) by the first/top Reduce+ * - @p axes_bottom: axes reduced by the second/bottom Reduce in the resulting tensor rank (after @p axes_top removal)+ *+ * This function converts each axis in @p axes_bottom back to the corresponding axis index in the original tensor+ * (i.e., the tensor rank before @p axes_top axes were removed), producing a set of axes that can be used to express+ * the bottom reduction directly on the original input.+ *+ * @param axes_bottom Axes indices in the tensor after the top reduction has removed @p axes_top dimensions.+ * @param axes_top Axes indices removed by the top reduction in the original tensor.+ * @return A set of axes indices in the original tensor rank corresponding to @p axes_bottom.+ *+ * @note The result is returned as a sorted unique set.+ */+std::set<size_t> remap_axes(const ov::AxisSet& axes_bottom, const ov::AxisSet& axes_top) {+ // Compute the highest index that needs to be mapped to set the required rank+ // Create a vector of original axis indices: [0, 1, ..., required_rank-1]+ const size_t max_bottom_axis = *axes_bottom.rbegin();+ const size_t required_rank = max_bottom_axis + axes_top.size() + 1;+ std::vector<size_t> original_axes(required_rank);+ std::iota(original_axes.begin(), original_axes.end(), 0);++ // Remove axes_top from original_axes+ auto reduced_axes = ov::util::reduce(original_axes, axes_top);++ // Map each axes_bottom index in the reduced axes back to the original indices+ std::set<size_t> remapped_axes;+ for (const size_t bottom_axis : axes_bottom) {+ remapped_axes.insert(reduced_axes[bottom_axis]);+ }+ return remapped_axes;+}++ov::Output<ov::Node> cast(const ov::Output<ov::Node>& in, const element::Type& target_type) {+ if (in.get_element_type() != target_type) {+ const auto convert_op = std::make_shared<ov::op::v0::Convert>(in, target_type);+ copy_runtime_info(in.get_node_shared_ptr(), convert_op);+ return convert_op;+ }+ return in;+}++ov::Output<ov::Node> make_1d(const ov::Output<ov::Node>& in) {+ const auto& ps = in.get_partial_shape();+ if (ps.rank() == ov::Dimension(0)) {+ const auto unsq_axis = ov::op::v0::Constant::create(in.get_element_type(), {}, {0});+ const auto unsq = std::make_shared<ov::op::v0::Unsqueeze>(in, unsq_axis);+ copy_runtime_info(in.get_node_shared_ptr(), {unsq_axis, unsq});+ return unsq;+ }+ return in;+}+template <typename T>std::shared_ptr<Node> create_pattern() {- auto input = pattern::any_input();+ auto input = pattern::any_input(pattern::has_static_rank());auto first_axis = pattern::any_input();auto reduce = pattern::wrap_type<T>({input, first_axis});auto second_axis = pattern::any_input();@@ -58,54 +118,57 @@ bool fuse_reduce_operations(const std::shared_ptr<Node>& node) {return false;}- if (!top_reduce->get_keep_dims()) {- const auto first_axes = top_reduce->get_reduction_axes();- const auto second_axes = bottom_reduce->get_reduction_axes();+ const ov::AxisSet axes_top = top_reduce->get_reduction_axes();+ const ov::AxisSet axes_bottom = bottom_reduce->get_reduction_axes();+ std::shared_ptr<Node> axes = nullptr;- // check if each axis in the second set is smaller than every axis in the first set- if (!std::all_of(first_axes.begin(), first_axes.end(), [&second_axes](const size_t first_axis) {- return std::all_of(second_axes.begin(), second_axes.end(), [first_axis](const size_t second_axis) {- return first_axis > second_axis;- });- })) {- return false;- }- }+ if (!axes_top.empty() && !axes_bottom.empty()) {+ // if both axes are constants, we can merge them into a single constant+ std::set<size_t> fused_axes{axes_top.begin(), axes_top.end()};- // Align reduce axes constants by shape and type- const bool dtype_match =- top_reduce->input_value(1).get_element_type() == bottom_reduce->input_value(1).get_element_type();- for (auto& reduce : {top_reduce, bottom_reduce}) {- const auto reduce_axes_output = reduce->input_value(1);- const auto reduce_axes_node = reduce_axes_output.get_node_shared_ptr();- const auto reduce_axes_rank = reduce_axes_output.get_partial_shape().rank();- if (reduce_axes_rank == Dimension(0)) {- const auto unsqueeze_const = v0::Constant::create(reduce_axes_node->get_element_type(), {}, {0});- const auto unsqueeze = std::make_shared<v0::Unsqueeze>(reduce_axes_output, unsqueeze_const);- reduce->inputs()[1].replace_source_output(unsqueeze);- copy_runtime_info(reduce_axes_node, {unsqueeze_const, unsqueeze});+ if (top_reduce->get_keep_dims()) {+ fused_axes.insert(axes_bottom.begin(), axes_bottom.end());+ } else {+ std::set<size_t> axes_remapped = remap_axes(axes_bottom, axes_top);+ fused_axes.insert(axes_remapped.begin(), axes_remapped.end());}++ axes = op::v0::Constant::create(element::i64,+ Shape{fused_axes.size()},+ std::vector<int64_t>(fused_axes.begin(), fused_axes.end()));+ } else if (top_reduce->get_keep_dims()) {+ // if the axes input of any of reduce is not constant, but keep_dims is true,+ // we can merge them by concatenating axes inputs+ auto axes1_input = top_reduce->input_value(1);+ auto axes2_input = bottom_reduce->input_value(1);++ ov::Output<ov::Node> axes1 = axes1_input;+ ov::Output<ov::Node> axes2 = axes2_input;++ // Align reduce axes constants by type+ const bool dtype_match = axes1_input.get_element_type() == axes2_input.get_element_type();if (!dtype_match) {- const auto cast = std::make_shared<v0::Convert>(reduce->input_value(1), ov::element::i64);- reduce->inputs()[1].replace_source_output(cast);- copy_runtime_info(reduce_axes_node, cast);+ axes1 = cast(axes1_input, element::i64);+ axes2 = cast(axes2_input, element::i64);}- }- std::shared_ptr<Node> axes =- std::make_shared<v0::Concat>(OutputVector{top_reduce->input_value(1), bottom_reduce->input_value(1)},- int64_t(0));- if (auto constant = ov::util::get_constant_from_source(axes)) {- axes = constant;+ // Align reduce axes constants by shape+ axes1 = make_1d(axes1);+ axes2 = make_1d(axes2);++ axes = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{axes1, axes2}, int64_t(0));+ } else {+ return false;}- axes->set_friendly_name(bottom_reduce->get_friendly_name() + "/Axes");- auto new_reduce = bottom_reduce->copy_with_new_inputs({top_reduce->input_value(0), axes->get_default_output()});- new_reduce->set_friendly_name(bottom_reduce->get_friendly_name());- copy_runtime_info({top_reduce, bottom_reduce}, {axes, new_reduce});- ov::replace_node(bottom_reduce, new_reduce);+ auto reduce_fused = bottom_reduce->copy_with_new_inputs({top_reduce->input_value(0), axes});+ reduce_fused->set_friendly_name(bottom_reduce->get_friendly_name());+ copy_runtime_info({top_reduce, bottom_reduce}, {axes, reduce_fused});+ ov::replace_node(bottom_reduce, reduce_fused);+return true;}+} // namespaceReduceMerge::ReduceMerge() {diff --git a/src/common/transformations/tests/common_optimizations/reduce_merge.cpp b/src/common/transformations/tests/common_optimizations/reduce_merge.cppindex bb2c332f85b..4ac7d3c9082 100644--- a/src/common/transformations/tests/common_optimizations/reduce_merge.cpp+++ b/src/common/transformations/tests/common_optimizations/reduce_merge.cpp@@ -9,6 +9,7 @@#include "common_test_utils/ov_test_utils.hpp"#include "openvino/core/model.hpp"#include "openvino/op/concat.hpp"+#include "openvino/op/convert.hpp"#include "openvino/op/reduce_l1.hpp"#include "openvino/op/reduce_l2.hpp"#include "openvino/op/reduce_logical_and.hpp"@@ -18,6 +19,7 @@#include "openvino/op/reduce_min.hpp"#include "openvino/op/reduce_prod.hpp"#include "openvino/op/reduce_sum.hpp"+#include "openvino/op/unsqueeze.hpp"#include "openvino/opsets/opset9_decl.hpp"#include "openvino/pass/manager.hpp"@@ -35,7 +37,7 @@ TEST_F(TransformationTestsF, ReduceMergeReduceL1) {}{auto data = std::make_shared<op::v0::Parameter>(element::f32, Shape{3, 2});- auto axes = op::v0::Constant::create(element::i64, Shape{2}, {0, 0});+ auto axes = op::v0::Constant::create(element::i64, Shape{1}, {0});auto reduce = std::make_shared<opset9::ReduceL1>(data, axes, true);model_ref = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data});}@@ -55,7 +57,7 @@ TEST_F(TransformationTestsF, ReduceMergeReduceL2) {}{auto data = std::make_shared<op::v0::Parameter>(element::f32, Shape{3, 2});- auto axes = op::v0::Constant::create(element::i64, Shape{2}, {0, 0});+ auto axes = op::v0::Constant::create(element::i64, Shape{1}, {0});auto reduce = std::make_shared<opset9::ReduceL2>(data, axes, true);model_ref = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data});}@@ -69,14 +71,13 @@ TEST_F(TransformationTestsF, ReduceMergeReduceLogicalAnd) {auto reduce1_axis = op::v0::Constant::create(element::i64, Shape{1}, {0});auto reduce1 = std::make_shared<opset9::ReduceLogicalAnd>(data, reduce1_axis, true);auto reduce2_axis = op::v0::Constant::create(element::i64, Shape{1}, {0});- model = std::make_shared<Model>(- OutputVector{std::make_shared<opset9::ReduceLogicalAnd>(reduce1, reduce2_axis, true)},- ParameterVector{data});+ auto reduce2 = std::make_shared<opset9::ReduceLogicalAnd>(reduce1, reduce2_axis, true);+ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{data});manager.register_pass<ov::pass::ReduceMerge>();}{auto data = std::make_shared<op::v0::Parameter>(element::boolean, Shape{3, 2});- auto axes = op::v0::Constant::create(element::i64, Shape{2}, {0, 0});+ auto axes = op::v0::Constant::create(element::i64, Shape{1}, {0});auto reduce = std::make_shared<opset9::ReduceLogicalAnd>(data, axes, true);model_ref = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data});}@@ -90,14 +91,13 @@ TEST_F(TransformationTestsF, ReduceMergeReduceLogicalOr) {auto reduce1_axes = op::v0::Constant::create(element::i64, Shape{1}, {0});auto reduce1 = std::make_shared<opset9::ReduceLogicalOr>(data, reduce1_axes, true);auto reduce2_axis = op::v0::Constant::create(element::i64, Shape{1}, {0});- model = std::make_shared<Model>(- OutputVector{std::make_shared<opset9::ReduceLogicalOr>(reduce1, reduce2_axis, true)},- ParameterVector{data});+ auto reduce2 = std::make_shared<opset9::ReduceLogicalOr>(reduce1, reduce2_axis, true);+ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{data});manager.register_pass<ov::pass::ReduceMerge>();}{auto data = std::make_shared<op::v0::Parameter>(element::boolean, Shape{1});- auto axes = op::v0::Constant::create(element::i64, Shape{2}, {0, 0});+ auto axes = op::v0::Constant::create(element::i64, Shape{1}, {0});auto reduce = std::make_shared<opset9::ReduceLogicalOr>(data, axes, true);model_ref = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data});}@@ -111,13 +111,13 @@ TEST_F(TransformationTestsF, ReduceMergeReduceMax) {auto reduce1_axes = op::v0::Constant::create(element::i64, Shape{1}, {0});auto reduce1 = std::make_shared<opset9::ReduceMax>(data, reduce1_axes, true);auto reduce2_axis = op::v0::Constant::create(element::i64, Shape{1}, {0});- model = std::make_shared<Model>(OutputVector{std::make_shared<opset9::ReduceMax>(reduce1, reduce2_axis, true)},- ParameterVector{data});+ auto reduce2 = std::make_shared<opset9::ReduceMax>(reduce1, reduce2_axis, true);+ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{data});manager.register_pass<ov::pass::ReduceMerge>();}{auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{3, 2});- auto axes = op::v0::Constant::create(element::i64, Shape{2}, {0, 0});+ auto axes = op::v0::Constant::create(element::i64, Shape{1}, {0});auto reduce = std::make_shared<opset9::ReduceMax>(data, axes, true);model_ref = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data});}@@ -131,13 +131,13 @@ TEST_F(TransformationTestsF, ReduceMergeReduceMean) {auto reduce1_axes = op::v0::Constant::create(element::i64, Shape{1}, {0});auto reduce1 = std::make_shared<opset9::ReduceMean>(data, reduce1_axes, true);auto reduce2_axis = op::v0::Constant::create(element::i64, Shape{1}, {0});- model = std::make_shared<Model>(OutputVector{std::make_shared<opset9::ReduceMean>(reduce1, reduce2_axis, true)},- ParameterVector{data});+ auto reduce2 = std::make_shared<opset9::ReduceMean>(reduce1, reduce2_axis, true);+ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{data});manager.register_pass<ov::pass::ReduceMerge>();}{auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{3, 2});- auto axes = op::v0::Constant::create(element::i64, Shape{2}, {0, 0});+ auto axes = op::v0::Constant::create(element::i64, Shape{1}, {0});auto reduce = std::make_shared<opset9::ReduceMean>(data, axes, true);model_ref = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data});}@@ -151,13 +151,13 @@ TEST_F(TransformationTestsF, ReduceMergeReduceMin) {auto reduce1_axes = op::v0::Constant::create(element::i64, Shape{1}, {0});auto reduce1 = std::make_shared<opset9::ReduceMin>(data, reduce1_axes, true);auto reduce2_axis = op::v0::Constant::create(element::i64, Shape{1}, {0});- model = std::make_shared<Model>(OutputVector{std::make_shared<opset9::ReduceMin>(reduce1, reduce2_axis, true)},- ParameterVector{data});+ auto reduce2 = std::make_shared<opset9::ReduceMin>(reduce1, reduce2_axis, true);+ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{data});manager.register_pass<ov::pass::ReduceMerge>();}{auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{3, 2});- auto axes = op::v0::Constant::create(element::i64, Shape{2}, {0, 0});+ auto axes = op::v0::Constant::create(element::i64, Shape{1}, {0});auto reduce = std::make_shared<opset9::ReduceMin>(data, axes, true);model_ref = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data});}@@ -171,13 +171,13 @@ TEST_F(TransformationTestsF, ReduceMergeReduceProd) {auto reduce1_axes = op::v0::Constant::create(element::i64, Shape{1}, {0});auto reduce1 = std::make_shared<opset9::ReduceProd>(data, reduce1_axes, true);auto reduce2_axis = op::v0::Constant::create(element::i64, Shape{1}, {0});- model = std::make_shared<Model>(OutputVector{std::make_shared<opset9::ReduceProd>(reduce1, reduce2_axis, true)},- ParameterVector{data});+ auto reduce2 = std::make_shared<opset9::ReduceProd>(reduce1, reduce2_axis, true);+ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{data});manager.register_pass<ov::pass::ReduceMerge>();}{auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{3, 2});- auto axes = op::v0::Constant::create(element::i64, Shape{2}, {0, 0});+ auto axes = op::v0::Constant::create(element::i64, Shape{1}, {0});auto reduce = std::make_shared<opset9::ReduceProd>(data, axes, true);model_ref = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data});}@@ -191,13 +191,13 @@ TEST_F(TransformationTestsF, ReduceMergeReduceSum) {auto reduce1_axes = op::v0::Constant::create(element::i64, Shape{1}, {0});auto reduce1 = std::make_shared<opset9::ReduceSum>(data, reduce1_axes, true);auto reduce2_axis = op::v0::Constant::create(element::i64, Shape{1}, {0});- model = std::make_shared<Model>(OutputVector{std::make_shared<opset9::ReduceSum>(reduce1, reduce2_axis, true)},- ParameterVector{data});+ auto reduce2 = std::make_shared<opset9::ReduceSum>(reduce1, reduce2_axis, true);+ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{data});manager.register_pass<ov::pass::ReduceMerge>();}{auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{3, 2});- auto axes = op::v0::Constant::create(element::i64, Shape{2}, {0, 0});+ auto axes = op::v0::Constant::create(element::i64, Shape{1}, {0});auto reduce = std::make_shared<opset9::ReduceSum>(data, axes, true);model_ref = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data});}@@ -211,61 +211,51 @@ TEST_F(TransformationTestsF, ReduceMergeNoReduceDiffKeepDims) {auto reduce1_axes = op::v0::Constant::create(element::i64, Shape{1}, {0});auto reduce1 = std::make_shared<opset9::ReduceL1>(A, reduce1_axes, true);auto reduce2_axis = op::v0::Constant::create(element::i64, Shape{1}, {0});- model = std::make_shared<Model>(OutputVector{std::make_shared<opset9::ReduceL1>(reduce1, reduce2_axis, false)},- ParameterVector{A});+ auto reduce2 = std::make_shared<opset9::ReduceL1>(reduce1, reduce2_axis, false);+ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{A});manager.register_pass<ov::pass::ReduceMerge>();}- {- auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{3, 2});- auto reduce1_axes = op::v0::Constant::create(element::i64, Shape{1}, {0});- auto reduce1 = std::make_shared<opset9::ReduceL1>(data, reduce1_axes, true);- auto reduce2_axis = op::v0::Constant::create(element::i64, Shape{1}, {0});- model_ref =- std::make_shared<Model>(OutputVector{std::make_shared<opset9::ReduceL1>(reduce1, reduce2_axis, false)},- ParameterVector{data});- }+ { model_ref = model->clone(); }+comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);comparator.enable(FunctionsComparator::CmpValues::ACCURACY);}-TEST_F(TransformationTestsF, ReduceMergeNotPassInvalidAxes) {+TEST_F(TransformationTestsF, ReduceMergeKeepDimsFalse) {{auto data = std::make_shared<op::v0::Parameter>(element::f32, Shape{3, 2});auto reduce1_axis = op::v0::Constant::create(element::i64, Shape{1}, {0});auto reduce1 = std::make_shared<opset9::ReduceL2>(data, reduce1_axis, false);auto reduce2_axis = op::v0::Constant::create(element::i64, Shape{1}, {0});- model = std::make_shared<Model>(OutputVector{std::make_shared<opset9::ReduceL2>(reduce1, reduce2_axis, false)},- ParameterVector{data});+ auto reduce2 = std::make_shared<opset9::ReduceL2>(reduce1, reduce2_axis, false);+ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{data});manager.register_pass<ov::pass::ReduceMerge>();}{auto data = std::make_shared<op::v0::Parameter>(element::f32, Shape{3, 2});- auto reduce1_axis = op::v0::Constant::create(element::i64, Shape{1}, {0});- auto reduce1 = std::make_shared<opset9::ReduceL2>(data, reduce1_axis, false);- auto reduce2_axis = op::v0::Constant::create(element::i64, Shape{1}, {0});- model_ref =- std::make_shared<Model>(OutputVector{std::make_shared<opset9::ReduceL2>(reduce1, reduce2_axis, false)},- ParameterVector{data});+ auto axes = op::v0::Constant::create(element::i64, Shape{2}, {0, 1});+ auto reduce = std::make_shared<opset9::ReduceL2>(data, axes, false);+ model_ref = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data});}comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);comparator.enable(FunctionsComparator::CmpValues::ACCURACY);}-TEST_F(TransformationTestsF, ReduceMergeDynamicShapes) {+TEST_F(TransformationTestsF, ReduceMergeDynamicShapesKeepDimsTrue) {{auto data =std::make_shared<op::v0::Parameter>(element::i64, PartialShape{Dimension::dynamic(), Dimension::dynamic()});auto reduce1_axis = op::v0::Constant::create(element::i64, Shape{1}, {0});auto reduce1 = std::make_shared<opset9::ReduceL2>(data, reduce1_axis, true);auto reduce2_axis = op::v0::Constant::create(element::i64, Shape{1}, {0});- model = std::make_shared<Model>(OutputVector{std::make_shared<opset9::ReduceL2>(reduce1, reduce2_axis, true)},- ParameterVector{data});+ auto reduce2 = std::make_shared<opset9::ReduceL2>(reduce1, reduce2_axis, true);+ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{data});manager.register_pass<ov::pass::ReduceMerge>();}{auto data =std::make_shared<op::v0::Parameter>(element::i64, PartialShape{Dimension::dynamic(), Dimension::dynamic()});- auto axes = op::v0::Constant::create(element::i64, Shape{2}, {0, 0});+ auto axes = op::v0::Constant::create(element::i64, Shape{1}, {0});auto reduce = std::make_shared<opset9::ReduceL2>(data, axes, true);model_ref = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data});}@@ -273,6 +263,82 @@ TEST_F(TransformationTestsF, ReduceMergeDynamicShapes) {comparator.enable(FunctionsComparator::CmpValues::ACCURACY);}+TEST_F(TransformationTestsF, ReduceMergeDynamicShapesKeepDimsFalse) {+ auto data_shape = PartialShape{Dimension::dynamic(), Dimension::dynamic(), Dimension::dynamic()};+ {+ auto data = std::make_shared<op::v0::Parameter>(element::i64, data_shape);+ auto axis1 = op::v0::Constant::create(element::i64, Shape{1}, {0});+ auto reduce1 = std::make_shared<opset9::ReduceL2>(data, axis1, false);+ auto axis2 = op::v0::Constant::create(element::i64, Shape{1}, {0});+ auto reduce2 = std::make_shared<opset9::ReduceL2>(reduce1, axis2, false);+ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{data});+ manager.register_pass<ov::pass::ReduceMerge>();+ }+ {+ auto data = std::make_shared<op::v0::Parameter>(element::i64, data_shape);+ auto axes = op::v0::Constant::create(element::i64, Shape{2}, {0, 1});+ auto reduce = std::make_shared<opset9::ReduceL2>(data, axes, false);+ model_ref = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data});+ }+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);+}++TEST_F(TransformationTestsF, ReduceMergeDynamicRankConcatKeepDimsTrue) {+ auto data_shape = PartialShape::dynamic(ov::Rank::dynamic());+ {+ auto data = std::make_shared<op::v0::Parameter>(element::i64, data_shape);+ auto axis1 = op::v0::Constant::create(element::i64, Shape{}, {-1});+ auto reduce1 = std::make_shared<opset9::ReduceL2>(data, axis1, true);+ auto axis2 = op::v0::Constant::create(element::i64, Shape{2}, {0, 1});+ auto reduce2 = std::make_shared<opset9::ReduceL2>(reduce1, axis2, true);+ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{data});+ manager.register_pass<ov::pass::ReduceMerge>();+ }+ { model_ref = model->clone(); }+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);+}++TEST_F(TransformationTestsF, ReduceMergeDynamicRankKeepDimsTrue) {+ auto data_shape = PartialShape::dynamic(ov::Rank::dynamic());+ {+ auto data = std::make_shared<op::v0::Parameter>(element::i64, data_shape);+ auto reduce1_axis = op::v0::Constant::create(element::i64, Shape{1}, {0});+ auto reduce1 = std::make_shared<opset9::ReduceL2>(data, reduce1_axis, true);+ auto reduce2_axis = op::v0::Constant::create(element::i64, Shape{1}, {0});+ auto reduce2 = std::make_shared<opset9::ReduceL2>(reduce1, reduce2_axis, true);+ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{data});+ manager.register_pass<ov::pass::ReduceMerge>();+ }+ {+ auto data = std::make_shared<op::v0::Parameter>(element::i64, data_shape);+ auto axis1 = op::v0::Constant::create(element::i64, Shape{1}, {0});+ auto axis2 = op::v0::Constant::create(element::i64, Shape{1}, {0});+ auto axes = std::make_shared<opset9::Concat>(OutputVector{axis1, axis2}, 0);+ auto reduce = std::make_shared<opset9::ReduceL2>(data, axes, true);+ model = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data});+ }+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);+}++TEST_F(TransformationTestsF, ReduceMergeDynamicRankKeepDimsFalse) {+ auto data_shape = PartialShape::dynamic(ov::Rank::dynamic());+ {+ auto data = std::make_shared<op::v0::Parameter>(element::i64, data_shape);+ auto reduce1_axis = op::v0::Constant::create(element::i64, Shape{1}, {0});+ auto reduce1 = std::make_shared<opset9::ReduceL2>(data, reduce1_axis, false);+ auto reduce2_axis = op::v0::Constant::create(element::i64, Shape{1}, {0});+ auto reduce2 = std::make_shared<opset9::ReduceL2>(reduce1, reduce2_axis, false);+ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{data});+ manager.register_pass<ov::pass::ReduceMerge>();+ }+ { model_ref = model->clone(); }+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);+}+TEST_F(TransformationTestsF, ReduceMerge3ReducesL1) {{auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{3, 2, 4});@@ -287,7 +353,7 @@ TEST_F(TransformationTestsF, ReduceMerge3ReducesL1) {}{auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{3, 2, 4});- auto axes = op::v0::Constant::create(element::i64, Shape{3}, {0, 2, 1});+ auto axes = op::v0::Constant::create(element::i64, Shape{3}, {0, 1, 2});auto reduce = std::make_shared<opset9::ReduceL1>(data, axes, true);model_ref = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data});}@@ -317,6 +383,79 @@ TEST_F(TransformationTestsF, ReduceMergeConcatAxes) {comparator.enable(FunctionsComparator::CmpValues::ACCURACY);}+TEST_F(TransformationTestsF, ReduceMergeConcatAxesDifferentTypes) {+ {+ auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{3, 2, 4});+ auto axis1 = std::make_shared<op::v0::Parameter>(element::i64, Shape{1});+ auto reduce1 = std::make_shared<opset9::ReduceL1>(data, axis1, true);+ auto axis2 = std::make_shared<op::v0::Parameter>(element::i32, Shape{1});+ auto reduce2 = std::make_shared<opset9::ReduceL1>(reduce1, axis2, true);+ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{data, axis1, axis2});+ manager.register_pass<ov::pass::ReduceMerge>();+ }+ {+ auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{3, 2, 4});+ auto axis1 = std::make_shared<op::v0::Parameter>(element::i64, Shape{1});+ auto axis2 = std::make_shared<op::v0::Parameter>(element::i32, Shape{1});+ auto convert = std::make_shared<opset9::Convert>(axis2, element::i64);+ auto axes = std::make_shared<opset9::Concat>(OutputVector{axis1, convert}, 0);+ auto reduce = std::make_shared<opset9::ReduceL1>(data, axes, true);+ model_ref = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data, axis1, axis2});+ }+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);+}++TEST_F(TransformationTestsF, ReduceMergeConcatAxesScalars) {+ {+ auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{3, 2, 4});+ auto axis1 = std::make_shared<op::v0::Parameter>(element::i64, Shape{});+ auto reduce1 = std::make_shared<opset9::ReduceL1>(data, axis1, true);+ auto axis2 = std::make_shared<op::v0::Parameter>(element::i64, Shape{});+ auto reduce2 = std::make_shared<opset9::ReduceL1>(reduce1, axis2, true);+ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{data, axis1, axis2});+ manager.register_pass<ov::pass::ReduceMerge>();+ }+ {+ auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{3, 2, 4});+ auto axis1 = std::make_shared<op::v0::Parameter>(element::i64, Shape{});+ auto axis2 = std::make_shared<op::v0::Parameter>(element::i64, Shape{});+ auto axis1_unsq =+ std::make_shared<opset9::Unsqueeze>(axis1, op::v0::Constant::create(element::i64, Shape{}, {0}));+ auto axis2_unsq =+ std::make_shared<opset9::Unsqueeze>(axis2, op::v0::Constant::create(element::i64, Shape{}, {0}));+ auto axes = std::make_shared<opset9::Concat>(OutputVector{axis1_unsq, axis2_unsq}, 0);+ auto reduce = std::make_shared<opset9::ReduceL1>(data, axes, true);+ model_ref = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data, axis1, axis2});+ }+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);+}++TEST_F(TransformationTestsF, ReduceMergeConcatAxesScalarAnd1DTensor) {+ {+ auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{3, 2, 4});+ auto axis1 = std::make_shared<op::v0::Parameter>(element::i64, Shape{});+ auto reduce1 = std::make_shared<opset9::ReduceL1>(data, axis1, true);+ auto axis2 = std::make_shared<op::v0::Parameter>(element::i64, Shape{1});+ auto reduce2 = std::make_shared<opset9::ReduceL1>(reduce1, axis2, true);+ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{data, axis1, axis2});+ manager.register_pass<ov::pass::ReduceMerge>();+ }+ {+ auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{3, 2, 4});+ auto axis1 = std::make_shared<op::v0::Parameter>(element::i64, Shape{});+ auto axis1_unsq =+ std::make_shared<opset9::Unsqueeze>(axis1, op::v0::Constant::create(element::i64, Shape{}, {0}));+ auto axis2 = std::make_shared<op::v0::Parameter>(element::i64, Shape{1});+ auto axes = std::make_shared<opset9::Concat>(OutputVector{axis1_unsq, axis2}, 0);+ auto reduce = std::make_shared<opset9::ReduceL1>(data, axes, true);+ model_ref = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data, axis1, axis2});+ }+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);+}+TEST_F(TransformationTestsF, ReduceMergeDifferentShapesAndTypes) {{auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{3, 2});@@ -329,10 +468,145 @@ TEST_F(TransformationTestsF, ReduceMergeDifferentShapesAndTypes) {}{auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{3, 2});- auto axes = op::v0::Constant::create(element::i64, Shape{2}, {0, 0});+ auto axes = op::v0::Constant::create(element::i64, Shape{1}, {0});+ auto reduce = std::make_shared<opset9::ReduceMean>(data, axes, true);+ model_ref = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data});+ }+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);+}++TEST_F(TransformationTestsF, ReduceMergeNegativeAxisKeepDimsFalse) {+ {+ auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{1, 2, 3, 4});+ auto reduce1_axes = op::v0::Constant::create(element::i64, Shape{1}, {-1});+ auto reduce1 = std::make_shared<opset9::ReduceMean>(data, reduce1_axes, false);+ auto reduce2_axis = op::v0::Constant::create(element::i64, Shape{1}, {-1});+ auto reduce2 = std::make_shared<opset9::ReduceMean>(reduce1, reduce2_axis, false);+ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{data});+ manager.register_pass<ov::pass::ReduceMerge>();+ }+ {+ auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{1, 2, 3, 4});+ auto axes = op::v0::Constant::create(element::i64, Shape{2}, {2, 3});+ auto reduce = std::make_shared<opset9::ReduceMean>(data, axes, false);+ model_ref = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data});+ }+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);+}++TEST_F(TransformationTestsF, ReduceMergeNegativeAxisKeepDimsTrue) {+ {+ auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{1, 2, 3, 4});+ auto reduce1_axes = op::v0::Constant::create(element::i64, Shape{1}, {-1});+ auto reduce1 = std::make_shared<opset9::ReduceMean>(data, reduce1_axes, true);+ auto reduce2_axis = op::v0::Constant::create(element::i64, Shape{1}, {-1});+ auto reduce2 = std::make_shared<opset9::ReduceMean>(reduce1, reduce2_axis, true);+ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{data});+ manager.register_pass<ov::pass::ReduceMerge>();+ }+ {+ auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{1, 2, 3, 4});+ auto axes = op::v0::Constant::create(element::i64, Shape{1}, {3});auto reduce = std::make_shared<opset9::ReduceMean>(data, axes, true);model_ref = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data});}comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);comparator.enable(FunctionsComparator::CmpValues::ACCURACY);}++TEST_F(TransformationTestsF, ReduceMergeNotPassParamKeepDimsFalse) {+ {+ auto data = std::make_shared<op::v0::Parameter>(element::i64, Shape{3, 2, 4});+ auto axis1 = std::make_shared<op::v0::Parameter>(element::i64, Shape{1});+ auto reduce1 = std::make_shared<opset9::ReduceL1>(data, axis1, false);+ auto axis2 = std::make_shared<op::v0::Parameter>(element::i64, Shape{1});+ auto reduce2 = std::make_shared<opset9::ReduceL1>(reduce1, axis2, false);+ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{data, axis1, axis2});+ manager.register_pass<ov::pass::ReduceMerge>();+ }+ { model_ref = model->clone(); }+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);+}++struct ReduceMergeKeepDimsFalseDifferentAxesParams {+ const Shape data_shape;+ const std::vector<int64_t> reduce1_axes;+ const std::vector<int64_t> reduce2_axes;+ const std::vector<int64_t> expected_axes;+ const std::string name;+};++class ReduceMergeKeepDimsFalseDifferentAxes+ : public TransformationTestsF,+ public ::testing::WithParamInterface<ReduceMergeKeepDimsFalseDifferentAxesParams> {};++TEST_P(ReduceMergeKeepDimsFalseDifferentAxes, CompareFunctions) {+ const auto& p = GetParam();+ {+ auto data = std::make_shared<op::v0::Parameter>(element::f32, p.data_shape);++ auto reduce1_axis = op::v0::Constant::create(element::i64, Shape{p.reduce1_axes.size()}, p.reduce1_axes);+ auto reduce1 = std::make_shared<opset9::ReduceL2>(data, reduce1_axis, false);++ auto reduce2_axis = op::v0::Constant::create(element::i64, Shape{p.reduce2_axes.size()}, p.reduce2_axes);+ auto reduce2 = std::make_shared<opset9::ReduceL2>(reduce1, reduce2_axis, false);++ model = std::make_shared<Model>(OutputVector{reduce2}, ParameterVector{data});+ manager.register_pass<ov::pass::ReduceMerge>();+ }++ {+ auto data = std::make_shared<op::v0::Parameter>(element::f32, p.data_shape);+ auto axes = op::v0::Constant::create(element::i64, Shape{p.expected_axes.size()}, p.expected_axes);+ auto reduce = std::make_shared<opset9::ReduceL2>(data, axes, false);+ model_ref = std::make_shared<Model>(OutputVector{reduce}, ParameterVector{data});+ }++ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);+}++INSTANTIATE_TEST_SUITE_P(+ TransformationTestsF,+ ReduceMergeKeepDimsFalseDifferentAxes,+ ::testing::Values(+ ReduceMergeKeepDimsFalseDifferentAxesParams{Shape{1, 2, 3, 4, 5}, {0, 1}, {0, 1}, {0, 1, 2, 3}, "equal_axes"},+ ReduceMergeKeepDimsFalseDifferentAxesParams{Shape{1, 2, 3, 4, 5}, {-1}, {0, 1}, {0, 1, 4}, "negative_axes1"},+ ReduceMergeKeepDimsFalseDifferentAxesParams{Shape{1, 2, 3, 4, 5}, {0, 1}, {-1}, {0, 1, 4}, "negative_axes2"},+ ReduceMergeKeepDimsFalseDifferentAxesParams{Shape{1, 2, 3, 4, 5, 6},+ {0, 1},+ {2, 3},+ {0, 1, 4, 5},+ "diff_axes_all_greater"},+ ReduceMergeKeepDimsFalseDifferentAxesParams{Shape{1, 2, 3, 4, 5, 6},+ {2, 3},+ {0, 1},+ {0, 1, 2, 3},+ "diff_axes_all_less"},+ ReduceMergeKeepDimsFalseDifferentAxesParams{Shape{1, 2, 3, 4, 5, 6},+ {1, 3},+ {0, 2},+ {0, 1, 3, 4},+ "diff_axes_interleaved"},+ ReduceMergeKeepDimsFalseDifferentAxesParams{Shape{1, 2, 3, 4, 5, 6},+ {1, 2},+ {0, 3},+ {0, 1, 2, 5},+ "diff_axes_all_less_and_greater"},+ // 2D -> scalar (single-axis then remaining-axis)+ ReduceMergeKeepDimsFalseDifferentAxesParams{Shape{3, 4}, {1}, {0}, {0, 1}, "2D_to_scalar"},+ // 3D: reduce last, then first of remaining axes+ ReduceMergeKeepDimsFalseDifferentAxesParams{Shape{2, 3, 4}, {2}, {0}, {0, 2}, "3D_last_then_first"},+ // 4D: reduce middle, then reduce remaining tail+ ReduceMergeKeepDimsFalseDifferentAxesParams{Shape{2, 3, 4, 5}, {1}, {1, 2}, {1, 2, 3}, "4D_middle_then_tail"},+ // Non-sorted reduce1 axes (tests normalization/ordering)+ ReduceMergeKeepDimsFalseDifferentAxesParams{Shape{2, 3, 4, 5}, {2, 0}, {1}, {0, 2, 3}, "unsorted_reduce1_axes"},+ ReduceMergeKeepDimsFalseDifferentAxesParams{Shape{2, 3, 4, 5}, {1}, {1, 0}, {0, 1, 2}, "unsorted_reduce2_axes"},+ // Reduce many axes first, then reduce the last remaining axis+ ReduceMergeKeepDimsFalseDifferentAxesParams{Shape{1, 2, 3, 4}, {0, 1, 2}, {0}, {0, 1, 2, 3}, "reduce_all"}),+ [](const ::testing::TestParamInfo<ReduceMergeKeepDimsFalseDifferentAxesParams>& info) {+ return info.param.name;+ });\ No newline at end of file