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