Skip to content

Commit

Permalink
[LPT] nGraph alignment: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eshoguli committed Jul 15, 2021
1 parent 8cefb95 commit a37161d
Show file tree
Hide file tree
Showing 235 changed files with 4,571 additions and 2,194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class AddTransformationTestValues {
ngraph::element::Type precision;
bool broadcast;
int constInput;
ngraph::pass::low_precision::LayerTransformation::Params params;
TestTransformationParams params;
Actual actual;
Expected expected;
std::string additionalLayer;
Expand Down Expand Up @@ -102,7 +102,7 @@ class AddTransformation : public LayerTransformation, public testing::WithParamI
inputShapes.first,
inputShapes.second,
testValues.broadcast,
testValues.params,
TestTransformationParams::toParams(testValues.params),
testValues.actual.precision1,
testValues.actual.dequantization1,
testValues.actual.precision2,
Expand All @@ -112,8 +112,7 @@ class AddTransformation : public LayerTransformation, public testing::WithParamI
testValues.additionalLayer);

SimpleLowPrecisionTransformer transform;
transform.add<ngraph::pass::low_precision::AddTransformation, ngraph::opset1::Add>(
low_precision::LayerTransformation::Params(testValues.params));
transform.add<ngraph::pass::low_precision::AddTransformation, ngraph::opset1::Add>(testValues.params);
transform.transform(actualFunction);

auto inputShape1Ref = inputShapes.first;
Expand All @@ -127,7 +126,7 @@ class AddTransformation : public LayerTransformation, public testing::WithParamI
inputShape1Ref,
inputShape2Ref,
testValues.broadcast,
testValues.params,
TestTransformationParams::toParams(testValues.params),
testValues.expected.precision1,
testValues.expected.dequantization1,
testValues.expected.precision2,
Expand Down Expand Up @@ -164,7 +163,7 @@ class AddTransformation : public LayerTransformation, public testing::WithParamI

