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