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