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 | |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 349 | /** Return the promoted data type of a given data type. |
| 350 | * |
| 351 | * @note If promoted data type is not supported an error will be thrown |
| 352 | * |
| 353 | * @param[in] dt Data type to get the promoted type of. |
| 354 | * |
| 355 | * @return Promoted data type |
| 356 | */ |
| 357 | inline DataType get_promoted_data_type(DataType dt) |
| 358 | { |
| 359 | switch(dt) |
| 360 | { |
| 361 | case DataType::U8: |
| 362 | return DataType::U16; |
| 363 | case DataType::S8: |
| 364 | return DataType::S16; |
| 365 | case DataType::QS8: |
| 366 | return DataType::QS16; |
| 367 | case DataType::U16: |
| 368 | return DataType::U32; |
| 369 | case DataType::S16: |
| 370 | return DataType::S32; |
| 371 | case DataType::QS16: |
| 372 | return DataType::QS32; |
| 373 | case DataType::QASYMM8: |
| 374 | case DataType::F16: |
| 375 | case DataType::U32: |
| 376 | case DataType::S32: |
| 377 | case DataType::F32: |
| 378 | case DataType::QS32: |
| 379 | ARM_COMPUTE_ERROR("Unsupported data type promotions!"); |
| 380 | default: |
| 381 | ARM_COMPUTE_ERROR("Undefined data type!"); |
| 382 | } |
| 383 | return DataType::UNKNOWN; |
| 384 | } |
| 385 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 386 | /** Separate a 2D convolution into two 1D convolutions |
| 387 | * |
| 388 | * @param[in] conv 2D convolution |
| 389 | * @param[out] conv_col 1D vertical convolution |
| 390 | * @param[out] conv_row 1D horizontal convolution |
| 391 | * @param[in] size Size of the 2D convolution |
| 392 | * |
| 393 | * @return true if the separation was successful |
| 394 | */ |
| 395 | inline bool separate_matrix(const int16_t *conv, int16_t *conv_col, int16_t *conv_row, uint8_t size) |
| 396 | { |
| 397 | int32_t min_col = -1; |
| 398 | int16_t min_col_val = -1; |
| 399 | |
| 400 | for(int32_t i = 0; i < size; ++i) |
| 401 | { |
| 402 | if(conv[i] != 0 && (min_col < 0 || abs(min_col_val) > abs(conv[i]))) |
| 403 | { |
| 404 | min_col = i; |
| 405 | min_col_val = conv[i]; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | if(min_col < 0) |
| 410 | { |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | for(uint32_t j = 0; j < size; ++j) |
| 415 | { |
| 416 | conv_col[j] = conv[min_col + j * size]; |
| 417 | } |
| 418 | |
| 419 | for(uint32_t i = 0; i < size; i++) |
| 420 | { |
| 421 | if(static_cast<int>(i) == min_col) |
| 422 | { |
| 423 | conv_row[i] = 1; |
| 424 | } |
| 425 | else |
| 426 | { |
| 427 | int16_t coeff = conv[i] / conv[min_col]; |
| 428 | |
| 429 | for(uint32_t j = 1; j < size; ++j) |
| 430 | { |
| 431 | if(conv[i + j * size] != (conv_col[j] * coeff)) |
| 432 | { |
| 433 | return false; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | conv_row[i] = coeff; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | return true; |
| 442 | } |
| 443 | |
| 444 | /** Calculate the scale of the given square matrix |
| 445 | * |
| 446 | * The scale is the absolute value of the sum of all the coefficients in the matrix. |
| 447 | * |
| 448 | * @note If the coefficients add up to 0 then the scale is set to 1. |
| 449 | * |
| 450 | * @param[in] matrix Matrix coefficients |
| 451 | * @param[in] matrix_size Number of elements per side of the square matrix. (Number of coefficients = matrix_size * matrix_size). |
| 452 | * |
| 453 | * @return The absolute value of the sum of the coefficients if they don't add up to 0, otherwise 1. |
| 454 | */ |
| 455 | inline uint32_t calculate_matrix_scale(const int16_t *matrix, unsigned int matrix_size) |
| 456 | { |
| 457 | const size_t size = matrix_size * matrix_size; |
| 458 | |
| 459 | return std::max(1, std::abs(std::accumulate(matrix, matrix + size, 0))); |
| 460 | } |
| 461 | |
steniu01 | 7ce53c6 | 2017-09-29 14:55:00 +0100 | [diff] [blame] | 462 | /** Calculate the output shapes of the depth concatenate function. |
| 463 | * |
| 464 | * @param[in] inputs_vector The vector that stores all the pointers to input. |
| 465 | * |
| 466 | * @return the output shape |
| 467 | */ |
| 468 | template <typename T> |
| 469 | TensorShape calculate_depth_concatenate_shape(const std::vector<T *> &inputs_vector) |
| 470 | { |
| 471 | TensorShape out_shape = inputs_vector[0]->info()->tensor_shape(); |
| 472 | |
| 473 | size_t max_x = 0; |
| 474 | size_t max_y = 0; |
| 475 | size_t depth = 0; |
| 476 | |
| 477 | for(const auto &tensor : inputs_vector) |
| 478 | { |
| 479 | ARM_COMPUTE_ERROR_ON(tensor == nullptr); |
| 480 | const TensorShape shape = tensor->info()->tensor_shape(); |
| 481 | max_x = std::max(shape.x(), max_x); |
| 482 | max_y = std::max(shape.y(), max_y); |
| 483 | depth += shape.z(); |
| 484 | } |
| 485 | |
| 486 | out_shape.set(0, max_x); |
| 487 | out_shape.set(1, max_y); |
| 488 | out_shape.set(2, depth); |
| 489 | |
| 490 | return out_shape; |
| 491 | } |
| 492 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 493 | /** Calculate accurary required by the horizontal and vertical convolution computations |
| 494 | * |
| 495 | * @param[in] conv_col Pointer to the vertical vector of the separated convolution filter |
| 496 | * @param[in] conv_row Pointer to the horizontal vector of the convolution filter |
| 497 | * @param[in] size Number of elements per vector of the separated matrix |
| 498 | * |
| 499 | * @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 |
| 500 | * element of the pair is the biggest data type needed for the second stage. |
| 501 | */ |
| 502 | inline std::pair<DataType, DataType> data_type_for_convolution(const int16_t *conv_col, const int16_t *conv_row, size_t size) |
| 503 | { |
| 504 | DataType first_stage = DataType::UNKNOWN; |
| 505 | DataType second_stage = DataType::UNKNOWN; |
| 506 | |
| 507 | auto gez = [](const int16_t &v) |
| 508 | { |
| 509 | return v >= 0; |
| 510 | }; |
| 511 | |
| 512 | auto accu_neg = [](const int &first, const int &second) |
| 513 | { |
| 514 | return first + (second < 0 ? second : 0); |
| 515 | }; |
| 516 | |
| 517 | auto accu_pos = [](const int &first, const int &second) |
| 518 | { |
| 519 | return first + (second > 0 ? second : 0); |
| 520 | }; |
| 521 | |
| 522 | const bool only_positive_coefficients = std::all_of(conv_row, conv_row + size, gez) && std::all_of(conv_col, conv_col + size, gez); |
| 523 | |
| 524 | if(only_positive_coefficients) |
| 525 | { |
| 526 | const int max_row_value = std::accumulate(conv_row, conv_row + size, 0) * UINT8_MAX; |
| 527 | const int max_value = std::accumulate(conv_col, conv_col + size, 0) * max_row_value; |
| 528 | |
| 529 | first_stage = (max_row_value <= UINT16_MAX) ? DataType::U16 : DataType::S32; |
| 530 | |
| 531 | second_stage = (max_value <= UINT16_MAX) ? DataType::U16 : DataType::S32; |
| 532 | } |
| 533 | else |
| 534 | { |
| 535 | const int min_row_value = std::accumulate(conv_row, conv_row + size, 0, accu_neg) * UINT8_MAX; |
| 536 | const int max_row_value = std::accumulate(conv_row, conv_row + size, 0, accu_pos) * UINT8_MAX; |
| 537 | const int neg_coeffs_sum = std::accumulate(conv_col, conv_col + size, 0, accu_neg); |
| 538 | const int pos_coeffs_sum = std::accumulate(conv_col, conv_col + size, 0, accu_pos); |
| 539 | const int min_value = neg_coeffs_sum * max_row_value + pos_coeffs_sum * min_row_value; |
| 540 | const int max_value = neg_coeffs_sum * min_row_value + pos_coeffs_sum * max_row_value; |
| 541 | |
| 542 | first_stage = ((INT16_MIN <= min_row_value) && (max_row_value <= INT16_MAX)) ? DataType::S16 : DataType::S32; |
| 543 | |
| 544 | second_stage = ((INT16_MIN <= min_value) && (max_value <= INT16_MAX)) ? DataType::S16 : DataType::S32; |
| 545 | } |
| 546 | |
| 547 | return std::make_pair(first_stage, second_stage); |
| 548 | } |
| 549 | |
| 550 | /** Calculate the accuracy required by the squared convolution calculation. |
| 551 | * |
| 552 | * |
| 553 | * @param[in] conv Pointer to the squared convolution matrix |
| 554 | * @param[in] size The total size of the convolution matrix |
| 555 | * |
| 556 | * @return The return is the biggest data type needed to do the convolution |
| 557 | */ |
| 558 | inline DataType data_type_for_convolution_matrix(const int16_t *conv, size_t size) |
| 559 | { |
| 560 | auto gez = [](const int16_t v) |
| 561 | { |
| 562 | return v >= 0; |
| 563 | }; |
| 564 | |
| 565 | const bool only_positive_coefficients = std::all_of(conv, conv + size, gez); |
| 566 | |
| 567 | if(only_positive_coefficients) |
| 568 | { |
| 569 | const int max_conv_value = std::accumulate(conv, conv + size, 0) * UINT8_MAX; |
| 570 | if(max_conv_value <= UINT16_MAX) |
| 571 | { |
| 572 | return DataType::U16; |
| 573 | } |
| 574 | else |
| 575 | { |
| 576 | return DataType::S32; |
| 577 | } |
| 578 | } |
| 579 | else |
| 580 | { |
| 581 | const int min_value = std::accumulate(conv, conv + size, 0, [](int a, int b) |
| 582 | { |
| 583 | return b < 0 ? a + b : a; |
| 584 | }) |
| 585 | * UINT8_MAX; |
| 586 | |
| 587 | const int max_value = std::accumulate(conv, conv + size, 0, [](int a, int b) |
| 588 | { |
| 589 | return b > 0 ? a + b : a; |
| 590 | }) |
| 591 | * UINT8_MAX; |
| 592 | |
| 593 | if((INT16_MIN <= min_value) && (INT16_MAX >= max_value)) |
| 594 | { |
| 595 | return DataType::S16; |
| 596 | } |
| 597 | else |
| 598 | { |
| 599 | return DataType::S32; |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | |
Pablo Tello | f5f34bb | 2017-08-22 13:34:13 +0100 | [diff] [blame] | 604 | /** Returns expected shape for the deconvolution output tensor. |
| 605 | * |
| 606 | * @param[in] out_dims widht and height of the output tensor, these values can be obtained with the function deconvolution_output_dimensions. |
| 607 | * @param[in] input Shape of the input tensor. |
| 608 | * @param[in] weights Shape of the weights tensor. |
| 609 | * |
| 610 | * @return Deconvolution output tensor shape. |
| 611 | */ |
| 612 | TensorShape deconvolution_output_shape(const std::pair<unsigned int, unsigned int> &out_dims, TensorShape input, TensorShape weights); |
| 613 | |
| 614 | /** Returns expected width and height of the deconvolution's output tensor. |
| 615 | * |
| 616 | * @param[in] in_width Width of input tensor (Number of columns) |
| 617 | * @param[in] in_height Height of input tensor (Number of rows) |
| 618 | * @param[in] kernel_width Kernel width. |
| 619 | * @param[in] kernel_height Kernel height. |
| 620 | * @param[in] padx X axis padding. |
| 621 | * @param[in] pady Y axis padding. |
| 622 | * @param[in] ax The number of zeros added to right edge of the input. |
| 623 | * @param[in] ay The number of zeros added to top edge of the input. |
| 624 | * @param[in] upscalex How much to scale the X axis. |
| 625 | * @param[in] upscaley How much to scale the Y axis. |
| 626 | * @param[in] round Rounding policy to be used when computing the output's dimensions. |
| 627 | * |
| 628 | * @return A pair with the new width in the first position and the new height in the second. |
| 629 | */ |
| 630 | |
| 631 | const std::pair<unsigned int, unsigned int> deconvolution_output_dimensions(unsigned int in_width, unsigned int in_height, |
| 632 | unsigned int kernel_width, unsigned int kernel_height, |
| 633 | unsigned int padx, unsigned int pady, unsigned int ax, unsigned int ay, |
| 634 | float upscalex, float upscaley, DimensionRoundingType round); |
| 635 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 636 | /** Returns expected width and height of output scaled tensor depending on dimensions rounding mode. |
| 637 | * |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 638 | * @param[in] width Width of input tensor (Number of columns) |
| 639 | * @param[in] height Height of input tensor (Number of rows) |
| 640 | * @param[in] kernel_width Kernel width. |
| 641 | * @param[in] kernel_height Kernel height. |
| 642 | * @param[in] pad_stride_info Pad and stride information. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 643 | * |
| 644 | * @return A pair with the new width in the first position and the new height in the second. |
| 645 | */ |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 646 | const std::pair<unsigned int, unsigned int> scaled_dimensions(unsigned int width, unsigned int height, |
| 647 | unsigned int kernel_width, unsigned int kernel_height, |
| 648 | const PadStrideInfo &pad_stride_info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 649 | |
| 650 | /** Convert a tensor format into a string. |
| 651 | * |
| 652 | * @param[in] format @ref Format to be translated to string. |
| 653 | * |
| 654 | * @return The string describing the format. |
| 655 | */ |
| 656 | const std::string &string_from_format(Format format); |
| 657 | |
| 658 | /** Convert a channel identity into a string. |
| 659 | * |
| 660 | * @param[in] channel @ref Channel to be translated to string. |
| 661 | * |
| 662 | * @return The string describing the channel. |
| 663 | */ |
| 664 | const std::string &string_from_channel(Channel channel); |
| 665 | |
| 666 | /** Convert a data type identity into a string. |
| 667 | * |
| 668 | * @param[in] dt @ref DataType to be translated to string. |
| 669 | * |
| 670 | * @return The string describing the data type. |
| 671 | */ |
| 672 | const std::string &string_from_data_type(DataType dt); |
| 673 | /** Convert a matrix pattern into a string. |
| 674 | * |
| 675 | * @param[in] pattern @ref MatrixPattern to be translated to string. |
| 676 | * |
| 677 | * @return The string describing the matrix pattern. |
| 678 | */ |
| 679 | const std::string &string_from_matrix_pattern(MatrixPattern pattern); |
| 680 | /** Translates a given activation function to a string. |
| 681 | * |
| 682 | * @param[in] act @ref ActivationLayerInfo::ActivationFunction to be translated to string. |
| 683 | * |
| 684 | * @return The string describing the activation function. |
| 685 | */ |
| 686 | const std::string &string_from_activation_func(ActivationLayerInfo::ActivationFunction act); |
| 687 | /** Translates a given non linear function to a string. |
| 688 | * |
| 689 | * @param[in] function @ref NonLinearFilterFunction to be translated to string. |
| 690 | * |
| 691 | * @return The string describing the non linear function. |
| 692 | */ |
| 693 | const std::string &string_from_non_linear_filter_function(NonLinearFilterFunction function); |
| 694 | /** Translates a given interpolation policy to a string. |
| 695 | * |
| 696 | * @param[in] policy @ref InterpolationPolicy to be translated to string. |
| 697 | * |
| 698 | * @return The string describing the interpolation policy. |
| 699 | */ |
| 700 | const std::string &string_from_interpolation_policy(InterpolationPolicy policy); |
| 701 | /** Translates a given border mode policy to a string. |
| 702 | * |
| 703 | * @param[in] border_mode @ref BorderMode to be translated to string. |
| 704 | * |
| 705 | * @return The string describing the border mode. |
| 706 | */ |
| 707 | const std::string &string_from_border_mode(BorderMode border_mode); |
| 708 | /** Translates a given normalization type to a string. |
| 709 | * |
| 710 | * @param[in] type @ref NormType to be translated to string. |
| 711 | * |
| 712 | * @return The string describing the normalization type. |
| 713 | */ |
| 714 | const std::string &string_from_norm_type(NormType type); |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 715 | /** Translates a given pooling type to a string. |
| 716 | * |
| 717 | * @param[in] type @ref PoolingType to be translated to string. |
| 718 | * |
| 719 | * @return The string describing the pooling type. |
| 720 | */ |
| 721 | const std::string &string_from_pooling_type(PoolingType type); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 722 | /** Lower a given string. |
| 723 | * |
| 724 | * @param[in] val Given string to lower. |
| 725 | * |
| 726 | * @return The lowered string |
| 727 | */ |
| 728 | std::string lower_string(const std::string &val); |
| 729 | |
| 730 | /** Check if a given data type is of floating point type |
| 731 | * |
| 732 | * @param[in] dt Input data type. |
| 733 | * |
| 734 | * @return True if data type is of floating point type, else false. |
| 735 | */ |
| 736 | inline bool is_data_type_float(DataType dt) |
| 737 | { |
| 738 | switch(dt) |
| 739 | { |
| 740 | case DataType::F16: |
| 741 | case DataType::F32: |
| 742 | return true; |
| 743 | default: |
| 744 | return false; |
| 745 | } |
| 746 | } |
| 747 | |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 748 | /** Check if a given data type is of quantized type |
| 749 | * |
| 750 | * @note Quantized is considered a super-set of fixed-point and asymmetric data types. |
| 751 | * |
| 752 | * @param[in] dt Input data type. |
| 753 | * |
| 754 | * @return True if data type is of quantized type, else false. |
| 755 | */ |
| 756 | inline bool is_data_type_quantized(DataType dt) |
| 757 | { |
| 758 | switch(dt) |
| 759 | { |
| 760 | case DataType::QS8: |
| 761 | case DataType::QASYMM8: |
| 762 | case DataType::QS16: |
| 763 | case DataType::QS32: |
| 764 | return true; |
| 765 | default: |
| 766 | return false; |
| 767 | } |
| 768 | } |
| 769 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 770 | /** Check if a given data type is of fixed point type |
| 771 | * |
| 772 | * @param[in] dt Input data type. |
| 773 | * |
| 774 | * @return True if data type is of fixed point type, else false. |
| 775 | */ |
| 776 | inline bool is_data_type_fixed_point(DataType dt) |
| 777 | { |
| 778 | switch(dt) |
| 779 | { |
| 780 | case DataType::QS8: |
| 781 | case DataType::QS16: |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 782 | case DataType::QS32: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 783 | return true; |
| 784 | default: |
| 785 | return false; |
| 786 | } |
| 787 | } |
| 788 | |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 789 | /** Check if a given data type is of asymmetric quantized type |
| 790 | * |
| 791 | * @param[in] dt Input data type. |
| 792 | * |
| 793 | * @return True if data type is of symmetric quantized type, else false. |
| 794 | */ |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 795 | inline bool is_data_type_quantized_asymmetric(DataType dt) |
Georgios Pinitas | 05078ec | 2017-11-02 13:06:59 +0000 | [diff] [blame] | 796 | { |
| 797 | switch(dt) |
| 798 | { |
| 799 | case DataType::QASYMM8: |
| 800 | return true; |
| 801 | default: |
| 802 | return false; |
| 803 | } |
| 804 | } |
| 805 | |
Georgios Pinitas | 8901096 | 2017-08-04 14:58:27 +0100 | [diff] [blame] | 806 | /** Create a string with the float in full precision. |
| 807 | * |
| 808 | * @param val Floating point value |
| 809 | * |
| 810 | * @return String with the floating point value. |
| 811 | */ |
| 812 | inline std::string float_to_string_with_full_precision(float val) |
| 813 | { |
| 814 | std::stringstream ss; |
| 815 | ss.precision(std::numeric_limits<float>::digits10 + 1); |
| 816 | ss << val; |
| 817 | return ss.str(); |
| 818 | } |
| 819 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 820 | /** Print consecutive elements to an output stream. |
| 821 | * |
| 822 | * @param[out] s Output stream to print the elements to. |
| 823 | * @param[in] ptr Pointer to print the elements from. |
| 824 | * @param[in] n Number of elements to print. |
| 825 | * @param[in] stream_width (Optional) Width of the stream. If set to 0 the element's width is used. Defaults to 0. |
| 826 | * @param[in] element_delim (Optional) Delimeter among the consecutive elements. Defaults to space delimeter |
| 827 | */ |
| 828 | template <typename T> |
| 829 | void print_consecutive_elements_impl(std::ostream &s, const T *ptr, unsigned int n, int stream_width = 0, const std::string &element_delim = " ") |
| 830 | { |
| 831 | using print_type = typename std::conditional<std::is_floating_point<T>::value, T, int>::type; |
| 832 | |
| 833 | for(unsigned int i = 0; i < n; ++i) |
| 834 | { |
| 835 | // Set stream width as it is not a "sticky" stream manipulator |
| 836 | if(stream_width != 0) |
| 837 | { |
| 838 | s.width(stream_width); |
| 839 | } |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 840 | |
| 841 | if(std::is_same<typename std::decay<T>::type, half>::value) |
| 842 | { |
| 843 | // We use T instead of print_type here is because the std::is_floating_point<half> returns false and then the print_type becomes int. |
| 844 | s << std::right << static_cast<T>(ptr[i]) << element_delim; |
| 845 | } |
| 846 | else |
| 847 | { |
| 848 | s << std::right << static_cast<print_type>(ptr[i]) << element_delim; |
| 849 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | |
| 853 | /** Identify the maximum width of n consecutive elements. |
| 854 | * |
| 855 | * @param[in] s The output stream which will be used to print the elements. Used to extract the stream format. |
| 856 | * @param[in] ptr Pointer to the elements. |
| 857 | * @param[in] n Number of elements. |
| 858 | * |
| 859 | * @return The maximum width of the elements. |
| 860 | */ |
| 861 | template <typename T> |
| 862 | int max_consecutive_elements_display_width_impl(std::ostream &s, const T *ptr, unsigned int n) |
| 863 | { |
| 864 | using print_type = typename std::conditional<std::is_floating_point<T>::value, T, int>::type; |
| 865 | |
| 866 | int max_width = -1; |
| 867 | for(unsigned int i = 0; i < n; ++i) |
| 868 | { |
| 869 | std::stringstream ss; |
| 870 | ss.copyfmt(s); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 871 | |
| 872 | if(std::is_same<typename std::decay<T>::type, half>::value) |
| 873 | { |
| 874 | // We use T instead of print_type here is because the std::is_floating_point<half> returns false and then the print_type becomes int. |
| 875 | ss << static_cast<T>(ptr[i]); |
| 876 | } |
| 877 | else |
| 878 | { |
| 879 | ss << static_cast<print_type>(ptr[i]); |
| 880 | } |
| 881 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 882 | max_width = std::max<int>(max_width, ss.str().size()); |
| 883 | } |
| 884 | return max_width; |
| 885 | } |
| 886 | |
| 887 | /** Print consecutive elements to an output stream. |
| 888 | * |
| 889 | * @param[out] s Output stream to print the elements to. |
| 890 | * @param[in] dt Data type of the elements |
| 891 | * @param[in] ptr Pointer to print the elements from. |
| 892 | * @param[in] n Number of elements to print. |
| 893 | * @param[in] stream_width (Optional) Width of the stream. If set to 0 the element's width is used. Defaults to 0. |
| 894 | * @param[in] element_delim (Optional) Delimeter among the consecutive elements. Defaults to space delimeter |
| 895 | */ |
| 896 | void print_consecutive_elements(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n, int stream_width, const std::string &element_delim = " "); |
| 897 | |
| 898 | /** Identify the maximum width of n consecutive elements. |
| 899 | * |
| 900 | * @param[in] s Output stream to print the elements to. |
| 901 | * @param[in] dt Data type of the elements |
| 902 | * @param[in] ptr Pointer to print the elements from. |
| 903 | * @param[in] n Number of elements to print. |
| 904 | * |
| 905 | * @return The maximum width of the elements. |
| 906 | */ |
| 907 | int max_consecutive_elements_display_width(std::ostream &s, DataType dt, const uint8_t *ptr, unsigned int n); |
| 908 | } |
| 909 | #endif /*__ARM_COMPUTE_UTILS_H__ */ |