Skip to content

Commit

Permalink
[CPU] Code generation: FakeQuantize decomposition & common transforma…
Browse files Browse the repository at this point in the history
…tions - tests #2
  • Loading branch information
eshoguli committed Mar 20, 2022
1 parent 126ec94 commit b4972a8
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (C) 2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include <gtest/gtest.h>

#include "common_test_utils/ngraph_test_utils.hpp"
#include "snippets/pass/common_optimizations.hpp"
#include "snippets/pass/constant_folding.hpp"
#include "fake_quantize_function.hpp"

// TODO: to dbug only
#include "snippets/op/subgraph.hpp"

namespace ov {
namespace test {
namespace snippets {

namespace {
std::shared_ptr<ngraph::snippets::op::Subgraph> getSubgraph(const std::shared_ptr<Model>& f) {
std::shared_ptr<ngraph::snippets::op::Subgraph> subgraph;
for (const auto& op : f->get_ops()) {
auto tmp_subgraph = as_type_ptr<ngraph::snippets::op::Subgraph>(op);
if (tmp_subgraph != nullptr) {
NGRAPH_CHECK(subgraph == nullptr, "function contains more than one subgraph");
subgraph = tmp_subgraph;
}
}
return subgraph;
}
} // namespace

class FakeQuantizeDecompositionTest : public TransformationTestsF {
public:
void register_passes() {
manager.register_pass<ngraph::snippets::pass::CommonOptimizations>();
manager.register_pass<ngraph::snippets::pass::ConstantFolding>();
}

void TearDown() override {
TransformationTestsF::TearDown();

auto body = getSubgraph(function)->get_body();
auto body_ref = getSubgraph(function_ref)->get_body();
auto res = comparator.compare(body, body_ref);
ASSERT_TRUE(res.valid) << res.message;
}
};

TEST_F(FakeQuantizeDecompositionTest, smoke_Snippets_FakeQuantizeDecomposition) {
function = FakeQuantizeFunction::getSubgraphWithFakeQuantize({1, 3, 299, 299}, element::f32, {{}, {}, {}, {}}, true);
function_ref = FakeQuantizeFunction::getSubgraphWithDecomposedFakeQuantize({1, 3, 299, 299}, element::f32, {{}, {}, {}, {}}, true);
register_passes();
}

} // namespace snippets
} // namespace test
} // namespace ov
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <vector>

#include "subgraph_tests/codegen_quantized.hpp"
#include "subgraph_tests/fake_quantize_decomposition_test.hpp"

using namespace LayerTestsDefinitions;

Expand Down Expand Up @@ -88,9 +88,9 @@ const std::vector<TestValues> testValues = {
};

INSTANTIATE_TEST_SUITE_P(
smoke_CodeGeneration,
CodegenQuantized,
smoke_Snippets,
FakeQuantizeDecompositionTest,
::testing::ValuesIn(testValues),
CodegenQuantized::getTestCaseName);
FakeQuantizeDecompositionTest::getTestCaseName);

} // namespace
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TestValues {
ExpectedValues expected;
};

