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> |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 39 | #include <random> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 40 | #include <sstream> |
| 41 | #include <string> |
| 42 | #include <type_traits> |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 43 | #include <vector> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | |
| 45 | namespace arm_compute |
| 46 | { |
| 47 | namespace test |
| 48 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | /** Round floating-point value with half value rounding to positive infinity. |
| 50 | * |
| 51 | * @param[in] value floating-point value to be rounded. |
| 52 | * |
| 53 | * @return Floating-point value of rounded @p value. |
| 54 | */ |
| 55 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 56 | inline T round_half_up(T value) |
| 57 | { |
| 58 | return std::floor(value + 0.5f); |
| 59 | } |
| 60 | |
| 61 | /** Round floating-point value with half value rounding to nearest even. |
| 62 | * |
| 63 | * @param[in] value floating-point value to be rounded. |
| 64 | * @param[in] epsilon precision. |
| 65 | * |
| 66 | * @return Floating-point value of rounded @p value. |
| 67 | */ |
| 68 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 69 | inline T round_half_even(T value, T epsilon = std::numeric_limits<T>::epsilon()) |
| 70 | { |
| 71 | T positive_value = std::abs(value); |
| 72 | T ipart = 0; |
| 73 | std::modf(positive_value, &ipart); |
| 74 | // If 'value' is exactly halfway between two integers |
| 75 | if(std::abs(positive_value - (ipart + 0.5f)) < epsilon) |
| 76 | { |
| 77 | // If 'ipart' is even then return 'ipart' |
| 78 | if(std::fmod(ipart, 2.f) < epsilon) |
| 79 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 80 | return support::cpp11::copysign(ipart, value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | } |
| 82 | // Else return the nearest even integer |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 83 | return support::cpp11::copysign(std::ceil(ipart + 0.5f), value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 84 | } |
| 85 | // Otherwise use the usual round to closest |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 86 | return support::cpp11::copysign(support::cpp11::round(positive_value), value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 87 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 88 | |
| 89 | namespace traits |
| 90 | { |
| 91 | // *INDENT-OFF* |
| 92 | // clang-format off |
| 93 | template <typename T> struct promote { }; |
| 94 | template <> struct promote<uint8_t> { using type = uint16_t; }; |
| 95 | template <> struct promote<int8_t> { using type = int16_t; }; |
| 96 | template <> struct promote<uint16_t> { using type = uint32_t; }; |
| 97 | template <> struct promote<int16_t> { using type = int32_t; }; |
| 98 | template <> struct promote<uint32_t> { using type = uint64_t; }; |
| 99 | template <> struct promote<int32_t> { using type = int64_t; }; |
| 100 | template <> struct promote<float> { using type = float; }; |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 101 | template <> struct promote<half> { using type = half; }; |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 102 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | |
| 104 | template <typename T> |
| 105 | using promote_t = typename promote<T>::type; |
| 106 | |
| 107 | template <typename T> |
| 108 | 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] | 109 | |
| 110 | template <typename T> |
| 111 | using make_unsigned_conditional_t = typename std::conditional<std::is_integral<T>::value, std::make_unsigned<T>, std::common_type<T>>::type; |
| 112 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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; |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 200 | anchor.set_num_dimensions(shape.num_dimensions()); |
| 201 | |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 202 | if(border_undefined) |
| 203 | { |
| 204 | ARM_COMPUTE_ERROR_ON(shape.num_dimensions() < 2); |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 205 | |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 206 | anchor.set(0, border_size.left); |
| 207 | anchor.set(1, border_size.top); |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 208 | |
| 209 | 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)); |
| 210 | 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)); |
| 211 | |
| 212 | shape.set(0, valid_shape_x); |
| 213 | shape.set(1, valid_shape_y); |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 214 | } |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 215 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 216 | return ValidRegion(std::move(anchor), std::move(shape)); |
| 217 | } |
| 218 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 219 | /** Write the value after casting the pointer according to @p data_type. |
| 220 | * |
| 221 | * @warning The type of the value must match the specified data type. |
| 222 | * |
| 223 | * @param[out] ptr Pointer to memory where the @p value will be written. |
| 224 | * @param[in] value Value that will be written. |
| 225 | * @param[in] data_type Data type that will be written. |
| 226 | */ |
| 227 | template <typename T> |
| 228 | void store_value_with_data_type(void *ptr, T value, DataType data_type) |
| 229 | { |
| 230 | switch(data_type) |
| 231 | { |
| 232 | case DataType::U8: |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 233 | case DataType::QASYMM8: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 234 | *reinterpret_cast<uint8_t *>(ptr) = value; |
| 235 | break; |
| 236 | case DataType::S8: |
| 237 | case DataType::QS8: |
| 238 | *reinterpret_cast<int8_t *>(ptr) = value; |
| 239 | break; |
| 240 | case DataType::U16: |
| 241 | *reinterpret_cast<uint16_t *>(ptr) = value; |
| 242 | break; |
| 243 | case DataType::S16: |
Michalis Spyrou | 0a8334c | 2017-06-14 18:00:05 +0100 | [diff] [blame] | 244 | case DataType::QS16: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 245 | *reinterpret_cast<int16_t *>(ptr) = value; |
| 246 | break; |
| 247 | case DataType::U32: |
| 248 | *reinterpret_cast<uint32_t *>(ptr) = value; |
| 249 | break; |
| 250 | case DataType::S32: |
| 251 | *reinterpret_cast<int32_t *>(ptr) = value; |
| 252 | break; |
| 253 | case DataType::U64: |
| 254 | *reinterpret_cast<uint64_t *>(ptr) = value; |
| 255 | break; |
| 256 | case DataType::S64: |
| 257 | *reinterpret_cast<int64_t *>(ptr) = value; |
| 258 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 259 | case DataType::F16: |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 260 | *reinterpret_cast<half *>(ptr) = value; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 261 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 262 | case DataType::F32: |
| 263 | *reinterpret_cast<float *>(ptr) = value; |
| 264 | break; |
| 265 | case DataType::F64: |
| 266 | *reinterpret_cast<double *>(ptr) = value; |
| 267 | break; |
| 268 | case DataType::SIZET: |
| 269 | *reinterpret_cast<size_t *>(ptr) = value; |
| 270 | break; |
| 271 | default: |
| 272 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /** Saturate a value of type T against the numeric limits of type U. |
| 277 | * |
| 278 | * @param[in] val Value to be saturated. |
| 279 | * |
| 280 | * @return saturated value. |
| 281 | */ |
| 282 | template <typename U, typename T> |
| 283 | T saturate_cast(T val) |
| 284 | { |
| 285 | if(val > static_cast<T>(std::numeric_limits<U>::max())) |
| 286 | { |
| 287 | val = static_cast<T>(std::numeric_limits<U>::max()); |
| 288 | } |
| 289 | if(val < static_cast<T>(std::numeric_limits<U>::lowest())) |
| 290 | { |
| 291 | val = static_cast<T>(std::numeric_limits<U>::lowest()); |
| 292 | } |
| 293 | return val; |
| 294 | } |
| 295 | |
| 296 | /** Find the signed promoted common type. |
| 297 | */ |
| 298 | template <typename... T> |
| 299 | struct common_promoted_signed_type |
| 300 | { |
| 301 | using common_type = typename std::common_type<T...>::type; |
| 302 | using promoted_type = traits::promote_t<common_type>; |
| 303 | using intermediate_type = typename traits::make_signed_conditional_t<promoted_type>::type; |
| 304 | }; |
| 305 | |
John Richardson | 3c5f949 | 2017-10-04 15:27:37 +0100 | [diff] [blame] | 306 | /** Find the unsigned promoted common type. |
| 307 | */ |
| 308 | template <typename... T> |
| 309 | struct common_promoted_unsigned_type |
| 310 | { |
| 311 | using common_type = typename std::common_type<T...>::type; |
| 312 | using promoted_type = traits::promote_t<common_type>; |
| 313 | using intermediate_type = typename traits::make_unsigned_conditional_t<promoted_type>::type; |
| 314 | }; |
| 315 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 316 | /** Convert a linear index into n-dimensional coordinates. |
| 317 | * |
| 318 | * @param[in] shape Shape of the n-dimensional tensor. |
| 319 | * @param[in] index Linear index specifying the i-th element. |
| 320 | * |
| 321 | * @return n-dimensional coordinates. |
| 322 | */ |
| 323 | inline Coordinates index2coord(const TensorShape &shape, int index) |
| 324 | { |
| 325 | int num_elements = shape.total_size(); |
| 326 | |
| 327 | ARM_COMPUTE_ERROR_ON_MSG(index < 0 || index >= num_elements, "Index has to be in [0, num_elements]"); |
| 328 | ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create coordinate from empty shape"); |
| 329 | |
| 330 | Coordinates coord{ 0 }; |
| 331 | |
| 332 | for(int d = shape.num_dimensions() - 1; d >= 0; --d) |
| 333 | { |
| 334 | num_elements /= shape[d]; |
| 335 | coord.set(d, index / num_elements); |
| 336 | index %= num_elements; |
| 337 | } |
| 338 | |
| 339 | return coord; |
| 340 | } |
| 341 | |
| 342 | /** Linearise the given coordinate. |
| 343 | * |
| 344 | * Transforms the given coordinate into a linear offset in terms of |
| 345 | * elements. |
| 346 | * |
| 347 | * @param[in] shape Shape of the n-dimensional tensor. |
| 348 | * @param[in] coord The to be converted coordinate. |
| 349 | * |
| 350 | * @return Linear offset to the element. |
| 351 | */ |
| 352 | inline int coord2index(const TensorShape &shape, const Coordinates &coord) |
| 353 | { |
| 354 | ARM_COMPUTE_ERROR_ON_MSG(shape.total_size() == 0, "Cannot get index from empty shape"); |
| 355 | ARM_COMPUTE_ERROR_ON_MSG(coord.num_dimensions() == 0, "Cannot get index of empty coordinate"); |
| 356 | |
| 357 | int index = 0; |
| 358 | int dim_size = 1; |
| 359 | |
| 360 | for(unsigned int i = 0; i < coord.num_dimensions(); ++i) |
| 361 | { |
| 362 | index += coord[i] * dim_size; |
| 363 | dim_size *= shape[i]; |
| 364 | } |
| 365 | |
| 366 | return index; |
| 367 | } |
| 368 | |
| 369 | /** Check if a coordinate is within a valid region */ |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 370 | inline bool is_in_valid_region(const ValidRegion &valid_region, Coordinates coord) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 371 | { |
Moritz Pflanzer | 219c691 | 2017-09-23 19:22:51 +0100 | [diff] [blame] | 372 | for(size_t d = 0; d < Coordinates::num_max_dimensions; ++d) |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 373 | { |
| 374 | 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] | 375 | { |
| 376 | return false; |
| 377 | } |
| 378 | } |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 379 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 380 | return true; |
| 381 | } |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 382 | |
| 383 | /** Create and initialize a tensor of the given type. |
| 384 | * |
| 385 | * @param[in] shape Tensor shape. |
| 386 | * @param[in] data_type Data type. |
| 387 | * @param[in] num_channels (Optional) Number of channels. |
| 388 | * @param[in] fixed_point_position (Optional) Number of fractional bits. |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 389 | * @param[in] quantization_info (Optional) Quantization info for asymmetric quantized types. |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 390 | * |
| 391 | * @return Initialized tensor of given type. |
| 392 | */ |
| 393 | template <typename T> |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 394 | inline T create_tensor(const TensorShape &shape, DataType data_type, int num_channels = 1, |
| 395 | int fixed_point_position = 0, QuantizationInfo quantization_info = QuantizationInfo()) |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 396 | { |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 397 | T tensor; |
| 398 | TensorInfo info(shape, num_channels, data_type, fixed_point_position); |
| 399 | info.set_quantization_info(quantization_info); |
| 400 | tensor.allocator()->init(info); |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 401 | |
| 402 | return tensor; |
| 403 | } |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 404 | |
| 405 | /** Create a vector of random ROIs. |
| 406 | * |
| 407 | * @param[in] shape The shape of the input tensor. |
| 408 | * @param[in] pool_info The ROI pooling information. |
| 409 | * @param[in] num_rois The number of ROIs to be created. |
| 410 | * @param[in] seed The random seed to be used. |
| 411 | * |
| 412 | * @return A vector that contains the requested number of random ROIs |
| 413 | */ |
| 414 | inline std::vector<ROI> generate_random_rois(const TensorShape &shape, const ROIPoolingLayerInfo &pool_info, unsigned int num_rois, std::random_device::result_type seed) |
| 415 | { |
| 416 | ARM_COMPUTE_ERROR_ON((pool_info.pooled_width() < 4) || (pool_info.pooled_height() < 4)); |
| 417 | |
| 418 | std::vector<ROI> rois; |
| 419 | std::mt19937 gen(seed); |
| 420 | const int pool_width = pool_info.pooled_width(); |
| 421 | const int pool_height = pool_info.pooled_height(); |
| 422 | const float roi_scale = pool_info.spatial_scale(); |
| 423 | |
| 424 | // Calculate distribution bounds |
| 425 | const auto scaled_width = static_cast<int>((shape.x() / roi_scale) / pool_width); |
| 426 | const auto scaled_height = static_cast<int>((shape.y() / roi_scale) / pool_height); |
| 427 | const auto min_width = static_cast<int>(pool_width / roi_scale); |
| 428 | const auto min_height = static_cast<int>(pool_height / roi_scale); |
| 429 | |
| 430 | // Create distributions |
| 431 | std::uniform_int_distribution<int> dist_batch(0, shape[3] - 1); |
| 432 | std::uniform_int_distribution<int> dist_x(0, scaled_width); |
| 433 | std::uniform_int_distribution<int> dist_y(0, scaled_height); |
| 434 | std::uniform_int_distribution<int> dist_w(min_width, std::max(min_width, (pool_width - 2) * scaled_width)); |
| 435 | std::uniform_int_distribution<int> dist_h(min_height, std::max(min_height, (pool_height - 2) * scaled_height)); |
| 436 | |
| 437 | for(unsigned int r = 0; r < num_rois; ++r) |
| 438 | { |
| 439 | ROI roi; |
| 440 | roi.batch_idx = dist_batch(gen); |
| 441 | roi.rect.x = dist_x(gen); |
| 442 | roi.rect.y = dist_y(gen); |
| 443 | roi.rect.width = dist_w(gen); |
| 444 | roi.rect.height = dist_h(gen); |
| 445 | rois.push_back(roi); |
| 446 | } |
| 447 | |
| 448 | return rois; |
| 449 | } |
| 450 | |
| 451 | template <typename T, typename ArrayAccessor_T> |
| 452 | inline void fill_array(ArrayAccessor_T &&array, const std::vector<T> &v) |
| 453 | { |
| 454 | array.resize(v.size()); |
| 455 | std::memcpy(array.buffer(), v.data(), v.size() * sizeof(T)); |
| 456 | } |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 457 | |
| 458 | /** Obtain numpy type string from DataType. |
| 459 | * |
| 460 | * @param[in] data_type Data type. |
| 461 | * |
| 462 | * @return numpy type string. |
| 463 | */ |
| 464 | inline std::string get_typestring(DataType data_type) |
| 465 | { |
| 466 | // Check endianness |
| 467 | const unsigned int i = 1; |
| 468 | const char *c = reinterpret_cast<const char *>(&i); |
| 469 | std::string endianness; |
| 470 | if(*c == 1) |
| 471 | { |
| 472 | endianness = std::string("<"); |
| 473 | } |
| 474 | else |
| 475 | { |
| 476 | endianness = std::string(">"); |
| 477 | } |
| 478 | const std::string no_endianness("|"); |
| 479 | |
| 480 | switch(data_type) |
| 481 | { |
| 482 | case DataType::U8: |
| 483 | return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t)); |
| 484 | case DataType::S8: |
| 485 | return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t)); |
| 486 | case DataType::U16: |
| 487 | return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t)); |
| 488 | case DataType::S16: |
| 489 | return endianness + "i" + support::cpp11::to_string(sizeof(int16_t)); |
| 490 | case DataType::U32: |
| 491 | return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t)); |
| 492 | case DataType::S32: |
| 493 | return endianness + "i" + support::cpp11::to_string(sizeof(int32_t)); |
| 494 | case DataType::U64: |
| 495 | return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t)); |
| 496 | case DataType::S64: |
| 497 | return endianness + "i" + support::cpp11::to_string(sizeof(int64_t)); |
| 498 | case DataType::F32: |
| 499 | return endianness + "f" + support::cpp11::to_string(sizeof(float)); |
| 500 | case DataType::F64: |
| 501 | return endianness + "f" + support::cpp11::to_string(sizeof(double)); |
| 502 | case DataType::SIZET: |
| 503 | return endianness + "u" + support::cpp11::to_string(sizeof(size_t)); |
| 504 | default: |
| 505 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 506 | } |
| 507 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 508 | } // namespace test |
| 509 | } // namespace arm_compute |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 510 | #endif /* __ARM_COMPUTE_TEST_UTILS_H__ */ |