Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 2017 ARM Limited. |
| 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_UTILS_H__ |
| 25 | #define __ARM_COMPUTE_UTILS_H__ |
| 26 | |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | |
| 30 | #include <algorithm> |
| 31 | #include <cstdint> |
| 32 | #include <cstdlib> |
| 33 | #include <numeric> |
| 34 | #include <sstream> |
| 35 | #include <string> |
| 36 | #include <type_traits> |
| 37 | #include <utility> |
steniu01 | 7ce53c6 | 2017-09-29 14:55:00 +0100 | [diff] [blame] | 38 | #include <vector> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | /** Computes the smallest number larger or equal to value that is a multiple of divisor. */ |
| 43 | template <typename S, typename T> |
| 44 | inline auto ceil_to_multiple(S value, T divisor) -> decltype(((value + divisor - 1) / divisor) * divisor) |
| 45 | { |
| 46 | ARM_COMPUTE_ERROR_ON(value < 0 || divisor <= 0); |
| 47 | return ((value + divisor - 1) / divisor) * divisor; |
| 48 | } |
| 49 | |
| 50 | /** Computes the largest number smaller or equal to value that is a multiple of divisor. */ |
| 51 | template <typename S, typename T> |
| 52 | inline auto floor_to_multiple(S value, T divisor) -> decltype((value / divisor) * divisor) |
| 53 | { |
| 54 | ARM_COMPUTE_ERROR_ON(value < 0 || divisor <= 0); |
| 55 | return (value / divisor) * divisor; |
| 56 | } |
| 57 | |
| 58 | /** Calculate the rounded up quotient of val / m. */ |
| 59 | template <typename S, typename T> |
| 60 | constexpr auto DIV_CEIL(S val, T m) -> decltype((val + m - 1) / m) |
| 61 | { |
| 62 | return (val + m - 1) / m; |
| 63 | } |
| 64 | |
| 65 | /** Returns the arm_compute library build information |
| 66 | * |
| 67 | * Contains the version number and the build options used to build the library |
| 68 | * |
| 69 | * @return The arm_compute library build information |
| 70 | */ |
| 71 | std::string build_information(); |
| 72 | |
| 73 | /** Load an entire file in memory |
| 74 | * |
| 75 | * @param[in] filename Name of the file to read. |
| 76 | * @param[in] binary Is it a binary file ? |
| 77 | * |
| 78 | * @return The content of the file. |
| 79 | */ |
| 80 | std::string read_file(const std::string &filename, bool binary); |
| 81 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 82 | /** The size in bytes of the data type |
| 83 | * |
| 84 | * @param[in] data_type Input data type |
| 85 | * |
| 86 | * @return The size in bytes of the data type |
| 87 | */ |
| 88 | inline size_t data_size_from_type(DataType data_type) |
| 89 | { |
| 90 | switch(data_type) |
| 91 | { |
| 92 | case DataType::U8: |
| 93 | case DataType::S8: |
| 94 | case DataType::QS8: |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 95 | case DataType::QASYMM8: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | return 1; |
| 97 | case DataType::U16: |
| 98 | case DataType::S16: |
| 99 | case DataType::F16: |
| 100 | case DataType::QS16: |
| 101 | return 2; |
| 102 | case DataType::F32: |
| 103 | case DataType::U32: |
| 104 | case DataType::S32: |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 105 | case DataType::QS32: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 106 | return 4; |
| 107 | case DataType::F64: |
| 108 | case DataType::U64: |
| 109 | case DataType::S64: |
| 110 | return 8; |
| 111 | case DataType::SIZET: |
| 112 | return sizeof(size_t); |
| 113 | default: |
| 114 | ARM_COMPUTE_ERROR("Invalid data type"); |
| 115 | return 0; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** The size in bytes of the pixel format |
| 120 | * |
| 121 | * @param[in] format Input format |
| 122 | * |
| 123 | * @return The size in bytes of the pixel format |
| 124 | */ |
| 125 | inline size_t pixel_size_from_format(Format format) |
| 126 | { |
| 127 | switch(format) |
| 128 | { |
| 129 | case Format::U8: |
| 130 | return 1; |
| 131 | case Format::U16: |
| 132 | case Format::S16: |
| 133 | case Format::F16: |
| 134 | case Format::UV88: |
| 135 | case Format::YUYV422: |
| 136 | case Format::UYVY422: |
| 137 | return 2; |
| 138 | case Format::RGB888: |
| 139 | return 3; |
| 140 | case Format::RGBA8888: |
| 141 | return 4; |
| 142 | case Format::U32: |
| 143 | case Format::S32: |
| 144 | case Format::F32: |
| 145 | return 4; |
| 146 | //Doesn't make sense for planar formats: |
| 147 | case Format::NV12: |
| 148 | case Format::NV21: |
| 149 | case Format::IYUV: |
| 150 | case Format::YUV444: |
| 151 | default: |
| 152 | ARM_COMPUTE_ERROR("Undefined pixel size for given format"); |
| 153 | return 0; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** The size in bytes of the data type |
| 158 | * |
| 159 | * @param[in] dt Input data type |
| 160 | * |
| 161 | * @return The size in bytes of the data type |
| 162 | */ |
| 163 | inline size_t element_size_from_data_type(DataType dt) |
| 164 | { |
| 165 | switch(dt) |
| 166 | { |
| 167 | case DataType::S8: |
| 168 | case DataType::U8: |
| 169 | case DataType::QS8: |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 170 | case DataType::QASYMM8: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 171 | return 1; |
| 172 | case DataType::U16: |
| 173 | case DataType::S16: |
| 174 | case DataType::QS16: |
| 175 | case DataType::F16: |
| 176 | return 2; |
| 177 | case DataType::U32: |
| 178 | case DataType::S32: |
| 179 | case DataType::F32: |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 180 | case DataType::QS32: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 181 | return 4; |
| 182 | default: |
| 183 | ARM_COMPUTE_ERROR("Undefined element size for given data type"); |
| 184 | return 0; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** Return the data type used by a given single-planar pixel format |
| 189 | * |
| 190 | * @param[in] format Input format |
| 191 | * |
| 192 | * @return The size in bytes of the pixel format |
| 193 | */ |
| 194 | inline DataType data_type_from_format(Format format) |
| 195 | { |
| 196 | switch(format) |
| 197 | { |
| 198 | case Format::U8: |
| 199 | case Format::UV88: |
| 200 | case Format::RGB888: |
| 201 | case Format::RGBA8888: |
| 202 | case Format::YUYV422: |
| 203 | case Format::UYVY422: |
| 204 | return DataType::U8; |
| 205 | case Format::U16: |
| 206 | return DataType::U16; |
| 207 | case Format::S16: |
| 208 | return DataType::S16; |
| 209 | case Format::U32: |
| 210 | return DataType::U32; |
| 211 | case Format::S32: |
| 212 | return DataType::S32; |
| 213 | case Format::F16: |
| 214 | return DataType::F16; |
| 215 | case Format::F32: |
| 216 | return DataType::F32; |
| 217 | //Doesn't make sense for planar formats: |
| 218 | case Format::NV12: |
| 219 | case Format::NV21: |
| 220 | case Format::IYUV: |
| 221 | case Format::YUV444: |
| 222 | default: |
| 223 | ARM_COMPUTE_ERROR("Not supported data_type for given format"); |
| 224 | return DataType::UNKNOWN; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** Return the plane index of a given channel given an input format. |
| 229 | * |
| 230 | * @param[in] format Input format |
| 231 | * @param[in] channel Input channel |
| 232 | * |
| 233 | * @return The plane index of the specific channel of the specific format |
| 234 | */ |
| 235 | inline int plane_idx_from_channel(Format format, Channel channel) |
| 236 | { |
| 237 | switch(format) |
| 238 | { |
| 239 | case Format::NV12: |
| 240 | case Format::NV21: |
| 241 | { |
| 242 | switch(channel) |
| 243 | { |
| 244 | case Channel::Y: |
| 245 | return 0; |
| 246 | case Channel::U: |
| 247 | case Channel::V: |
| 248 | return 1; |
| 249 | default: |
| 250 | ARM_COMPUTE_ERROR("Not supported channel"); |
| 251 | return 0; |
| 252 | } |
| 253 | } |
| 254 | case Format::IYUV: |
| 255 | case Format::YUV444: |
| 256 | { |
| 257 | switch(channel) |
| 258 | { |
| 259 | case Channel::Y: |
| 260 | return 0; |
| 261 | case Channel::U: |
| 262 | return 1; |
| 263 | case Channel::V: |
| 264 | return 2; |
| 265 | default: |
| 266 | ARM_COMPUTE_ERROR("Not supported channel"); |
| 267 | return 0; |
| 268 | } |
| 269 | } |
| 270 | default: |
| 271 | ARM_COMPUTE_ERROR("Not supported format"); |
| 272 | return 0; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /** Return the number of planes for a given format |
| 277 | * |
| 278 | * @param[in] format Input format |
| 279 | * |
| 280 | * @return The number of planes for a given image format. |
| 281 | */ |
| 282 | inline size_t num_planes_from_format(Format format) |
| 283 | { |
| 284 | switch(format) |
| 285 | { |
| 286 | case Format::U8: |
| 287 | case Format::S16: |
| 288 | case Format::U16: |
| 289 | case Format::S32: |
| 290 | case Format::U32: |
| 291 | case Format::F16: |
| 292 | case Format::F32: |
| 293 | case Format::RGB888: |
| 294 | case Format::RGBA8888: |
| 295 | case Format::YUYV422: |
| 296 | case Format::UYVY422: |
| 297 | return 1; |
| 298 | case Format::NV12: |
| 299 | case Format::NV21: |
| 300 | return 2; |
| 301 | case Format::IYUV: |
| 302 | case Format::YUV444: |
| 303 | return 3; |
| 304 | default: |
| 305 | ARM_COMPUTE_ERROR("Not supported format"); |
| 306 | return 0; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /** Return the number of channels for a given single-planar pixel format |
| 311 | * |
| 312 | * @param[in] format Input format |
| 313 | * |
| 314 | * @return The number of channels for a given image format. |
| 315 | */ |
| 316 | inline size_t num_channels_from_format(Format format) |
| 317 | { |
| 318 | switch(format) |
| 319 | { |
| 320 | case Format::U8: |
| 321 | case Format::U16: |
| 322 | case Format::S16: |
| 323 | case Format::U32: |
| 324 | case Format::S32: |
| 325 | case Format::F16: |
| 326 | case Format::F32: |
| 327 | return 1; |
| 328 | // Because the U and V channels are subsampled |
| 329 | // these formats appear like having only 2 channels: |
| 330 | case Format::YUYV422: |
| 331 | case Format::UYVY422: |
| 332 | return 2; |
| 333 | case Format::UV88: |
| 334 | return 2; |
| 335 | case Format::RGB888: |
| 336 | return 3; |
| 337 | case Format::RGBA8888: |
| 338 | return 4; |
| 339 | //Doesn't make sense for planar formats: |
| 340 | case Format::NV12: |
| 341 | case Format::NV21: |
| 342 | case Format::IYUV: |
| 343 | case Format::YUV444: |
| 344 | default: |
| 345 | return 0; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /** Separate a 2D convolution into two 1D convolutions |
| 350 | * |
| 351 | * @param[in] conv 2D convolution |
| 352 | * @param[out] conv_col 1D vertical convolution |
| 353 | * @param[out] conv_row 1D horizontal convolution |
| 354 | * @param[in] size Size of the 2D convolution |
| 355 | * |
| 356 | * @return true if the separation was successful |
| 357 | */ |
| 358 | inline bool separate_matrix(const int16_t *conv, int16_t *conv_col, int16_t *conv_row, uint8_t size) |
| 359 | { |
| 360 | int32_t min_col = -1; |
| 361 | int16_t min_col_val = -1; |
| 362 | |
| 363 | for(int32_t i = 0; i < size; ++i) |
| 364 | { |
| 365 | if(conv[i] != 0 && (min_col < 0 || abs(min_col_val) > abs(conv[i]))) |
| 366 | { |
| 367 | min_col = i; |
| 368 | min_col_val = conv[i]; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | if(min_col < 0) |
| 373 | { |
| 374 | return false; |
| 375 | } |
| 376 | |
| 377 | for(uint32_t j = 0; j < size; ++j) |
| 378 | { |
| 379 | conv_col[j] = conv[min_col + j * size]; |
| 380 | } |
| 381 | |
| 382 | for(uint32_t i = 0; i < size; i++) |
| 383 | { |
| 384 | if(static_cast<int>(i) == min_col) |
| 385 | { |
| 386 | conv_row[i] = 1; |
| 387 | } |
| 388 | else |
| 389 | { |
| 390 | int16_t coeff = conv[i] / conv[min_col]; |
| 391 | |
| 392 | for(uint32_t j = 1; j < size; ++j) |
| 393 | { |
| 394 | if(conv[i + j * size] != (conv_col[j] * coeff)) |
| 395 | { |
| 396 | return false; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | conv_row[i] = coeff; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | return true; |
| 405 | } |
| 406 | |
| 407 | /** Calculate the scale of the given square matrix |
| 408 | * |
| 409 | * The scale is the absolute value of the sum of all the coefficients in the matrix. |
| 410 | * |
| 411 | * @note If the coefficients add up to 0 then the scale is set to 1. |
| 412 | * |
| 413 | * @param[in] matrix Matrix coefficients |
| 414 | * @param[in] matrix_size Number of elements per side of the square matrix. (Number of coefficients = matrix_size * matrix_size). |
| 415 | * |
| 416 | * @return The absolute value of the sum of the coefficients if they don't add up to 0, otherwise 1. |
| 417 | */ |
| 418 | inline uint32_t calculate_matrix_scale(const int16_t *matrix, unsigned int matrix_size) |
| 419 | { |
| 420 | const size_t size = matrix_size * matrix_size; |
| 421 | |
| 422 | return std::max(1, std::abs(std::accumulate(matrix, matrix + size, 0))); |
| 423 | } |
| 424 | |
steniu01 | 7ce53c6 | 2017-09-29 14:55:00 +0100 | [diff] [blame] | 425 | /** Calculate the output shapes of the depth concatenate function. |
| 426 | * |
| 427 | * @param[in] inputs_vector The vector that stores all the pointers to input. |
| 428 | * |
| 429 | * @return the output shape |
| 430 | */ |
| 431 | template <typename T> |
| 432 | TensorShape calculate_depth_concatenate_shape(const std::vector<T *> &inputs_vector) |
| 433 | { |
| 434 | TensorShape out_shape = inputs_vector[0]->info()->tensor_shape(); |
| 435 | |
| 436 | size_t max_x = 0; |
| 437 | size_t max_y = 0; |
| 438 | size_t depth = 0; |
| 439 | |
| 440 | for(const auto &tensor : inputs_vector) |
| 441 | { |
| 442 | ARM_COMPUTE_ERROR_ON(tensor == nullptr); |
| 443 | const TensorShape shape = tensor->info()->tensor_shape(); |
| 444 | max_x = std::max(shape.x(), max_x); |
| 445 | max_y = std::max(shape.y(), max_y); |
| 446 | depth += shape.z(); |
| 447 | } |
| 448 | |
| 449 | out_shape.set(0, max_x); |
| 450 | out_shape.set(1, max_y); |
| 451 | out_shape.set(2, depth); |
| 452 | |
| 453 | return out_shape; |
| 454 | } |
| 455 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 456 | /** Calculate accurary required by the horizontal and vertical convolution computations |
| 457 | * |
| 458 | * @param[in] conv_col Pointer to the vertical vector of the separated convolution filter |
| 459 | * @param[in] conv_row Pointer to the horizontal vector of the convolution filter |
| 460 | * @param[in] size Number of elements per vector of the separated matrix |
| 461 | * |
| 462 | * @return The return type is a pair. The first element of the pair is the biggest data type needed for the first stage. The second |
| 463 | * element of the pair is the biggest data type needed for the second stage. |
| 464 | */ |
| 465 | inline std::pair<DataType, DataType> data_type_for_convolution(const int16_t *conv_col, const int16_t *conv_row, size_t size) |
| 466 | { |
| 467 | DataType first_stage = DataType::UNKNOWN; |
| 468 | DataType second_stage = DataType::UNKNOWN; |
| 469 | |
| 470 | auto gez = [](const int16_t &v) |
| 471 | { |
| 472 | return v >= 0; |
| 473 | }; |
| 474 | |
| 475 | auto accu_neg = [](const int &first, const int &second) |
| 476 | { |
| 477 | return first + (second < 0 ? second : 0); |
| 478 | }; |
| 479 | |
| 480 | auto accu_pos = [](const int &first, const int &second) |
| 481 | { |
| 482 | return first + (second > 0 ? second : 0); |
| 483 | }; |
| 484 | |
| 485 | const bool only_positive_coefficients = std::all_of(conv_row, conv_row + size, gez) && std::all_of(conv_col, conv_col + size, gez); |
| 486 | |
| 487 | if(only_positive_coefficients) |
| 488 | { |
| 489 | const int max_row_value = std::accumulate(conv_row, conv_row + size, 0) * UINT8_MAX; |
| 490 | const int max_value = std::accumulate(conv_col, conv_col + size, 0) * max_row_value; |
| 491 | |
| 492 | first_stage = (max_row_value <= UINT16_MAX) ? DataType::U16 : DataType::S32; |
| 493 | |
| 494 | second_stage = (max_value <= UINT16_MAX) ? DataType::U16 : DataType::S32; |
| 495 | } |
| 496 | else |
| 497 | { |
| 498 | const int min_row_value = std::accumulate(conv_row, conv_row + size, 0, accu_neg) * UINT8_MAX; |
| 499 | const int max_row_value = std::accumulate(conv_row, conv_row + size, 0, accu_pos) * UINT8_MAX; |
| 500 | const int neg_coeffs_sum = std::accumulate(conv_col, conv_col + size, 0, accu_neg); |
| 501 | const int pos_coeffs_sum = std::accumulate(conv_col, conv_col + size, 0, accu_pos); |
| 502 | const int min_value = neg_coeffs_sum * max_row_value + pos_coeffs_sum * min_row_value; |
| 503 | const int max_value = neg_coeffs_sum * min_row_value + pos_coeffs_sum * max_row_value; |
| 504 | |
| 505 | first_stage = ((INT16_MIN <= min_row_value) && (max_row_value <= INT16_MAX)) ? DataType::S16 : DataType::S32; |
| 506 | |
| 507 | second_stage = ((INT16_MIN <= min_value) && (max_value <= INT16_MAX)) ? DataType::S16 : DataType::S32; |
| 508 | } |
| 509 | |
| 510 | return std::make_pair(first_stage, second_stage); |
| 511 | } |
| 512 | |
| 513 | /** Calculate the accuracy required by the squared convolution calculation. |
| 514 | * |
| 515 | * |
| 516 | * @param[in] conv Pointer to the squared convolution matrix |
| 517 | * @param[in] size The total size of the convolution matrix |
| 518 | * |
| 519 | * @return The return is the biggest data type needed to do the convolution |
| 520 | */ |
| 521 | inline DataType data_type_for_convolution_matrix(const int16_t *conv, size_t size) |
| 522 | { |
| 523 | auto gez = [](const int16_t v) |
| 524 | { |
| 525 | return v >= 0; |
| 526 | }; |
| 527 | |
| 528 | const bool only_positive_coefficients = std::all_of(conv, conv + size, gez); |
| 529 | |
| 530 | if(only_positive_coefficients) |
| 531 | { |
| 532 | const int max_conv_value = std::accumulate(conv, conv + size, 0) * UINT8_MAX; |
| 533 | if(max_conv_value <= UINT16_MAX) |
| 534 | { |
| 535 | return DataType::U16; |
| 536 | } |
| 537 | else |
| 538 | { |
| 539 | return DataType::S32; |
| 540 | } |
| 541 | } |
| 542 | else |
| 543 | { |
| 544 | const int min_value = std::accumulate(conv, conv + size, 0, [](int a, int b) |
| 545 | { |
| 546 | return b < 0 ? a + b : a; |
| 547 | }) |
| 548 | * UINT8_MAX; |
| 549 | |
| 550 | const int max_value = std::accumulate(conv, conv + size, 0, [](int a, int b) |
| 551 | { |
| 552 | return b > 0 ? a + b : a; |
| 553 | }) |
| 554 | * UINT8_MAX; |
| 555 | |
| 556 | if((INT16_MIN <= min_value) && (INT16_MAX >= max_value)) |
| 557 | { |
| 558 | return DataType::S16; |
| 559 | } |
| 560 | else |
| 561 | { |
| 562 | return DataType::S32; |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 567 | /** Returns expected shape for the deconvolution output tensor. |
| 568 | * |
| 569 | * @param[in] out_dims widht and height of the output tensor, these values can be obtained with the function deconvolution_output_dimensions. |
| 570 | * @param[in] input Shape of the input tensor. |
| 571 | * @param[in] weights Shape of the weights tensor. |
| 572 | * |
| 573 | * @return Deconvolution output tensor shape. |
| 574 | */ |
| 575 | TensorShape deconvolution_output_shape(const std::pair<unsigned int, unsigned int> &out_dims, TensorShape input, TensorShape weights); |
| 576 | |
| 577 | /** Returns expected width and height of the deconvolution's output tensor. |
| 578 | * |
| 579 | * @param[in] in_width Width of input tensor (Number of columns) |
| 580 | * @param[in] in_height Height of input tensor (Number of rows) |
| 581 | * @param[in] kernel_width Kernel width. |
| 582 | * @param[in] kernel_height Kernel height. |
| 583 | * @param[in] padx X axis padding. |
| 584 | * @param[in] pady Y axis padding. |
| 585 | * @param[in] ax The number of zeros added to right edge of the input. |
| 586 | * @param[in] ay The number of zeros added to top edge of the input. |
| 587 | * @param[in] upscalex How much to scale the X axis. |
| 588 | * @param[in] upscaley How much to scale the Y axis. |
| 589 | * @param[in] round Rounding policy to be used when computing the output's dimensions. |
| 590 | * |
| 591 | * @return A pair with the new width in the first position and the new height in the second. |
| 592 | */ |
| 593 | |
| 594 | const std::pair<unsigned int, unsigned int> deconvolution_output_dimensions(unsigned int in_width, unsigned int in_height, |
| 595 | unsigned int kernel_width, unsigned int kernel_height, |
| 596 | unsigned int padx, unsigned int pady, unsigned int ax, unsigned int ay, |
| 597 | float upscalex, float upscaley, DimensionRoundingType round); |
| 598 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 599 | /** Returns expected width and height of output scaled tensor depending on dimensions rounding mode. |
| 600 | * |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 601 | * @param[in] width Width of input tensor (Number of columns) |
| 602 | * @param[in] height Height of input tensor (Number of rows) |
| 603 | * @param[in] kernel_width Kernel width. |
| 604 | * @param[in] kernel_height Kernel height. |
| 605 | * @param[in] pad_stride_info Pad and stride information. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 606 | * |
| 607 | * @return A pair with the new width in the first position and the new height in the second. |
| 608 | */ |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 609 | const std::pair<unsigned int, unsigned int> scaled_dimensions(unsigned int width, unsigned int height, |
| 610 | unsigned int kernel_width, unsigned int kernel_height, |
| 611 | const PadStrideInfo &pad_stride_info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 612 | |
| 613 | /** Convert a tensor format into a string. |
| 614 | * |
| 615 | * @param[in] format @ref Format to be translated to string. |
| 616 | * |
| 617 | * @return The string describing the format. |
| 618 | */ |
| 619 | const std::string &string_from_format(Format format); |
| 620 | |
| 621 | /** Convert a channel identity into a string. |
| 622 | * |
| 623 | * @param[in] channel @ref Channel to be translated to string. |
| 624 | * |
| 625 | * @return The string describing the channel. |
| 626 | */ |
| 627 | const std::string &string_from_channel(Channel channel); |
| 628 | |
| 629 | /** Convert a data type identity into a string. |
| 630 | * |
| 631 | * @param[in] dt @ref DataType to be translated to string. |
| 632 | * |
| 633 | * @return The string describing the data type. |
| 634 | */ |
| 635 | const std::string &string_from_data_type(DataType dt); |
| 636 | /** Convert a matrix pattern into a string. |
| 637 | * |
| 638 | * @param[in] pattern @ref MatrixPattern to be translated to string. |
| 639 | * |
| 640 | * @return The string describing the matrix pattern. |
| 641 | */ |
| 642 | const std::string &string_from_matrix_pattern(MatrixPattern pattern); |
| 643 | /** Translates a given activation function to a string. |
| 644 | * |
| 645 | * @param[in] act @ref ActivationLayerInfo::ActivationFunction to be translated to string. |
| 646 | * |
| 647 | * @return The string describing the activation function. |
| 648 | */ |
| 649 | const std::string &string_from_activation_func(ActivationLayerInfo::ActivationFunction act); |
| 650 | /** Translates a given non linear function to a string. |
| 651 | * |
| 652 | * @param[in] function @ref NonLinearFilterFunction to be translated to string. |
| 653 | * |
| 654 | * @return The string describing the non linear function. |
| 655 | */ |
| 656 | const std::string &string_from_non_linear_filter_function(NonLinearFilterFunction function); |
| 657 | /** Translates a given interpolation policy to a string. |
| 658 | * |
| 659 | * @param[in] policy @ref InterpolationPolicy to be translated to string. |
| 660 | * |
| 661 | * @return The string describing the interpolation policy. |
| 662 | */ |
| 663 | const std::string &string_from_interpolation_policy(InterpolationPolicy policy); |
| 664 | /** Translates a given border mode policy to a string. |
| 665 | * |
| 666 | * @param[in] border_mode @ref BorderMode to be translated to string. |
| 667 | * |
| 668 | * @return The string describing the border mode. |
| 669 | */ |
| 670 | const std::string &string_from_border_mode(BorderMode border_mode); |
| 671 | /** Translates a given normalization type to a string. |
| 672 | * |
| 673 | * @param[in] type @ref NormType to be translated to string. |
| 674 | * |
| 675 | * @return The string describing the normalization type. |
| 676 | */ |
| 677 | const std::string &string_from_norm_type(NormType type); |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 678 | /** Translates a given pooling type to a string. |
| 679 | * |
| 680 | * @param[in] type @ref PoolingType to be translated to string. |
| 681 | * |
| 682 | * @return The string describing the pooling type. |
| 683 | */ |
| 684 | const std::string &string_from_pooling_type(PoolingType type); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 685 | /** Lower a given string. |
| 686 | * |
| 687 | * @param[in] val Given string to lower. |
| 688 | * |
| 689 | * @return The lowered string |
| 690 | */ |
| 691 | std::string lower_string(const std::string &val); |
| 692 | |
| 693 | /** Check if a given data type is of floating point type |
| 694 | * |
| 695 | * @param[in] dt Input data type. |
| 696 | * |
| 697 | * @return True if data type is of floating point type, else false. |
| 698 | */ |
| 699 | inline bool is_data_type_float(DataType dt) |
| 700 | { |
| 701 | switch(dt) |
| 702 | { |
| 703 | case DataType::F16: |
| 704 | case DataType::F32: |
| 705 | return true; |
| 706 | default: |
| 707 | return false; |
| 708 | } |
| 709 | } |
| 710 | |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 711 | /** Check if a given data type is of quantized type |
| 712 | * |
| 713 | * @note Quantized is considered a super-set of fixed-point and asymmetric data types. |
| 714 | * |
| 715 | * @param[in] dt Input data type. |
| 716 | * |
| 717 | * @return True if data type is of quantized type, else false. |
| 718 | */ |
| 719 | inline bool is_data_type_quantized(DataType dt) |
| 720 | { |
| 721 | switch(dt) |
| 722 | { |
| 723 | case DataType::QS8: |
| 724 | case DataType::QASYMM8: |
| 725 | case DataType::QS16: |
| 726 | case DataType::QS32: |
| 727 | return true; |
| 728 | default: |
| 729 | return false; |
| 730 | } |
| 731 | } |
| 732 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 733 | /** Check if a given data type is of fixed point type |
| 734 | * |
| 735 | * @param[in] dt Input data type. |
| 736 | * |
| 737 | * @return True if data type is of fixed point type, else false. |
| 738 | */ |
| 739 | inline bool is_data_type_fixed_point(DataType dt) |
| 740 | { |
| 741 | switch(dt) |
| 742 | { |
| 743 | case DataType::QS8: |
| 744 | case DataType::QS16: |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 745 | case DataType::QS32: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 746 | return true; |
| 747 | default: |
| 748 | return false; |
| 749 | } |
| 750 | } |
| 751 | |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 752 | /** Check if a given data type is of asymmetric quantized type |
| 753 | * |
| 754 | * @param[in] dt Input data type. |
| 755 | * |
| 756 | * @return True if data type is of symmetric quantized type, else false. |
| 757 | */ |
| 758 | inline bool is_data_type_assymetric(DataType dt) |
| 759 | { |
| 760 | switch(dt) |
| 761 | { |
| 762 | case DataType::QASYMM8: |
| 763 | return true; |
| 764 | default: |
| 765 | return false; |
| 766 | } |
| 767 | } |
| 768 | |
Georgios Pinitas | 8901096 | 2017-08-04 14:58:27 +0100 | [diff] [blame] | 769 | /** Create a string with the float in full precision. |
| 770 | * |
| 771 | * @param val Floating point value |
| 772 | * |
| 773 | * @return String with the floating point value. |
| 774 | */ |
| 775 | inline std::string float_to_string_with_full_precision(float val) |
| 776 | { |
| 777 | std::stringstream ss; |
| 778 | ss.precision(std::numeric_limits<float>::digits10 + 1); |
| 779 | ss << val; |
| 780 | return ss.str(); |
| 781 | } |
| 782 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 783 | /** Print consecutive elements to an output stream. |
| 784 | * |
| 785 | * @param[out] s Output stream to print the elements to. |
| 786 | * @param[in] ptr Pointer to print the elements from. |
| 787 | * @param[in] n Number of elements to print. |
| 788 | * @param[in] stream_width (Optional) Width of the stream. If set to 0 the element's width is used. Defaults to 0. |
| 789 | * @param[in] element_delim (Optional) Delimeter among the consecutive elements. Defaults to space delimeter |
| 790 | */ |
| 791 | template <typename T> |
| 792 | void print_consecutive_elements_impl(std::ostream &s, const T *ptr, unsigned int n, int stream_width = 0, const std::string &element_delim = " ") |
| 793 | { |
| 794 | using print_type = typename std::conditional<std::is_floating_point<T>::value, T, int>::type; |
| 795 | |
| 796 | for(unsigned int i = 0; i < n; ++i) |
| 797 | { |
| 798 | // Set stream width as it is not a "sticky" stream manipulator |
| 799 | if(stream_width != 0) |
| 800 | { |
| 801 | s.width(stream_width); |
| 802 | } |
| 803 | s << std::right << static_cast<print_type>(ptr[i]) << element_delim; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | /** Identify the maximum width of n consecutive elements. |
| 808 | * |
| 809 | * @param[in] s The output stream which will be used to print the elements. Used to extract the stream format. |
| 810 | * @param[in] ptr Pointer to the elements. |
| 811 | * @param[in] n Number of elements. |
| 812 | * |
| 813 | * @return The maximum width of the elements. |
| 814 | */ |
| 815 | template <typename T> |
| 816 | int max_consecutive_elements_display_width_impl(std::ostream &s, const T *ptr, unsigned int n) |
| 817 | { |
| 818 | using print_type = typename std::conditional<std::is_floating_point<T>::value, T, int>::type; |
| 819 | |
| 820 | int max_width = -1; |
| 821 | for(unsigned int i = 0; i < n; ++i) |
| 822 | { |
| 823 | std::stringstream ss; |
| 824 | ss.copyfmt(s); |
| 825 | ss << static_cast<print_type>(ptr[i]); |
| 826 | max_width = std::max<int>(max_width, ss.str().size()); |
| 827 | } |
| 828 | return max_width; |
| 829 | } |
| 830 | |
| 831 | /** Print consecutive elements to an output stream. |
| 832 | * |
| 833 | * @param[out] s Output stream to print the elements to. |
| 834 | * @param[in] dt Data type of the elements |
| 835 | * @param[in] ptr Pointer to print the elements from. |
| 836 | * @param[in] n Number of elements to print. |
| 837 | * @param[in] stream_width (Optional) Width of the stream. If set to 0 the element's width is used. Defaults to 0. |
| 838 | * @param[in] element_delim (Optional) Delimeter among the consecutive elements. Defaults to space delimeter |
| 839 | */ |
| 840 | void print_consecutive_elements(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n, int stream_width, const std::string &element_delim = " "); |
| 841 | |
| 842 | /** Identify the maximum width of n consecutive elements. |
| 843 | * |
| 844 | * @param[in] s Output stream to print the elements to. |
| 845 | * @param[in] dt Data type of the elements |
| 846 | * @param[in] ptr Pointer to print the elements from. |
| 847 | * @param[in] n Number of elements to print. |
| 848 | * |
| 849 | * @return The maximum width of the elements. |
| 850 | */ |
| 851 | int max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n); |
| 852 | } |
| 853 | #endif /*__ARM_COMPUTE_UTILS_H__ */ |