Skip to content

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

[LPT] ConcatTransformation: support scalar equal DQ propagation through dynamic dimension (#28350)

Vladislav Golubev6eb75bbeff18
diff --git a/src/common/low_precision_transformations/src/concat.cpp b/src/common/low_precision_transformations/src/concat.cpp
index fe39ed8d4f6..db77179a229 100644
--- a/src/common/low_precision_transformations/src/concat.cpp
+++ b/src/common/low_precision_transformations/src/concat.cpp
@@ -82,31 +82,42 @@ bool ConcatTransformation::transform(ov::pass::pattern::Matcher &m) {
allDequantizationShiftConvertAreNotZero = false;
}
- // constant shape must be broadcastable to the shape on data.
- auto broadcastElementWiseConst = [](std::shared_ptr<opset1::Constant> operation, const Shape targetShape) {
- auto targetShapeConst = std::make_shared<opset1::Constant>(element::i64, Shape{ targetShape.size() }, targetShape);
- auto broadcast = fold<ov::opset1::Broadcast>(operation, targetShapeConst);
- return broadcast;
+ const auto& concat_out_shape = concat->get_output_partial_shape(0);
+ const auto axis = ov::util::try_normalize_axis(concat->get_axis(), concat_out_shape.rank(), *concat);
+ const bool scalar_equal_constants_requested = concat_out_shape[axis].is_dynamic();
+
+ auto adaptConstForConcatenation = [scalar_equal_constants_requested](
+ const std::shared_ptr<opset1::Constant>& constant,
+ const Shape& targetShape) {
+ if (scalar_equal_constants_requested) {
+ OPENVINO_ASSERT(targetShape.empty(), "scalar_equal_constants_requested implies targetShape is empty");
+ return std::make_shared<opset1::Constant>(*constant, ov::Shape{});
+ } else {
+ auto targetShapeConst = std::make_shared<opset1::Constant>(element::i64, Shape{ targetShape.size() }, targetShape);
+ auto bcastedConst = ov::as_type_ptr<opset1::Constant>(fold<ov::opset1::Broadcast>(constant, targetShapeConst));
+ OPENVINO_ASSERT(bcastedConst, "adaptConstForConcatenation must return constant");
+ return bcastedConst;
+ }
};
- bool someDqInLowPrecision = std::any_of(
+ const bool someDqInLowPrecision = std::any_of(
layerDequantizations.begin(),
layerDequantizations.end(),
[](const FakeQuantizeDequantization& value) { return value.isLowPrecision(); });
- bool someDqInFpPrecision = std::any_of(
+ const bool someDqInFpPrecision = std::any_of(
layerDequantizations.begin(),
layerDequantizations.end(),
[](const FakeQuantizeDequantization& value) { return !value.isLowPrecision(); });
- bool DqWithDifferentPrecision = someDqInLowPrecision && someDqInFpPrecision;
- const auto axis =
- ov::util::try_normalize_axis(concat->get_axis(), concat->get_output_partial_shape(0).rank(), *concat);
+ const bool DqWithDifferentPrecision = someDqInLowPrecision && someDqInFpPrecision;
OutputVector dataNodes;
NodeVector convertNodes;
- NodeVector subConstants;
- NodeVector mulConstants;
+
+ using ConstVector = std::vector<std::shared_ptr<opset1::Constant>>;
+ ConstVector subConstants;
+ ConstVector mulConstants;
std::shared_ptr<opset1::Convert> subtractConvert = nullptr;
for (size_t i = 0; i < layerDequantizations.size(); ++i) {
const auto& dequantization = layerDequantizations[i];
@@ -121,8 +132,13 @@ bool ConcatTransformation::transform(ov::pass::pattern::Matcher &m) {
convertNodes.push_back(dequantization.convert);
}
- Shape targetShape(concat->get_input_partial_shape(i).rank().get_length(), 1ul);
- targetShape[axis] = concat->get_input_partial_shape(i)[axis].get_length();
+ const auto targetShape = [&]() {
+ if (scalar_equal_constants_requested)
+ return ov::Shape{};
+ Shape targetShape(concat->get_input_partial_shape(i).rank().get_length(), 1ul);
+ targetShape[axis] = concat->get_input_partial_shape(i)[axis].get_length();
+ return targetShape;
+ }();
if (!allDequantizationShiftAreZero) {
auto subtractInput = dequantization.subtract == nullptr ?
@@ -132,13 +148,15 @@ bool ConcatTransformation::transform(ov::pass::pattern::Matcher &m) {
deqPrecision),
targetShape,
std::vector<float>({ 0.f })) :
- broadcastElementWiseConst(dequantization.subtractConstant, targetShape);
+ adaptConstForConcatenation(dequantization.subtractConstant, targetShape);
if (allDequantizationShiftConvertAreNotZero) {
if (subtractConvert == nullptr && dequantization.subtractConvert != nullptr) {
subtractConvert = dequantization.subtractConvert;
}
} else if (dequantization.subtractConvert != nullptr) {
- subtractInput = foldConvert(subtractInput, dequantization.subtractConvert->get_convert_element_type());
+ const auto& dstType = dequantization.subtractConvert->get_convert_element_type();
+ subtractInput = ov::as_type_ptr<opset1::Constant>(foldConvert(subtractInput, dstType));
+ OPENVINO_ASSERT(subtractInput, "foldConvert must finish successfully for the concatenated subtract constant");
NetworkHelper::copyInfo(dequantization.subtractConvert, subtractInput);
}
subConstants.push_back(subtractInput);
@@ -147,7 +165,7 @@ bool ConcatTransformation::transform(ov::pass::pattern::Matcher &m) {
if (!allDequantizationMultiplyAreZero) {
mulConstants.push_back(dequantization.multiply == nullptr ?
std::make_shared<ov::opset1::Constant>(deqPrecision, targetShape, std::vector<float>({ 1.0f })) :
- broadcastElementWiseConst(dequantization.multiplyConstant, targetShape));
+ adaptConstForConcatenation(dequantization.multiplyConstant, targetShape));
}
}
@@ -162,10 +180,31 @@ bool ConcatTransformation::transform(ov::pass::pattern::Matcher &m) {
lastDequantization = convert;
}
+ auto concat_constants_if_needed = [&](const ConstVector& constants) -> std::shared_ptr<ov::Node> {
+ OPENVINO_ASSERT(!constants.empty(), "concat_constants_if_needed expects non empty constants vec");
+ if (constants.size() == 1ul) {
+ return constants[0];
+ }
+ if (scalar_equal_constants_requested) {
+ if (ov::shape_size(constants[0]->get_shape()) == 1) {
+ const auto ref_value = constants[0]->cast_vector<float>();
+ if (std::all_of(constants.cbegin() + 1, constants.cend(), [&ref_value](const auto& constant) {
+ return constant->template cast_vector<float>() == ref_value;
+ })) {
+ return constants[0];
+ }
+ }
+ OPENVINO_THROW("in case of dynamic concatenation dim all constants must be scalar and equal");
+ }
+ ov::OutputVector concatInputs;
+ std::transform(constants.begin(), constants.end(), std::back_inserter(concatInputs), [](const auto& constant) {
+ return constant->output(0);
+ });
+ return fold<ov::opset1::Concat>(concatInputs, axis);
+ };
+
if (!subConstants.empty()) {
- std::shared_ptr<ov::Node> subtractNode = subConstants.size() == 1ul ?
- subConstants[0] :
- ov::pass::low_precision::fold<ov::opset1::Concat>(subConstants, axis);
+ auto subtractNode = concat_constants_if_needed(subConstants);
if (subtractConvert != nullptr)
subtractNode = subtractConvert->clone_with_new_inputs({subtractNode});
const auto subtract = std::make_shared<opset1::Subtract>(
@@ -181,9 +220,7 @@ bool ConcatTransformation::transform(ov::pass::pattern::Matcher &m) {
const auto multiply = std::make_shared<ov::op::TypeRelaxed<opset1::Multiply>>(
opset1::Multiply(
lastDequantization,
- NetworkHelper::toScalarIfPossible(mulConstants.size() == 1ul ?
- mulConstants[0] :
- ov::pass::low_precision::fold<ov::opset1::Concat>(mulConstants, axis))),
+ NetworkHelper::toScalarIfPossible(concat_constants_if_needed(mulConstants))),
layerDequantizations[0].multiply->get_output_element_type(0));
NetworkHelper::copyInfo({ concat, multiply }, multiply);
@@ -216,9 +253,32 @@ bool ConcatTransformation::canBeTransformed(const std::shared_ptr<Node>& layer)
return false;
}
+ auto base_dq_check = [&](const FakeQuantizeDequantization& dequantization) {
+ return !dequantization.empty() && (!updatePrecisions || dequantization.isLowPrecision());
+ };
+
const size_t normalizedAxis = ov::util::try_normalize_axis(axis, outRank, *concat);
if (outPShape[normalizedAxis].is_dynamic()) {
- return false;
+ // in case of dynamic dimension we can propagate all dequantizations only if they are all scalar and equal,
+ // since DQ broadcast is impossible (requested shape is unknown), and only single scalar DQ after Concat can be set
+ const auto dequantization_ref = NetworkHelper::getDequantization(concat, defaultPrecisions, 0);
+ if (!base_dq_check(dequantization_ref) || !dequantization_ref.isPerTensor())
+ return false;
+
+ auto extract_values = [](const std::shared_ptr<ov::op::v0::Constant>& constant) {
+ return constant ? constant->cast_vector<float>() : std::vector<float>();
+ };
+ const auto ref_shifts = extract_values(dequantization_ref.subtractConstant);
+ const auto ref_scales = extract_values(dequantization_ref.multiplyConstant);
+
+ for (size_t i = 1ul; i < concat->get_input_size(); i++) {
+ const auto cur_dequantization = NetworkHelper::getDequantization(concat, defaultPrecisions, i);
+ if (!base_dq_check(dequantization_ref) ||
+ ref_shifts != extract_values(cur_dequantization.subtractConstant) ||
+ ref_scales != extract_values(cur_dequantization.multiplyConstant))
+ return false;
+ }
+ return true;
}
auto checkConstShape = [&normalizedAxis, &outRank](const std::shared_ptr<opset1::Constant>& constant) {
@@ -235,7 +295,6 @@ bool ConcatTransformation::canBeTransformed(const std::shared_ptr<Node>& layer)
};
const auto check_const_precision = [](
- const FakeQuantizeDequantization& dequantization,
const std::shared_ptr<Node>& constant,
ov::element::Type& const_precision) {
if (constant == nullptr) {
@@ -253,9 +312,8 @@ bool ConcatTransformation::canBeTransformed(const std::shared_ptr<Node>& layer)
for (size_t i = 0ul; i < concat->get_input_size(); i++) {
const FakeQuantizeDequantization dequantization = NetworkHelper::getDequantization(concat, defaultPrecisions, i);
- if (dequantization.empty() || (updatePrecisions && !dequantization.isLowPrecision())) {
+ if (!base_dq_check(dequantization))
return false;
- }
if (((dequantization.subtract != nullptr) && (!checkConstShape(dequantization.subtractConstant))) ||
((dequantization.multiply != nullptr) && (!checkConstShape(dequantization.multiplyConstant)))) {
@@ -268,9 +326,9 @@ bool ConcatTransformation::canBeTransformed(const std::shared_ptr<Node>& layer)
return false;
}
- if (!check_const_precision(dequantization, dequantization.subtractConvert, const_precision) ||
- ((dequantization.subtractConvert == nullptr) && !check_const_precision(dequantization, dequantization.subtractConstant, const_precision)) ||
- !check_const_precision(dequantization, dequantization.multiplyConstant, const_precision)) {
+ if (!check_const_precision(dequantization.subtractConvert, const_precision) ||
+ ((dequantization.subtractConvert == nullptr) && !check_const_precision(dequantization.subtractConstant, const_precision)) ||
+ !check_const_precision(dequantization.multiplyConstant, const_precision)) {
return false;
}
}
diff --git a/src/common/low_precision_transformations/tests/concat_transformation.cpp b/src/common/low_precision_transformations/tests/concat_transformation.cpp
index 4d6973f3f44..d833e21ad81 100644
--- a/src/common/low_precision_transformations/tests/concat_transformation.cpp
+++ b/src/common/low_precision_transformations/tests/concat_transformation.cpp
@@ -128,6 +128,110 @@ const std::vector<ConcatTransformationTestValues> testValues = {
{ov::element::f32, {128.f}, {0.1f}}
}
},
+ // dynamic concatenation axis, but the same per-tensor values
+ {
+ {{1, -1, 4, 4}, {1, -1, 4, 4}},
+ std::int64_t{1},
+ LayerTransformation::createParamsU8I8(),
+ {
+ ov::element::u8,
+ {
+ {ov::element::f32, {128.f}, {0.1f}},
+ {ov::element::f32, {128.f}, {0.1f}}
+ }
+ },
+ {
+ ov::element::u8,
+ {{}, {}},
+ ov::element::u8,
+ {ov::element::f32, {128.f}, {0.1f}}
+ }
+ },
+ // dynamic concatenation axis, but the same per-tensor values
+ {
+ {{1, -1, 4, 4}, {1, -1, 4, 4}},
+ std::int64_t{1},
+ LayerTransformation::createParamsU8I8(),
+ {
+ ov::element::u8,
+ {
+ {ov::element::f32, {}, {{0.1f}, ov::element::f32, {1, 1, 1}}},
+ {ov::element::f32, {}, {{0.1f}, ov::element::f32, {1, 1, 1}}}
+ }
+ },
+ {
+ ov::element::u8,
+ {{}, {}},
+ ov::element::u8,
+ {ov::element::f32, {}, {0.1f}}
+ }
+ },
+ // dynamic concatenation axis, dq don't match
+ {
+ {{1, -1, 4, 4}, {1, -1, 4, 4}},
+ std::int64_t{1},
+ LayerTransformation::createParamsU8I8(),
+ {
+ ov::element::u8,
+ {
+ {ov::element::f32, {128.f}, {0.1f}},
+ {ov::element::f32, {}, {0.1f}}
+ }
+ },
+ {
+ ov::element::u8,
+ {
+ {ov::element::f32, {128.f}, {0.1f}},
+ {ov::element::f32, {}, {0.1f}}
+ },
+ ov::element::f32,
+ {}
+ }
+ },
+ // dynamic concatenation axis, different per-tensor values
+ {
+ {{1, -1, 4, 4}, {1, -1, 4, 4}},
+ std::int64_t{1},
+ LayerTransformation::createParamsU8I8(),
+ {
+ ov::element::u8,
+ {
+ {ov::element::f32, {128.f}, {0.1f}},
+ {ov::element::f32, {128.f}, {10.f}}
+ }
+ },
+ {
+ ov::element::u8,
+ {
+ {ov::element::f32, {128.f}, {0.1f}},
+ {ov::element::f32, {128.f}, {10.f}}
+ },
+ ov::element::f32,
+ {}
+ }
+ },
+ // dynamic output concatenation axis, but one input dim is static
+ {
+ {{1, -1, 4, 4}, {1, 3, 4, 4}},
+ std::int64_t{1},
+ LayerTransformation::createParamsU8I8(),
+ {
+ ov::element::u8,
+ {
+ {ov::element::f32, {128.f}, {0.1f}},
+ {ov::element::f32, {{128.f, 64.f, 128.f}}, {{10.f, 1.f, 10.f}}}
+ }
+ },
+ {
+ ov::element::u8,
+ {
+ {ov::element::f32, {128.f}, {0.1f}},
+ {ov::element::f32, {{128.f, 64.f, 128.f}}, {{10.f, 1.f, 10.f}}}
+ },
+ ov::element::f32,
+ {}
+ }
+ },
{
{{1, 3, 4, 4}, {1, 3, 4, 4}},
std::int64_t{1},