1 // Copyright (C) 2018-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
6 * @brief A header file for data layouts and conversion between them
15 #include "ie_common.h"
16 #include "ie_precision.hpp"
18 namespace InferenceEngine {
21 * @brief This class describes blocking layouts
23 class INFERENCE_ENGINE_API_CLASS(BlockingDesc) {
26 * @brief The default constructor which creates empty blocking descriptor
30 * @brief The constructor which allows to create blocking descriptors for standard layouts
32 * @param dims real dimensions
33 * @param layout memory layout
35 BlockingDesc(const SizeVector& dims, Layout layout);
37 * @brief The constructor allows to create blocking descriptors for blocked memory
39 * @param blocked_dims blocked dimensions
40 * @param order the order of dimensions
42 BlockingDesc(const SizeVector& blocked_dims, const SizeVector& order);
44 * @brief The constructor allows to create blocking descriptors for blocked memory
46 * @param blocked_dims blocked dimensions
47 * @param order the order of dimensions
48 * @param offset offset to the current memory block
50 BlockingDesc(const SizeVector& blocked_dims, const SizeVector& order, size_t offset);
52 * @brief The constructor allows to create blocking descriptors for blocked memory
54 * @param blocked_dims blocked dimensions
55 * @param order the order of dimensions
56 * @param offset offset to the current memory block
57 * @param dimOffsets per-dimension offset from the padding to actual data,
59 BlockingDesc(const SizeVector& blocked_dims, const SizeVector& order, size_t offset, SizeVector dimOffsets);
61 * @brief The constructor allows to create blocking descriptors for blocked memory
63 * @param blocked_dims blocked dimensions
64 * @param order the order of dimensions
65 * @param offset offset to the current memory block
66 * @param dimOffsets per-dimension offset from the padding to actual data,
67 * @param strides strides for each dimension
69 BlockingDesc(const SizeVector& blocked_dims, const SizeVector& order, size_t offset, SizeVector dimOffsets,
73 * @brief Returns the blocked dimensions vector
75 * @return blocked dimensions
77 const SizeVector& getBlockDims() const {
82 * @brief Returns the vector of order
86 const SizeVector& getOrder() const {
91 * @brief Returns the per-dimension offset vector
95 const SizeVector& getOffsetPaddingToData() const {
96 return offsetPaddingToData;
100 * @brief Returns the offset to the current memory block
104 size_t getOffsetPadding() const {
105 return offsetPadding;
109 * @brief Returns strides for each dimension
113 const SizeVector& getStrides() const {
118 * @brief The comparison operator for the BlockingDesc
120 * @param rhs object to compare
121 * @return true if objects are equal
123 bool operator==(const BlockingDesc& rhs) const;
125 * @brief The comparison operator for the BlockingDesc
127 * @param rhs object to compare
128 * @return true if objects aren't equal
130 bool operator!=(const BlockingDesc& rhs) const;
133 void fillDesc(const SizeVector& blocked_dims, const SizeVector& order);
136 /** Blocked dimensions. */
137 SizeVector blockedDims;
138 /** Strides for blocked dimensions */
140 /** The order of blocked dimensions **/
142 /** Per-dimension offset from the padding to actual data, the top-level
143 * tensor with offsets applied must lie within the padding area. */
144 SizeVector offsetPaddingToData;
145 /** Offset from memory origin to the current block, non-zero only in
146 * a description of a memory sub-block. */
147 size_t offsetPadding;
151 * @brief This class defines Tensor description
153 class INFERENCE_ENGINE_API_CLASS(TensorDesc) {
156 * @brief The constructor creates the tensor descriptor using blocking descriptor
158 * @param precision memory precision
159 * @param dims memory dimensions
160 * @param blockDesc blocking descriptor
162 TensorDesc(const Precision& precision, SizeVector dims, const BlockingDesc& blockDesc);
164 * @brief The constructor creates the tensor descriptor using standard layout
166 * @param precision memory precision
167 * @param dims memory dimensions
168 * @param layout memory layout
170 TensorDesc(const Precision& precision, SizeVector dims, Layout layout);
172 * @brief The constructor creates the empty tensor descriptor with precision and layout
174 * @param precision memory precision
175 * @param layout memory layout
177 TensorDesc(const Precision& precision, Layout layout);
179 * @brief The default constructor which creates empty tensor descriptor
184 * @brief Reshapes the tensor descriptor
186 * @param dims new dimensions
187 * @param layout new layout if it is necessary
189 void reshape(const SizeVector& dims, Layout layout = Layout::ANY);
191 * @brief Reshapes the tensor descriptor
193 * @param dims new dimensions
194 * @param blockDesc new blocking descriptor
196 void reshape(const SizeVector& dims, const BlockingDesc& blockDesc);
199 * @brief Returns the vector of dimensions
203 SizeVector& getDims() {
207 * @brief Returns the constant vector of dimensions
211 const SizeVector& getDims() const noexcept {
215 * @brief Sets dimensions
217 * @param dims new dimensions
219 void setDims(const SizeVector& dims);
222 * @brief Returns the memory layout
226 Layout getLayout() const {
231 * @brief Sets the layout
233 * @param l memory layout
235 void setLayout(Layout l) {
236 bool inconsistentLayout = true;
239 inconsistentLayout = !dims.empty();
242 inconsistentLayout = dims.size() != 1;
244 case Layout::BLOCKED:
246 inconsistentLayout = false;
249 inconsistentLayout = dims.size() != 6;
255 inconsistentLayout = dims.size() != 5;
260 inconsistentLayout = dims.size() != 4;
263 inconsistentLayout = dims.size() != 3;
268 inconsistentLayout = dims.size() != 2;
273 if (inconsistentLayout)
274 THROW_IE_EXCEPTION << "Size of dims(" << std::to_string(dims.size()) << ") and format(" << l
275 << ") are inconsistent.";
280 * @brief Returns the memory precision
284 const Precision& getPrecision() const {
289 * @brief Sets the memory precision
293 void setPrecision(const Precision& p) {
298 * @brief Returns the blocking descriptor
300 * @return blocking descriptor
302 const BlockingDesc& getBlockingDesc() const {
307 * @brief The comparison operator for the TensorDesc
309 * @param rhs object to compare
310 * @return true if objects are equal
312 bool operator==(const TensorDesc& rhs) const;
314 * @brief The comparison operator for the TensorDesc
316 * @param rhs object to compare
317 * @return true if objects aren't equal
319 bool operator!=(const TensorDesc& rhs) const;
322 * @brief Calculates offset for the vector of dimensions
324 * @param v vector of dimensions
327 size_t offset(const SizeVector& v) const;
329 * @brief Calculates offset for the local offset
331 * @param l local offset
334 size_t offset(size_t l) const;
337 * @brief Returns the standard layout for dimensions
339 * @param dims the vector of dimensions
340 * @return the standard memory layout
342 static Layout getLayoutByDims(SizeVector dims);
350 * @brief blob's dimensions
354 * @brief memory precision
358 * Detailed information about layout construction
360 BlockingDesc blockingDesc;
363 } // namespace InferenceEngine