1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
/*
* Copyright (c) 2016-2024 Arm Limited.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef ACL_ARM_COMPUTE_CORE_ITENSORINFO_H
#define ACL_ARM_COMPUTE_CORE_ITENSORINFO_H
#include "arm_compute/core/Coordinates.h"
#include "arm_compute/core/Strides.h"
#include "arm_compute/core/TensorShape.h"
#include "arm_compute/core/Types.h"
#include "arm_compute/core/utils/misc/Utility.h"
#include "support/ICloneable.h"
#include <cstddef>
namespace arm_compute
{
class QuantizationInfo;
// Note: Any changes to the fields of the class below that have setters should be mirrored
// (if possible) in the auto_init_if_empty function in AutoConfiguration.h
/** Store the tensor's metadata */
class ITensorInfo : public misc::ICloneable<ITensorInfo>
{
public:
using TensorDimsState = std::vector<int>;
/** An id that uniquely identifies an ITensorInfo within some domain (e.g. a workload)
*/
using Id = int32_t;
/** An invalid tensor id within a domain */
static constexpr Id invalid_tensor_id = 0;
/** Get the value representing dynamic dimension state
*
* @return Value representing dynamic dimension state
*
*/
static constexpr int32_t get_dynamic_state_value()
{
return _dynamic_dimension;
}
/** Get the value representing static dimension state
*
* @return Value representing static dimension state
*
*/
static constexpr int32_t get_static_state_value()
{
return _static_dimension;
}
/** Default virtual destructor */
virtual ~ITensorInfo() = default;
/** Set the data type to the specified value.
*
* @warning This resets the format to UNKNOWN.
*
* @param[in] data_type The new data type.
*
* @return Reference to this ITensorInfo object
*/
virtual ITensorInfo &set_data_type(DataType data_type) = 0;
/** Set the number of channels to the specified value.
*
* @warning This resets the format to UNKNOWN.
*
* @param[in] num_channels New number of channels.
*
* @return Reference to this ITensorInfo object
*/
virtual ITensorInfo &set_num_channels(int num_channels) = 0;
/** Set the format of an already initialized tensor.
*
* @note If the data type has already been configured (i.e. not UNKNOWN) it
* must match the new format. If data type hasn't been configured it will
* be based on the format.
*
* @param[in] format Single-plane format of the tensor.
*
* @return Reference to this ITensorInfo object
*/
virtual ITensorInfo &set_format(Format format) = 0;
/** Set the shape of an already initialized tensor.
*
* @warning Changing the shape requires to recompute the strides and is
* therefore only possible if the tensor hasn't been allocated yet.
*
* @param[in] shape New tensor shape.
*
* @return Reference to this ITensorInfo object
*/
virtual ITensorInfo &set_tensor_shape(const TensorShape &shape) = 0;
/** Set the state for each dimension of the tensor
*
* This sets the state of each dimension of the shape in terms of dynamic behavior using -1 where appropriate.
* The index in the state is a 1 to 1 mapping with the shape dimension index.
* For example if you want to express [?, 3, 3] as a dynamic input then [-1, 3, 3] has to be set as a state
*
* @param[in] state Tensor dimensions state
*
* @return Reference to this ITensorInfo object
*/
virtual ITensorInfo &set_tensor_dims_state(const TensorDimsState &state) = 0;
/** Set the quantization settings (scale and offset) of the tensor.
*
* @param[in] quantization_info QuantizationInfo containing the scale and offset
*
* @return Reference to this ITensorInfo object
*/
virtual ITensorInfo &set_quantization_info(const QuantizationInfo &quantization_info) = 0;
/** Set the data layout of the tensor.
*
* @param[in] data_layout DataLayout containing the layout data information.
*
* @return Reference to this ITensorInfo object
*/
virtual ITensorInfo &set_data_layout(const DataLayout &data_layout) = 0;
/** Resets the padding settings of the tensor.
*
* @return Reference to this ITensorInfo object
*/
virtual ITensorInfo &reset_padding() = 0;
/** Update the offset to the first element and the strides to automatically computed values.
*
* @note The padding used by this method is really conservative so that the tensor can be used for most functions.
*
* @return True if the strides or the offset to the first element have changed.
*/
virtual bool auto_padding() = 0;
/** Set the lock paddings flag of the tensor.
* It should be set to True, when the tensor could be mapped to camera or frame buffer.
*
* @return Reference to this ITensorInfo object
*/
virtual ITensorInfo &set_lock_paddings(bool flag) = 0;
/** Get the lock paddings flag value
*
* @return lock paddings flag value
*/
virtual bool lock_paddings() const = 0;
/** Update the offset to the first element, the strides and the total size.
*
* @note This function can only increase the offset, strides and total size.
*
* @param[in] padding Padding around the XY plane in number of elements.
*
* @return True if the strides, offset and total size have changed.
*/
virtual bool extend_padding(const PaddingSize &padding) = 0;
/** Return the size of the requested dimension
*
* @param[in] index Index of the dimension
*
* @return Dimension of the requested dimension
*/
virtual size_t dimension(size_t index) const = 0;
/** Return the size of the requested data layout dimension
*
* @param[in] dimension DataLayoutDimension of the dimension
*
* @return Dimension of the requested dimension
*/
virtual size_t dimension(DataLayoutDimension dimension) const = 0;
/** The strides in bytes for accessing each dimension of the tensor
*
* @return Strides in bytes for each tensor dimension
*/
virtual const Strides &strides_in_bytes() const = 0;
/** The offset from the beginning of the memory allocation to the first element of the tensor.
* This can be used to access efficiently elements in a 2D tensor
*
* @return The offset in bytes to access the first element of the tensor.
*/
virtual size_t offset_first_element_in_bytes() const = 0;
/** The offset in bytes from the beginning of the memory allocation to access the element at position (x, y, z ...)
*
* @param[in] pos Vector with the coordinates of the element to access.
* The size of this vector must be equal to the number of dimensions of the tensor
*
* @return Offset in bytes from the beginning of the memory allocation to access the element (x, y, z, ...)
*/
virtual int32_t offset_element_in_bytes(const Coordinates &pos) const = 0;
/** Element size in bytes calculated as data_size() * num_channels()
*
* @return The size of one element in bytes
*/
virtual size_t element_size() const = 0;
/** The number of dimensions of the tensor (rank)
*
* @return The number of dimensions of the tensor (rank)
*/
virtual size_t num_dimensions() const = 0;
/** The number of channels for each tensor element
*
* @return The number of channels for each tensor element
*/
virtual size_t num_channels() const = 0;
/** Size for each dimension of the tensor
*
* @return A vector with the size for each dimension of the tensor
*/
virtual const TensorShape &tensor_shape() const = 0;
/** State of each dimension of the tensor shape
*
* @return A vector with the state for each dimension of the tensor, where -1 specifies dynamic dimension
*/
virtual const TensorDimsState &tensor_dims_state() const = 0;
/** Data type used for each element of the tensor
*
* @return Tensor data type
*/
virtual DataType data_type() const = 0;
/** Colour format of the image
*
* @return Colour format of the image
*/
virtual Format format() const = 0;
/** Returns the total size of the tensor in bytes.
*
* @return Total size of the tensor in bytes.
*/
virtual size_t total_size() const = 0;
/** Padding of tensor.
*
* @return Padding.
*/
virtual PaddingSize padding() const = 0;
/** Checks if the tensor has been allocated with padding or not.
*
* @return True if padding is allocated in the tensor, otherwise false.
*/
virtual bool has_padding() const = 0;
/** Flag indicating whether the size of the tensor can be changed.
*
* @return True if the tensor size can be changed.
*/
virtual bool is_resizable() const = 0;
/** Flag indicating whether the shape of the tensor is dynamic, meaning that it can change on kernel/function execution.
*
* @return True if its dynamic else false
*/
virtual bool is_dynamic() const = 0;
/** Flag indicating whether the values of the tensor are constant, meaning that they can change on kernel/function execution.
*
* @return True if values are constant else false
*/
virtual bool are_values_constant() const = 0;
/** Set the flag whether the tensor size can be changed.
*
* @param[in] is_resizable Flag that marks the tensor if it can be changed or not.
*
* @return Reference to this ITensorInfo object
*/
virtual ITensorInfo &set_is_resizable(bool is_resizable) = 0;
/** Set the flag whether the tensor values can change during kernel/function execution.
*
* @param[in] are_values_constant Flag that marks the tensor values if they can be changed or not.
*
* @return Reference to this ITensorInfo object
*/
virtual ITensorInfo &set_are_values_constant(bool are_values_constant) = 0;
/** Set the offset from the beginning of the memory allocation to the first element of the tensor.
*
* @param[in] offset Offset in bytes to first element in tensor.
*
* @return Reference to this ITensorInfo object
*/
virtual ITensorInfo &set_offset_first_element_in_bytes(const size_t offset) = 0;
/** Valid region of the tensor. All elements in the valid region have defined values, i.e. are not undefined.
*
* @return The valid region.
*/
virtual ValidRegion valid_region() const = 0;
/** Set the valid region of the tensor.
*
* @param[in] valid_region Valid region to set.
*/
virtual void set_valid_region(const ValidRegion &valid_region) = 0;
/** Get the quantization settings (scale and offset) of the tensor.
*
* @return A QuantizationInfo containing the scale and offset.
*/
virtual QuantizationInfo quantization_info() const = 0;
/** Get the data layout of the tensor.
*
* @return A DataLayout containing the layout data information.
*/
virtual DataLayout data_layout() const = 0;
/** Get the workload tensor id of the tensor.
*
* @return Workload tensor id of the tensor
*/
virtual Id id() const = 0;
/** Set the tensor id
*/
virtual ITensorInfo &set_id(ITensorInfo::Id id) = 0;
/** Check if the tensor id is valid
*/
bool has_valid_id() const
{
return id() != invalid_tensor_id;
}
/** If infos are broadcast compatible tensor info's, return the broadcasted shape and the intersection of
* the broadcasted valid regions of the tensors.
*
* Two tensor info's are broadcast compatible if their shapes are broadcast compatible.
*
* Two tensor shapes are broadcast compatible if for each dimension, they're equal or one of them is 1.
*
* If two shapes are compatible, each dimension in the broadcasted shape is the max of the original dimensions.
*
* @param[in] infos Tensor info's.
*
* @return The broadcasted shape and valid region, or an empty shape and valid region if the info's are
* not broadcast compatible.
*/
template <typename... Infos>
static std::pair<TensorShape, ValidRegion> broadcast_shape_and_valid_region(const Infos &...infos)
{
TensorShape bc_shape = TensorShape::broadcast_shape(infos.tensor_shape()...);
ValidRegion bc_valid_region{Coordinates(), bc_shape};
auto broadcast_valid_region = [&bc_valid_region](const ITensorInfo &info)
{
if (info.num_dimensions() != 0)
{
for (size_t d = 0; d < bc_valid_region.shape.num_dimensions(); ++d)
{
const bool is_broadcast = (info.tensor_shape()[d] == 1);
const int anchor_max = std::max(bc_valid_region.anchor[d], info.valid_region().anchor[d]);
const size_t valid_min = std::min(bc_valid_region.shape[d], info.valid_region().shape[d]);
if (!is_broadcast || (valid_min == 0))
{
bc_valid_region.anchor.set(d, anchor_max);
bc_valid_region.shape.set(d, valid_min);
}
}
}
};
utility::for_each(broadcast_valid_region, infos...);
return std::pair<TensorShape, ValidRegion>(bc_shape, bc_valid_region);
}
private:
static constexpr int32_t _dynamic_dimension = -1;
static constexpr int32_t _static_dimension = 0;
};
} // namespace arm_compute
#endif // ACL_ARM_COMPUTE_CORE_ITENSORINFO_H
|