Skip to content

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

[LPT] TransparentBaseTransformation: canBeTransformed extension (#31475)

Vladislav Golubev1ec9ffda2fa2
diff --git a/src/common/low_precision_transformations/src/broadcast.cpp b/src/common/low_precision_transformations/src/broadcast.cpp
index f1d12acd70d..e5a728e473f 100644
--- a/src/common/low_precision_transformations/src/broadcast.cpp
+++ b/src/common/low_precision_transformations/src/broadcast.cpp
@@ -33,7 +33,7 @@ BroadcastTransformation::BroadcastTransformation(const Params& params) : Transpa
}
bool BroadcastTransformation::canBeTransformed(const std::shared_ptr<ov::Node>& layer) const {
- if (!LayerTransformation::canBeTransformed(layer)) {
+ if (!TransparentBaseTransformation::canBeTransformed(layer)) {
return false;
}
diff --git a/src/common/low_precision_transformations/src/depth_to_space.cpp b/src/common/low_precision_transformations/src/depth_to_space.cpp
index ad4452b1b26..49d0ea1faa6 100644
--- a/src/common/low_precision_transformations/src/depth_to_space.cpp
+++ b/src/common/low_precision_transformations/src/depth_to_space.cpp
@@ -29,7 +29,7 @@ DepthToSpaceTransformation::DepthToSpaceTransformation(const Params& params) : T
}
bool DepthToSpaceTransformation::canBeTransformed(const std::shared_ptr<ov::Node>& layer) const {
- if (!LayerTransformation::canBeTransformed(layer)) {
+ if (!TransparentBaseTransformation::canBeTransformed(layer)) {
return false;
}
diff --git a/src/common/low_precision_transformations/src/transparent_base_transformation.cpp b/src/common/low_precision_transformations/src/transparent_base_transformation.cpp
index 8b7a94f5ef9..9dd2aae50c9 100644
--- a/src/common/low_precision_transformations/src/transparent_base_transformation.cpp
+++ b/src/common/low_precision_transformations/src/transparent_base_transformation.cpp
@@ -28,6 +28,21 @@ bool TransparentBaseTransformation::transform(ov::pass::pattern::Matcher &m) {
}
bool TransparentBaseTransformation::canBeTransformed(const std::shared_ptr<Node>& layer) const {
+ if (!LayerTransformation::canBeTransformed(layer)) {
+ return false;
+ }
+
+ const auto dequantization = NetworkHelper::getDequantization(layer, defaultPrecisions);
+ if (dequantization.multiply == nullptr) {
+ return false;
+ }
+
+ // If dequantization is placed on constant path and it doesn't change precision,
+ // there is no point in the DQ propagation since it can be constant folded
+ if (ov::is_type<ov::op::v0::Constant>(dequantization.data.get_node_shared_ptr()) &&
+ dequantization.data.get_element_type() == dequantization.multiply->get_output_element_type(0)) {
+ return false;
+ }
return true;
}
diff --git a/src/common/low_precision_transformations/tests/broadcast_transformation.cpp b/src/common/low_precision_transformations/tests/broadcast_transformation.cpp
index 1e178e0a1d8..b2d0f79af47 100644
--- a/src/common/low_precision_transformations/tests/broadcast_transformation.cpp
+++ b/src/common/low_precision_transformations/tests/broadcast_transformation.cpp
@@ -2,16 +2,17 @@
// SPDX-License-Identifier: Apache-2.0
//
-#include "layer_transformation.hpp"
+#include <gtest/gtest.h>
-#include <string>
#include <memory>
-
-#include <gtest/gtest.h>
+#include <string>
#include "common_test_utils/ov_test_utils.hpp"
+#include "layer_transformation.hpp"
#include "low_precision/broadcast.hpp"
+#include "openvino/op/broadcast.hpp"
#include "ov_lpt_models/broadcast.hpp"
+#include "ov_lpt_models/common/builders.hpp"
#include "simple_low_precision_transformer.hpp"
namespace {
@@ -30,7 +31,7 @@ public:
};
TestTransformationParams params;
- Shape tagetShape;
+ Shape targetShape;
Shape axesMapping;
Pattern actual;
Pattern expected;
@@ -50,7 +51,7 @@ public:
inputShape,
testValues.actual.precisionBeforeDequantization,
testValues.actual.dequantizationBefore,
- testValues.tagetShape,
+ testValues.targetShape,
testValues.axesMapping,
testValues.actual.dequantizationAfter);
@@ -63,7 +64,7 @@ public:
inputShape,
testValues.expected.precisionBeforeDequantization,
testValues.expected.dequantizationBefore,
- testValues.tagetShape,
+ testValues.targetShape,
testValues.axesMapping,
testValues.expected.dequantizationAfter);
}
@@ -74,7 +75,7 @@ public:
result <<
v1 << "_" <<
inputShape << "_" <<
- testValues.tagetShape << "_" <<
+ testValues.targetShape << "_" <<
testValues.axesMapping << "_" <<
testValues.actual.precisionBeforeDequantization << "_" <<
testValues.actual.dequantizationBefore << "_" <<
@@ -199,4 +200,19 @@ INSTANTIATE_TEST_SUITE_P(
BroadcastTransformation::getTestCaseName);
} // chw_broadcast
+// Note: in case of DQ ops on constant path which don't change precision,
+// the transformation should be skipped in order to constant fold the constant subgraph
+TEST_F(TransformationTestsF, smoke_LPT_BroadcastTransformation_DQ_on_constant_input) {
+ const auto data = ov::op::v0::Constant::create(ov::element::f16, {1, 3, 1, 1}, {1.f});
+ const DequantizationOperations dq_before_params{{}, {0.1f}, {0.2f}};
+ const auto dq_before = makeDequantization(data, dq_before_params);
+
+ const auto target_shape = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{4});
+ const auto bcast = std::make_shared<ov::op::v3::Broadcast>(dq_before, target_shape);
+ model = std::make_shared<ov::Model>(ov::OutputVector{bcast}, ov::ParameterVector{target_shape});
+
+ const auto default_params = TestTransformationParams::toParams(LayerTransformation::createParamsU8I8());
+ manager.register_pass<ov::pass::low_precision::BroadcastTransformation>(default_params);
+}
+
} // namespace
diff --git a/src/tests/ov_helpers/ov_lpt_models/include/ov_lpt_models/broadcast.hpp b/src/tests/ov_helpers/ov_lpt_models/include/ov_lpt_models/broadcast.hpp
index 4384fecd089..0df00adbf21 100644
--- a/src/tests/ov_helpers/ov_lpt_models/include/ov_lpt_models/broadcast.hpp
+++ b/src/tests/ov_helpers/ov_lpt_models/include/ov_lpt_models/broadcast.hpp
@@ -19,7 +19,7 @@ public:
const ov::PartialShape& inputShape,
const ov::element::Type precisionBeforeDequantization,
const ov::builder::subgraph::DequantizationOperations& dequantizationBefore,
- const Shape& tagetShape,
+ const Shape& targetShape,
const Shape& axesMapping,
const ov::builder::subgraph::DequantizationOperations& dequantizationAfter);
};
diff --git a/src/tests/ov_helpers/ov_lpt_models/src/broadcast.cpp b/src/tests/ov_helpers/ov_lpt_models/src/broadcast.cpp
index 1dcac844c98..b388af77504 100644
--- a/src/tests/ov_helpers/ov_lpt_models/src/broadcast.cpp
+++ b/src/tests/ov_helpers/ov_lpt_models/src/broadcast.cpp
@@ -18,16 +18,16 @@ namespace subgraph {
namespace {
template <typename T>
std::shared_ptr<ov::Node> make_broadcast(const std::shared_ptr<ov::Node>& parent,
- const Shape& tagetShape,
+ const Shape& targetShape,
const Shape& axesMapping) {
if (axesMapping.empty()) {
return std::make_shared<T>(
parent,
- std::make_shared<ov::opset1::Constant>(ov::element::i32, Shape{tagetShape.size()}, tagetShape));
+ std::make_shared<ov::opset1::Constant>(ov::element::i32, Shape{targetShape.size()}, targetShape));
}
return std::make_shared<T>(
parent,
- std::make_shared<ov::opset1::Constant>(ov::element::i32, Shape{tagetShape.size()}, tagetShape),
+ std::make_shared<ov::opset1::Constant>(ov::element::i32, Shape{targetShape.size()}, targetShape),
std::make_shared<ov::opset1::Constant>(ov::element::i32, Shape{axesMapping.size()}, axesMapping));
}
} // namespace
@@ -37,32 +37,20 @@ std::shared_ptr<ov::Model> BroadcastFunction::get(
const ov::PartialShape& inputShape,
const ov::element::Type precisionBeforeDequantization,
const ov::builder::subgraph::DequantizationOperations& dequantizationBefore,
- const Shape& tagetShape,
+ const Shape& targetShape,
const Shape& axesMapping,
const ov::builder::subgraph::DequantizationOperations& dequantizationAfter) {
const auto input = std::make_shared<ov::opset1::Parameter>(precisionBeforeDequantization, inputShape);
- std::shared_ptr<ov::Node> parent = input;
+ const auto dq_before = makeDequantization(input, dequantizationBefore);
- if (!dequantizationBefore.empty()) {
- parent = makeDequantization(parent, dequantizationBefore);
- }
-
- parent = v1 ?
- make_broadcast<ov::opset1::Broadcast>(parent, tagetShape, axesMapping) :
- make_broadcast<ov::opset3::Broadcast>(parent, tagetShape, axesMapping);
- parent->set_friendly_name("broadcast");
-
- if (!dequantizationAfter.empty()) {
- parent = makeDequantization(parent, dequantizationAfter);
- }
-
- const std::shared_ptr<ov::opset1::Result> result = std::make_shared<ov::opset1::Result>(parent);
+ const auto bcast = v1 ? make_broadcast<ov::opset1::Broadcast>(dq_before, targetShape, axesMapping)
+ : make_broadcast<ov::opset3::Broadcast>(dq_before, targetShape, axesMapping);
+ bcast->set_friendly_name("broadcast");
- const std::shared_ptr<ov::Model> function = std::make_shared<ov::Model>(
- ov::ResultVector{ result },
- std::vector<std::shared_ptr<ov::op::v0::Parameter>> { input },
- "BroadcastTransformation");
- return function;
+ const auto dq_after = makeDequantization(bcast, dequantizationAfter);
+ const auto model =
+ std::make_shared<ov::Model>(ov::OutputVector{dq_after}, ov::ParameterVector{input}, "BroadcastTransformation");
+ return model;
}
} // namespace subgraph