1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
6 * @brief A header file for data layouts and conversion between them
14 #include "ie_common.h"
15 #include "ie_precision.hpp"
17 namespace InferenceEngine {
20 * @brief This class describes blocking layouts
22 class INFERENCE_ENGINE_API_CLASS(BlockingDesc) {
25 * @brief The default constructor which creates empty blocking descriptor
29 * @brief The constructor which allows to create blocking descriptors for standard layouts
30 * @param dims real dimensions
31 * @param layout memory layout
33 BlockingDesc(const SizeVector& dims, Layout layout);
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
39 BlockingDesc(const SizeVector& blocked_dims, const SizeVector& order);
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
46 BlockingDesc(const SizeVector& blocked_dims, const SizeVector& order, size_t offset);
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,
54 BlockingDesc(const SizeVector& blocked_dims, const SizeVector& order, size_t offset, SizeVector dimOffsets);
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
63 BlockingDesc(const SizeVector& blocked_dims, const SizeVector& order, size_t offset, SizeVector dimOffsets, SizeVector strides);
67 virtual ~BlockingDesc() = default;
70 * @brief Returns the blocked dimensions vector
71 * @return blocked dimensions
73 const SizeVector& getBlockDims() const {
78 * @brief Returns the vector of order
81 const SizeVector& getOrder() const {
86 * @brief Returns the per-dimension offset vector
89 const SizeVector& getOffsetPaddingToData() const {
90 return offsetPaddingToData;
94 * @brief Returns the offset to the current memory block
97 size_t getOffsetPadding() const {
102 * @brief Returns strides for each dimension
105 const SizeVector& getStrides() const {
110 * @brief The comparison operator for the BlockingDesc
111 * @param rhs object to compare
112 * @return true if objects are equal
114 bool operator == (const BlockingDesc& rhs) const;
116 * @brief The comparison operator for the BlockingDesc
117 * @param rhs object to compare
118 * @return true if objects aren't equal
120 bool operator != (const BlockingDesc& rhs) const;
123 void fillDesc(const SizeVector& blocked_dims, const SizeVector& order);
126 /** Blocked dimensions. */
127 SizeVector blockedDims;
128 /** Strides for blocked dimensions */
130 /** The order of blocked dimensions **/
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;
141 * @brief This class defines Tensor description
143 class INFERENCE_ENGINE_API_CLASS(TensorDesc) {
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
151 TensorDesc(const Precision& precision, SizeVector dims, const BlockingDesc& blockDesc);
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
158 TensorDesc(const Precision& precision, SizeVector dims, Layout layout);
160 * @brief The constructor creates the empty tensor descriptor with precision and layout
161 * @param precision memory precision
162 * @param layout memory layout
164 TensorDesc(const Precision& precision, Layout layout);
166 * @brief The default constructor which creates empty tensor descriptor
172 virtual ~TensorDesc() = default;
175 * @brief Reshapes the tensor descriptor
176 * @param dims new dimensions
177 * @param layout new layout if it is necessary
179 void reshape(const SizeVector &dims, Layout layout = Layout::ANY);
181 * @brief Reshapes the tensor descriptor
182 * @param dims new dimensions
183 * @param blockDesc new blocking descriptor
185 void reshape(const SizeVector &dims, const BlockingDesc &blockDesc);
188 * @brief Returns the vector of dimensions
191 SizeVector& getDims() {
195 * @brief Returns the constant vector of dimensions
198 const SizeVector& getDims() const {
202 * @brief Sets dimensions
203 * @param dims new dimensions
205 void setDims(const SizeVector& dims);
208 * @brief Returns the memory layout
211 Layout getLayout() const {
216 * @brief Sets the layout
217 * @param l memory layout
219 void setLayout(Layout l) {
220 bool inconsistentLayout = true;
223 inconsistentLayout = !dims.empty();
226 inconsistentLayout = dims.size() != 1;
228 case Layout::BLOCKED:
229 inconsistentLayout = false;
233 inconsistentLayout = dims.size() != 5;
238 inconsistentLayout = dims.size() != 4;
241 inconsistentLayout = dims.size() != 3;
246 inconsistentLayout = dims.size() != 2;
251 if (inconsistentLayout)
252 THROW_IE_EXCEPTION << "Size of dims(" << std::to_string(dims.size()) << ") and format(" << l << ") are inconsistent.";
257 * @brief Returns the memory precision
260 const Precision& getPrecision() const {
265 * @brief Sets the memory precision
268 void setPrecision(const Precision& p) {
273 * @brief Returns the blocking descriptor
274 * @return blocking descriptor
276 const BlockingDesc& getBlockingDesc() const {
281 * @brief The comparison operator for the TensorDesc
282 * @param rhs object to compare
283 * @return true if objects are equal
285 bool operator == (const TensorDesc& rhs) const;
287 * @brief The comparison operator for the TensorDesc
288 * @param rhs object to compare
289 * @return true if objects aren't equal
291 bool operator != (const TensorDesc& rhs) const;
294 * @brief Calculates offset for the vector of dimensions
295 * @param v vector of dimensions
298 size_t offset(const SizeVector& v) const;
300 * @brief Calculates offset for the local offset
301 * @param l local offset
304 size_t offset(size_t l) const;
307 * @brief Returns the standard layout for dimensions
308 * @param dims the vector of dimensions
309 * @return the standard memory layout
311 static Layout getLayoutByDims(SizeVector dims);
318 * @brief blob's dimensions
322 * @brief memory precision
326 * Detailed information about layout construction
328 BlockingDesc blockingDesc;
334 static const size_t I_N = 3;
335 static const size_t I_C = 2;
336 static const size_t I_H = 1;
337 static const size_t I_W = 0;
340 * @deprecated Uses TensorDesc working with layouts
341 * @brief This class helps calculating offset in different layouts
343 class INFERENCE_ENGINE_API_CLASS(LayoutOffsetCounter) {
351 * @brief Stores multipliers that are calculated during the LayoutOffsetCounter construction.
352 * The multipliers are used for conversion.
357 * @brief A default constructor
358 * @param dims Tensor dimension array (reverse NCHW order as in the IR: w,h,c,n)
360 LayoutOffsetCounter(Layout layout, SizeVector dims);
363 * @brief Calculates an offset for the specified layout
364 * @param pos Tensor position array (reverse NCHW order as in the IR: w,h,c,n)
366 size_t Offset(SizeVector pos);
370 * @deprecated Please use TensorDescriptors for conversion
372 template<typename T> void ConvertLayout(Layout sourceLayout, Layout destLayout, const T* sourceBuffer, T* destBuffer, SizeVector dims) {
373 if (dims.size() == 0) return;
375 SizeVector pos(dims.size(), 0);
376 LayoutOffsetCounter srcOffsetCounter(sourceLayout, dims);
377 LayoutOffsetCounter destOffsetCounter(destLayout, dims);
380 // Setting the current item
381 size_t ps = srcOffsetCounter.Offset(pos);
382 size_t pd = destOffsetCounter.Offset(pos);
384 destBuffer[pd] = sourceBuffer[ps];
389 while (pos[caret] >= dims[caret]) {
392 if (caret == pos.size()) {
393 // We have finished converting
401 } // namespace InferenceEngine