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 eltwise_test_params {
31 Sum = 0, Prod = 1, Max = 2
40 MKLDNNPlugin::impl_desc_type selectedType;
42 std::vector<std::function<void(MKLDNNPlugin::PrimitiveDescInfo)>> comp;
45 template<typename data_t>
46 void ref_eltwise(const std::vector<InferenceEngine::TBlob<data_t>> &src, InferenceEngine::TBlob<data_t> &dst, eltwise_test_params prm) {
47 std::vector<float> scales;
48 if (prm.scales != "") {
49 std::istringstream stream(prm.scales);
51 while (getline(stream, str, ',')) {
52 float val = std::stof(str);
53 scales.push_back(val);
56 for (int i = 0; i < src.size(); i++) {
57 scales.push_back(1.0f);
61 data_t *dst_data = dst.data();
63 const data_t *src_data = src[0].readOnly();
65 for (int i = 0; i < src[0].size(); i++) {
67 case eltwise_test_params::Sum: {
68 dst_data[i] = scales[0]*src_data[i];
72 dst_data[i] = src_data[i];
77 for (int n = 1; n < src.size(); n++) {
78 src_data = src[n].readOnly();
80 for (int i = 0; i < src[n].size(); i++) {
82 case eltwise_test_params::Sum: {
83 dst_data[i] += scales[n]*src_data[i];
87 case eltwise_test_params::Prod: {
88 dst_data[i] *= src_data[i];
92 case eltwise_test_params::Max: {
93 dst_data[i] = (std::max)(dst_data[i], src_data[i]);
101 class MKLDNNGraphEltwiseTests: public TestsCommon,
102 public WithParamInterface<eltwise_test_params> {
103 std::string model_t = R"V0G0N(
104 <net name="EltwiseOnly" version="2" precision="FP32" batch="1">
106 <layer name="in1" type="Input" precision="FP32" id="1">
116 <layer name="in2" type="Input" precision="FP32" id="2">
126 <layer name="in3" type="Input" precision="FP32" id="3">
136 <layer name="con" id="4" type="Eltwise" precision="FP32">
137 <elementwise_data operation="_OP_" coeff="_COEFF_"/>
169 <edge from-layer="1" from-port="1" to-layer="4" to-port="1"/>
170 <edge from-layer="2" from-port="2" to-layer="4" to-port="2"/>
171 <edge from-layer="3" from-port="3" to-layer="4" to-port="3"/>
177 std::string getModel(eltwise_test_params p) {
178 std::string model = model_t;
183 } else if (p.op == 1) {
185 } else if (p.op == 2) {
189 REPLACE_WITH_NUM(model, "_IW_", p.in.w);
190 REPLACE_WITH_NUM(model, "_IH_", p.in.h);
191 REPLACE_WITH_NUM(model, "_IC_", p.in.c);
192 REPLACE_WITH_NUM(model, "_IN_", p.in.n);
193 REPLACE_WITH_STR(model, "_OP_", op);
194 REPLACE_WITH_STR(model, "_COEFF_", p.scales);
198 virtual void TearDown() {
201 virtual void SetUp() {
203 TestsCommon::SetUp();
204 eltwise_test_params p = ::testing::WithParamInterface<eltwise_test_params>::GetParam();
205 std::string model = getModel(p);
207 InferenceEngine::CNNNetReader net_reader;
208 ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
210 MKLDNNGraphTestClass graph;
211 graph.CreateGraph(net_reader.getNetwork());
213 auto& nodes = graph.getNodes();
214 for (int i = 0; i < nodes.size(); i++) {
215 if (nodes[i]->getType() == MKLDNNPlugin::Eltwise) {
216 ASSERT_EQ(p.num_prim_desc, nodes[i]->getSupportedPrimitiveDescriptors().size());
217 for (size_t j = 0; j < p.num_prim_desc && j < p.comp.size(); j++) {
218 p.comp.at(j)(nodes[i]->getSupportedPrimitiveDescriptors().at(j));
220 ASSERT_NE(nullptr, nodes[i]->getSelectedPrimitiveDescriptor());
221 ASSERT_EQ(p.selectedType, nodes[i]->getSelectedPrimitiveDescriptor()->getImplementationType());
225 InferenceEngine::SizeVector dims_src = {p.in.n, p.in.c, p.in.h, p.in.w};
227 InferenceEngine::Blob::Ptr src1 = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src);
230 InferenceEngine::TBlob<float>* srcPtr1 = dynamic_cast<InferenceEngine::TBlob<float>*>(src1.get());
232 if (srcPtr1 == nullptr)
233 FAIL() << "Cannot cast blob to TBlob<float>.";
235 fill_data(src1->buffer(), src1->size());
236 InferenceEngine::Blob::Ptr src2 = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src);
239 InferenceEngine::TBlob<float>* srcPtr2 = dynamic_cast<InferenceEngine::TBlob<float>*>(src2.get());
241 if (srcPtr2 == nullptr)
242 FAIL() << "Cannot cast blob to TBlob<float>.";
243 fill_data(src2->buffer(), src2->size());
244 InferenceEngine::Blob::Ptr src3 = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src);
247 InferenceEngine::TBlob<float>* srcPtr3 = dynamic_cast<InferenceEngine::TBlob<float>*>(src3.get());
249 if (srcPtr3 == nullptr)
250 FAIL() << "Cannot cast blob to TBlob<float>.";
251 fill_data(src3->buffer(), src3->size());
252 InferenceEngine::BlobMap srcs;
253 srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src1));
254 srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in2", src2));
255 srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in3", src3));
257 InferenceEngine::OutputsDataMap out;
258 out = net_reader.getNetwork().getOutputsInfo();
259 InferenceEngine::BlobMap outputBlobs;
261 std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
263 InferenceEngine::TBlob<float>::Ptr output;
264 output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
266 outputBlobs[item.first] = output;
268 graph.Infer(srcs, outputBlobs);
270 InferenceEngine::TBlob<float> dst_ref(item.second->getTensorDesc());
273 std::vector<InferenceEngine::TBlob<float>> src_vec = {*srcPtr1, *srcPtr2, *srcPtr3};
275 ref_eltwise(src_vec, dst_ref, p);
277 compare(*output, dst_ref);
278 } catch (const InferenceEngine::details::InferenceEngineException &e) {
284 TEST_P(MKLDNNGraphEltwiseTests, TestsEltwise) {}
287 INSTANTIATE_TEST_CASE_P(
288 TestsEltwise, MKLDNNGraphEltwiseTests,
290 eltwise_test_params{{1, 3, 3, 3}, eltwise_test_params::opType::Sum, "", 3, MKLDNNPlugin::impl_desc_type::ref, {
291 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
292 ASSERT_EQ(MKLDNNPlugin::impl_desc_type::ref, impl.getImplementationType());
293 ASSERT_EQ(3, impl.getConfig().inConfs.size());
294 ASSERT_EQ(1, impl.getConfig().outConfs.size());
295 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(0).desc.getLayout());
296 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(1).desc.getLayout());
297 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().outConfs.at(0).desc.getLayout());
300 eltwise_test_params{{1, 3, 3, 3}, eltwise_test_params::opType::Sum, "1.0,1.0,1.0", 3, MKLDNNPlugin::impl_desc_type::ref, {
301 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
302 ASSERT_EQ(MKLDNNPlugin::impl_desc_type::ref, impl.getImplementationType());
303 ASSERT_EQ(3, impl.getConfig().inConfs.size());
304 ASSERT_EQ(1, impl.getConfig().outConfs.size());
305 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(0).desc.getLayout());
306 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(1).desc.getLayout());
307 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().outConfs.at(0).desc.getLayout());
310 eltwise_test_params{{1, 3, 3, 3}, eltwise_test_params::opType::Sum, "1.5,0.5,-2.0", 3, MKLDNNPlugin::impl_desc_type::ref, {
311 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
312 ASSERT_EQ(MKLDNNPlugin::impl_desc_type::ref, impl.getImplementationType());
313 ASSERT_EQ(3, impl.getConfig().inConfs.size());
314 ASSERT_EQ(1, impl.getConfig().outConfs.size());
315 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(0).desc.getLayout());
316 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(1).desc.getLayout());
317 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(2).desc.getLayout());
318 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().outConfs.at(0).desc.getLayout());
321 eltwise_test_params{{1, 3, 3, 3}, eltwise_test_params::opType::Prod, "", 3, MKLDNNPlugin::impl_desc_type::ref, {
322 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
323 ASSERT_EQ(MKLDNNPlugin::impl_desc_type::ref, impl.getImplementationType());
324 ASSERT_EQ(3, impl.getConfig().inConfs.size());
325 ASSERT_EQ(1, impl.getConfig().outConfs.size());
326 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(0).desc.getLayout());
327 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(1).desc.getLayout());
328 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(2).desc.getLayout());
329 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().outConfs.at(0).desc.getLayout());
332 eltwise_test_params{{1, 3, 3, 3}, eltwise_test_params::opType::Max, "", 3, MKLDNNPlugin::impl_desc_type::ref, {
333 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
334 ASSERT_EQ(MKLDNNPlugin::impl_desc_type::ref, impl.getImplementationType());
335 ASSERT_EQ(3, impl.getConfig().inConfs.size());
336 ASSERT_EQ(1, impl.getConfig().outConfs.size());
337 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(0).desc.getLayout());
338 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(1).desc.getLayout());
339 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(2).desc.getLayout());
340 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().outConfs.at(0).desc.getLayout());
345 class MKLDNNGraphDynBatchEltwiseTests: public MKLDNNGraphEltwiseTests {
347 virtual void SetUp() {
349 TestsCommon::SetUp();
350 eltwise_test_params p = ::testing::WithParamInterface<eltwise_test_params>::GetParam();
351 std::string model = getModel(p);
356 InferenceEngine::CNNNetReader net_reader;
357 ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
358 InferenceEngine::CNNNetwork network = net_reader.getNetwork();
359 auto implNet = dynamic_cast<InferenceEngine::details::CNNNetworkImpl *>(&((InferenceEngine::ICNNNetwork&)network));
360 ASSERT_NE(nullptr, implNet) << "Failed to cast ICNNNetwork to CNNNetworkImpl";
361 InferenceEngine::ResponseDesc resp;
362 InferenceEngine::StatusCode sts = implNet->setBatchSizeReshape(MB, &resp);
363 ASSERT_EQ((int)InferenceEngine::StatusCode::OK, sts) << resp.msg;
365 MKLDNNGraphTestClass graph;
366 graph.setProperty({{InferenceEngine::PluginConfigParams::KEY_DYN_BATCH_ENABLED, InferenceEngine::PluginConfigParams::YES}});
367 graph.CreateGraph(net_reader.getNetwork());
369 InferenceEngine::SizeVector dims_src = {MB, p.in.c, p.in.h, p.in.w};
371 InferenceEngine::Blob::Ptr src1 = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src);
374 InferenceEngine::TBlob<float>* srcPtr1 = dynamic_cast<InferenceEngine::TBlob<float>*>(src1.get());
376 if (srcPtr1 == nullptr)
377 FAIL() << "Cannot cast blob to TBlob<float>.";
379 fill_data(src1->buffer(), src1->size());
380 InferenceEngine::Blob::Ptr src2 = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src);
383 InferenceEngine::TBlob<float>* srcPtr2 = dynamic_cast<InferenceEngine::TBlob<float>*>(src2.get());
385 if (srcPtr2 == nullptr)
386 FAIL() << "Cannot cast blob to TBlob<float>.";
387 fill_data(src2->buffer(), src2->size());
388 InferenceEngine::Blob::Ptr src3 = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src);
391 InferenceEngine::TBlob<float>* srcPtr3 = dynamic_cast<InferenceEngine::TBlob<float>*>(src3.get());
393 if (srcPtr3 == nullptr)
394 FAIL() << "Cannot cast blob to TBlob<float>.";
395 fill_data(src3->buffer(), src3->size());
396 InferenceEngine::BlobMap srcs;
397 srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src1));
398 srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in2", src2));
399 srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in3", src3));
401 InferenceEngine::OutputsDataMap out;
402 out = net_reader.getNetwork().getOutputsInfo();
403 InferenceEngine::BlobMap outputBlobs;
405 std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
407 InferenceEngine::TBlob<float>::Ptr output;
408 output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
410 outputBlobs[item.first] = output;
413 auto checkDepthwise = [](const MKLDNNPlugin::MKLDNNNodePtr& node) {
414 return node->getType() == MKLDNNPlugin::Eltwise;
417 graph.checkDynBatch(srcs, outputBlobs, MB, MB, checkDepthwise);
418 graph.checkDynBatch(srcs, outputBlobs, 1, MB, checkDepthwise);
419 } catch (const InferenceEngine::details::InferenceEngineException &e) {
425 TEST_P(MKLDNNGraphDynBatchEltwiseTests, TestsDynBatchEltwise) {}
427 INSTANTIATE_TEST_CASE_P(
428 TestsDynBatchEltwise, MKLDNNGraphDynBatchEltwiseTests,
430 eltwise_test_params{{1, 3, 3, 3}, eltwise_test_params::opType::Sum, "", 3, MKLDNNPlugin::impl_desc_type::ref},
431 eltwise_test_params{{1, 3, 3, 3}, eltwise_test_params::opType::Sum, "1.0,1.0,1.0", 3, MKLDNNPlugin::impl_desc_type::ref},
432 eltwise_test_params{{1, 3, 3, 3}, eltwise_test_params::opType::Sum, "1.5,0.5,-2.0", 3, MKLDNNPlugin::impl_desc_type::ref},
433 eltwise_test_params{{1, 3, 3, 3}, eltwise_test_params::opType::Prod, "", 3, MKLDNNPlugin::impl_desc_type::ref},
434 eltwise_test_params{{1, 3, 3, 3}, eltwise_test_params::opType::Max, "", 3, MKLDNNPlugin::impl_desc_type::ref}));