1 // Copyright (C) 2018 Intel Corporation
3 // SPDX-License-Identifier: Apache-2.0
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"
11 #include "test_graph.hpp"
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"
18 using namespace ::testing;
20 using namespace mkldnn;
22 struct batchnorm4D_test_params {
34 MKLDNNPlugin::impl_desc_type selectedType;
35 std::vector<MKLDNNPlugin::impl_desc_type> preferTypes;
37 std::vector<std::function<void(MKLDNNPlugin::PrimitiveDescInfo)>> comp;
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];
48 const double eps = prm.epsilon;
50 const data_t *src_data = src.readOnly();
51 data_t *dst_data = dst.data();
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;
59 sqrt_variance = 1. / sqrt(v_variance + eps);
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
67 dst_data[idx] = (src_data[idx] - v_mean) * sqrt_variance;
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">
77 <layer name="in1" type="Input" precision="FP32" id="0">
87 <layer name="batchNorm" id="1" type="BatchNormalization" precision="FP32">
88 <batch_norm_data epsilon="_EPSILON_" PrimitivesPriority="_IMPLS_"/>
90 <weights offset="0" size="_S1_" />
91 <biases offset="_S1_" size="_S1_" />
112 <edge from-layer="0" from-port="0" to-layer="1" to-port="1"/>
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);
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);
130 size_t w_data_size = p.in.c * sizeof(float);
131 REPLACE_WITH_NUM(model, "_S1_", w_data_size);
134 for (const auto& preferType : p.preferTypes) {
137 impls += "cpu:" + MKLDNNGraphTestClass::getStrPrimitiveDescriptorType(preferType);
139 REPLACE_WITH_STR(model, "_IMPLS_", impls);
143 virtual void TearDown() {
146 virtual void SetUp() {
148 TestsCommon::SetUp();
149 batchnorm4D_test_params p = ::testing::WithParamInterface<batchnorm4D_test_params>::GetParam();
150 std::string model = getModel(p);
152 InferenceEngine::CNNNetReader net_reader;
153 ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
155 InferenceEngine::TBlob<uint8_t> *weights = new InferenceEngine::TBlob<uint8_t>(InferenceEngine::Precision::FP32, InferenceEngine::C, {p.in.c * 2 * sizeof(float)});
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++) {
165 InferenceEngine::TBlob<uint8_t>::Ptr weights_ptr = InferenceEngine::TBlob<uint8_t>::Ptr(weights);
167 net_reader.SetWeights(weights_ptr);
169 MKLDNNGraphTestClass graph;
170 graph.CreateGraph(net_reader.getNetwork());
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));
179 ASSERT_NE(nullptr, nodes[i]->getSelectedPrimitiveDescriptor());
180 ASSERT_TRUE(nodes[i]->getSelectedPrimitiveDescriptor()->getImplementationType() | p.selectedType);
183 ASSERT_GE(5, nodes.size());
185 InferenceEngine::SizeVector dims_src = {p.in.n, p.in.c, p.in.h, p.in.w};
187 InferenceEngine::Blob::Ptr src = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src);
189 fill_data(src->buffer(), src->size());
191 InferenceEngine::TBlob<float>* srcPtr = dynamic_cast<InferenceEngine::TBlob<float>*>(src.get());
193 if (srcPtr == nullptr)
194 FAIL() << "Cannot cast blob to TBlob<float>.";
196 InferenceEngine::BlobMap srcs;
197 srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src));
199 InferenceEngine::OutputsDataMap out;
200 out = net_reader.getNetwork().getOutputsInfo();
201 InferenceEngine::BlobMap outputBlobs;
203 std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
205 InferenceEngine::TBlob<float>::Ptr output;
206 output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
208 outputBlobs[item.first] = output;
210 graph.Infer(srcs, outputBlobs);
212 InferenceEngine::TBlob<float> dst_ref(item.second->getTensorDesc());
215 ref_batchnorm4D(*srcPtr, (const float*) weights->buffer(), ((const float*) weights->buffer() + p.in.c), dst_ref, p);
217 compare(*output, dst_ref);
218 } catch (const InferenceEngine::details::InferenceEngineException &e) {
224 TEST_P(MKLDNNGraphBatchNormTests, TestsBatchNorm) {}
227 INSTANTIATE_TEST_CASE_P(
228 TestsBatchNorm, MKLDNNGraphBatchNormTests,
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}}));
235 class MKLDNNGraphDynBatchBatchNormTests: public MKLDNNGraphBatchNormTests {
238 virtual void SetUp() {
240 TestsCommon::SetUp();
241 batchnorm4D_test_params p = ::testing::WithParamInterface<batchnorm4D_test_params>::GetParam();
242 std::string model = getModel(p);
247 InferenceEngine::CNNNetReader net_reader;
248 ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
250 InferenceEngine::TBlob<uint8_t> *weights = new InferenceEngine::TBlob<uint8_t>(InferenceEngine::Precision::U8, InferenceEngine::C, {p.in.c * 4 * sizeof(float)});
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++) {
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;
268 MKLDNNGraphTestClass graph;
269 graph.setProperty({{InferenceEngine::PluginConfigParams::KEY_DYN_BATCH_ENABLED, InferenceEngine::PluginConfigParams::YES}});
270 graph.CreateGraph(net_reader.getNetwork());
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>.";
279 fill_data(src->buffer(), src->size());
281 InferenceEngine::BlobMap srcs;
282 srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src));
284 InferenceEngine::OutputsDataMap out;
285 out = net_reader.getNetwork().getOutputsInfo();
286 InferenceEngine::BlobMap outputBlobs;
288 std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
290 InferenceEngine::TBlob<float>::Ptr output;
291 output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
293 outputBlobs[item.first] = output;
295 auto checkScaleShift = [](const MKLDNNPlugin::MKLDNNNodePtr& node) {
296 return node->getType() == MKLDNNPlugin::BatchNormalization;
299 graph.checkDynBatch(srcs, outputBlobs, MB, MB, checkScaleShift);
300 graph.checkDynBatch(srcs, outputBlobs, 1, MB, checkScaleShift);
301 } catch (const InferenceEngine::details::InferenceEngineException &e) {
307 TEST_P(MKLDNNGraphDynBatchBatchNormTests, TestsDynBatchBatchNorm) {}
309 INSTANTIATE_TEST_CASE_P(
310 TestsDynBatchBatchNorm, MKLDNNGraphDynBatchBatchNormTests,
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}}));