ie_infer_request.hpp
Go to the documentation of this file.
1 // Copyright (C) 2018-2019 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  * @file ie_infer_request.hpp
8  */
9 #pragma once
10 
11 #include <map>
12 #include <memory>
13 #include <string>
14 
16 #include "ie_iinfer_request.hpp"
17 #include "ie_plugin_ptr.hpp"
18 
19 namespace InferenceEngine {
20 
21 namespace details {
22 
23 class ICompletionCallbackWrapper {
24 public:
25  virtual ~ICompletionCallbackWrapper() = default;
26 
27  virtual void call(InferenceEngine::IInferRequest::Ptr request, InferenceEngine::StatusCode code) const noexcept = 0;
28 };
29 
30 template <class T>
31 class CompletionCallbackWrapper : public ICompletionCallbackWrapper {
32  T lambda;
33 
34 public:
35  explicit CompletionCallbackWrapper(const T& lambda): lambda(lambda) {}
36 
37  void call(InferenceEngine::IInferRequest::Ptr /*request*/, InferenceEngine::StatusCode /*code*/) const
38  noexcept override {
39  lambda();
40  }
41 };
42 
43 template <>
44 class CompletionCallbackWrapper<IInferRequest::CompletionCallback> : public ICompletionCallbackWrapper {
46 
47 public:
48  explicit CompletionCallbackWrapper(const IInferRequest::CompletionCallback& callBack): callBack(callBack) {}
49 
50  void call(InferenceEngine::IInferRequest::Ptr request, InferenceEngine::StatusCode code) const noexcept override {
51  callBack(request, code);
52  }
53 };
54 
55 } // namespace details
56 
57 /**
58  * @brief This class is a wrapper of IInferRequest to provide setters/getters
59  * of input/output which operates with BlobMaps.
60  * It can throw exceptions safely for the application, where it is properly handled.
61  */
62 class InferRequest {
63  IInferRequest::Ptr actual;
65  std::shared_ptr<details::ICompletionCallbackWrapper> callback;
66 
67  static void callWrapper(InferenceEngine::IInferRequest::Ptr request, InferenceEngine::StatusCode code) {
68  details::ICompletionCallbackWrapper* pWrapper = nullptr;
69  ResponseDesc dsc;
70  request->GetUserData(reinterpret_cast<void**>(&pWrapper), &dsc);
71  pWrapper->call(request, code);
72  }
73 
74 public:
75  /**
76  * @brief Default constructor
77  */
78  InferRequest() = default;
79 
80  /**
81  * @brief Destructor
82  */
84  actual = nullptr;
85  }
86 
87  /**
88  * @brief Sets input/output data to infer
89  * @note: Memory allocation does not happen
90  * @param name Name of input or output blob.
91  * @param data Reference to input or output blob. The type of a blob must match the network input precision and
92  * size.
93  */
94  void SetBlob(const std::string& name, const Blob::Ptr& data) {
95  CALL_STATUS_FNC(SetBlob, name.c_str(), data);
96  }
97 
98  /**
99  * @brief Wraps original method
100  * IInferRequest::GetBlob
101  */
102  Blob::Ptr GetBlob(const std::string& name) {
103  Blob::Ptr data;
104  CALL_STATUS_FNC(GetBlob, name.c_str(), data);
105  std::string error = "Internal error: blob with name `" + name + "` is not allocated!";
106  auto blobPtr = data.get();
107  if (blobPtr == nullptr) THROW_IE_EXCEPTION << error;
108  if (blobPtr->buffer() == nullptr) THROW_IE_EXCEPTION << error;
109  return data;
110  }
111 
112  /**
113  * @brief Wraps original method
114  * IInferRequest::Infer
115  */
116  void Infer() {
117  CALL_STATUS_FNC_NO_ARGS(Infer);
118  }
119 
120  /**
121  * @brief Wraps original method
122  * IInferRequest::GetPerformanceCounts
123  */
124  std::map<std::string, InferenceEngineProfileInfo> GetPerformanceCounts() const {
125  std::map<std::string, InferenceEngineProfileInfo> perfMap;
126  CALL_STATUS_FNC(GetPerformanceCounts, perfMap);
127  return perfMap;
128  }
129 
130  /**
131  * @brief Sets input data to infer
132  * @note: Memory allocation doesn't happen
133  * @param inputs - a reference to a map of input blobs accessed by input names.
134  * The type of Blob must correspond to the network input precision and size.
135  */
136  void SetInput(const BlobMap& inputs) {
137  for (auto&& input : inputs) {
138  CALL_STATUS_FNC(SetBlob, input.first.c_str(), input.second);
139  }
140  }
141 
142  /**
143  * @brief Sets data that will contain result of the inference
144  * @note: Memory allocation doesn't happen
145  * @param results - a reference to a map of result blobs accessed by output names.
146  * The type of Blob must correspond to the network output precision and size.
147  */
148  void SetOutput(const BlobMap& results) {
149  for (auto&& result : results) {
150  CALL_STATUS_FNC(SetBlob, result.first.c_str(), result.second);
151  }
152  }
153 
154  /**
155  * @brief Sets new batch size when dynamic batching is enabled in executable network that created this request.
156  * @param batch new batch size to be used by all the following inference calls for this request.
157  */
158  void SetBatch(const int batch) {
159  CALL_STATUS_FNC(SetBatch, batch);
160  }
161 
162  /**
163  * constructs InferRequest from the initialized shared_pointer
164  * @param request Initialized shared pointer
165  * @param plg Plugin to use
166  */
167  explicit InferRequest(IInferRequest::Ptr request, InferenceEnginePluginPtr plg = {}): actual(request), plg(plg) {}
168 
169  /**
170  * @brief Start inference of specified input(s) in asynchronous mode
171  * @note: It returns immediately. Inference starts also immediately.
172  */
173  void StartAsync() {
174  CALL_STATUS_FNC_NO_ARGS(StartAsync);
175  }
176 
177  /**
178  * @brief Wraps original method
179  * IInferRequest::Wait
180  */
181  StatusCode Wait(int64_t millis_timeout) {
182  ResponseDesc resp;
183  auto res = actual->Wait(millis_timeout, &resp);
184  if (res != OK && res != RESULT_NOT_READY && res != INFER_NOT_STARTED) {
185  InferenceEngine::details::extract_exception(res, resp.msg);
186  }
187  return res;
188  }
189 
190  /**
191  * @brief Wraps original method
192  * IInferRequest::SetCompletionCallback
193  *
194  * @param callbackToSet Lambda callback object which will be called on processing finish.
195  */
196  template <class T>
197  void SetCompletionCallback(const T& callbackToSet) {
198  callback.reset(new details::CompletionCallbackWrapper<T>(callbackToSet));
199  CALL_STATUS_FNC(SetUserData, callback.get());
200  actual->SetCompletionCallback(callWrapper);
201  }
202 
203  /**
204  * @brief IInferRequest pointer to be used directly in CreateInferRequest functions
205  */
206  operator IInferRequest::Ptr&() {
207  return actual;
208  }
209 
210  /**
211  * @brief Checks if current InferRequest object is not initialized
212  * @return true if current InferRequest object is not initialized, false - otherwise
213  */
214  bool operator!() const noexcept {
215  return !actual;
216  }
217 
218  /**
219  * @brief Checks if current InferRequest object is initialized
220  * @return true if current InferRequest object is initialized, false - otherwise
221  */
222  explicit operator bool() const noexcept {
223  return !!actual;
224  }
225 
226  /**
227  * @brief A smart pointer to the InferRequest object
228  */
229  using Ptr = std::shared_ptr<InferRequest>;
230 };
231 
232 namespace details {
233 
234 template <>
235 class CompletionCallbackWrapper<std::function<void(InferRequest, StatusCode)>> : public ICompletionCallbackWrapper {
236  std::function<void(InferRequest, StatusCode)> lambda;
237 
238 public:
239  explicit CompletionCallbackWrapper(const std::function<void(InferRequest, InferenceEngine::StatusCode)>& lambda)
240  : lambda(lambda) {}
241 
242  void call(InferenceEngine::IInferRequest::Ptr request, InferenceEngine::StatusCode code) const noexcept override {
243  lambda(InferRequest(request), code);
244  }
245 };
246 
247 } // namespace details
248 } // namespace InferenceEngine
InferRequest(IInferRequest::Ptr request, InferenceEnginePluginPtr plg={})
Definition: ie_infer_request.hpp:167
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:24
void StartAsync()
Start inference of specified input(s) in asynchronous mode.
Definition: ie_infer_request.hpp:173
bool operator!() const noexcept
Checks if current InferRequest object is not initialized.
Definition: ie_infer_request.hpp:214
InferenceEngine::details::SOPointer< IInferencePlugin > InferenceEnginePluginPtr
A C++ helper to work with objects created by the plugin. Implements different interfaces.
Definition: ie_plugin_ptr.hpp:41
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:344
Inference Engine API.
Definition: ie_argmax_layer.hpp:11
Blob::Ptr GetBlob(const std::string &name)
Wraps original method IInferRequest::GetBlob.
Definition: ie_infer_request.hpp:102
StatusCode Wait(int64_t millis_timeout)
Wraps original method IInferRequest::Wait.
Definition: ie_infer_request.hpp:181
A header file that provides macros to handle no exception methods.
void SetInput(const BlobMap &inputs)
Sets input data to infer.
Definition: ie_infer_request.hpp:136
A header file contains a wrapper class for handling plugin instantiation and releasing resources...
void SetOutput(const BlobMap &results)
Sets data that will contain result of the inference.
Definition: ie_infer_request.hpp:148
void SetBlob(const std::string &name, const Blob::Ptr &data)
Sets input/output data to infer.
Definition: ie_infer_request.hpp:94
a header file for IInferRequest interface
This class is a wrapper of IInferRequest to provide setters/getters of input/output which operates wi...
Definition: ie_infer_request.hpp:62
void SetCompletionCallback(const T &callbackToSet)
Wraps original method IInferRequest::SetCompletionCallback.
Definition: ie_infer_request.hpp:197
std::shared_ptr< InferRequest > Ptr
A smart pointer to the InferRequest object.
Definition: ie_infer_request.hpp:229
Represents detailed information for an error.
Definition: ie_common.h:235
StatusCode
This enum contains codes for all possible return values of the interface functions.
Definition: ie_common.h:212
std::shared_ptr< Blob > Ptr
A smart pointer containing Blob object.
Definition: ie_blob.h:40
void(* CompletionCallback)(InferenceEngine::IInferRequest::Ptr context, InferenceEngine::StatusCode code)
Completion callback definition as pointer to a function.
Definition: ie_iinfer_request.hpp:121
std::map< std::string, InferenceEngineProfileInfo > GetPerformanceCounts() const
Wraps original method IInferRequest::GetPerformanceCounts.
Definition: ie_infer_request.hpp:124
void Infer()
Wraps original method IInferRequest::Infer.
Definition: ie_infer_request.hpp:116
char msg[256]
A character buffer that holds the detailed information for an error.
Definition: ie_common.h:239
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:158
~InferRequest()
Destructor.
Definition: ie_infer_request.hpp:83
std::shared_ptr< IInferRequest > Ptr
A shared pointer to the IInferRequest object.
Definition: ie_iinfer_request.hpp:41