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