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, SizeVector strides);
64  /**
65  * @brief Destructor
66  */
67  virtual ~BlockingDesc() = default;
68 
69  /**
70  * @brief Returns the blocked dimensions vector
71  * @return blocked dimensions
72  */
73  const SizeVector& getBlockDims() const {
74  return blockedDims;
75  }
76 
77  /**
78  * @brief Returns the vector of order
79  * @return order
80  */
81  const SizeVector& getOrder() const {
82  return order;
83  }
84 
85  /**
86  * @brief Returns the per-dimension offset vector
87  * @return offsets
88  */
90  return offsetPaddingToData;
91  }
92 
93  /**
94  * @brief Returns the offset to the current memory block
95  * @return offset
96  */
97  size_t getOffsetPadding() const {
98  return offsetPadding;
99  }
100 
101  /**
102  * @brief Returns strides for each dimension
103  * @return strides
104  */
105  const SizeVector& getStrides() const {
106  return strides;
107  }
108 
109  /**
110  * @brief The comparison operator for the BlockingDesc
111  * @param rhs object to compare
112  * @return true if objects are equal
113  */
114  bool operator == (const BlockingDesc& rhs) const;
115  /**
116  * @brief The comparison operator for the BlockingDesc
117  * @param rhs object to compare
118  * @return true if objects aren't equal
119  */
120  bool operator != (const BlockingDesc& rhs) const;
121 
122 protected:
123  void fillDesc(const SizeVector& blocked_dims, const SizeVector& order);
124 
125 private:
126  /** Blocked dimensions. */
127  SizeVector blockedDims;
128  /** Strides for blocked dimensions */
129  SizeVector strides;
130  /** The order of blocked dimensions **/
131  SizeVector order;
132  /** Per-dimension offset from the padding to actual data, the top-level
133  * tensor with offsets applied must lie within the padding area. */
134  SizeVector offsetPaddingToData;
135  /** Offset from memory origin to the current block, non-zero only in
136  * a description of a memory sub-block. */
137  size_t offsetPadding;
138 };
139 
140 /**
141  * @brief This class defines Tensor description
142  */
143 class INFERENCE_ENGINE_API_CLASS(TensorDesc) {
144 public:
145  /**
146  * @brief The constructor creates the tensor descriptor using blocking descriptor
147  * @param precision memory precision
148  * @param dims memory dimensions
149  * @param blockDesc blocking descriptor
150  */
151  TensorDesc(const Precision& precision, SizeVector dims, const BlockingDesc& blockDesc);
152  /**
153  * @brief The constructor creates the tensor descriptor using standard layout
154  * @param precision memory precision
155  * @param dims memory dimensions
156  * @param layout memory layout
157  */
158  TensorDesc(const Precision& precision, SizeVector dims, Layout layout);
159  /**
160  * @brief The constructor creates the empty tensor descriptor with precision and layout
161  * @param precision memory precision
162  * @param layout memory layout
163  */
164  TensorDesc(const Precision& precision, Layout layout);
165  /**
166  * @brief The default constructor which creates empty tensor descriptor
167  */
168  TensorDesc();
169  /**
170  * @brief Destructor
171  */
172  virtual ~TensorDesc() = default;
173 
174  /**
175  * @brief Reshapes the tensor descriptor
176  * @param dims new dimensions
177  * @param layout new layout if it is necessary
178  */
179  void reshape(const SizeVector &dims, Layout layout = Layout::ANY);
180  /**
181  * @brief Reshapes the tensor descriptor
182  * @param dims new dimensions
183  * @param blockDesc new blocking descriptor
184  */
185  void reshape(const SizeVector &dims, const BlockingDesc &blockDesc);
186 
187  /**
188  * @brief Returns the vector of dimensions
189  * @return dimensions
190  */
192  return dims;
193  }
194  /**
195  * @brief Returns the constant vector of dimensions
196  * @return dimensions
197  */
198  const SizeVector& getDims() const noexcept {
199  return dims;
200  }
201  /**
202  * @brief Sets dimensions
203  * @param dims new dimensions
204  */
205  void setDims(const SizeVector& dims);
206 
207  /**
208  * @brief Returns the memory layout
209  * @return layout
210  */
211  Layout getLayout() const {
212  return layout;
213  }
214 
215  /**
216  * @brief Sets the layout
217  * @param l memory layout
218  */
219  void setLayout(Layout l) {
220  bool inconsistentLayout = true;
221  switch (l) {
222  case Layout::SCALAR:
223  inconsistentLayout = !dims.empty();
224  break;
225  case Layout::C:
226  inconsistentLayout = dims.size() != 1;
227  break;
228  case Layout::BLOCKED:
229  case Layout::ANY:
230  inconsistentLayout = false;
231  break;
232  case Layout::NCDHW:
233  case Layout::NDHWC:
234  inconsistentLayout = dims.size() != 5;
235  break;
236  case Layout::OIHW:
237  case Layout::NCHW:
238  case Layout::NHWC:
239  inconsistentLayout = dims.size() != 4;
240  break;
241  case Layout::CHW:
242  inconsistentLayout = dims.size() != 3;
243  break;
244  case Layout::CN:
245  case Layout::NC:
246  case Layout::HW:
247  inconsistentLayout = dims.size() != 2;
248  break;
249  default:
250  break;
251  }
252  if (inconsistentLayout)
253  THROW_IE_EXCEPTION << "Size of dims(" << std::to_string(dims.size()) << ") and format(" << l << ") are inconsistent.";
254  layout = l;
255  }
256 
257  /**
258  * @brief Returns the memory precision
259  * @return precision
260  */
261  const Precision& getPrecision() const {
262  return precision;
263  }
264 
265  /**
266  * @brief Sets the memory precision
267  * @param p precision
268  */
269  void setPrecision(const Precision& p) {
270  precision = p;
271  }
272 
273  /**
274  * @brief Returns the blocking descriptor
275  * @return blocking descriptor
276  */
277  const BlockingDesc& getBlockingDesc() const {
278  return blockingDesc;
279  }
280 
281  /**
282  * @brief The comparison operator for the TensorDesc
283  * @param rhs object to compare
284  * @return true if objects are equal
285  */
286  bool operator == (const TensorDesc& rhs) const;
287  /**
288  * @brief The comparison operator for the TensorDesc
289  * @param rhs object to compare
290  * @return true if objects aren't equal
291  */
292  bool operator != (const TensorDesc& rhs) const;
293 
294  /**
295  * @brief Calculates offset for the vector of dimensions
296  * @param v vector of dimensions
297  * @return offset
298  */
299  size_t offset(const SizeVector& v) const;
300  /**
301  * @brief Calculates offset for the local offset
302  * @param l local offset
303  * @return offset
304  */
305  size_t offset(size_t l) const;
306 
307  /**
308  * @brief Returns the standard layout for dimensions
309  * @param dims the vector of dimensions
310  * @return the standard memory layout
311  */
312  static Layout getLayoutByDims(SizeVector dims);
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 /**
333  * @deprecated Deprecated since provides dims in reverse order
334  */
335 INFERENCE_ENGINE_DEPRECATED
336 static const size_t I_N = 3;
337 
338 /**
339  * @deprecated Deprecated since provides dims in reverse order
340  */
341 INFERENCE_ENGINE_DEPRECATED
342 static const size_t I_C = 2;
343 
344 /**
345  * @deprecated Deprecated since provides dims in reverse order
346  */
347 INFERENCE_ENGINE_DEPRECATED
348 static const size_t I_H = 1;
349 
350 /**
351  * @deprecated Deprecated since provides dims in reverse order
352  */
353 INFERENCE_ENGINE_DEPRECATED
354 static const size_t I_W = 0;
355 
356 /**
357  * @deprecated Uses TensorDesc working with layouts
358  * @brief This class helps calculating offset in different layouts
359  */
360 class INFERENCE_ENGINE_DEPRECATED INFERENCE_ENGINE_API_CLASS(LayoutOffsetCounter) {
361 private:
362  Layout _layout;
363  SizeVector _dims;
364 
365  size_t _dims_count;
366 
367  /**
368  * @brief Stores multipliers that are calculated during the LayoutOffsetCounter construction.
369  * The multipliers are used for conversion.
370  */
371  SizeVector _muls;
372 public:
373  /**
374  * @brief A default constructor
375  * @param dims Tensor dimension array (reverse NCHW order as in the IR: w,h,c,n)
376  */
377  LayoutOffsetCounter(Layout layout, SizeVector dims);
378 
379  IE_SUPPRESS_DEPRECATED_START
380  /**
381  * @brief A copy constructor
382  */
384 
385  /**
386  * @brief A copy assignment operator
387  * @param l A value to copy from
388  */
389  LayoutOffsetCounter & operator = (const LayoutOffsetCounter & l);
390  IE_SUPPRESS_DEPRECATED_END
391 
392  /**
393  * @brief A destructor
394  */
395  ~LayoutOffsetCounter();
396 
397  /**
398  * @brief Calculates an offset for the specified layout
399  * @param pos Tensor position array (reverse NCHW order as in the IR: w,h,c,n)
400  */
401  size_t Offset(SizeVector pos);
402 };
403 
404 /**
405  * @deprecated Please use TensorDesc for conversion
406  */
407 template<typename T>
408 INFERENCE_ENGINE_DEPRECATED
409 void ConvertLayout(Layout sourceLayout, Layout destLayout, const T* sourceBuffer, T* destBuffer, SizeVector dims) {
410  IE_SUPPRESS_DEPRECATED_START
411  if (dims.size() == 0) return;
412 
413  SizeVector pos(dims.size(), 0);
414  LayoutOffsetCounter srcOffsetCounter(sourceLayout, dims);
415  LayoutOffsetCounter destOffsetCounter(destLayout, dims);
416 
417  while (true) {
418  // Setting the current item
419  size_t ps = srcOffsetCounter.Offset(pos);
420  size_t pd = destOffsetCounter.Offset(pos);
421 
422  destBuffer[pd] = sourceBuffer[ps];
423 
424  // Advancing pos
425  size_t caret = 0;
426  pos[caret]++;
427  while (pos[caret] >= dims[caret]) {
428  pos[caret] = 0;
429  caret++;
430  if (caret == pos.size()) {
431  // We have finished converting
432  return;
433  }
434  pos[caret]++;
435  }
436  }
437  IE_SUPPRESS_DEPRECATED_END
438 }
439 
440 } // namespace InferenceEngine
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:22
Layout
Layouts that the inference engine supports.
Definition: ie_common.h:76
Layout getLayout() const
Returns the memory layout.
Definition: ie_layouts.h:211
Inference Engine API.
Definition: ie_argmax_layer.hpp:11
static const size_t I_N
Definition: ie_layouts.h:336
const BlockingDesc & getBlockingDesc() const
Returns the blocking descriptor.
Definition: ie_layouts.h:277
static const size_t I_W
Definition: ie_layouts.h:354
void setLayout(Layout l)
Sets the layout.
Definition: ie_layouts.h:219
This class helps calculating offset in different layouts.
Definition: ie_layouts.h:360
const SizeVector & getDims() const noexcept
Returns the constant vector of dimensions.
Definition: ie_layouts.h:198
const SizeVector & getStrides() const
Returns strides for each dimension.
Definition: ie_layouts.h:105
This class defines Tensor description.
Definition: ie_layouts.h:143
SizeVector & getDims()
Returns the vector of dimensions.
Definition: ie_layouts.h:191
A header file that provides class for describing precision of data.
const SizeVector & getBlockDims() const
Returns the blocked dimensions vector.
Definition: ie_layouts.h:73
void ConvertLayout(Layout sourceLayout, Layout destLayout, const T *sourceBuffer, T *destBuffer, SizeVector dims)
Definition: ie_layouts.h:409
static const size_t I_C
Definition: ie_layouts.h:342
size_t getOffsetPadding() const
Returns the offset to the current memory block.
Definition: ie_layouts.h:97
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:81
static const size_t I_H
Definition: ie_layouts.h:348
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:26
This class describes blocking layouts.
Definition: ie_layouts.h:22
size_t Offset(SizeVector pos)
Calculates an offset for the specified layout.
const SizeVector & getOffsetPaddingToData() const
Returns the per-dimension offset vector.
Definition: ie_layouts.h:89
const Precision & getPrecision() const
Returns the memory precision.
Definition: ie_layouts.h:261
This is a header file with common inference engine definitions.
This class holds precision value and provides precision related operations.
Definition: ie_precision.hpp:19
void setPrecision(const Precision &p)
Sets the memory precision.
Definition: ie_layouts.h:269