ie_iplugin_internal.hpp
Go to the documentation of this file.
1 // Copyright (C) 2018-2021 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_iplugin_internal.hpp
8  */
9 
10 #pragma once
11 
12 #include <ie_iextension.h>
13 #include <ie_input_info.hpp>
14 #include <ie_parameter.hpp>
15 #include <cpp/ie_cnn_network.h>
16 
17 #include <blob_factory.hpp>
18 
19 #include <istream>
20 #include <map>
21 #include <memory>
22 #include <string>
23 
24 namespace InferenceEngine {
25 
26 class ICore;
27 class IExecutableNetworkInternal;
28 class RemoteContext;
29 class IExtension;
30 
31 /**
32  * @brief Copies preprocess info
33  *
34  * @param[in] from PreProcessInfo to copy from
35  * @return copy of preprocess info
36  */
37 INFERENCE_ENGINE_API_CPP(PreProcessInfo) copyPreProcess(const PreProcessInfo& from);
38 
39 /**
40  * @brief Copies the values of `std::string` indexed map and apply const cast
41  *
42  * @param[in] map map to copy
43  * @return map that contains pointers to constant values
44  */
45 template<typename T>
46 std::map<std::string, std::shared_ptr<const T>> constMapCast(const std::map<std::string, std::shared_ptr<T>>& map) {
47  std::map<std::string, std::shared_ptr<const T>> res;
48  for (auto&& v : map) res.emplace(v.first, std::const_pointer_cast<const T>(v.second));
49  return res;
50 }
51 
52 /**
53  * @brief Copies the values of `std::string` indexed map and apply const cast
54  *
55  * @param[in] map map to copy
56  * @return map that contains pointers to values
57  */
58 template<typename T>
59 std::map<std::string, std::shared_ptr<T>> constMapCast(const std::map<std::string, std::shared_ptr<const T>>& map) {
60  std::map<std::string, std::shared_ptr<T>> res;
61  for (auto&& v : map) res.emplace(v.first, std::const_pointer_cast<T>(v.second));
62  return res;
63 }
64 
65 /**
66  * @brief Copies InputInfo
67  *
68  * @param[in] networkInputs The network inputs to copy from
69  * @return copy of network inputs
70  */
71 INFERENCE_ENGINE_API_CPP(InputsDataMap) copyInfo(const InputsDataMap& networkInputs);
72 
73 /**
74  * @brief Copies OutputsData
75  *
76  * @param[in] networkInputs network outputs to copy from
77  * @return copy of network outputs
78  */
79 INFERENCE_ENGINE_API_CPP(OutputsDataMap) copyInfo(const OutputsDataMap& networkOutputs);
80 
81 /**
82  * @interface IInferencePlugin
83  * @brief An API of plugin to be implemented by a plugin
84  * @ingroup ie_dev_api_plugin_api
85  */
86 class INFERENCE_ENGINE_API_CLASS(IInferencePlugin) : public std::enable_shared_from_this<IInferencePlugin> {
87  class VersionStore : public Version {
88  std::string _dsc;
89  std::string _buildNumber;
90 
91  void copyFrom(const Version& v);
92 
93  public:
94  VersionStore() = default;
95 
96  explicit VersionStore(const Version& v);
97 
98  VersionStore& operator=(const VersionStore& v);
99  } _version;
100 
101 public:
102  /**
103  * @brief A shared pointer to IInferencePlugin interface
104  */
105  using Ptr = std::shared_ptr<IInferencePlugin>;
106 
107  /**
108  * @brief Sets a plugin version
109  * @param version A version to set
110  */
111  void SetVersion(const Version & version);
112 
113  /**
114  * @brief Gets a plugin version
115  * @return A const InferenceEngine::Version object
116  */
117  const Version& GetVersion() const;
118 
119  /**
120  * @brief Provides a name of a plugin
121  * @return The name.
122  */
123  virtual std::string GetName() const noexcept;
124 
125  /**
126  * @brief Sets a name for a plugin
127  * @param[in] name The name
128  */
129  virtual void SetName(const std::string& name) noexcept;
130 
131  /**
132  * @brief Creates an executable network from an pares network object, users can create as many networks as they need
133  * and use them simultaneously (up to the limitation of the HW resources)
134  * @param network A network object acquired from InferenceEngine::Core::ReadNetwork
135  * @param config A string-string map of config parameters relevant only for this load operation
136  * @return Created Executable Network object
137  */
138  virtual std::shared_ptr<IExecutableNetworkInternal> LoadNetwork(const CNNNetwork& network,
139  const std::map<std::string, std::string>& config);
140 
141  /**
142  * @brief Creates an executable network from network object, on specified remote context
143  * @param network A network object acquired from InferenceEngine::Core::ReadNetwork
144  * @param config string-string map of config parameters relevant only for this load operation
145  * @param context A pointer to plugin context derived from RemoteContext class used to
146  * execute the network
147  * @return Created Executable Network object
148  */
149  virtual std::shared_ptr<IExecutableNetworkInternal> LoadNetwork(const CNNNetwork& network,
150  const std::map<std::string, std::string>& config,
151  const std::shared_ptr<RemoteContext>& context);
152 
153  /**
154  * @brief Creates an executable network from model file path
155  * @param modelPath A path to model
156  * @param config A string-string map of config parameters relevant only for this load operation
157  * @return Created Executable Network object
158  */
159  virtual std::shared_ptr<IExecutableNetworkInternal> LoadNetwork(const std::string& modelPath,
160  const std::map<std::string, std::string>& config);
161 
162  /**
163  * @brief Registers extension within plugin
164  * @param extension - pointer to already loaded extension
165  */
166  virtual void AddExtension(const std::shared_ptr<IExtension>& extension);
167 
168  /**
169  * @brief Sets configuration for plugin, acceptable keys can be found in ie_plugin_config.hpp
170  * @param config string-string map of config parameters
171  */
172  virtual void SetConfig(const std::map<std::string, std::string>& config);
173 
174  /**
175  * @brief Gets configuration dedicated to plugin behaviour
176  * @param name - value of config corresponding to config key
177  * @param options - configuration details for config
178  * @return Value of config corresponding to config key
179  */
180  virtual Parameter GetConfig(const std::string& name, const std::map<std::string, Parameter>& options) const;
181 
182  /**
183  * @brief Gets general runtime metric for dedicated hardware
184  * @param name - metric name to request
185  * @param options - configuration details for metric
186  * @return Metric value corresponding to metric key
187  */
188  virtual Parameter GetMetric(const std::string& name, const std::map<std::string, Parameter>& options) const;
189 
190  /**
191  * @brief Creates a remote context instance based on a map of parameters
192  * @param[in] params The map of parameters
193  * @return A remote context object
194  */
195  virtual std::shared_ptr<RemoteContext> CreateContext(const ParamMap& params);
196 
197  /**
198  * @brief Provides a default remote context instance if supported by a plugin
199  * @param[in] params The map of parameters
200  * @return The default context.
201  */
202  virtual std::shared_ptr<RemoteContext> GetDefaultContext(const ParamMap& params);
203 
204  /**
205  * @deprecated Use ImportNetwork(std::istream& networkModel, const std::map<std::string, std::string>& config)
206  * @brief Creates an executable network from an previously exported network
207  * @param modelFileName - path to the location of the exported file
208  * @param config A string -> string map of parameters
209  * @return An Executable network
210  */
211  virtual std::shared_ptr<IExecutableNetworkInternal> ImportNetwork(const std::string& modelFileName,
212  const std::map<std::string, std::string>& config);
213 
214  /**
215  * @brief Creates an executable network from an previously exported network using plugin implementation
216  * and removes Inference Engine magic and plugin name
217  * @param networkModel Reference to network model output stream
218  * @param config A string -> string map of parameters
219  * @return An Executable network
220  */
221  virtual std::shared_ptr<IExecutableNetworkInternal> ImportNetwork(std::istream& networkModel,
222  const std::map<std::string, std::string>& config);
223 
224  /**
225  * @brief Creates an executable network from an previously exported network using plugin implementation
226  * and removes Inference Engine magic and plugin name
227  * @param networkModel Reference to network model output stream
228  * @param context A pointer to plugin context derived from RemoteContext class used to
229  * execute the network
230  * @param config A string -> string map of parameters
231  * @return An Executable network
232  */
233  virtual std::shared_ptr<IExecutableNetworkInternal> ImportNetwork(std::istream& networkModel,
234  const std::shared_ptr<RemoteContext>& context,
235  const std::map<std::string, std::string>& config);
236 
237  /**
238  * @brief Sets pointer to ICore interface
239  * @param core Pointer to Core interface
240  */
241  virtual void SetCore(ICore* core);
242 
243  /**
244  * @brief Gets reference to ICore interface
245  * @return Reference to ICore interface
246  */
247  virtual ICore* GetCore() const noexcept;
248 
249  /**
250  * @brief Queries a plugin about supported layers in network
251  * @param[in] network The network object to query
252  * @param[in] config The map of configuration parameters
253  * @return The result of query operator containing supported layers map
254  */
255  virtual QueryNetworkResult QueryNetwork(const CNNNetwork& network, const std::map<std::string, std::string>& config) const;
256 
257 protected:
258  ~IInferencePlugin() = default;
259 
260  /**
261  * @brief Creates an executable network from a parsed network object, users can create as many networks as they need
262  * and use them simultaneously (up to the limitation of the HW resources)
263  * @note The function is used in
264  * InferencePluginInternal::LoadNetwork(const CNNNetwork&, const std::map<std::string, std::string>&)
265  * which performs common steps first and calls this plugin-dependent method implementation after.
266  * @param network A network object
267  * @param config string-string map of config parameters relevant only for this load operation
268  * @return Shared pointer to the ExecutableNetwork object
269  */
270  virtual std::shared_ptr<IExecutableNetworkInternal> LoadExeNetworkImpl(const CNNNetwork& network,
271  const std::map<std::string, std::string>& config);
272 
273  /**
274  * @brief Creates an executable network using remote context from a parsed network object,
275  * users can create as many networks as they need and use them simultaneously (up to the limitation of the HW resources)
276  * @note The function is used in
277  * InferencePluginInternal::LoadNetwork(const CNNNetwork&, const std::map<std::string, std::string>&, RemoteContext::Ptr)
278  * which performs common steps first and calls this plugin-dependent method implementation after.
279  * @param network A network object
280  * @param context A remote context
281  * @param config string-string map of config parameters relevant only for this load operation
282  * @return Shared pointer to the ExecutableNetwork object
283  */
284  virtual std::shared_ptr<IExecutableNetworkInternal> LoadExeNetworkImpl(const CNNNetwork& network,
285  const std::shared_ptr<RemoteContext>& context,
286  const std::map<std::string, std::string>& config);
287 
288  /**
289  * @brief Set input and output information to executable network. This method is used to
290  * set addtional information to InferenceEngine::IExecutableNetworkInternal create by device plugin.
291  * @param exeNetwork An executable network object to set information to
292  * @param inputs An input information to set
293  * @param outputs An output information to set
294  */
295  void SetExeNetworkInfo(const std::shared_ptr<IExecutableNetworkInternal>& exeNetwork,
296  const ConstInputsDataMap& inputs,
297  const ConstOutputsDataMap& outputs);
298 
299  std::string _pluginName; //!< A device name that plugins enables
300  std::map<std::string, std::string> _config; //!< A map config keys -> values
301  ICore* _core = nullptr; //!< A pointer to ICore interface
302 };
303 
304 namespace details {
305 template <>
306 class SOCreatorTrait<IInferencePlugin> {
307 public:
308  static constexpr auto name = "CreatePluginEngine";
309 };
310 } // namespace details
311 
312 } // namespace InferenceEngine
313 
314 /**
315  * @def IE_DEFINE_PLUGIN_CREATE_FUNCTION(PluginType, version)
316  * @brief Defines the exported `CreatePluginEngine` function which is used to create a plugin instance
317  * @ingroup ie_dev_api_plugin_api
318  */
319 #define IE_DEFINE_PLUGIN_CREATE_FUNCTION(PluginType, version, ...) \
320  INFERENCE_PLUGIN_API(void) CreatePluginEngine(::std::shared_ptr<::InferenceEngine::IInferencePlugin>& plugin) { \
321  try { \
322  plugin = ::std::make_shared<PluginType>(__VA_ARGS__); \
323  } catch (const InferenceEngine::Exception&) { \
324  throw; \
325  } catch (const std::exception& ex) { \
326  IE_THROW() << ex.what(); \
327  } catch (...) { \
328  IE_THROW(Unexpected); \
329  } \
330  plugin->SetVersion(version); \
331  }
Minimal ICore interface to allow plugin to get information from Core Inference Engine class.
Definition: ie_icore.hpp:29
An internal API of executable network to be implemented by plugin,.
Definition: ie_iexecutable_network_internal.hpp:30
An API of plugin to be implemented by a plugin.
Definition: ie_iplugin_internal.hpp:86
void SetVersion(const Version &version)
Sets a plugin version.
virtual std::string GetName() const noexcept
Provides a name of a plugin.
const Version & GetVersion() const
Gets a plugin version.
std::shared_ptr< IInferencePlugin > Ptr
A shared pointer to IInferencePlugin interface.
Definition: ie_iplugin_internal.hpp:105
Inference Engine Plugin API namespace.
PreProcessInfo copyPreProcess(const PreProcessInfo &from)
Copies preprocess info.
std::map< std::string, InputInfo::Ptr > InputsDataMap
std::map< std::string, CDataPtr > ConstOutputsDataMap
std::map< std::string, InputInfo::CPtr > ConstInputsDataMap
std::map< std::string, std::shared_ptr< const T > > constMapCast(const std::map< std::string, std::shared_ptr< T >> &map)
Copies the values of std::string indexed map and apply const cast.
Definition: ie_iplugin_internal.hpp:46
std::map< std::string, Parameter > ParamMap
std::map< std::string, DataPtr > OutputsDataMap
InputsDataMap copyInfo(const InputsDataMap &networkInputs)
Copies InputInfo.
Serializes a std::vector to a std::ostream
Definition: debug.h:35