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);
66 * @brief Returns the blocked dimensions vector
67 * @return blocked dimensions
69 const SizeVector& getBlockDims() const {
74 * @brief Returns the vector of order
77 const SizeVector& getOrder() const {
82 * @brief Returns the per-dimension offset vector
85 const SizeVector& getOffsetPaddingToData() const {
86 return offsetPaddingToData;
90 * @brief Returns the offset to the current memory block
93 size_t getOffsetPadding() const {
98 * @brief Returns strides for each dimension
101 const SizeVector& getStrides() const {
106 * @brief The comparison operator for the BlockingDesc
107 * @param rhs object to compare
108 * @return true if objects are equal
110 bool operator == (const BlockingDesc& rhs) const;
112 * @brief The comparison operator for the BlockingDesc
113 * @param rhs object to compare
114 * @return true if objects aren't equal
116 bool operator != (const BlockingDesc& rhs) const;
119 void fillDesc(const SizeVector& blocked_dims, const SizeVector& order);
122 /** Blocked dimensions. */
123 SizeVector blockedDims;
124 /** Strides for blocked dimensions */
126 /** The order of blocked dimensions **/
128 /** Per-dimension offset from the padding to actual data, the top-level
129 * tensor with offsets applied must lie within the padding area. */
130 SizeVector offsetPaddingToData;
131 /** Offset from memory origin to the current block, non-zero only in
132 * a description of a memory sub-block. */
133 size_t offsetPadding;
137 * @brief This class defines Tensor description
139 class INFERENCE_ENGINE_API_CLASS(TensorDesc) {
142 * @brief The constructor creates the tensor descriptor using blocking descriptor
143 * @param precision memory precision
144 * @param dims memory dimensions
145 * @param blockDesc blocking descriptor
147 TensorDesc(const Precision& precision, SizeVector dims, const BlockingDesc& blockDesc);
149 * @brief The constructor creates the tensor descriptor using standard layout
150 * @param precision memory precision
151 * @param dims memory dimensions
152 * @param layout memory layout
154 TensorDesc(const Precision& precision, SizeVector dims, Layout layout);
156 * @brief The constructor creates the empty tensor descriptor with precision and layout
157 * @param precision memory precision
158 * @param layout memory layout
160 TensorDesc(const Precision& precision, Layout layout);
162 * @brief The default constructor which creates empty tensor descriptor
167 * @brief Reshapes the tensor descriptor
168 * @param dims new dimensions
169 * @param layout new layout if it is necessary
171 void reshape(const SizeVector &dims, Layout layout = Layout::ANY);
173 * @brief Reshapes the tensor descriptor
174 * @param dims new dimensions
175 * @param blockDesc new blocking descriptor
177 void reshape(const SizeVector &dims, const BlockingDesc &blockDesc);
180 * @brief Returns the vector of dimensions
183 SizeVector& getDims() {
187 * @brief Returns the constant vector of dimensions
190 const SizeVector& getDims() const noexcept {
194 * @brief Sets dimensions
195 * @param dims new dimensions
197 void setDims(const SizeVector& dims);
200 * @brief Returns the memory layout
203 Layout getLayout() const {
208 * @brief Sets the layout
209 * @param l memory layout
211 void setLayout(Layout l) {
212 bool inconsistentLayout = true;
215 inconsistentLayout = !dims.empty();
218 inconsistentLayout = dims.size() != 1;
220 case Layout::BLOCKED:
222 inconsistentLayout = false;
225 inconsistentLayout = dims.size() != 6;
231 inconsistentLayout = dims.size() != 5;
236 inconsistentLayout = dims.size() != 4;
239 inconsistentLayout = dims.size() != 3;
244 inconsistentLayout = dims.size() != 2;
249 if (inconsistentLayout)
250 THROW_IE_EXCEPTION << "Size of dims(" << std::to_string(dims.size()) << ") and format(" << l << ") are inconsistent.";
255 * @brief Returns the memory precision
258 const Precision& getPrecision() const {
263 * @brief Sets the memory precision
266 void setPrecision(const Precision& p) {
271 * @brief Returns the blocking descriptor
272 * @return blocking descriptor
274 const BlockingDesc& getBlockingDesc() const {
279 * @brief The comparison operator for the TensorDesc
280 * @param rhs object to compare
281 * @return true if objects are equal
283 bool operator == (const TensorDesc& rhs) const;
285 * @brief The comparison operator for the TensorDesc
286 * @param rhs object to compare
287 * @return true if objects aren't equal
289 bool operator != (const TensorDesc& rhs) const;
292 * @brief Calculates offset for the vector of dimensions
293 * @param v vector of dimensions
296 size_t offset(const SizeVector& v) const;
298 * @brief Calculates offset for the local offset
299 * @param l local offset
302 size_t offset(size_t l) const;
305 * @brief Returns the standard layout for dimensions
306 * @param dims the vector of dimensions
307 * @return the standard memory layout
309 static Layout getLayoutByDims(SizeVector dims);
316 * @brief blob's dimensions
320 * @brief memory precision
324 * Detailed information about layout construction
326 BlockingDesc blockingDesc;
329 } // namespace InferenceEngine