Skip to content

Commit

Permalink
add cpu test
Browse files Browse the repository at this point in the history
  • Loading branch information
ndemashov committed Aug 4, 2021
1 parent d956a64 commit bdfed62
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace low_precision {

class LP_TRANSFORMATIONS_API MoveFakeQuantize : public LayerTransformation {
public:
MoveFakeQuantize(const Params& params);
MoveFakeQuantize(const Params& params = Params());
bool transform(TransformationContext& context, ngraph::pattern::Matcher &m) override;
bool isPrecisionPreserved(std::shared_ptr<Node> layer) const noexcept override;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
#include "low_precision/transpose.hpp"
#include "low_precision/unsqueeze.hpp"
#include "low_precision/variadic_split.hpp"
#include "low_precision/move_fake_quantize.hpp"

// cleanup transformations
#include "low_precision/convert.hpp"
Expand Down Expand Up @@ -197,6 +198,7 @@ bool ngraph::pass::low_precision::LowPrecision::run_on_function(std::shared_ptr<
prerequisites->add_matcher<PullReshapeThroughDequantization>(supportedTypes);
prerequisites->add_matcher<PullTransposeThroughDequantization>(supportedTypes);
prerequisites->add_matcher<ngraph::pass::LinOpSequenceFusion>();
prerequisites->add_matcher<ngraph::pass::low_precision::MoveFakeQuantize>();

manager.register_pass<TypeRelaxedReplacer>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
//

#include "low_precision/move_fake_quatize.hpp"
#include "low_precision/move_fake_quantize.hpp"

#include <ngraph/pattern/op/wrap_type.hpp>
#include <ngraph/opsets/opset1.hpp>
Expand Down
4 changes: 4 additions & 0 deletions inference-engine/src/mkldnn_plugin/mkldnn_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,9 @@ static void Transformation(CNNNetwork& clonedNetwork, const Config& conf) {

manager.run_passes(nGraphFunc);

//ngraph::pass:
ngraph::pass::VisualizeTree("C:/Users/ndemasho/rep/Visual/cpu.common.svg").run_on_function(nGraphFunc);

using namespace ngraph::pass::low_precision;
if (useLpt) {
OV_ITT_SCOPE(FIRST_INFERENCE, MKLDNNPlugin::itt::domains::MKLDNN_LT, "LowPrecisionTransformations");
Expand Down Expand Up @@ -367,6 +370,7 @@ static void Transformation(CNNNetwork& clonedNetwork, const Config& conf) {
return MultiplyToGroupConvolutionTransformation::isDynamicOrScalar(node);
});
lptManager.run_passes(nGraphFunc);
ngraph::pass::VisualizeTree("C:/Users/ndemasho/rep/Visual/cpu.commonAfter.svg").run_on_function(nGraphFunc);
}

ngraph::pass::Manager postLPTPassManager;
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#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;

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),
//LayerTestsUtils::LayerTransformationParamsNGraphFactory::createParams().setUpdatePrecisions(false),
};

const std::vector<LayerTestsDefinitions::MoveFakeQuantizeTransformationParam> params = {
{
{},
{},
{},
{},
{},
{},
"relu",
{ 256ul, {}, {0.f}, {2.55f}, {0.f}, {255.f}},
{},
{},
"Concat",
"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",
"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 = {
{ 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_CPU),
::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,56 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include <string>
#include <memory>

#include "shared_test_classes/base/low_precision_transformations/layer_transformation.hpp"
#include "lpt_ngraph_functions/common/fake_quantize_on_data.hpp"
#include "lpt_ngraph_functions/common/fake_quantize_on_weights.hpp"

#include "low_precision/move_fake_quantize.hpp"

#include "lpt_ngraph_functions/move_fake_quantize_function.hpp"

namespace LayerTestsDefinitions {

class MoveFakeQuantizeTransformationParam {
public:
ngraph::builder::subgraph::FakeQuantizeOnDataWithConstant fakeQuantizeBefore1;
ngraph::builder::subgraph::DequantizationOperations::Convert convertBefore1;
ngraph::builder::subgraph::DequantizationOperations dequantizationBefore1;
ngraph::builder::subgraph::FakeQuantizeOnDataWithConstant fakeQuantizeBefore2;
ngraph::builder::subgraph::DequantizationOperations::Convert convertBefore2;
ngraph::builder::subgraph::DequantizationOperations dequantizationBefore2;
std::string operation;
ngraph::builder::subgraph::FakeQuantizeOnDataWithConstant fakeQuantizeAfter;
ngraph::builder::subgraph::DequantizationOperations::Convert convertAfter;
ngraph::builder::subgraph::DequantizationOperations dequantizationAfter;
std::string layerName;
std::string expectedKernelType;
};

typedef std::tuple <
ngraph::element::Type,
ngraph::Shape,
std::string,
ngraph::pass::low_precision::LayerTransformation::Params,
MoveFakeQuantizeTransformationParam
> MoveFakeQuantizeTransformationParams;

class MoveFakeQuantizeTransformation :
public testing::WithParamInterface<MoveFakeQuantizeTransformationParams>,
public LayerTestsUtils::LayerTransformation {
public:
static std::string getTestCaseName(testing::TestParamInfo<MoveFakeQuantizeTransformationParams> obj);

protected:
void SetUp() override;

void Run() override;
};

} // namespace LayerTestsDefinitions
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "low_precision_transformations/move_fake_quantize_transformation.hpp"

#include <memory>
#include <tuple>
#include <vector>
#include <string>

#include <ie_core.hpp>

#include "common_test_utils/common_utils.hpp"
#include "functional_test_utils/plugin_cache.hpp"
#include "shared_test_classes/base/layer_test_utils.hpp"
#include "functional_test_utils/blob_utils.hpp"
#include "ngraph_functions/pass/convert_prc.hpp"
#include "lpt_ngraph_functions/move_fake_quantize_function.hpp"

namespace LayerTestsDefinitions {

std::string MoveFakeQuantizeTransformation::getTestCaseName(testing::TestParamInfo<MoveFakeQuantizeTransformationParams> obj) {
ngraph::element::Type netPrecision;
ngraph::PartialShape inputShape;
std::string targetDevice;
ngraph::pass::low_precision::LayerTransformation::Params params;
MoveFakeQuantizeTransformationParam param;
std::tie(netPrecision, inputShape, targetDevice, params, param) = obj.param;

std::ostringstream result;
result << getTestCaseNameByParams(netPrecision, inputShape, targetDevice, params);
return result.str();
}

void MoveFakeQuantizeTransformation::SetUp() {
threshold = 0.1f;

ngraph::element::Type netPrecision;
ngraph::PartialShape inputShape;
ngraph::pass::low_precision::LayerTransformation::Params params;
MoveFakeQuantizeTransformationParam param;
std::tie(netPrecision, inputShape, targetDevice, params, param) = this->GetParam();

function = ngraph::builder::subgraph::MoveFakeQuantize::get(
netPrecision,
inputShape,
param.fakeQuantizeBefore1,
param.convertBefore1,
param.dequantizationBefore1,
param.fakeQuantizeBefore2,
param.convertBefore2,
param.dequantizationBefore2,
param.operation,
param.fakeQuantizeAfter,
param.convertAfter,
param.dequantizationAfter,
{},
{},
{});
}

void MoveFakeQuantizeTransformation::Run() {
LayerTestsCommon::Run();

const auto params = std::get<4>(GetParam());
const auto actualPrecision = getRuntimePrecisionByType(params.layerName);
auto expectedPrecision = params.expectedKernelType;
if (expectedPrecision == "FP32" && std::get<0>(GetParam()) == ngraph::element::f16) {
expectedPrecision = "FP16";
}
EXPECT_EQ(actualPrecision, expectedPrecision);
}

TEST_P(MoveFakeQuantizeTransformation, CompareWithRefImpl) {
Run();
};

} // namespace LayerTestsDefinitions
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace ngraph {
if (operation == "relu") {
auto relu1 = std::make_shared<ngraph::opset1::Relu>(input1->output(0));
auto relu2 = std::make_shared<ngraph::opset1::Relu>(input2->output(0));
parent1 = makeFakeQuantizeTypeRelaxed(relu1, inputPrecision, fqOnData1);
parent1 = makeFakeQuantizeTypeRelaxed(relu1, inputPrecision, fqOnData1);// ???????? ?? MoveFakeQuantize
parent2 = makeFakeQuantizeTypeRelaxed(relu2, inputPrecision, fqOnData2);
}
else {
Expand Down

0 comments on commit bdfed62

Please sign in to comment.