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  InferRequest() = default;
73 
74  ~InferRequest() {
75  actual = nullptr;
76  }
77 
78  /**
79  * @brief Sets input/output data to infer
80  * @note: Memory allocation does not happen
81  * @param name Name of input or output blob.
82  * @param data Reference to input or output blob. The type of a blob must match the network input precision and size.
83  */
84  void SetBlob(const std::string &name, const Blob::Ptr &data) {
85  CALL_STATUS_FNC(SetBlob, name.c_str(), data);
86  }
87 
88  /**
89  * @brief Wraps original method
90  * IInferRequest::GetBlob
91  */
92  Blob::Ptr GetBlob(const std::string &name) {
93  Blob::Ptr data;
94  CALL_STATUS_FNC(GetBlob, name.c_str(), data);
95  std::string error = "Internal error: blob with name `" + name + "` is not allocated!";
96  auto blobPtr = data.get();
97  if (blobPtr == nullptr) THROW_IE_EXCEPTION << error;
98  if (blobPtr->buffer() == nullptr) THROW_IE_EXCEPTION << error;
99  return data;
100  }
101 
102  /**
103  * @brief Wraps original method
104  * IInferRequest::Infer
105  */
106  void Infer() {
107  CALL_STATUS_FNC_NO_ARGS(Infer);
108  }
109 
110  /**
111  * @brief Wraps original method
112  * IInferRequest::GetPerformanceCounts
113  */
114  std::map<std::string, InferenceEngineProfileInfo> GetPerformanceCounts() const {
115  std::map<std::string, InferenceEngineProfileInfo> perfMap;
116  CALL_STATUS_FNC(GetPerformanceCounts, perfMap);
117  return perfMap;
118  }
119 
120  /**
121  * @brief Sets input data to infer
122  * @note: Memory allocation doesn't happen
123  * @param inputs - a reference to a map of input blobs accessed by input names.
124  * The type of Blob must correspond to the network input precision and size.
125  */
126  void SetInput(const BlobMap &inputs) {
127  for (auto &&input : inputs) {
128  CALL_STATUS_FNC(SetBlob, input.first.c_str(), input.second);
129  }
130  }
131 
132  /**
133  * @brief Sets data that will contain result of the inference
134  * @note: Memory allocation doesn't happen
135  * @param results - a reference to a map of result blobs accessed by output names.
136  * The type of Blob must correspond to the network output precision and size.
137  */
138  void SetOutput(const BlobMap &results) {
139  for (auto &&result : results) {
140  CALL_STATUS_FNC(SetBlob, result.first.c_str(), result.second);
141  }
142  }
143 
144  /**
145  * @brief Sets new batch size when dynamic batching is enabled in executable network that created this request.
146  * @param batch new batch size to be used by all the following inference calls for this request.
147  */
148  void SetBatch(const int batch) {
149  CALL_STATUS_FNC(SetBatch, batch);
150  }
151 
152  /**
153  * constructs InferRequest from initialised shared_pointer
154  * @param actual
155  */
156  explicit InferRequest(IInferRequest::Ptr request, InferenceEnginePluginPtr plg = {})
157  : actual(request), plg(plg) {}
158 
159  /**
160  * @brief Start inference of specified input(s) in asynchronous mode
161  * @note: It returns immediately. Inference starts also immediately.
162  */
163  void StartAsync() {
164  CALL_STATUS_FNC_NO_ARGS(StartAsync);
165  }
166 
167  /**
168  * @brief Wraps original method
169  * IInferRequest::Wait
170  */
171  StatusCode Wait(int64_t millis_timeout) {
172  return actual->Wait(millis_timeout, nullptr);
173  }
174 
175  /**
176  * @brief Wraps original method
177  * IInferRequest::SetCompletionCallback
178  *
179  * @param callbackToSet Lambda callback object which will be called on processing finish.
180  */
181  template <class T>
182  void SetCompletionCallback(const T & callbackToSet) {
183  callback.reset(new details::CompletionCallbackWrapper<T>(callbackToSet));
184  CALL_STATUS_FNC(SetUserData, callback.get());
185  actual->SetCompletionCallback(callWrapper);
186  }
187 
188  /**
189  * @brief IInferRequest pointer to be used directly in CreateInferRequest functions
190  */
191  operator IInferRequest::Ptr &() {
192  return actual;
193  }
194 
195  bool operator!() const noexcept {
196  return !actual;
197  }
198 
199  explicit operator bool() const noexcept {
200  return !!actual;
201  }
202 
203  using Ptr = std::shared_ptr<InferRequest>;
204 };
205 
206 namespace details {
207 
208 template<>
209 class CompletionCallbackWrapper<std::function<void(InferRequest, StatusCode)>>
210  : public ICompletionCallbackWrapper {
211  std::function<void(InferRequest, StatusCode)> lambda;
212 public:
213  explicit CompletionCallbackWrapper(const std::function<void(InferRequest, InferenceEngine::StatusCode)> &lambda)
214  : lambda(lambda) {}
215 
216  void call(InferenceEngine::IInferRequest::Ptr request,
217  InferenceEngine::StatusCode code) const noexcept override {
218  lambda(InferRequest(request), code);
219  }
220 };
221 
222 } // namespace details
223 } // namespace InferenceEngine
InferRequest(IInferRequest::Ptr request, InferenceEnginePluginPtr plg={})
Definition: ie_infer_request.hpp:156
#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:163
Definition: ie_argmax_layer.hpp:11
Blob::Ptr GetBlob(const std::string &name)
Wraps original method IInferRequest::GetBlob.
Definition: ie_infer_request.hpp:92
StatusCode Wait(int64_t millis_timeout)
Wraps original method IInferRequest::Wait.
Definition: ie_infer_request.hpp:171
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:126
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:138
void SetBlob(const std::string &name, const Blob::Ptr &data)
Sets input/output data to infer.
Definition: ie_infer_request.hpp:84
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:182
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:102
std::map< std::string, InferenceEngineProfileInfo > GetPerformanceCounts() const
Wraps original method IInferRequest::GetPerformanceCounts.
Definition: ie_infer_request.hpp:114
void Infer()
Wraps original method IInferRequest::Infer.
Definition: ie_infer_request.hpp:106
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:148