42b3ef950129a6b49adbc5ea16c4061040e89262
[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 "ngraph_functions/subgraph_builders.hpp"
10 #include "low_precision/network_helper.hpp"
11
12 namespace ngraph {
13 namespace builder {
14 namespace subgraph {
15
16 std::shared_ptr<ngraph::Function> MaxPoolFunction::getOriginal(
17     const ngraph::element::Type originalFunctionPrecision,
18     const ngraph::Shape& inputShape,
19     const ActualValues& values) {
20     const auto input = std::make_shared<ngraph::opset1::Parameter>(values.lowPrecision, ngraph::Shape(inputShape));
21     std::shared_ptr<ngraph::Node> parent = input;
22
23     const std::shared_ptr<ngraph::Node> convert = std::make_shared<ngraph::opset1::Convert>(parent, originalFunctionPrecision);
24     parent = convert;
25
26     if (!values.subtractValues.empty()) {
27         const std::shared_ptr<ngraph::Node> subtract = std::make_shared<ngraph::opset1::Subtract>(
28             parent,
29             std::make_shared<ngraph::opset1::Constant>(originalFunctionPrecision, Shape({ values.subtractValues.size() }), values.subtractValues));
30         parent = subtract;
31     }
32
33     const std::shared_ptr<ngraph::Node> multiply = std::make_shared<ngraph::opset1::Multiply>(
34         parent,
35         std::make_shared<ngraph::opset1::Constant>(originalFunctionPrecision, Shape({ values.mutliplyValues.size() }), values.mutliplyValues));
36     parent = multiply;
37
38     const std::shared_ptr<ngraph::Node> maxPool = std::make_shared<ngraph::opset1::MaxPool>(
39         parent,
40         Strides{ 1, 1 },
41         Shape{ 1, 1 },
42         Shape{ 0, 0 },
43         Shape{ 2, 2 },
44         op::RoundingType::FLOOR);
45     maxPool->set_friendly_name("output");
46
47     ngraph::ResultVector results{ std::make_shared<ngraph::opset1::Result>(maxPool) };
48     return std::make_shared<ngraph::Function>(results, ngraph::ParameterVector{ input }, "MaxPoolTransformation");
49 }
50
51 std::shared_ptr<ngraph::Function> MaxPoolFunction::getOriginal(
52     const ngraph::element::Type originalFunctionPrecision,
53     const ngraph::Shape& inputShape,
54     const FakeQuantizeOnData& fakeQuantizeOnData) {
55     const auto input = std::make_shared<ngraph::opset1::Parameter>(originalFunctionPrecision, ngraph::Shape(inputShape));
56
57     const auto fakeQuantize = ngraph::builder::makeFakeQuantize(
58         input, originalFunctionPrecision, fakeQuantizeOnData.quantizationLevel, fakeQuantizeOnData.constantShape,
59         fakeQuantizeOnData.inputLowValues, fakeQuantizeOnData.inputHighValues, fakeQuantizeOnData.outputLowValues, fakeQuantizeOnData.outputHighValues);
60
61     const std::shared_ptr<ngraph::Node> maxPool = std::make_shared<ngraph::opset1::MaxPool>(
62         fakeQuantize,
63         Strides{ 1, 1 },
64         Shape{ 1, 1 },
65         Shape{ 0, 0 },
66         Shape{ 2, 2 },
67         op::RoundingType::FLOOR);
68
69     ngraph::ResultVector results{ std::make_shared<ngraph::opset1::Result>(maxPool) };
70     return std::make_shared<ngraph::Function>(results, ngraph::ParameterVector{ input }, "MaxPoolTransformation");
71 }
72
73 std::shared_ptr<ngraph::Function> MaxPoolFunction::getReference(
74     const ngraph::element::Type originalFunctionPrecision,
75     const ngraph::Shape& inputShape,
76     const ExpectedValues& values) {
77     auto input = std::make_shared<ngraph::opset1::Parameter>(values.activationPrecision, ngraph::Shape(inputShape));
78     std::shared_ptr<ngraph::Node> parent = input;
79
80     const std::shared_ptr<ngraph::Node> maxPool = std::make_shared<ngraph::opset1::MaxPool>(
81         parent,
82         Strides{ 1, 1 },
83         Shape{ 1, 1 },
84         Shape{ 0, 0 },
85         Shape{ 2, 2 },
86         op::RoundingType::FLOOR);
87     parent = maxPool;
88
89     if (parent->get_output_element_type(0) != originalFunctionPrecision) {
90         const std::shared_ptr<ngraph::Node> convert = std::make_shared<ngraph::opset1::Convert>(parent, originalFunctionPrecision);
91         parent = convert;
92     }
93
94     if (!values.subtractValues.empty()) {
95         const std::shared_ptr<ngraph::Node> subtract = std::make_shared<ngraph::opset1::Subtract>(
96             parent,
97             std::make_shared<ngraph::opset1::Constant>(originalFunctionPrecision, Shape({ values.subtractValues.size() }), values.subtractValues));
98         parent = subtract;
99     }
100
101     const std::shared_ptr<ngraph::Node> multiply = std::make_shared<ngraph::opset1::Multiply>(
102         parent,
103         std::make_shared<ngraph::opset1::Constant>(originalFunctionPrecision, Shape({ values.mutliplyValues.size() }), values.mutliplyValues));
104     multiply->set_friendly_name("output");
105
106     ngraph::ResultVector results{ std::make_shared<ngraph::opset1::Result>(multiply) };
107     return std::make_shared<ngraph::Function>(results, ngraph::ParameterVector{ input }, "MaxPoolTransformation");
108 }
109
110 }  // namespace subgraph
111 }  // namespace builder
112 }  // namespace ngraph