Skip to content

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

Concat reducemax bug (#33384)

Yash Vijay839a40c4df14
diff --git a/src/common/transformations/src/transformations/common_optimizations/concat_reduce_fusion.cpp b/src/common/transformations/src/transformations/common_optimizations/concat_reduce_fusion.cpp
index f958bc047e1..bb11108f613 100644
--- a/src/common/transformations/src/transformations/common_optimizations/concat_reduce_fusion.cpp
+++ b/src/common/transformations/src/transformations/common_optimizations/concat_reduce_fusion.cpp
@@ -10,6 +10,7 @@
#include "itt.hpp"
#include "openvino/core/graph_util.hpp"
#include "openvino/core/rt_info.hpp"
+#include "openvino/core/validation_util.hpp"
#include "openvino/op/concat.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/maximum.hpp"
@@ -94,7 +95,9 @@ PullSqueezeThroughEltwise::PullSqueezeThroughEltwise() {
ReplaceConcatReduceByMinOrMax::ReplaceConcatReduceByMinOrMax() {
MATCHER_SCOPE(ReplaceConcatReduceByMinOrMax);
- auto concat_pattern = pattern::wrap_type<v0::Concat>({pattern::any_input(), pattern::any_input()});
+ auto concat_input1 = pattern::any_input(pattern::has_static_rank());
+ auto concat_input2 = pattern::any_input(pattern::has_static_rank());
+ auto concat_pattern = pattern::wrap_type<v0::Concat>({concat_input1, concat_input2});
auto reduce_axes_pattern = pattern::wrap_type<v0::Constant>();
auto reduce_pattern = pattern::wrap_type<v1::ReduceMin, v1::ReduceMax>({concat_pattern, reduce_axes_pattern});
@@ -107,6 +110,23 @@ ReplaceConcatReduceByMinOrMax::ReplaceConcatReduceByMinOrMax() {
if (!reduce || !concat)
return false;
+ // Fusing Concat+Reduce into Max/Min is only valid if the operation matches element-wise Max/Min.
+ // This requires that the concatenation only merged "single" elements along that axis,
+ // so that Max(A, B) is equivalent to ReduceMax(Concat(A, B)).
+ // If inputs have dimension > 1 along the concat axis, ReduceMax performs a reduction within A and within B,
+ // which Max/Min(A, B) does not capture (it broadcasts).
+ int64_t concat_axis = concat->get_axis();
+ for (const auto& input : concat->inputs()) {
+ const auto& p_shape = input.get_partial_shape();
+ // Rank is guaranteed static by pattern predicates
+ const int64_t rank = p_shape.rank().get_length();
+ const auto norm_axis = ov::util::normalize_axis(concat_axis, rank);
+
+ if (p_shape[norm_axis].is_dynamic() || p_shape[norm_axis].get_length() != 1) {
+ return false;
+ }
+ }
+
const auto& reduction_axes = reduce->get_reduction_axes();
if (reduction_axes.size() != 1 || concat->get_axis() != static_cast<int64_t>(*reduction_axes.begin())) {
return false;
diff --git a/src/common/transformations/tests/common_optimizations/concat_reduce_fusion.cpp b/src/common/transformations/tests/common_optimizations/concat_reduce_fusion.cpp
index 3593de063c3..d08ae6cc1da 100644
--- a/src/common/transformations/tests/common_optimizations/concat_reduce_fusion.cpp
+++ b/src/common/transformations/tests/common_optimizations/concat_reduce_fusion.cpp
@@ -14,6 +14,7 @@
#include "openvino/op/minimum.hpp"
#include "openvino/op/reduce_max.hpp"
#include "openvino/op/reduce_min.hpp"
+#include "openvino/op/reshape.hpp"
#include "openvino/op/squeeze.hpp"
#include "openvino/op/unsqueeze.hpp"
#include "openvino/pass/constant_folding.hpp"
@@ -107,12 +108,7 @@ TEST_F(TransformationTestsF, ConcatReduceMaxFusionDynamicRank) {
model = std::make_shared<Model>(OutputVector{reduce_max}, ParameterVector{left_input, right_input});
manager.register_pass<ov::pass::ConcatReduceFusion>();
}
- {
- auto left_input = std::make_shared<v0::Parameter>(element::f32, shape);
- auto right_input = std::make_shared<v0::Parameter>(element::f32, shape);
- auto maximum = std::make_shared<v1::Maximum>(left_input, right_input);
- model_ref = std::make_shared<Model>(OutputVector{maximum}, ParameterVector{left_input, right_input});
- }
+ // With dynamic rank, transformation should NOT apply - model remains unchanged
}
TEST_F(TransformationTestsF, ConcatReduceMinFusionDynamicShape) {
@@ -163,12 +159,7 @@ TEST_F(TransformationTestsF, ConcatReduceMinFusionDynamicRank) {
model = std::make_shared<Model>(OutputVector{reduce_max}, ParameterVector{left_input, right_input});
manager.register_pass<ov::pass::ConcatReduceFusion>();
}
- {
- auto left_input = std::make_shared<v0::Parameter>(element::f32, shape);
- auto right_input = std::make_shared<v0::Parameter>(element::f32, shape);
- auto maximum = std::make_shared<v1::Minimum>(left_input, right_input);
- model_ref = std::make_shared<Model>(OutputVector{maximum}, ParameterVector{left_input, right_input});
- }
+ // With dynamic rank, transformation should NOT apply - model remains unchanged
}
TEST_F(TransformationTestsF, PullSqueezeThroughEltwiseStaticShape) {
@@ -292,3 +283,178 @@ TEST_F(TransformationTestsF, PullSqueezeThroughEltwiseSqueezeEliminationDynamicR
model_ref = std::make_shared<Model>(OutputVector{add}, ParameterVector{left_input, right_input});
}
}
+
+// Test: Right input has dimension > 1 along concat axis - no fusion should occur
+TEST_F(TransformationTestsF, ConcatReduceNoFusionNoBroadcast) {
+ PartialShape left_shape{8, 1, 5};
+ PartialShape right_shape{8, 4, 5};
+ std::int64_t concat_axis = 1;
+ std::int64_t reduce_axis = 1;
+ {
+ auto left_input = std::make_shared<v0::Parameter>(element::f64, left_shape);
+ auto right_input = std::make_shared<v0::Parameter>(element::f64, right_shape);
+
+ auto concat = std::make_shared<v0::Concat>(NodeVector{left_input, right_input}, concat_axis);
+ auto reduce_min =
+ std::make_shared<v1::ReduceMin>(concat, v0::Constant::create(element::i64, Shape{}, {reduce_axis}), false);
+
+ model = std::make_shared<Model>(OutputVector{reduce_min}, ParameterVector{left_input, right_input});
+ manager.register_pass<ov::pass::ConcatReduceFusion>();
+ }
+ // Reference model should remain unchanged - no fusion should occur due to right input dim=4
+}
+
+// Test: ReduceMin with keep_dims=false on inputs with singleton dimension
+TEST_F(TransformationTestsF, ConcatReduceMinFusionNoUnsqueezeSingleDimStaticShape) {
+ PartialShape shape{2, 5, 3, 1, 4};
+ std::int64_t reduce_axis = 3;
+ {
+ auto left_input = std::make_shared<v0::Parameter>(element::f32, shape);
+ auto right_input = std::make_shared<v0::Parameter>(element::f32, shape);
+ auto concat = std::make_shared<v0::Concat>(NodeVector{left_input, right_input}, reduce_axis);
+
+ auto reduce_min =
+ std::make_shared<v1::ReduceMin>(concat, v0::Constant::create(element::i64, Shape{}, {reduce_axis}));
+
+ model = std::make_shared<Model>(OutputVector{reduce_min}, ParameterVector{left_input, right_input});
+ manager.register_pass<ov::pass::ConcatReduceFusion>();
+ }
+ {
+ auto left_input = std::make_shared<v0::Parameter>(element::f32, shape);
+ auto right_input = std::make_shared<v0::Parameter>(element::f32, shape);
+ auto minimum = std::make_shared<v1::Minimum>(left_input, right_input);
+ // Keep dims is false - so Squeeze has been inserted after Minimum
+ // Then Squeeze is replaced with Reshape by EliminateSqueeze pass
+ auto reshape = std::make_shared<v1::Reshape>(
+ minimum,
+ v0::Constant::create(element::i64, Shape{4}, std::vector<int64_t>{2, 5, 3, 4}),
+ false);
+ model_ref = std::make_shared<Model>(OutputVector{reshape}, ParameterVector{left_input, right_input});
+ }
+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);
+}
+
+// Test: ReduceMin with keep_dims=true on inputs with singleton dimension
+TEST_F(TransformationTestsF, ConcatReduceMinFusionNoUnsqueezeSingleDimStaticShapeKeepDims) {
+ PartialShape shape{2, 5, 3, 1, 4};
+ std::int64_t reduce_axis = 3;
+ bool keep_dims = true;
+ {
+ auto left_input = std::make_shared<v0::Parameter>(element::f32, shape);
+ auto right_input = std::make_shared<v0::Parameter>(element::f32, shape);
+ auto concat = std::make_shared<v0::Concat>(NodeVector{left_input, right_input}, reduce_axis);
+
+ auto reduce_min = std::make_shared<v1::ReduceMin>(concat,
+ v0::Constant::create(element::i64, Shape{}, {reduce_axis}),
+ keep_dims);
+ model = std::make_shared<Model>(OutputVector{reduce_min}, ParameterVector{left_input, right_input});
+ manager.register_pass<ov::pass::ConcatReduceFusion>();
+ }
+ {
+ auto left_input = std::make_shared<v0::Parameter>(element::f32, shape);
+ auto right_input = std::make_shared<v0::Parameter>(element::f32, shape);
+ auto minimum = std::make_shared<v1::Minimum>(left_input, right_input);
+ model_ref = std::make_shared<Model>(OutputVector{minimum}, ParameterVector{left_input, right_input});
+ }
+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);
+}
+
+// Test: ReduceMax with keep_dims=false on inputs with singleton dimension
+TEST_F(TransformationTestsF, ConcatReduceMaxFusionNoUnsqueezeSingleDimStaticShape) {
+ PartialShape shape{2, 5, 3, 1, 4};
+ std::int64_t reduce_axis = 3;
+ {
+ auto left_input = std::make_shared<v0::Parameter>(element::f32, shape);
+ auto right_input = std::make_shared<v0::Parameter>(element::f32, shape);
+ auto concat = std::make_shared<v0::Concat>(NodeVector{left_input, right_input}, reduce_axis);
+
+ auto reduce_max =
+ std::make_shared<v1::ReduceMax>(concat, v0::Constant::create(element::i64, Shape{}, {reduce_axis}));
+
+ model = std::make_shared<Model>(OutputVector{reduce_max}, ParameterVector{left_input, right_input});
+ manager.register_pass<ov::pass::ConcatReduceFusion>();
+ }
+ {
+ auto left_input = std::make_shared<v0::Parameter>(element::f32, shape);
+ auto right_input = std::make_shared<v0::Parameter>(element::f32, shape);
+ auto maximum = std::make_shared<v1::Maximum>(left_input, right_input);
+ // Keep dims is false - so Squeeze has been inserted after Maximum
+ // Then Squeeze is replaced with Reshape by EliminateSqueeze pass
+ auto reshape = std::make_shared<v1::Reshape>(
+ maximum,
+ v0::Constant::create(element::i64, Shape{4}, std::vector<int64_t>{2, 5, 3, 4}),
+ false);
+ model_ref = std::make_shared<Model>(OutputVector{reshape}, ParameterVector{left_input, right_input});
+ }
+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);
+}
+
+// Test: ReduceMax with keep_dims=true on inputs with singleton dimension
+TEST_F(TransformationTestsF, ConcatReduceMaxFusionNoUnsqueezeSingleDimStaticShapeKeepDims) {
+ PartialShape shape{2, 5, 3, 1, 4};
+ std::int64_t reduce_axis = 3;
+ bool keep_dims = true;
+ {
+ auto left_input = std::make_shared<v0::Parameter>(element::f32, shape);
+ auto right_input = std::make_shared<v0::Parameter>(element::f32, shape);
+ auto concat = std::make_shared<v0::Concat>(NodeVector{left_input, right_input}, reduce_axis);
+
+ auto reduce_max = std::make_shared<v1::ReduceMax>(concat,
+ v0::Constant::create(element::i64, Shape{}, {reduce_axis}),
+ keep_dims);
+ model = std::make_shared<Model>(OutputVector{reduce_max}, ParameterVector{left_input, right_input});
+ manager.register_pass<ov::pass::ConcatReduceFusion>();
+ }
+ {
+ auto left_input = std::make_shared<v0::Parameter>(element::f32, shape);
+ auto right_input = std::make_shared<v0::Parameter>(element::f32, shape);
+ auto maximum = std::make_shared<v1::Maximum>(left_input, right_input);
+ model_ref = std::make_shared<Model>(OutputVector{maximum}, ParameterVector{left_input, right_input});
+ }
+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);
+}
+
+// Test: Static rank with dynamic dimension along concat axis - no fusion should occur
+// This verifies that transformation is skipped when we cannot statically confirm dim=1
+TEST_F(TransformationTestsF, ConcatReduceMaxNoFusionDynamicDimAlongConcatAxis) {
+ // Shape has static rank but dynamic dimension at the concat axis
+ PartialShape shape{-1, -1, -1, -1};
+ std::int64_t concat_axis = 2;
+ {
+ auto left_input = std::make_shared<v0::Parameter>(element::f32, shape);
+ auto right_input = std::make_shared<v0::Parameter>(element::f32, shape);
+
+ // Direct concat without Unsqueeze - dimension along axis 2 is unknown
+ auto concat = std::make_shared<v0::Concat>(NodeVector{left_input, right_input}, concat_axis);
+
+ auto reduce_max =
+ std::make_shared<v1::ReduceMax>(concat, v0::Constant::create(element::i64, Shape{}, {concat_axis}));
+
+ model = std::make_shared<Model>(OutputVector{reduce_max}, ParameterVector{left_input, right_input});
+ manager.register_pass<ov::pass::ConcatReduceFusion>();
+ }
+ // No model_ref - transformation should NOT apply because we cannot confirm dim=1
+ // Model should remain unchanged
+}
+
+// Test: Static rank with dynamic dimension along concat axis for ReduceMin - no fusion should occur
+TEST_F(TransformationTestsF, ConcatReduceMinNoFusionDynamicDimAlongConcatAxis) {
+ // Shape has static rank but dynamic dimension at the concat axis
+ PartialShape shape{-1, -1, -1, -1};
+ std::int64_t concat_axis = 2;
+ {
+ auto left_input = std::make_shared<v0::Parameter>(element::f32, shape);
+ auto right_input = std::make_shared<v0::Parameter>(element::f32, shape);
+
+ // Direct concat without Unsqueeze - dimension along axis 2 is unknown
+ auto concat = std::make_shared<v0::Concat>(NodeVector{left_input, right_input}, concat_axis);
+
+ auto reduce_min =
+ std::make_shared<v1::ReduceMin>(concat, v0::Constant::create(element::i64, Shape{}, {concat_axis}));
+
+ model = std::make_shared<Model>(OutputVector{reduce_min}, ParameterVector{left_input, right_input});
+ manager.register_pass<ov::pass::ConcatReduceFusion>();
+ }
+ // No model_ref - transformation should NOT apply because we cannot confirm dim=1
+ // Model should remain unchanged
+}