Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
John Richardson | 25f2368 | 2017-11-27 14:35:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017, 2018 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 | #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" |
John Richardson | 25f2368 | 2017-11-27 14:35:09 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/HOGInfo.h" |
| 31 | #include "arm_compute/core/Size2D.h" |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/TensorInfo.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/TensorShape.h" |
| 34 | #include "arm_compute/core/Types.h" |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 35 | #include "support/ToolchainSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | |
Joel Liang | 1c5ffd6 | 2017-12-28 10:09:51 +0800 | [diff] [blame] | 37 | #ifdef ARM_COMPUTE_CL |
| 38 | #include "arm_compute/core/CL/OpenCL.h" |
| 39 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 40 | #endif /* ARM_COMPUTE_CL */ |
| 41 | |
| 42 | #ifdef ARM_COMPUTE_GC |
| 43 | #include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" |
| 44 | #include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h" |
| 45 | #endif /* ARM_COMPUTE_GC */ |
| 46 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 47 | #include <cmath> |
| 48 | #include <cstddef> |
| 49 | #include <limits> |
| 50 | #include <memory> |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 51 | #include <random> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 52 | #include <sstream> |
| 53 | #include <string> |
| 54 | #include <type_traits> |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 55 | #include <vector> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 56 | |
| 57 | namespace arm_compute |
| 58 | { |
Joel Liang | 1c5ffd6 | 2017-12-28 10:09:51 +0800 | [diff] [blame] | 59 | #ifdef ARM_COMPUTE_CL |
| 60 | class CLTensor; |
| 61 | #endif /* ARM_COMPUTE_CL */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 62 | namespace test |
| 63 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | /** Round floating-point value with half value rounding to positive infinity. |
| 65 | * |
| 66 | * @param[in] value floating-point value to be rounded. |
| 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_up(T value) |
| 72 | { |
| 73 | return std::floor(value + 0.5f); |
| 74 | } |
| 75 | |
| 76 | /** Round floating-point value with half value rounding to nearest even. |
| 77 | * |
| 78 | * @param[in] value floating-point value to be rounded. |
| 79 | * @param[in] epsilon precision. |
| 80 | * |
| 81 | * @return Floating-point value of rounded @p value. |
| 82 | */ |
| 83 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 84 | inline T round_half_even(T value, T epsilon = std::numeric_limits<T>::epsilon()) |
| 85 | { |
| 86 | T positive_value = std::abs(value); |
| 87 | T ipart = 0; |
| 88 | std::modf(positive_value, &ipart); |
| 89 | // If 'value' is exactly halfway between two integers |
| 90 | if(std::abs(positive_value - (ipart + 0.5f)) < epsilon) |
| 91 | { |
| 92 | // If 'ipart' is even then return 'ipart' |
| 93 | if(std::fmod(ipart, 2.f) < epsilon) |
| 94 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 95 | return support::cpp11::copysign(ipart, value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | } |
| 97 | // Else return the nearest even integer |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 98 | return support::cpp11::copysign(std::ceil(ipart + 0.5f), value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | } |
| 100 | // Otherwise use the usual round to closest |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 101 | return support::cpp11::copysign(support::cpp11::round(positive_value), value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 102 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | |
| 104 | namespace traits |
| 105 | { |
| 106 | // *INDENT-OFF* |
| 107 | // clang-format off |
| 108 | template <typename T> struct promote { }; |
| 109 | template <> struct promote<uint8_t> { using type = uint16_t; }; |
| 110 | template <> struct promote<int8_t> { using type = int16_t; }; |
| 111 | template <> struct promote<uint16_t> { using type = uint32_t; }; |
| 112 | template <> struct promote<int16_t> { using type = int32_t; }; |
| 113 | template <> struct promote<uint32_t> { using type = uint64_t; }; |
| 114 | template <> struct promote<int32_t> { using type = int64_t; }; |
| 115 | template <> struct promote<float> { using type = float; }; |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 116 | template <> struct promote<half> { using type = half; }; |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 117 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | |
| 119 | template <typename T> |
| 120 | using promote_t = typename promote<T>::type; |
| 121 | |
| 122 | template <typename T> |
| 123 | using make_signed_conditional_t = typename std::conditional<std::is_integral<T>::value, std::make_signed<T>, std::common_type<T>>::type; |
John Richardson | 3c5f949 | 2017-10-04 15:27:37 +0100 | [diff] [blame] | 124 | |
| 125 | template <typename T> |
| 126 | using make_unsigned_conditional_t = typename std::conditional<std::is_integral<T>::value, std::make_unsigned<T>, std::common_type<T>>::type; |
| 127 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 128 | // clang-format on |
| 129 | // *INDENT-ON* |
| 130 | } |
| 131 | |
| 132 | /** Look up the format corresponding to a channel. |
| 133 | * |
| 134 | * @param[in] channel Channel type. |
| 135 | * |
| 136 | * @return Format that contains the given channel. |
| 137 | */ |
| 138 | inline Format get_format_for_channel(Channel channel) |
| 139 | { |
| 140 | switch(channel) |
| 141 | { |
| 142 | case Channel::R: |
| 143 | case Channel::G: |
| 144 | case Channel::B: |
| 145 | return Format::RGB888; |
| 146 | default: |
| 147 | throw std::runtime_error("Unsupported channel"); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** Return the format of a channel. |
| 152 | * |
| 153 | * @param[in] channel Channel type. |
| 154 | * |
| 155 | * @return Format of the given channel. |
| 156 | */ |
| 157 | inline Format get_channel_format(Channel channel) |
| 158 | { |
| 159 | switch(channel) |
| 160 | { |
| 161 | case Channel::R: |
| 162 | case Channel::G: |
| 163 | case Channel::B: |
| 164 | return Format::U8; |
| 165 | default: |
| 166 | throw std::runtime_error("Unsupported channel"); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** Base case of foldl. |
| 171 | * |
| 172 | * @return value. |
| 173 | */ |
| 174 | template <typename F, typename T> |
| 175 | inline T foldl(F &&, const T &value) |
| 176 | { |
| 177 | return value; |
| 178 | } |
| 179 | |
| 180 | /** Base case of foldl. |
| 181 | * |
| 182 | * @return func(value1, value2). |
| 183 | */ |
| 184 | template <typename F, typename T, typename U> |
| 185 | inline auto foldl(F &&func, T &&value1, U &&value2) -> decltype(func(value1, value2)) |
| 186 | { |
| 187 | return func(value1, value2); |
| 188 | } |
| 189 | |
| 190 | /** Fold left. |
| 191 | * |
| 192 | * @param[in] func Binary function to be called. |
| 193 | * @param[in] initial Initial value. |
| 194 | * @param[in] value Argument passed to the function. |
| 195 | * @param[in] values Remaining arguments. |
| 196 | */ |
| 197 | template <typename F, typename I, typename T, typename... Vs> |
| 198 | inline I foldl(F &&func, I &&initial, T &&value, Vs &&... values) |
| 199 | { |
| 200 | return foldl(std::forward<F>(func), func(std::forward<I>(initial), std::forward<T>(value)), std::forward<Vs>(values)...); |
| 201 | } |
| 202 | |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 203 | /** 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] | 204 | * |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 205 | * @param[in] shape Shape used as size of the valid region. |
| 206 | * @param[in] border_undefined (Optional) Boolean indicating if the border mode is undefined. |
| 207 | * @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] | 208 | * |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 209 | * @return A valid region starting at (0, 0, ...) with size of @p shape if @p border_undefined is false; otherwise |
| 210 | * 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] | 211 | */ |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 212 | 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] | 213 | { |
| 214 | Coordinates anchor; |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 215 | anchor.set_num_dimensions(shape.num_dimensions()); |
| 216 | |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 217 | if(border_undefined) |
| 218 | { |
| 219 | ARM_COMPUTE_ERROR_ON(shape.num_dimensions() < 2); |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 220 | |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 221 | anchor.set(0, border_size.left); |
| 222 | anchor.set(1, border_size.top); |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 223 | |
| 224 | const int valid_shape_x = std::max(0, static_cast<int>(shape.x()) - static_cast<int>(border_size.left) - static_cast<int>(border_size.right)); |
| 225 | const int valid_shape_y = std::max(0, static_cast<int>(shape.y()) - static_cast<int>(border_size.top) - static_cast<int>(border_size.bottom)); |
| 226 | |
| 227 | shape.set(0, valid_shape_x); |
| 228 | shape.set(1, valid_shape_y); |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 229 | } |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 230 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 231 | return ValidRegion(std::move(anchor), std::move(shape)); |
| 232 | } |
| 233 | |
Gian Marco | 37908d9 | 2017-11-07 14:38:22 +0000 | [diff] [blame] | 234 | /** Create a valid region for Gaussian Pyramid Half based on tensor shape and valid region at level "i - 1" and border mode |
| 235 | * |
| 236 | * @note The border size is 2 in case of Gaussian Pyramid Half |
| 237 | * |
| 238 | * @param[in] shape Shape used at level "i - 1" of Gaussian Pyramid Half |
| 239 | * @param[in] valid_region Valid region used at level "i - 1" of Gaussian Pyramid Half |
| 240 | * @param[in] border_undefined (Optional) Boolean indicating if the border mode is undefined. |
| 241 | * |
| 242 | * return The valid region for the level "i" of Gaussian Pyramid Half |
| 243 | */ |
| 244 | inline ValidRegion shape_to_valid_region_gaussian_pyramid_half(TensorShape shape, ValidRegion valid_region, bool border_undefined = false) |
| 245 | { |
| 246 | constexpr int border_size = 2; |
| 247 | Coordinates anchor; |
| 248 | anchor.set_num_dimensions(shape.num_dimensions()); |
| 249 | |
| 250 | // Compute tensor shape for level "i" of Gaussian Pyramid Half |
| 251 | // dst_width = (src_width + 1) * 0.5f |
| 252 | // dst_height = (src_height + 1) * 0.5f |
| 253 | TensorShape dst_shape = shape; |
| 254 | dst_shape.set(0, (shape[0] + 1) * 0.5f); |
| 255 | dst_shape.set(1, (shape[1] + 1) * 0.5f); |
| 256 | |
| 257 | if(border_undefined) |
| 258 | { |
| 259 | ARM_COMPUTE_ERROR_ON(shape.num_dimensions() < 2); |
| 260 | |
| 261 | // Compute the left and top invalid borders |
| 262 | float invalid_border_left = static_cast<float>(valid_region.anchor.x() + border_size) / 2.0f; |
| 263 | float invalid_border_top = static_cast<float>(valid_region.anchor.y() + border_size) / 2.0f; |
| 264 | |
| 265 | // For the new anchor point we can have 2 cases: |
| 266 | // 1) If the width/height of the tensor shape is odd, we have to take the ceil value of (valid_region.anchor.x() + border_size) / 2.0f or (valid_region.anchor.y() + border_size / 2.0f |
| 267 | // 2) If the width/height of the tensor shape is even, we have to take the floor value of (valid_region.anchor.x() + border_size) / 2.0f or (valid_region.anchor.y() + border_size) / 2.0f |
| 268 | // In this manner we should be able to propagate correctly the valid region along all levels of the pyramid |
| 269 | invalid_border_left = (shape[0] % 2) ? std::ceil(invalid_border_left) : std::floor(invalid_border_left); |
| 270 | invalid_border_top = (shape[1] % 2) ? std::ceil(invalid_border_top) : std::floor(invalid_border_top); |
| 271 | |
| 272 | // Set the anchor point |
| 273 | anchor.set(0, static_cast<int>(invalid_border_left)); |
| 274 | anchor.set(1, static_cast<int>(invalid_border_top)); |
| 275 | |
| 276 | // Compute shape |
| 277 | // Calculate the right and bottom invalid borders at the previous level of the pyramid |
| 278 | const float prev_invalid_border_right = static_cast<float>(shape[0] - (valid_region.anchor.x() + valid_region.shape[0])); |
| 279 | const float prev_invalid_border_bottom = static_cast<float>(shape[1] - (valid_region.anchor.y() + valid_region.shape[1])); |
| 280 | |
| 281 | // Calculate the right and bottom invalid borders at the current level of the pyramid |
| 282 | const float invalid_border_right = std::ceil((prev_invalid_border_right + static_cast<float>(border_size)) / 2.0f); |
| 283 | const float invalid_border_bottom = std::ceil((prev_invalid_border_bottom + static_cast<float>(border_size)) / 2.0f); |
| 284 | |
| 285 | const int valid_shape_x = std::max(0, static_cast<int>(dst_shape.x()) - static_cast<int>(invalid_border_left) - static_cast<int>(invalid_border_right)); |
| 286 | const int valid_shape_y = std::max(0, static_cast<int>(dst_shape.y()) - static_cast<int>(invalid_border_top) - static_cast<int>(invalid_border_bottom)); |
| 287 | |
| 288 | dst_shape.set(0, valid_shape_x); |
| 289 | dst_shape.set(1, valid_shape_y); |
| 290 | } |
| 291 | |
| 292 | return ValidRegion(std::move(anchor), std::move(dst_shape)); |
| 293 | } |
| 294 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 295 | /** Write the value after casting the pointer according to @p data_type. |
| 296 | * |
| 297 | * @warning The type of the value must match the specified data type. |
| 298 | * |
| 299 | * @param[out] ptr Pointer to memory where the @p value will be written. |
| 300 | * @param[in] value Value that will be written. |
| 301 | * @param[in] data_type Data type that will be written. |
| 302 | */ |
| 303 | template <typename T> |
| 304 | void store_value_with_data_type(void *ptr, T value, DataType data_type) |
| 305 | { |
| 306 | switch(data_type) |
| 307 | { |
| 308 | case DataType::U8: |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 309 | case DataType::QASYMM8: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 310 | *reinterpret_cast<uint8_t *>(ptr) = value; |
| 311 | break; |
| 312 | case DataType::S8: |
| 313 | case DataType::QS8: |
| 314 | *reinterpret_cast<int8_t *>(ptr) = value; |
| 315 | break; |
| 316 | case DataType::U16: |
| 317 | *reinterpret_cast<uint16_t *>(ptr) = value; |
| 318 | break; |
| 319 | case DataType::S16: |
Michalis Spyrou | 0a8334c | 2017-06-14 18:00:05 +0100 | [diff] [blame] | 320 | case DataType::QS16: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 321 | *reinterpret_cast<int16_t *>(ptr) = value; |
| 322 | break; |
| 323 | case DataType::U32: |
| 324 | *reinterpret_cast<uint32_t *>(ptr) = value; |
| 325 | break; |
| 326 | case DataType::S32: |
| 327 | *reinterpret_cast<int32_t *>(ptr) = value; |
| 328 | break; |
| 329 | case DataType::U64: |
| 330 | *reinterpret_cast<uint64_t *>(ptr) = value; |
| 331 | break; |
| 332 | case DataType::S64: |
| 333 | *reinterpret_cast<int64_t *>(ptr) = value; |
| 334 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 335 | case DataType::F16: |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 336 | *reinterpret_cast<half *>(ptr) = value; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 337 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 338 | case DataType::F32: |
| 339 | *reinterpret_cast<float *>(ptr) = value; |
| 340 | break; |
| 341 | case DataType::F64: |
| 342 | *reinterpret_cast<double *>(ptr) = value; |
| 343 | break; |
| 344 | case DataType::SIZET: |
| 345 | *reinterpret_cast<size_t *>(ptr) = value; |
| 346 | break; |
| 347 | default: |
| 348 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /** Saturate a value of type T against the numeric limits of type U. |
| 353 | * |
| 354 | * @param[in] val Value to be saturated. |
| 355 | * |
| 356 | * @return saturated value. |
| 357 | */ |
| 358 | template <typename U, typename T> |
| 359 | T saturate_cast(T val) |
| 360 | { |
| 361 | if(val > static_cast<T>(std::numeric_limits<U>::max())) |
| 362 | { |
| 363 | val = static_cast<T>(std::numeric_limits<U>::max()); |
| 364 | } |
| 365 | if(val < static_cast<T>(std::numeric_limits<U>::lowest())) |
| 366 | { |
| 367 | val = static_cast<T>(std::numeric_limits<U>::lowest()); |
| 368 | } |
| 369 | return val; |
| 370 | } |
| 371 | |
| 372 | /** Find the signed promoted common type. |
| 373 | */ |
| 374 | template <typename... T> |
| 375 | struct common_promoted_signed_type |
| 376 | { |
| 377 | using common_type = typename std::common_type<T...>::type; |
| 378 | using promoted_type = traits::promote_t<common_type>; |
| 379 | using intermediate_type = typename traits::make_signed_conditional_t<promoted_type>::type; |
| 380 | }; |
| 381 | |
John Richardson | 3c5f949 | 2017-10-04 15:27:37 +0100 | [diff] [blame] | 382 | /** Find the unsigned promoted common type. |
| 383 | */ |
| 384 | template <typename... T> |
| 385 | struct common_promoted_unsigned_type |
| 386 | { |
| 387 | using common_type = typename std::common_type<T...>::type; |
| 388 | using promoted_type = traits::promote_t<common_type>; |
| 389 | using intermediate_type = typename traits::make_unsigned_conditional_t<promoted_type>::type; |
| 390 | }; |
| 391 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 392 | /** Convert a linear index into n-dimensional coordinates. |
| 393 | * |
| 394 | * @param[in] shape Shape of the n-dimensional tensor. |
| 395 | * @param[in] index Linear index specifying the i-th element. |
| 396 | * |
| 397 | * @return n-dimensional coordinates. |
| 398 | */ |
| 399 | inline Coordinates index2coord(const TensorShape &shape, int index) |
| 400 | { |
| 401 | int num_elements = shape.total_size(); |
| 402 | |
| 403 | ARM_COMPUTE_ERROR_ON_MSG(index < 0 || index >= num_elements, "Index has to be in [0, num_elements]"); |
| 404 | ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create coordinate from empty shape"); |
| 405 | |
| 406 | Coordinates coord{ 0 }; |
| 407 | |
| 408 | for(int d = shape.num_dimensions() - 1; d >= 0; --d) |
| 409 | { |
| 410 | num_elements /= shape[d]; |
| 411 | coord.set(d, index / num_elements); |
| 412 | index %= num_elements; |
| 413 | } |
| 414 | |
| 415 | return coord; |
| 416 | } |
| 417 | |
| 418 | /** Linearise the given coordinate. |
| 419 | * |
| 420 | * Transforms the given coordinate into a linear offset in terms of |
| 421 | * elements. |
| 422 | * |
| 423 | * @param[in] shape Shape of the n-dimensional tensor. |
| 424 | * @param[in] coord The to be converted coordinate. |
| 425 | * |
| 426 | * @return Linear offset to the element. |
| 427 | */ |
| 428 | inline int coord2index(const TensorShape &shape, const Coordinates &coord) |
| 429 | { |
| 430 | ARM_COMPUTE_ERROR_ON_MSG(shape.total_size() == 0, "Cannot get index from empty shape"); |
| 431 | ARM_COMPUTE_ERROR_ON_MSG(coord.num_dimensions() == 0, "Cannot get index of empty coordinate"); |
| 432 | |
| 433 | int index = 0; |
| 434 | int dim_size = 1; |
| 435 | |
| 436 | for(unsigned int i = 0; i < coord.num_dimensions(); ++i) |
| 437 | { |
| 438 | index += coord[i] * dim_size; |
| 439 | dim_size *= shape[i]; |
| 440 | } |
| 441 | |
| 442 | return index; |
| 443 | } |
| 444 | |
| 445 | /** Check if a coordinate is within a valid region */ |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 446 | inline bool is_in_valid_region(const ValidRegion &valid_region, Coordinates coord) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 447 | { |
Moritz Pflanzer | 219c691 | 2017-09-23 19:22:51 +0100 | [diff] [blame] | 448 | for(size_t d = 0; d < Coordinates::num_max_dimensions; ++d) |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 449 | { |
| 450 | if(coord[d] < valid_region.start(d) || coord[d] >= valid_region.end(d)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 451 | { |
| 452 | return false; |
| 453 | } |
| 454 | } |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 455 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 456 | return true; |
| 457 | } |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 458 | |
| 459 | /** Create and initialize a tensor of the given type. |
| 460 | * |
| 461 | * @param[in] shape Tensor shape. |
| 462 | * @param[in] data_type Data type. |
| 463 | * @param[in] num_channels (Optional) Number of channels. |
| 464 | * @param[in] fixed_point_position (Optional) Number of fractional bits. |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 465 | * @param[in] quantization_info (Optional) Quantization info for asymmetric quantized types. |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 466 | * |
| 467 | * @return Initialized tensor of given type. |
| 468 | */ |
| 469 | template <typename T> |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 470 | inline T create_tensor(const TensorShape &shape, DataType data_type, int num_channels = 1, |
| 471 | int fixed_point_position = 0, QuantizationInfo quantization_info = QuantizationInfo()) |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 472 | { |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 473 | T tensor; |
| 474 | TensorInfo info(shape, num_channels, data_type, fixed_point_position); |
| 475 | info.set_quantization_info(quantization_info); |
| 476 | tensor.allocator()->init(info); |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 477 | |
| 478 | return tensor; |
| 479 | } |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 480 | |
Ioan-Cristian Szabo | 2c35018 | 2017-12-20 16:27:37 +0000 | [diff] [blame] | 481 | /** Create and initialize a tensor of the given type. |
| 482 | * |
| 483 | * @param[in] shape Tensor shape. |
| 484 | * @param[in] format Format type. |
| 485 | * |
| 486 | * @return Initialized tensor of given type. |
| 487 | */ |
| 488 | template <typename T> |
| 489 | inline T create_tensor(const TensorShape &shape, Format format) |
| 490 | { |
| 491 | TensorInfo info(shape, format); |
| 492 | |
| 493 | T tensor; |
| 494 | tensor.allocator()->init(info); |
| 495 | |
| 496 | return tensor; |
| 497 | } |
| 498 | |
John Richardson | 25f2368 | 2017-11-27 14:35:09 +0000 | [diff] [blame] | 499 | /** Create and initialize a HOG (Histogram of Oriented Gradients) of the given type. |
| 500 | * |
| 501 | * @param[in] cell_size Cell size in pixels |
| 502 | * @param[in] block_size Block size in pixels. Must be a multiple of cell_size. |
| 503 | * @param[in] detection_window_size Detection window size in pixels. Must be a multiple of block_size and block_stride. |
| 504 | * @param[in] block_stride Distance in pixels between 2 consecutive blocks along the x and y direction. Must be a multiple of cell size |
| 505 | * @param[in] num_bins Number of histogram bins for each cell |
| 506 | * @param[in] normalization_type (Optional) Normalization type to use for each block |
| 507 | * @param[in] l2_hyst_threshold (Optional) Threshold used for L2HYS_NORM normalization method |
| 508 | * @param[in] phase_type (Optional) Type of @ref PhaseType |
| 509 | * |
| 510 | * @return Initialized HOG of given type. |
| 511 | */ |
| 512 | template <typename T> |
| 513 | inline T create_HOG(const Size2D &cell_size, const Size2D &block_size, const Size2D &detection_window_size, const Size2D &block_stride, size_t num_bins, |
| 514 | HOGNormType normalization_type = HOGNormType::L2HYS_NORM, float l2_hyst_threshold = 0.2f, PhaseType phase_type = PhaseType::UNSIGNED) |
| 515 | { |
| 516 | T hog; |
| 517 | HOGInfo hog_info(cell_size, block_size, block_size, block_stride, num_bins, normalization_type, l2_hyst_threshold, phase_type); |
| 518 | hog.init(hog_info); |
| 519 | |
| 520 | return hog; |
| 521 | } |
| 522 | |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 523 | /** Create a vector of random ROIs. |
| 524 | * |
| 525 | * @param[in] shape The shape of the input tensor. |
| 526 | * @param[in] pool_info The ROI pooling information. |
| 527 | * @param[in] num_rois The number of ROIs to be created. |
| 528 | * @param[in] seed The random seed to be used. |
| 529 | * |
| 530 | * @return A vector that contains the requested number of random ROIs |
| 531 | */ |
| 532 | inline std::vector<ROI> generate_random_rois(const TensorShape &shape, const ROIPoolingLayerInfo &pool_info, unsigned int num_rois, std::random_device::result_type seed) |
| 533 | { |
| 534 | ARM_COMPUTE_ERROR_ON((pool_info.pooled_width() < 4) || (pool_info.pooled_height() < 4)); |
| 535 | |
| 536 | std::vector<ROI> rois; |
| 537 | std::mt19937 gen(seed); |
| 538 | const int pool_width = pool_info.pooled_width(); |
| 539 | const int pool_height = pool_info.pooled_height(); |
| 540 | const float roi_scale = pool_info.spatial_scale(); |
| 541 | |
| 542 | // Calculate distribution bounds |
| 543 | const auto scaled_width = static_cast<int>((shape.x() / roi_scale) / pool_width); |
| 544 | const auto scaled_height = static_cast<int>((shape.y() / roi_scale) / pool_height); |
| 545 | const auto min_width = static_cast<int>(pool_width / roi_scale); |
| 546 | const auto min_height = static_cast<int>(pool_height / roi_scale); |
| 547 | |
| 548 | // Create distributions |
| 549 | std::uniform_int_distribution<int> dist_batch(0, shape[3] - 1); |
| 550 | std::uniform_int_distribution<int> dist_x(0, scaled_width); |
| 551 | std::uniform_int_distribution<int> dist_y(0, scaled_height); |
| 552 | std::uniform_int_distribution<int> dist_w(min_width, std::max(min_width, (pool_width - 2) * scaled_width)); |
| 553 | std::uniform_int_distribution<int> dist_h(min_height, std::max(min_height, (pool_height - 2) * scaled_height)); |
| 554 | |
| 555 | for(unsigned int r = 0; r < num_rois; ++r) |
| 556 | { |
| 557 | ROI roi; |
| 558 | roi.batch_idx = dist_batch(gen); |
| 559 | roi.rect.x = dist_x(gen); |
| 560 | roi.rect.y = dist_y(gen); |
| 561 | roi.rect.width = dist_w(gen); |
| 562 | roi.rect.height = dist_h(gen); |
| 563 | rois.push_back(roi); |
| 564 | } |
| 565 | |
| 566 | return rois; |
| 567 | } |
| 568 | |
| 569 | template <typename T, typename ArrayAccessor_T> |
| 570 | inline void fill_array(ArrayAccessor_T &&array, const std::vector<T> &v) |
| 571 | { |
| 572 | array.resize(v.size()); |
| 573 | std::memcpy(array.buffer(), v.data(), v.size() * sizeof(T)); |
| 574 | } |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 575 | |
| 576 | /** Obtain numpy type string from DataType. |
| 577 | * |
| 578 | * @param[in] data_type Data type. |
| 579 | * |
| 580 | * @return numpy type string. |
| 581 | */ |
| 582 | inline std::string get_typestring(DataType data_type) |
| 583 | { |
| 584 | // Check endianness |
| 585 | const unsigned int i = 1; |
| 586 | const char *c = reinterpret_cast<const char *>(&i); |
| 587 | std::string endianness; |
| 588 | if(*c == 1) |
| 589 | { |
| 590 | endianness = std::string("<"); |
| 591 | } |
| 592 | else |
| 593 | { |
| 594 | endianness = std::string(">"); |
| 595 | } |
| 596 | const std::string no_endianness("|"); |
| 597 | |
| 598 | switch(data_type) |
| 599 | { |
| 600 | case DataType::U8: |
| 601 | return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t)); |
| 602 | case DataType::S8: |
| 603 | return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t)); |
| 604 | case DataType::U16: |
| 605 | return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t)); |
| 606 | case DataType::S16: |
| 607 | return endianness + "i" + support::cpp11::to_string(sizeof(int16_t)); |
| 608 | case DataType::U32: |
| 609 | return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t)); |
| 610 | case DataType::S32: |
| 611 | return endianness + "i" + support::cpp11::to_string(sizeof(int32_t)); |
| 612 | case DataType::U64: |
| 613 | return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t)); |
| 614 | case DataType::S64: |
| 615 | return endianness + "i" + support::cpp11::to_string(sizeof(int64_t)); |
| 616 | case DataType::F32: |
| 617 | return endianness + "f" + support::cpp11::to_string(sizeof(float)); |
| 618 | case DataType::F64: |
| 619 | return endianness + "f" + support::cpp11::to_string(sizeof(double)); |
| 620 | case DataType::SIZET: |
| 621 | return endianness + "u" + support::cpp11::to_string(sizeof(size_t)); |
| 622 | default: |
| 623 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 624 | } |
| 625 | } |
Joel Liang | 1c5ffd6 | 2017-12-28 10:09:51 +0800 | [diff] [blame] | 626 | |
| 627 | /** Sync if necessary. |
| 628 | */ |
| 629 | template <typename TensorType> |
| 630 | inline void sync_if_necessary() |
| 631 | { |
| 632 | #ifdef ARM_COMPUTE_CL |
| 633 | if(opencl_is_available() && std::is_same<typename std::decay<TensorType>::type, arm_compute::CLTensor>::value) |
| 634 | { |
| 635 | CLScheduler::get().sync(); |
| 636 | } |
| 637 | #endif /* ARM_COMPUTE_CL */ |
| 638 | } |
| 639 | |
| 640 | /** Sync tensor if necessary. |
| 641 | * |
| 642 | * @note: If the destination tensor not being used on OpenGL ES, GPU will optimize out the operation. |
| 643 | * |
| 644 | * @param[in] tensor Tensor to be sync. |
| 645 | */ |
| 646 | template <typename TensorType> |
| 647 | inline void sync_tensor_if_necessary(TensorType &tensor) |
| 648 | { |
| 649 | #ifdef ARM_COMPUTE_GC |
| 650 | if(opengles31_is_available() && std::is_same<typename std::decay<TensorType>::type, arm_compute::GCTensor>::value) |
| 651 | { |
| 652 | // Force sync the tensor by calling map and unmap. |
| 653 | IGCTensor &t = dynamic_cast<IGCTensor &>(tensor); |
| 654 | t.map(); |
| 655 | t.unmap(); |
| 656 | } |
| 657 | #endif /* ARM_COMPUTE_GC */ |
| 658 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 659 | } // namespace test |
| 660 | } // namespace arm_compute |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 661 | #endif /* __ARM_COMPUTE_TEST_UTILS_H__ */ |