Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / engines / mkldnn / dumper_test.cpp
1 //
2 // Copyright 2016-2018 Intel Corporation.
3 //
4 // This software and the related documents are Intel copyrighted materials,
5 // and your use of them is governed by the express license under which they
6 // were provided to you (End User License Agreement for the Intel(R) Software
7 // Development Products (Version May 2017)). Unless the License provides
8 // otherwise, you may not use, modify, copy, publish, distribute, disclose or
9 // transmit this software or the related documents without Intel's prior
10 // written permission.
11 //
12 // This software and the related documents are provided as is, with no
13 // express or implied warranties, other than those that are expressly
14 // stated in the License.
15 //
16
17 #include <gtest/gtest.h>
18
19 #include "mkldnn_graph.h"
20 #include "mkldnn_graph_dumper.h"
21 #include "ie_blob.h"
22 #include "ie_util_internal.hpp"
23 #include "details/ie_cnn_network_tools.h"
24 #include "xml_net_builder.hpp"
25 #include "graph_tools.hpp"
26
27 #include <string>
28 #include <map>
29
30 using namespace InferenceEngine;
31 using namespace MKLDNNPlugin;
32 using std::string;
33 using std::map;
34
35 class NetGen : testing::V2NetBuilder {
36     string model;
37     TBlob<uint8_t>::Ptr weights;
38
39 public:
40     NetGen(): testing::V2NetBuilder(buildNetworkWithOneInput(
41             "SomeNet", {2,3,16,16}, "FP32")) {
42         using prm_t = map<string, string>;
43
44         testing::InOutShapes inout = {{{2,3,16,16}},{{2,16,16,16}}};
45
46         prm_t conv_prm = {
47                 {"stride-x", std::to_string(1)},
48                 {"stride-y", std::to_string(1)},
49                 {"pad-x",    std::to_string(1)},
50                 {"pad-y",    std::to_string(1)},
51                 {"kernel-x", std::to_string(3)},
52                 {"kernel-y", std::to_string(3)},
53                 {"output",   std::to_string(16)},
54                 {"group",    std::to_string(1)}
55         };
56         size_t wght = 3*16*3*3*sizeof(float);
57         size_t bias = 16*sizeof(float);
58
59         prm_t relu_prm = {{"negative_slope", std::to_string(0)}};
60
61         addLayer("Convolution", "FP32", &conv_prm, {{{2,3,16,16}},{{2,16,16,16}}}, wght, bias);
62         addLayer("Relu", "FP32", &relu_prm, {{{2,16,16,16}},{{2,16,16,16}}});
63
64         model = finish();
65
66         weights.reset(new TBlob<uint8_t>({Precision::U8, {wght+bias}, C}));
67         weights->allocate();
68     }
69
70     CNNNetwork net() {
71         CNNNetReader net_reader;
72         net_reader.ReadNetwork(model.data(), model.length());
73         net_reader.SetWeights(weights);
74
75         return net_reader.getNetwork();
76     }
77 };
78
79 TEST(MKLDNNLayersTests, DumpSimpleGraph) {
80     auto net = NetGen().net();
81     MKLDNNGraph graph;
82     MKLDNNExtensionManager::Ptr extMgr;
83     graph.CreateGraph(net, extMgr);
84
85     auto dump_net = dump_graph_as_ie_net(graph);
86     auto layers = details::CNNNetSortTopologically(*dump_net);
87
88     ASSERT_EQ(layers.size(), 4);
89     ASSERT_EQ(layers[0]->type, "Input");
90     ASSERT_EQ(layers[1]->type, "Conv_Activ");
91     ASSERT_EQ(layers[2]->type, "Reorder");
92     ASSERT_EQ(layers[3]->type, "Output");
93 }
94
95 TEST(MKLDNNLayersTests, DumpSimpleGraphToDot) {
96     auto net = NetGen().net();
97     MKLDNNGraph graph;
98     MKLDNNExtensionManager::Ptr extMgr;
99     graph.CreateGraph(net, extMgr);
100
101     std::stringstream buff;
102     dump_graph_as_dot(graph, buff);
103
104     std::string dot = buff.str();
105     std::cout << dot;
106     ASSERT_EQ(std::count(dot.begin(), dot.end(), '{'), 1); // 1-graph
107     ASSERT_EQ(std::count(dot.begin(), dot.end(), '}'), 1);
108     ASSERT_EQ(std::count(dot.begin(), dot.end(), '['), 10); // 4-node 3-data 3-shape
109     ASSERT_EQ(std::count(dot.begin(), dot.end(), ']'), 10);
110     ASSERT_EQ(std::count(dot.begin(), dot.end(), '>'), 6); // connection
111 }