blob: 5b6eb2d127b24afdfff6fa45b8ade9eeae27efc4 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas49be2e32019-09-02 13:18:55 +01002 * Copyright (c) 2016-2019 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef __ARM_COMPUTE_ITENSORINFO_H__
25#define __ARM_COMPUTE_ITENSORINFO_H__
26
27#include "arm_compute/core/Coordinates.h"
28#include "arm_compute/core/Strides.h"
29#include "arm_compute/core/TensorShape.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Utils.h"
Georgios Pinitas283c1792017-11-10 18:14:06 +000032#include "arm_compute/core/utils/misc/ICloneable.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000033#include "arm_compute/core/utils/misc/Utility.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
35#include <cstddef>
36
37namespace arm_compute
38{
39/** Store the tensor's metadata */
Georgios Pinitas283c1792017-11-10 18:14:06 +000040class ITensorInfo : public misc::ICloneable<ITensorInfo>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041{
42public:
43 /** Default virtual destructor */
44 virtual ~ITensorInfo() = default;
45 /** Set the data type to the specified value.
46 *
47 * @warning This resets the format to UNKNOWN.
48 *
49 * @param[in] data_type The new data type.
Georgios Pinitas283c1792017-11-10 18:14:06 +000050 *
51 * @return Reference to this ITensorInfo object
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052 */
Georgios Pinitas283c1792017-11-10 18:14:06 +000053 virtual ITensorInfo &set_data_type(DataType data_type) = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054 /** Set the number of channels to the specified value.
55 *
56 * @warning This resets the format to UNKNOWN.
57 *
58 * @param[in] num_channels New number of channels.
Georgios Pinitas283c1792017-11-10 18:14:06 +000059 *
60 * @return Reference to this ITensorInfo object
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 */
Georgios Pinitas283c1792017-11-10 18:14:06 +000062 virtual ITensorInfo &set_num_channels(int num_channels) = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063 /** Set the format of an already initialized tensor.
64 *
65 * @note If the data type has already been configured (i.e. not UNKNOWN) it
66 * must match the new format. If data type hasn't been configured it will
67 * be based on the format.
68 *
69 * @param[in] format Single-plane format of the tensor.
Georgios Pinitas283c1792017-11-10 18:14:06 +000070 *
71 * @return Reference to this ITensorInfo object
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072 */
Georgios Pinitas283c1792017-11-10 18:14:06 +000073 virtual ITensorInfo &set_format(Format format) = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074 /** Set the shape of an already initialized tensor.
75 *
76 * @warning Changing the shape requires to recompute the strides and is
77 * therefore only possible if the tensor hasn't been allocated yet.
78 *
79 * @param[in] shape New tensor shape.
Georgios Pinitas283c1792017-11-10 18:14:06 +000080 *
81 * @return Reference to this ITensorInfo object
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082 */
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000083 virtual ITensorInfo &set_tensor_shape(const TensorShape &shape) = 0;
Georgios Pinitas283c1792017-11-10 18:14:06 +000084 /** Set the quantization settings (scale and offset) of the tensor.
Anthony Barbierf202e502017-11-23 18:02:04 +000085 *
86 * @param[in] quantization_info QuantizationInfo containing the scale and offset
87 *
88 * @return Reference to this ITensorInfo object
89 */
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +000090 virtual ITensorInfo &set_quantization_info(const QuantizationInfo &quantization_info) = 0;
Isabella Gottardid17a6772018-02-27 17:41:55 +000091 /** Set the data layout of the tensor.
92 *
93 * @param[in] data_layout DataLayout containing the layout data information.
94 *
95 * @return Reference to this ITensorInfo object
96 */
97 virtual ITensorInfo &set_data_layout(const DataLayout &data_layout) = 0;
Georgios Pinitas30902ed2017-11-14 15:32:57 +000098 /** Resets the padding settings of the tensor.
99 *
100 * @return Reference to this ITensorInfo object
101 */
102 virtual ITensorInfo &reset_padding() = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103 /** Update the offset to the first element and the strides to automatically computed values.
104 *
105 * @note The padding used by this method is really conservative so that the tensor can be used for most functions.
106 *
107 * @return True if the strides or the offset to the first element have changed.
108 */
109 virtual bool auto_padding() = 0;
110 /** Update the offset to the first element, the strides and the total size.
111 *
112 * @note This function can only increase the offset, strides and total size.
113 *
114 * @param[in] padding Padding around the XY plane in number of elements.
115 *
116 * @return True if the strides, offset and total size have changed.
117 */
118 virtual bool extend_padding(const PaddingSize &padding) = 0;
119 /** Return the size of the requested dimension
120 *
121 * @param[in] index Index of the dimension
122 *
123 * @return Dimension of the requested dimension
124 */
125 virtual size_t dimension(size_t index) const = 0;
Isabella Gottardid56e7702018-02-28 14:29:36 +0000126 /** Return the size of the requested data layout dimension
127 *
128 * @param[in] dimension DataLayoutDimension of the dimension
129 *
130 * @return Dimension of the requested dimension
131 */
132 virtual size_t dimension(DataLayoutDimension dimension) const = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100133 /** The strides in bytes for accessing each dimension of the tensor
134 *
135 * @return Strides in bytes for each tensor dimension
136 */
137 virtual const Strides &strides_in_bytes() const = 0;
138 /** The offset from the beginning of the memory allocation to the first element of the tensor.
139 * This can be used to access efficiently elements in a 2D tensor
140 *
141 * @return The offset in bytes to access the first element of the tensor.
142 */
143 virtual size_t offset_first_element_in_bytes() const = 0;
144 /** The offset in bytes from the beginning of the memory allocation to access the element at position (x, y, z ...)
145 *
146 * @param[in] pos Vector with the coordinates of the element to access.
147 * The size of this vector must be equal to the number of dimensions of the tensor
148 *
149 * @return Offset in bytes from the beginning of the memory allocation to access the element (x, y, z, ...)
150 */
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100151 virtual int32_t offset_element_in_bytes(const Coordinates &pos) const = 0;
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100152
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153 /** Element size in bytes calculated as data_size() * num_channels()
154 *
155 * @return The size of one element in bytes
156 */
157 virtual size_t element_size() const = 0;
158 /** The number of dimensions of the tensor (rank)
159 *
160 * @return The number of dimensions of the tensor (rank)
161 */
162 virtual size_t num_dimensions() const = 0;
163 /** The number of channels for each tensor element
164 *
165 * @return The number of channels for each tensor element
166 */
167 virtual size_t num_channels() const = 0;
168 /** Size for each dimension of the tensor
169 *
170 * @return A vector with the size for each dimension of the tensor
171 */
172 virtual const TensorShape &tensor_shape() const = 0;
173 /** Data type used for each element of the tensor
174 *
175 * @return Tensor data type
176 */
177 virtual DataType data_type() const = 0;
178 /** Colour format of the image
179 *
180 * @return Colour format of the image
181 */
182 virtual Format format() const = 0;
183 /** Returns the total size of the tensor in bytes.
184 *
185 * @return Total size of the tensor in bytes.
186 */
187 virtual size_t total_size() const = 0;
188 /** Padding of tensor.
189 *
190 * @return Padding.
191 */
192 virtual PaddingSize padding() const = 0;
193 /** Checks if the tensor has been allocated with padding or not.
194 *
195 * @return True if padding is allocated in the tensor, otherwise false.
196 */
197 virtual bool has_padding() const = 0;
198 /** Flag indicating whether the size of the tensor can be changed.
199 *
200 * @return True if the tensor size can be changed.
201 */
202 virtual bool is_resizable() const = 0;
Georgios Pinitas49be2e32019-09-02 13:18:55 +0100203 /** Flag indicating whether the shape of the tensor is dynamic, meaning that it can change on kernel/function execution.
204 *
205 * @return True if its dynamic else false
206 */
207 virtual bool is_dynamic() const = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100208 /** Set the flag whether the tensor size can be changed.
209 *
210 * @param[in] is_resizable Flag that marks the tensor if it can be changed or not.
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000211 *
212 * @return Reference to this ITensorInfo object
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100213 */
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000214 virtual ITensorInfo &set_is_resizable(bool is_resizable) = 0;
Georgios Pinitas49be2e32019-09-02 13:18:55 +0100215 /** Set the flag whether the tensor size is dynamic.
216 *
217 * @param[in] is_dynamic Flag that marks the tensor if it's dynamic.
218 *
219 * @return Reference to this ITensorInfo object
220 */
221 virtual ITensorInfo &set_is_dynamic(bool is_dynamic) = 0;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100222 /** Valid region of the tensor. All elements in the valid region have defined values, i.e. are not undefined.
223 *
224 * @return The valid region.
225 */
226 virtual ValidRegion valid_region() const = 0;
227 /** Set the valid region of the tensor.
228 *
229 * @param[in] valid_region Valid region to set.
230 */
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000231 virtual void set_valid_region(const ValidRegion &valid_region) = 0;
Michel Iwaniec00633802017-10-12 14:14:15 +0100232
233 /** Get the quantization settings (scale and offset) of the tensor.
234 *
235 * @return A QuantizationInfo containing the scale and offset.
236 */
237 virtual QuantizationInfo quantization_info() const = 0;
Isabella Gottardid17a6772018-02-27 17:41:55 +0000238 /** Get the data layout of the tensor.
239 *
240 * @return A DataLayout containing the layout data information.
241 */
242 virtual DataLayout data_layout() const = 0;
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000243
244 /** If infos are broadcast compatible tensor info's, return the broadcasted shape and the intersection of
245 * the broadcasted valid regions of the tensors.
246 *
247 * Two tensor info's are broadcast compatible if their shapes are broadcast compatible.
248 *
249 * Two tensor shapes are broadcast compatible if for each dimension, they're equal or one of them is 1.
250 *
251 * If two shapes are compatible, each dimension in the broadcasted shape is the max of the original dimensions.
252 *
253 * @param[in] infos Tensor info's.
254 *
255 * @return The broadcasted shape and valid region, or an empty shape and valid region if the info's are
256 * not broadcast compatible.
257 */
258 template <typename... Infos>
259 static std::pair<TensorShape, ValidRegion> broadcast_shape_and_valid_region(const Infos &... infos)
260 {
261 TensorShape bc_shape = TensorShape::broadcast_shape(infos.tensor_shape()...);
262 ValidRegion bc_valid_region{ Coordinates(), bc_shape };
263
264 auto broadcast_valid_region = [&bc_valid_region](const ITensorInfo & info)
265 {
266 if(info.num_dimensions() != 0)
267 {
268 for(size_t d = 0; d < bc_valid_region.shape.num_dimensions(); ++d)
269 {
270 const bool is_broadcast = (info.tensor_shape()[d] == 1);
271
272 const int anchor_max = std::max(bc_valid_region.anchor[d], info.valid_region().anchor[d]);
273 const size_t valid_min = std::min(bc_valid_region.shape[d], info.valid_region().shape[d]);
274
275 if(!is_broadcast || (valid_min == 0))
276 {
277 bc_valid_region.anchor.set(d, anchor_max);
278 bc_valid_region.shape.set(d, valid_min);
279 }
280 }
281 }
282 };
283
284 utility::for_each(broadcast_valid_region, infos...);
285
286 return std::pair<TensorShape, ValidRegion>(bc_shape, bc_valid_region);
287 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100288};
Georgios Pinitas49be2e32019-09-02 13:18:55 +0100289} // namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100290#endif /*__ARM_COMPUTE_TENSORINFO_H__ */