Skip to content

Commit

Permalink
FakeQuantize - reference implementation refactor (openvinotoolkit#6478)
Browse files Browse the repository at this point in the history
* new implementation of FakeQuantize

* move FakeQuantize backend test to fake_quantize.in.cpp

* enable some test for FakeQuantize

* Add support for PDPD broadcasting and some backend tests

* arm plugin expect less attributes in function call

* try to fix arm plugin build

* try to build arm plugin

* start changing backend test

* add check for attributes number in visitor test

* fix backend test after refactoring

* add default parameter value to runtime::reference::fake_quantize

* Revert "add default parameter value to runtime::reference::fake_quantize"

This reverts commit 9d2c00d.

* add SLT for FakeQuantize

* remove fesetround

* change `v1` namesapce to `v0` in ref impl

* add FakeQuantize-1 to VERIFIED_OP_REFERENCES

* pass immutable reference to test functions
  • Loading branch information
pelszkow authored and akuporos committed Sep 29, 2021
1 parent 63fa6a2 commit 258e50e
Show file tree
Hide file tree
Showing 16 changed files with 635 additions and 365 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ using namespace LayerTestsDefinitions;

namespace {

const std::vector<InferenceEngine::Precision> netPrecisions = {
InferenceEngine::Precision::FP32,
InferenceEngine::Precision::FP16
const ngraph::op::AutoBroadcastSpec numpyBroadcast = ngraph::op::AutoBroadcastType::NUMPY;

const ngraph::op::AutoBroadcastSpec noneBroadcast = ngraph::op::AutoBroadcastType::NONE;

const std::vector<ngraph::op::AutoBroadcastSpec> broadcasts = {
{ngraph::op::AutoBroadcastType::NUMPY},
{ngraph::op::AutoBroadcastType::PDPD, -1},
};

const std::vector<InferenceEngine::Precision>
netPrecisions = {InferenceEngine::Precision::FP32,
InferenceEngine::Precision::FP16};

const std::vector<std::vector<size_t>> inputShapes = {{1, 1}, {2, 6}, {1, 1, 1}, {2, 6, 13},
{1, 1, 1, 1}, {3, 10, 5, 6}, {2, 8, 5, 18}, {2, 16, 3, 18}, {3, 49, 5, 6},
{1, 1, 1, 1, 1}, {3, 10, 2, 5, 6}, {2, 8, 1, 5, 18}, {2, 16, 4, 3, 18}, {3, 49, 7, 5, 6}};
Expand All @@ -30,10 +38,11 @@ const auto fqParams = ::testing::Combine(
::testing::ValuesIn(levels),
::testing::ValuesIn(constShapes),
::testing::Values(fqArgs),
::testing::Values(inputParams)
::testing::Values(inputParams),
::testing::ValuesIn(broadcasts)
);

INSTANTIATE_TEST_SUITE_P(smoke_FakeQuantize, FakeQuantizeLayerTest,
INSTANTIATE_TEST_SUITE_P(smoke_FakeQuantize, FakeQuantizeLayerTestRevise,
::testing::Combine(
fqParams,
::testing::ValuesIn(netPrecisions),
Expand All @@ -44,27 +53,52 @@ INSTANTIATE_TEST_SUITE_P(smoke_FakeQuantize, FakeQuantizeLayerTest,
::testing::ValuesIn(inputShapes),
::testing::Values(CommonTestUtils::DEVICE_CPU),
::testing::Values(config)),
FakeQuantizeLayerTest::getTestCaseName);
FakeQuantizeLayerTestRevise::getTestCaseName);


const std::vector<size_t> singleShape = {3, 4, 2, 5};
const auto noneBroadcastFqParams = ::testing::Combine(
::testing::ValuesIn(levels),
::testing::Values(singleShape),
::testing::Values(fqArgs),
::testing::Values(inputParams),
::testing::Values(noneBroadcast)
);

INSTANTIATE_TEST_SUITE_P(smoke_FakeQuantizeNoneBroadcast, FakeQuantizeLayerTestRevise,
::testing::Combine(
noneBroadcastFqParams,
::testing::ValuesIn(netPrecisions),
::testing::Values(InferenceEngine::Precision::UNSPECIFIED),
::testing::Values(InferenceEngine::Precision::UNSPECIFIED),
::testing::Values(InferenceEngine::Layout::ANY),
::testing::Values(InferenceEngine::Layout::ANY),
::testing::Values(singleShape),
::testing::Values(CommonTestUtils::DEVICE_CPU),
::testing::Values(config)),
FakeQuantizeLayerTestRevise::getTestCaseName);

const std::vector<std::vector<size_t>> inputShapesPerChannel = {{11, 10, 22, 19}, {11, 10, 5, 6}};
const std::vector<std::vector<size_t>> constShapesPerChannelAxis0 = {{11, 1, 1, 1}};
const std::vector<std::vector<size_t>> constShapesPerChannelAxis1 = {{1, 10, 1, 1}};
const std::vector<std::vector<size_t>> constShapesPerChannelAxis1 = {{1, 10, 1, 1}, {10, 1, 1}};

const auto fqParamsPerChannelAxis0 = ::testing::Combine(
::testing::ValuesIn(levels),
::testing::ValuesIn(constShapesPerChannelAxis0),
::testing::Values(fqArgs),
::testing::Values(inputParams)
::testing::Values(inputParams),
::testing::Values(numpyBroadcast)
);

const auto fqParamsPerChannelAxis1 = ::testing::Combine(
::testing::ValuesIn(levels),
::testing::ValuesIn(constShapesPerChannelAxis1),
::testing::Values(fqArgs),
::testing::Values(inputParams)
::testing::Values(inputParams),
::testing::Values(numpyBroadcast)
);

INSTANTIATE_TEST_SUITE_P(smoke_FakeQuantizePerChannelAxis0, FakeQuantizeLayerTest,
INSTANTIATE_TEST_SUITE_P(smoke_FakeQuantizePerChannelAxis0, FakeQuantizeLayerTestRevise,
::testing::Combine(
fqParamsPerChannelAxis0,
::testing::ValuesIn(netPrecisions),
Expand All @@ -75,9 +109,9 @@ INSTANTIATE_TEST_SUITE_P(smoke_FakeQuantizePerChannelAxis0, FakeQuantizeLayerTes
::testing::ValuesIn(inputShapesPerChannel),
::testing::Values(CommonTestUtils::DEVICE_CPU),
::testing::Values(config)),
FakeQuantizeLayerTest::getTestCaseName);
FakeQuantizeLayerTestRevise::getTestCaseName);

INSTANTIATE_TEST_SUITE_P(smoke_FakeQuantizePerChannelAxis1, FakeQuantizeLayerTest,
INSTANTIATE_TEST_SUITE_P(smoke_FakeQuantizePerChannelAxis1, FakeQuantizeLayerTestRevise,
::testing::Combine(
fqParamsPerChannelAxis1,
::testing::ValuesIn(netPrecisions),
Expand All @@ -88,18 +122,19 @@ INSTANTIATE_TEST_SUITE_P(smoke_FakeQuantizePerChannelAxis1, FakeQuantizeLayerTes
::testing::ValuesIn(inputShapesPerChannel),
::testing::Values(CommonTestUtils::DEVICE_CPU),
::testing::Values(config)),
FakeQuantizeLayerTest::getTestCaseName);
FakeQuantizeLayerTestRevise::getTestCaseName);

const std::vector<std::vector<size_t>> inputShapesPerChannel2D = {{1, 10}};
const std::vector<std::vector<size_t>> constShapesPerChannel2D = { {10}, {1, 10}, {1} };
const auto fqParamsPerChannel2D = ::testing::Combine(
::testing::ValuesIn(levels),
::testing::ValuesIn(constShapesPerChannel2D),
::testing::Values(fqArgs),
::testing::Values(inputParams)
::testing::Values(inputParams),
::testing::Values(numpyBroadcast)
);

INSTANTIATE_TEST_SUITE_P(smoke_FakeQuantizePerChannel2D, FakeQuantizeLayerTest,
INSTANTIATE_TEST_SUITE_P(smoke_FakeQuantizePerChannel2D, FakeQuantizeLayerTestRevise,
::testing::Combine(
fqParamsPerChannel2D,
::testing::ValuesIn(netPrecisions),
Expand All @@ -110,6 +145,6 @@ INSTANTIATE_TEST_SUITE_P(smoke_FakeQuantizePerChannel2D, FakeQuantizeLayerTest,
::testing::ValuesIn(inputShapesPerChannel2D),
::testing::Values(CommonTestUtils::DEVICE_CPU),
::testing::Values(config)),
FakeQuantizeLayerTest::getTestCaseName);
FakeQuantizeLayerTestRevise::getTestCaseName);

} // namespace
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ const auto fqParams = ::testing::Combine(
::testing::ValuesIn(levels),
::testing::ValuesIn(constShapes),
::testing::ValuesIn(fqArgs),
::testing::ValuesIn(inputParams)
::testing::ValuesIn(inputParams),
::testing::Values(ngraph::op::AutoBroadcastType::NUMPY)
);

INSTANTIATE_TEST_SUITE_P(smoke_FakeQuantize, FakeQuantizeLayerTest,
INSTANTIATE_TEST_SUITE_P(smoke_FakeQuantize, FakeQuantizeLayerTestRevise,
::testing::Combine(
fqParams,
::testing::ValuesIn(netPrecisions),
Expand All @@ -95,6 +96,6 @@ INSTANTIATE_TEST_SUITE_P(smoke_FakeQuantize, FakeQuantizeLayerTest,
::testing::ValuesIn(inputShapes),
::testing::Values(CommonTestUtils::DEVICE_GNA),
::testing::ValuesIn(gnaQuantModes)),
FakeQuantizeLayerTest::getTestCaseName);
FakeQuantizeLayerTestRevise::getTestCaseName);

} // namespace
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ const auto fqParams = ::testing::Combine(
::testing::ValuesIn(levels),
::testing::ValuesIn(constShapes),
::testing::Values(fqArgs),
::testing::Values(inputParams)
::testing::Values(inputParams),
::testing::Values(ngraph::op::AutoBroadcastType::NUMPY)
);

INSTANTIATE_TEST_SUITE_P(smoke_FakeQuantize, FakeQuantizeLayerTest,
INSTANTIATE_TEST_SUITE_P(smoke_FakeQuantize, FakeQuantizeLayerTestRevise,
::testing::Combine(
fqParams,
::testing::ValuesIn(netPrecisions),
Expand All @@ -43,6 +44,6 @@ INSTANTIATE_TEST_SUITE_P(smoke_FakeQuantize, FakeQuantizeLayerTest,
::testing::ValuesIn(inputShapes),
::testing::Values(CommonTestUtils::DEVICE_GPU),
::testing::Values(config)),
FakeQuantizeLayerTest::getTestCaseName);
FakeQuantizeLayerTestRevise::getTestCaseName);

} // namespace
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace LayerTestsDefinitions {

TEST_P(FakeQuantizeLayerTest, CompareWithRefs) {
TEST_P(FakeQuantizeLayerTestRevise, CompareWithRefs) {
Run();
SKIP_IF_CURRENT_TEST_IS_DISABLED();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

namespace LayerTestsDefinitions {


typedef std::tuple<
size_t, // levels
std::vector<size_t>, // const inputs shape
Expand All @@ -48,7 +49,45 @@ typedef std::tuple<
class FakeQuantizeLayerTest : public testing::WithParamInterface<fqLayerTestParamsSet>,
virtual public LayerTestsUtils::LayerTestsCommon {
public:
static std::string getTestCaseName(testing::TestParamInfo<fqLayerTestParamsSet> obj);
static std::string getTestCaseName(const testing::TestParamInfo<fqLayerTestParamsSet>& obj);
InferenceEngine::Blob::Ptr GenerateInput(const InferenceEngine::InputInfo &info) const override;
protected:
void SetUp() override;
void UpdateSeed();

protected:
float inputDataMin = 0.0;
float inputDataMax = 10.0;
float inputDataResolution = 1.0;
int32_t seed = 1;
};


//TODO after update all plugins remove *Revise types
typedef std::tuple<
size_t, // fake quantize levels
std::vector<size_t>, // fake quantize inputs shape
std::vector<float>, // fake quantize (inputLow, inputHigh, outputLow, outputHigh) or empty for random
std::vector<float>, // input generator data (low, high, resolution) or empty for default
ngraph::op::AutoBroadcastSpec // fake quantize broadcast mode
> fqSpecificParamsRevise;
typedef std::tuple<
fqSpecificParamsRevise,
InferenceEngine::Precision, // Net precision
InferenceEngine::Precision, // Input precision
InferenceEngine::Precision, // Output precision
InferenceEngine::Layout, // Input layout
InferenceEngine::Layout, // Output layout
InferenceEngine::SizeVector, // Input shapes
LayerTestsUtils::TargetDevice, // Device name

std::pair<std::string, std::map<std::string, std::string>> // Additional backend configuration and alis name to it
> fqLayerTestParamsSetRevise;

class FakeQuantizeLayerTestRevise : public testing::WithParamInterface<fqLayerTestParamsSetRevise>,
virtual public LayerTestsUtils::LayerTestsCommon {
public:
static std::string getTestCaseName(const testing::TestParamInfo<fqLayerTestParamsSetRevise>& obj);
InferenceEngine::Blob::Ptr GenerateInput(const InferenceEngine::InputInfo &info) const override;
protected:
void SetUp() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

namespace LayerTestsDefinitions {

std::string FakeQuantizeLayerTest::getTestCaseName(testing::TestParamInfo<fqLayerTestParamsSet> obj) {

std::string FakeQuantizeLayerTest::getTestCaseName(const testing::TestParamInfo<fqLayerTestParamsSet>& obj) {
fqSpecificParams fqParams;
InferenceEngine::Precision netPrecision;
InferenceEngine::Precision inPrc, outPrc;
Expand Down Expand Up @@ -113,4 +114,117 @@ void FakeQuantizeLayerTest::UpdateSeed() {
std::cout << "\033[0;32m" << "[ ] " << "\033[0;0m"
<< "seed = " << seed << std::endl;
}



std::string FakeQuantizeLayerTestRevise::getTestCaseName(const testing::TestParamInfo<fqLayerTestParamsSetRevise>& obj) {
fqSpecificParamsRevise fqParams;
InferenceEngine::Precision netPrecision;
InferenceEngine::Precision inPrc, outPrc;
InferenceEngine::Layout inLayout, outLayout;
InferenceEngine::SizeVector inputShapes;
std::string targetDevice;
std::pair<std::string, std::map<std::string, std::string>> config;
std::tie(fqParams, netPrecision, inPrc, outPrc, inLayout, outLayout, inputShapes, targetDevice, config) = obj.param;
size_t levels;
std::vector<size_t> constShape;
std::vector<float> fqDirectArgs;
std::vector<float> inputArg;
ngraph::op::AutoBroadcastSpec broadcast;
std::tie(levels, constShape, fqDirectArgs, inputArg, broadcast) = fqParams;

std::ostringstream result;
result << "IS=" << CommonTestUtils::vec2str(inputShapes) << "_";
result << "CS=" << CommonTestUtils::vec2str(constShape) << "_";
result << "LEVELS=" << levels << "_";
result << "netPRC=" << netPrecision.name() << "_";
result << "inPRC=" << inPrc.name() << "_";
result << "outPRC=" << outPrc.name() << "_";
result << "inL=" << inLayout << "_";
result << "outL=" << outLayout << "_";
result << "trgDev=" << targetDevice;
if (!config.first.empty()) {
result << "_targetConfig=" << config.first;
}
if (!fqDirectArgs.empty()) {
result << "_fqArgs=" << fqDirectArgs[0] << "_" << fqDirectArgs[1] << "_" << fqDirectArgs[2] << "_" << fqDirectArgs[3];
}
if (inputArg.size() == 3) {
result << "_inputArg=" << inputArg[0] << "_" << inputArg[1] << "_" << inputArg[2];
}
result << "_" << broadcast.m_type;
return result.str();
}

void FakeQuantizeLayerTestRevise::SetUp() {
fqSpecificParamsRevise fqParams;
std::vector<size_t> inputShape;
std::pair<std::string, std::map<std::string, std::string>> config;
auto netPrecision = InferenceEngine::Precision::UNSPECIFIED;
std::tie(fqParams, netPrecision, inPrc, outPrc, inLayout, outLayout, inputShape, targetDevice, config) = this->GetParam();
InferenceEngine::SizeVector kernel, stride, dilation;
size_t levels;
std::vector<size_t> constShape;
std::vector<float> fqDirectArg;
std::vector<float> inputArg;
ngraph::op::AutoBroadcastSpec broadcast;
std::tie(levels, constShape, fqDirectArg, inputArg, broadcast) = fqParams;
if (inputArg.size() == 3) {
inputDataMin = inputArg[0];
inputDataMax = inputArg[1];
inputDataResolution = inputArg[2];
}
if (fqDirectArg.size() != 0) {
threshold = (fqDirectArg[3] - fqDirectArg[2]) / levels;
}
auto ngPrc = FuncTestUtils::PrecisionUtils::convertIE2nGraphPrc(netPrecision);
auto params = ngraph::builder::makeParams(ngPrc, {inputShape});
auto paramOuts = ngraph::helpers::convert2OutputVector(ngraph::helpers::castOps2Nodes<ngraph::op::Parameter>(params));

UpdateSeed();

std::shared_ptr<ngraph::Node> fakeQNode;
if (fqDirectArg.empty()) {
int32_t ngraphSeed = seed;
if (NGRAPH_SEED != USE_CLOCK_TIME) {
ngraphSeed = NGRAPH_SEED;
}
std::cout << "\033[0;32m" << "[ ] " << "\033[0;0m"
<< "ngraphSeed = " << ngraphSeed << std::endl;
fakeQNode = ngraph::builder::makeFakeQuantize(paramOuts[0], ngPrc, levels, constShape, ngraphSeed);
} else {
fakeQNode = ngraph::builder::makeFakeQuantize(
paramOuts[0],
ngPrc,
levels,
constShape,
{fqDirectArg[0]},
{fqDirectArg[1]},
{fqDirectArg[2]},
{fqDirectArg[3]});
}
auto fq = std::dynamic_pointer_cast<ngraph::opset1::FakeQuantize>(fakeQNode);

ngraph::ResultVector results{std::make_shared<ngraph::opset1::Result>(fq)};
function = std::make_shared<ngraph::Function>(results, params, "fakeQuantize");

configuration = config.second;
}

InferenceEngine::Blob::Ptr FakeQuantizeLayerTestRevise::GenerateInput(const InferenceEngine::InputInfo &info) const {
return FuncTestUtils::createAndFillBlob(info.getTensorDesc(), inputDataMax - inputDataMin, inputDataMin, 1 / inputDataResolution,
seed);
}

void FakeQuantizeLayerTestRevise::UpdateSeed() {
if (BASE_SEED == USE_CLOCK_TIME) {
seed = std::chrono::system_clock::now().time_since_epoch().count();
} else if (BASE_SEED == USE_INCREMENTAL_SEED) {
seed += 9999;
} else {
seed = BASE_SEED;
}
std::cout << "\033[0;32m" << "[ ] " << "\033[0;0m"
<< "seed = " << seed << std::endl;
}
} // namespace LayerTestsDefinitions
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'ExperimentalDetectronPriorGridGenerator-6',
'ExperimentalDetectronROIFeatureExtractor-6',
'ExperimentalDetectronTopKROIs-6',
'FakeQuantize-1',
'Floor-1'
'FloorMod-1'
'GRUSequence-5',
Expand Down
2 changes: 1 addition & 1 deletion ngraph/core/include/ngraph/op/fake_quantize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#pragma once

#include "ngraph/node.hpp"
#include "ngraph/op/op.hpp"
#include "ngraph/op/util/attr_types.hpp"
#include "ngraph/op/util/fused_op.hpp"

namespace ngraph
{
Expand Down
Loading

0 comments on commit 258e50e

Please sign in to comment.