1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
5 #include <gtest/gtest.h>
6 #include <gmock/gmock-spec-builders.h>
7 #include "mkldnn_plugin/mkldnn_graph.h"
9 #include "test_graph.hpp"
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"
17 using namespace ::testing;
19 using namespace mkldnn;
22 struct power_test_params {
36 MKLDNNPlugin::impl_desc_type selectedType;
38 std::vector<std::function<void(MKLDNNPlugin::PrimitiveDescInfo)>> comp;
41 template <typename data_t>
42 void ref_power(const InferenceEngine::TBlob<data_t> &src, InferenceEngine::TBlob<data_t> &dst, power_test_params prm) {
43 const data_t *src_data = src.readOnly();
44 data_t *dst_data = dst.data();
46 for (int i=0; i < src.size(); i++)
47 dst_data[i] = pow(src_data[i]*prm.scale + prm.shift, prm.power);
50 class MKLDNNGraphPowerTests: public TestsCommon,
51 public WithParamInterface<power_test_params> {
52 std::string model_t = R"V0G0N(
53 <Net Name="Power_Only" version="2" precision="FP32" batch="1">
55 <layer name="in1" type="Input" precision="FP32" id="0">
65 <layer name="power" id="1" type="Power" precision="FP32">
66 <power_data power="_POWER_" scale="_SCALE_" shift="_SHIFT_"/>
86 <edge from-layer="0" from-port="0" to-layer="1" to-port="1"/>
92 std::string getModel(power_test_params p) {
93 std::string model = model_t;
95 REPLACE_WITH_NUM(model, "_IW_", p.in.w);
96 REPLACE_WITH_NUM(model, "_IH_", p.in.h);
97 REPLACE_WITH_NUM(model, "_IC_", p.in.c);
98 REPLACE_WITH_NUM(model, "_IN_", p.in.n);
99 REPLACE_WITH_NUM(model, "_POWER_", p.power);
100 REPLACE_WITH_NUM(model, "_SCALE_", p.scale);
101 REPLACE_WITH_NUM(model, "_SHIFT_", p.shift);
106 virtual void TearDown() {
109 virtual void SetUp() {
111 TestsCommon::SetUp();
112 power_test_params p = ::testing::WithParamInterface<power_test_params>::GetParam();
113 std::string model = getModel(p);
115 InferenceEngine::CNNNetReader net_reader;
116 ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
118 MKLDNNGraphTestClass graph;
119 graph.CreateGraph(net_reader.getNetwork());
120 auto& nodes = graph.getNodes();
121 for (int i = 0; i < nodes.size(); i++) {
122 if (nodes[i]->getType() == MKLDNNPlugin::Power) {
123 ASSERT_EQ(p.num_prim_desc, nodes[i]->getSupportedPrimitiveDescriptors().size());
124 for (size_t j = 0; j < p.num_prim_desc && j < p.comp.size(); j++) {
125 p.comp.at(j)(nodes[i]->getSupportedPrimitiveDescriptors().at(j));
127 ASSERT_NE(nullptr, nodes[i]->getSelectedPrimitiveDescriptor());
128 ASSERT_EQ(p.selectedType, nodes[i]->getSelectedPrimitiveDescriptor()->getImplementationType());
132 InferenceEngine::SizeVector dims_src = {p.in.n, p.in.c, p.in.h, p.in.w};
134 InferenceEngine::Blob::Ptr src = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src);
136 fill_data(src->buffer(), src->size());
138 InferenceEngine::TBlob<float>* srcPtr = dynamic_cast<InferenceEngine::TBlob<float>*>(src.get());
140 if (srcPtr == nullptr)
141 FAIL() << "Cannot cast blob to TBlob<float>.";
143 InferenceEngine::BlobMap srcs;
144 srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src));
146 InferenceEngine::OutputsDataMap out;
147 out = net_reader.getNetwork().getOutputsInfo();
148 InferenceEngine::BlobMap outputBlobs;
150 std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
152 InferenceEngine::TBlob<float>::Ptr output;
153 output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
155 outputBlobs[item.first] = output;
157 graph.Infer(srcs, outputBlobs);
159 InferenceEngine::TBlob<float> dst_ref(item.second->getTensorDesc());
162 ref_power(*srcPtr, dst_ref, p);
164 compare(*output, dst_ref);
165 } catch (const InferenceEngine::details::InferenceEngineException &e) {
171 TEST_P(MKLDNNGraphPowerTests, TestsPower) {}
174 INSTANTIATE_TEST_CASE_P(
175 TestsPower, MKLDNNGraphPowerTests,
178 {1, 3, 13, 13}, 1, 2, 0.5f, 3, MKLDNNPlugin::impl_desc_type::unknown, {
179 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
180 ASSERT_EQ(MKLDNNPlugin::impl_desc_type::unknown, impl.getImplementationType());
181 ASSERT_EQ(1, impl.getConfig().inConfs.size());
182 ASSERT_EQ(1, impl.getConfig().outConfs.size());
183 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(0).desc.getLayout());
184 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().outConfs.at(0).desc.getLayout());
186 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
187 ASSERT_EQ(MKLDNNPlugin::impl_desc_type::unknown, impl.getImplementationType());
188 ASSERT_EQ(1, impl.getConfig().inConfs.size());
189 ASSERT_EQ(1, impl.getConfig().outConfs.size());
190 ASSERT_EQ(InferenceEngine::Layout::BLOCKED, impl.getConfig().inConfs.at(0).desc.getLayout());
191 ASSERT_EQ(InferenceEngine::Layout::BLOCKED, impl.getConfig().outConfs.at(0).desc.getLayout());
193 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
194 ASSERT_EQ(MKLDNNPlugin::impl_desc_type::unknown, impl.getImplementationType());
195 ASSERT_EQ(1, impl.getConfig().inConfs.size());
196 ASSERT_EQ(1, impl.getConfig().outConfs.size());
197 ASSERT_EQ(InferenceEngine::Layout::BLOCKED, impl.getConfig().inConfs.at(0).desc.getLayout());
198 ASSERT_EQ(InferenceEngine::Layout::BLOCKED, impl.getConfig().outConfs.at(0).desc.getLayout());
200 power_test_params{{1, 1, 23, 23}, 3, 8, 2, 3 },
201 power_test_params{{1, 8, 23, 23}, 8, 2, 1, 3 }
204 class MKLDNNGraphDynBatchPowerTests: public MKLDNNGraphPowerTests {
205 std::string model_t = R"V0G0N(
206 <Net Name="Power_Only" version="2" precision="FP32" batch="1">
208 <layer name="in1" type="Input" precision="FP32" id="0">
218 <layer name="power" id="1" type="Power" precision="FP32">
219 <power_data power="_POWER_" scale="_SCALE_" shift="_SHIFT_"/>
239 <edge from-layer="0" from-port="0" to-layer="1" to-port="1"/>
244 std::string getModel(power_test_params p) {
245 std::string model = model_t;
247 REPLACE_WITH_NUM(model, "_IW_", p.in.w);
248 REPLACE_WITH_NUM(model, "_IH_", p.in.h);
249 REPLACE_WITH_NUM(model, "_IC_", p.in.c);
250 REPLACE_WITH_NUM(model, "_IN_", p.in.n);
251 REPLACE_WITH_NUM(model, "_POWER_", p.power);
252 REPLACE_WITH_NUM(model, "_SCALE_", p.scale);
253 REPLACE_WITH_NUM(model, "_SHIFT_", p.shift);
259 virtual void TearDown() {
262 virtual void SetUp() {
264 TestsCommon::SetUp();
265 power_test_params p = ::testing::WithParamInterface<power_test_params>::GetParam();
266 std::string model = getModel(p);
271 InferenceEngine::CNNNetReader net_reader;
272 ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
273 InferenceEngine::CNNNetwork network = net_reader.getNetwork();
274 auto implNet = dynamic_cast<InferenceEngine::details::CNNNetworkImpl *>(&((InferenceEngine::ICNNNetwork&)network));
275 ASSERT_NE(nullptr, implNet) << "Failed to cast ICNNNetwork to CNNNetworkImpl";
276 InferenceEngine::ResponseDesc resp;
277 InferenceEngine::StatusCode sts = implNet->setBatchSizeReshape(MB, &resp);
278 ASSERT_EQ((int)InferenceEngine::StatusCode::OK, sts) << resp.msg;
280 MKLDNNGraphTestClass graph;
281 graph.setProperty({{InferenceEngine::PluginConfigParams::KEY_DYN_BATCH_ENABLED, InferenceEngine::PluginConfigParams::YES}});
282 graph.CreateGraph(net_reader.getNetwork());
284 InferenceEngine::SizeVector dims_src = {MB, p.in.c, p.in.h, p.in.w};
286 InferenceEngine::Blob::Ptr src = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src);
288 fill_data(src->buffer(), src->size());
290 InferenceEngine::TBlob<float>* srcPtr = dynamic_cast<InferenceEngine::TBlob<float>*>(src.get());
292 if (srcPtr == nullptr)
293 FAIL() << "Cannot cast blob to TBlob<float>.";
295 InferenceEngine::BlobMap srcs;
296 srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src));
298 InferenceEngine::OutputsDataMap out;
299 out = net_reader.getNetwork().getOutputsInfo();
300 InferenceEngine::BlobMap outputBlobs;
302 std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
304 InferenceEngine::TBlob<float>::Ptr output;
305 output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
307 outputBlobs[item.first] = output;
309 auto checkPower = [](const MKLDNNPlugin::MKLDNNNodePtr& node) {
310 return node->getType() == MKLDNNPlugin::Power;
312 graph.checkDynBatch(srcs, outputBlobs, MB, MB, checkPower);
313 graph.checkDynBatch(srcs, outputBlobs, 1, MB, checkPower);
314 } catch (const InferenceEngine::details::InferenceEngineException &e) {
320 TEST_P(MKLDNNGraphDynBatchPowerTests, TestsDynBatchPower) {}
322 INSTANTIATE_TEST_CASE_P(
323 TestsDynBatchPower, MKLDNNGraphDynBatchPowerTests,
326 {1, 3, 13, 13}, 1, 2, 0.5f, 3, MKLDNNPlugin::impl_desc_type::unknown, {
327 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
328 ASSERT_EQ(MKLDNNPlugin::impl_desc_type::unknown, impl.getImplementationType());
329 ASSERT_EQ(1, impl.getConfig().inConfs.size());
330 ASSERT_EQ(1, impl.getConfig().outConfs.size());
331 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(0).desc.getLayout());
332 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().outConfs.at(0).desc.getLayout());
334 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
335 ASSERT_EQ(MKLDNNPlugin::impl_desc_type::unknown, impl.getImplementationType());
336 ASSERT_EQ(1, impl.getConfig().inConfs.size());
337 ASSERT_EQ(1, impl.getConfig().outConfs.size());
338 ASSERT_EQ(InferenceEngine::Layout::BLOCKED, impl.getConfig().inConfs.at(0).desc.getLayout());
339 ASSERT_EQ(InferenceEngine::Layout::BLOCKED, impl.getConfig().outConfs.at(0).desc.getLayout());
341 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
342 ASSERT_EQ(MKLDNNPlugin::impl_desc_type::unknown, impl.getImplementationType());
343 ASSERT_EQ(1, impl.getConfig().inConfs.size());
344 ASSERT_EQ(1, impl.getConfig().outConfs.size());
345 ASSERT_EQ(InferenceEngine::Layout::BLOCKED, impl.getConfig().inConfs.at(0).desc.getLayout());
346 ASSERT_EQ(InferenceEngine::Layout::BLOCKED, impl.getConfig().outConfs.at(0).desc.getLayout());
348 power_test_params{{1, 1, 23, 23}, 3, 8, 2, 3 },
349 power_test_params{{1, 8, 23, 23}, 8, 2, 1, 3 }