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 opearation
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 inline std::ostream& operator<<(std::ostream& out, const Layout& p) {
95  switch (p) {
96 #define PRINT_LAYOUT(name) \
97  case name: \
98  out << #name; \
99  break;
100 
101  PRINT_LAYOUT(ANY);
102  PRINT_LAYOUT(NCHW);
103  PRINT_LAYOUT(NHWC);
104  PRINT_LAYOUT(NCDHW);
105  PRINT_LAYOUT(NDHWC);
106  PRINT_LAYOUT(OIHW);
107  PRINT_LAYOUT(C);
108  PRINT_LAYOUT(CHW);
109  PRINT_LAYOUT(HW);
110  PRINT_LAYOUT(NC);
111  PRINT_LAYOUT(CN);
112  PRINT_LAYOUT(BLOCKED);
113 #undef PRINT_LAYOUT
114  default:
115  out << static_cast<int>(p);
116  break;
117  }
118  return out;
119 }
120 
121 /**
122  * @enum ColorFormat
123  * @brief Extra information about input color format for preprocessing
124  */
125 enum ColorFormat : uint32_t {
126  RAW = 0u, ///< Plain blob (default), no extra color processing required
127  RGB, ///< RGB color format
128  BGR, ///< BGR color format, default in DLDT
129  RGBX, ///< RGBX color format with X ignored during inference
130  BGRX, ///< BGRX color format with X ignored during inference
131  NV12, ///< NV12 color format represented as compound Y+UV blob
132  I420, ///< I420 color format represented as compound Y+U+V blob
133 };
134 inline std::ostream& operator<<(std::ostream& out, const ColorFormat& fmt) {
135  switch (fmt) {
136 #define PRINT_COLOR_FORMAT(name) \
137  case name: \
138  out << #name; \
139  break;
140 
141  PRINT_COLOR_FORMAT(RAW);
142  PRINT_COLOR_FORMAT(RGB);
143  PRINT_COLOR_FORMAT(BGR);
144  PRINT_COLOR_FORMAT(RGBX);
145  PRINT_COLOR_FORMAT(BGRX);
146  PRINT_COLOR_FORMAT(NV12);
147  PRINT_COLOR_FORMAT(I420);
148 #undef PRINT_COLOR_FORMAT
149 
150  default:
151  out << static_cast<uint32_t>(fmt);
152  break;
153  }
154  return out;
155 }
156 
157 /**
158  * @struct InferenceEngineProfileInfo
159  * @brief Represents basic inference profiling information per layer.
160  *
161  * If the layer is executed using tiling, the sum time per each tile is indicated as the total execution time.
162  * Due to parallel execution, the total execution time for all layers might be greater than the total inference time.
163  */
165  /**
166  * @brief Defines the general status of the layer
167  */
168  enum LayerStatus {
169  NOT_RUN, //!< A layer is not exectued
170  OPTIMIZED_OUT, //!< A layer is optimized out during graph optimization phase
171  EXECUTED //!< A layer is executed
172  };
173 
174  /**
175  * @brief Defines a layer status
176  */
178 
179  /**
180  * @brief The absolute time in microseconds that the layer ran (in total)
181  */
182  long long realTime_uSec;
183  /**
184  * @brief The net host cpu time that the layer ran
185  */
186  long long cpu_uSec;
187 
188  /**
189  * @brief An execution type of unit
190  */
191  char exec_type[256] = {};
192 
193  /**
194  * @brief A layer type
195  */
196  char layer_type[256] = {};
197 
198  /**
199  * @brief An execution index of the unit
200  */
201  unsigned execution_index;
202 };
203 
204 /**
205  * @enum StatusCode
206  * @brief This enum contains codes for all possible return values of the interface functions
207  */
208 enum StatusCode : int {
209  OK = 0,
210  GENERAL_ERROR = -1,
211  NOT_IMPLEMENTED = -2,
212  NETWORK_NOT_LOADED = -3,
213  PARAMETER_MISMATCH = -4,
214  NOT_FOUND = -5,
215  OUT_OF_BOUNDS = -6,
216  /*
217  * @brief exception not of std::exception derived type was thrown
218  */
219  UNEXPECTED = -7,
220  REQUEST_BUSY = -8,
221  RESULT_NOT_READY = -9,
222  NOT_ALLOCATED = -10,
223  INFER_NOT_STARTED = -11,
224  NETWORK_NOT_READ = -12
225 };
226 
227 /**
228  * @struct ResponseDesc
229  * @brief Represents detailed information for an error
230  */
231 struct ResponseDesc {
232  /**
233  * @brief A character buffer that holds the detailed information for an error.
234  */
235  char msg[4096] = {};
236 };
237 
238 
239 /**
240  * @brief Responce structure encapsulating information about supported layer
241  */
243  /**
244  * @brief A map of supported layers:
245  * - key - a layer name
246  * - value - a device name on which layer is assigned
247  */
248  std::map<std::string, std::string> supportedLayersMap;
249 
250  /**
251  * @brief A status code
252  */
254 
255  /**
256  * @brief Response message
257  */
259 };
260 
261 /** @brief This class represents StatusCode::GENERIC_ERROR exception */
262 class GeneralError : public std::logic_error {
263  using std::logic_error::logic_error;
264 };
265 
266 /** @brief This class represents StatusCode::NOT_IMPLEMENTED exception */
267 class NotImplemented : public std::logic_error {
268  using std::logic_error::logic_error;
269 };
270 
271 /** @brief This class represents StatusCode::NETWORK_NOT_LOADED exception */
272 class NetworkNotLoaded : public std::logic_error {
273  using std::logic_error::logic_error;
274 };
275 
276 /** @brief This class represents StatusCode::PARAMETER_MISMATCH exception */
277 class ParameterMismatch : public std::logic_error {
278  using std::logic_error::logic_error;
279 };
280 
281 /** @brief This class represents StatusCode::NOT_FOUND exception */
282 class NotFound : public std::logic_error {
283  using std::logic_error::logic_error;
284 };
285 
286 /** @brief This class represents StatusCode::OUT_OF_BOUNDS exception */
287 class OutOfBounds : public std::logic_error {
288  using std::logic_error::logic_error;
289 };
290 
291 /** @brief This class represents StatusCode::UNEXPECTED exception */
292 class Unexpected : public std::logic_error {
293  using std::logic_error::logic_error;
294 };
295 
296 /** @brief This class represents StatusCode::REQUEST_BUSY exception */
297 class RequestBusy : public std::logic_error {
298  using std::logic_error::logic_error;
299 };
300 
301 /** @brief This class represents StatusCode::RESULT_NOT_READY exception */
302 class ResultNotReady : public std::logic_error {
303  using std::logic_error::logic_error;
304 };
305 
306 /** @brief This class represents StatusCode::NOT_ALLOCATED exception */
307 class NotAllocated : public std::logic_error {
308  using std::logic_error::logic_error;
309 };
310 
311 /** @brief This class represents StatusCode::INFER_NOT_STARTED exception */
312 class InferNotStarted : public std::logic_error {
313  using std::logic_error::logic_error;
314 };
315 } // namespace InferenceEngine
316 
317 /** @brief This class represents StatusCode::NETWORK_NOT_READ exception */
318 class NetworkNotRead : public std::logic_error {
319  using std::logic_error::logic_error;
320 };
321 
322 #if defined(_WIN32)
323 #define __PRETTY_FUNCTION__ __FUNCSIG__
324 #else
325 #define __PRETTY_FUNCTION__ __PRETTY_FUNCTION__
326 #endif
InferenceEngine::Data
This class represents the main Data representation node.
Definition: ie_data.h:28
InferenceEngine::InferenceEngineProfileInfo::LayerStatus
LayerStatus
Defines the general status of the layer.
Definition: ie_common.h:168
InferenceEngine::InferenceEngineProfileInfo::layer_type
char layer_type[256]
A layer type.
Definition: ie_common.h:196
InferenceEngine::QueryNetworkResult::supportedLayersMap
std::map< std::string, std::string > supportedLayersMap
A map of supported layers:
Definition: ie_common.h:248
InferenceEngine::NetworkNotLoaded
This class represents StatusCode::NETWORK_NOT_LOADED exception.
Definition: ie_common.h:272
InferenceEngine::InferenceEngineProfileInfo::execution_index
unsigned execution_index
An execution index of the unit.
Definition: ie_common.h:201
InferenceEngine::NotImplemented
This class represents StatusCode::NOT_IMPLEMENTED exception.
Definition: ie_common.h:267
InferenceEngine::ResponseDesc
Represents detailed information for an error.
Definition: ie_common.h:231
InferenceEngine::InferenceEngineProfileInfo::OPTIMIZED_OUT
@ OPTIMIZED_OUT
A layer is optimized out during graph optimization phase.
Definition: ie_common.h:170
RGB
@ RGB
RGB color format.
Definition: ie_c_api.h:206
InferenceEngine::RequestBusy
This class represents StatusCode::REQUEST_BUSY exception.
Definition: ie_common.h:297
InferenceEngine::InferenceEngineProfileInfo
Represents basic inference profiling information per layer.
Definition: ie_common.h:164
RAW
@ RAW
Plain blob (default), no extra color processing required.
Definition: ie_c_api.h:205
InferenceEngine::InferenceEngineProfileInfo::exec_type
char exec_type[256]
An execution type of unit.
Definition: ie_common.h:191
I420
@ I420
I420 color format represented as compound Y+U+V blob.
Definition: ie_c_api.h:211
BGRX
@ BGRX
BGRX color format with X ignored during inference.
Definition: ie_c_api.h:209
InferenceEngine::StatusCode
StatusCode
This enum contains codes for all possible return values of the interface functions.
Definition: ie_common.h:208
BGR
@ BGR
BGR color format, default in DLDT.
Definition: ie_c_api.h:207
NV12
@ NV12
NV12 color format represented as compound Y+UV blob.
Definition: ie_c_api.h:210
InferenceEngine::InferenceEngineProfileInfo::EXECUTED
@ EXECUTED
A layer is executed.
Definition: ie_common.h:171
InferenceEngine::InferenceEngineProfileInfo::realTime_uSec
long long realTime_uSec
The absolute time in microseconds that the layer ran (in total)
Definition: ie_common.h:182
RGBX
@ RGBX
RGBX color format with X ignored during inference.
Definition: ie_c_api.h:208
InferenceEngine::GeneralError
This class represents StatusCode::GENERIC_ERROR exception.
Definition: ie_common.h:262
InferenceEngine::InferNotStarted
This class represents StatusCode::INFER_NOT_STARTED exception.
Definition: ie_common.h:312
InferenceEngine::QueryNetworkResult::rc
StatusCode rc
A status code.
Definition: ie_common.h:253
InferenceEngine::InferenceEngineProfileInfo::status
LayerStatus status
Defines a layer status.
Definition: ie_common.h:177
InferenceEngine::Unexpected
This class represents StatusCode::UNEXPECTED exception.
Definition: ie_common.h:292
InferenceEngine::GOIDHW
@ GOIDHW
NDHWC layout for operation weights.
Definition: ie_common.h:76
InferenceEngine::OutOfBounds
This class represents StatusCode::OUT_OF_BOUNDS exception.
Definition: ie_common.h:287
InferenceEngine::CDataPtr
std::shared_ptr< const Data > CDataPtr
Smart pointer to constant Data.
Definition: ie_common.h:42
InferenceEngine::SizeVector
std::vector< size_t > SizeVector
Represents tensor size.
Definition: ie_common.h:27
InferenceEngine::ResultNotReady
This class represents StatusCode::RESULT_NOT_READY exception.
Definition: ie_common.h:302
InferenceEngine::ResponseDesc::msg
char msg[4096]
A character buffer that holds the detailed information for an error.
Definition: ie_common.h:235
InferenceEngine::InferenceEngineProfileInfo::cpu_uSec
long long cpu_uSec
The net host cpu time that the layer ran.
Definition: ie_common.h:186
InferenceEngine::NotFound
This class represents StatusCode::NOT_FOUND exception.
Definition: ie_common.h:282
InferenceEngine::UserValue::v_ptr
void * v_ptr
A pointer to a void.
Definition: ie_common.h:56
InferenceEngine::OIDHW
@ OIDHW
NDHWC layout for operation weights.
Definition: ie_common.h:75
InferenceEngine::InferenceEngineProfileInfo::NOT_RUN
@ NOT_RUN
A layer is not exectued.
Definition: ie_common.h:169
InferenceEngine::QueryNetworkResult::resp
ResponseDesc resp
Response message.
Definition: ie_common.h:258
InferenceEngine::DataPtr
std::shared_ptr< Data > DataPtr
Smart pointer to Data.
Definition: ie_common.h:37
InferenceEngine::ColorFormat
ColorFormat
Extra information about input color format for preprocessing.
Definition: ie_common.h:125
InferenceEngine::GOIHW
@ GOIHW
NDHWC layout for operation weights.
Definition: ie_common.h:74
InferenceEngine::UserValue::v_int
int v_int
An integer value.
Definition: ie_common.h:54
InferenceEngine::ParameterMismatch
This class represents StatusCode::PARAMETER_MISMATCH exception.
Definition: ie_common.h:277
InferenceEngine::UserValue::v_float
float v_float
A floating point value.
Definition: ie_common.h:55
InferenceEngine::QueryNetworkResult
Responce structure encapsulating information about supported layer.
Definition: ie_common.h:242
InferenceEngine::Layout
Layout
Layouts that the inference engine supports.
Definition: ie_common.h:63
InferenceEngine::DataWeakPtr
std::weak_ptr< Data > DataWeakPtr
Smart weak pointer to Data.
Definition: ie_common.h:47
InferenceEngine::UserValue
The method holds the user values to enable binding of data per graph node.
Definition: ie_common.h:53
NetworkNotRead
This class represents StatusCode::NETWORK_NOT_READ exception.
Definition: ie_common.h:318
InferenceEngine::NotAllocated
This class represents StatusCode::NOT_ALLOCATED exception.
Definition: ie_common.h:307