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