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