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