Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
SiCong Li | 5a63d1e | 2023-01-06 16:28:57 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2023 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_TEST_UTILS_H |
| 25 | #define ARM_COMPUTE_TEST_UTILS_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
| 27 | #include "arm_compute/core/Coordinates.h" |
| 28 | #include "arm_compute/core/Error.h" |
John Richardson | 25f2368 | 2017-11-27 14:35:09 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/Size2D.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" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 33 | #include "support/StringSupport.h" |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 34 | #include "support/ToolchainSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | |
Joel Liang | 1c5ffd6 | 2017-12-28 10:09:51 +0800 | [diff] [blame] | 36 | #ifdef ARM_COMPUTE_CL |
| 37 | #include "arm_compute/core/CL/OpenCL.h" |
| 38 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 39 | #endif /* ARM_COMPUTE_CL */ |
| 40 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 41 | #include <cmath> |
| 42 | #include <cstddef> |
| 43 | #include <limits> |
| 44 | #include <memory> |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 45 | #include <random> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | #include <sstream> |
| 47 | #include <string> |
| 48 | #include <type_traits> |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 49 | #include <vector> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 50 | |
SiCong Li | 5a63d1e | 2023-01-06 16:28:57 +0000 | [diff] [blame] | 51 | #include "arm_compute/dynamic_fusion/sketch/attributes/Conv2dAttributes.h" |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 52 | #include "arm_compute/runtime/CPP/CPPScheduler.h" |
| 53 | #include "arm_compute/runtime/RuntimeContext.h" |
| 54 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 55 | namespace arm_compute |
| 56 | { |
Joel Liang | 1c5ffd6 | 2017-12-28 10:09:51 +0800 | [diff] [blame] | 57 | #ifdef ARM_COMPUTE_CL |
| 58 | class CLTensor; |
| 59 | #endif /* ARM_COMPUTE_CL */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 60 | namespace test |
| 61 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 62 | /** Round floating-point value with half value rounding to positive infinity. |
| 63 | * |
| 64 | * @param[in] value floating-point value to be rounded. |
| 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_up(T value) |
| 70 | { |
| 71 | return std::floor(value + 0.5f); |
| 72 | } |
| 73 | |
| 74 | /** Round floating-point value with half value rounding to nearest even. |
| 75 | * |
| 76 | * @param[in] value floating-point value to be rounded. |
| 77 | * @param[in] epsilon precision. |
| 78 | * |
| 79 | * @return Floating-point value of rounded @p value. |
| 80 | */ |
| 81 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 82 | inline T round_half_even(T value, T epsilon = std::numeric_limits<T>::epsilon()) |
| 83 | { |
| 84 | T positive_value = std::abs(value); |
| 85 | T ipart = 0; |
| 86 | std::modf(positive_value, &ipart); |
| 87 | // If 'value' is exactly halfway between two integers |
| 88 | if(std::abs(positive_value - (ipart + 0.5f)) < epsilon) |
| 89 | { |
| 90 | // If 'ipart' is even then return 'ipart' |
| 91 | if(std::fmod(ipart, 2.f) < epsilon) |
| 92 | { |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 93 | return support::cpp11::copysign(ipart, value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 94 | } |
| 95 | // Else return the nearest even integer |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 96 | return support::cpp11::copysign(std::ceil(ipart + 0.5f), value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | } |
| 98 | // Otherwise use the usual round to closest |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 99 | return support::cpp11::copysign(support::cpp11::round(positive_value), value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 101 | |
| 102 | namespace traits |
| 103 | { |
| 104 | // *INDENT-OFF* |
| 105 | // clang-format off |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 106 | /** Promote a type */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 107 | template <typename T> struct promote { }; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 108 | /** Promote uint8_t to uint16_t */ |
| 109 | template <> struct promote<uint8_t> { using type = uint16_t; /**< Promoted type */ }; |
| 110 | /** Promote int8_t to int16_t */ |
| 111 | template <> struct promote<int8_t> { using type = int16_t; /**< Promoted type */ }; |
| 112 | /** Promote uint16_t to uint32_t */ |
| 113 | template <> struct promote<uint16_t> { using type = uint32_t; /**< Promoted type */ }; |
| 114 | /** Promote int16_t to int32_t */ |
| 115 | template <> struct promote<int16_t> { using type = int32_t; /**< Promoted type */ }; |
| 116 | /** Promote uint32_t to uint64_t */ |
| 117 | template <> struct promote<uint32_t> { using type = uint64_t; /**< Promoted type */ }; |
| 118 | /** Promote int32_t to int64_t */ |
| 119 | template <> struct promote<int32_t> { using type = int64_t; /**< Promoted type */ }; |
| 120 | /** Promote float to float */ |
| 121 | template <> struct promote<float> { using type = float; /**< Promoted type */ }; |
| 122 | /** Promote half to half */ |
| 123 | template <> struct promote<half> { using type = half; /**< Promoted type */ }; |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 124 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 125 | /** Get promoted type */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 126 | template <typename T> |
| 127 | using promote_t = typename promote<T>::type; |
| 128 | |
| 129 | template <typename T> |
| 130 | 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] | 131 | |
| 132 | template <typename T> |
| 133 | using make_unsigned_conditional_t = typename std::conditional<std::is_integral<T>::value, std::make_unsigned<T>, std::common_type<T>>::type; |
| 134 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 135 | // clang-format on |
| 136 | // *INDENT-ON* |
Ramy Elgammal | 73f19af | 2022-10-23 11:44:49 +0100 | [diff] [blame] | 137 | } // namespace traits |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 138 | |
| 139 | /** Look up the format corresponding to a channel. |
| 140 | * |
| 141 | * @param[in] channel Channel type. |
| 142 | * |
| 143 | * @return Format that contains the given channel. |
| 144 | */ |
| 145 | inline Format get_format_for_channel(Channel channel) |
| 146 | { |
| 147 | switch(channel) |
| 148 | { |
| 149 | case Channel::R: |
| 150 | case Channel::G: |
| 151 | case Channel::B: |
| 152 | return Format::RGB888; |
| 153 | default: |
| 154 | throw std::runtime_error("Unsupported channel"); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** Return the format of a channel. |
| 159 | * |
| 160 | * @param[in] channel Channel type. |
| 161 | * |
| 162 | * @return Format of the given channel. |
| 163 | */ |
| 164 | inline Format get_channel_format(Channel channel) |
| 165 | { |
| 166 | switch(channel) |
| 167 | { |
| 168 | case Channel::R: |
| 169 | case Channel::G: |
| 170 | case Channel::B: |
| 171 | return Format::U8; |
| 172 | default: |
| 173 | throw std::runtime_error("Unsupported channel"); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /** Base case of foldl. |
| 178 | * |
| 179 | * @return value. |
| 180 | */ |
| 181 | template <typename F, typename T> |
| 182 | inline T foldl(F &&, const T &value) |
| 183 | { |
| 184 | return value; |
| 185 | } |
| 186 | |
| 187 | /** Base case of foldl. |
| 188 | * |
| 189 | * @return func(value1, value2). |
| 190 | */ |
| 191 | template <typename F, typename T, typename U> |
| 192 | inline auto foldl(F &&func, T &&value1, U &&value2) -> decltype(func(value1, value2)) |
| 193 | { |
| 194 | return func(value1, value2); |
| 195 | } |
| 196 | |
| 197 | /** Fold left. |
| 198 | * |
| 199 | * @param[in] func Binary function to be called. |
| 200 | * @param[in] initial Initial value. |
| 201 | * @param[in] value Argument passed to the function. |
| 202 | * @param[in] values Remaining arguments. |
| 203 | */ |
| 204 | template <typename F, typename I, typename T, typename... Vs> |
| 205 | inline I foldl(F &&func, I &&initial, T &&value, Vs &&... values) |
| 206 | { |
| 207 | return foldl(std::forward<F>(func), func(std::forward<I>(initial), std::forward<T>(value)), std::forward<Vs>(values)...); |
| 208 | } |
| 209 | |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 210 | /** 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] | 211 | * |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 212 | * @param[in] a_shape Shape used as size of the valid region. |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 213 | * @param[in] border_undefined (Optional) Boolean indicating if the border mode is undefined. |
| 214 | * @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] | 215 | * |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 216 | * @return A valid region starting at (0, 0, ...) with size of @p shape if @p border_undefined is false; otherwise |
| 217 | * 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] | 218 | */ |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 219 | inline ValidRegion shape_to_valid_region(const TensorShape &a_shape, bool border_undefined = false, BorderSize border_size = BorderSize(0)) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 220 | { |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 221 | ValidRegion valid_region{ Coordinates(), a_shape }; |
| 222 | |
| 223 | Coordinates &anchor = valid_region.anchor; |
| 224 | TensorShape &shape = valid_region.shape; |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 225 | |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 226 | if(border_undefined) |
| 227 | { |
| 228 | ARM_COMPUTE_ERROR_ON(shape.num_dimensions() < 2); |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 229 | |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 230 | anchor.set(0, border_size.left); |
| 231 | anchor.set(1, border_size.top); |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 232 | |
| 233 | 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)); |
| 234 | 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)); |
| 235 | |
| 236 | shape.set(0, valid_shape_x); |
| 237 | shape.set(1, valid_shape_y); |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 238 | } |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 239 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 240 | return valid_region; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 241 | } |
| 242 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 243 | /** Write the value after casting the pointer according to @p data_type. |
| 244 | * |
| 245 | * @warning The type of the value must match the specified data type. |
| 246 | * |
| 247 | * @param[out] ptr Pointer to memory where the @p value will be written. |
| 248 | * @param[in] value Value that will be written. |
| 249 | * @param[in] data_type Data type that will be written. |
| 250 | */ |
| 251 | template <typename T> |
| 252 | void store_value_with_data_type(void *ptr, T value, DataType data_type) |
| 253 | { |
| 254 | switch(data_type) |
| 255 | { |
| 256 | case DataType::U8: |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 257 | case DataType::QASYMM8: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 258 | *reinterpret_cast<uint8_t *>(ptr) = value; |
| 259 | break; |
| 260 | case DataType::S8: |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 261 | case DataType::QASYMM8_SIGNED: |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 262 | case DataType::QSYMM8: |
| 263 | case DataType::QSYMM8_PER_CHANNEL: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 264 | *reinterpret_cast<int8_t *>(ptr) = value; |
| 265 | break; |
| 266 | case DataType::U16: |
Michele Di Giorgio | 35ea9a7 | 2019-08-23 12:02:06 +0100 | [diff] [blame] | 267 | case DataType::QASYMM16: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 268 | *reinterpret_cast<uint16_t *>(ptr) = value; |
| 269 | break; |
| 270 | case DataType::S16: |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 271 | case DataType::QSYMM16: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 272 | *reinterpret_cast<int16_t *>(ptr) = value; |
| 273 | break; |
| 274 | case DataType::U32: |
| 275 | *reinterpret_cast<uint32_t *>(ptr) = value; |
| 276 | break; |
| 277 | case DataType::S32: |
| 278 | *reinterpret_cast<int32_t *>(ptr) = value; |
| 279 | break; |
| 280 | case DataType::U64: |
| 281 | *reinterpret_cast<uint64_t *>(ptr) = value; |
| 282 | break; |
| 283 | case DataType::S64: |
| 284 | *reinterpret_cast<int64_t *>(ptr) = value; |
| 285 | break; |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 286 | case DataType::BFLOAT16: |
| 287 | *reinterpret_cast<bfloat16 *>(ptr) = bfloat16(value); |
| 288 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 289 | case DataType::F16: |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 290 | *reinterpret_cast<half *>(ptr) = value; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 291 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 292 | case DataType::F32: |
| 293 | *reinterpret_cast<float *>(ptr) = value; |
| 294 | break; |
| 295 | case DataType::F64: |
| 296 | *reinterpret_cast<double *>(ptr) = value; |
| 297 | break; |
| 298 | case DataType::SIZET: |
| 299 | *reinterpret_cast<size_t *>(ptr) = value; |
| 300 | break; |
| 301 | default: |
| 302 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /** Saturate a value of type T against the numeric limits of type U. |
| 307 | * |
| 308 | * @param[in] val Value to be saturated. |
| 309 | * |
| 310 | * @return saturated value. |
| 311 | */ |
| 312 | template <typename U, typename T> |
| 313 | T saturate_cast(T val) |
| 314 | { |
| 315 | if(val > static_cast<T>(std::numeric_limits<U>::max())) |
| 316 | { |
| 317 | val = static_cast<T>(std::numeric_limits<U>::max()); |
| 318 | } |
| 319 | if(val < static_cast<T>(std::numeric_limits<U>::lowest())) |
| 320 | { |
| 321 | val = static_cast<T>(std::numeric_limits<U>::lowest()); |
| 322 | } |
| 323 | return val; |
| 324 | } |
| 325 | |
| 326 | /** Find the signed promoted common type. |
| 327 | */ |
| 328 | template <typename... T> |
| 329 | struct common_promoted_signed_type |
| 330 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 331 | /** Common type */ |
| 332 | using common_type = typename std::common_type<T...>::type; |
| 333 | /** Promoted type */ |
| 334 | using promoted_type = traits::promote_t<common_type>; |
| 335 | /** Intermediate type */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 336 | using intermediate_type = typename traits::make_signed_conditional_t<promoted_type>::type; |
| 337 | }; |
| 338 | |
John Richardson | 3c5f949 | 2017-10-04 15:27:37 +0100 | [diff] [blame] | 339 | /** Find the unsigned promoted common type. |
| 340 | */ |
| 341 | template <typename... T> |
| 342 | struct common_promoted_unsigned_type |
| 343 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 344 | /** Common type */ |
| 345 | using common_type = typename std::common_type<T...>::type; |
| 346 | /** Promoted type */ |
| 347 | using promoted_type = traits::promote_t<common_type>; |
| 348 | /** Intermediate type */ |
John Richardson | 3c5f949 | 2017-10-04 15:27:37 +0100 | [diff] [blame] | 349 | using intermediate_type = typename traits::make_unsigned_conditional_t<promoted_type>::type; |
| 350 | }; |
| 351 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 352 | /** Convert a linear index into n-dimensional coordinates. |
| 353 | * |
| 354 | * @param[in] shape Shape of the n-dimensional tensor. |
| 355 | * @param[in] index Linear index specifying the i-th element. |
| 356 | * |
| 357 | * @return n-dimensional coordinates. |
| 358 | */ |
| 359 | inline Coordinates index2coord(const TensorShape &shape, int index) |
| 360 | { |
| 361 | int num_elements = shape.total_size(); |
| 362 | |
| 363 | ARM_COMPUTE_ERROR_ON_MSG(index < 0 || index >= num_elements, "Index has to be in [0, num_elements]"); |
| 364 | ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create coordinate from empty shape"); |
| 365 | |
| 366 | Coordinates coord{ 0 }; |
| 367 | |
| 368 | for(int d = shape.num_dimensions() - 1; d >= 0; --d) |
| 369 | { |
| 370 | num_elements /= shape[d]; |
| 371 | coord.set(d, index / num_elements); |
| 372 | index %= num_elements; |
| 373 | } |
| 374 | |
| 375 | return coord; |
| 376 | } |
| 377 | |
| 378 | /** Linearise the given coordinate. |
| 379 | * |
| 380 | * Transforms the given coordinate into a linear offset in terms of |
| 381 | * elements. |
| 382 | * |
| 383 | * @param[in] shape Shape of the n-dimensional tensor. |
| 384 | * @param[in] coord The to be converted coordinate. |
| 385 | * |
| 386 | * @return Linear offset to the element. |
| 387 | */ |
| 388 | inline int coord2index(const TensorShape &shape, const Coordinates &coord) |
| 389 | { |
| 390 | ARM_COMPUTE_ERROR_ON_MSG(shape.total_size() == 0, "Cannot get index from empty shape"); |
| 391 | ARM_COMPUTE_ERROR_ON_MSG(coord.num_dimensions() == 0, "Cannot get index of empty coordinate"); |
| 392 | |
| 393 | int index = 0; |
| 394 | int dim_size = 1; |
| 395 | |
| 396 | for(unsigned int i = 0; i < coord.num_dimensions(); ++i) |
| 397 | { |
| 398 | index += coord[i] * dim_size; |
| 399 | dim_size *= shape[i]; |
| 400 | } |
| 401 | |
| 402 | return index; |
| 403 | } |
| 404 | |
| 405 | /** Check if a coordinate is within a valid region */ |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 406 | inline bool is_in_valid_region(const ValidRegion &valid_region, Coordinates coord) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 407 | { |
Moritz Pflanzer | 219c691 | 2017-09-23 19:22:51 +0100 | [diff] [blame] | 408 | for(size_t d = 0; d < Coordinates::num_max_dimensions; ++d) |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 409 | { |
| 410 | 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] | 411 | { |
| 412 | return false; |
| 413 | } |
| 414 | } |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 415 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 416 | return true; |
| 417 | } |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 418 | |
| 419 | /** Create and initialize a tensor of the given type. |
| 420 | * |
Sang-Hoon Park | 5ff38da | 2021-03-02 09:41:13 +0000 | [diff] [blame] | 421 | * @param[in] info Tensor information to be used to create the tensor |
| 422 | * @param[in] ctx (Optional) Pointer to the runtime context. |
| 423 | * |
| 424 | * @return Initialized tensor of given type. |
| 425 | */ |
| 426 | template <typename T> |
| 427 | inline T create_tensor(const TensorInfo &info, IRuntimeContext *ctx = nullptr) |
| 428 | { |
| 429 | T tensor(ctx); |
| 430 | tensor.allocator()->init(info); |
| 431 | return tensor; |
| 432 | } |
| 433 | |
| 434 | /** Create and initialize a tensor of the given type. |
| 435 | * |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 436 | * @param[in] shape Tensor shape. |
| 437 | * @param[in] data_type Data type. |
| 438 | * @param[in] num_channels (Optional) Number of channels. |
| 439 | * @param[in] quantization_info (Optional) Quantization info for asymmetric quantized types. |
| 440 | * @param[in] data_layout (Optional) Data layout. Default is NCHW. |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 441 | * @param[in] ctx (Optional) Pointer to the runtime context. |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 442 | * |
| 443 | * @return Initialized tensor of given type. |
| 444 | */ |
| 445 | template <typename T> |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 446 | inline T create_tensor(const TensorShape &shape, DataType data_type, int num_channels = 1, |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 447 | QuantizationInfo quantization_info = QuantizationInfo(), DataLayout data_layout = DataLayout::NCHW, IRuntimeContext *ctx = nullptr) |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 448 | { |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 449 | T tensor(ctx); |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 450 | TensorInfo info(shape, num_channels, data_type); |
Chunosov | d621bca | 2017-11-03 17:33:15 +0700 | [diff] [blame] | 451 | info.set_quantization_info(quantization_info); |
Michalis Spyrou | cf581f5 | 2018-03-02 10:25:59 +0000 | [diff] [blame] | 452 | info.set_data_layout(data_layout); |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 453 | |
Sang-Hoon Park | 5ff38da | 2021-03-02 09:41:13 +0000 | [diff] [blame] | 454 | return create_tensor<T>(info, ctx); |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 455 | } |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 456 | |
Ioan-Cristian Szabo | 2c35018 | 2017-12-20 16:27:37 +0000 | [diff] [blame] | 457 | /** Create and initialize a tensor of the given type. |
| 458 | * |
| 459 | * @param[in] shape Tensor shape. |
| 460 | * @param[in] format Format type. |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 461 | * @param[in] ctx (Optional) Pointer to the runtime context. |
Ioan-Cristian Szabo | 2c35018 | 2017-12-20 16:27:37 +0000 | [diff] [blame] | 462 | * |
| 463 | * @return Initialized tensor of given type. |
| 464 | */ |
| 465 | template <typename T> |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 466 | inline T create_tensor(const TensorShape &shape, Format format, IRuntimeContext *ctx = nullptr) |
Ioan-Cristian Szabo | 2c35018 | 2017-12-20 16:27:37 +0000 | [diff] [blame] | 467 | { |
| 468 | TensorInfo info(shape, format); |
| 469 | |
Sang-Hoon Park | 5ff38da | 2021-03-02 09:41:13 +0000 | [diff] [blame] | 470 | return create_tensor<T>(info, ctx); |
Ioan-Cristian Szabo | 2c35018 | 2017-12-20 16:27:37 +0000 | [diff] [blame] | 471 | } |
| 472 | |
John Richardson | 684cb0f | 2018-01-09 11:17:00 +0000 | [diff] [blame] | 473 | /** Create a vector with a uniform distribution of floating point values across the specified range. |
| 474 | * |
| 475 | * @param[in] num_values The number of values to be created. |
| 476 | * @param[in] min The minimum value in distribution (inclusive). |
| 477 | * @param[in] max The maximum value in distribution (inclusive). |
| 478 | * @param[in] seed The random seed to be used. |
| 479 | * |
| 480 | * @return A vector that contains the requested number of random floating point values |
| 481 | */ |
| 482 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 483 | inline std::vector<T> generate_random_real(unsigned int num_values, T min, T max, std::random_device::result_type seed) |
| 484 | { |
| 485 | std::vector<T> v(num_values); |
| 486 | std::mt19937 gen(seed); |
| 487 | std::uniform_real_distribution<T> dist(min, max); |
| 488 | |
| 489 | for(unsigned int i = 0; i < num_values; ++i) |
| 490 | { |
| 491 | v.at(i) = dist(gen); |
| 492 | } |
| 493 | |
| 494 | return v; |
| 495 | } |
| 496 | |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 497 | template <typename T, typename ArrayAccessor_T> |
| 498 | inline void fill_array(ArrayAccessor_T &&array, const std::vector<T> &v) |
| 499 | { |
| 500 | array.resize(v.size()); |
| 501 | std::memcpy(array.buffer(), v.data(), v.size() * sizeof(T)); |
| 502 | } |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 503 | |
| 504 | /** Obtain numpy type string from DataType. |
| 505 | * |
| 506 | * @param[in] data_type Data type. |
| 507 | * |
| 508 | * @return numpy type string. |
| 509 | */ |
| 510 | inline std::string get_typestring(DataType data_type) |
| 511 | { |
| 512 | // Check endianness |
| 513 | const unsigned int i = 1; |
| 514 | const char *c = reinterpret_cast<const char *>(&i); |
| 515 | std::string endianness; |
| 516 | if(*c == 1) |
| 517 | { |
| 518 | endianness = std::string("<"); |
| 519 | } |
| 520 | else |
| 521 | { |
| 522 | endianness = std::string(">"); |
| 523 | } |
| 524 | const std::string no_endianness("|"); |
| 525 | |
| 526 | switch(data_type) |
| 527 | { |
| 528 | case DataType::U8: |
| 529 | return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t)); |
| 530 | case DataType::S8: |
| 531 | return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t)); |
| 532 | case DataType::U16: |
| 533 | return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t)); |
| 534 | case DataType::S16: |
| 535 | return endianness + "i" + support::cpp11::to_string(sizeof(int16_t)); |
| 536 | case DataType::U32: |
| 537 | return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t)); |
| 538 | case DataType::S32: |
| 539 | return endianness + "i" + support::cpp11::to_string(sizeof(int32_t)); |
| 540 | case DataType::U64: |
| 541 | return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t)); |
| 542 | case DataType::S64: |
| 543 | return endianness + "i" + support::cpp11::to_string(sizeof(int64_t)); |
| 544 | case DataType::F32: |
| 545 | return endianness + "f" + support::cpp11::to_string(sizeof(float)); |
| 546 | case DataType::F64: |
| 547 | return endianness + "f" + support::cpp11::to_string(sizeof(double)); |
| 548 | case DataType::SIZET: |
| 549 | return endianness + "u" + support::cpp11::to_string(sizeof(size_t)); |
| 550 | default: |
| 551 | ARM_COMPUTE_ERROR("NOT SUPPORTED!"); |
| 552 | } |
| 553 | } |
Joel Liang | 1c5ffd6 | 2017-12-28 10:09:51 +0800 | [diff] [blame] | 554 | |
| 555 | /** Sync if necessary. |
| 556 | */ |
| 557 | template <typename TensorType> |
| 558 | inline void sync_if_necessary() |
| 559 | { |
| 560 | #ifdef ARM_COMPUTE_CL |
| 561 | if(opencl_is_available() && std::is_same<typename std::decay<TensorType>::type, arm_compute::CLTensor>::value) |
| 562 | { |
| 563 | CLScheduler::get().sync(); |
| 564 | } |
| 565 | #endif /* ARM_COMPUTE_CL */ |
| 566 | } |
| 567 | |
| 568 | /** Sync tensor if necessary. |
| 569 | * |
| 570 | * @note: If the destination tensor not being used on OpenGL ES, GPU will optimize out the operation. |
| 571 | * |
| 572 | * @param[in] tensor Tensor to be sync. |
| 573 | */ |
| 574 | template <typename TensorType> |
| 575 | inline void sync_tensor_if_necessary(TensorType &tensor) |
| 576 | { |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 577 | ARM_COMPUTE_UNUSED(tensor); |
Joel Liang | 1c5ffd6 | 2017-12-28 10:09:51 +0800 | [diff] [blame] | 578 | } |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame] | 579 | |
| 580 | /** Construct and return object for dimensions' state filled with the given value |
| 581 | * |
| 582 | * @param[in] value The value to fill |
| 583 | * |
| 584 | * @return Constructed class |
| 585 | */ |
| 586 | inline ITensorInfo::TensorDimsState construct_dims_state(int32_t value) |
| 587 | { |
| 588 | auto states = ITensorInfo::TensorDimsState{}; |
| 589 | std::fill(states.begin(), states.end(), value); |
| 590 | return states; |
| 591 | } |
| 592 | |
| 593 | /** Construct and return object for dimensions' state filled with the value for dynamic state |
| 594 | * |
| 595 | * @return Constructed class filled with the value for dynamic state |
| 596 | */ |
| 597 | inline ITensorInfo::TensorDimsState construct_dynamic_dims_state() |
| 598 | { |
| 599 | return construct_dims_state(ITensorInfo::get_dynamic_state_value()); |
| 600 | } |
| 601 | |
| 602 | /** Construct and return object for dimensions' state filled with the value for non-dynamic state |
| 603 | * |
| 604 | * @return Constructed class filled with the value for non-dynamic state |
| 605 | */ |
| 606 | inline ITensorInfo::TensorDimsState construct_static_dims_state() |
| 607 | { |
| 608 | return construct_dims_state(ITensorInfo::get_static_state_value()); |
| 609 | } |
| 610 | |
| 611 | /** Set the dimension states of the given tensor to dynamic |
| 612 | * |
| 613 | * @param[in] t The tensor to set to dynamic state |
| 614 | * |
| 615 | */ |
| 616 | template <typename TensorType> |
| 617 | void set_tensor_dynamic(TensorType &t) |
| 618 | { |
| 619 | t.info()->set_tensor_dims_state(construct_dynamic_dims_state()); |
| 620 | } |
| 621 | |
| 622 | /** Set the dimension states of the given tensor to state |
| 623 | * |
| 624 | * @param[in] t The tensor to set to static state |
| 625 | * |
| 626 | */ |
| 627 | template <typename TensorType> |
| 628 | void set_tensor_static(TensorType &t) |
| 629 | { |
| 630 | t.info()->set_tensor_dims_state(construct_static_dims_state()); |
| 631 | } |
Ramy Elgammal | 73f19af | 2022-10-23 11:44:49 +0100 | [diff] [blame] | 632 | |
| 633 | inline experimental::dynamic_fusion::Conv2dAttributes convert_pad_stride_info_to_conv_attr(const PadStrideInfo &info, const Size2D &dialation) |
| 634 | { |
| 635 | const Padding2D info_pad(info.pad_left(), info.pad_right(), info.pad_top(), info.pad_bottom()); |
| 636 | const Size2D info_stride(info.stride().first, info.stride().second); |
| 637 | return arm_compute::experimental::dynamic_fusion::Conv2dAttributes().pad(info_pad).stride(info_stride).dilation(dialation); |
| 638 | } |
| 639 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 640 | } // namespace test |
| 641 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 642 | #endif /* ARM_COMPUTE_TEST_UTILS_H */ |