Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / engines / mkldnn / graph / layers / internal / graph_tile_test.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include <gtest/gtest.h>
6 #include <gmock/gmock-spec-builders.h>
7 #include "mkldnn_plugin/mkldnn_graph.h"
8
9 #include "test_graph.hpp"
10
11 #include "single_layer_common.hpp"
12 #include <mkldnn_plugin/mkldnn_extension_utils.h>
13 #include <inference_engine/cnn_network_impl.hpp>
14 #include "tests_common.hpp"
15
16
17 using namespace ::testing;
18 using namespace std;
19 using namespace mkldnn;
20
21
22 struct tile_test_params {
23     struct {
24         size_t n;
25         size_t c;
26         size_t h;
27         size_t w;
28     } in;
29
30     size_t axis;
31     size_t tiles;
32
33     size_t num_prim_desc;
34
35     MKLDNNPlugin::impl_desc_type selectedType;
36
37     std::vector<std::function<void(MKLDNNPlugin::PrimitiveDescInfo)>> comp;
38 };
39
40
41 template <typename data_t>
42 void ref_tile(const InferenceEngine::TBlob<data_t> &src, InferenceEngine::TBlob<data_t> &dst_blob, tile_test_params prm) {
43     const float* m_src = src.readOnly();
44     int m_outer_dim = 1;
45     int m_inner_dim = 1;
46
47     for (int i=0; i < prm.axis; i++ ) m_outer_dim *= src.dims()[i];
48     for (int i=prm.axis; i < src.dims().size(); i++ ) m_inner_dim *= src.dims()[i];
49
50     float* dst = dst_blob.data();
51
52     for (int i = 0; i < m_outer_dim; ++i) {
53         for (int t = 0; t < prm.tiles; ++t) {
54             memcpy(dst, m_src, m_inner_dim* sizeof(float));
55             dst += m_inner_dim;
56         }
57         m_src += m_inner_dim;
58     }
59 }
60
61 class MKLDNNGraphTileTests: public TestsCommon,
62                                      public WithParamInterface<tile_test_params> {
63     std::string model_t = R"V0G0N(
64 <Net Name="Tile_Only" version="2" precision="FP32" batch="1">
65     <layers>
66         <layer name="in1" type="Input" precision="FP32" id="0">
67             <output>
68                 <port id="0">
69                     <dim>_IN_</dim>
70                     <dim>_IC_</dim>
71                     <dim>_IH_</dim>
72                     <dim>_IW_</dim>
73                 </port>
74             </output>
75         </layer>
76         <layer name="tile" id="1" type="Tile" precision="FP32">
77             <data axis="_AX_" tiles="_TL_"/>
78
79             <input>
80                 <port id="1">
81                     <dim>_IN_</dim>
82                     <dim>_IC_</dim>
83                     <dim>_IH_</dim>
84                     <dim>_IW_</dim>
85                 </port>
86             </input>
87             <output>
88                 <port id="2">
89                     <dim>_ON_</dim>
90                     <dim>_OC_</dim>
91                     <dim>_OH_</dim>
92                     <dim>_OW_</dim>
93                 </port>
94             </output>
95         </layer>
96     </layers>
97     <edges>
98         <edge from-layer="0" from-port="0" to-layer="1" to-port="1"/>
99     </edges>
100 </Net>
101 )V0G0N";
102
103 protected:
104     std::string getModel(tile_test_params p) {
105         std::string model = model_t;
106
107         REPLACE_WITH_NUM(model, "_IW_", p.in.w);
108         REPLACE_WITH_NUM(model, "_IH_", p.in.h);
109         REPLACE_WITH_NUM(model, "_IC_", p.in.c);
110         REPLACE_WITH_NUM(model, "_IN_", p.in.n);
111
112         REPLACE_WITH_NUM(model, "_OW_", (p.axis == 3) ? p.in.w*p.tiles : p.in.w);
113         REPLACE_WITH_NUM(model, "_OH_", (p.axis == 2) ? p.in.h*p.tiles : p.in.h);
114         REPLACE_WITH_NUM(model, "_OC_", (p.axis == 1) ? p.in.c*p.tiles : p.in.c);
115         REPLACE_WITH_NUM(model, "_ON_", (p.axis == 0) ? p.in.n*p.tiles : p.in.n);
116
117         REPLACE_WITH_NUM(model, "_AX_", p.axis);
118         REPLACE_WITH_NUM(model, "_TL_", p.tiles);
119
120         return model;
121     }
122     virtual void TearDown() {
123     }
124
125     virtual void SetUp() {
126         try {
127             TestsCommon::SetUp();
128             tile_test_params p = ::testing::WithParamInterface<tile_test_params>::GetParam();
129             std::string model = getModel(p);
130
131             InferenceEngine::CNNNetReader net_reader;
132             ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
133
134             MKLDNNGraphTestClass graph;
135             graph.CreateGraph(net_reader.getNetwork());
136             auto& nodes = graph.getNodes();
137             for (int i = 0; i < nodes.size(); i++) {
138                 if (nodes[i]->getType() == MKLDNNPlugin::Tile) {
139                     ASSERT_EQ(p.num_prim_desc, nodes[i]->getSupportedPrimitiveDescriptors().size());
140                     for (size_t j = 0; j < p.num_prim_desc && j < p.comp.size(); j++) {
141                         p.comp.at(j)(nodes[i]->getSupportedPrimitiveDescriptors().at(j));
142                     }
143                     ASSERT_NE(nullptr, nodes[i]->getSelectedPrimitiveDescriptor());
144                     ASSERT_EQ(p.selectedType, nodes[i]->getSelectedPrimitiveDescriptor()->getImplementationType());
145                 }
146             }
147
148             InferenceEngine::SizeVector dims_src = {p.in.n, p.in.c, p.in.h, p.in.w};
149
150             InferenceEngine::Blob::Ptr src = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src);
151             src->allocate();
152             fill_data(src->buffer(), src->size());
153
154             InferenceEngine::TBlob<float>* srcPtr = dynamic_cast<InferenceEngine::TBlob<float>*>(src.get());
155
156             if (srcPtr == nullptr)
157                 FAIL() << "Cannot cast blob to TBlob<float>.";
158
159             InferenceEngine::BlobMap srcs;
160             srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src));
161
162             InferenceEngine::OutputsDataMap out;
163             out = net_reader.getNetwork().getOutputsInfo();
164             InferenceEngine::BlobMap outputBlobs;
165
166             std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
167
168             InferenceEngine::TBlob<float>::Ptr output;
169             output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
170             output->allocate();
171             outputBlobs[item.first] = output;
172
173             graph.Infer(srcs, outputBlobs);
174
175             InferenceEngine::TBlob<float> dst_ref(item.second->getTensorDesc());
176             dst_ref.allocate();
177
178             ref_tile(*srcPtr, dst_ref, p);
179
180             compare(*output, dst_ref);
181         } catch (const InferenceEngine::details::InferenceEngineException &e) {
182             FAIL() << e.what();
183         }
184     }
185 };
186
187 TEST_P(MKLDNNGraphTileTests, TestsTile) {}
188
189
190 INSTANTIATE_TEST_CASE_P(
191         TestsTile, MKLDNNGraphTileTests,
192         ::testing::Values(
193                 tile_test_params{
194                         {1, 128, 1, 1}, 3, 24, 1, MKLDNNPlugin::impl_desc_type::unknown, {
195                                          [](MKLDNNPlugin::PrimitiveDescInfo impl) {
196                                              ASSERT_EQ(MKLDNNPlugin::impl_desc_type::unknown, impl.getImplementationType());
197                                              ASSERT_EQ(1, impl.getConfig().inConfs.size());
198                                              ASSERT_EQ(1, impl.getConfig().outConfs.size());
199                                              ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(0).desc.getLayout());
200                                              ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().outConfs.at(0).desc.getLayout());
201                                          }
202                                  }}));
203
204 class MKLDNNGraphDynBatchTileTests: public MKLDNNGraphTileTests {
205 protected:
206     virtual void SetUp() {
207         try {
208             TestsCommon::SetUp();
209             tile_test_params p = ::testing::WithParamInterface<tile_test_params>::GetParam();
210             std::string model = getModel(p);
211             size_t MB = p.in.n;
212             if (MB < 2)
213                 MB = 2;
214
215             InferenceEngine::CNNNetReader net_reader;
216             ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
217             InferenceEngine::CNNNetwork network = net_reader.getNetwork();
218             auto implNet = dynamic_cast<InferenceEngine::details::CNNNetworkImpl *>(&((InferenceEngine::ICNNNetwork&)network));
219             ASSERT_NE(nullptr, implNet) << "Failed to cast ICNNNetwork to CNNNetworkImpl";
220             InferenceEngine::ResponseDesc resp;
221             InferenceEngine::StatusCode sts  = implNet->setBatchSizeReshape(MB, &resp);
222             ASSERT_EQ((int)InferenceEngine::StatusCode::OK, sts) << resp.msg;
223
224             MKLDNNGraphTestClass graph;
225             graph.setProperty({{InferenceEngine::PluginConfigParams::KEY_DYN_BATCH_ENABLED, InferenceEngine::PluginConfigParams::YES}});
226             graph.CreateGraph(net_reader.getNetwork());
227
228             InferenceEngine::SizeVector dims_src = {MB, p.in.c, p.in.h, p.in.w};
229
230             InferenceEngine::Blob::Ptr src = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src);
231             src->allocate();
232             fill_data(src->buffer(), src->size());
233
234             InferenceEngine::TBlob<float>* srcPtr = dynamic_cast<InferenceEngine::TBlob<float>*>(src.get());
235
236             if (srcPtr == nullptr)
237                 FAIL() << "Cannot cast blob to TBlob<float>.";
238
239             InferenceEngine::BlobMap srcs;
240             srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src));
241
242             InferenceEngine::OutputsDataMap out;
243             out = net_reader.getNetwork().getOutputsInfo();
244             InferenceEngine::BlobMap outputBlobs;
245
246             std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
247
248             InferenceEngine::TBlob<float>::Ptr output;
249             output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
250             output->allocate();
251             outputBlobs[item.first] = output;
252
253             auto checkTile = [](const MKLDNNPlugin::MKLDNNNodePtr& node) {
254                 return node->getType() == MKLDNNPlugin::Tile;
255             };
256
257             graph.checkDynBatch(srcs, outputBlobs, MB, MB, checkTile);
258             graph.checkDynBatch(srcs, outputBlobs, 1, MB, checkTile);
259         } catch (const InferenceEngine::details::InferenceEngineException &e) {
260             FAIL() << e.what();
261         }
262     }
263 };
264
265 TEST_P(MKLDNNGraphDynBatchTileTests, TestsDynBatchTile) {}
266
267
268 INSTANTIATE_TEST_CASE_P(
269         TestsDynBatchTile, MKLDNNGraphDynBatchTileTests,
270         ::testing::Values(
271                 tile_test_params{
272                         {1, 128, 1, 1}, 3, 24, 1, MKLDNNPlugin::impl_desc_type::unknown, {
273                                 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
274                                     ASSERT_EQ(MKLDNNPlugin::impl_desc_type::unknown, impl.getImplementationType());
275                                     ASSERT_EQ(1, impl.getConfig().inConfs.size());
276                                     ASSERT_EQ(1, impl.getConfig().outConfs.size());
277                                     ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(0).desc.getLayout());
278                                     ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().outConfs.at(0).desc.getLayout());
279                                 }
280                         }}));