ie_infer_request.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 A header file that provides wrapper classes for infer requests and callbacks.
7  *
8  * @file ie_infer_request.hpp
9  */
10 #pragma once
11 
12 #include <map>
13 #include <memory>
14 #include <string>
15 
16 #include "cpp/ie_memory_state.hpp"
17 #include "ie_remote_context.hpp"
18 #include "ie_iinfer_request.hpp"
20 #include "details/ie_so_loader.h"
21 
22 namespace InferenceEngine {
23 
24 namespace details {
25 
26 class ICompletionCallbackWrapper {
27 public:
28  virtual ~ICompletionCallbackWrapper() = default;
29 
30  virtual void call(InferenceEngine::IInferRequest::Ptr request, InferenceEngine::StatusCode code) const noexcept = 0;
31 };
32 
33 template <class T>
34 class CompletionCallbackWrapper : public ICompletionCallbackWrapper {
35  T lambda;
36 
37 public:
38  explicit CompletionCallbackWrapper(const T& lambda): lambda(lambda) {}
39 
40  void call(InferenceEngine::IInferRequest::Ptr /*request*/, InferenceEngine::StatusCode /*code*/) const
41  noexcept override {
42  lambda();
43  }
44 };
45 
46 template <>
47 class CompletionCallbackWrapper<IInferRequest::CompletionCallback> : public ICompletionCallbackWrapper {
49 
50 public:
51  explicit CompletionCallbackWrapper(const IInferRequest::CompletionCallback& callBack): callBack(callBack) {}
52 
53  void call(InferenceEngine::IInferRequest::Ptr request, InferenceEngine::StatusCode code) const noexcept override {
54  callBack(request, code);
55  }
56 };
57 
58 } // namespace details
59 
60 /**
61  * @copybrief IInferRequest
62  *
63  * Wraps IInferRequest
64  * It can throw exceptions safely for the application, where it is properly handled.
65  */
66 class InferRequest {
67  IInferRequest::Ptr actual;
68  InferenceEngine::details::SharedObjectLoader::Ptr plg;
69  std::shared_ptr<details::ICompletionCallbackWrapper> callback;
70 
71  static void callWrapper(InferenceEngine::IInferRequest::Ptr request, InferenceEngine::StatusCode code) {
72  details::ICompletionCallbackWrapper* pWrapper = nullptr;
73  ResponseDesc dsc;
74  request->GetUserData(reinterpret_cast<void**>(&pWrapper), &dsc);
75  pWrapper->call(request, code);
76  }
77 
78 public:
79  /**
80  * @brief Default constructor
81  */
82  InferRequest() = default;
83 
84  /**
85  * constructs InferRequest from the initialized shared_pointer
86  * @param request Initialized shared pointer to IInferRequest interface
87  * @param splg Plugin to use. This is required to ensure that InferRequest can work properly even if plugin object is destroyed.
88  */
89  explicit InferRequest(IInferRequest::Ptr request,
90  InferenceEngine::details::SharedObjectLoader::Ptr splg = {}):
91  actual(request), plg(splg) {
92  // plg can be null, but not the actual
93  if (actual == nullptr) THROW_IE_EXCEPTION << "InferRequest was not initialized.";
94  }
95 
96  /**
97  * @brief Destructor
98  */
100  actual = nullptr;
101  }
102 
103  /**
104  * @brief Sets input/output data to infer
105  *
106  * @note Memory allocation does not happen
107  * @param name Name of input or output blob.
108  * @param data Reference to input or output blob. The type of a blob must match the network input precision and
109  * size.
110  */
111  void SetBlob(const std::string& name, const Blob::Ptr& data) {
112  CALL_STATUS_FNC(SetBlob, name.c_str(), data);
113  }
114 
115  /**
116  * @copybrief IInferRequest::GetBlob
117  *
118  * Wraps IInferRequest::GetBlob
119  * @param name A name of Blob to get
120  * @return A shared pointer to a Blob with a name @p name. If a blob is not found, an exception is thrown.
121  */
122  Blob::Ptr GetBlob(const std::string& name) {
123  Blob::Ptr data;
124  CALL_STATUS_FNC(GetBlob, name.c_str(), data);
125  std::string error = "Internal error: blob with name `" + name + "` is not allocated!";
126  auto blobPtr = data.get();
127  const bool remoteBlobPassed = blobPtr->is<RemoteBlob>();
128  if (blobPtr == nullptr) THROW_IE_EXCEPTION << error;
129  if (!remoteBlobPassed && blobPtr->buffer() == nullptr) THROW_IE_EXCEPTION << error;
130  return data;
131  }
132 
133  /**
134  * @brief Sets blob with a pre-process information
135  * @note Returns an error in case if data blob is output
136  * @param name Name of input blob.
137  * @param data A reference to input. The type of Blob must correspond to the network input precision and size.
138  * @param info Preprocess info for blob.
139  */
140  void SetBlob(const std::string &name, const Blob::Ptr &data, const PreProcessInfo& info) {
141  CALL_STATUS_FNC(SetBlob, name.c_str(), data, info);
142  }
143 
144  /**
145  * @brief Gets pre-process for input data
146  * @param name Name of input blob.
147  * @return pointer to pre-process info of blob with name
148  */
149  const PreProcessInfo& GetPreProcess(const std::string& name) const {
150  const PreProcessInfo* info = nullptr;
151  CALL_STATUS_FNC(GetPreProcess, name.c_str(), &info);
152  return *info;
153  }
154 
155  /**
156  * @copybrief IInferRequest::Infer
157  * @note blocks all methods of InferRequest while request is ongoing (running or waiting in queue)
158  *
159  * Wraps IInferRequest::Infer
160  */
161  void Infer() {
162  CALL_STATUS_FNC_NO_ARGS(Infer);
163  }
164 
165  /**
166  * @copybrief IInferRequest::Cancel
167  *
168  * Wraps IInferRequest::Cancel
169  */
170  void Cancel() {
171  CALL_STATUS_FNC_NO_ARGS(Cancel);
172  }
173 
174  /**
175  * @copybrief IInferRequest::GetPerformanceCounts
176  *
177  * Wraps IInferRequest::GetPerformanceCounts
178  * @return Map of layer names to profiling information for that layer
179  */
180  std::map<std::string, InferenceEngineProfileInfo> GetPerformanceCounts() const {
181  std::map<std::string, InferenceEngineProfileInfo> perfMap;
182  CALL_STATUS_FNC(GetPerformanceCounts, perfMap);
183  return perfMap;
184  }
185 
186  /**
187  * @brief Sets input data to infer
188  *
189  * @note Memory allocation doesn't happen
190  * @param inputs A reference to a map of input blobs accessed by input names.
191  * The type of Blob must correspond to the network input precision and size.
192  */
193  void SetInput(const BlobMap& inputs) {
194  for (auto&& input : inputs) {
195  CALL_STATUS_FNC(SetBlob, input.first.c_str(), input.second);
196  }
197  }
198 
199  /**
200  * @brief Sets data that will contain result of the inference
201  *
202  * @note Memory allocation doesn't happen
203  * @param results - a reference to a map of result blobs accessed by output names.
204  * The type of Blob must correspond to the network output precision and size.
205  */
206  void SetOutput(const BlobMap& results) {
207  for (auto&& result : results) {
208  CALL_STATUS_FNC(SetBlob, result.first.c_str(), result.second);
209  }
210  }
211 
212  /**
213  * @brief Sets new batch size when dynamic batching is enabled in executable network that created this request.
214  *
215  * @param batch new batch size to be used by all the following inference calls for this request.
216  */
217  void SetBatch(const int batch) {
218  CALL_STATUS_FNC(SetBatch, batch);
219  }
220 
221  /**
222  * @brief Start inference of specified input(s) in asynchronous mode
223  *
224  * @note It returns immediately. Inference starts also immediately.
225  */
226  void StartAsync() {
227  CALL_STATUS_FNC_NO_ARGS(StartAsync);
228  }
229 
230  /**
231  * @copybrief IInferRequest::Wait
232  *
233  * Wraps IInferRequest::Wait
234  * @param millis_timeout Maximum duration in milliseconds to block for
235  * @note There are special cases when millis_timeout is equal some value of the WaitMode enum:
236  * * STATUS_ONLY - immediately returns inference status (IInferRequest::RequestStatus). It does not block or
237  * interrupt current thread
238  * * RESULT_READY - waits until inference result becomes available
239  * @return A status code of operation
240  */
241  StatusCode Wait(int64_t millis_timeout) {
242  ResponseDesc resp;
243  if (actual == nullptr) THROW_IE_EXCEPTION << "InferRequest was not initialized.";
244  auto res = actual->Wait(millis_timeout, &resp);
245  if (res != OK && res != RESULT_NOT_READY &&
246  res != INFER_NOT_STARTED && res != INFER_CANCELLED) {
247  THROW_IE_EXCEPTION << InferenceEngine::details::as_status << res << resp.msg;
248  }
249  return res;
250  }
251 
252  /**
253  * @copybrief IInferRequest::SetCompletionCallback
254  *
255  * Wraps IInferRequest::SetCompletionCallback
256  *
257  * @param callbackToSet Lambda callback object which will be called on processing finish.
258  */
259  template <class T>
260  void SetCompletionCallback(const T& callbackToSet) {
261  callback.reset(new details::CompletionCallbackWrapper<T>(callbackToSet));
262  CALL_STATUS_FNC(SetUserData, callback.get());
263  actual->SetCompletionCallback(callWrapper);
264  }
265 
266  /**
267  * @copybrief IExecutableNetwork::QueryState
268  *
269  * Wraps IExecutableNetwork::QueryState
270  * @return A vector of Memory State objects
271  */
272  std::vector<VariableState> QueryState() {
273  IE_SUPPRESS_DEPRECATED_START
274  if (actual == nullptr) THROW_IE_EXCEPTION << "ExecutableNetwork was not initialized.";
275  IVariableState::Ptr pState = nullptr;
276  auto res = OK;
277  std::vector<VariableState> controller;
278  for (size_t idx = 0; res == OK; ++idx) {
279  ResponseDesc resp;
280  res = actual->QueryState(pState, idx, &resp);
281  if (res != OK && res != OUT_OF_BOUNDS) {
282  THROW_IE_EXCEPTION << resp.msg;
283  }
284  if (res != OUT_OF_BOUNDS) {
285  controller.push_back(VariableState(pState, plg));
286  }
287  }
288  IE_SUPPRESS_DEPRECATED_END
289 
290  return controller;
291  }
292 
293  /**
294  * @brief IInferRequest pointer to be used directly in CreateInferRequest functions
295  * @return A shared pointer to underlying IInferRequest interface
296  */
297  operator IInferRequest::Ptr&() {
298  if (actual == nullptr) THROW_IE_EXCEPTION << "InferRequest was not initialized.";
299  return actual;
300  }
301 
302  /**
303  * @brief Checks if current InferRequest object is not initialized
304  * @return true if current InferRequest object is not initialized, false - otherwise
305  */
306  bool operator!() const noexcept {
307  return !actual;
308  }
309 
310  /**
311  * @brief Checks if current InferRequest object is initialized
312  * @return true if current InferRequest object is initialized, false - otherwise
313  */
314  explicit operator bool() const noexcept {
315  return !!actual;
316  }
317 
318  /**
319  * @brief A smart pointer to the InferRequest object
320  */
321  using Ptr = std::shared_ptr<InferRequest>;
322 };
323 
324 namespace details {
325 
326 template <>
327 class CompletionCallbackWrapper<std::function<void(InferRequest, StatusCode)>> : public ICompletionCallbackWrapper {
328  std::function<void(InferRequest, StatusCode)> lambda;
329 
330 public:
331  explicit CompletionCallbackWrapper(const std::function<void(InferRequest, InferenceEngine::StatusCode)>& lambda)
332  : lambda(lambda) {}
333 
334  void call(InferenceEngine::IInferRequest::Ptr request, InferenceEngine::StatusCode code) const noexcept override {
335  lambda(InferRequest(request), code);
336  }
337 };
338 
339 } // namespace details
340 } // namespace InferenceEngine
std::shared_ptr< Blob > Ptr
A smart pointer containing Blob object.
Definition: ie_blob.h:43
void(* CompletionCallback)(InferenceEngine::IInferRequest::Ptr context, InferenceEngine::StatusCode code)
Completion callback definition as pointer to a function.
Definition: ie_iinfer_request.hpp:148
std::shared_ptr< IInferRequest > Ptr
A shared pointer to the IInferRequest object.
Definition: ie_iinfer_request.hpp:44
std::shared_ptr< IVariableState > Ptr
A shared pointer to the IVariableState interface.
Definition: ie_imemory_state.hpp:32
This is an interface of asynchronous infer request.
Definition: ie_infer_request.hpp:66
InferRequest(IInferRequest::Ptr request, InferenceEngine::details::SharedObjectLoader::Ptr splg={})
Definition: ie_infer_request.hpp:89
std::vector< VariableState > QueryState()
Gets state control interface for given executable network.
Definition: ie_infer_request.hpp:272
std::map< std::string, InferenceEngineProfileInfo > GetPerformanceCounts() const
Queries performance measures per layer to get feedback of what is the most time consuming layer.
Definition: ie_infer_request.hpp:180
void SetInput(const BlobMap &inputs)
Sets input data to infer.
Definition: ie_infer_request.hpp:193
void SetBlob(const std::string &name, const Blob::Ptr &data, const PreProcessInfo &info)
Sets blob with a pre-process information.
Definition: ie_infer_request.hpp:140
void SetBlob(const std::string &name, const Blob::Ptr &data)
Sets input/output data to infer.
Definition: ie_infer_request.hpp:111
void SetBatch(const int batch)
Sets new batch size when dynamic batching is enabled in executable network that created this request.
Definition: ie_infer_request.hpp:217
void Infer()
Infers specified input(s) in synchronous mode.
Definition: ie_infer_request.hpp:161
const PreProcessInfo & GetPreProcess(const std::string &name) const
Gets pre-process for input data.
Definition: ie_infer_request.hpp:149
void StartAsync()
Start inference of specified input(s) in asynchronous mode.
Definition: ie_infer_request.hpp:226
InferRequest()=default
Default constructor.
void SetCompletionCallback(const T &callbackToSet)
Sets a callback function that will be called on success or failure of asynchronous request.
Definition: ie_infer_request.hpp:260
bool operator!() const noexcept
Checks if current InferRequest object is not initialized.
Definition: ie_infer_request.hpp:306
void SetOutput(const BlobMap &results)
Sets data that will contain result of the inference.
Definition: ie_infer_request.hpp:206
Blob::Ptr GetBlob(const std::string &name)
Gets input/output data for inference.
Definition: ie_infer_request.hpp:122
std::shared_ptr< InferRequest > Ptr
A smart pointer to the InferRequest object.
Definition: ie_infer_request.hpp:321
StatusCode Wait(int64_t millis_timeout)
Waits for the result to become available. Blocks until specified millis_timeout has elapsed or the re...
Definition: ie_infer_request.hpp:241
void Cancel()
Cancels current async inference request.
Definition: ie_infer_request.hpp:170
~InferRequest()
Destructor.
Definition: ie_infer_request.hpp:99
This class stores pre-process information for the input.
Definition: ie_preprocess.hpp:55
This class represents an Inference Engine abstraction to the memory allocated on the remote (non-CPU)...
Definition: ie_remote_context.hpp:31
C++ exception based error reporting wrapper of API class IVariableState.
Definition: ie_memory_state.hpp:28
#define THROW_IE_EXCEPTION
A macro used to throw general exception with a description.
Definition: ie_exception.hpp:25
A header file that provides macros to handle no exception methods.
a header file for IInferRequest interface
A header file that provides wrapper classes for IVariableState.
This is a header file for the IE RemoteContext and RemoteBlob classes.
A header file for definition of abstraction over platform specific shared objects.
Inference Engine C++ API.
Definition: cldnn_config.hpp:15
StatusCode
This enum contains codes for all possible return values of the interface functions.
Definition: ie_common.h:224
std::map< std::string, Blob::Ptr > BlobMap
This is a convenient type for working with a map containing pairs(string, pointer to a Blob instance)...
Definition: ie_blob.h:486
Node result(NodeInput data, Optional[str] name=None)
Represents detailed information for an error.
Definition: ie_common.h:248
char msg[4096]
A character buffer that holds the detailed information for an error.
Definition: ie_common.h:252