ie_iinfer_request_internal.hpp
1 // Copyright (C) 2018-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 #pragma once
6 
7 #include <ie_blob.h>
8 #include <ie_common.h>
9 #include <ie_preprocess_data.hpp>
10 #include <ie_input_info.hpp>
11 #include <cpp/ie_infer_request.hpp>
12 
13 #include <map>
14 #include <memory>
15 #include <string>
16 
17 namespace InferenceEngine {
18 
19 class IExecutableNetworkInternal;
20 class IVariableStateInternal;
21 
22 /**
23  * @interface IInferRequestInternal
24  * @brief An internal API of synchronous inference request to be implemented by plugin,
25  * which is used in InferRequestBase forwarding mechanism
26  * @ingroup ie_dev_api_infer_request_api
27  */
28 class INFERENCE_ENGINE_API_CLASS(IInferRequestInternal) : public std::enable_shared_from_this<IInferRequestInternal> {
29 public:
30  /**
31  * @brief A shared pointer to a IInferRequestInternal interface
32  */
33  using Ptr = std::shared_ptr<IInferRequestInternal>;
34 
35  IInferRequestInternal() = default;
36 
37  /**
38  * @brief Constructs a new instance.
39  * @param[in] networkInputs The network inputs info
40  * @param[in] networkOutputs The network outputs data
41  */
42  IInferRequestInternal(const InputsDataMap& networkInputs, const OutputsDataMap& networkOutputs);
43 
44  /**
45  * @brief Infers specified input(s) in synchronous mode
46  * @note blocks all method of InferRequest while request is ongoing (running or waiting in queue)
47  */
48  virtual void Infer();
49 
50  /**
51  * @brief The minimal infer function to be implemented by plugins. It infers specified input(s) in synchronous mode
52  * @note
53  * * This method is used in IInferRequestInternal::Infer, which calls the common code first and after uses this
54  * plugin dependent implementation.
55  * * Blocks all method of InferRequest while request is ongoing (running or waiting in queue)
56  */
57  virtual void InferImpl();
58 
59  /**
60  * @brief Cancel current inference request execution
61  */
62  virtual void Cancel();
63 
64  /**
65  * @brief Queries performance measures per layer to get feedback of what is the most time consuming layer.
66  * Note: not all plugins may provide meaningful data
67  * @return - a map of layer names to profiling information for that layer.
68  */
69  virtual std::map<std::string, InferenceEngineProfileInfo> GetPerformanceCounts() const;
70 
71  /**
72  * @brief Set input/output data to infer
73  * @note Memory allocation doesn't happen
74  * @param name - a name of input or output blob.
75  * @param data - a reference to input or output blob. The type of Blob must correspond to the network input
76  * precision and size.
77  */
78  virtual void SetBlob(const std::string& name, const Blob::Ptr& data);
79 
80  /**
81  * @brief Get input/output data to infer
82  * @note Memory allocation doesn't happen
83  * @param name - a name of input or output blob.
84  * @param data - a reference to input or output blob. The type of Blob must correspond to the network input
85  * precision and size.
86  */
87  virtual Blob::Ptr GetBlob(const std::string& name);
88 
89  /**
90  * @brief Sets pre-process for input data
91  * @param name Name of input blob.
92  * @param data - a reference to input or output blob. The type of Blob must correspond to the network input precision and size.
93  * @param info Preprocess info for blob.
94  */
95  virtual void SetBlob(const std::string& name, const Blob::Ptr& data, const PreProcessInfo& info);
96 
97  /**
98  * @brief Gets pre-process for input data
99  * @param name Name of input blob.
100  * @param info pointer to a pointer to PreProcessInfo structure
101  */
102  virtual const PreProcessInfo& GetPreProcess(const std::string& name) const;
103 
104  /**
105  * @brief Sets new batch size when dynamic batching is enabled in executable network that created this request.
106  * @param batch - new batch size to be used by all the following inference calls for this request.
107  */
108  virtual void SetBatch(int batch);
109 
110  /**
111  * @brief Queries memory states.
112  * @return Returns memory states
113  */
114  virtual std::vector<std::shared_ptr<IVariableStateInternal>> QueryState();
115 
116  /**
117  * @brief Start inference of specified input(s) in asynchronous mode
118  * @note The method returns immediately. Inference starts also immediately.
119  */
120  virtual void StartAsync();
121 
122  /**
123  * @brief The minimal asynchronous inference function to be implemented by plugins.
124  * It starts inference of specified input(s) in asynchronous mode
125  * @note
126  * * The methos is used in AsyncInferRequestInternal::StartAsync which performs common steps first and
127  * calls plugin dependent implementation of this method after.
128  * * It returns immediately. Inference starts also immediately.
129  */
130  virtual void StartAsyncImpl();
131 
132  /**
133  * @brief Waits for the result to become available. Blocks until specified millis_timeout has elapsed or the result
134  * becomes available, whichever comes first.
135  * @param millis_timeout - maximum duration in milliseconds to block for
136  * @note There are special cases when millis_timeout is equal some value of WaitMode enum:
137  * * STATUS_ONLY - immediately returns request status (InferRequest::StatusCode). It doesn't block or interrupt
138  * current thread.
139  * * RESULT_READY - waits until inference result becomes available
140  * @return A status code
141  */
142  virtual StatusCode Wait(int64_t millis_timeout);
143 
144  /**
145  * @brief Alias for callback type
146  */
147  using Callback = std::function<void(std::exception_ptr)>;
148 
149  /**
150  * @brief Set callback function which will be called on success or failure of asynchronous request
151  * @param callback - function to be called with the following description:
152  */
153  virtual void SetCallback(Callback callback);
154 
155  /**
156  * @brief Check that @p blob is valid. Throws an exception if it's not.
157  *
158  * @param[in] blob The blob to check
159  * @param[in] name The name of input or output depending of if the @p blob is input or output
160  * @param[in] isInput Indicates if @p is input
161  * @param[in] refDims The reference dims, empty if not specified
162  */
163  void checkBlob(const Blob::Ptr& blob, const std::string& name, bool isInput, const SizeVector& refDims = {}) const;
164 
165  /**
166  * @brief Check that all of the blobs is valid. Throws an exception if it's not.
167  */
168  virtual void checkBlobs();
169 
170  /**
171  * @brief Sets the pointer to executable network internal.
172  * @note Needed to correctly handle ownership between objects.
173  * @param[in] exeNetwork The executable network
174  */
175  void setPointerToExecutableNetworkInternal(const std::shared_ptr<IExecutableNetworkInternal>& exeNetwork);
176 
177  /**
178  * @brief Gets the pointer to userData.
179  * @return Pointer to user data
180  */
181  INFERENCE_ENGINE_DEPRECATED("The method will be removed")
182  void* GetUserData() noexcept;
183 
184  /**
185  * @brief Sets the pointer to userData.
186  * @param[in] Pointer to user data
187  */
188  INFERENCE_ENGINE_DEPRECATED("The method will be removed")
189  void SetUserData(void* userData) noexcept;
190 
191 protected:
192  /**
193  * @brief Destroys the object.
194  */
196 
197  /**
198  * @brief Checks and executes input data pre-processing if needed.
199  * @param inputs Inputs blobs to perform preprocessing on
200  * @param serial Whether to use multiple threads to execute the step
201  */
202  void execDataPreprocessing(InferenceEngine::BlobMap& preprocessedBlobs, bool serial = false);
203 
204  /**
205  * @brief Helper function to find input or output blob by name
206  * @param name A name of input or output blob.
207  * @param foundInput A pointer to input information if found.
208  * @param foundOutput A pointer to output DataPtr if found.
209  * @return `True` - if loaded network has input with provided name,
210  * `false` - if loaded network has output with provided name
211  * @throws [not_found] exception if there is no input and output layers with given name
212  */
213  bool findInputAndOutputBlobByName(const std::string& name, InputInfo::Ptr& foundInput, DataPtr& foundOutput) const;
214 
215  /**
216  * @brief Checks whether pre-processing step is required for a given input
217  * @param info InputInfo corresponding to input blob
218  * @param userBlob Input Blob object corresponding to input info
219  * @param deviceBlob Blob object in plugin's desired format
220  * @return `True` if pre-processing is required, `false` otherwise
221  */
222  bool preProcessingRequired(const InputInfo::Ptr& info, const Blob::Ptr& userBlob, const Blob::Ptr& deviceBlob = nullptr);
223 
224  void addInputPreProcessingFor(const std::string& name, Blob::Ptr const& from, const Blob::Ptr& to);
225 
226  InferenceEngine::InputsDataMap _networkInputs; //!< Holds information about network inputs info
227  InferenceEngine::OutputsDataMap _networkOutputs; //!< Holds information about network outputs data
228  InferenceEngine::BlobMap _inputs; //!< A map of user passed blobs for network inputs
229  InferenceEngine::BlobMap _deviceInputs; //!< A map of actual network inputs, in plugin specific format
230  InferenceEngine::BlobMap _outputs; //!< A map of user passed blobs for network outputs
231  std::map<std::string, PreProcessDataPtr> _preProcData; //!< A map of pre-process data per input
232  int m_curBatch = -1; //!< Current batch value used in dynamic batching
233 
234  /**
235  * @brief A shared pointer to IInferRequestInternal
236  * @note Needed to correctly handle ownership between objects.
237  */
238  std::shared_ptr<IExecutableNetworkInternal> _exeNetwork;
239  Callback _callback; //!< A callback
240 
241 private:
242  void* _userData = nullptr;
243 };
244 
245 /**
246  * @brief SOPointer to IInferRequestInternal.
247  */
248 using SoIInferRequestInternal = details::SOPointer<IInferRequestInternal>;
249 
250 } // namespace InferenceEngine
std::shared_ptr< Blob > Ptr
An internal API of executable network to be implemented by plugin,.
Definition: ie_iexecutable_network_internal.hpp:30
An internal API of synchronous inference request to be implemented by plugin, which is used in InferR...
Definition: ie_iinfer_request_internal.hpp:28
virtual const PreProcessInfo & GetPreProcess(const std::string &name) const
Gets pre-process for input data.
virtual void InferImpl()
The minimal infer function to be implemented by plugins. It infers specified input(s) in synchronous ...
void checkBlob(const Blob::Ptr &blob, const std::string &name, bool isInput, const SizeVector &refDims={}) const
Check that blob is valid. Throws an exception if it's not.
virtual void checkBlobs()
Check that all of the blobs is valid. Throws an exception if it's not.
virtual void Cancel()
Cancel current inference request execution.
virtual std::map< std::string, InferenceEngineProfileInfo > GetPerformanceCounts() const
Queries performance measures per layer to get feedback of what is the most time consuming layer....
virtual StatusCode Wait(int64_t millis_timeout)
Waits for the result to become available. Blocks until specified millis_timeout has elapsed or the re...
std::shared_ptr< IInferRequestInternal > Ptr
A shared pointer to a IInferRequestInternal interface.
Definition: ie_iinfer_request_internal.hpp:33
virtual void SetBatch(int batch)
Sets new batch size when dynamic batching is enabled in executable network that created this request.
virtual void StartAsyncImpl()
The minimal asynchronous inference function to be implemented by plugins. It starts inference of spec...
std::function< void(std::exception_ptr)> Callback
Alias for callback type.
Definition: ie_iinfer_request_internal.hpp:147
virtual void SetBlob(const std::string &name, const Blob::Ptr &data, const PreProcessInfo &info)
Sets pre-process for input data.
virtual void StartAsync()
Start inference of specified input(s) in asynchronous mode.
void setPointerToExecutableNetworkInternal(const std::shared_ptr< IExecutableNetworkInternal > &exeNetwork)
Sets the pointer to executable network internal.
virtual void SetBlob(const std::string &name, const Blob::Ptr &data)
Set input/output data to infer.
virtual Blob::Ptr GetBlob(const std::string &name)
Get input/output data to infer.
virtual void SetCallback(Callback callback)
Set callback function which will be called on success or failure of asynchronous request.
virtual std::vector< std::shared_ptr< IVariableStateInternal > > QueryState()
Queries memory states.
IInferRequestInternal(const InputsDataMap &networkInputs, const OutputsDataMap &networkOutputs)
Constructs a new instance.
virtual void Infer()
Infers specified input(s) in synchronous mode.
Inference Engine Plugin API namespace.
std::map< std::string, InputInfo::Ptr > InputsDataMap
std::map< std::string, DataPtr > OutputsDataMap
std::shared_ptr< Data > DataPtr
std::vector< size_t > SizeVector
details::SOPointer< IInferRequestInternal > SoIInferRequestInternal
SOPointer to IInferRequestInternal.
Definition: ie_iinfer_request_internal.hpp:248
std::map< std::string, Blob::Ptr > BlobMap
Serializes a std::vector to a std::ostream
Definition: debug.h:35