[LPT] integration branch: Reshape fix, Concat generalization, runtime info usage...
[platform/upstream/dldt.git] / inference-engine / tests / ngraph_functions / src / low_precision_transformations / max_pool_function.cpp
1 // Copyright (C) 2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include "ngraph_functions/low_precision_transformations/max_pool_function.hpp"
6
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"
12
13 namespace ngraph {
14 namespace builder {
15 namespace subgraph {
16
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;
23
24     const std::shared_ptr<ngraph::Node> convert = std::make_shared<ngraph::opset1::Convert>(parent, originalFunctionPrecision);
25     parent = convert;
26
27     if (!values.subtractValues.empty()) {
28         const std::shared_ptr<ngraph::Node> subtract = std::make_shared<ngraph::opset1::Subtract>(
29             parent,
30             std::make_shared<ngraph::opset1::Constant>(originalFunctionPrecision, Shape({ values.subtractValues.size() }), values.subtractValues));
31         parent = subtract;
32     }
33
34     const std::shared_ptr<ngraph::Node> multiply = std::make_shared<ngraph::opset1::Multiply>(
35         parent,
36         std::make_shared<ngraph::opset1::Constant>(originalFunctionPrecision, Shape({ values.mutliplyValues.size() }), values.mutliplyValues));
37     parent = multiply;
38
39     const std::shared_ptr<ngraph::Node> maxPool = std::make_shared<ngraph::opset1::MaxPool>(
40         parent,
41         Strides{ 1, 1 },
42         Shape{ 1, 1 },
43         Shape{ 0, 0 },
44         Shape{ 2, 2 },
45         op::RoundingType::FLOOR);
46     maxPool->set_friendly_name("output");
47
48     ngraph::ResultVector results{ std::make_shared<ngraph::opset1::Result>(maxPool) };
49     return std::make_shared<ngraph::Function>(results, ngraph::ParameterVector{ input }, "MaxPoolTransformation");
50 }
51
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));
57
58     const auto fakeQuantize = ngraph::builder::makeFakeQuantize(
59         input, originalFunctionPrecision, fakeQuantizeOnData.quantizationLevel, fakeQuantizeOnData.constantShape,
60         fakeQuantizeOnData.inputLowValues, fakeQuantizeOnData.inputHighValues, fakeQuantizeOnData.outputLowValues, fakeQuantizeOnData.outputHighValues);
61
62     const std::shared_ptr<ngraph::Node> maxPool = std::make_shared<ngraph::opset1::MaxPool>(
63         fakeQuantize,
64         Strides{ 1, 1 },
65         Shape{ 1, 1 },
66         Shape{ 0, 0 },
67         Shape{ 2, 2 },
68         op::RoundingType::FLOOR);
69
70     ngraph::ResultVector results{ std::make_shared<ngraph::opset1::Result>(maxPool) };
71     return std::make_shared<ngraph::Function>(results, ngraph::ParameterVector{ input }, "MaxPoolTransformation");
72 }
73
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;
80
81     const std::shared_ptr<ngraph::Node> maxPool = std::make_shared<ngraph::opset1::MaxPool>(
82         parent,
83         Strides{ 1, 1 },
84         Shape{ 1, 1 },
85         Shape{ 0, 0 },
86         Shape{ 2, 2 },
87         op::RoundingType::FLOOR);
88     parent = maxPool;
89
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);
92         parent = convert;
93     }
94
95     if (!values.subtractValues.empty()) {
96         const std::shared_ptr<ngraph::Node> subtract = std::make_shared<ngraph::pass::low_precision::DequantizationSubtract>(
97             parent,
98             std::make_shared<ngraph::opset1::Constant>(originalFunctionPrecision, Shape({ values.subtractValues.size() }), values.subtractValues));
99         parent = subtract;
100     }
101
102     const std::shared_ptr<ngraph::Node> multiply = std::make_shared<ngraph::pass::low_precision::DequantizationMultiply>(
103         parent,
104         std::make_shared<ngraph::opset1::Constant>(originalFunctionPrecision, Shape({ values.mutliplyValues.size() }), values.mutliplyValues));
105     multiply->set_friendly_name("output");
106
107     ngraph::ResultVector results{ std::make_shared<ngraph::opset1::Result>(multiply) };
108     return std::make_shared<ngraph::Function>(results, ngraph::ParameterVector{ input }, "MaxPoolTransformation");
109 }
110
111 }  // namespace subgraph
112 }  // namespace builder
113 }  // namespace ngraph