ie_common.h
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 This is a header file with common inference engine definitions.
7  *
8  * @file ie_common.h
9  */
10 #pragma once
11 
12 #include <algorithm>
13 #include <cstdlib>
14 #include <memory>
15 #include <ostream>
16 #include <string>
17 #include <vector>
18 #include <map>
19 
20 namespace InferenceEngine {
21 /**
22  * @brief Represents tensor size.
23  *
24  * The order is opposite to the order in Caffe*: (w,h,n,b) where the most frequently changing element in memory is
25  * first.
26  */
27 using SizeVector = std::vector<size_t>;
28 
29 /**
30  * @brief The main data representation node
31  */
32 class Data;
33 
34 /**
35  * @brief Smart pointer to Data
36  */
37 using DataPtr = std::shared_ptr<Data>;
38 
39 /**
40  * @brief Smart pointer to constant Data
41  */
42 using CDataPtr = std::shared_ptr<const Data>;
43 
44 /**
45  * @brief Smart weak pointer to Data
46  */
47 using DataWeakPtr = std::weak_ptr<Data>;
48 
49 /**
50  * @union UserValue
51  * @brief The method holds the user values to enable binding of data per graph node.
52  */
53 union UserValue {
54  int v_int; //!< An integer value
55  float v_float; //!< A floating point value
56  void* v_ptr; //!< A pointer to a void
57 };
58 
59 /**
60  * @enum Layout
61  * @brief Layouts that the inference engine supports
62  */
63 enum Layout : uint8_t {
64  ANY = 0, //!< "any" layout
65 
66  // I/O data layouts
67  NCHW = 1, //!< NCHW layout for input / output blobs
68  NHWC = 2, //!< NHWC layout for input / output blobs
69  NCDHW = 3, //!< NCDHW layout for input / output blobs
70  NDHWC = 4, //!< NDHWC layout for input / output blobs
71 
72  // weight layouts
73  OIHW = 64, //!< NDHWC layout for operation weights
74  GOIHW = 65, //!< NDHWC layout for operation weights
75  OIDHW = 66, //!< NDHWC layout for operation weights
76  GOIDHW = 67, //!< NDHWC layout for operation weights
77 
78  // Scalar
79  SCALAR = 95, //!< A scalar layout
80 
81  // bias layouts
82  C = 96, //!< A bias layout for operation
83 
84  // Single image layouts
85  CHW = 128, //!< A single image layout (e.g. for mean image)
86 
87  // 2D
88  HW = 192, //!< HW 2D layout
89  NC = 193, //!< HC 2D layout
90  CN = 194, //!< CN 2D layout
91 
92  BLOCKED = 200, //!< A blocked layout
93 };
94 
95 /**
96  * @brief Prints a string representation of InferenceEngine::Layout to a stream
97  * @param out An output stream to send to
98  * @param p A layout value to print to a stream
99  * @return A reference to the `out` stream
100  */
101 inline std::ostream& operator<<(std::ostream& out, const Layout& p) {
102  switch (p) {
103 #define PRINT_LAYOUT(name) \
104  case name: \
105  out << #name; \
106  break;
107 
108  PRINT_LAYOUT(ANY);
109  PRINT_LAYOUT(NCHW);
110  PRINT_LAYOUT(NHWC);
111  PRINT_LAYOUT(NCDHW);
112  PRINT_LAYOUT(NDHWC);
113  PRINT_LAYOUT(OIHW);
114  PRINT_LAYOUT(C);
115  PRINT_LAYOUT(CHW);
116  PRINT_LAYOUT(HW);
117  PRINT_LAYOUT(NC);
118  PRINT_LAYOUT(CN);
119  PRINT_LAYOUT(BLOCKED);
120 #undef PRINT_LAYOUT
121  default:
122  out << static_cast<int>(p);
123  break;
124  }
125  return out;
126 }
127 
128 /**
129  * @enum ColorFormat
130  * @brief Extra information about input color format for preprocessing
131  */
132 enum ColorFormat : uint32_t {
133  RAW = 0u, ///< Plain blob (default), no extra color processing required
134  RGB, ///< RGB color format
135  BGR, ///< BGR color format, default in DLDT
136  RGBX, ///< RGBX color format with X ignored during inference
137  BGRX, ///< BGRX color format with X ignored during inference
138  NV12, ///< NV12 color format represented as compound Y+UV blob
139  I420, ///< I420 color format represented as compound Y+U+V blob
140 };
141 
142 /**
143  * @brief Prints a string representation of InferenceEngine::ColorFormat to a stream
144  * @param out An output stream to send to
145  * @param fmt A color format value to print to a stream
146  * @return A reference to the `out` stream
147  */
148 inline std::ostream& operator<<(std::ostream& out, const ColorFormat& fmt) {
149  switch (fmt) {
150 #define PRINT_COLOR_FORMAT(name) \
151  case name: \
152  out << #name; \
153  break;
154 
155  PRINT_COLOR_FORMAT(RAW);
156  PRINT_COLOR_FORMAT(RGB);
157  PRINT_COLOR_FORMAT(BGR);
158  PRINT_COLOR_FORMAT(RGBX);
159  PRINT_COLOR_FORMAT(BGRX);
160  PRINT_COLOR_FORMAT(NV12);
161  PRINT_COLOR_FORMAT(I420);
162 #undef PRINT_COLOR_FORMAT
163 
164  default:
165  out << static_cast<uint32_t>(fmt);
166  break;
167  }
168  return out;
169 }
170 
171 /**
172  * @struct InferenceEngineProfileInfo
173  * @brief Represents basic inference profiling information per layer.
174  *
175  * If the layer is executed using tiling, the sum time per each tile is indicated as the total execution time.
176  * Due to parallel execution, the total execution time for all layers might be greater than the total inference time.
177  */
179  /**
180  * @brief Defines the general status of the layer
181  */
182  enum LayerStatus {
183  NOT_RUN, //!< A layer is not executed
184  OPTIMIZED_OUT, //!< A layer is optimized out during graph optimization phase
185  EXECUTED //!< A layer is executed
186  };
187 
188  /**
189  * @brief Defines a layer status
190  */
192 
193  /**
194  * @brief The absolute time in microseconds that the layer ran (in total)
195  */
196  long long realTime_uSec;
197  /**
198  * @brief The net host cpu time that the layer ran
199  */
200  long long cpu_uSec;
201 
202  /**
203  * @brief An execution type of unit
204  */
205  char exec_type[256] = {};
206 
207  /**
208  * @brief A layer type
209  */
210  char layer_type[256] = {};
211 
212  /**
213  * @brief An execution index of the unit
214  */
215  unsigned execution_index;
216 };
217 
218 /**
219  * @enum StatusCode
220  * @brief This enum contains codes for all possible return values of the interface functions
221  */
222 enum StatusCode : int {
223  OK = 0,
224  GENERAL_ERROR = -1,
225  NOT_IMPLEMENTED = -2,
226  NETWORK_NOT_LOADED = -3,
227  PARAMETER_MISMATCH = -4,
228  NOT_FOUND = -5,
229  OUT_OF_BOUNDS = -6,
230  /*
231  * @brief exception not of std::exception derived type was thrown
232  */
233  UNEXPECTED = -7,
234  REQUEST_BUSY = -8,
235  RESULT_NOT_READY = -9,
236  NOT_ALLOCATED = -10,
237  INFER_NOT_STARTED = -11,
238  NETWORK_NOT_READ = -12
239 };
240 
241 /**
242  * @struct ResponseDesc
243  * @brief Represents detailed information for an error
244  */
245 struct ResponseDesc {
246  /**
247  * @brief A character buffer that holds the detailed information for an error.
248  */
249  char msg[4096] = {};
250 };
251 
252 /**
253  * @brief Response structure encapsulating information about supported layer
254  */
256  /**
257  * @brief A map of supported layers:
258  * - key - a layer name
259  * - value - a device name on which layer is assigned
260  */
261  std::map<std::string, std::string> supportedLayersMap;
262 
263  /**
264  * @brief A status code
265  */
267 
268  /**
269  * @brief Response message
270  */
272 };
273 
274 /** @brief This class represents StatusCode::GENERIC_ERROR exception */
275 class GeneralError : public std::logic_error {
276  using std::logic_error::logic_error;
277 };
278 
279 /** @brief This class represents StatusCode::NOT_IMPLEMENTED exception */
280 class NotImplemented : public std::logic_error {
281  using std::logic_error::logic_error;
282 };
283 
284 /** @brief This class represents StatusCode::NETWORK_NOT_LOADED exception */
285 class NetworkNotLoaded : public std::logic_error {
286  using std::logic_error::logic_error;
287 };
288 
289 /** @brief This class represents StatusCode::PARAMETER_MISMATCH exception */
290 class ParameterMismatch : public std::logic_error {
291  using std::logic_error::logic_error;
292 };
293 
294 /** @brief This class represents StatusCode::NOT_FOUND exception */
295 class NotFound : public std::logic_error {
296  using std::logic_error::logic_error;
297 };
298 
299 /** @brief This class represents StatusCode::OUT_OF_BOUNDS exception */
300 class OutOfBounds : public std::logic_error {
301  using std::logic_error::logic_error;
302 };
303 
304 /** @brief This class represents StatusCode::UNEXPECTED exception */
305 class Unexpected : public std::logic_error {
306  using std::logic_error::logic_error;
307 };
308 
309 /** @brief This class represents StatusCode::REQUEST_BUSY exception */
310 class RequestBusy : public std::logic_error {
311  using std::logic_error::logic_error;
312 };
313 
314 /** @brief This class represents StatusCode::RESULT_NOT_READY exception */
315 class ResultNotReady : public std::logic_error {
316  using std::logic_error::logic_error;
317 };
318 
319 /** @brief This class represents StatusCode::NOT_ALLOCATED exception */
320 class NotAllocated : public std::logic_error {
321  using std::logic_error::logic_error;
322 };
323 
324 /** @brief This class represents StatusCode::INFER_NOT_STARTED exception */
325 class InferNotStarted : public std::logic_error {
326  using std::logic_error::logic_error;
327 };
328 
329 /** @brief This class represents StatusCode::NETWORK_NOT_READ exception */
330 class NetworkNotRead : public std::logic_error {
331  using std::logic_error::logic_error;
332 };
333 
334 } // namespace InferenceEngine
335 
336 #if defined(_WIN32)
337 #define __PRETTY_FUNCTION__ __FUNCSIG__
338 #else
339 #define __PRETTY_FUNCTION__ __PRETTY_FUNCTION__
340 #endif
This class represents the main Data representation node.
Definition: ie_data.h:28
This class represents StatusCode::GENERIC_ERROR exception.
Definition: ie_common.h:275
This class represents StatusCode::INFER_NOT_STARTED exception.
Definition: ie_common.h:325
This class represents StatusCode::NETWORK_NOT_LOADED exception.
Definition: ie_common.h:285
This class represents StatusCode::NETWORK_NOT_READ exception.
Definition: ie_common.h:330
This class represents StatusCode::NOT_ALLOCATED exception.
Definition: ie_common.h:320
This class represents StatusCode::NOT_FOUND exception.
Definition: ie_common.h:295
This class represents StatusCode::NOT_IMPLEMENTED exception.
Definition: ie_common.h:280
This class represents StatusCode::OUT_OF_BOUNDS exception.
Definition: ie_common.h:300
This class represents StatusCode::PARAMETER_MISMATCH exception.
Definition: ie_common.h:290
This class represents StatusCode::REQUEST_BUSY exception.
Definition: ie_common.h:310
This class represents StatusCode::RESULT_NOT_READY exception.
Definition: ie_common.h:315
This class represents StatusCode::UNEXPECTED exception.
Definition: ie_common.h:305
@ BGRX
BGRX color format with X ignored during inference.
Definition: ie_c_api.h:209
@ RGB
RGB color format.
Definition: ie_c_api.h:206
@ BGR
BGR color format, default in DLDT.
Definition: ie_c_api.h:207
@ RGBX
RGBX color format with X ignored during inference.
Definition: ie_c_api.h:208
@ NV12
NV12 color format represented as compound Y+UV blob.
Definition: ie_c_api.h:210
@ RAW
Plain blob (default), no extra color processing required.
Definition: ie_c_api.h:205
@ I420
I420 color format represented as compound Y+U+V blob.
Definition: ie_c_api.h:211
@ BLOCKED
"BLOCKED" layout
Definition: ie_c_api.h:165
@ NC
"NC" layout
Definition: ie_c_api.h:162
@ NCHW
"NCHW" layout
Definition: ie_c_api.h:143
@ CN
"CN" layout
Definition: ie_c_api.h:163
@ C
"C" layout
Definition: ie_c_api.h:155
@ NHWC
"NHWC" layout
Definition: ie_c_api.h:144
@ OIHW
"OIHW" layout
Definition: ie_c_api.h:149
@ SCALAR
"SCALAR" layout
Definition: ie_c_api.h:152
@ CHW
"CHW" layout
Definition: ie_c_api.h:158
@ ANY
"ANY" layout
Definition: ie_c_api.h:140
@ HW
"HW" layout
Definition: ie_c_api.h:161
@ NCDHW
"NCDHW" layout
Definition: ie_c_api.h:145
@ NDHWC
"NDHWC" layout
Definition: ie_c_api.h:146
std::shared_ptr< const Data > CDataPtr
Smart pointer to constant Data.
Definition: ie_common.h:42
Layout
Layouts that the inference engine supports.
Definition: ie_common.h:63
@ OIDHW
NDHWC layout for operation weights.
Definition: ie_common.h:75
@ GOIHW
NDHWC layout for operation weights.
Definition: ie_common.h:74
@ GOIDHW
NDHWC layout for operation weights.
Definition: ie_common.h:76
StatusCode
This enum contains codes for all possible return values of the interface functions.
Definition: ie_common.h:222
std::ostream & operator<<(std::ostream &out, const Layout &p)
Prints a string representation of InferenceEngine::Layout to a stream.
Definition: ie_common.h:101
ColorFormat
Extra information about input color format for preprocessing.
Definition: ie_common.h:132
std::shared_ptr< Data > DataPtr
Smart pointer to Data.
Definition: ie_common.h:37
std::vector< size_t > SizeVector
Represents tensor size.
Definition: ie_common.h:27
std::weak_ptr< Data > DataWeakPtr
Smart weak pointer to Data.
Definition: ie_common.h:47
Represents basic inference profiling information per layer.
Definition: ie_common.h:178
long long cpu_uSec
The net host cpu time that the layer ran.
Definition: ie_common.h:200
LayerStatus status
Defines a layer status.
Definition: ie_common.h:191
long long realTime_uSec
The absolute time in microseconds that the layer ran (in total)
Definition: ie_common.h:196
char layer_type[256]
A layer type.
Definition: ie_common.h:210
LayerStatus
Defines the general status of the layer.
Definition: ie_common.h:182
@ NOT_RUN
A layer is not executed.
Definition: ie_common.h:183
@ OPTIMIZED_OUT
A layer is optimized out during graph optimization phase.
Definition: ie_common.h:184
@ EXECUTED
A layer is executed.
Definition: ie_common.h:185
unsigned execution_index
An execution index of the unit.
Definition: ie_common.h:215
char exec_type[256]
An execution type of unit.
Definition: ie_common.h:205
Response structure encapsulating information about supported layer.
Definition: ie_common.h:255
StatusCode rc
A status code.
Definition: ie_common.h:266
ResponseDesc resp
Response message.
Definition: ie_common.h:271
std::map< std::string, std::string > supportedLayersMap
A map of supported layers:
Definition: ie_common.h:261
Represents detailed information for an error.
Definition: ie_common.h:245
char msg[4096]
A character buffer that holds the detailed information for an error.
Definition: ie_common.h:249
The method holds the user values to enable binding of data per graph node.
Definition: ie_common.h:53
void * v_ptr
A pointer to a void.
Definition: ie_common.h:56
float v_float
A floating point value.
Definition: ie_common.h:55
int v_int
An integer value.
Definition: ie_common.h:54