diff --git a/inference-engine/src/low_precision_transformations/src/move_fake_quantize.cpp b/inference-engine/src/low_precision_transformations/src/move_fake_quantize.cpp index cb08fb1c35c8b3..f8461f517db2b3 100644 --- a/inference-engine/src/low_precision_transformations/src/move_fake_quantize.cpp +++ b/inference-engine/src/low_precision_transformations/src/move_fake_quantize.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "low_precision/network_helper.hpp" @@ -20,7 +21,22 @@ namespace low_precision { NGRAPH_RTTI_DEFINITION(ngraph::pass::low_precision::MoveFakeQuantize, "MoveFakeQuantize", 0); MoveFakeQuantize::MoveFakeQuantize(const Params& params) : LayerTransformation(params) { - auto matcher = ngraph::pattern::wrap_type(); + auto concat = ngraph::pattern::wrap_type(); + auto operation = ngraph::pattern::wrap_type({ concat }); + auto input_low = ngraph::pattern::wrap_type(); + auto input_high = ngraph::pattern::wrap_type(); + auto output_low = ngraph::pattern::wrap_type(); + auto output_high = ngraph::pattern::wrap_type(); + auto fq_with_operation = ngraph::pattern::wrap_type({ operation, + input_low, + input_high, + output_low, + output_high}); + auto fq = ngraph::pattern::wrap_type({ concat, + input_low, + input_high, + output_low, + output_high }); ngraph::graph_rewrite_callback callback = [this](pattern::Matcher& m) { auto op = m.get_match_root(); @@ -31,7 +47,9 @@ MoveFakeQuantize::MoveFakeQuantize(const Params& params) : LayerTransformation(p return transform(*context, m); }; - auto m = std::make_shared(matcher, "MoveFakeQuantize"); + auto m = std::make_shared( + std::make_shared(OutputVector{fq, fq_with_operation}), + "MoveFakeQuantize"); this->register_matcher(m, callback); } @@ -39,14 +57,8 @@ bool MoveFakeQuantize::transform(TransformationContext& context, ngraph::pattern auto fq = m.get_match_root(); auto operation = fq->get_input_node_shared_ptr(0); - // TODO: temporary to enable other transformations <= update matcher instead this validation - if (!is_type(operation) && !is_type(operation)) { - return false; - } - - auto type = operation->get_type_name(); std::shared_ptr concat, fq1input, fq2input; - if (strcmp(type, "Concat") == 0) { + if (is_type(operation)) { concat = operation; fq1input = operation->get_input_node_shared_ptr(0); fq2input = operation->get_input_node_shared_ptr(1); @@ -54,9 +66,11 @@ bool MoveFakeQuantize::transform(TransformationContext& context, ngraph::pattern concat = operation->get_input_node_shared_ptr(0); auto input1 = concat->get_input_node_shared_ptr(0); auto input2 = concat->get_input_node_shared_ptr(1); - if (strcmp(type, "Relu") == 0) { + if (is_type(operation)) { fq1input = std::make_shared(input1->output(0)); fq2input = std::make_shared(input2->output(0)); + } else { + return false; } } @@ -66,21 +80,19 @@ bool MoveFakeQuantize::transform(TransformationContext& context, ngraph::pattern fq->get_input_node_shared_ptr(3), fq->get_input_node_shared_ptr(4), as_type_ptr(fq)->get_levels()); + fq1->set_friendly_name("concat_fq1"); auto fq2 = std::make_shared(fq2input, fq->get_input_node_shared_ptr(1), fq->get_input_node_shared_ptr(2), fq->get_input_node_shared_ptr(3), fq->get_input_node_shared_ptr(4), as_type_ptr(fq)->get_levels()); + fq2->set_friendly_name("concat_fq2"); auto new_concat = concat->clone_with_new_inputs({ fq1->output(0), fq2->output(0) }); - //auto& rtInfo = new_concat->get_rt_info(); - //new_concat->set_friendly_name("output"); - //rtInfo["Variant::std::string"] = std::make_shared>("concat"); - + new_concat->set_friendly_name(concat->get_friendly_name()); replace_node(concat, new_concat); replace_node(fq, new_concat); - updateOutput(context, new_concat, fq); return true; } diff --git a/inference-engine/tests/functional/inference_engine/lp_transformations/move_fake_quantize_for_concat_transformation.cpp b/inference-engine/tests/functional/inference_engine/lp_transformations/move_fake_quantize_transformation.cpp similarity index 98% rename from inference-engine/tests/functional/inference_engine/lp_transformations/move_fake_quantize_for_concat_transformation.cpp rename to inference-engine/tests/functional/inference_engine/lp_transformations/move_fake_quantize_transformation.cpp index 7621d008faa06f..5c5150445da7ca 100644 --- a/inference-engine/tests/functional/inference_engine/lp_transformations/move_fake_quantize_for_concat_transformation.cpp +++ b/inference-engine/tests/functional/inference_engine/lp_transformations/move_fake_quantize_transformation.cpp @@ -43,14 +43,14 @@ namespace { class MoveFakeQuantizeActualValues { public: - ngraph::builder::subgraph::FakeQuantizeOnDataWithConstant fakeQuantizeBefore1; //before1 + ngraph::builder::subgraph::FakeQuantizeOnDataWithConstant fakeQuantizeBefore1; ngraph::builder::subgraph::DequantizationOperations::Convert convertBefore1; ngraph::builder::subgraph::DequantizationOperations dequantizationBefore1; - ngraph::builder::subgraph::FakeQuantizeOnDataWithConstant fakeQuantizeBefore2; //before1 + ngraph::builder::subgraph::FakeQuantizeOnDataWithConstant fakeQuantizeBefore2; ngraph::builder::subgraph::DequantizationOperations::Convert convertBefore2; ngraph::builder::subgraph::DequantizationOperations dequantizationBefore2; std::string operation; - ngraph::builder::subgraph::FakeQuantizeOnDataWithConstant fakeQuantizeAfter; // after + ngraph::builder::subgraph::FakeQuantizeOnDataWithConstant fakeQuantizeAfter; ngraph::builder::subgraph::DequantizationOperations::Convert convertAfter; ngraph::builder::subgraph::DequantizationOperations dequantizationAfter; }; @@ -171,7 +171,6 @@ class MoveFakeQuantize : public LayerTransformation, public testing::WithParamIn ngraph::element::undefined, {}, testValues.axis); - auto supportedPrecisionsOnActivation = std::vector({ ngraph::pass::low_precision::OperationPrecisionRestriction::create({{0, testValues.params.precisionsOnActivations}}) }); @@ -184,10 +183,8 @@ class MoveFakeQuantize : public LayerTransformation, public testing::WithParamIn const auto params = TestTransformationParams::toParams(testValues.params); SimpleLowPrecisionTransformer transformer(supportedPrecisionsOnActivation, quantizationRestrictions); - //SimpleLowPrecisionTransformer transformer({}, {}); transformer.commonGraphRewrite->add_matcher(params); transformer.transform(actualFunction); - // dequantization output precision depends on input precision // to avoid huge amount of tests cases let's define dequantization output precision as input precision if (!testValues.result.dequantizationAfter.multiply.empty()) { diff --git a/inference-engine/tests/functional/plugin/cpu/shared_tests_instances/low_precision_transformations/move_fake_quantize_transformation.cpp b/inference-engine/tests/functional/plugin/cpu/shared_tests_instances/low_precision_transformations/move_fake_quantize_transformation.cpp index 29406d3dbc2085..33df9f64a825f4 100644 --- a/inference-engine/tests/functional/plugin/cpu/shared_tests_instances/low_precision_transformations/move_fake_quantize_transformation.cpp +++ b/inference-engine/tests/functional/plugin/cpu/shared_tests_instances/low_precision_transformations/move_fake_quantize_transformation.cpp @@ -5,7 +5,6 @@ #include #include "low_precision_transformations/move_fake_quantize_transformation.hpp" -#include "low_precision_transformations/convolution_with_incorrect_weights.hpp" #include "common_test_utils/test_constants.hpp" using namespace LayerTestsDefinitions; @@ -21,84 +20,34 @@ const std::vector tras }; const std::vector params = { - { + { {}, {}, {}, {}, {}, {}, - "relu", + "", { 256ul, {}, {0.f}, {2.55f}, {0.f}, {2.55f}}, {}, {}, "Concatenation", "U8" }, - /*{ - {}, - false, - { 255ul, ngraph::Shape { 1, 1, 1, 1 }, { 0.f }, { 254.f }, { -12.7f }, { 12.7f } }, - false, - "Convolution", - "FP32" - }, - { - { 256ul, ngraph::Shape { 1, 1, 1, 1 }, { 0.f }, { 255.f }, { 0.f }, { 25.5f } }, - false, - { 255ul, ngraph::Shape { 1, 1, 1, 1 }, { 0.f }, { 254.f }, { -12.7f }, { 12.7f } }, - false, - "Convolution", - "U8" - }, - { - { 256ul, ngraph::Shape {}, { 0.f }, { 255.f }, { 0.f }, { 25.5f } }, - false, - { 255ul, ngraph::Shape {}, { 0.f }, { 254.f }, { -12.7f }, { 12.7f } }, - false, - "Convolution", - "U8" - }, - { - { 16ul, ngraph::Shape { 1, 1, 1, 1 }, { 0.f }, { 255.f }, { 0.f }, { 25.5f } }, - false, - { 16ul, ngraph::Shape { 1, 1, 1, 1 }, { 0.f }, { 254.f }, { -12.7f }, { 12.7f } }, - false, - "Convolution", - "FP32" - }, - { - { 16ul, ngraph::Shape { 1, 1, 1, 1 }, { 0.f }, { 25.5f }, { 0.f }, { 25.5f } }, - false, - { 255ul, ngraph::Shape { 1, 1, 1, 1 }, { -12.7f }, { 12.7f }, { -12.7f }, { 12.7f } }, - false, - "Convolution", - "FP32" - }, - { - { 256ul, ngraph::Shape { 1, 1, 1, 1 }, { 0.f }, { 255.f }, { 0.f }, { 25.5f } }, - false, - { 16ul, ngraph::Shape { 1, 1, 1, 1 }, { 0.f }, { 254.f }, { -12.7f }, { 12.7f } }, - false, - "Convolution", - "FP32" - }, { - { 256ul, ngraph::Shape { 1, 1, 1, 1 }, { 0.f }, { 255.f }, { -12.7f }, { 12.8f } }, - true, - { 255ul, ngraph::Shape { 1, 1, 1, 1 }, { 0.f }, { 254.f }, { -12.7f }, { 12.7f } }, - false, - "Convolution", + {}, + {}, + {}, + {}, + {}, + {}, + "relu", + { 256ul, {}, { -12.7f }, { 12.7f }, { -12.7f }, { 12.7f }}, + {}, + {}, + "Concatenation", "U8" }, - { - { 256ul, ngraph::Shape { 1 }, { 0.f }, { 255.f }, { -18.7f }, { 18.8f } }, - true, - { 255ul, ngraph::Shape { 1 }, { 0.f }, { 254.f }, { -18.7f }, { 18.7f } }, - false, - "Convolution", - "U8" - },*/ }; const std::vector shapes = { @@ -114,28 +63,4 @@ INSTANTIATE_TEST_SUITE_P(smoke_LPT, MoveFakeQuantizeTransformation, ::testing::ValuesIn(trasformationParamValues), ::testing::ValuesIn(params)), MoveFakeQuantizeTransformation::getTestCaseName); - -//const std::vector incorrectWeightsParams = { -// // incorrect weights -// { -// { 256ul, ngraph::Shape { 1, 1, 1, 1 }, { 0.f }, { 255.f }, { 0.f }, { 25.5f } }, -// { 255ul, ngraph::Shape { 1, 1, 1, 1 }, { 0.f }, { 254.f }, { -127.f }, { 127.f } }, -// false -// }, -// // correct weights -// { -// { 256ul, ngraph::Shape { 1, 1, 1, 1 }, { 0.f }, { 255.f }, { 0.f }, { 25.5f } }, -// { 255ul, ngraph::Shape { 1, 1, 1, 1 }, { 0.f }, { 254.f }, { -127.f }, { 127.f } }, -// true -// } -//}; -// -//INSTANTIATE_TEST_SUITE_P(smoke_LPT, ConvolutionWIthIncorrectWeightsTransformation, -// ::testing::Combine( -// ::testing::ValuesIn(netPrecisions), -// ::testing::Values(ngraph::Shape({ 1, 3, 16, 16 })), -// ::testing::Values(CommonTestUtils::DEVICE_CPU), -// ::testing::ValuesIn(trasformationParamValues), -// ::testing::ValuesIn(incorrectWeightsParams)), -// ConvolutionWIthIncorrectWeightsTransformation::getTestCaseName); } // namespace diff --git a/inference-engine/tests/functional/plugin/gpu/shared_tests_instances/low_precision_transformations/move_fake_quantize_transformation.cpp b/inference-engine/tests/functional/plugin/gpu/shared_tests_instances/low_precision_transformations/move_fake_quantize_transformation.cpp new file mode 100644 index 00000000000000..450806e863cf3f --- /dev/null +++ b/inference-engine/tests/functional/plugin/gpu/shared_tests_instances/low_precision_transformations/move_fake_quantize_transformation.cpp @@ -0,0 +1,66 @@ +// Copyright (C) 2018-2021 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include + +#include "low_precision_transformations/move_fake_quantize_transformation.hpp" +#include "common_test_utils/test_constants.hpp" + +using namespace LayerTestsDefinitions; + +namespace { + const std::vector netPrecisions = { + ngraph::element::f32, + ngraph::element::f16 + }; + + const std::vector trasformationParamValues = { + LayerTestsUtils::LayerTransformationParamsNGraphFactory::createParams().setUpdatePrecisions(true) + }; + + const std::vector params = { + { + {}, + {}, + {}, + {}, + {}, + {}, + "", + { 256ul, {}, {0.f}, {2.55f}, {0.f}, {2.55f}}, + {}, + {}, + "Concatenation", + "U8" + }, + { + {}, + {}, + {}, + {}, + {}, + {}, + "relu", + { 256ul, {}, { -12.7f }, { 12.7f }, { -12.7f }, { 12.7f }}, + {}, + {}, + "Concatenation", + "U8" + }, + }; + + const std::vector shapes = { + { 1, 3, 16, 16 }, + { 4, 3, 16, 16 } + }; + + INSTANTIATE_TEST_SUITE_P(smoke_LPT, MoveFakeQuantizeTransformation, + ::testing::Combine( + ::testing::ValuesIn(netPrecisions), + ::testing::ValuesIn(shapes), + ::testing::Values(CommonTestUtils::DEVICE_GPU), + ::testing::ValuesIn(trasformationParamValues), + ::testing::ValuesIn(params)), + MoveFakeQuantizeTransformation::getTestCaseName); +} // namespace diff --git a/inference-engine/tests/functional/plugin/shared/src/low_precision_transformations/move_fake_quantize_transformation.cpp b/inference-engine/tests/functional/plugin/shared/src/low_precision_transformations/move_fake_quantize_transformation.cpp index d521dd2286e1f2..b3214a98a0d908 100644 --- a/inference-engine/tests/functional/plugin/shared/src/low_precision_transformations/move_fake_quantize_transformation.cpp +++ b/inference-engine/tests/functional/plugin/shared/src/low_precision_transformations/move_fake_quantize_transformation.cpp @@ -27,7 +27,8 @@ std::string MoveFakeQuantizeTransformation::getTestCaseName(testing::TestParamIn std::tie(netPrecision, inputShape, targetDevice, params, param) = obj.param; std::ostringstream result; - result << getTestCaseNameByParams(netPrecision, inputShape, targetDevice, params); + result << getTestCaseNameByParams(netPrecision, inputShape, targetDevice, params) << "_" << + param.operation; return result.str(); } diff --git a/inference-engine/tests/ngraph_helpers/lpt_ngraph_functions/src/move_fake_quantize_function.cpp b/inference-engine/tests/ngraph_helpers/lpt_ngraph_functions/src/move_fake_quantize_function.cpp index 234423bc5cfa5e..8295ecbe472eb3 100644 --- a/inference-engine/tests/ngraph_helpers/lpt_ngraph_functions/src/move_fake_quantize_function.cpp +++ b/inference-engine/tests/ngraph_helpers/lpt_ngraph_functions/src/move_fake_quantize_function.cpp @@ -41,53 +41,48 @@ std::shared_ptr MoveFakeQuantize::get( const auto input2 = std::make_shared(inputPrecision, inputShape); input2->set_friendly_name("input2"); - - if (fqOnData3.empty()) { - std::shared_ptr parent1, parent2; + std::shared_ptr parent1 = input1, parent2 = input2; + if (!fqOnData1.empty()) { if (operation == "relu") { auto relu1 = std::make_shared(input1->output(0)); - auto relu2 = std::make_shared(input2->output(0)); - parent1 = makeFakeQuantize(relu1, inputPrecision, fqOnData1); - parent2 = makeFakeQuantize(relu2, inputPrecision, fqOnData2); } else { parent1 = makeFakeQuantize(input1, inputPrecision, fqOnData1); - parent2 = makeFakeQuantize(input1, inputPrecision, fqOnData2); } - + parent1->set_friendly_name("concat_fq1"); if (!convert1.empty()) { parent1 = std::make_shared(parent1, convert1.outPrecision); } if (!dequantization1.empty()) { parent1 = makeDequantization(parent1, dequantization1); } - + } + if (!fqOnData2.empty()) { + if (operation == "relu") { + auto relu2 = std::make_shared(input2->output(0)); + parent2 = makeFakeQuantize(relu2, inputPrecision, fqOnData2); + } else { + parent2 = makeFakeQuantize(input1, inputPrecision, fqOnData2); + } + parent2->set_friendly_name("concat_fq2"); if (!convert2.empty()) { - parent2 = std::make_shared(parent2, convert2.outPrecision); + parent1 = std::make_shared(parent2, convert2.outPrecision); } - if (!dequantization2.empty()) { + if (!dequantization1.empty()) { parent2 = makeDequantization(parent2, dequantization2); } - - const std::shared_ptr concat = std::make_shared(ngraph::OutputVector{ parent1, parent2 }, axis); - auto& rtInfo = concat->get_rt_info(); - rtInfo["Variant::std::string"] = std::make_shared>("concat"); - + } + const std::shared_ptr concat = std::make_shared(ngraph::OutputVector{ parent1, parent2 }, axis); + concat->set_friendly_name("concat"); + auto& rtInfo = concat->get_rt_info(); + rtInfo["Variant::std::string"] = std::make_shared>("concat"); + std::shared_ptr parent = concat; + if (!dequantizationAfter.empty()) { const auto lastDequantization = makeDequantization(concat, dequantizationAfter); - lastDequantization->set_friendly_name("output"); - - ngraph::ResultVector results{ std::make_shared(lastDequantization) }; - std::shared_ptr function = std::make_shared( - results, - ngraph::ParameterVector{ input1, input2 }, - "MoveFakeQuantize"); - return function; - } else { - const std::shared_ptr concat = std::make_shared(ngraph::OutputVector{ input1, input2 }, axis); - auto& rtInfo = concat->get_rt_info(); - rtInfo["Variant::std::string"] = std::make_shared>("concat"); - - concat->set_friendly_name("output"); + lastDequantization->set_friendly_name("multiply"); + parent = lastDequantization; + } + if (!fqOnData3.empty()) { std::shared_ptr fq; if (operation == "relu") { auto relu = std::make_shared(concat->output(0)); @@ -95,14 +90,16 @@ std::shared_ptr MoveFakeQuantize::get( } else { fq = makeFakeQuantize(concat, inputPrecision, fqOnData3); } - - ngraph::ResultVector results{ std::make_shared(fq) }; - std::shared_ptr function = std::make_shared( - results, - ngraph::ParameterVector{ input1, input2 }, - "MoveFakeQuantize"); - return function; + fq->set_friendly_name("fakeQuantizeAfter"); + parent = fq; } + parent->set_friendly_name("output"); + ngraph::ResultVector results{ std::make_shared(parent) }; + std::shared_ptr function = std::make_shared( + results, + ngraph::ParameterVector{ input1, input2 }, + "MoveFakeQuantize"); + return function; } std::shared_ptr MoveFakeQuantize::makeMaxPool(const Output& parent, const std::vector& kernel) {