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"
10 #include "test_graph.hpp"
12 #include <mkldnn_plugin/mkldnn_extension_utils.h>
13 #include <mkldnn_plugin/mkldnn_plugin.h>
14 #include <mkldnn_plugin/config.h>
17 using namespace mkldnn;
19 class MKLDNNTestExecNetwork: public MKLDNNPlugin::MKLDNNExecNetwork {
21 MKLDNNTestExecNetwork(InferenceEngine::ICNNNetwork &network, const MKLDNNPlugin::Config &cfg)
22 : MKLDNNExecNetwork(network, cfg, {}) {}
23 MKLDNNPlugin::MKLDNNGraph& getGraph() {
28 class MKLDNNTestEngine: public MKLDNNPlugin::Engine {
30 MKLDNNPlugin::MKLDNNGraph& getGraph() {
31 auto * execNetworkInt =
32 dynamic_cast<InferenceEngine::ExecutableNetworkBase<InferenceEngine::ExecutableNetworkInternal> *>(_loadedNetwork.get());
34 THROW_IE_EXCEPTION << "Cannot find loaded network!";
36 auto * network = reinterpret_cast<MKLDNNTestExecNetwork *>(execNetworkInt->getImpl().get());
38 THROW_IE_EXCEPTION << "Cannot get mkldnn graph!";
39 return network->getGraph();
43 class MKLDNNGraphLeaksTests: public ::testing::Test {
45 void addOutputToEachNode(InferenceEngine::CNNNetReader& net_reader, std::vector<std::string>& new_outputs,
46 InferenceEngine::CNNLayerPtr cnnLayer) {
47 auto outputs = net_reader.getNetwork().getOutputsInfo();
48 if (outputs.find(cnnLayer->name) != outputs.end())
51 net_reader.getNetwork().addOutput(cnnLayer->name);
52 new_outputs.push_back(cnnLayer->name);
54 for (const auto &layer : cnnLayer->outData) {
55 for (const auto &data : layer->getInputTo()) {
56 addOutputToEachNode(net_reader, new_outputs, data.second);
61 void fill_data(float *data, size_t size, size_t duty_ratio = 10) {
62 for (size_t i = 0; i < size; i++) {
63 if ( ( i / duty_ratio)%2 == 1) {
66 data[i] = (float) sin((float)i);
72 TEST_F(MKLDNNGraphLeaksTests, MKLDNN_not_release_outputs_fp32) {
74 InferenceEngine::CNNNetReader net_reader;
75 std::string model = "<net name=\"LeNet\" version=\"2\" batch=\"1\">\n"
77 " <layer name=\"data\" type=\"Input\" precision=\"FP32\" id=\"0\">\n"
87 " <layer name=\"conv1\" type=\"Convolution\" precision=\"FP32\" id=\"1\">\n"
88 " <convolution_data stride-x=\"1\" stride-y=\"1\" pad-x=\"0\" pad-y=\"0\" kernel-x=\"5\" kernel-y=\"5\" output=\"20\" group=\"1\"/>\n"
105 " <weights offset=\"0\" size=\"2000\"/>\n"
106 " <biases offset=\"2000\" size=\"80\"/>\n"
108 " <layer name=\"pool1\" type=\"Pooling\" precision=\"FP32\" id=\"2\">\n"
109 " <pooling_data kernel-x=\"2\" kernel-y=\"2\" pad-x=\"0\" pad-y=\"0\" stride-x=\"2\" stride-y=\"2\" rounding-type=\"ceil\" pool-method=\"max\"/>\n"
127 " <layer name=\"conv2\" type=\"Convolution\" precision=\"FP32\" id=\"3\">\n"
128 " <convolution_data stride-x=\"1\" stride-y=\"1\" pad-x=\"0\" pad-y=\"0\" kernel-x=\"5\" kernel-y=\"5\" output=\"50\" group=\"1\"/>\n"
145 " <weights offset=\"2080\" size=\"100000\"/>\n"
146 " <biases offset=\"102080\" size=\"200\"/>\n"
148 " <layer name=\"pool2\" type=\"Pooling\" precision=\"FP32\" id=\"4\">\n"
149 " <pooling_data kernel-x=\"2\" kernel-y=\"2\" pad-x=\"0\" pad-y=\"0\" stride-x=\"2\" stride-y=\"2\" rounding-type=\"ceil\" pool-method=\"max\"/>\n"
167 " <layer name=\"ip1\" type=\"FullyConnected\" precision=\"FP32\" id=\"5\">\n"
168 " <fc_data out-size=\"500\"/>\n"
178 " <port id=\"10\">\n"
183 " <weights offset=\"102280\" size=\"1600000\"/>\n"
184 " <biases offset=\"1702280\" size=\"2000\"/>\n"
186 " <layer name=\"relu1\" type=\"ReLU\" precision=\"FP32\" id=\"6\">\n"
188 " <port id=\"11\">\n"
194 " <port id=\"12\">\n"
200 " <layer name=\"ip2\" type=\"FullyConnected\" precision=\"FP32\" id=\"7\">\n"
201 " <fc_data out-size=\"10\"/>\n"
203 " <port id=\"13\">\n"
209 " <port id=\"14\">\n"
214 " <weights offset=\"1704280\" size=\"20000\"/>\n"
215 " <biases offset=\"1724280\" size=\"40\"/>\n"
217 " <layer name=\"prob\" type=\"SoftMax\" precision=\"FP32\" id=\"8\">\n"
219 " <port id=\"15\">\n"
225 " <port id=\"16\">\n"
233 " <edge from-layer=\"0\" from-port=\"0\" to-layer=\"1\" to-port=\"1\"/>\n"
234 " <edge from-layer=\"1\" from-port=\"2\" to-layer=\"2\" to-port=\"3\"/>\n"
235 " <edge from-layer=\"2\" from-port=\"4\" to-layer=\"3\" to-port=\"5\"/>\n"
236 " <edge from-layer=\"3\" from-port=\"6\" to-layer=\"4\" to-port=\"7\"/>\n"
237 " <edge from-layer=\"4\" from-port=\"8\" to-layer=\"5\" to-port=\"9\"/>\n"
238 " <edge from-layer=\"5\" from-port=\"10\" to-layer=\"6\" to-port=\"11\"/>\n"
239 " <edge from-layer=\"6\" from-port=\"12\" to-layer=\"7\" to-port=\"13\"/>\n"
240 " <edge from-layer=\"7\" from-port=\"14\" to-layer=\"8\" to-port=\"15\"/>\n"
244 size_t weights_size = 1724320;
245 net_reader.ReadNetwork(model.c_str(), model.size());
247 InferenceEngine::TBlob<uint8_t> *weights = new InferenceEngine::TBlob<uint8_t>(InferenceEngine::Precision::U8, InferenceEngine::C, {weights_size});
249 fill_data((float *) weights->buffer(), weights->size() / sizeof(float));
250 InferenceEngine::TBlob<uint8_t>::Ptr weights_ptr = InferenceEngine::TBlob<uint8_t>::Ptr(weights);
252 net_reader.SetWeights(weights_ptr);
254 auto outputs = net_reader.getNetwork().getOutputsInfo();
255 std::vector<std::string> new_outputs;
257 for (auto input : net_reader.getNetwork().getInputsInfo()) {
258 for (const auto &layer : input.second->getInputData()->getInputTo()) {
259 addOutputToEachNode(net_reader, new_outputs, layer.second);
263 ASSERT_NE(1, net_reader.getNetwork().getOutputsInfo().size());
265 std::shared_ptr<MKLDNNTestEngine> score_engine(new MKLDNNTestEngine());
266 ASSERT_NO_THROW(score_engine->LoadNetwork(net_reader.getNetwork()));
268 size_t modified_outputs_size = score_engine->getGraph().GetOutputNodes().size();
270 InferenceEngine::CNNNetReader net_reader2;
271 net_reader2.ReadNetwork(model.c_str(), model.size());
272 net_reader2.SetWeights(weights_ptr);
273 ASSERT_EQ(1, net_reader2.getNetwork().getOutputsInfo().size());
275 ASSERT_NO_THROW(score_engine->LoadNetwork(net_reader2.getNetwork()));
277 size_t original_outputs_size = score_engine->getGraph().GetOutputNodes().size();
279 ASSERT_NE(modified_outputs_size, original_outputs_size);
280 ASSERT_EQ(1, original_outputs_size);