Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 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 Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/utils/misc/ICloneable.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 33 | #include "arm_compute/core/utils/misc/Utility.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
| 35 | #include <cstddef> |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | /** Store the tensor's metadata */ |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 40 | class ITensorInfo : public misc::ICloneable<ITensorInfo> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 41 | { |
| 42 | public: |
| 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 Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 50 | * |
| 51 | * @return Reference to this ITensorInfo object |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 52 | */ |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 53 | virtual ITensorInfo &set_data_type(DataType data_type) = 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 54 | /** 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 Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 59 | * |
| 60 | * @return Reference to this ITensorInfo object |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 61 | */ |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 62 | virtual ITensorInfo &set_num_channels(int num_channels) = 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 63 | /** 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 Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 70 | * |
| 71 | * @return Reference to this ITensorInfo object |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 72 | */ |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 73 | virtual ITensorInfo &set_format(Format format) = 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 74 | /** 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 Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 80 | * |
| 81 | * @return Reference to this ITensorInfo object |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 82 | */ |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 83 | virtual ITensorInfo &set_tensor_shape(const TensorShape &shape) = 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 84 | /** Set the fixed point position to the specified value |
| 85 | * |
| 86 | * @warning The fixed point position must be set once the data type has been configured |
| 87 | * |
| 88 | * @param[in] fixed_point_position The new fixed point position |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 89 | * |
| 90 | * @return Reference to this ITensorInfo object |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 91 | */ |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 92 | virtual ITensorInfo &set_fixed_point_position(int fixed_point_position) = 0; |
| 93 | /** Set the quantization settings (scale and offset) of the tensor. |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 94 | * |
| 95 | * @param[in] quantization_info QuantizationInfo containing the scale and offset |
| 96 | * |
| 97 | * @return Reference to this ITensorInfo object |
| 98 | */ |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 99 | virtual ITensorInfo &set_quantization_info(const QuantizationInfo &quantization_info) = 0; |
Isabella Gottardi | d17a677 | 2018-02-27 17:41:55 +0000 | [diff] [blame] | 100 | /** Set the data layout of the tensor. |
| 101 | * |
| 102 | * @param[in] data_layout DataLayout containing the layout data information. |
| 103 | * |
| 104 | * @return Reference to this ITensorInfo object |
| 105 | */ |
| 106 | virtual ITensorInfo &set_data_layout(const DataLayout &data_layout) = 0; |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 107 | /** Resets the padding settings of the tensor. |
| 108 | * |
| 109 | * @return Reference to this ITensorInfo object |
| 110 | */ |
| 111 | virtual ITensorInfo &reset_padding() = 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 112 | /** Update the offset to the first element and the strides to automatically computed values. |
| 113 | * |
| 114 | * @note The padding used by this method is really conservative so that the tensor can be used for most functions. |
| 115 | * |
| 116 | * @return True if the strides or the offset to the first element have changed. |
| 117 | */ |
| 118 | virtual bool auto_padding() = 0; |
| 119 | /** Update the offset to the first element, the strides and the total size. |
| 120 | * |
| 121 | * @note This function can only increase the offset, strides and total size. |
| 122 | * |
| 123 | * @param[in] padding Padding around the XY plane in number of elements. |
| 124 | * |
| 125 | * @return True if the strides, offset and total size have changed. |
| 126 | */ |
| 127 | virtual bool extend_padding(const PaddingSize &padding) = 0; |
| 128 | /** Return the size of the requested dimension |
| 129 | * |
| 130 | * @param[in] index Index of the dimension |
| 131 | * |
| 132 | * @return Dimension of the requested dimension |
| 133 | */ |
| 134 | virtual size_t dimension(size_t index) const = 0; |
Isabella Gottardi | d56e770 | 2018-02-28 14:29:36 +0000 | [diff] [blame] | 135 | /** Return the size of the requested data layout dimension |
| 136 | * |
| 137 | * @param[in] dimension DataLayoutDimension of the dimension |
| 138 | * |
| 139 | * @return Dimension of the requested dimension |
| 140 | */ |
| 141 | virtual size_t dimension(DataLayoutDimension dimension) const = 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 142 | /** The strides in bytes for accessing each dimension of the tensor |
| 143 | * |
| 144 | * @return Strides in bytes for each tensor dimension |
| 145 | */ |
| 146 | virtual const Strides &strides_in_bytes() const = 0; |
| 147 | /** The offset from the beginning of the memory allocation to the first element of the tensor. |
| 148 | * This can be used to access efficiently elements in a 2D tensor |
| 149 | * |
| 150 | * @return The offset in bytes to access the first element of the tensor. |
| 151 | */ |
| 152 | virtual size_t offset_first_element_in_bytes() const = 0; |
| 153 | /** The offset in bytes from the beginning of the memory allocation to access the element at position (x, y, z ...) |
| 154 | * |
| 155 | * @param[in] pos Vector with the coordinates of the element to access. |
| 156 | * The size of this vector must be equal to the number of dimensions of the tensor |
| 157 | * |
| 158 | * @return Offset in bytes from the beginning of the memory allocation to access the element (x, y, z, ...) |
| 159 | */ |
| 160 | virtual size_t offset_element_in_bytes(const Coordinates &pos) const = 0; |
| 161 | /** Fixed point position used when the tensor data type is QS8 or QS16 |
| 162 | * |
| 163 | * @return The fixed point position that expresses the number of bits for the fractional part of the number |
| 164 | */ |
| 165 | virtual int fixed_point_position() const = 0; |
| 166 | /** Element size in bytes calculated as data_size() * num_channels() |
| 167 | * |
| 168 | * @return The size of one element in bytes |
| 169 | */ |
| 170 | virtual size_t element_size() const = 0; |
| 171 | /** The number of dimensions of the tensor (rank) |
| 172 | * |
| 173 | * @return The number of dimensions of the tensor (rank) |
| 174 | */ |
| 175 | virtual size_t num_dimensions() const = 0; |
| 176 | /** The number of channels for each tensor element |
| 177 | * |
| 178 | * @return The number of channels for each tensor element |
| 179 | */ |
| 180 | virtual size_t num_channels() const = 0; |
| 181 | /** Size for each dimension of the tensor |
| 182 | * |
| 183 | * @return A vector with the size for each dimension of the tensor |
| 184 | */ |
| 185 | virtual const TensorShape &tensor_shape() const = 0; |
| 186 | /** Data type used for each element of the tensor |
| 187 | * |
| 188 | * @return Tensor data type |
| 189 | */ |
| 190 | virtual DataType data_type() const = 0; |
| 191 | /** Colour format of the image |
| 192 | * |
| 193 | * @return Colour format of the image |
| 194 | */ |
| 195 | virtual Format format() const = 0; |
| 196 | /** Returns the total size of the tensor in bytes. |
| 197 | * |
| 198 | * @return Total size of the tensor in bytes. |
| 199 | */ |
| 200 | virtual size_t total_size() const = 0; |
| 201 | /** Padding of tensor. |
| 202 | * |
| 203 | * @return Padding. |
| 204 | */ |
| 205 | virtual PaddingSize padding() const = 0; |
| 206 | /** Checks if the tensor has been allocated with padding or not. |
| 207 | * |
| 208 | * @return True if padding is allocated in the tensor, otherwise false. |
| 209 | */ |
| 210 | virtual bool has_padding() const = 0; |
| 211 | /** Flag indicating whether the size of the tensor can be changed. |
| 212 | * |
| 213 | * @return True if the tensor size can be changed. |
| 214 | */ |
| 215 | virtual bool is_resizable() const = 0; |
| 216 | /** Set the flag whether the tensor size can be changed. |
| 217 | * |
| 218 | * @param[in] is_resizable Flag that marks the tensor if it can be changed or not. |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 219 | * |
| 220 | * @return Reference to this ITensorInfo object |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 221 | */ |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 222 | virtual ITensorInfo &set_is_resizable(bool is_resizable) = 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 223 | /** Valid region of the tensor. All elements in the valid region have defined values, i.e. are not undefined. |
| 224 | * |
| 225 | * @return The valid region. |
| 226 | */ |
| 227 | virtual ValidRegion valid_region() const = 0; |
| 228 | /** Set the valid region of the tensor. |
| 229 | * |
| 230 | * @param[in] valid_region Valid region to set. |
| 231 | */ |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 232 | virtual void set_valid_region(const ValidRegion &valid_region) = 0; |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 233 | |
| 234 | /** Get the quantization settings (scale and offset) of the tensor. |
| 235 | * |
| 236 | * @return A QuantizationInfo containing the scale and offset. |
| 237 | */ |
| 238 | virtual QuantizationInfo quantization_info() const = 0; |
Isabella Gottardi | d17a677 | 2018-02-27 17:41:55 +0000 | [diff] [blame] | 239 | /** Get the data layout of the tensor. |
| 240 | * |
| 241 | * @return A DataLayout containing the layout data information. |
| 242 | */ |
| 243 | virtual DataLayout data_layout() const = 0; |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 244 | |
| 245 | /** If infos are broadcast compatible tensor info's, return the broadcasted shape and the intersection of |
| 246 | * the broadcasted valid regions of the tensors. |
| 247 | * |
| 248 | * Two tensor info's are broadcast compatible if their shapes are broadcast compatible. |
| 249 | * |
| 250 | * Two tensor shapes are broadcast compatible if for each dimension, they're equal or one of them is 1. |
| 251 | * |
| 252 | * If two shapes are compatible, each dimension in the broadcasted shape is the max of the original dimensions. |
| 253 | * |
| 254 | * @param[in] infos Tensor info's. |
| 255 | * |
| 256 | * @return The broadcasted shape and valid region, or an empty shape and valid region if the info's are |
| 257 | * not broadcast compatible. |
| 258 | */ |
| 259 | template <typename... Infos> |
| 260 | static std::pair<TensorShape, ValidRegion> broadcast_shape_and_valid_region(const Infos &... infos) |
| 261 | { |
| 262 | TensorShape bc_shape = TensorShape::broadcast_shape(infos.tensor_shape()...); |
| 263 | ValidRegion bc_valid_region{ Coordinates(), bc_shape }; |
| 264 | |
| 265 | auto broadcast_valid_region = [&bc_valid_region](const ITensorInfo & info) |
| 266 | { |
| 267 | if(info.num_dimensions() != 0) |
| 268 | { |
| 269 | for(size_t d = 0; d < bc_valid_region.shape.num_dimensions(); ++d) |
| 270 | { |
| 271 | const bool is_broadcast = (info.tensor_shape()[d] == 1); |
| 272 | |
| 273 | const int anchor_max = std::max(bc_valid_region.anchor[d], info.valid_region().anchor[d]); |
| 274 | const size_t valid_min = std::min(bc_valid_region.shape[d], info.valid_region().shape[d]); |
| 275 | |
| 276 | if(!is_broadcast || (valid_min == 0)) |
| 277 | { |
| 278 | bc_valid_region.anchor.set(d, anchor_max); |
| 279 | bc_valid_region.shape.set(d, valid_min); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | }; |
| 284 | |
| 285 | utility::for_each(broadcast_valid_region, infos...); |
| 286 | |
| 287 | return std::pair<TensorShape, ValidRegion>(bc_shape, bc_valid_region); |
| 288 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 289 | }; |
| 290 | } |
| 291 | #endif /*__ARM_COMPUTE_TENSORINFO_H__ */ |