1 // Copyright (C) 2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
5 #include "ngraph_functions/low_precision_transformations/max_pool_function.hpp"
7 #include <ngraph/opsets/opset1.hpp>
8 #include <ngraph_ops/type_relaxed.hpp>
9 #include "low_precision/network_helper.hpp"
10 #include "ngraph_functions/subgraph_builders.hpp"
11 #include "ngraph_functions/low_precision_transformations/common/builders.hpp"
17 std::shared_ptr<ngraph::Function> MaxPoolFunction::getOriginal(
18 const ngraph::element::Type originalFunctionPrecision,
19 const ngraph::Shape& inputShape,
20 const ActualValues& values) {
21 const auto input = std::make_shared<ngraph::opset1::Parameter>(values.lowPrecision, ngraph::Shape(inputShape));
22 std::shared_ptr<ngraph::Node> parent = input;
24 const std::shared_ptr<ngraph::Node> convert = std::make_shared<ngraph::opset1::Convert>(parent, originalFunctionPrecision);
27 if (!values.subtractValues.empty()) {
28 const std::shared_ptr<ngraph::Node> subtract = std::make_shared<ngraph::opset1::Subtract>(
30 std::make_shared<ngraph::opset1::Constant>(originalFunctionPrecision, Shape({ values.subtractValues.size() }), values.subtractValues));
34 const std::shared_ptr<ngraph::Node> multiply = std::make_shared<ngraph::opset1::Multiply>(
36 std::make_shared<ngraph::opset1::Constant>(originalFunctionPrecision, Shape({ values.mutliplyValues.size() }), values.mutliplyValues));
39 const std::shared_ptr<ngraph::Node> maxPool = std::make_shared<ngraph::opset1::MaxPool>(
45 op::RoundingType::FLOOR);
46 maxPool->set_friendly_name("output");
48 ngraph::ResultVector results{ std::make_shared<ngraph::opset1::Result>(maxPool) };
49 return std::make_shared<ngraph::Function>(results, ngraph::ParameterVector{ input }, "MaxPoolTransformation");
52 std::shared_ptr<ngraph::Function> MaxPoolFunction::getOriginal(
53 const ngraph::element::Type originalFunctionPrecision,
54 const ngraph::Shape& inputShape,
55 const FakeQuantizeOnData& fakeQuantizeOnData) {
56 const auto input = std::make_shared<ngraph::opset1::Parameter>(originalFunctionPrecision, ngraph::Shape(inputShape));
58 const auto fakeQuantize = ngraph::builder::makeFakeQuantize(
59 input, originalFunctionPrecision, fakeQuantizeOnData.quantizationLevel, fakeQuantizeOnData.constantShape,
60 fakeQuantizeOnData.inputLowValues, fakeQuantizeOnData.inputHighValues, fakeQuantizeOnData.outputLowValues, fakeQuantizeOnData.outputHighValues);
62 const std::shared_ptr<ngraph::Node> maxPool = std::make_shared<ngraph::opset1::MaxPool>(
68 op::RoundingType::FLOOR);
70 ngraph::ResultVector results{ std::make_shared<ngraph::opset1::Result>(maxPool) };
71 return std::make_shared<ngraph::Function>(results, ngraph::ParameterVector{ input }, "MaxPoolTransformation");
74 std::shared_ptr<ngraph::Function> MaxPoolFunction::getReference(
75 const ngraph::element::Type originalFunctionPrecision,
76 const ngraph::Shape& inputShape,
77 const ExpectedValues& values) {
78 auto input = std::make_shared<ngraph::opset1::Parameter>(values.activationPrecision, ngraph::Shape(inputShape));
79 std::shared_ptr<ngraph::Node> parent = input;
81 const std::shared_ptr<ngraph::Node> maxPool = std::make_shared<ngraph::opset1::MaxPool>(
87 op::RoundingType::FLOOR);
90 if (parent->get_output_element_type(0) != originalFunctionPrecision) {
91 const std::shared_ptr<ngraph::Node> convert = std::make_shared<ngraph::pass::low_precision::DequantizationConvert>(parent, originalFunctionPrecision);
95 if (!values.subtractValues.empty()) {
96 const std::shared_ptr<ngraph::Node> subtract = std::make_shared<ngraph::pass::low_precision::DequantizationSubtract>(
98 std::make_shared<ngraph::opset1::Constant>(originalFunctionPrecision, Shape({ values.subtractValues.size() }), values.subtractValues));
102 const std::shared_ptr<ngraph::Node> multiply = std::make_shared<ngraph::pass::low_precision::DequantizationMultiply>(
104 std::make_shared<ngraph::opset1::Constant>(originalFunctionPrecision, Shape({ values.mutliplyValues.size() }), values.mutliplyValues));
105 multiply->set_friendly_name("output");
107 ngraph::ResultVector results{ std::make_shared<ngraph::opset1::Result>(multiply) };
108 return std::make_shared<ngraph::Function>(results, ngraph::ParameterVector{ input }, "MaxPoolTransformation");
111 } // namespace subgraph
112 } // namespace builder
113 } // namespace ngraph