Skip to content

Commit

Permalink
[LPT] MoveFQ fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ndemashov authored and v-Golubev committed Aug 15, 2021
1 parent 78045da commit 2d9f85a
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <memory>
#include <ngraph/ngraph.hpp>
#include <ngraph/opsets/opset1.hpp>
#include <ngraph/pattern/op/or.hpp>

#include "low_precision/network_helper.hpp"

Expand All @@ -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<opset1::FakeQuantize>();
auto concat = ngraph::pattern::wrap_type<opset1::Concat>();
auto operation = ngraph::pattern::wrap_type<opset1::Relu>({ concat });
auto input_low = ngraph::pattern::wrap_type<ngraph::opset1::Constant>();
auto input_high = ngraph::pattern::wrap_type<ngraph::opset1::Constant>();
auto output_low = ngraph::pattern::wrap_type<ngraph::opset1::Constant>();
auto output_high = ngraph::pattern::wrap_type<ngraph::opset1::Constant>();
auto fq_with_operation = ngraph::pattern::wrap_type<opset1::FakeQuantize>({ operation,
input_low,
input_high,
output_low,
output_high});
auto fq = ngraph::pattern::wrap_type<opset1::FakeQuantize>({ concat,
input_low,
input_high,
output_low,
output_high });

ngraph::graph_rewrite_callback callback = [this](pattern::Matcher& m) {
auto op = m.get_match_root();
Expand All @@ -31,32 +47,30 @@ MoveFakeQuantize::MoveFakeQuantize(const Params& params) : LayerTransformation(p
return transform(*context, m);
};

auto m = std::make_shared<ngraph::pattern::Matcher>(matcher, "MoveFakeQuantize");
auto m = std::make_shared<ngraph::pattern::Matcher>(
std::make_shared<pattern::op::Or>(OutputVector{fq, fq_with_operation}),
"MoveFakeQuantize");
this->register_matcher(m, callback);
}

bool MoveFakeQuantize::transform(TransformationContext& context, ngraph::pattern::Matcher& m) {
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<opset1::Relu>(operation) && !is_type<opset1::Concat>(operation)) {
return false;
}

auto type = operation->get_type_name();
std::shared_ptr<ngraph::Node> concat, fq1input, fq2input;
if (strcmp(type, "Concat") == 0) {
if (is_type<opset1::Concat>(operation)) {
concat = operation;
fq1input = operation->get_input_node_shared_ptr(0);
fq2input = operation->get_input_node_shared_ptr(1);
} else {
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<opset1::Relu>(operation)) {
fq1input = std::make_shared<ngraph::opset1::Relu>(input1->output(0));
fq2input = std::make_shared<ngraph::opset1::Relu>(input2->output(0));
} else {
return false;
}
}

Expand All @@ -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<opset1::FakeQuantize>(fq)->get_levels());
fq1->set_friendly_name("concat_fq1");
auto fq2 = std::make_shared<opset1::FakeQuantize>(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<opset1::FakeQuantize>(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<VariantWrapper<std::string>>("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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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>({
ngraph::pass::low_precision::OperationPrecisionRestriction::create<ngraph::opset1::AvgPool>({{0, testValues.params.precisionsOnActivations}})
});
Expand All @@ -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<ngraph::pass::low_precision::MoveFakeQuantize>(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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <vector>

#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;
Expand All @@ -21,84 +20,34 @@ const std::vector<ngraph::pass::low_precision::LayerTransformation::Params> tras
};

const std::vector<LayerTestsDefinitions::MoveFakeQuantizeTransformationParam> 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<ngraph::Shape> shapes = {
Expand All @@ -114,28 +63,4 @@ INSTANTIATE_TEST_SUITE_P(smoke_LPT, MoveFakeQuantizeTransformation,
::testing::ValuesIn(trasformationParamValues),
::testing::ValuesIn(params)),
MoveFakeQuantizeTransformation::getTestCaseName);

//const std::vector<LayerTestsDefinitions::ConvolutionWIthIncorrectWeightsParam> 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include <vector>

#include "low_precision_transformations/move_fake_quantize_transformation.hpp"
#include "common_test_utils/test_constants.hpp"

using namespace LayerTestsDefinitions;

namespace {
const std::vector<ngraph::element::Type> netPrecisions = {
ngraph::element::f32,
ngraph::element::f16
};

const std::vector<ngraph::pass::low_precision::LayerTransformation::Params> trasformationParamValues = {
LayerTestsUtils::LayerTransformationParamsNGraphFactory::createParams().setUpdatePrecisions(true)
};

const std::vector<LayerTestsDefinitions::MoveFakeQuantizeTransformationParam> 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<ngraph::Shape> 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
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Loading

0 comments on commit 2d9f85a

Please sign in to comment.