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