Skip to content

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

[TRANSFORMATIONS] Fix broacasting check for MultiplyConvolutionFusion (#25876)

Andrii Staikov037518e4429e
diff --git a/src/common/transformations/include/transformations/common_optimizations/mul_conv_fusion.hpp b/src/common/transformations/include/transformations/common_optimizations/mul_conv_fusion.hpp
index 75d73b5f2a8..4fae74debc1 100644
--- a/src/common/transformations/include/transformations/common_optimizations/mul_conv_fusion.hpp
+++ b/src/common/transformations/include/transformations/common_optimizations/mul_conv_fusion.hpp
@@ -71,7 +71,7 @@ class TRANSFORMATIONS_API MultiplyGroupConvolutionBackpropDataFusion;
*
* Restrictions:
* - weights' shape is static
- * - if the constant input to Multiply has the same rank as 'input', the constant first dimension has to be 1
+ * - if the constant input to Multiply has the same rank as weights, the constant first dimension has to be 1
* - constant input to Multiply has to be broadcastable to weights when 'Convolution Op' is either Convolution or
* GroupConvolution
* - shape of a constant input to Multiply has to be in one of following forms: (1), (1, 1, ..., 1), (C, 1, ..., 1), (1,
diff --git a/src/common/transformations/include/transformations/utils/utils.hpp b/src/common/transformations/include/transformations/utils/utils.hpp
index 9b1ed41c9da..fed812ecc86 100644
--- a/src/common/transformations/include/transformations/utils/utils.hpp
+++ b/src/common/transformations/include/transformations/utils/utils.hpp
@@ -201,6 +201,12 @@ TRANSFORMATIONS_API bool constantIsEqualTo(const std::shared_ptr<ov::op::v0::Con
TRANSFORMATIONS_API bool has_f16_constants(const std::shared_ptr<const ov::Model>& function);
+/**
+ * \brief Check if 'other_shape' can be broadcasted to 'ref_shape'
+ *
+ * \param ref_shape The target shape we use as reference we are trying to broadcast to.
+ * \param other_shape The shape we use to check if it can be broadcasted to 'ref_shape'.
+ */
TRANSFORMATIONS_API bool check_for_broadcast(const PartialShape& ref_shape, const PartialShape& other_shape);
TRANSFORMATIONS_API std::shared_ptr<Node> activation(const std::string& activation_name, const Output<Node>& apply_to);
diff --git a/src/common/transformations/src/transformations/common_optimizations/add_fake_quantize_fusion.cpp b/src/common/transformations/src/transformations/common_optimizations/add_fake_quantize_fusion.cpp
index bf2e63270ba..9d14dc70bd4 100644
--- a/src/common/transformations/src/transformations/common_optimizations/add_fake_quantize_fusion.cpp
+++ b/src/common/transformations/src/transformations/common_optimizations/add_fake_quantize_fusion.cpp
@@ -50,7 +50,7 @@ ov::pass::AddFakeQuantizeFusion::AddFakeQuantizeFusion() {
return false;
auto const_shape = add_const->get_shape();
- if (ov::op::util::check_for_broadcast(input.get_partial_shape(), const_shape)) {
+ if (!ov::op::util::check_for_broadcast(input.get_partial_shape(), const_shape)) {
// We can't eliminate Add if Constant input broadcasts another input shape because
// when we reconnect input from Add to FQ won't broadcast given input, so it will result
// in shape collision.
diff --git a/src/common/transformations/src/transformations/common_optimizations/conv_mul_fusion.cpp b/src/common/transformations/src/transformations/common_optimizations/conv_mul_fusion.cpp
index e4e7866f3d9..622e8c55b63 100644
--- a/src/common/transformations/src/transformations/common_optimizations/conv_mul_fusion.cpp
+++ b/src/common/transformations/src/transformations/common_optimizations/conv_mul_fusion.cpp
@@ -46,7 +46,7 @@ ov::pass::ConvolutionMultiplyFusion::ConvolutionMultiplyFusion() {
auto expected_shape = Shape(weights_rank, 1);
expected_shape[1] = channel_dim;
- if (op::util::check_for_broadcast(expected_shape, const_shape)) {
+ if (!op::util::check_for_broadcast(expected_shape, const_shape)) {
return false;
}
@@ -110,7 +110,7 @@ ov::pass::GroupConvolutionMultiplyFusion::GroupConvolutionMultiplyFusion() {
auto expected_shape = Shape(weights_rank - 1, 1);
expected_shape[1] = G * O;
- if (op::util::check_for_broadcast(expected_shape, const_shape)) {
+ if (!op::util::check_for_broadcast(expected_shape, const_shape)) {
return false;
}
@@ -198,7 +198,7 @@ ov::pass::ConvolutionBackpropDataMultiplyFusion::ConvolutionBackpropDataMultiply
auto expected_shape = Shape(weights_rank, 1);
expected_shape[1] = channel_dim;
- if (op::util::check_for_broadcast(expected_shape, const_shape)) {
+ if (!op::util::check_for_broadcast(expected_shape, const_shape)) {
return false;
}
@@ -275,7 +275,7 @@ ov::pass::GroupConvolutionBackpropDataMultiplyFusion::GroupConvolutionBackpropDa
auto expected_shape = Shape(weights_rank - 1, 1);
expected_shape[1] = G * O;
- if (op::util::check_for_broadcast(expected_shape, const_shape)) {
+ if (!op::util::check_for_broadcast(expected_shape, const_shape)) {
return false;
}
diff --git a/src/common/transformations/src/transformations/common_optimizations/mul_conv_fusion.cpp b/src/common/transformations/src/transformations/common_optimizations/mul_conv_fusion.cpp
index c366fbbe2e2..2ef4486ffc4 100644
--- a/src/common/transformations/src/transformations/common_optimizations/mul_conv_fusion.cpp
+++ b/src/common/transformations/src/transformations/common_optimizations/mul_conv_fusion.cpp
@@ -21,7 +21,7 @@
ov::pass::MultiplyConvolutionFusion::MultiplyConvolutionFusion() {
MATCHER_SCOPE(MultiplyConvolutionFusion);
- auto input_pattern = pattern::any_input();
+ auto input_pattern = pattern::any_input(pattern::has_static_rank());
auto mul_const_pattern = ov::pass::pattern::wrap_type<ov::op::v0::Constant>();
auto mul_pattern = ov::pass::pattern::wrap_type<ov::op::v1::Multiply>({input_pattern, mul_const_pattern},
pattern::consumers_count(1));
@@ -38,14 +38,24 @@ ov::pass::MultiplyConvolutionFusion::MultiplyConvolutionFusion() {
const auto& weights = pattern_to_output.at(weights_pattern);
const auto& mul_const = pattern_to_output.at(mul_const_pattern);
+ const auto& input = pattern_to_output.at(input_pattern);
- const auto& weights_shape = weights.get_shape();
- const auto& mul_const_shape = mul_const.get_shape();
- // Check if mul_const if broadcastable to weights.
+ auto weights_shape = weights.get_partial_shape();
+ auto mul_const_shape = mul_const.get_partial_shape();
+ auto input_shape = input.get_partial_shape();
+
+ // Check if constant in multiply broadcasts input's shape.
+ // If this is the case, we cannot perform the transformation as
+ // 'bare' input's shape will not be aligned with weights.
+ if (ov::op::util::check_for_broadcast(mul_const_shape, input_shape)) {
+ return false;
+ }
+
+ // Check if mul_const is broadcastable to weights.
// Also if mul_const's rank matches weights rank and mul_const.shape[0] != 1
// then we can't fuse the multiply, since first dimension in mul_const corresponds to
// batch size, while first dimension in weights corresponds to output channel count
- if (op::util::check_for_broadcast(weights_shape, mul_const_shape) ||
+ if (!ov::op::util::check_for_broadcast(weights_shape, mul_const_shape) ||
(weights_shape.size() == mul_const_shape.size() && mul_const_shape[0] != 1)) {
return false;
}
@@ -55,7 +65,6 @@ ov::pass::MultiplyConvolutionFusion::MultiplyConvolutionFusion() {
if (!new_weights)
new_weights = weights_multiply;
- const auto& input = pattern_to_output.at(input_pattern);
const auto& conv = pattern_to_output.at(conv_pattern).get_node_shared_ptr();
auto new_conv = conv->clone_with_new_inputs({input, new_weights});
@@ -89,8 +98,19 @@ ov::pass::MultiplyGroupConvolutionFusion::MultiplyGroupConvolutionFusion() {
const auto& weights = pattern_to_output.at(weights_pattern);
std::shared_ptr<Node> mul_const = pattern_to_output.at(mul_const_pattern).get_node_shared_ptr();
-
const auto& weights_shape = weights.get_shape();
+
+ const auto& input = pattern_to_output.at(input_pattern);
+
+ auto mul_const_shape = mul_const->output(0).get_partial_shape();
+ auto input_shape = input.get_partial_shape();
+ // Check if constant in multiply broadcasts input's shape.
+ // If this is the case, we cannot perform the transformation as
+ // 'bare' input's shape will not be aligned with weights.
+ if (ov::op::util::check_for_broadcast(mul_const_shape, input_shape)) {
+ return false;
+ }
+
if (shape_size(mul_const->get_shape()) > 1) {
auto mul_const_shape = mul_const->get_shape();
// extend mul_const_shape rank with unit dimensions
@@ -106,7 +126,7 @@ ov::pass::MultiplyGroupConvolutionFusion::MultiplyGroupConvolutionFusion() {
// Reshape mul_const from shape (1, C, H, W) to (G, 1, C / G, H, W) to match GroupConvolution weights format
Shape new_shape{G, 1, C};
std::copy(mul_const_shape.begin() + 2, mul_const_shape.end(), std::back_inserter(new_shape));
- if (op::util::check_for_broadcast(weights_shape, new_shape)) {
+ if (!ov::op::util::check_for_broadcast(weights_shape, new_shape)) {
return false;
}
mul_const = std::make_shared<ov::op::v1::Reshape>(
@@ -120,7 +140,6 @@ ov::pass::MultiplyGroupConvolutionFusion::MultiplyGroupConvolutionFusion() {
if (!new_weights)
new_weights = weights_multiply;
- const auto& input = pattern_to_output.at(input_pattern);
const auto& conv = pattern_to_output.at(conv_pattern).get_node_shared_ptr();
auto new_conv = conv->clone_with_new_inputs({input, new_weights});
@@ -154,8 +173,18 @@ ov::pass::MultiplyConvolutionBackpropDataFusion::MultiplyConvolutionBackpropData
return false;
const auto& weights = pattern_to_output.at(weights_pattern);
- const auto& weights_shape = weights.get_shape();
+ auto weights_shape = weights.get_shape();
std::shared_ptr<Node> mul_const = pattern_to_output.at(mul_const_pattern).get_node_shared_ptr();
+ const auto& input = pattern_to_output.at(input_pattern);
+
+ auto mul_const_shape = mul_const->output(0).get_partial_shape();
+ auto input_shape = input.get_partial_shape();
+ // Check if constant in multiply broadcasts input's shape.
+ // If this is the case, we cannot perform the transformation as
+ // 'bare' input's shape will not be aligned with weights.
+ if (ov::op::util::check_for_broadcast(mul_const_shape, input_shape)) {
+ return false;
+ }
if (shape_size(mul_const->get_shape()) > 1) {
auto mul_const_shape = mul_const->get_shape();
@@ -174,7 +203,7 @@ ov::pass::MultiplyConvolutionBackpropDataFusion::MultiplyConvolutionBackpropData
// Reshape mul_const from shape (1, C, 1, 1) to (C, 1, 1, 1) to match ConvolutionBackpropData weights format
Shape new_shape{mul_const_shape[1], 1};
new_shape.insert(new_shape.end(), mul_const_shape.size() - 2, 1);
- if (op::util::check_for_broadcast(weights_shape, new_shape)) {
+ if (!ov::op::util::check_for_broadcast(weights_shape, new_shape)) {
return false;
}
mul_const = std::make_shared<ov::op::v1::Reshape>(
@@ -188,7 +217,6 @@ ov::pass::MultiplyConvolutionBackpropDataFusion::MultiplyConvolutionBackpropData
if (!new_weights)
new_weights = weights_multiply;
- const auto& input = pattern_to_output.at(input_pattern);
const auto& conv = pattern_to_output.at(conv_pattern).get_node_shared_ptr();
auto new_conv = conv->clone_with_new_inputs({input, new_weights});
@@ -223,6 +251,16 @@ ov::pass::MultiplyGroupConvolutionBackpropDataFusion::MultiplyGroupConvolutionBa
const auto& weights = pattern_to_output.at(weights_pattern);
std::shared_ptr<Node> mul_const = pattern_to_output.at(mul_const_pattern).get_node_shared_ptr();
+ const auto& input = pattern_to_output.at(input_pattern);
+
+ auto mul_const_shape = mul_const->output(0).get_partial_shape();
+ auto input_shape = input.get_partial_shape();
+ // Check if constant in multiply broadcasts input's shape.
+ // If this is the case, we cannot perform the transformation as
+ // 'bare' input's shape will not be aligned with weights.
+ if (ov::op::util::check_for_broadcast(mul_const_shape, input_shape)) {
+ return false;
+ }
const auto& weights_shape = weights.get_shape();
if (shape_size(mul_const->get_shape()) > 1) {
@@ -244,7 +282,7 @@ ov::pass::MultiplyGroupConvolutionBackpropDataFusion::MultiplyGroupConvolutionBa
auto C = mul_const_shape[1] / G;
Shape new_shape{G, C, 1};
new_shape.insert(new_shape.end(), mul_const_shape.size() - 2, 1);
- if (op::util::check_for_broadcast(weights_shape, new_shape)) {
+ if (!ov::op::util::check_for_broadcast(weights_shape, new_shape)) {
return false;
}
mul_const = std::make_shared<ov::op::v1::Reshape>(
@@ -258,7 +296,6 @@ ov::pass::MultiplyGroupConvolutionBackpropDataFusion::MultiplyGroupConvolutionBa
if (!new_weights)
new_weights = weights_multiply;
- const auto& input = pattern_to_output.at(input_pattern);
const auto& conv = pattern_to_output.at(conv_pattern).get_node_shared_ptr();
auto new_conv = conv->clone_with_new_inputs({input, new_weights});
diff --git a/src/common/transformations/src/transformations/common_optimizations/mul_fake_quantize_fusion.cpp b/src/common/transformations/src/transformations/common_optimizations/mul_fake_quantize_fusion.cpp
index 365a74f000b..fb1184365f4 100644
--- a/src/common/transformations/src/transformations/common_optimizations/mul_fake_quantize_fusion.cpp
+++ b/src/common/transformations/src/transformations/common_optimizations/mul_fake_quantize_fusion.cpp
@@ -46,7 +46,7 @@ ov::pass::MulFakeQuantizeFusion::MulFakeQuantizeFusion() {
return false;
auto const_shape = mul_const->get_shape();
- if (ov::op::util::check_for_broadcast(input.get_partial_shape(), const_shape)) {
+ if (!ov::op::util::check_for_broadcast(input.get_partial_shape(), const_shape)) {
// We can't eliminate Multiply if Constant input broadcasts another input shape because
// when we reconnect input from Multiply to FQ won't broadcast given input, so it will result
// in shape collision.
diff --git a/src/common/transformations/src/transformations/utils/utils.cpp b/src/common/transformations/src/transformations/utils/utils.cpp
index dd8efc7747d..eced4b1b095 100644
--- a/src/common/transformations/src/transformations/utils/utils.cpp
+++ b/src/common/transformations/src/transformations/utils/utils.cpp
@@ -135,24 +135,37 @@ bool has_f16_constants(const std::shared_ptr<const ov::Model>& function) {
}
bool check_for_broadcast(const ov::PartialShape& ref_shape, const ov::PartialShape& other_shape) {
- // Check that other_shape doesn't broadcast ref_shape
- if (ref_shape.rank().is_dynamic() || other_shape.rank().is_dynamic() || other_shape.size() > ref_shape.size()) {
- return true;
+ if (ref_shape.rank().is_dynamic() || other_shape.rank().is_dynamic()) {
+ return false;
+ }
+
+ // Check that other_shape's rank is not bigger
+ // than ref_shape's rank and the other way
+ // broadcasting is needed.
+ if (other_shape.size() > ref_shape.size()) {
+ return false;
}
auto ref_it = ref_shape.rbegin();
auto other_it = other_shape.rbegin();
- // Check that other_shape dims are equal to ref_shape dims
- // In case if other_shape rank is less than ref_shape rank
- // we stop comparison and return true
+
+ // Align shapes to the right and run iterator from
+ // the right of a smaller shape.
+ // Check if other_shape's dimension is equal to the
+ // corresponding dimension of ref_shape or if
+ // other_shape's dimension is equal to 1.
+ // (standard broadcasting rules)
while (other_it != other_shape.rend()) {
- if ((other_it->is_dynamic() || other_it->get_length() != 1) &&
- (ref_it->is_dynamic() || ref_it->get_length() == 1)) {
- return true;
+ if (other_it->is_dynamic() || ref_it->is_dynamic()) {
+ return false;
+ }
+
+ if (*other_it != *ref_it && *other_it != 1) {
+ return false;
}
++other_it;
++ref_it;
}
- return false;
+ return true;
}
std::shared_ptr<ov::Node> activation(const std::string& activation_name, const ov::Output<ov::Node>& apply_to) {