2 // Copyright (C) 2018-2019 Intel Corporation.
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.
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.
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"
26 using namespace InferenceEngine;
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");
40 node_properties.emplace_back("fillcolor", "#11F110");
45 FallbackPolicy::FallbackPolicy(std::map<std::string, InferenceEngine::IHeteroDeviceLoader::Ptr> &deviceLoaders,
47 _deviceLoaders(deviceLoaders),
48 _dumpDotFile(dumpDotFile) {
51 void FallbackPolicy::init(const std::string &config, const std::map<std::string, std::string> &allConfigs,
52 const std::vector<InferenceEngine::IExtensionPtr> &extensions) {
54 THROW_IE_EXCEPTION << "Cannot set affinity according to fallback policy because the order of devices was not initialized";
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));
63 _fallbackDevices.push_back(config.substr(i, config.length() - i));
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;
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) {
81 _deviceLoaders[i]->QueryNetwork(i, network, config, r);
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()) {
99 std::stringstream stream(std::stringstream::out);
100 stream << "hetero_affinity_" << network.getName() << ".dot";
102 std::ofstream file(stream.str().c_str());
103 saveGraphToDot(network, file, dla_layer_colorer);