blob: 81fd253cee161fd4eba8005f2b88e0a15f54a0cc [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_TEST_UTILS_H
25#define ARM_COMPUTE_TEST_UTILS_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
27#include "arm_compute/core/Coordinates.h"
28#include "arm_compute/core/Error.h"
John Richardson25f23682017-11-27 14:35:09 +000029#include "arm_compute/core/HOGInfo.h"
John Richardson8de92612018-02-22 14:09:31 +000030#include "arm_compute/core/PyramidInfo.h"
John Richardson25f23682017-11-27 14:35:09 +000031#include "arm_compute/core/Size2D.h"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010032#include "arm_compute/core/TensorInfo.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include "arm_compute/core/TensorShape.h"
34#include "arm_compute/core/Types.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000035#include "support/StringSupport.h"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010036#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
Joel Liang1c5ffd62017-12-28 10:09:51 +080038#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 Barbier6ff3b192017-09-04 18:44:23 +010048#include <cmath>
49#include <cstddef>
50#include <limits>
51#include <memory>
SiCong Li3e363692017-07-04 15:02:10 +010052#include <random>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053#include <sstream>
54#include <string>
55#include <type_traits>
SiCong Li3e363692017-07-04 15:02:10 +010056#include <vector>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057
Georgios Pinitas12833d02019-07-25 13:31:10 +010058#include "arm_compute/runtime/CPP/CPPScheduler.h"
59#include "arm_compute/runtime/RuntimeContext.h"
60
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061namespace arm_compute
62{
Joel Liang1c5ffd62017-12-28 10:09:51 +080063#ifdef ARM_COMPUTE_CL
64class CLTensor;
65#endif /* ARM_COMPUTE_CL */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066namespace test
67{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068/** Round floating-point value with half value rounding to positive infinity.
69 *
70 * @param[in] value floating-point value to be rounded.
71 *
72 * @return Floating-point value of rounded @p value.
73 */
74template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
75inline T round_half_up(T value)
76{
77 return std::floor(value + 0.5f);
78}
79
80/** Round floating-point value with half value rounding to nearest even.
81 *
82 * @param[in] value floating-point value to be rounded.
83 * @param[in] epsilon precision.
84 *
85 * @return Floating-point value of rounded @p value.
86 */
87template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
88inline T round_half_even(T value, T epsilon = std::numeric_limits<T>::epsilon())
89{
90 T positive_value = std::abs(value);
91 T ipart = 0;
92 std::modf(positive_value, &ipart);
93 // If 'value' is exactly halfway between two integers
94 if(std::abs(positive_value - (ipart + 0.5f)) < epsilon)
95 {
96 // If 'ipart' is even then return 'ipart'
97 if(std::fmod(ipart, 2.f) < epsilon)
98 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010099 return support::cpp11::copysign(ipart, value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100 }
101 // Else return the nearest even integer
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100102 return support::cpp11::copysign(std::ceil(ipart + 0.5f), value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103 }
104 // Otherwise use the usual round to closest
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100105 return support::cpp11::copysign(support::cpp11::round(positive_value), value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107
108namespace traits
109{
110// *INDENT-OFF*
111// clang-format off
Alex Gildayc357c472018-03-21 13:54:09 +0000112/** Promote a type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113template <typename T> struct promote { };
Alex Gildayc357c472018-03-21 13:54:09 +0000114/** Promote uint8_t to uint16_t */
115template <> struct promote<uint8_t> { using type = uint16_t; /**< Promoted type */ };
116/** Promote int8_t to int16_t */
117template <> struct promote<int8_t> { using type = int16_t; /**< Promoted type */ };
118/** Promote uint16_t to uint32_t */
119template <> struct promote<uint16_t> { using type = uint32_t; /**< Promoted type */ };
120/** Promote int16_t to int32_t */
121template <> struct promote<int16_t> { using type = int32_t; /**< Promoted type */ };
122/** Promote uint32_t to uint64_t */
123template <> struct promote<uint32_t> { using type = uint64_t; /**< Promoted type */ };
124/** Promote int32_t to int64_t */
125template <> struct promote<int32_t> { using type = int64_t; /**< Promoted type */ };
126/** Promote float to float */
127template <> struct promote<float> { using type = float; /**< Promoted type */ };
128/** Promote half to half */
129template <> struct promote<half> { using type = half; /**< Promoted type */ };
Pablo Tello383deec2017-06-23 10:40:05 +0100130
Alex Gildayc357c472018-03-21 13:54:09 +0000131/** Get promoted type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132template <typename T>
133using promote_t = typename promote<T>::type;
134
135template <typename T>
136using make_signed_conditional_t = typename std::conditional<std::is_integral<T>::value, std::make_signed<T>, std::common_type<T>>::type;
John Richardson3c5f9492017-10-04 15:27:37 +0100137
138template <typename T>
139using make_unsigned_conditional_t = typename std::conditional<std::is_integral<T>::value, std::make_unsigned<T>, std::common_type<T>>::type;
140
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141// clang-format on
142// *INDENT-ON*
143}
144
145/** Look up the format corresponding to a channel.
146 *
147 * @param[in] channel Channel type.
148 *
149 * @return Format that contains the given channel.
150 */
151inline Format get_format_for_channel(Channel channel)
152{
153 switch(channel)
154 {
155 case Channel::R:
156 case Channel::G:
157 case Channel::B:
158 return Format::RGB888;
159 default:
160 throw std::runtime_error("Unsupported channel");
161 }
162}
163
164/** Return the format of a channel.
165 *
166 * @param[in] channel Channel type.
167 *
168 * @return Format of the given channel.
169 */
170inline Format get_channel_format(Channel channel)
171{
172 switch(channel)
173 {
174 case Channel::R:
175 case Channel::G:
176 case Channel::B:
177 return Format::U8;
178 default:
179 throw std::runtime_error("Unsupported channel");
180 }
181}
182
183/** Base case of foldl.
184 *
185 * @return value.
186 */
187template <typename F, typename T>
188inline T foldl(F &&, const T &value)
189{
190 return value;
191}
192
193/** Base case of foldl.
194 *
195 * @return func(value1, value2).
196 */
197template <typename F, typename T, typename U>
198inline auto foldl(F &&func, T &&value1, U &&value2) -> decltype(func(value1, value2))
199{
200 return func(value1, value2);
201}
202
203/** Fold left.
204 *
205 * @param[in] func Binary function to be called.
206 * @param[in] initial Initial value.
207 * @param[in] value Argument passed to the function.
208 * @param[in] values Remaining arguments.
209 */
210template <typename F, typename I, typename T, typename... Vs>
211inline I foldl(F &&func, I &&initial, T &&value, Vs &&... values)
212{
213 return foldl(std::forward<F>(func), func(std::forward<I>(initial), std::forward<T>(value)), std::forward<Vs>(values)...);
214}
215
SiCong Libacaf9a2017-06-19 13:41:45 +0100216/** Create a valid region based on tensor shape, border mode and border size
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100217 *
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000218 * @param[in] a_shape Shape used as size of the valid region.
SiCong Libacaf9a2017-06-19 13:41:45 +0100219 * @param[in] border_undefined (Optional) Boolean indicating if the border mode is undefined.
220 * @param[in] border_size (Optional) Border size used to specify the region to exclude.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221 *
SiCong Libacaf9a2017-06-19 13:41:45 +0100222 * @return A valid region starting at (0, 0, ...) with size of @p shape if @p border_undefined is false; otherwise
223 * return A valid region starting at (@p border_size.left, @p border_size.top, ...) with reduced size of @p shape.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100224 */
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000225inline ValidRegion shape_to_valid_region(const TensorShape &a_shape, bool border_undefined = false, BorderSize border_size = BorderSize(0))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100226{
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000227 ValidRegion valid_region{ Coordinates(), a_shape };
228
229 Coordinates &anchor = valid_region.anchor;
230 TensorShape &shape = valid_region.shape;
Moritz Pflanzera1848362017-08-25 12:30:03 +0100231
SiCong Libacaf9a2017-06-19 13:41:45 +0100232 if(border_undefined)
233 {
234 ARM_COMPUTE_ERROR_ON(shape.num_dimensions() < 2);
Moritz Pflanzera1848362017-08-25 12:30:03 +0100235
SiCong Libacaf9a2017-06-19 13:41:45 +0100236 anchor.set(0, border_size.left);
237 anchor.set(1, border_size.top);
Moritz Pflanzera1848362017-08-25 12:30:03 +0100238
239 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));
240 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));
241
242 shape.set(0, valid_shape_x);
243 shape.set(1, valid_shape_y);
SiCong Libacaf9a2017-06-19 13:41:45 +0100244 }
Moritz Pflanzera1848362017-08-25 12:30:03 +0100245
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000246 return valid_region;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100247}
248
Gian Marco37908d92017-11-07 14:38:22 +0000249/** Create a valid region for Gaussian Pyramid Half based on tensor shape and valid region at level "i - 1" and border mode
250 *
251 * @note The border size is 2 in case of Gaussian Pyramid Half
252 *
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000253 * @param[in] a_shape Shape used at level "i - 1" of Gaussian Pyramid Half
254 * @param[in] a_valid_region Valid region used at level "i - 1" of Gaussian Pyramid Half
Gian Marco37908d92017-11-07 14:38:22 +0000255 * @param[in] border_undefined (Optional) Boolean indicating if the border mode is undefined.
256 *
257 * return The valid region for the level "i" of Gaussian Pyramid Half
258 */
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000259inline ValidRegion shape_to_valid_region_gaussian_pyramid_half(const TensorShape &a_shape, const ValidRegion &a_valid_region, bool border_undefined = false)
Gian Marco37908d92017-11-07 14:38:22 +0000260{
261 constexpr int border_size = 2;
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000262
263 ValidRegion valid_region{ Coordinates(), a_shape };
264
265 Coordinates &anchor = valid_region.anchor;
266 TensorShape &shape = valid_region.shape;
Gian Marco37908d92017-11-07 14:38:22 +0000267
268 // Compute tensor shape for level "i" of Gaussian Pyramid Half
269 // dst_width = (src_width + 1) * 0.5f
270 // dst_height = (src_height + 1) * 0.5f
Gian Marco Iodice2abb2162018-04-11 10:49:04 +0100271 shape.set(0, (a_shape[0] + 1) * 0.5f);
272 shape.set(1, (a_shape[1] + 1) * 0.5f);
Gian Marco37908d92017-11-07 14:38:22 +0000273
274 if(border_undefined)
275 {
276 ARM_COMPUTE_ERROR_ON(shape.num_dimensions() < 2);
277
278 // Compute the left and top invalid borders
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000279 float invalid_border_left = static_cast<float>(a_valid_region.anchor.x() + border_size) / 2.0f;
280 float invalid_border_top = static_cast<float>(a_valid_region.anchor.y() + border_size) / 2.0f;
Gian Marco37908d92017-11-07 14:38:22 +0000281
282 // For the new anchor point we can have 2 cases:
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000283 // 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
284 // 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 Marco37908d92017-11-07 14:38:22 +0000285 // In this manner we should be able to propagate correctly the valid region along all levels of the pyramid
Gian Marco Iodice2abb2162018-04-11 10:49:04 +0100286 invalid_border_left = (a_shape[0] % 2) ? std::ceil(invalid_border_left) : std::floor(invalid_border_left);
287 invalid_border_top = (a_shape[1] % 2) ? std::ceil(invalid_border_top) : std::floor(invalid_border_top);
Gian Marco37908d92017-11-07 14:38:22 +0000288
289 // Set the anchor point
290 anchor.set(0, static_cast<int>(invalid_border_left));
291 anchor.set(1, static_cast<int>(invalid_border_top));
292
293 // Compute shape
294 // Calculate the right and bottom invalid borders at the previous level of the pyramid
Gian Marco Iodice2abb2162018-04-11 10:49:04 +0100295 const float prev_invalid_border_right = static_cast<float>(a_shape[0] - (a_valid_region.anchor.x() + a_valid_region.shape[0]));
296 const float prev_invalid_border_bottom = static_cast<float>(a_shape[1] - (a_valid_region.anchor.y() + a_valid_region.shape[1]));
Gian Marco37908d92017-11-07 14:38:22 +0000297
298 // Calculate the right and bottom invalid borders at the current level of the pyramid
299 const float invalid_border_right = std::ceil((prev_invalid_border_right + static_cast<float>(border_size)) / 2.0f);
300 const float invalid_border_bottom = std::ceil((prev_invalid_border_bottom + static_cast<float>(border_size)) / 2.0f);
301
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000302 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));
303 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 Marco37908d92017-11-07 14:38:22 +0000304
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000305 shape.set(0, valid_shape_x);
306 shape.set(1, valid_shape_y);
Gian Marco37908d92017-11-07 14:38:22 +0000307 }
308
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000309 return valid_region;
Gian Marco37908d92017-11-07 14:38:22 +0000310}
311
John Richardson2d008a42018-03-22 13:48:41 +0000312/** Create a valid region for Laplacian Pyramid based on tensor shape and valid region at level "i - 1" and border mode
313 *
314 * @note The border size is 2 in case of Laplacian Pyramid
315 *
316 * @param[in] a_shape Shape used at level "i - 1" of Laplacian Pyramid
317 * @param[in] a_valid_region Valid region used at level "i - 1" of Laplacian Pyramid
318 * @param[in] border_undefined (Optional) Boolean indicating if the border mode is undefined.
319 *
320 * return The valid region for the level "i" of Laplacian Pyramid
321 */
322inline ValidRegion shape_to_valid_region_laplacian_pyramid(const TensorShape &a_shape, const ValidRegion &a_valid_region, bool border_undefined = false)
323{
324 ValidRegion valid_region = shape_to_valid_region_gaussian_pyramid_half(a_shape, a_valid_region, border_undefined);
325
326 if(border_undefined)
327 {
328 const BorderSize gaussian5x5_border(2);
329
330 auto border_left = static_cast<int>(gaussian5x5_border.left);
331 auto border_right = static_cast<int>(gaussian5x5_border.right);
332 auto border_top = static_cast<int>(gaussian5x5_border.top);
333 auto border_bottom = static_cast<int>(gaussian5x5_border.bottom);
334
335 valid_region.anchor.set(0, valid_region.anchor[0] + border_left);
336 valid_region.anchor.set(1, valid_region.anchor[1] + border_top);
337 valid_region.shape.set(0, std::max(0, static_cast<int>(valid_region.shape[0]) - border_right - border_left));
338 valid_region.shape.set(1, std::max(0, static_cast<int>(valid_region.shape[1]) - border_top - border_bottom));
339 }
340
341 return valid_region;
342}
343
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100344/** Write the value after casting the pointer according to @p data_type.
345 *
346 * @warning The type of the value must match the specified data type.
347 *
348 * @param[out] ptr Pointer to memory where the @p value will be written.
349 * @param[in] value Value that will be written.
350 * @param[in] data_type Data type that will be written.
351 */
352template <typename T>
353void store_value_with_data_type(void *ptr, T value, DataType data_type)
354{
355 switch(data_type)
356 {
357 case DataType::U8:
Chunosovd621bca2017-11-03 17:33:15 +0700358 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100359 *reinterpret_cast<uint8_t *>(ptr) = value;
360 break;
361 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100362 case DataType::QASYMM8_SIGNED:
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100363 case DataType::QSYMM8:
364 case DataType::QSYMM8_PER_CHANNEL:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100365 *reinterpret_cast<int8_t *>(ptr) = value;
366 break;
367 case DataType::U16:
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +0100368 case DataType::QASYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100369 *reinterpret_cast<uint16_t *>(ptr) = value;
370 break;
371 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100372 case DataType::QSYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100373 *reinterpret_cast<int16_t *>(ptr) = value;
374 break;
375 case DataType::U32:
376 *reinterpret_cast<uint32_t *>(ptr) = value;
377 break;
378 case DataType::S32:
379 *reinterpret_cast<int32_t *>(ptr) = value;
380 break;
381 case DataType::U64:
382 *reinterpret_cast<uint64_t *>(ptr) = value;
383 break;
384 case DataType::S64:
385 *reinterpret_cast<int64_t *>(ptr) = value;
386 break;
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000387 case DataType::BFLOAT16:
388 *reinterpret_cast<bfloat16 *>(ptr) = bfloat16(value);
389 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100390 case DataType::F16:
Georgios Pinitas583137c2017-08-31 18:12:42 +0100391 *reinterpret_cast<half *>(ptr) = value;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100392 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100393 case DataType::F32:
394 *reinterpret_cast<float *>(ptr) = value;
395 break;
396 case DataType::F64:
397 *reinterpret_cast<double *>(ptr) = value;
398 break;
399 case DataType::SIZET:
400 *reinterpret_cast<size_t *>(ptr) = value;
401 break;
402 default:
403 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
404 }
405}
406
407/** Saturate a value of type T against the numeric limits of type U.
408 *
409 * @param[in] val Value to be saturated.
410 *
411 * @return saturated value.
412 */
413template <typename U, typename T>
414T saturate_cast(T val)
415{
416 if(val > static_cast<T>(std::numeric_limits<U>::max()))
417 {
418 val = static_cast<T>(std::numeric_limits<U>::max());
419 }
420 if(val < static_cast<T>(std::numeric_limits<U>::lowest()))
421 {
422 val = static_cast<T>(std::numeric_limits<U>::lowest());
423 }
424 return val;
425}
426
427/** Find the signed promoted common type.
428 */
429template <typename... T>
430struct common_promoted_signed_type
431{
Alex Gildayc357c472018-03-21 13:54:09 +0000432 /** Common type */
433 using common_type = typename std::common_type<T...>::type;
434 /** Promoted type */
435 using promoted_type = traits::promote_t<common_type>;
436 /** Intermediate type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100437 using intermediate_type = typename traits::make_signed_conditional_t<promoted_type>::type;
438};
439
John Richardson3c5f9492017-10-04 15:27:37 +0100440/** Find the unsigned promoted common type.
441 */
442template <typename... T>
443struct common_promoted_unsigned_type
444{
Alex Gildayc357c472018-03-21 13:54:09 +0000445 /** Common type */
446 using common_type = typename std::common_type<T...>::type;
447 /** Promoted type */
448 using promoted_type = traits::promote_t<common_type>;
449 /** Intermediate type */
John Richardson3c5f9492017-10-04 15:27:37 +0100450 using intermediate_type = typename traits::make_unsigned_conditional_t<promoted_type>::type;
451};
452
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100453/** Convert a linear index into n-dimensional coordinates.
454 *
455 * @param[in] shape Shape of the n-dimensional tensor.
456 * @param[in] index Linear index specifying the i-th element.
457 *
458 * @return n-dimensional coordinates.
459 */
460inline Coordinates index2coord(const TensorShape &shape, int index)
461{
462 int num_elements = shape.total_size();
463
464 ARM_COMPUTE_ERROR_ON_MSG(index < 0 || index >= num_elements, "Index has to be in [0, num_elements]");
465 ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create coordinate from empty shape");
466
467 Coordinates coord{ 0 };
468
469 for(int d = shape.num_dimensions() - 1; d >= 0; --d)
470 {
471 num_elements /= shape[d];
472 coord.set(d, index / num_elements);
473 index %= num_elements;
474 }
475
476 return coord;
477}
478
479/** Linearise the given coordinate.
480 *
481 * Transforms the given coordinate into a linear offset in terms of
482 * elements.
483 *
484 * @param[in] shape Shape of the n-dimensional tensor.
485 * @param[in] coord The to be converted coordinate.
486 *
487 * @return Linear offset to the element.
488 */
489inline int coord2index(const TensorShape &shape, const Coordinates &coord)
490{
491 ARM_COMPUTE_ERROR_ON_MSG(shape.total_size() == 0, "Cannot get index from empty shape");
492 ARM_COMPUTE_ERROR_ON_MSG(coord.num_dimensions() == 0, "Cannot get index of empty coordinate");
493
494 int index = 0;
495 int dim_size = 1;
496
497 for(unsigned int i = 0; i < coord.num_dimensions(); ++i)
498 {
499 index += coord[i] * dim_size;
500 dim_size *= shape[i];
501 }
502
503 return index;
504}
505
506/** Check if a coordinate is within a valid region */
Moritz Pflanzera1848362017-08-25 12:30:03 +0100507inline bool is_in_valid_region(const ValidRegion &valid_region, Coordinates coord)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100508{
Moritz Pflanzer219c6912017-09-23 19:22:51 +0100509 for(size_t d = 0; d < Coordinates::num_max_dimensions; ++d)
Moritz Pflanzera1848362017-08-25 12:30:03 +0100510 {
511 if(coord[d] < valid_region.start(d) || coord[d] >= valid_region.end(d))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100512 {
513 return false;
514 }
515 }
Moritz Pflanzera1848362017-08-25 12:30:03 +0100516
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100517 return true;
518}
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100519
520/** Create and initialize a tensor of the given type.
521 *
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100522 * @param[in] shape Tensor shape.
523 * @param[in] data_type Data type.
524 * @param[in] num_channels (Optional) Number of channels.
525 * @param[in] quantization_info (Optional) Quantization info for asymmetric quantized types.
526 * @param[in] data_layout (Optional) Data layout. Default is NCHW.
Pablo Tellodb8485a2019-09-24 11:03:47 +0100527 * @param[in] ctx (Optional) Pointer to the runtime context.
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100528 *
529 * @return Initialized tensor of given type.
530 */
531template <typename T>
Chunosovd621bca2017-11-03 17:33:15 +0700532inline T create_tensor(const TensorShape &shape, DataType data_type, int num_channels = 1,
Pablo Tellodb8485a2019-09-24 11:03:47 +0100533 QuantizationInfo quantization_info = QuantizationInfo(), DataLayout data_layout = DataLayout::NCHW, IRuntimeContext *ctx = nullptr)
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100534{
Pablo Tellodb8485a2019-09-24 11:03:47 +0100535 T tensor(ctx);
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100536 TensorInfo info(shape, num_channels, data_type);
Chunosovd621bca2017-11-03 17:33:15 +0700537 info.set_quantization_info(quantization_info);
Michalis Spyroucf581f52018-03-02 10:25:59 +0000538 info.set_data_layout(data_layout);
Chunosovd621bca2017-11-03 17:33:15 +0700539 tensor.allocator()->init(info);
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100540
541 return tensor;
542}
SiCong Li3e363692017-07-04 15:02:10 +0100543
Ioan-Cristian Szabo2c350182017-12-20 16:27:37 +0000544/** Create and initialize a tensor of the given type.
545 *
546 * @param[in] shape Tensor shape.
547 * @param[in] format Format type.
Pablo Tellodb8485a2019-09-24 11:03:47 +0100548 * @param[in] ctx (Optional) Pointer to the runtime context.
Ioan-Cristian Szabo2c350182017-12-20 16:27:37 +0000549 *
550 * @return Initialized tensor of given type.
551 */
552template <typename T>
Pablo Tellodb8485a2019-09-24 11:03:47 +0100553inline T create_tensor(const TensorShape &shape, Format format, IRuntimeContext *ctx = nullptr)
Ioan-Cristian Szabo2c350182017-12-20 16:27:37 +0000554{
555 TensorInfo info(shape, format);
556
Pablo Tellodb8485a2019-09-24 11:03:47 +0100557 T tensor(ctx);
Ioan-Cristian Szabo2c350182017-12-20 16:27:37 +0000558 tensor.allocator()->init(info);
559
560 return tensor;
561}
562
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100563/** Create and initialize a multi-image of the given type.
564 *
565 * @param[in] shape Tensor shape.
566 * @param[in] format Format type.
567 *
568 * @return Initialized tensor of given type.
569 */
570template <typename T>
571inline T create_multi_image(const TensorShape &shape, Format format)
572{
573 T multi_image;
574 multi_image.init(shape.x(), shape.y(), format);
575
576 return multi_image;
577}
578
John Richardson25f23682017-11-27 14:35:09 +0000579/** Create and initialize a HOG (Histogram of Oriented Gradients) of the given type.
580 *
John Richardson684cb0f2018-01-09 11:17:00 +0000581 * @param[in] hog_info HOGInfo object
John Richardson25f23682017-11-27 14:35:09 +0000582 *
583 * @return Initialized HOG of given type.
584 */
585template <typename T>
John Richardson684cb0f2018-01-09 11:17:00 +0000586inline T create_HOG(const HOGInfo &hog_info)
John Richardson25f23682017-11-27 14:35:09 +0000587{
John Richardson684cb0f2018-01-09 11:17:00 +0000588 T hog;
John Richardson25f23682017-11-27 14:35:09 +0000589 hog.init(hog_info);
590
591 return hog;
592}
593
John Richardson8de92612018-02-22 14:09:31 +0000594/** Create and initialize a Pyramid of the given type.
595 *
596 * @param[in] pyramid_info The PyramidInfo object.
597 *
598 * @return Initialized Pyramid of given type.
599 */
600template <typename T>
601inline T create_pyramid(const PyramidInfo &pyramid_info)
602{
603 T pyramid;
604 pyramid.init_auto_padding(pyramid_info);
605
606 return pyramid;
607}
608
John Richardson32af1f82018-06-05 12:47:20 +0100609/** Initialize a convolution matrix.
610 *
611 * @param[in, out] conv The input convolution matrix.
612 * @param[in] width The width of the convolution matrix.
613 * @param[in] height The height of the convolution matrix.
614 * @param[in] seed The random seed to be used.
615 */
616inline void init_conv(int16_t *conv, unsigned int width, unsigned int height, std::random_device::result_type seed)
617{
618 std::mt19937 gen(seed);
619 std::uniform_int_distribution<int16_t> distribution_int16(-32768, 32767);
620
621 for(unsigned int i = 0; i < width * height; ++i)
622 {
623 conv[i] = distribution_int16(gen);
624 }
625}
626
627/** Initialize a separable convolution matrix.
628 *
629 * @param[in, out] conv The input convolution matrix.
630 * @param[in] width The width of the convolution matrix.
631 * @param[in] height The height of the convolution matrix.
632 * @param[in] seed The random seed to be used.
633 */
634inline void init_separable_conv(int16_t *conv, unsigned int width, unsigned int height, std::random_device::result_type seed)
635{
636 std::mt19937 gen(seed);
637 // Set it between -128 and 127 to ensure the matrix does not overflow
638 std::uniform_int_distribution<int16_t> distribution_int16(-128, 127);
639
Michalis Spyroufae513c2019-10-16 17:41:33 +0100640 int16_t *conv_row = new int16_t[width];
641 int16_t *conv_col = new int16_t[height];
John Richardson32af1f82018-06-05 12:47:20 +0100642
643 conv_row[0] = conv_col[0] = 1;
644 for(unsigned int i = 1; i < width; ++i)
645 {
646 conv_row[i] = distribution_int16(gen);
647 }
648
649 for(unsigned int i = 1; i < height; ++i)
650 {
651 conv_col[i] = distribution_int16(gen);
652 }
653
654 // Multiply two matrices
655 for(unsigned int i = 0; i < width; ++i)
656 {
657 for(unsigned int j = 0; j < height; ++j)
658 {
659 conv[i * width + j] = conv_col[i] * conv_row[j];
660 }
661 }
Michalis Spyroufae513c2019-10-16 17:41:33 +0100662
663 delete[] conv_row;
664 delete[] conv_col;
John Richardson32af1f82018-06-05 12:47:20 +0100665}
666
John Richardson684cb0f2018-01-09 11:17:00 +0000667/** Create a vector with a uniform distribution of floating point values across the specified range.
668 *
669 * @param[in] num_values The number of values to be created.
670 * @param[in] min The minimum value in distribution (inclusive).
671 * @param[in] max The maximum value in distribution (inclusive).
672 * @param[in] seed The random seed to be used.
673 *
674 * @return A vector that contains the requested number of random floating point values
675 */
676template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
677inline std::vector<T> generate_random_real(unsigned int num_values, T min, T max, std::random_device::result_type seed)
678{
679 std::vector<T> v(num_values);
680 std::mt19937 gen(seed);
681 std::uniform_real_distribution<T> dist(min, max);
682
683 for(unsigned int i = 0; i < num_values; ++i)
684 {
685 v.at(i) = dist(gen);
686 }
687
688 return v;
689}
690
John Richardson8de92612018-02-22 14:09:31 +0000691/** Create a vector of random keypoints for pyramid representation.
692 *
693 * @param[in] shape The shape of the input tensor.
694 * @param[in] num_keypoints The number of keypoints to be created.
695 * @param[in] seed The random seed to be used.
696 * @param[in] num_levels The number of pyramid levels.
697 *
698 * @return A vector that contains the requested number of random keypoints
699 */
700inline std::vector<KeyPoint> generate_random_keypoints(const TensorShape &shape, size_t num_keypoints, std::random_device::result_type seed, size_t num_levels = 1)
701{
702 std::vector<KeyPoint> keypoints;
703 std::mt19937 gen(seed);
704
705 // Calculate distribution bounds
706 const auto min = static_cast<int>(std::pow(2, num_levels));
707 const auto max_width = static_cast<int>(shape.x());
708 const auto max_height = static_cast<int>(shape.y());
709
710 ARM_COMPUTE_ERROR_ON(min > max_width || min > max_height);
711
712 // Create distributions
713 std::uniform_int_distribution<> dist_w(min, max_width);
714 std::uniform_int_distribution<> dist_h(min, max_height);
715
716 for(unsigned int i = 0; i < num_keypoints; i++)
717 {
718 KeyPoint keypoint;
719 keypoint.x = dist_w(gen);
720 keypoint.y = dist_h(gen);
721 keypoint.tracking_status = 1;
722
723 keypoints.push_back(keypoint);
724 }
725
726 return keypoints;
727}
728
SiCong Li3e363692017-07-04 15:02:10 +0100729template <typename T, typename ArrayAccessor_T>
730inline void fill_array(ArrayAccessor_T &&array, const std::vector<T> &v)
731{
732 array.resize(v.size());
733 std::memcpy(array.buffer(), v.data(), v.size() * sizeof(T));
734}
SiCong Li86b53332017-08-23 11:02:43 +0100735
736/** Obtain numpy type string from DataType.
737 *
738 * @param[in] data_type Data type.
739 *
740 * @return numpy type string.
741 */
742inline std::string get_typestring(DataType data_type)
743{
744 // Check endianness
745 const unsigned int i = 1;
746 const char *c = reinterpret_cast<const char *>(&i);
747 std::string endianness;
748 if(*c == 1)
749 {
750 endianness = std::string("<");
751 }
752 else
753 {
754 endianness = std::string(">");
755 }
756 const std::string no_endianness("|");
757
758 switch(data_type)
759 {
760 case DataType::U8:
761 return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t));
762 case DataType::S8:
763 return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t));
764 case DataType::U16:
765 return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t));
766 case DataType::S16:
767 return endianness + "i" + support::cpp11::to_string(sizeof(int16_t));
768 case DataType::U32:
769 return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t));
770 case DataType::S32:
771 return endianness + "i" + support::cpp11::to_string(sizeof(int32_t));
772 case DataType::U64:
773 return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t));
774 case DataType::S64:
775 return endianness + "i" + support::cpp11::to_string(sizeof(int64_t));
776 case DataType::F32:
777 return endianness + "f" + support::cpp11::to_string(sizeof(float));
778 case DataType::F64:
779 return endianness + "f" + support::cpp11::to_string(sizeof(double));
780 case DataType::SIZET:
781 return endianness + "u" + support::cpp11::to_string(sizeof(size_t));
782 default:
783 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
784 }
785}
Joel Liang1c5ffd62017-12-28 10:09:51 +0800786
787/** Sync if necessary.
788 */
789template <typename TensorType>
790inline void sync_if_necessary()
791{
792#ifdef ARM_COMPUTE_CL
793 if(opencl_is_available() && std::is_same<typename std::decay<TensorType>::type, arm_compute::CLTensor>::value)
794 {
795 CLScheduler::get().sync();
796 }
797#endif /* ARM_COMPUTE_CL */
798}
799
800/** Sync tensor if necessary.
801 *
802 * @note: If the destination tensor not being used on OpenGL ES, GPU will optimize out the operation.
803 *
804 * @param[in] tensor Tensor to be sync.
805 */
806template <typename TensorType>
807inline void sync_tensor_if_necessary(TensorType &tensor)
808{
809#ifdef ARM_COMPUTE_GC
810 if(opengles31_is_available() && std::is_same<typename std::decay<TensorType>::type, arm_compute::GCTensor>::value)
811 {
812 // Force sync the tensor by calling map and unmap.
813 IGCTensor &t = dynamic_cast<IGCTensor &>(tensor);
814 t.map();
815 t.unmap();
816 }
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100817#else /* ARM_COMPUTE_GC */
818 ARM_COMPUTE_UNUSED(tensor);
Joel Liang1c5ffd62017-12-28 10:09:51 +0800819#endif /* ARM_COMPUTE_GC */
820}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100821} // namespace test
822} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000823#endif /* ARM_COMPUTE_TEST_UTILS_H */