ie_layouts.h
Go to the documentation of this file.
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief A header file for data layouts and conversion between them
7  * @file ie_layouts.h
8  */
9 #pragma once
10 
11 #include <algorithm>
12 
13 #include "ie_api.h"
14 #include "ie_common.h"
15 #include "ie_precision.hpp"
16 
17 namespace InferenceEngine {
18 
19 /**
20  * @brief This class describes blocking layouts
21  */
22 class INFERENCE_ENGINE_API_CLASS(BlockingDesc) {
23 public:
24  /**
25  * @brief The default constructor which creates empty blocking descriptor
26  */
27  BlockingDesc();
28  /**
29  * @brief The constructor which allows to create blocking descriptors for standard layouts
30  * @param dims real dimensions
31  * @param layout memory layout
32  */
33  BlockingDesc(const SizeVector& dims, Layout layout);
34  /**
35  * @brief The constructor allows to create blocking descriptors for blocked memory
36  * @param blocked_dims blocked dimensions
37  * @param order the order of dimensions
38  */
39  BlockingDesc(const SizeVector& blocked_dims, const SizeVector& order);
40  /**
41  * @brief The constructor allows to create blocking descriptors for blocked memory
42  * @param blocked_dims blocked dimensions
43  * @param order the order of dimensions
44  * @param offset offset to the current memory block
45  */
46  BlockingDesc(const SizeVector& blocked_dims, const SizeVector& order, size_t offset);
47  /**
48  * @brief The constructor allows to create blocking descriptors for blocked memory
49  * @param blocked_dims blocked dimensions
50  * @param order the order of dimensions
51  * @param offset offset to the current memory block
52  * @param dimOffsets per-dimension offset from the padding to actual data,
53  */
54  BlockingDesc(const SizeVector& blocked_dims, const SizeVector& order, size_t offset, SizeVector dimOffsets);
55  /**
56  * @brief The constructor allows to create blocking descriptors for blocked memory
57  * @param blocked_dims blocked dimensions
58  * @param order the order of dimensions
59  * @param offset offset to the current memory block
60  * @param dimOffsets per-dimension offset from the padding to actual data,
61  * @param strides strides for each dimension
62  */
63  BlockingDesc(const SizeVector& blocked_dims, const SizeVector& order, size_t offset, SizeVector dimOffsets,
64  SizeVector strides);
65 
66  /**
67  * @brief Returns the blocked dimensions vector
68  * @return blocked dimensions
69  */
70  const SizeVector& getBlockDims() const {
71  return blockedDims;
72  }
73 
74  /**
75  * @brief Returns the vector of order
76  * @return order
77  */
78  const SizeVector& getOrder() const {
79  return order;
80  }
81 
82  /**
83  * @brief Returns the per-dimension offset vector
84  * @return offsets
85  */
87  return offsetPaddingToData;
88  }
89 
90  /**
91  * @brief Returns the offset to the current memory block
92  * @return offset
93  */
94  size_t getOffsetPadding() const {
95  return offsetPadding;
96  }
97 
98  /**
99  * @brief Returns strides for each dimension
100  * @return strides
101  */
102  const SizeVector& getStrides() const {
103  return strides;
104  }
105 
106  /**
107  * @brief The comparison operator for the BlockingDesc
108  * @param rhs object to compare
109  * @return true if objects are equal
110  */
111  bool operator==(const BlockingDesc& rhs) const;
112  /**
113  * @brief The comparison operator for the BlockingDesc
114  * @param rhs object to compare
115  * @return true if objects aren't equal
116  */
117  bool operator!=(const BlockingDesc& rhs) const;
118 
119 protected:
120  void fillDesc(const SizeVector& blocked_dims, const SizeVector& order);
121 
122 private:
123  /** Blocked dimensions. */
124  SizeVector blockedDims;
125  /** Strides for blocked dimensions */
126  SizeVector strides;
127  /** The order of blocked dimensions **/
128  SizeVector order;
129  /** Per-dimension offset from the padding to actual data, the top-level
130  * tensor with offsets applied must lie within the padding area. */
131  SizeVector offsetPaddingToData;
132  /** Offset from memory origin to the current block, non-zero only in
133  * a description of a memory sub-block. */
134  size_t offsetPadding;
135 };
136 
137 /**
138  * @brief This class defines Tensor description
139  */
140 class INFERENCE_ENGINE_API_CLASS(TensorDesc) {
141 public:
142  /**
143  * @brief The constructor creates the tensor descriptor using blocking descriptor
144  * @param precision memory precision
145  * @param dims memory dimensions
146  * @param blockDesc blocking descriptor
147  */
148  TensorDesc(const Precision& precision, SizeVector dims, const BlockingDesc& blockDesc);
149  /**
150  * @brief The constructor creates the tensor descriptor using standard layout
151  * @param precision memory precision
152  * @param dims memory dimensions
153  * @param layout memory layout
154  */
155  TensorDesc(const Precision& precision, SizeVector dims, Layout layout);
156  /**
157  * @brief The constructor creates the empty tensor descriptor with precision and layout
158  * @param precision memory precision
159  * @param layout memory layout
160  */
161  TensorDesc(const Precision& precision, Layout layout);
162  /**
163  * @brief The default constructor which creates empty tensor descriptor
164  */
165  TensorDesc();
166 
167  /**
168  * @brief Reshapes the tensor descriptor
169  * @param dims new dimensions
170  * @param layout new layout if it is necessary
171  */
172  void reshape(const SizeVector& dims, Layout layout = Layout::ANY);
173  /**
174  * @brief Reshapes the tensor descriptor
175  * @param dims new dimensions
176  * @param blockDesc new blocking descriptor
177  */
178  void reshape(const SizeVector& dims, const BlockingDesc& blockDesc);
179 
180  /**
181  * @brief Returns the vector of dimensions
182  * @return dimensions
183  */
185  return dims;
186  }
187  /**
188  * @brief Returns the constant vector of dimensions
189  * @return dimensions
190  */
191  const SizeVector& getDims() const noexcept {
192  return dims;
193  }
194  /**
195  * @brief Sets dimensions
196  * @param dims new dimensions
197  */
198  void setDims(const SizeVector& dims);
199 
200  /**
201  * @brief Returns the memory layout
202  * @return layout
203  */
204  Layout getLayout() const {
205  return layout;
206  }
207 
208  /**
209  * @brief Sets the layout
210  * @param l memory layout
211  */
212  void setLayout(Layout l) {
213  bool inconsistentLayout = true;
214  switch (l) {
215  case Layout::SCALAR:
216  inconsistentLayout = !dims.empty();
217  break;
218  case Layout::C:
219  inconsistentLayout = dims.size() != 1;
220  break;
221  case Layout::BLOCKED:
222  case Layout::ANY:
223  inconsistentLayout = false;
224  break;
225  case Layout::GOIDHW:
226  inconsistentLayout = dims.size() != 6;
227  break;
228  case Layout::NCDHW:
229  case Layout::NDHWC:
230  case Layout::OIDHW:
231  case Layout::GOIHW:
232  inconsistentLayout = dims.size() != 5;
233  break;
234  case Layout::OIHW:
235  case Layout::NCHW:
236  case Layout::NHWC:
237  inconsistentLayout = dims.size() != 4;
238  break;
239  case Layout::CHW:
240  inconsistentLayout = dims.size() != 3;
241  break;
242  case Layout::CN:
243  case Layout::NC:
244  case Layout::HW:
245  inconsistentLayout = dims.size() != 2;
246  break;
247  default:
248  break;
249  }
250  if (inconsistentLayout)
251  THROW_IE_EXCEPTION << "Size of dims(" << std::to_string(dims.size()) << ") and format(" << l
252  << ") are inconsistent.";
253  layout = l;
254  }
255 
256  /**
257  * @brief Returns the memory precision
258  * @return precision
259  */
260  const Precision& getPrecision() const {
261  return precision;
262  }
263 
264  /**
265  * @brief Sets the memory precision
266  * @param p precision
267  */
268  void setPrecision(const Precision& p) {
269  precision = p;
270  }
271 
272  /**
273  * @brief Returns the blocking descriptor
274  * @return blocking descriptor
275  */
276  const BlockingDesc& getBlockingDesc() const {
277  return blockingDesc;
278  }
279 
280  /**
281  * @brief The comparison operator for the TensorDesc
282  * @param rhs object to compare
283  * @return true if objects are equal
284  */
285  bool operator==(const TensorDesc& rhs) const;
286  /**
287  * @brief The comparison operator for the TensorDesc
288  * @param rhs object to compare
289  * @return true if objects aren't equal
290  */
291  bool operator!=(const TensorDesc& rhs) const;
292 
293  /**
294  * @brief Calculates offset for the vector of dimensions
295  * @param v vector of dimensions
296  * @return offset
297  */
298  size_t offset(const SizeVector& v) const;
299  /**
300  * @brief Calculates offset for the local offset
301  * @param l local offset
302  * @return offset
303  */
304  size_t offset(size_t l) const;
305 
306  /**
307  * @brief Returns the standard layout for dimensions
308  * @param dims the vector of dimensions
309  * @return the standard memory layout
310  */
311  static Layout getLayoutByDims(SizeVector dims);
312 
313 private:
314  /**
315  * Memory layout
316  */
317  Layout layout;
318  /**
319  * @brief blob's dimensions
320  */
321  SizeVector dims;
322  /**
323  * @brief memory precision
324  */
325  Precision precision;
326  /**
327  * Detailed information about layout construction
328  */
329  BlockingDesc blockingDesc;
330 };
331 
332 } // namespace InferenceEngine
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:24
Layout
Layouts that the inference engine supports.
Definition: ie_common.h:77
Layout getLayout() const
Returns the memory layout.
Definition: ie_layouts.h:204
Inference Engine API.
Definition: ie_argmax_layer.hpp:11
const BlockingDesc & getBlockingDesc() const
Returns the blocking descriptor.
Definition: ie_layouts.h:276
void setLayout(Layout l)
Sets the layout.
Definition: ie_layouts.h:212
const SizeVector & getDims() const noexcept
Returns the constant vector of dimensions.
Definition: ie_layouts.h:191
const SizeVector & getStrides() const
Returns strides for each dimension.
Definition: ie_layouts.h:102
This class defines Tensor description.
Definition: ie_layouts.h:140
SizeVector & getDims()
Returns the vector of dimensions.
Definition: ie_layouts.h:184
A header file that provides class for describing precision of data.
const SizeVector & getBlockDims() const
Returns the blocked dimensions vector.
Definition: ie_layouts.h:70
size_t getOffsetPadding() const
Returns the offset to the current memory block.
Definition: ie_layouts.h:94
The macro defines a symbol import/export mechanism essential for Microsoft Windows(R) OS...
const SizeVector & getOrder() const
Returns the vector of order.
Definition: ie_layouts.h:78
std::vector< size_t > SizeVector
Represents tensor size. The order is opposite to the order in Caffe*: (w,h,n,b) where the most freque...
Definition: ie_common.h:27
This class describes blocking layouts.
Definition: ie_layouts.h:22
const SizeVector & getOffsetPaddingToData() const
Returns the per-dimension offset vector.
Definition: ie_layouts.h:86
const Precision & getPrecision() const
Returns the memory precision.
Definition: ie_layouts.h:260
This is a header file with common inference engine definitions.
This class holds precision value and provides precision related operations.
Definition: ie_precision.hpp:21
void setPrecision(const Precision &p)
Sets the memory precision.
Definition: ie_layouts.h:268