[LPT] Fixes for the cases with convert before subtraction constant (#12835)

This commit is contained in:
Vladislav Golubev authored and GitHub committed 2022-09-06 20:41:29 +03:00
1 parent 9d55355daf
commit 88e4ac5e53
12 files changed
+224 -5

No files matched your search

@@ -128,6 +128,10 @@ public:
const element::Type deqPrecision = element::f32,
std::shared_ptr<ngraph::Node> input = nullptr);
static std::shared_ptr<ngraph::Node> makeDequantizationSubtract(
const ngraph::Output<ngraph::Node>& parent,
const ngraph::Output<ngraph::Node>& subtract_constant);
static FakeQuantizeDequantization createDequantizationFromFakeQuantize(
std::shared_ptr<opset1::FakeQuantize> fq,
element::Type precision,
@@ -156,7 +160,7 @@ public:
static std::shared_ptr<opset1::Constant> normalizeDequantizationShape(
const std::shared_ptr<Node>& eltwise,
const bool convertIsExpected = false);
const bool convertIsExpected = true);
// 1. remove Convert if possible
// 2. optimize Constant if possible
@@ -1187,6 +1187,16 @@ FakeQuantizeDequantization NetworkHelper::makeDequantization(
return FakeQuantizeDequantization(input, convert, subtract, nullptr, subtractConstant, multiply, multiplyConstant);
}
std::shared_ptr<ov::Node> NetworkHelper::makeDequantizationSubtract(
const ov::Output<ov::Node>& parent,
const ov::Output<ov::Node>& subtract_constant) {
return subtract_constant.get_element_type() != parent.get_element_type()
? std::dynamic_pointer_cast<ov::Node>(std::make_shared<opset1::Subtract>(
parent,
std::make_shared<opset1::Convert>(subtract_constant, parent.get_element_type())))
: std::make_shared<opset1::Subtract>(parent, subtract_constant);
}
FakeQuantizeDequantization NetworkHelper::createDequantizationFromFakeQuantize(
std::shared_ptr<opset1::FakeQuantize> fq,
element::Type precision,
@@ -1644,6 +1654,9 @@ NetworkHelper::InsertDequantizationResult NetworkHelper::moveDequantizationAfter
op::TemporaryReplaceOutputType(foldConvert(dequantization.subtractConstant, parentPrecision), element::f32).get());
ngraph::copy_runtime_info({ newOperation, parent }, parent);
} else {
// Subtract constant could be changed (including a shape) before propagation in some cases
// so it's necessary to compute the shape for a subtractConvert before creating a new subtract
dequantization.subtractConvert->validate_and_infer_types();
parent = std::make_shared<opset1::Subtract>(parent, dequantization.subtractConvert);
ngraph::copy_runtime_info({ newOperation, parent }, parent);
}
@@ -1736,6 +1749,9 @@ NetworkHelper::InsertDequantizationResult NetworkHelper::moveDequantizationBefor
foldConvert(subtractConstant, parentPrecision), element::f32).get());
parent->set_friendly_name(dequantization.subtract->get_friendly_name() + "_" + std::to_string(i + 1));
} else {
// Subtract constant could be changed (including a shape) before propagation in some cases
// so it's necessary to compute the shape for a subtractConvert before creating a new subtract
dequantization.subtractConvert->validate_and_infer_types();
parent = std::make_shared<opset1::Subtract>(parent, dequantization.subtractConvert);
}
ngraph::copy_runtime_info(dequantization.subtract, parent);
@@ -42,7 +42,7 @@ bool ShuffleChannelsTransformation::transform(TransformationContext& context, ng
auto dequantization = NetworkHelper::getDequantization(shuffleChannels, defaultPrecisions);
const auto shuffleDequantizationConstant = [&](const std::shared_ptr<Node>& eltwise) {
const auto normalizedConst = NetworkHelper::normalizeDequantizationShape(eltwise);
const auto normalizedConst = NetworkHelper::normalizeDequantizationShape(eltwise, true);
const auto constShape = normalizedConst->get_shape();
if (shape_size(constShape) == 1ul) {
@@ -90,7 +90,7 @@ bool SplitTransformation::transform(TransformationContext& context, ngraph::patt
}
if (dequantization.subtract) {
const auto subtract = std::make_shared<opset1::Subtract>(parent, splitedSub[i]);
const auto subtract = NetworkHelper::makeDequantizationSubtract(parent, splitedSub[i]);
copy_runtime_info({ newSplit, subtract }, subtract);
parent = subtract;
}
@@ -125,8 +125,9 @@ bool TransposeTransformation::canBeTransformed(const TransformationContext& cont
return true;
}();
const auto values = constant->cast_vector<float>();
// TODO: remove legacy limitation
if (!isPerTensor) {
const auto values = constant->cast_vector<float>();
if ((values.size() < 2ul) || (values[0] != 0) || (values[1] != 1)) {
return false;
}