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 <mkldnn_plugin/mkldnn_extension_utils.h>
14 #include "tests_common.hpp"
17 using namespace ::testing;
19 using namespace mkldnn;
22 struct input_test_params {
25 MKLDNNPlugin::impl_desc_type selectedType;
27 std::vector<std::function<void(MKLDNNPlugin::PrimitiveDescInfo)>> comp;
30 class MKLDNNGraphInputTests: public TestsCommon,
31 public WithParamInterface<input_test_params> {
32 std::string model_t = R"V0G0N(
33 <net name="InputsOnly" version="2" precision="FP32" batch="1">
35 <layer name="in1" type="Input" precision="FP32" id="1">
45 <layer name="in2" type="Input" precision="FP32" id="2">
55 <layer name="in3" type="Input" precision="FP32" id="3">
63 <layer name="power1" id="4" type="Power" precision="FP32">
64 <power_data power="1" scale="1" shift="1"/>
82 <layer name="power2" id="5" type="Power" precision="FP32">
83 <power_data power="1" scale="1" shift="1"/>
101 <layer name="power3" id="6" type="Power" precision="FP32">
102 <power_data power="1" scale="1" shift="1"/>
118 <edge from-layer="1" from-port="1" to-layer="4" to-port="4"/>
119 <edge from-layer="2" from-port="2" to-layer="5" to-port="6"/>
120 <edge from-layer="3" from-port="3" to-layer="6" to-port="8"/>
125 std::string getModel(input_test_params p) {
130 virtual void TearDown() {
133 virtual void SetUp() {
135 TestsCommon::SetUp();
136 input_test_params p = ::testing::WithParamInterface<input_test_params>::GetParam();
137 std::string model = getModel(p);
139 InferenceEngine::CNNNetReader net_reader;
140 ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
142 MKLDNNGraphTestClass graph;
143 graph.CreateGraph(net_reader.getNetwork());
145 auto& nodes = graph.getNodes();
146 for (int i = 0; i < nodes.size(); i++) {
147 if (nodes[i]->getType() == MKLDNNPlugin::Input || nodes[i]->getType() == MKLDNNPlugin::Output) {
148 ASSERT_EQ(p.num_prim_desc, nodes[i]->getSupportedPrimitiveDescriptors().size());
149 size_t count = (nodes[i]->getType() == MKLDNNPlugin::Input) ? 0 : 2;
150 if (nodes[i]->getName() == "in3") {
153 if (nodes[i]->getName() == "out_power3") {
156 for (size_t j = 0; j < p.num_prim_desc && j < p.comp.size(); j++) {
157 p.comp.at(count)(nodes[i]->getSupportedPrimitiveDescriptors().at(j));
159 ASSERT_NE(nullptr, nodes[i]->getSelectedPrimitiveDescriptor());
160 ASSERT_EQ(p.selectedType, nodes[i]->getSelectedPrimitiveDescriptor()->getImplementationType());
163 } catch (const InferenceEngine::details::InferenceEngineException &e) {
169 TEST_P(MKLDNNGraphInputTests, TestsInput) {}
172 INSTANTIATE_TEST_CASE_P(
173 TestsInput, MKLDNNGraphInputTests,
175 input_test_params{1, MKLDNNPlugin::impl_desc_type::unknown, {
176 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
177 ASSERT_EQ(MKLDNNPlugin::impl_desc_type::unknown, impl.getImplementationType());
178 ASSERT_EQ(0, impl.getConfig().inConfs.size());
179 ASSERT_EQ(1, impl.getConfig().outConfs.size());
180 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().outConfs.at(0).desc.getLayout());
182 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
183 ASSERT_EQ(MKLDNNPlugin::impl_desc_type::unknown, impl.getImplementationType());
184 ASSERT_EQ(0, impl.getConfig().inConfs.size());
185 ASSERT_EQ(1, impl.getConfig().outConfs.size());
186 ASSERT_EQ(InferenceEngine::Layout::NC, impl.getConfig().outConfs.at(0).desc.getLayout());
188 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
189 ASSERT_EQ(MKLDNNPlugin::impl_desc_type::unknown, impl.getImplementationType());
190 ASSERT_EQ(1, impl.getConfig().inConfs.size());
191 ASSERT_EQ(0, impl.getConfig().outConfs.size());
192 ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(0).desc.getLayout());
194 [](MKLDNNPlugin::PrimitiveDescInfo impl) {
195 ASSERT_EQ(MKLDNNPlugin::impl_desc_type::unknown, impl.getImplementationType());
196 ASSERT_EQ(1, impl.getConfig().inConfs.size());
197 ASSERT_EQ(0, impl.getConfig().outConfs.size());
198 ASSERT_EQ(InferenceEngine::Layout::NC, impl.getConfig().inConfs.at(0).desc.getLayout());
203 class MKLDNNGraphConstInputTests: public TestsCommon {
204 std::string model_t = R"V0G0N(
205 <net name="ConcatOnly" version="2" precision="FP32" batch="1">
207 <layer name="in1" type="Input" precision="FP32" id="1">
217 <custom offset="0" size="48"/>
220 <layer name="in2" type="Const" precision="FP32" id="2">
230 <custom offset="48" size="24"/>
233 <layer name="con" id="3" type="Concat" precision="FP32">
234 <concat_data axis="2"/>
260 <edge from-layer="1" from-port="1" to-layer="3" to-port="1"/>
261 <edge from-layer="2" from-port="2" to-layer="3" to-port="2"/>
267 virtual void TearDown() {
270 virtual void SetUp() {
272 TestsCommon::SetUp();
273 std::string model = model_t;
275 InferenceEngine::CNNNetReader net_reader;
276 ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
278 InferenceEngine::TBlob<uint8_t> *weights = new InferenceEngine::TBlob<uint8_t>(InferenceEngine::Precision::U8, InferenceEngine::C, {72});
280 float * data = weights->buffer();
282 std::cout << weights->size() << std::endl;
284 InferenceEngine::SizeVector dims_src1 = {1, 3, 2, 2};
285 InferenceEngine::SizeVector dims_src2 = {1, 3, 1, 2};
286 InferenceEngine::Blob::Ptr src1 = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src1);
288 float *srcData = src1->buffer();
289 for (size_t i = 0; i < 12; i++, data++, srcData++) {
294 InferenceEngine::Blob::Ptr src2 = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src2);
296 srcData = src2->buffer();
297 for (size_t i = 0; i < 6; i++, data++, srcData++) {
301 InferenceEngine::TBlob<uint8_t>::Ptr weights_ptr = InferenceEngine::TBlob<uint8_t>::Ptr(weights);
303 net_reader.SetWeights(weights_ptr);
305 MKLDNNGraphTestClass graph;
306 graph.CreateGraph(net_reader.getNetwork());
307 auto& nodes = graph.getNodes();
308 ASSERT_LE(3, nodes.size());
310 InferenceEngine::BlobMap srcs;
312 InferenceEngine::OutputsDataMap out;
313 out = net_reader.getNetwork().getOutputsInfo();
314 InferenceEngine::BlobMap outputBlobs;
316 std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
318 InferenceEngine::TBlob<float>::Ptr output;
319 output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
321 outputBlobs[item.first] = output;
323 graph.Infer(srcs, outputBlobs);
326 float *src1_ptr = src1->buffer();
327 size_t src1_size = src1->size();
328 float *src2_ptr = src2->buffer();
329 size_t src2_size = src2->size();
330 float *dst_ptr = output->buffer();
331 size_t dst_size = output->size();
333 int len1 = 1, len2 = 1, cycles;
334 for (int dim = 2; dim < output->dims().size(); dim++) {
335 len1 *= src1->dims()[dim];
336 len2 *= src2->dims()[dim];
340 int index1 = 0, index2 = 0, index = 0;
341 for (int cycle = 0; cycle < cycles; cycle ++) {
342 for (int i1 = 0; i1 < len1; i1++) {
343 if (src1_ptr[index1] != dst_ptr[index])
345 FAIL() << "index: " << index << " src: " << src1_ptr[index1] << ", dst: " << dst_ptr[index];
349 for (int i2 = 0; i2 < len2; i2++) {
350 if (src2_ptr[index2] != dst_ptr[index])
352 FAIL() << "index: " << index << " src: " << src2_ptr[index2] << ", dst: " << dst_ptr[index];
357 } catch (const InferenceEngine::details::InferenceEngineException &e) {
363 TEST_F(MKLDNNGraphConstInputTests, TestsConstInput) {}