TEST_P(AddTransformation, CompareFunctions) {
actualFunction->validate_nodes_and_infer_types();
auto res = compare_functions(referenceFunction, actualFunction, true, true, true);
auto res = compare_functions(referenceFunction, actualFunction, true, true, false);
ASSERT_TRUE(res.first) << res.second;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// Copyright (C) 2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "layer_transformation.hpp"

#include <memory>

#include <gtest/gtest.h>

#include <transformations/utils/utils.hpp>
#include <transformations/init_node_info.hpp>

#include <low_precision/avg_pool.hpp>
#include <low_precision/concat.hpp>
#include <low_precision/convolution.hpp>
#include <low_precision/fake_quantize_decomposition.hpp>
#include <low_precision/max_pool.hpp>

#include "common_test_utils/ngraph_test_utils.hpp"
#include "simple_low_precision_transformer.hpp"
#include "lpt_ngraph_functions/align_concat_quantization_parameters_function.hpp"
#include "lpt_ngraph_functions/common/dequantization_operations.hpp"

using namespace testing;
using namespace ngraph::pass;

class AlignConcatQuantizationParametersTransformationTestValues {
public:
public:
class Actual {
public:
ngraph::element::Type inputPrecision;
ngraph::builder::subgraph::DequantizationOperations dequantization;
};

class Expected {
public:
ngraph::element::Type inputPrecision;
ngraph::builder::subgraph::DequantizationOperations dequantizationBefore;
ngraph::element::Type preicsionAfterOperation;
ngraph::builder::subgraph::DequantizationOperations dequantizationAfter;
};

TestTransformationParams params;
Actual actual;
Expected expected;
};

typedef std::tuple<
ngraph::element::Type,
ngraph::Shape,
bool, // additional FakeQuantize After
std::string, // additional layer before FQ
AlignConcatQuantizationParametersTransformationTestValues> AlignConcatQuantizationParametersTransformationParams;

class AlignConcatQuantizationParametersTransformation :
public LayerTransformation,
public testing::WithParamInterface<AlignConcatQuantizationParametersTransformationParams> {
public:
void SetUp() override {
ngraph::element::Type precision;
ngraph::Shape shape;
bool addFakeQuantize;
std::string additionalLayer;
AlignConcatQuantizationParametersTransformationTestValues testValues;
std::tie(precision, shape, addFakeQuantize, additionalLayer, testValues) = GetParam();

actualFunction = ngraph::builder::subgraph::AlignConcatQuantizationParametersFunction::getOriginal(
precision,
testValues.actual.inputPrecision,
shape,
addFakeQuantize,
additionalLayer,
testValues.actual.dequantization);

auto supportedPrecisions = std::vector<ngraph::pass::low_precision::OperationPrecisionRestriction>({
ngraph::pass::low_precision::OperationPrecisionRestriction::create<ngraph::opset1::Convolution>({
{0, {ngraph::element::u8}},
{1, {ngraph::element::i8}}
})
});

auto perTensorQuantization = std::vector<ngraph::pass::low_precision::OperationPerTensorQuantizationRestriction>({
ngraph::pass::low_precision::OperationPerTensorQuantizationRestriction::create<ngraph::opset1::Convolution>({0}),
});

SimpleLowPrecisionTransformer transform(supportedPrecisions, perTensorQuantization);
transform.add<ngraph::pass::low_precision::AvgPoolTransformation, ngraph::opset1::AvgPool>(testValues.params);
transform.add<ngraph::pass::low_precision::ConcatTransformation, ngraph::opset1::Concat>(testValues.params);
transform.add<ngraph::pass::low_precision::ConvolutionTransformation, ngraph::opset1::Convolution>(testValues.params);
transform.add<ngraph::pass::low_precision::FakeQuantizeDecompositionTransformation, ngraph::opset1::FakeQuantize>(testValues.params);
transform.add<ngraph::pass::low_precision::MaxPoolTransformation, ngraph::opset1::MaxPool>(testValues.params);
transform.transform(actualFunction);

referenceFunction = ngraph::builder::subgraph::AlignConcatQuantizationParametersFunction::getReference(
precision,
testValues.expected.inputPrecision,
shape,
addFakeQuantize,
additionalLayer,
testValues.expected.dequantizationBefore,
testValues.expected.preicsionAfterOperation,
testValues.expected.dequantizationAfter);
}

static std::string getTestCaseName(testing::TestParamInfo<AlignConcatQuantizationParametersTransformationParams> obj) {
ngraph::element::Type precision;
ngraph::Shape shape;
bool addFakeQuantize;
std::string additionalLayer;
AlignConcatQuantizationParametersTransformationTestValues testValues;
std::tie(precision, shape, addFakeQuantize, additionalLayer, testValues) = obj.param;

std::ostringstream result;
result <<
precision << "_" <<
LayerTransformation::getTestCaseNameByParams(testValues.actual.inputPrecision, shape, testValues.params) << "_" <<
testValues.actual.dequantization << "_" <<
testValues.expected.dequantizationBefore << "_" <<
testValues.expected.preicsionAfterOperation << "_" <<
testValues.expected.dequantizationAfter << "_" <<
(addFakeQuantize ? "_FQ_after_" : "_") << additionalLayer;
return result.str();
}
};

TEST_P(AlignConcatQuantizationParametersTransformation, CompareFunctions) {
InitNodeInfo().run_on_function(actualFunction);
actualFunction->validate_nodes_and_infer_types();

auto res = compare_functions(referenceFunction, actualFunction, true, true);
ASSERT_TRUE(res.first) << res.second;
}

const std::vector<ngraph::element::Type> precisions = {
ngraph::element::f32
};

const std::vector<std::string> additionalLayer = {
"maxpool" // any transparent layer
};

const std::vector<bool> addFQ = {
false
};

const std::vector<ngraph::Shape> shapes = {
{ 1, 3, 9, 9 },
{ 4, 3, 9, 9 }
};

const std::vector<AlignConcatQuantizationParametersTransformationTestValues> testValues = {
// U8 per tensor quantization
{
LayerTransformation::createParamsU8I8(),
{
ngraph::element::f32,
{{ngraph::element::f32}, {128.f}, {0.02f}}
},
{
ngraph::element::f32,
{{}, {std::vector<float>(6, 128.f), element::f32, {1, 6, 1, 1}}, {}},
ngraph::element::f32,
{{}, {}, {std::vector<float>(9, 0.0001f), element::f32, {1, 9, 1, 1}}}
}
}
};

INSTANTIATE_TEST_SUITE_P(
smoke_LPT,
AlignConcatQuantizationParametersTransformation,
::testing::Combine(
::testing::ValuesIn(precisions),
::testing::ValuesIn(shapes),
::testing::ValuesIn(addFQ),
::testing::ValuesIn(additionalLayer),
::testing::ValuesIn(testValues)),
AlignConcatQuantizationParametersTransformation::getTestCaseName);
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <transformations/init_node_info.hpp>
#include <low_precision/avg_pool.hpp>
#include <low_precision/max_pool.hpp>
#include <low_precision/transformer.hpp>

#include "common_test_utils/ngraph_test_utils.hpp"
#include "simple_low_precision_transformer.hpp"
Expand All @@ -25,7 +24,6 @@ using namespace ngraph::pass;
using namespace ngraph;

class AvgPoolTransformationTestValues {
public:
public:
class Actual {
public:
Expand All @@ -41,7 +39,7 @@ class AvgPoolTransformationTestValues {
ngraph::builder::subgraph::DequantizationOperations dequantizationAfter;
};

ngraph::pass::low_precision::LayerTransformation::Params params;
TestTransformationParams params;
Actual actual;
Expected expected;
};
Expand All @@ -67,7 +65,7 @@ class AvgPoolTransformation : public LayerTransformation, public testing::WithPa
testValues.actual.inputPrecision,
shape,
addFakeQuantize,
additionalLayer,
{ additionalLayer },
testValues.actual.dequantization);

SimpleLowPrecisionTransformer transform;
Expand All @@ -80,9 +78,10 @@ class AvgPoolTransformation : public LayerTransformation, public testing::WithPa
testValues.expected.inputPrecision,
shape,
addFakeQuantize,
additionalLayer,
{ additionalLayer },
testValues.expected.dequantizationBefore,
testValues.expected.preicsionAfterOperation,
{},
testValues.expected.dequantizationAfter);
}

Expand Down
Loading

0 comments on commit a37161d

Please sign in to comment.