Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 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_TEST_UTILS_H__ |
| 25 | #define __ARM_COMPUTE_TEST_UTILS_H__ |
| 26 | |
| 27 | #include "arm_compute/core/Coordinates.h" |
| 28 | #include "arm_compute/core/Error.h" |
| 29 | #include "arm_compute/core/FixedPoint.h" |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/TensorInfo.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/TensorShape.h" |
| 32 | #include "arm_compute/core/Types.h" |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 33 | #include "support/ToolchainSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
| 35 | #include <cmath> |
| 36 | #include <cstddef> |
| 37 | #include <limits> |
| 38 | #include <memory> |
| 39 | #include <sstream> |
| 40 | #include <string> |
| 41 | #include <type_traits> |
| 42 | |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame^] | 43 | #ifdef ARM_COMPUTE_ENABLE_FP16 |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 44 | #include <arm_fp16.h> // needed for float16_t |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame^] | 45 | #endif /* ARM_COMPUTE_ENABLE_FP16 */ |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 46 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 47 | namespace arm_compute |
| 48 | { |
| 49 | namespace test |
| 50 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 51 | /** Round floating-point value with half value rounding to positive infinity. |
| 52 | * |
| 53 | * @param[in] value floating-point value to be rounded. |
| 54 | * |
| 55 | * @return Floating-point value of rounded @p value. |
| 56 | */ |
| 57 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 58 | inline T round_half_up(T value) |
| 59 | { |
| 60 | return std::floor(value + 0.5f); |
| 61 | } |
| 62 | |
| 63 | /** Round floating-point value with half value rounding to nearest even. |
| 64 | * |
| 65 | * @param[in] value floating-point value to be rounded. |
| 66 | * @param[in] epsilon precision. |
| 67 | * |
| 68 | * @return Floating-point value of rounded @p value. |
| 69 | */ |
| 70 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 71 | inline T round_half_even(T value, T epsilon = std::numeric_limits<T>::epsilon()) |
| 72 | { |
| 73 | T positive_value = std::abs(value); |
| 74 | T ipart = 0; |
| 75 | std::modf(positive_value, &ipart); |
| 76 | // If 'value' is exactly halfway between two integers |
| 77 | if(std::abs(positive_value - (ipart + 0.5f)) < epsilon) |
| 78 | { |
| 79 | // If 'ipart' is even then return 'ipart' |
| 80 | if(std::fmod(ipart, 2.f) < epsilon) |
| 81 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 82 | return support::cpp11::copysign(ipart, value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 83 | } |
| 84 | // Else return the nearest even integer |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 85 | return support::cpp11::copysign(std::ceil(ipart + 0.5f), value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 86 | } |
| 87 | // Otherwise use the usual round to closest |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 88 | return support::cpp11::copysign(support::cpp11::round(positive_value), value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 89 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | |
| 91 | namespace traits |
| 92 | { |
| 93 | // *INDENT-OFF* |
| 94 | // clang-format off |
| 95 | template <typename T> struct promote { }; |
| 96 | template <> struct promote<uint8_t> { using type = uint16_t; }; |
| 97 | template <> struct promote<int8_t> { using type = int16_t; }; |
| 98 | template <> struct promote<uint16_t> { using type = uint32_t; }; |
| 99 | template <> struct promote<int16_t> { using type = int32_t; }; |
| 100 | template <> struct promote<uint32_t> { using type = uint64_t; }; |
| 101 | template <> struct promote<int32_t> { using type = int64_t; }; |
| 102 | template <> struct promote<float> { using type = float; }; |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 103 | #ifdef ARM_COMPUTE_ENABLE_FP16 |
| 104 | template <> struct promote<float16_t> { using type = float16_t; }; |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame^] | 105 | #endif /* ARM_COMPUTE_ENABLE_FP16 */ |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 106 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 107 | |
| 108 | template <typename T> |
| 109 | using promote_t = typename promote<T>::type; |
| 110 | |
| 111 | template <typename T> |
| 112 | using make_signed_conditional_t = typename std::conditional<std::is_integral<T>::value, std::make_signed<T>, std::common_type<T>>::type; |
| 113 | // clang-format on |
| 114 | // *INDENT-ON* |
| 115 | } |
| 116 | |
| 117 | /** Look up the format corresponding to a channel. |
| 118 | * |
| 119 | * @param[in] channel Channel type. |
| 120 | * |
| 121 | * @return Format that contains the given channel. |
| 122 | */ |
| 123 | inline Format get_format_for_channel(Channel channel) |
| 124 | { |
| 125 | switch(channel) |
| 126 | { |
| 127 | case Channel::R: |
| 128 | case Channel::G: |
| 129 | case Channel::B: |
| 130 | return Format::RGB888; |
| 131 | default: |
| 132 | throw std::runtime_error("Unsupported channel"); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** Return the format of a channel. |
| 137 | * |
| 138 | * @param[in] channel Channel type. |
| 139 | * |
| 140 | * @return Format of the given channel. |
| 141 | */ |
| 142 | inline Format get_channel_format(Channel channel) |
| 143 | { |
| 144 | switch(channel) |
| 145 | { |
| 146 | case Channel::R: |
| 147 | case Channel::G: |
| 148 | case Channel::B: |
| 149 | return Format::U8; |
| 150 | default: |
| 151 | throw std::runtime_error("Unsupported channel"); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** Base case of foldl. |
| 156 | * |
| 157 | * @return value. |
| 158 | */ |
| 159 | template <typename F, typename T> |
| 160 | inline T foldl(F &&, const T &value) |
| 161 | { |
| 162 | return value; |
| 163 | } |
| 164 | |
| 165 | /** Base case of foldl. |
| 166 | * |
| 167 | * @return func(value1, value2). |
| 168 | */ |
| 169 | template <typename F, typename T, typename U> |
| 170 | inline auto foldl(F &&func, T &&value1, U &&value2) -> decltype(func(value1, value2)) |
| 171 | { |
| 172 | return func(value1, value2); |
| 173 | } |
| 174 | |
| 175 | /** Fold left. |
| 176 | * |
| 177 | * @param[in] func Binary function to be called. |
| 178 | * @param[in] initial Initial value. |
| 179 | * @param[in] value Argument passed to the function. |
| 180 | * @param[in] values Remaining arguments. |
| 181 | */ |
| 182 | template <typename F, typename I, typename T, typename... Vs> |
| 183 | inline I foldl(F &&func, I &&initial, T &&value, Vs &&... values) |
| 184 | { |
| 185 | return foldl(std::forward<F>(func), func(std::forward<I>(initial), std::forward<T>(value)), std::forward<Vs>(values)...); |
| 186 | } |
| 187 | |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 188 | /** Create a valid region based on tensor shape, border mode and border size |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 189 | * |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 190 | * @param[in] shape Shape used as size of the valid region. |
| 191 | * @param[in] border_undefined (Optional) Boolean indicating if the border mode is undefined. |
| 192 | * @param[in] border_size (Optional) Border size used to specify the region to exclude. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 193 | * |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 194 | * @return A valid region starting at (0, 0, ...) with size of @p shape if @p border_undefined is false; otherwise |
| 195 | * return A valid region starting at (@p border_size.left, @p border_size.top, ...) with reduced size of @p shape. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 196 | */ |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 197 | inline ValidRegion shape_to_valid_region(TensorShape shape, bool border_undefined = false, BorderSize border_size = BorderSize(0)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 198 | { |
| 199 | Coordinates anchor; |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 200 | anchor.set(std::max(0, static_cast<int>(shape.num_dimensions()) - 1), 0); |
| 201 | if(border_undefined) |
| 202 | { |
| 203 | ARM_COMPUTE_ERROR_ON(shape.num_dimensions() < 2); |
| 204 | anchor.set(0, border_size.left); |
| 205 | anchor.set(1, border_size.top); |
| 206 | shape.set(0, shape.x() - border_size.left - border_size.right); |
| 207 | shape.set(1, shape.y() - border_size.top - border_size.bottom); |
| 208 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 209 | return ValidRegion(std::move(anchor), std::move(shape)); |
| 210 | } |
| 211 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 212 | /** Write the value after casting the pointer according to @p data_type. |
| 213 | * |
| 214 | * @warning The type of the value must match the specified data type. |
| 215 | * |
| 216 | * @param[out] ptr Pointer to memory where the @p value will be written. |
| 217 | * @param[in] value Value that will be written. |
| 218 | * @param[in] data_type Data type that will be written. |
| 219 | */ |
| 220 | template <typename T> |
| 221 | void store_value_with_data_type(void *ptr, T value, DataType data_type) |
| 222 | { |
| 223 | switch(data_type) |
| 224 | { |
| 225 | case DataType::U8: |
| 226 | *reinterpret_cast<uint8_t *>(ptr) = value; |
| 227 | break; |
| 228 | case DataType::S8: |
| 229 | case DataType::QS8: |
| 230 | *reinterpret_cast<int8_t *>(ptr) = value; |
| 231 | break; |
| 232 | case DataType::U16: |
| 233 | *reinterpret_cast<uint16_t *>(ptr) = value; |
| 234 | break; |
| 235 | case DataType::S16: |
Michalis Spyrou | 0a8334c | 2017-06-14 18:00:05 +0100 | [diff] [blame] | 236 | case DataType::QS16: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 237 | *reinterpret_cast<int16_t *>(ptr) = value; |
| 238 | break; |
| 239 | case DataType::U32: |
| 240 | *reinterpret_cast<uint32_t *>(ptr) = value; |
| 241 | break; |
| 242 | case DataType::S32: |
| 243 | *reinterpret_cast<int32_t *>(ptr) = value; |
| 244 | break; |
| 245 | case DataType::U64: |
| 246 | *reinterpret_cast<uint64_t *>(ptr) = value; |
| 247 | break; |
| 248 | case DataType::S64: |
| 249 | *reinterpret_cast<int64_t *>(ptr) = value; |
| 250 | break; |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 251 | #if ARM_COMPUTE_ENABLE_FP16 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 252 | case DataType::F16: |
| 253 | *reinterpret_cast<float16_t *>(ptr) = value; |
| 254 | break; |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 255 | #endif /* ARM_COMPUTE_ENABLE_FP16 */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 256 | case DataType::F32: |
| 257 | *reinterpret_cast<float *>(ptr) = value; |
| 258 | break; |
| 259 | case DataType::F64: |
| 260 | *reinterpret_cast<double *>(ptr) = value; |
| 261 | break; |
| 262 | case DataType::SIZET: |
| 263 | *reinterpret_cast<size_t *>(ptr) = value; |
| 264 | break; |
| 265 | default: |
| 266 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /** Saturate a value of type T against the numeric limits of type U. |
| 271 | * |
| 272 | * @param[in] val Value to be saturated. |
| 273 | * |
| 274 | * @return saturated value. |
| 275 | */ |
| 276 | template <typename U, typename T> |
| 277 | T saturate_cast(T val) |
| 278 | { |
| 279 | if(val > static_cast<T>(std::numeric_limits<U>::max())) |
| 280 | { |
| 281 | val = static_cast<T>(std::numeric_limits<U>::max()); |
| 282 | } |
| 283 | if(val < static_cast<T>(std::numeric_limits<U>::lowest())) |
| 284 | { |
| 285 | val = static_cast<T>(std::numeric_limits<U>::lowest()); |
| 286 | } |
| 287 | return val; |
| 288 | } |
| 289 | |
| 290 | /** Find the signed promoted common type. |
| 291 | */ |
| 292 | template <typename... T> |
| 293 | struct common_promoted_signed_type |
| 294 | { |
| 295 | using common_type = typename std::common_type<T...>::type; |
| 296 | using promoted_type = traits::promote_t<common_type>; |
| 297 | using intermediate_type = typename traits::make_signed_conditional_t<promoted_type>::type; |
| 298 | }; |
| 299 | |
| 300 | /** Convert a linear index into n-dimensional coordinates. |
| 301 | * |
| 302 | * @param[in] shape Shape of the n-dimensional tensor. |
| 303 | * @param[in] index Linear index specifying the i-th element. |
| 304 | * |
| 305 | * @return n-dimensional coordinates. |
| 306 | */ |
| 307 | inline Coordinates index2coord(const TensorShape &shape, int index) |
| 308 | { |
| 309 | int num_elements = shape.total_size(); |
| 310 | |
| 311 | ARM_COMPUTE_ERROR_ON_MSG(index < 0 || index >= num_elements, "Index has to be in [0, num_elements]"); |
| 312 | ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create coordinate from empty shape"); |
| 313 | |
| 314 | Coordinates coord{ 0 }; |
| 315 | |
| 316 | for(int d = shape.num_dimensions() - 1; d >= 0; --d) |
| 317 | { |
| 318 | num_elements /= shape[d]; |
| 319 | coord.set(d, index / num_elements); |
| 320 | index %= num_elements; |
| 321 | } |
| 322 | |
| 323 | return coord; |
| 324 | } |
| 325 | |
| 326 | /** Linearise the given coordinate. |
| 327 | * |
| 328 | * Transforms the given coordinate into a linear offset in terms of |
| 329 | * elements. |
| 330 | * |
| 331 | * @param[in] shape Shape of the n-dimensional tensor. |
| 332 | * @param[in] coord The to be converted coordinate. |
| 333 | * |
| 334 | * @return Linear offset to the element. |
| 335 | */ |
| 336 | inline int coord2index(const TensorShape &shape, const Coordinates &coord) |
| 337 | { |
| 338 | ARM_COMPUTE_ERROR_ON_MSG(shape.total_size() == 0, "Cannot get index from empty shape"); |
| 339 | ARM_COMPUTE_ERROR_ON_MSG(coord.num_dimensions() == 0, "Cannot get index of empty coordinate"); |
| 340 | |
| 341 | int index = 0; |
| 342 | int dim_size = 1; |
| 343 | |
| 344 | for(unsigned int i = 0; i < coord.num_dimensions(); ++i) |
| 345 | { |
| 346 | index += coord[i] * dim_size; |
| 347 | dim_size *= shape[i]; |
| 348 | } |
| 349 | |
| 350 | return index; |
| 351 | } |
| 352 | |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 353 | /** Check if Coordinates dimensionality can match the respective shape one. |
| 354 | * |
| 355 | * @param coords Coordinates |
| 356 | * @param shape Shape to match dimensionality |
| 357 | * |
| 358 | * @return True if Coordinates can match the dimensionality of the shape else false. |
| 359 | */ |
| 360 | inline bool match_shape(Coordinates &coords, const TensorShape &shape) |
| 361 | { |
| 362 | auto check_nz = [](unsigned int i) |
| 363 | { |
| 364 | return i != 0; |
| 365 | }; |
| 366 | |
| 367 | unsigned int coords_dims = coords.num_dimensions(); |
| 368 | unsigned int shape_dims = shape.num_dimensions(); |
| 369 | |
| 370 | // Increase coordinates scenario |
| 371 | if(coords_dims < shape_dims) |
| 372 | { |
| 373 | coords.set_num_dimensions(shape_dims); |
| 374 | return true; |
| 375 | } |
| 376 | // Decrease coordinates scenario |
| 377 | if(coords_dims > shape_dims && !std::any_of(coords.begin() + shape_dims, coords.end(), check_nz)) |
| 378 | { |
| 379 | coords.set_num_dimensions(shape_dims); |
| 380 | return true; |
| 381 | } |
| 382 | |
| 383 | return (coords_dims == shape_dims); |
| 384 | } |
| 385 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 386 | /** Check if a coordinate is within a valid region */ |
| 387 | inline bool is_in_valid_region(const ValidRegion &valid_region, const Coordinates &coord) |
| 388 | { |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 389 | Coordinates coords(coord); |
| 390 | ARM_COMPUTE_ERROR_ON_MSG(!match_shape(coords, valid_region.shape), "Shapes of valid region and coordinates do not agree"); |
| 391 | for(int d = 0; static_cast<size_t>(d) < coords.num_dimensions(); ++d) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 392 | { |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 393 | if(coords[d] < valid_region.start(d) || coords[d] >= valid_region.end(d)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 394 | { |
| 395 | return false; |
| 396 | } |
| 397 | } |
| 398 | return true; |
| 399 | } |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 400 | |
| 401 | /** Create and initialize a tensor of the given type. |
| 402 | * |
| 403 | * @param[in] shape Tensor shape. |
| 404 | * @param[in] data_type Data type. |
| 405 | * @param[in] num_channels (Optional) Number of channels. |
| 406 | * @param[in] fixed_point_position (Optional) Number of fractional bits. |
| 407 | * |
| 408 | * @return Initialized tensor of given type. |
| 409 | */ |
| 410 | template <typename T> |
| 411 | inline T create_tensor(const TensorShape &shape, DataType data_type, int num_channels = 1, int fixed_point_position = 0) |
| 412 | { |
| 413 | T tensor; |
| 414 | tensor.allocator()->init(TensorInfo(shape, num_channels, data_type, fixed_point_position)); |
| 415 | |
| 416 | return tensor; |
| 417 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 418 | } // namespace test |
| 419 | } // namespace arm_compute |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame^] | 420 | #endif /* __ARM_COMPUTE_TEST_UTILS_H__ */ |