Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / engines / mkldnn / graph / layers / internal / graph_batchnorm_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 using namespace ::testing;
17 using namespace std;
18 using namespace mkldnn;
19
20 struct batchnorm4D_test_params {
21     struct {
22         size_t n;
23         size_t c;
24         size_t h;
25         size_t w;
26     } in;
27
28     double epsilon;
29
30     size_t num_prim_desc;
31
32     MKLDNNPlugin::impl_desc_type selectedType;
33     std::vector<MKLDNNPlugin::impl_desc_type> preferTypes;
34
35     std::vector<std::function<void(MKLDNNPlugin::PrimitiveDescInfo)>> comp;
36 };
37
38 template <typename data_t>
39 void ref_batchnorm4D(const InferenceEngine::TBlob<data_t> &src, const data_t *variance, const data_t *mean,
40                      InferenceEngine::TBlob<data_t> &dst, batchnorm4D_test_params prm) {
41     size_t MB = src.dims()[0];
42     size_t IC = src.dims()[1];
43     size_t IH = src.dims()[2];
44     size_t IW = src.dims()[3];
45
46     const double eps = prm.epsilon;
47
48     const data_t *src_data = src.readOnly();
49     data_t *dst_data = dst.data();
50
51     for (int c = 0; c < IC; ++c) {
52         data_t v_mean = mean[c];
53         data_t v_variance = variance[c];
54         data_t sqrt_variance = 0;
55
56         sqrt_variance = 1. / sqrt(v_variance + eps);
57
58         for (int n = 0; n < MB; ++n)
59             for (int h = 0; h < IH; ++h)
60                 for (int w = 0; w < IW; ++w) {
61                     size_t idx = n * IC * IH * IW
62                                  + c * IH * IW
63                                  + h * IW + w;
64                     dst_data[idx] = (src_data[idx] - v_mean) * sqrt_variance;
65                 }
66     }
67 }
68
69 class MKLDNNGraphBatchNormTests: public TestsCommon,
70                                      public WithParamInterface<batchnorm4D_test_params> {
71     std::string model_t = R"V0G0N(
72 <Net Name="BatchNorm4D_Only" version="2" precision="FP32" batch="1">
73     <layers>
74         <layer name="in1" type="Input" precision="FP32" id="0">
75             <output>
76                 <port id="0">
77                     <dim>_IN_</dim>
78                     <dim>_IC_</dim>
79                     <dim>_IH_</dim>
80                     <dim>_IW_</dim>
81                 </port>
82             </output>
83         </layer>
84         <layer name="batchNorm" id="1" type="BatchNormalization" precision="FP32">
85             <batch_norm_data epsilon="_EPSILON_" PrimitivesPriority="_IMPLS_"/>
86
87             <weights offset="0" size="_S1_" />
88             <biases offset="_S1_" size="_S1_" />
89
90             <input>
91                 <port id="1">
92                     <dim>_IN_</dim>
93                     <dim>_IC_</dim>
94                     <dim>_IH_</dim>
95                     <dim>_IW_</dim>
96                 </port>
97             </input>
98             <output>
99                 <port id="2">
100                     <dim>_IN_</dim>
101                     <dim>_OC_</dim>
102                     <dim>_OH_</dim>
103                     <dim>_OW_</dim>
104                 </port>
105             </output>
106         </layer>
107     </layers>
108     <edges>
109         <edge from-layer="0" from-port="0" to-layer="1" to-port="1"/>
110     </edges>
111 </Net>
112 )V0G0N";
113
114 protected:
115     std::string getModel(batchnorm4D_test_params p) {
116         std::string model = model_t;
117         REPLACE_WITH_NUM(model, "_IW_", p.in.w);
118         REPLACE_WITH_NUM(model, "_IH_", p.in.h);
119         REPLACE_WITH_NUM(model, "_IC_", p.in.c);
120         REPLACE_WITH_NUM(model, "_IN_", p.in.n);
121         REPLACE_WITH_NUM(model, "_EPSILON_", p.epsilon);
122
123         REPLACE_WITH_NUM(model, "_OW_", p.in.w);
124         REPLACE_WITH_NUM(model, "_OH_", p.in.h);
125         REPLACE_WITH_NUM(model, "_OC_", p.in.c);
126
127         size_t w_data_size = p.in.c * sizeof(float);
128         REPLACE_WITH_NUM(model, "_S1_", w_data_size);
129
130         std::string impls;
131         for (const auto& preferType : p.preferTypes) {
132             if (!impls.empty())
133                 impls += ",";
134             impls += "cpu:" + MKLDNNGraphTestClass::getStrPrimitiveDescriptorType(preferType);
135         }
136         REPLACE_WITH_STR(model, "_IMPLS_", impls);
137         return model;
138     }
139
140     virtual void TearDown() {
141     }
142
143     virtual void SetUp() {
144         try {
145             TestsCommon::SetUp();
146             batchnorm4D_test_params p = ::testing::WithParamInterface<batchnorm4D_test_params>::GetParam();
147             std::string model = getModel(p);
148
149             InferenceEngine::CNNNetReader net_reader;
150             ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
151
152             InferenceEngine::TBlob<uint8_t> *weights = new InferenceEngine::TBlob<uint8_t>(InferenceEngine::Precision::FP32, InferenceEngine::C, {p.in.c * 2 * sizeof(float)});
153             weights->allocate();
154             fill_data(weights->buffer(), weights->size() / sizeof(float));
155             float * data = weights->buffer();
156             for (size_t i = 0; i < weights->size() / sizeof(float); i++) {
157                 if (data[i] < 0) {
158                     data[i] *= -1;
159                 }
160             }
161
162             InferenceEngine::TBlob<uint8_t>::Ptr weights_ptr = InferenceEngine::TBlob<uint8_t>::Ptr(weights);
163
164             net_reader.SetWeights(weights_ptr);
165
166             MKLDNNGraphTestClass graph;
167             graph.CreateGraph(net_reader.getNetwork());
168
169             auto& nodes = graph.getNodes();
170             for (int i = 0; i < nodes.size(); i++) {
171                 if (nodes[i]->getType() == MKLDNNPlugin::BatchNormalization) {
172                     ASSERT_LE(p.num_prim_desc, nodes[i]->getSupportedPrimitiveDescriptors().size());
173                     for (size_t j = 0; j < p.num_prim_desc && j < p.comp.size(); j++) {
174                         p.comp.at(j)(nodes[i]->getSupportedPrimitiveDescriptors().at(j));
175                     }
176                     ASSERT_NE(nullptr, nodes[i]->getSelectedPrimitiveDescriptor());
177                     ASSERT_TRUE(nodes[i]->getSelectedPrimitiveDescriptor()->getImplementationType() | p.selectedType);
178                 }
179             }
180             ASSERT_GE(5, nodes.size());
181
182             InferenceEngine::SizeVector dims_src = {p.in.n, p.in.c, p.in.h, p.in.w};
183
184             InferenceEngine::Blob::Ptr src = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src);
185             src->allocate();
186             fill_data(src->buffer(), src->size());
187
188             InferenceEngine::TBlob<float>* srcPtr = dynamic_cast<InferenceEngine::TBlob<float>*>(src.get());
189
190             if (srcPtr == nullptr)
191                 FAIL() << "Cannot cast blob to TBlob<float>.";
192
193             InferenceEngine::BlobMap srcs;
194             srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src));
195
196             InferenceEngine::OutputsDataMap out;
197             out = net_reader.getNetwork().getOutputsInfo();
198             InferenceEngine::BlobMap outputBlobs;
199
200             std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
201
202             InferenceEngine::TBlob<float>::Ptr output;
203             output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
204             output->allocate();
205             outputBlobs[item.first] = output;
206
207             graph.Infer(srcs, outputBlobs);
208
209             InferenceEngine::TBlob<float> dst_ref(item.second->getTensorDesc());
210             dst_ref.allocate();
211
212             ref_batchnorm4D(*srcPtr, (const float*) weights->buffer(), ((const float*) weights->buffer() + p.in.c), dst_ref, p);
213
214             compare(*output, dst_ref);
215         } catch (const InferenceEngine::details::InferenceEngineException &e) {
216             FAIL() << e.what();
217         }
218     }
219 };
220
221 TEST_P(MKLDNNGraphBatchNormTests, TestsBatchNorm) {}
222
223
224 INSTANTIATE_TEST_CASE_P(
225         TestsBatchNorm, MKLDNNGraphBatchNormTests,
226         ::testing::Values(
227                 batchnorm4D_test_params{{1, 32, 128, 256}, 1e-6, 5, MKLDNNPlugin::impl_desc_type::jit},
228                 batchnorm4D_test_params{{3, 3, 128, 256}, 1e-6, 5, MKLDNNPlugin::impl_desc_type::jit},
229                 batchnorm4D_test_params{{1, 32, 128, 256}, 1e-6, 5, MKLDNNPlugin::impl_desc_type::ref, {MKLDNNPlugin::impl_desc_type::ref_any}},
230                 batchnorm4D_test_params{{3, 3, 128, 256}, 1e-6, 5, MKLDNNPlugin::impl_desc_type::ref, {MKLDNNPlugin::impl_desc_type::ref_any}}));
231
232 class MKLDNNGraphDynBatchBatchNormTests: public MKLDNNGraphBatchNormTests {
233 protected:
234
235     virtual void SetUp() {
236         try {
237             TestsCommon::SetUp();
238             batchnorm4D_test_params p = ::testing::WithParamInterface<batchnorm4D_test_params>::GetParam();
239             std::string model = getModel(p);
240             size_t MB = p.in.n;
241             if (MB < 2)
242                 MB = 2;
243
244             InferenceEngine::CNNNetReader net_reader;
245             ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
246
247             InferenceEngine::TBlob<uint8_t> *weights = new InferenceEngine::TBlob<uint8_t>(InferenceEngine::Precision::U8, InferenceEngine::C, {p.in.c * 4 * sizeof(float)});
248             weights->allocate();
249             fill_data( weights->data().as<float*>(), weights->size() / sizeof(float));
250             float * data = weights->buffer();
251             for (size_t i = 0; i < weights->size() / sizeof(float); i++) {
252                 if (data[i] < 0) {
253                     data[i] *= -1;
254                 }
255             }
256             InferenceEngine::TBlob<uint8_t>::Ptr weights_ptr = InferenceEngine::TBlob<uint8_t>::Ptr(weights);
257             net_reader.SetWeights(weights_ptr);
258             InferenceEngine::CNNNetwork network = net_reader.getNetwork();
259             auto implNet = dynamic_cast<InferenceEngine::details::CNNNetworkImpl *>(&((InferenceEngine::ICNNNetwork&)network));
260             ASSERT_NE(nullptr, implNet) << "Failed to cast ICNNNetwork to CNNNetworkImpl";
261             InferenceEngine::ResponseDesc resp;
262             InferenceEngine::StatusCode sts  = implNet->setBatchSizeReshape(MB, &resp);
263             ASSERT_EQ((int)InferenceEngine::StatusCode::OK, sts) << resp.msg;
264
265             MKLDNNGraphTestClass graph;
266             graph.setProperty({{InferenceEngine::PluginConfigParams::KEY_DYN_BATCH_ENABLED, InferenceEngine::PluginConfigParams::YES}});
267             graph.CreateGraph(net_reader.getNetwork());
268
269             InferenceEngine::SizeVector dims_src = {MB, p.in.c, p.in.h, p.in.w};
270             InferenceEngine::Blob::Ptr src = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src);
271             InferenceEngine::TBlob<float>* srcPtr = dynamic_cast<InferenceEngine::TBlob<float>*>(src.get());
272             if (srcPtr == nullptr)
273                 FAIL() << "Cannot cast blob to TBlob<float>.";
274
275             src->allocate();
276             fill_data(src->buffer(), src->size());
277
278             InferenceEngine::BlobMap srcs;
279             srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src));
280
281             InferenceEngine::OutputsDataMap out;
282             out = net_reader.getNetwork().getOutputsInfo();
283             InferenceEngine::BlobMap outputBlobs;
284
285             std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
286
287             InferenceEngine::TBlob<float>::Ptr output;
288             output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
289             output->allocate();
290             outputBlobs[item.first] = output;
291
292             auto checkScaleShift = [](const MKLDNNPlugin::MKLDNNNodePtr& node) {
293                 return node->getType() == MKLDNNPlugin::BatchNormalization;
294             };
295
296             graph.checkDynBatch(srcs, outputBlobs, MB, MB, checkScaleShift);
297             graph.checkDynBatch(srcs, outputBlobs, 1, MB, checkScaleShift);
298         } catch (const InferenceEngine::details::InferenceEngineException &e) {
299             FAIL() << e.what();
300         }
301     }
302 };
303
304 TEST_P(MKLDNNGraphDynBatchBatchNormTests, TestsDynBatchBatchNorm) {}
305
306 INSTANTIATE_TEST_CASE_P(
307         TestsDynBatchBatchNorm, MKLDNNGraphDynBatchBatchNormTests,
308         ::testing::Values(
309                 batchnorm4D_test_params{{1, 32, 128, 256}, 1e-6, 5, MKLDNNPlugin::impl_desc_type::jit},
310                 batchnorm4D_test_params{{3, 3, 128, 256}, 1e-6, 5, MKLDNNPlugin::impl_desc_type::jit},
311                 batchnorm4D_test_params{{1, 32, 128, 256}, 1e-6, 5, MKLDNNPlugin::impl_desc_type::ref, {MKLDNNPlugin::impl_desc_type::ref_any}},
312                 batchnorm4D_test_params{{3, 3, 128, 256}, 1e-6, 5, MKLDNNPlugin::impl_desc_type::ref, {MKLDNNPlugin::impl_desc_type::ref_any}}));