Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / hetero_plugin / fallback_policy.cpp
1 //
2 // Copyright (C) 2018-2019 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 "fallback_policy.h"
18 #include "hetero_device_loader.h"
19 #include "details/ie_cnn_network_iterator.hpp"
20 #include "ie_layers.h"
21 #include "ie_util_internal.hpp"
22 #include <fstream>
23 #include <vector>
24 #include <memory>
25
26 using namespace InferenceEngine;
27
28 void dla_layer_colorer(const CNNLayerPtr layer,
29                        ordered_properties &printed_properties,
30                        ordered_properties &node_properties) {
31     printed_properties.insert(printed_properties.begin(),
32                               std::pair<std::string, std::string>("device", layer->affinity));
33     if (layer->affinity == "CPU") {
34         node_properties.emplace_back("fillcolor", "#5A5DF0");
35     } else if (layer->affinity == "FPGA") {
36         node_properties.emplace_back("fillcolor", "#20F608");
37     } else if (layer->affinity == "GPU") {
38         node_properties.emplace_back("fillcolor", "#F1F290");
39     } else {
40         node_properties.emplace_back("fillcolor", "#11F110");
41     }
42 }
43
44
45 FallbackPolicy::FallbackPolicy(std::map<std::string, InferenceEngine::IHeteroDeviceLoader::Ptr> &deviceLoaders,
46                                bool dumpDotFile) :
47     _deviceLoaders(deviceLoaders),
48     _dumpDotFile(dumpDotFile) {
49 }
50
51 void FallbackPolicy::init(const std::string &config, const std::map<std::string, std::string> &allConfigs,
52                           const std::vector<InferenceEngine::IExtensionPtr> &extensions) {
53     if (config.empty()) {
54         THROW_IE_EXCEPTION << "Cannot set affinity according to fallback policy because the order of devices was not initialized";
55     }
56     // parsing the string and splitting to tokens
57     std::string::size_type i = 0;
58     std::string::size_type idelimeter;
59     while ((idelimeter = config.find(',', i)) != std::string::npos) {
60         _fallbackDevices.push_back(config.substr(i, idelimeter - i));
61         i = idelimeter + 1;
62     }
63     _fallbackDevices.push_back(config.substr(i, config.length() - i));
64
65     for (auto d : _fallbackDevices) {
66         if (_deviceLoaders.find(d) == _deviceLoaders.end()) {
67             IHeteroDeviceLoader::Ptr loader;
68             loader = std::make_shared<HeteroDeviceLoader>(d);
69             HeteroDeviceLoader *pdl = dynamic_cast<HeteroDeviceLoader *>(loader.get());
70             pdl->initConfigs(allConfigs, extensions);
71             _deviceLoaders[d] = loader;
72         }
73     }
74 }
75
76 void FallbackPolicy::setAffinity(const std::map<std::string, std::string>& config, ICNNNetwork& network) {
77     std::map<std::string, QueryNetworkResult> queryResults;
78     // go oger devices, create appropriate plugins and
79     for (const auto &i : _fallbackDevices) {
80         QueryNetworkResult r;
81         _deviceLoaders[i]->QueryNetwork(i, network, config, r);
82         queryResults[i] = r;
83     }
84
85     details::CNNNetworkIterator i(const_cast<ICNNNetwork *>(&network));
86     while (i != details::CNNNetworkIterator()) {
87         CNNLayer::Ptr layer = *i;
88         for (auto &&j : _fallbackDevices) {
89             auto &qr = queryResults[j];
90             if (qr.supportedLayers.find(layer->name) != qr.supportedLayers.end()) {
91                 layer->affinity = j;
92                 break;
93             }
94         }
95         i++;
96     }
97
98     if (_dumpDotFile) {
99         std::stringstream stream(std::stringstream::out);
100         stream << "hetero_affinity_" << network.getName() << ".dot";
101
102         std::ofstream file(stream.str().c_str());
103         saveGraphToDot(network, file, dla_layer_colorer);
104     }
105 }