class CodegenQuantized : public testing::WithParamInterface<TestValues>,
class FakeQuantizeDecompositionTest : public testing::WithParamInterface<TestValues>,
virtual public LayerTestsUtils::LayerTestsCommon {
public:
static std::string getTestCaseName(testing::TestParamInfo<TestValues> obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: Apache-2.0
//

#include "subgraph_tests/codegen_quantized.hpp"
#include "subgraph_tests/fake_quantize_decomposition_test.hpp"

#include <memory>
#include <tuple>
Expand All @@ -12,10 +12,11 @@

#include <ie_core.hpp>
#include "ngraph_ops/type_relaxed.hpp"
#include "fake_quantize_function.hpp"

namespace LayerTestsDefinitions {

std::string CodegenQuantized::getTestCaseName(testing::TestParamInfo<TestValues> obj) {
std::string FakeQuantizeDecompositionTest::getTestCaseName(testing::TestParamInfo<TestValues> obj) {
std::ostringstream result;
result << "IS=" << CommonTestUtils::vec2str(obj.param.actual.inputShape) << "_";
result << "netPRC=" << obj.param.actual.modelType << "_";
Expand All @@ -27,7 +28,7 @@ std::string CodegenQuantized::getTestCaseName(testing::TestParamInfo<TestValues>
return result.str();
}

void CodegenQuantized::GenerateInputs() {
void FakeQuantizeDecompositionTest::GenerateInputs() {
inputs.clear();
const auto& inputsInfo = executableNetwork.GetInputsInfo();
const auto& functionParams = function->get_parameters();
Expand All @@ -51,56 +52,18 @@ void CodegenQuantized::GenerateInputs() {
}
}

void CodegenQuantized::SetUp() {
void FakeQuantizeDecompositionTest::SetUp() {
auto& values = this->GetParam();
targetDevice = values.actual.targetDevice;

const auto parameter = std::make_shared<ngraph::opset1::Parameter>(values.actual.inputType, ngraph::Shape{values.actual.inputShape});
parameter->set_friendly_name("parameter");

const auto convert1 = std::make_shared<ngraph::opset1::Convert>(parameter, ov::element::u8);
convert1->set_friendly_name("convert1");

const auto relu1 = std::make_shared<ngraph::opset1::Relu>(convert1);
relu1->set_friendly_name("relu1");

const auto convert2 = std::make_shared<ngraph::opset1::Convert>(relu1, ov::element::f32);
convert2->set_friendly_name("convert2");

const auto slope2 = std::make_shared<ngraph::opset1::Constant>(ov::element::f32, ov::Shape{}, std::vector<float>{-1.f});
const auto relu2 = std::make_shared<ngraph::opset1::PRelu>(convert2, slope2);
relu2->set_friendly_name("relu2");

auto generate = [](const ov::element::Type precision, const ngraph::Shape& shape, const float initialValue, const std::string& name) {
const auto size = ngraph::shape_size(shape);
std::vector<float> values(size);
for (auto i = 0; i < size; ++i) {
values[i] = static_cast<float>(initialValue + i);
}
auto constant = std::make_shared<ngraph::opset1::Constant>(precision, shape, values);
constant->set_friendly_name(name);
return constant;
};

const auto fakeQuantize = std::make_shared<ngraph::opset1::FakeQuantize>(
relu2,
generate(values.actual.inputType, values.actual.fakeQuantizeShapes[0], values.actual.zeroPoint, "inputLow"),
generate(values.actual.inputType, values.actual.fakeQuantizeShapes[1], 20.f, "inputHigh"),
generate(values.actual.inputType, values.actual.fakeQuantizeShapes[2], values.actual.zeroPoint, "outputLow"),
generate(values.actual.inputType, values.actual.fakeQuantizeShapes[3], 20.f, "outputHigh"),
256ul);
fakeQuantize->set_friendly_name("fakeQuantize");

const auto relu3 = std::make_shared<ngraph::opset1::Relu>(fakeQuantize);
relu3->set_friendly_name("relu3");

const auto result = std::make_shared<ngraph::opset1::Result>(relu3);
result->set_friendly_name("result");

function = std::make_shared<ngraph::Function>(ngraph::ResultVector{result}, ngraph::ParameterVector{parameter}, "CodegenQuantized");
function = ov::test::snippets::FakeQuantizeFunction::get(
values.actual.inputShape,
values.actual.inputType,
values.actual.fakeQuantizeShapes,
values.actual.zeroPoint);
}

void CodegenQuantized::Run() {
void FakeQuantizeDecompositionTest::Run() {
LayerTestsCommon::Run();

auto& values = this->GetParam();
Expand All @@ -126,7 +89,7 @@ void CodegenQuantized::Run() {
}
}

TEST_P(CodegenQuantized, CompareWithRefImpl) {
TEST_P(FakeQuantizeDecompositionTest, CompareWithRefImpl) {
Run();
};

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

#pragma once

#include "ngraph/ngraph.hpp"

namespace ov {
namespace test {
namespace snippets {

class FakeQuantizeFunction {
public:
// Parameter => [service ops for workaround to add FakeQuantize to Snippet] => FakeQuantize => Result
static std::shared_ptr<ov::Model> get(
const ngraph::Shape& inputShape,
const element::Type inputType,
const std::vector<ngraph::Shape>& fakeQuantizeShapes,
const float zeroPoint);

// Parameter => Subgraph (Parameter => FakeQuantize => Result) => Result
static std::shared_ptr<ov::Model> getSubgraphWithFakeQuantize(
const ngraph::Shape& inputShape,
const element::Type inputType,
const std::vector<ngraph::Shape>& fakeQuantizeShapes,
const float zeroPoint);

// Parameter => Subgraph (Parameter => element-wise ops from FakeQuantize decomposition results => Result) => Result
static std::shared_ptr<ov::Model> getSubgraphWithDecomposedFakeQuantize(
const ngraph::Shape& inputShape,
const element::Type inputType,
const std::vector<ngraph::Shape>& fakeQuantizeShapes,
const float zeroPoint);
};

} // namespace snippets
} // namespace test
} // namespace ov

Loading

0 comments on commit b4972a8

Please sign in to comment.