Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / hetero_plugin / hetero_plugin.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 "hetero_plugin.h"
18 #include <memory>
19 #include <vector>
20 #include "ie_plugin_config.hpp"
21 #include "hetero/hetero_plugin_config.hpp"
22 #include <cpp_interfaces/base/ie_plugin_base.hpp>
23 #include "hetero_plugin_base.hpp"
24 #include "inference_engine.hpp"
25 #include "hetero_executable_network.h"
26 #include "fallback_policy.h"
27
28 using namespace InferenceEngine;
29 using namespace InferenceEngine::PluginConfigParams;
30 using namespace InferenceEngine::HeteroConfigParams;
31 using namespace HeteroPlugin;
32 using namespace std;
33
34 static Version heteroPluginDescription = {
35         {1, 6},  // plugin API version
36         CI_BUILD_NUMBER,
37         "dliaPlugin"  // plugin description message -
38 };
39
40 void Engine::GetVersion(const Version *&versionInfo)noexcept {
41     versionInfo = &heteroPluginDescription;
42 }
43
44
45 Engine::Engine() {
46     _config[InferenceEngine::PluginConfigParams::KEY_EXCLUSIVE_ASYNC_REQUESTS] = "YES";
47     _config[KEY_HETERO_DUMP_GRAPH_DOT] = NO;
48 }
49
50 InferenceEngine::ExecutableNetworkInternal::Ptr Engine::LoadExeNetworkImpl(InferenceEngine::ICNNNetwork &network,
51                                                                            const std::map<std::string, std::string> &config) {
52     // TODO(amalyshe) do we need here verification of input precisions?
53     std::map<std::string, std::string> tconfig;
54     tconfig = config;
55
56     // we must not override the parameter, but need to copy everything from plugin config
57     for (auto c : _config) {
58         if (tconfig.find(c.first) == tconfig.end()) {
59             tconfig[c.first] = c.second;
60         }
61     }
62
63     return std::make_shared<HeteroExecutableNetwork>(network, tconfig, _extensions, _deviceLoaders, error_listener);
64 }
65
66 void Engine::SetConfig(const std::map<std::string, std::string> &config) {
67     if (_config.find("TARGET_FALLBACK") == _config.end()) {
68         _config["TARGET_FALLBACK"] = "";
69     }
70
71     for (auto &&i : config) {
72         _config[i.first] = i.second;
73     }
74 }
75
76 void Engine::SetDeviceLoader(const std::string &device,
77                              IHeteroDeviceLoader::Ptr pLoader) {
78     _deviceLoaders[device] = pLoader;
79 }
80
81 void Engine::AddExtension(InferenceEngine::IExtensionPtr extension) {
82     _extensions.push_back(extension);
83 }
84
85 void Engine::SetAffinity(InferenceEngine::ICNNNetwork &network,
86                          const std::map<std::string, std::string> &config) {
87     // TODO(amalyshe) config is not used here, talk with RAN why it appeared in initial interface
88     FallbackPolicy fbPolicy(_deviceLoaders, _config[KEY_HETERO_DUMP_GRAPH_DOT]== YES);
89     fbPolicy.init(_config["TARGET_FALLBACK"], config, _extensions);
90     fbPolicy.setAffinity(config, network);
91 }
92
93
94 INFERENCE_PLUGIN_API(StatusCode) CreatePluginEngine(
95         IInferencePlugin *&plugin,
96         ResponseDesc *resp) noexcept {
97     try {
98         plugin = new HeteroPluginBase<Engine>(
99                 {{1, 6}, "heteroPlugin", "heteroPlugin"},
100                 std::make_shared<Engine>());
101         return OK;
102     }
103     catch (std::exception &ex) {
104         return DescriptionBuffer(GENERAL_ERROR, resp) << ex.what();
105     }
106 }
107
108 void Engine::SetLogCallback(IErrorListener &listener) {
109     error_listener = &listener;
110     for (auto& device_loader : _deviceLoaders)
111         device_loader.second->SetLogCallback(*error_listener);
112 }