Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Ramy Elgammal | d2d9361 | 2022-12-22 15:21:03 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2023 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 | #include "arm_compute/core/TensorInfo.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Helpers.h" |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/TensorInfo.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Validate.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 30 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 31 | #include "src/core/helpers/Utils.h" |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 32 | |
| 33 | #include <memory> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 35 | namespace arm_compute |
| 36 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | TensorInfo::TensorInfo() |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 38 | : _total_size(0), |
| 39 | _offset_first_element_in_bytes(0), |
| 40 | _strides_in_bytes(), |
| 41 | _num_channels(0), |
| 42 | _tensor_shape(), |
| 43 | _dims_state(), |
| 44 | _data_type(DataType::UNKNOWN), |
| 45 | _format(Format::UNKNOWN), |
| 46 | _is_resizable{true}, |
| 47 | _valid_region{Coordinates(), _tensor_shape}, |
| 48 | _padding{0}, |
| 49 | _quantization_info(), |
| 50 | _data_layout(DataLayout::NCHW), |
| 51 | _are_values_constant(true), |
| 52 | _id(invalid_tensor_id), |
| 53 | _lock_paddings(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 54 | { |
| 55 | } |
| 56 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 57 | TensorInfo::TensorInfo(const ITensorInfo &info) : TensorInfo() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 58 | { |
| 59 | _total_size = info.total_size(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 60 | _offset_first_element_in_bytes = info.offset_first_element_in_bytes(); |
| 61 | _strides_in_bytes = info.strides_in_bytes(); |
| 62 | _num_channels = info.num_channels(); |
| 63 | _tensor_shape = info.tensor_shape(); |
Georgios Pinitas | b14a0f0 | 2021-01-08 03:14:31 +0000 | [diff] [blame] | 64 | _dims_state = info.tensor_dims_state(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 65 | _data_type = info.data_type(); |
| 66 | _format = info.format(); |
| 67 | _is_resizable = info.is_resizable(); |
| 68 | _valid_region = info.valid_region(); |
| 69 | _padding = info.padding(); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 70 | _quantization_info = info.quantization_info(); |
Isabella Gottardi | d17a677 | 2018-02-27 17:41:55 +0000 | [diff] [blame] | 71 | _data_layout = info.data_layout(); |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 72 | _are_values_constant = info.are_values_constant(); |
SiCong Li | 5a2bc01 | 2023-01-12 12:54:49 +0000 | [diff] [blame] | 73 | _id = info.id(); |
Ramy Elgammal | d2d9361 | 2022-12-22 15:21:03 +0000 | [diff] [blame] | 74 | _lock_paddings = info.lock_paddings(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 77 | TensorInfo::TensorInfo(const TensorInfo &info) : TensorInfo() |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 78 | { |
| 79 | _total_size = info.total_size(); |
| 80 | _offset_first_element_in_bytes = info.offset_first_element_in_bytes(); |
| 81 | _strides_in_bytes = info.strides_in_bytes(); |
| 82 | _num_channels = info.num_channels(); |
| 83 | _tensor_shape = info.tensor_shape(); |
| 84 | _dims_state = info.tensor_dims_state(); |
| 85 | _data_type = info.data_type(); |
| 86 | _format = info.format(); |
| 87 | _is_resizable = info.is_resizable(); |
| 88 | _valid_region = info.valid_region(); |
| 89 | _padding = info.padding(); |
| 90 | _quantization_info = info.quantization_info(); |
| 91 | _data_layout = info.data_layout(); |
| 92 | _are_values_constant = info.are_values_constant(); |
SiCong Li | 5a2bc01 | 2023-01-12 12:54:49 +0000 | [diff] [blame] | 93 | _id = info.id(); |
Ramy Elgammal | d2d9361 | 2022-12-22 15:21:03 +0000 | [diff] [blame] | 94 | _lock_paddings = false; |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 95 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 96 | TensorInfo::TensorInfo(Format format) : TensorInfo(TensorShape(), format) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | { |
| 98 | } |
| 99 | |
| 100 | TensorInfo::TensorInfo(unsigned int width, unsigned int height, Format format) |
| 101 | : TensorInfo(TensorShape(width, height), format) |
| 102 | { |
| 103 | } |
| 104 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 105 | TensorInfo::TensorInfo(const TensorShape &tensor_shape, Format format) : TensorInfo() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 106 | { |
| 107 | init(tensor_shape, format); |
| 108 | } |
| 109 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 110 | TensorInfo::TensorInfo(size_t num_channels, DataType data_type) : TensorInfo() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | { |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 112 | init(TensorShape(), num_channels, data_type); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 115 | TensorInfo::TensorInfo(const TensorShape &tensor_shape, size_t num_channels, DataType data_type) : TensorInfo() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 116 | { |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 117 | init(tensor_shape, num_channels, data_type); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 120 | TensorInfo::TensorInfo(const TensorShape &tensor_shape, |
| 121 | size_t num_channels, |
| 122 | DataType data_type, |
| 123 | QuantizationInfo quantization_info) |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 124 | : TensorInfo() |
| 125 | { |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 126 | init(tensor_shape, num_channels, data_type); |
Michalis Spyrou | 18574c1 | 2019-06-05 10:51:07 +0100 | [diff] [blame] | 127 | _quantization_info = std::move(quantization_info); |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Manuel Bottini | 581f178 | 2019-11-13 17:24:43 +0000 | [diff] [blame] | 130 | TensorInfo::TensorInfo(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, DataLayout data_layout) |
| 131 | : TensorInfo() |
| 132 | { |
| 133 | init(tensor_shape, num_channels, data_type); |
| 134 | _data_layout = data_layout; |
| 135 | } |
| 136 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 137 | void TensorInfo::init(Format format) |
| 138 | { |
| 139 | init(TensorShape(), format); |
| 140 | } |
| 141 | |
| 142 | void TensorInfo::init(const TensorShape &tensor_shape, Format format) |
| 143 | { |
| 144 | size_t num_channels = num_channels_from_format(format); |
| 145 | const DataType type = data_type_from_format(format); |
| 146 | |
| 147 | init(tensor_shape, num_channels, type); |
| 148 | |
| 149 | _format = format; |
| 150 | } |
| 151 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 152 | void TensorInfo::init(const TensorShape &tensor_shape, |
| 153 | Format format, |
| 154 | const Strides &strides_in_bytes, |
| 155 | size_t offset_first_element_in_bytes, |
| 156 | size_t total_size_in_bytes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 157 | { |
| 158 | size_t num_channels = num_channels_from_format(format); |
| 159 | const DataType type = data_type_from_format(format); |
| 160 | |
| 161 | init(tensor_shape, num_channels, type, strides_in_bytes, offset_first_element_in_bytes, total_size_in_bytes); |
| 162 | |
| 163 | _format = format; |
| 164 | } |
| 165 | |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 166 | void TensorInfo::init(size_t num_channels, DataType data_type) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 167 | { |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 168 | init(TensorShape(), num_channels, data_type); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 171 | void TensorInfo::init(const TensorShape &tensor_shape, size_t num_channels, DataType data_type) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 172 | { |
| 173 | ARM_COMPUTE_ERROR_ON(num_channels == 0); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 174 | |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 175 | _data_type = data_type; |
| 176 | _num_channels = num_channels; |
| 177 | _format = Format::UNKNOWN; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 178 | |
| 179 | set_tensor_shape(tensor_shape); |
| 180 | } |
| 181 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 182 | void TensorInfo::init(const TensorShape &tensor_shape, |
| 183 | size_t num_channels, |
| 184 | DataType data_type, |
| 185 | const Strides &strides_in_bytes, |
| 186 | size_t offset_first_element_in_bytes, |
| 187 | size_t total_size_in_bytes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 188 | { |
| 189 | ARM_COMPUTE_ERROR_ON(num_channels == 0); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 190 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 191 | _data_type = data_type; |
| 192 | _num_channels = num_channels; |
| 193 | _format = Format::UNKNOWN; |
| 194 | _tensor_shape = tensor_shape; |
| 195 | _offset_first_element_in_bytes = offset_first_element_in_bytes; |
| 196 | _strides_in_bytes = strides_in_bytes; |
| 197 | _total_size = total_size_in_bytes; |
| 198 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 199 | _valid_region = ValidRegion{Coordinates(), _tensor_shape}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 202 | size_t TensorInfo::init_auto_padding(const TensorShape &tensor_shape, Format format) |
| 203 | { |
| 204 | const size_t num_channels = num_channels_from_format(format); |
| 205 | const DataType type = data_type_from_format(format); |
| 206 | size_t total_size = init_auto_padding(tensor_shape, num_channels, type); |
| 207 | |
| 208 | _format = format; |
| 209 | |
| 210 | return total_size; |
| 211 | } |
| 212 | |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 213 | size_t TensorInfo::init_auto_padding(const TensorShape &tensor_shape, size_t num_channels, DataType data_type) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 214 | { |
| 215 | ARM_COMPUTE_ERROR_ON(num_channels == 0); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 216 | |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 217 | _data_type = data_type; |
| 218 | _num_channels = num_channels; |
| 219 | _format = Format::UNKNOWN; |
| 220 | _tensor_shape = tensor_shape; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 221 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 222 | _valid_region = ValidRegion{Coordinates(), _tensor_shape}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 223 | |
| 224 | auto_padding(); |
| 225 | |
| 226 | return _total_size; |
| 227 | } |
| 228 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 229 | bool TensorInfo::auto_padding() |
| 230 | { |
| 231 | ARM_COMPUTE_ERROR_ON(!_is_resizable); |
| 232 | |
| 233 | // Some kernels compute 32 elements at the time, worst case scenario they |
| 234 | // will read 32 values after the last element |
| 235 | const size_t extra_pad_x = _tensor_shape.num_dimensions() < 1 ? 0 : 32; |
| 236 | const size_t pad_x = _tensor_shape.num_dimensions() < 1 ? 0 : 4; |
| 237 | const size_t pad_y = _tensor_shape.num_dimensions() < 2 ? 0 : 4; |
| 238 | |
| 239 | return extend_padding(PaddingSize(pad_y, pad_x + extra_pad_x, pad_y, pad_x)); |
| 240 | } |
| 241 | |
| 242 | std::tuple<Strides, size_t, size_t> TensorInfo::calculate_padding_requirements(const PaddingSize &padding) |
| 243 | { |
| 244 | // Calculate resulting stride for the X, Y and Z dimension |
| 245 | const size_t stride_x = element_size(); |
| 246 | const size_t stride_y = (padding.left + _tensor_shape[0] + padding.right) * stride_x; |
| 247 | const size_t stride_z = (padding.top + _tensor_shape[1] + padding.bottom) * stride_y; |
| 248 | |
| 249 | Strides required_strides; |
| 250 | size_t required_total_size = 0; |
| 251 | const size_t required_offset_first_element = padding.left * stride_x + padding.top * stride_y; |
| 252 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 253 | switch (_tensor_shape.num_dimensions()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 254 | { |
| 255 | case 0: |
| 256 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 257 | if (_tensor_shape.total_size() > 0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 258 | { |
Gian Marco Iodice | ee94f6a | 2017-09-11 17:38:02 +0100 | [diff] [blame] | 259 | required_strides = Strides(stride_x, stride_x); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 260 | required_total_size = stride_z; |
| 261 | } |
| 262 | break; |
| 263 | } |
| 264 | case 1: |
Gian Marco Iodice | ee94f6a | 2017-09-11 17:38:02 +0100 | [diff] [blame] | 265 | required_strides = compute_strides(*this, stride_x, stride_y); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 266 | required_total_size = stride_z; |
| 267 | break; |
| 268 | case 2: |
| 269 | required_strides = compute_strides(*this, stride_x, stride_y); |
| 270 | required_total_size = stride_z; |
| 271 | break; |
| 272 | default: |
| 273 | { |
| 274 | required_strides = compute_strides(*this, stride_x, stride_y, stride_z); |
| 275 | |
| 276 | const unsigned int idx_last_dimension = _tensor_shape.num_dimensions() - 1; |
| 277 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 278 | required_total_size = |
| 279 | static_cast<size_t>(_tensor_shape[idx_last_dimension]) * required_strides[idx_last_dimension]; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 280 | break; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | return std::make_tuple(required_strides, required_offset_first_element, required_total_size); |
| 285 | } |
| 286 | |
Ramy Elgammal | d2d9361 | 2022-12-22 15:21:03 +0000 | [diff] [blame] | 287 | ITensorInfo &TensorInfo::set_lock_paddings(bool flag) |
| 288 | { |
| 289 | _lock_paddings = flag; |
| 290 | return *this; |
| 291 | } |
| 292 | |
| 293 | bool TensorInfo::lock_paddings() const |
| 294 | { |
| 295 | return _lock_paddings; |
| 296 | } |
| 297 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 298 | bool TensorInfo::extend_padding(const PaddingSize &padding) |
| 299 | { |
Ramy Elgammal | d2d9361 | 2022-12-22 15:21:03 +0000 | [diff] [blame] | 300 | ARM_COMPUTE_ERROR_ON(_lock_paddings); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 301 | ARM_COMPUTE_ERROR_ON(!_is_resizable); |
| 302 | |
| 303 | bool updated = false; |
| 304 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 305 | if (padding.top > _padding.top) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 306 | { |
| 307 | _padding.top = padding.top; |
| 308 | updated = true; |
| 309 | } |
| 310 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 311 | if (padding.right > _padding.right) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 312 | { |
| 313 | _padding.right = padding.right; |
| 314 | updated = true; |
| 315 | } |
| 316 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 317 | if (padding.bottom > _padding.bottom) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 318 | { |
| 319 | _padding.bottom = padding.bottom; |
| 320 | updated = true; |
| 321 | } |
| 322 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 323 | if (padding.left > _padding.left) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 324 | { |
| 325 | _padding.left = padding.left; |
| 326 | updated = true; |
| 327 | } |
| 328 | |
| 329 | std::tie(_strides_in_bytes, _offset_first_element_in_bytes, _total_size) = calculate_padding_requirements(_padding); |
| 330 | |
| 331 | return updated; |
| 332 | } |
| 333 | |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 334 | std::unique_ptr<ITensorInfo> TensorInfo::clone() const |
| 335 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 336 | return std::make_unique<TensorInfo>(*this); |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | ITensorInfo &TensorInfo::set_data_type(DataType data_type) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 340 | { |
| 341 | _data_type = data_type; |
| 342 | _format = Format::UNKNOWN; |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 343 | return set_tensor_shape(tensor_shape()); // Force total size and strides to update |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 344 | } |
| 345 | |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 346 | ITensorInfo &TensorInfo::set_num_channels(int num_channels) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 347 | { |
| 348 | _num_channels = num_channels; |
| 349 | _format = Format::UNKNOWN; |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 350 | return *this; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 351 | } |
| 352 | |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 353 | ITensorInfo &TensorInfo::set_format(Format format) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 354 | { |
| 355 | _format = format; |
| 356 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 357 | if (_data_type == DataType::UNKNOWN) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 358 | { |
| 359 | _num_channels = num_channels_from_format(format); |
| 360 | _data_type = data_type_from_format(format); |
| 361 | } |
| 362 | else |
| 363 | { |
| 364 | ARM_COMPUTE_ERROR_ON(num_channels_from_format(format) != _num_channels); |
| 365 | ARM_COMPUTE_ERROR_ON(data_type_from_format(format) != _data_type); |
| 366 | } |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 367 | return *this; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 368 | } |
| 369 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 370 | ITensorInfo &TensorInfo::set_tensor_shape(const TensorShape &shape) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 371 | { |
| 372 | _tensor_shape = shape; |
| 373 | _offset_first_element_in_bytes = 0; |
| 374 | _strides_in_bytes = compute_strides(*this); |
| 375 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 376 | if (_tensor_shape.num_dimensions() == 0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 377 | { |
| 378 | _total_size = _strides_in_bytes[0]; |
| 379 | } |
| 380 | else |
| 381 | { |
| 382 | const unsigned int idx_last_dimension = _tensor_shape.num_dimensions() - 1; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 383 | _total_size = static_cast<size_t>(_tensor_shape[idx_last_dimension]) * _strides_in_bytes[idx_last_dimension]; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 384 | } |
| 385 | |
Georgios Pinitas | 652bde5 | 2018-01-10 15:33:28 +0000 | [diff] [blame] | 386 | std::tie(_strides_in_bytes, _offset_first_element_in_bytes, _total_size) = calculate_padding_requirements(_padding); |
| 387 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 388 | _valid_region = ValidRegion{Coordinates(), _tensor_shape}; |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 389 | return *this; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 390 | } |
| 391 | |
Georgios Pinitas | b14a0f0 | 2021-01-08 03:14:31 +0000 | [diff] [blame] | 392 | ITensorInfo &TensorInfo::set_tensor_dims_state(const TensorDimsState &state) |
| 393 | { |
| 394 | _dims_state = state; |
| 395 | return *this; |
| 396 | } |
| 397 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 398 | ITensorInfo &TensorInfo::set_quantization_info(const QuantizationInfo &quantization_info) |
Georgios Pinitas | 283c179 | 2017-11-10 18:14:06 +0000 | [diff] [blame] | 399 | { |
| 400 | _quantization_info = quantization_info; |
| 401 | return *this; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 402 | } |
| 403 | |
Isabella Gottardi | d17a677 | 2018-02-27 17:41:55 +0000 | [diff] [blame] | 404 | ITensorInfo &TensorInfo::set_data_layout(const DataLayout &data_layout) |
| 405 | { |
| 406 | _data_layout = data_layout; |
| 407 | return *this; |
| 408 | } |
| 409 | |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 410 | ITensorInfo &TensorInfo::reset_padding() |
| 411 | { |
| 412 | _padding = PaddingSize(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 413 | if (((_format != Format::UNKNOWN) || (_data_type != DataType::UNKNOWN)) && _total_size != 0) |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 414 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 415 | std::tie(_strides_in_bytes, _offset_first_element_in_bytes, _total_size) = |
| 416 | calculate_padding_requirements(_padding); |
Georgios Pinitas | 30902ed | 2017-11-14 15:32:57 +0000 | [diff] [blame] | 417 | } |
| 418 | return *this; |
| 419 | } |
| 420 | |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 421 | int32_t TensorInfo::offset_element_in_bytes(const Coordinates &pos) const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 422 | { |
| 423 | ARM_COMPUTE_ERROR_ON_COORDINATES_DIMENSIONS_GTE(pos, _tensor_shape.num_dimensions()); |
| 424 | |
Michalis Spyrou | 7c60c99 | 2019-10-10 14:33:47 +0100 | [diff] [blame] | 425 | int32_t offset = _offset_first_element_in_bytes; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 426 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 427 | for (size_t i = 0; i < _tensor_shape.num_dimensions(); ++i) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 428 | { |
| 429 | offset += pos[i] * _strides_in_bytes[i]; |
| 430 | } |
| 431 | |
| 432 | return offset; |
| 433 | } |
Giorgio Arena | 63e0beb | 2021-09-24 14:04:27 +0100 | [diff] [blame] | 434 | } // namespace arm_compute |