ie_plugin_internal.hpp
Go to the documentation of this file.
1 // Copyright (C) 2018-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief Inference Engine plugin API wrapper, to be used by particular implementors
7  * @file ie_plugin_internal.hpp
8  */
9 
10 #pragma once
11 
12 #include <ie_plugin_config.hpp>
13 
14 #include <map>
15 #include <memory>
16 #include <string>
17 #include <limits>
18 
20 #include "cpp_interfaces/impl/ie_executable_network_internal.hpp"
22 
23 using namespace InferenceEngine;
24 using namespace InferenceEngine::details;
25 
26 namespace InferenceEngine {
27 
28 namespace {
29 
30 /**
31  * @private
32  */
33 static inline void parsePluginName(std::istream& networkModel) {
34  ExportMagic magic = {};
35  auto currentPos = networkModel.tellg();
36  networkModel.read(magic.data(), magic.size());
37  auto exportedWithName = (exportMagic == magic);
38  if (exportedWithName) {
39  networkModel.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
40  } else {
41  networkModel.seekg(currentPos, networkModel.beg);
42  }
43 }
44 
45 } // namespace
46 
47 /**
48  * @brief Optimal implementation of IInferencePlugin interface to avoid duplication in all plugins
49  * @ingroup ie_dev_api_plugin_api
50  */
52 protected:
53  /**
54  * @brief Destroys the object.
55  */
56  ~InferencePluginInternal() override = default;
57 
58 public:
60  const std::map<std::string, std::string>& config) override {
61  return LoadNetworkImplPrivate(network, config);
62  }
63 
64  ExecutableNetwork LoadNetwork(const ICNNNetwork& network, const std::map<std::string, std::string>& config,
65  RemoteContext::Ptr context) override {
66  return LoadNetworkImplPrivate(network, config, context);;
67  }
68 
69  IExecutableNetwork::Ptr ImportNetwork(const std::string& modelFileName,
70  const std::map<std::string, std::string>& config) override {
71  (void)modelFileName;
72  (void)config;
74  }
75 
76  ExecutableNetwork ImportNetwork(std::istream& networkModel,
77  const std::map<std::string, std::string>& config) override {
78  parsePluginName(networkModel);
79  return ImportNetworkImpl(networkModel, config);
80  }
81 
82  ExecutableNetwork ImportNetwork(std::istream& networkModel,
83  const RemoteContext::Ptr& context,
84  const std::map<std::string, std::string>& config) override {
85  parsePluginName(networkModel);
86  return ImportNetworkImpl(networkModel, context, config);
87  }
88 
89  void SetConfig(const std::map<std::string, std::string>& config) override {
90  (void)config;
92  }
93 
94  void SetCore(ICore* core) noexcept override {
95  assert(nullptr != core);
96  _core = core;
97  }
98 
99  ICore* GetCore() const noexcept override {
100  return _core;
101  }
102 
103  void AddExtension(InferenceEngine::IExtensionPtr /*extension*/) override {
105  }
106 
107  void QueryNetwork(const ICNNNetwork& /*network*/, const std::map<std::string, std::string>& /*config*/,
108  QueryNetworkResult& /*res*/) const override {
110  }
111 
112  void SetName(const std::string& pluginName) noexcept override {
113  _pluginName = pluginName;
114  }
115 
116  std::string GetName() const noexcept override {
117  return _pluginName;
118  }
119 
120  Parameter GetConfig(const std::string& /*name*/,
121  const std::map<std::string, Parameter>& /*options*/) const override {
123  }
124 
125  Parameter GetMetric(const std::string& /*name*/,
126  const std::map<std::string, Parameter>& /*options*/) const override {
128  }
129 
130  RemoteContext::Ptr CreateContext(const ParamMap& /*params*/) override {
132  }
133 
136  }
137 
138 private:
139  /**
140  * @brief A helper method which clones a ICNNNetwork object, keeps InputsDataMap and OutputsDataMap data maps,
141  * and creates an IExecutableNetwork object
142  * @param network An input ICNNNetwork object used to create an executable network object
143  * @param config A map of string -> string configuration options.
144  * @param context An optional pointer to RemoteContext
145  * @return An output executable network object
146  */
147  ExecutableNetwork LoadNetworkImplPrivate(const ICNNNetwork& network,
148  const std::map<std::string, std::string>& config,
149  RemoteContext::Ptr context = nullptr) {
150  InputsDataMap networkInputs, networkInputsCloned;
151  OutputsDataMap networkOutputs, networkOutputsCloned;
152  network.getInputsInfo(networkInputs);
153  network.getOutputsInfo(networkOutputs);
154  copyInputOutputInfo(networkInputs, networkOutputs, networkInputsCloned, networkOutputsCloned);
155 
157  if (nullptr == context) {
158  impl = LoadExeNetworkImpl(network, config);
159  } else {
160  impl = LoadExeNetworkImpl(network, context, config);
161  }
162 
163  impl->setNetworkInputs(networkInputsCloned);
164  impl->setNetworkOutputs(networkOutputsCloned);
165  impl->SetPointerToPlugin(shared_from_this());
166 
167  auto executableNetwork = make_executable_network(impl);
168  return ExecutableNetwork(executableNetwork);
169  }
170 
171 protected:
172  /**
173  * @brief Creates an executable network from a parsed network object, users can create as many networks as they need
174  * and use them simultaneously (up to the limitation of the HW resources)
175  * @note The function is used in
176  * InferencePluginInternal::LoadNetwork(const ICNNNetwork&, const std::map<std::string, std::string>&)
177  * which performs common steps first and calls this plugin-dependent method implementation after.
178  * @param network A network object
179  * @param config string-string map of config parameters relevant only for this load operation
180  * @return Shared pointer to the ExecutableNetwork object
181  */
183  const std::map<std::string, std::string>& config) = 0;
184 
185  /**
186  * @brief Creates an executable network using remote context from a parsed network object,
187  * users can create as many networks as they need and use them simultaneously (up to the limitation of the HW resources)
188  * @note The function is used in
189  * InferencePluginInternal::LoadNetwork(const ICNNNetwork&, const std::map<std::string, std::string>&, RemoteContext::Ptr)
190  * which performs common steps first and calls this plugin-dependent method implementation after.
191  * @param network A network object
192  * @param context A remote context
193  * @param config string-string map of config parameters relevant only for this load operation
194  * @return Shared pointer to the ExecutableNetwork object
195  */
197  RemoteContext::Ptr context,
198  const std::map<std::string, std::string>& config) {
199  (void)network;
200  (void)context;
201  (void)config;
203  }
204 
205  /**
206  * @brief Creates an executable network from an previously exported network
207  * @note The function is called from
208  * IInferencePlugin::ImportNetwork(std::istream&, const RemoteContext::Ptr&, const std::map<std::string, std::string>&)
209  * performs common steps first and calls this plugin-dependent implementation after.
210  * @param networkModel Reference to network model output stream
211  * @param config A string -> string map of parameters
212  * @return An Executable network
213  */
214  virtual ExecutableNetwork ImportNetworkImpl(std::istream& networkModel,
215  const std::map<std::string, std::string>& config) {
216  (void)networkModel;
217  (void)config;
219  }
220 
221  /**
222  * @brief Imports network wit RemoteContext
223  * @param networkModel Reference to network model output stream
224  * @param context - a pointer to plugin context derived from RemoteContext class used to
225  * execute the network
226  * @param config A string -> string map of parameters
227  * @return An Executable network
228  */
229  virtual ExecutableNetwork ImportNetworkImpl(std::istream& networkModel,
230  const RemoteContext::Ptr& context,
231  const std::map<std::string, std::string>& config) {
233  }
234 
235  std::string _pluginName; //!< A device name that plugins enables
236  std::map<std::string, std::string> _config; //!< A map config keys -> values
237  ICore* _core = nullptr; //!< A pointer to ICore interface
238 };
239 
240 } // namespace InferenceEngine
InferenceEngine
Inference Engine Plugin API namespace.
InferenceEngine::InferencePluginInternal::LoadNetwork
ExecutableNetwork LoadNetwork(const ICNNNetwork &network, const std::map< std::string, std::string > &config) override
Creates an executable network from an pares network object, users can create as many networks as they...
Definition: ie_plugin_internal.hpp:59
InferenceEngine::InferencePluginInternal
Optimal implementation of IInferencePlugin interface to avoid duplication in all plugins.
Definition: ie_plugin_internal.hpp:51
InferenceEngine::InferencePluginInternal::QueryNetwork
void QueryNetwork(const ICNNNetwork &, const std::map< std::string, std::string > &, QueryNetworkResult &) const override
Queries a plugin about supported layers in network.
Definition: ie_plugin_internal.hpp:107
InferenceEngine::ICNNNetwork::getOutputsInfo
virtual void getOutputsInfo(OutputsDataMap &out) const noexcept=0
ie_iplugin_internal.hpp
Inference Engine plugin API wrapper, to be used by particular implementors.
InferenceEngine::InferencePluginInternal::SetName
void SetName(const std::string &pluginName) noexcept override
Sets a name for a plugin.
Definition: ie_plugin_internal.hpp:112
InferenceEngine::InferencePluginInternal::_config
std::map< std::string, std::string > _config
A map config keys -> values.
Definition: ie_plugin_internal.hpp:236
InferenceEngine::InferencePluginInternal::SetConfig
void SetConfig(const std::map< std::string, std::string > &config) override
Sets configuration for plugin, acceptable keys can be found in ie_plugin_config.hpp.
Definition: ie_plugin_internal.hpp:89
InferenceEngine::Parameter
InferenceEngine::InferencePluginInternal::LoadNetwork
ExecutableNetwork LoadNetwork(const ICNNNetwork &network, const std::map< std::string, std::string > &config, RemoteContext::Ptr context) override
Creates an executable network from network object, on specified remote context.
Definition: ie_plugin_internal.hpp:64
InferenceEngine::InferencePluginInternal::ImportNetworkImpl
virtual ExecutableNetwork ImportNetworkImpl(std::istream &networkModel, const std::map< std::string, std::string > &config)
Creates an executable network from an previously exported network.
Definition: ie_plugin_internal.hpp:214
InferenceEngine::IExecutableNetwork::Ptr
std::shared_ptr< IExecutableNetwork > Ptr
InferenceEngine::InferencePluginInternal::LoadExeNetworkImpl
virtual ExecutableNetworkInternal::Ptr LoadExeNetworkImpl(const ICNNNetwork &network, const std::map< std::string, std::string > &config)=0
Creates an executable network from a parsed network object, users can create as many networks as they...
InferenceEngine::ICNNNetwork
InferenceEngine::copyInputOutputInfo
static void copyInputOutputInfo(const InputsDataMap &networkInputs, const OutputsDataMap &networkOutputs, InputsDataMap &_networkInputs, OutputsDataMap &_networkOutputs)
Copies InputInfo and output Data.
Definition: ie_iplugin_internal.hpp:57
NOT_IMPLEMENTED_str
#define NOT_IMPLEMENTED_str
Defines the not implemented message.
Definition: exception2status.hpp:132
InferenceEngine::InferencePluginInternal::ImportNetworkImpl
virtual ExecutableNetwork ImportNetworkImpl(std::istream &networkModel, const RemoteContext::Ptr &context, const std::map< std::string, std::string > &config)
Imports network wit RemoteContext.
Definition: ie_plugin_internal.hpp:229
InferenceEngine::InferencePluginInternal::ImportNetwork
IExecutableNetwork::Ptr ImportNetwork(const std::string &modelFileName, const std::map< std::string, std::string > &config) override
Creates an executable network from an previously exported network.
Definition: ie_plugin_internal.hpp:69
InferenceEngine::InferencePluginInternal::AddExtension
void AddExtension(InferenceEngine::IExtensionPtr) override
Registers extension within plugin.
Definition: ie_plugin_internal.hpp:103
InferenceEngine::details
A namespace with non-public Inference Engine Plugin API.
Definition: caseless.hpp:19
InferenceEngine::InferencePluginInternal::GetName
std::string GetName() const noexcept override
Provides a name of a plugin.
Definition: ie_plugin_internal.hpp:116
InferenceEngine::InferencePluginInternal::LoadExeNetworkImpl
virtual ExecutableNetworkInternal::Ptr LoadExeNetworkImpl(const ICNNNetwork &network, RemoteContext::Ptr context, const std::map< std::string, std::string > &config)
Creates an executable network using remote context from a parsed network object, users can create as ...
Definition: ie_plugin_internal.hpp:196
InferenceEngine::ExecutableNetworkInternal::Ptr
std::shared_ptr< ExecutableNetworkInternal > Ptr
A shared pointer to ExecutableNetworkInternal object.
Definition: ie_executable_network_internal.hpp:31
ParamMap
std::map< std::string, Parameter > ParamMap
InferenceEngine::ExportMagic
std::array< char, 4 > ExportMagic
Type of magic value.
Definition: ie_icore.hpp:113
InferenceEngine::RemoteContext::Ptr
std::shared_ptr< RemoteContext > Ptr
InferenceEngine::ExecutableNetwork
ie_plugin_config.hpp
InferenceEngine::InferencePluginInternal::ImportNetwork
ExecutableNetwork ImportNetwork(std::istream &networkModel, const std::map< std::string, std::string > &config) override
Creates an executable network from an previously exported network using plugin implementation and rem...
Definition: ie_plugin_internal.hpp:76
THROW_IE_EXCEPTION
#define THROW_IE_EXCEPTION
InferenceEngine::ICNNNetwork::getInputsInfo
virtual void getInputsInfo(InputsDataMap &inputs) const noexcept=0
InferenceEngine::InferencePluginInternal::CreateContext
RemoteContext::Ptr CreateContext(const ParamMap &) override
Creates a remote context instance based on a map of parameters.
Definition: ie_plugin_internal.hpp:130
InferenceEngine::InferencePluginInternal::~InferencePluginInternal
~InferencePluginInternal() override=default
Destroys the object.
InputsDataMap
std::map< std::string, InputInfo::Ptr > InputsDataMap
InferenceEngine::exportMagic
constexpr static const ExportMagic exportMagic
Magic number used by ie core to identify exported network with plugin name.
Definition: ie_icore.hpp:119
InferenceEngine::ICore
Minimal ICore interface to allow plugin to get information from Core Inference Engine class.
Definition: ie_icore.hpp:29
InferenceEngine::QueryNetworkResult
InferenceEngine::InferencePluginInternal::GetCore
ICore * GetCore() const noexcept override
Gets reference to ICore interface.
Definition: ie_plugin_internal.hpp:99
InferenceEngine::InferencePluginInternal::ImportNetwork
ExecutableNetwork ImportNetwork(std::istream &networkModel, const RemoteContext::Ptr &context, const std::map< std::string, std::string > &config) override
Creates an executable network from an previously exported network using plugin implementation and rem...
Definition: ie_plugin_internal.hpp:82
InferenceEngine::InferencePluginInternal::GetDefaultContext
RemoteContext::Ptr GetDefaultContext() override
Provides a default remote context instance if supported by a plugin.
Definition: ie_plugin_internal.hpp:134
InferenceEngine::InferencePluginInternal::SetCore
void SetCore(ICore *core) noexcept override
Sets pointer to ICore interface.
Definition: ie_plugin_internal.hpp:94
InferenceEngine::InferencePluginInternal::GetMetric
Parameter GetMetric(const std::string &, const std::map< std::string, Parameter > &) const override
Gets general runtime metric for dedicated hardware.
Definition: ie_plugin_internal.hpp:125
ie_executable_network_base.hpp
inference engine executanle network API wrapper, to be used by particular implementors
InferenceEngine::InferencePluginInternal::_pluginName
std::string _pluginName
A device name that plugins enables.
Definition: ie_plugin_internal.hpp:235
InferenceEngine::InferencePluginInternal::GetConfig
Parameter GetConfig(const std::string &, const std::map< std::string, Parameter > &) const override
Gets configuration dedicated to plugin behaviour.
Definition: ie_plugin_internal.hpp:120
OutputsDataMap
std::map< std::string, DataPtr > OutputsDataMap
InferenceEngine::IInferencePlugin
An API of plugin to be implemented by a plugin.
Definition: ie_iplugin_internal.hpp:91