Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / shape_infer / built_in_shape_infer_general_test.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #pragma once
6
7 #include <gtest/gtest.h>
8 #include <inference_engine/blob_factory.hpp>
9 #include <inference_engine/shape_infer/built-in/ie_built_in_holder.hpp>
10 #include <utility>
11 #include <inference_engine/ie_format_parser.h>
12 #include <xml_helper.hpp>
13 #include <xml_net_builder.hpp>
14 #include <single_layer_common.hpp>
15 #include <tests_common.hpp>
16
17 namespace IE = InferenceEngine;
18
19 struct param_size {
20     // dimensions order: x, y, z, ...
21     std::vector<unsigned> dims;
22     param_size() {}
23 //    param_size(const std::vector<unsigned>& dims) {
24 //        this->dims = dims;
25 //    }
26     param_size(std::initializer_list<unsigned> dims) {
27         this->dims = dims;
28     }
29     bool empty() {
30         return dims.empty();
31     }
32
33     friend std::ostream &operator<<(std::ostream &os, param_size const &paramSize) {
34         auto d_size = paramSize.dims.size();
35         if (d_size > 0) {
36             os << "dims[" << std::to_string(0) << "]=" << std::to_string(paramSize.dims[0]);
37             for (int i = 1; i < paramSize.dims.size(); i++)
38                 os << ", dims[" << std::to_string(i) << "]=" << std::to_string(paramSize.dims[i]);
39         }
40         return os;
41     };
42
43     std::string toSeparetedRow(const char *separator) {
44         auto d_size = dims.size();
45         std::string res;
46         if (d_size > 0) {
47             res = std::to_string(dims[d_size - 1]);
48             for (int i = d_size - 2; i >= 0; i--) {
49                 res += separator + std::to_string(dims[i]);
50             }
51         }
52         return res;
53     }
54 };
55
56 PRETTY_PARAM(kernel, param_size);
57
58 PRETTY_PARAM(stride, param_size);
59
60 PRETTY_PARAM(pad, param_size);
61
62 PRETTY_PARAM(pad_end, param_size);
63
64 PRETTY_PARAM(auto_pad, std::string);
65
66 PRETTY_PARAM(out_channels, unsigned);
67
68 PRETTY_PARAM(group, unsigned);
69
70 PRETTY_PARAM(dilation_factor, param_size);
71
72 PRETTY_PARAM(pool_type, std::string);
73
74 PRETTY_PARAM(exclude_pad, bool);
75
76 PRETTY_PARAM(LayerType, std::string)
77
78 PRETTY_PARAM(LayerDataName, std::string)
79
80 PRETTY_PARAM(InOutShapes, testing::InOutShapes)
81
82 PRETTY_PARAM(NewInOutShapes, testing::InOutShapes)
83
84 PRETTY_PARAM(MapParams, MapStrStr)
85
86 PRETTY_PARAM(CanInfer, bool);
87
88 PRETTY_PARAM(IsTransposed, bool);
89
90 PRETTY_PARAM(TopologyPath, std::string);
91
92 PRETTY_PARAM(ModelPath, std::string);
93
94 static size_t BATCH = 100;
95
96 class BuiltInShapeInferCommon : public TestsCommon {
97 protected:
98     void SetUp() override {
99         holder = std::make_shared<IE::ShapeInfer::BuiltInShapeInferHolder>();
100     }
101
102     IE::IShapeInferImpl::Ptr getShapeInferImpl(const std::string &type) {
103         IE::IShapeInferImpl::Ptr impl;
104         sts = holder->getShapeInferImpl(impl, type.c_str(), &resp);
105         if (sts != IE::StatusCode::OK) THROW_IE_EXCEPTION << resp.msg;
106         return impl;
107     }
108
109 protected:
110     IE::StatusCode sts = IE::StatusCode::GENERAL_ERROR;
111     IE::ResponseDesc resp;
112     std::shared_ptr<IE::IShapeInferExtension> holder;
113 };
114
115 template<class T>
116 class BuiltInShapeInferTestWithParam : public BuiltInShapeInferCommon,
117                                        public testing::WithParamInterface<T> {
118
119 protected:
120     static std::vector<IE::Blob::CPtr> getBlobs(const std::vector<IE::SizeVector>& shapes) {
121         std::vector<IE::Blob::CPtr> inBlobs;
122         for (auto const& dims : shapes) {
123             IE::TensorDesc desc(IE::Precision::FP32, dims, IE::TensorDesc::getLayoutByDims(dims));
124             auto blob = make_blob_with_precision(desc);
125             inBlobs.push_back(blob);
126         }
127         return inBlobs;
128     }
129
130     static IE::ICNNNetwork::InputShapes
131     setInputShapes(const IE::ICNNNetwork &cnnNetwork,
132                    const std::vector<IE::SizeVector> &shapesToSet) {
133         IE::ICNNNetwork::InputShapes inputShapes;
134         IE::InputsDataMap inputs;
135         cnnNetwork.getInputsInfo(inputs);
136         for (const auto &pair : inputs) {
137             auto info = pair.second;
138             if (info) {
139                 auto data = info->getInputData();
140                 if (data) {
141                     inputShapes[data->name] = data->getTensorDesc().getDims();
142                 }
143             }
144         }
145         int i = 0;
146         for (auto &pair : inputShapes) {
147             pair.second = shapesToSet[i++];
148         }
149         return inputShapes;
150     }
151
152     static void checkNetworkInOut(const IE::ICNNNetwork &network,
153                                   const testing::InOutShapes &inOutData) {
154         IE::InputsDataMap inputsDataMap;
155         IE::OutputsDataMap outputsDataMap;
156         network.getInputsInfo(inputsDataMap);
157         network.getOutputsInfo(outputsDataMap);
158         int i = 0;
159         for (auto pair : inputsDataMap) {
160             ASSERT_EQ(inOutData.inDims[i++], pair.second->getTensorDesc().getDims());
161         }
162         i = 0;
163         for (auto pair : outputsDataMap) {
164             ASSERT_EQ(inOutData.outDims[i++], pair.second->getDims());
165         }
166     }
167
168     template<int Version = 3>
169     static IE::details::CNNNetworkImplPtr
170     buildSingleLayerNetwork(const std::string &layerType,
171                             const testing::InOutShapes &inOutShapes,
172                             std::map<std::string, std::string> *params,
173                             const std::string &layerDataName = "data") {
174         auto *parser = new IE::details::FormatParser(Version);
175         return buildSingleLayerNetworkCommon<Version>(parser, layerType, inOutShapes, params, layerDataName);
176     }
177
178 protected:
179     std::vector<IE::SizeVector> outShapes;
180     std::map<std::string, std::string> params;
181     std::map<std::string, IE::Blob::Ptr> blobs;
182 };
183
184 class BuiltInShapeInferImplTest
185         : public BuiltInShapeInferTestWithParam<std::tuple<LayerType, InOutShapes, NewInOutShapes, MapParams, LayerDataName, CanInfer>> {
186 protected:
187     void SetUp() override {
188         BuiltInShapeInferCommon::SetUp();
189         auto params = GetParam();
190         type = std::get<0>(params);
191         inOutShapes = std::get<1>(params);
192         newInOutShapes = std::get<2>(params);
193         layerParams = std::get<3>(params);
194         layerDataName = std::get<4>(params);
195         canInfer = std::get<5>(params);
196     }
197
198 protected:
199     std::string type;
200     testing::InOutShapes inOutShapes;
201     testing::InOutShapes newInOutShapes;
202     MapStrStr layerParams;
203     std::string layerDataName;
204     bool canInfer{};
205 };
206