Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / engines / mkldnn / graph / layers / extensions / interp_tests.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 "tests_common.hpp"
14
15
16 using namespace ::testing;
17 using namespace std;
18 using namespace mkldnn;
19
20
21 struct interp_test_params {
22     struct {
23         size_t n;
24         size_t c;
25         size_t h;
26         size_t w;
27     } in;
28
29     struct {
30         size_t h;
31         size_t w;
32     } out;
33
34     int pad_beg;
35     int pad_end;
36
37     size_t num_prim_desc;
38
39     int selectedType;
40
41     std::vector<std::function<void(MKLDNNPlugin::PrimitiveDescInfo)>> comp;
42 };
43
44 void interpolate(const int N, const int C, const float *src, const int x1, const int y1, const int IH_pad, const int IW_pad,
45                       const int IH, const int IW, float *dst, const int x2, const int y2, const int OH_pad, const int OW_pad, const int OH, const int OW) {
46     if (IH_pad == OH_pad && IW_pad == OW_pad) {
47         for (int i = 0; i < N * C * OH * OW; i++) {
48             dst[i] = src[i];
49         }
50         return;
51     }
52
53     const float rh = (OH_pad > 1) ? static_cast<float>(IH_pad - 1) / (OH_pad - 1) : 0.0f;
54     const float rw = (OW_pad > 1) ? static_cast<float>(IW_pad - 1) / (OW_pad - 1) : 0.0f;
55
56     const int block_size = 1;
57
58     // Align channel number to block size to deal with channels padding in IE with multiple blobs
59     int CB = (C + block_size - 1) & (-block_size);
60
61     int CH = (C + block_size - 1) / block_size;
62
63     for (int n = 0; n < N; n++) {
64         for (int cb = 0; cb < CH; ++cb) {
65             for (int h = 0; h < OH_pad; ++h) {
66                 const float *psrc = src + n * CB * IH * IW;
67
68                 float fh = rh * h;
69                 int ih0 = static_cast<int>(fh);
70                 int ih1 = (ih0 < IH_pad - 1) ? ih0 + 1 : ih0;
71
72                 float h_lambda0 = fh - ih0;
73                 float h_lambda1 = 1.0f - h_lambda0;
74
75                 for (int w = 0; w < OW_pad; ++w) {
76                     float fw = rw * w;
77                     int iw0 = static_cast<int>(fw);
78                     int iw1 = (iw0 < IW_pad - 1) ? iw0 + 1 : iw0;
79
80                     float w_lambda0 = fw - iw0;
81                     float w_lambda1 = 1.0f - w_lambda0;
82
83                     const float *psrc00 =
84                             psrc + cb * block_size * IW * IH + (y1 + ih0) * IW * block_size + (x1 + iw0) * block_size;
85                     const float *psrc01 =
86                             psrc + cb * block_size * IW * IH + (y1 + ih0) * IW * block_size + (x1 + iw1) * block_size;
87                     const float *psrc10 =
88                             psrc + cb * block_size * IW * IH + (y1 + ih1) * IW * block_size + (x1 + iw0) * block_size;
89                     const float *psrc11 =
90                             psrc + cb * block_size * IW * IH + (y1 + ih1) * IW * block_size + (x1 + iw1) * block_size;
91
92                     float *pdst = dst + n * CB * OH * OW + cb * block_size * OW * OH + (y2 + h) * OW * block_size +
93                                   (x2 + w) * block_size;
94
95                     for (int c = 0; c < block_size; ++c) {
96                         pdst[c] = h_lambda1 * (w_lambda1 * psrc00[c] + w_lambda0 * psrc01[c]) +
97                                   h_lambda0 * (w_lambda1 * psrc10[c] + w_lambda0 * psrc11[c]);
98                     }
99                 }
100             }
101         }
102     }
103 }
104
105 template <typename data_t>
106 void ref_interp(const InferenceEngine::TBlob<data_t> &src, InferenceEngine::TBlob<data_t> &dst, interp_test_params prm) {
107     int IB = static_cast<int>(src.getTensorDesc().getDims()[0]);
108     int IC = static_cast<int>(src.getTensorDesc().getDims()[1]);
109     int IH = static_cast<int>(src.getTensorDesc().getDims()[2]);
110     int IW = static_cast<int>(src.getTensorDesc().getDims()[3]);
111
112     int OH = static_cast<int>(dst.getTensorDesc().getDims()[2]);
113     int OW = static_cast<int>(dst.getTensorDesc().getDims()[3]);
114
115     int IH_pad = IH + prm.pad_beg + prm.pad_end;
116     int IW_pad = IW + prm.pad_beg + prm.pad_end;
117
118     const data_t *src_data = src.readOnly();
119     data_t *dst_data = dst.data();
120
121     interpolate(IB, IC, src_data, -prm.pad_beg, -prm.pad_beg, IH_pad, IW_pad, IH, IW, dst_data, 0, 0, OH, OW, OH, OW);
122 }
123
124 class MKLDNNCPUExtInterpTests: public TestsCommon, public WithParamInterface<interp_test_params> {
125     std::string model_t = R"V0G0N(
126 <Net Name="Convolution_Only" version="2" precision="FP32" batch="1">
127     <layers>
128         <layer name="in1" type="Input" precision="FP32" id="0">
129             <output>
130                 <port id="0">
131                     <dim>_IN_</dim>
132                     <dim>_IC_</dim>
133                     <dim>_IH_</dim>
134                     <dim>_IW_</dim>
135                 </port>
136             </output>
137         </layer>
138         <layer name="interp1" id="1" type="Interp" precision="FP32">
139             <data pad_beg="_PB_" pad_end="_PE_" height="_OH_" width="_OW_"/>
140
141             <input>
142                 <port id="1">
143                     <dim>_IN_</dim>
144                     <dim>_IC_</dim>
145                     <dim>_IH_</dim>
146                     <dim>_IW_</dim>
147                 </port>
148             </input>
149             <output>
150                 <port id="2">
151                     <dim>_IN_</dim>
152                     <dim>_IC_</dim>
153                     <dim>_OH_</dim>
154                     <dim>_OW_</dim>
155                 </port>
156             </output>
157         </layer>
158     </layers>
159     <edges>
160         <edge from-layer="0" from-port="0" to-layer="1" to-port="1"/>
161     </edges>
162 </Net>
163 )V0G0N";
164
165     std::string getModel(interp_test_params p) {
166         std::string model = model_t;
167         REPLACE_WITH_NUM(model, "_IW_", p.in.w);
168         REPLACE_WITH_NUM(model, "_IH_", p.in.h);
169         REPLACE_WITH_NUM(model, "_IC_", p.in.c);
170         REPLACE_WITH_NUM(model, "_IN_", p.in.n);
171
172         REPLACE_WITH_NUM(model, "_OH_", p.out.h);
173         REPLACE_WITH_NUM(model, "_OW_", p.out.w);
174
175         REPLACE_WITH_NUM(model, "_PB_", p.pad_beg);
176         REPLACE_WITH_NUM(model, "_PE_", p.pad_end);
177         return model;
178     }
179
180 protected:
181     virtual void TearDown() {
182     }
183
184     virtual void SetUp() {
185         try {
186             TestsCommon::SetUp();
187             interp_test_params p = ::testing::WithParamInterface<interp_test_params>::GetParam();
188             std::string model = getModel(p);
189
190             InferenceEngine::CNNNetReader net_reader;
191             ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
192
193             InferenceEngine::Extension cpuExt(make_so_name("cpu_extension"));
194             MKLDNNPlugin::MKLDNNExtensionManager::Ptr extMgr(new MKLDNNPlugin::MKLDNNExtensionManager());
195             extMgr->AddExtension(InferenceEngine::IExtensionPtr(&cpuExt, [](InferenceEngine::IExtension*){}));
196
197             MKLDNNGraphTestClass graph;
198             graph.CreateGraph(net_reader.getNetwork(), extMgr);
199
200             auto& nodes = graph.getNodes();
201             nodes = graph.getNodes();
202             for (auto &node : nodes) {
203                 if (node->getName() == "interp1") {
204                     ASSERT_LE(p.num_prim_desc, node->getSupportedPrimitiveDescriptors().size());
205                     for (size_t j = 0; j < p.num_prim_desc && j < p.comp.size(); j++) {
206                         p.comp.at(j)(node->getSupportedPrimitiveDescriptors().at(j));
207                     }
208                     ASSERT_NE(nullptr, node->getSelectedPrimitiveDescriptor());
209                     ASSERT_EQ(p.selectedType,
210                               node->getSelectedPrimitiveDescriptor()->getImplementationType() & p.selectedType);
211                 }
212             }
213             ASSERT_LE(4, nodes.size());
214
215             InferenceEngine::SizeVector dims_src = {p.in.w, p.in.h, p.in.c, p.in.n};
216
217             InferenceEngine::Blob::Ptr src = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src);
218             src->allocate();
219             fill_data(src->buffer(), src->size());
220
221             auto * srcPtr = dynamic_cast<InferenceEngine::TBlob<float>*>(src.get());
222
223             if (srcPtr == nullptr)
224                 FAIL() << "Cannot cast blob to TBlob<float>.";
225
226             InferenceEngine::BlobMap srcs;
227             srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src));
228
229             InferenceEngine::OutputsDataMap out;
230             out = net_reader.getNetwork().getOutputsInfo();
231             InferenceEngine::BlobMap outputBlobs;
232
233             std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
234
235             InferenceEngine::TBlob<float>::Ptr output;
236             output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
237             output->allocate();
238             outputBlobs[item.first] = output;
239
240             graph.Infer(srcs, outputBlobs);
241
242
243             InferenceEngine::TBlob<float> dst_ref(item.second->getTensorDesc());
244             dst_ref.allocate();
245             ref_interp(*srcPtr, dst_ref, p);
246             compare(*output, dst_ref);
247         } catch (const InferenceEngine::details::InferenceEngineException &e) {
248             FAIL() << e.what();
249         }
250     }
251 };
252
253 TEST_P(MKLDNNCPUExtInterpTests, TestsInterp) {}
254
255 INSTANTIATE_TEST_CASE_P(
256         TestsInterp, MKLDNNCPUExtInterpTests,
257         ::testing::Values(
258                 interp_test_params{{1, 256, 1, 1}, {33, 65}, 0, 0, 1, MKLDNNPlugin::impl_desc_type::unknown },
259                 interp_test_params{{1, 2, 33, 65}, {33, 65}, 0, 0, 1, MKLDNNPlugin::impl_desc_type::unknown }));