blob: 7d960dd08f81683440e93dcd475488137c730273 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Ioan-Cristian Szabo91d20d92017-10-27 17:35:40 +01002 * Copyright (c) 2017-2018 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 */
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 Richardson25f23682017-11-27 14:35:09 +000030#include "arm_compute/core/HOGInfo.h"
John Richardson8de92612018-02-22 14:09:31 +000031#include "arm_compute/core/PyramidInfo.h"
John Richardson25f23682017-11-27 14:35:09 +000032#include "arm_compute/core/Size2D.h"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010033#include "arm_compute/core/TensorInfo.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/core/TensorShape.h"
35#include "arm_compute/core/Types.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
58namespace arm_compute
59{
Joel Liang1c5ffd62017-12-28 10:09:51 +080060#ifdef ARM_COMPUTE_CL
61class CLTensor;
62#endif /* ARM_COMPUTE_CL */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063namespace test
64{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065/** 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 */
71template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
72inline 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 */
84template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
85inline 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 Pflanzerd0ae8b82017-06-29 14:51:57 +010096 return support::cpp11::copysign(ipart, value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097 }
98 // Else return the nearest even integer
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010099 return support::cpp11::copysign(std::ceil(ipart + 0.5f), value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100 }
101 // Otherwise use the usual round to closest
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100102 return support::cpp11::copysign(support::cpp11::round(positive_value), value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104
105namespace traits
106{
107// *INDENT-OFF*
108// clang-format off
Alex Gildayc357c472018-03-21 13:54:09 +0000109/** Promote a type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110template <typename T> struct promote { };
Alex Gildayc357c472018-03-21 13:54:09 +0000111/** Promote uint8_t to uint16_t */
112template <> struct promote<uint8_t> { using type = uint16_t; /**< Promoted type */ };
113/** Promote int8_t to int16_t */
114template <> struct promote<int8_t> { using type = int16_t; /**< Promoted type */ };
115/** Promote uint16_t to uint32_t */
116template <> struct promote<uint16_t> { using type = uint32_t; /**< Promoted type */ };
117/** Promote int16_t to int32_t */
118template <> struct promote<int16_t> { using type = int32_t; /**< Promoted type */ };
119/** Promote uint32_t to uint64_t */
120template <> struct promote<uint32_t> { using type = uint64_t; /**< Promoted type */ };
121/** Promote int32_t to int64_t */
122template <> struct promote<int32_t> { using type = int64_t; /**< Promoted type */ };
123/** Promote float to float */
124template <> struct promote<float> { using type = float; /**< Promoted type */ };
125/** Promote half to half */
126template <> struct promote<half> { using type = half; /**< Promoted type */ };
Pablo Tello383deec2017-06-23 10:40:05 +0100127
Alex Gildayc357c472018-03-21 13:54:09 +0000128/** Get promoted type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129template <typename T>
130using promote_t = typename promote<T>::type;
131
132template <typename T>
133using 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 +0100134
135template <typename T>
136using make_unsigned_conditional_t = typename std::conditional<std::is_integral<T>::value, std::make_unsigned<T>, std::common_type<T>>::type;
137
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138// 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 */
148inline 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 */
167inline 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 */
184template <typename F, typename T>
185inline T foldl(F &&, const T &value)
186{
187 return value;
188}
189
190/** Base case of foldl.
191 *
192 * @return func(value1, value2).
193 */
194template <typename F, typename T, typename U>
195inline 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 */
207template <typename F, typename I, typename T, typename... Vs>
208inline 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 Libacaf9a2017-06-19 13:41:45 +0100213/** Create a valid region based on tensor shape, border mode and border size
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100214 *
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000215 * @param[in] a_shape Shape used as size of the valid region.
SiCong Libacaf9a2017-06-19 13:41:45 +0100216 * @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 Barbier6ff3b192017-09-04 18:44:23 +0100218 *
SiCong Libacaf9a2017-06-19 13:41:45 +0100219 * @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 Barbier6ff3b192017-09-04 18:44:23 +0100221 */
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000222inline 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 +0100223{
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000224 ValidRegion valid_region{ Coordinates(), a_shape };
225
226 Coordinates &anchor = valid_region.anchor;
227 TensorShape &shape = valid_region.shape;
Moritz Pflanzera1848362017-08-25 12:30:03 +0100228
SiCong Libacaf9a2017-06-19 13:41:45 +0100229 if(border_undefined)
230 {
231 ARM_COMPUTE_ERROR_ON(shape.num_dimensions() < 2);
Moritz Pflanzera1848362017-08-25 12:30:03 +0100232
SiCong Libacaf9a2017-06-19 13:41:45 +0100233 anchor.set(0, border_size.left);
234 anchor.set(1, border_size.top);
Moritz Pflanzera1848362017-08-25 12:30:03 +0100235
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 Libacaf9a2017-06-19 13:41:45 +0100241 }
Moritz Pflanzera1848362017-08-25 12:30:03 +0100242
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000243 return valid_region;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100244}
245
Gian Marco37908d92017-11-07 14:38:22 +0000246/** 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 Recasbcbc9702017-12-18 11:28:27 +0000250 * @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 Marco37908d92017-11-07 14:38:22 +0000252 * @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 Recasbcbc9702017-12-18 11:28:27 +0000256inline 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 +0000257{
258 constexpr int border_size = 2;
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000259
260 ValidRegion valid_region{ Coordinates(), a_shape };
261
262 Coordinates &anchor = valid_region.anchor;
263 TensorShape &shape = valid_region.shape;
Gian Marco37908d92017-11-07 14:38:22 +0000264
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 Iodice2abb2162018-04-11 10:49:04 +0100268 shape.set(0, (a_shape[0] + 1) * 0.5f);
269 shape.set(1, (a_shape[1] + 1) * 0.5f);
Gian Marco37908d92017-11-07 14:38:22 +0000270
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 Recasbcbc9702017-12-18 11:28:27 +0000276 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 Marco37908d92017-11-07 14:38:22 +0000278
279 // For the new anchor point we can have 2 cases:
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000280 // 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 Marco37908d92017-11-07 14:38:22 +0000282 // 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 +0100283 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 Marco37908d92017-11-07 14:38:22 +0000285
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 Iodice2abb2162018-04-11 10:49:04 +0100292 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 Marco37908d92017-11-07 14:38:22 +0000294
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 Recasbcbc9702017-12-18 11:28:27 +0000299 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 Marco37908d92017-11-07 14:38:22 +0000301
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000302 shape.set(0, valid_shape_x);
303 shape.set(1, valid_shape_y);
Gian Marco37908d92017-11-07 14:38:22 +0000304 }
305
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000306 return valid_region;
Gian Marco37908d92017-11-07 14:38:22 +0000307}
308
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100309/** 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 */
317template <typename T>
318void store_value_with_data_type(void *ptr, T value, DataType data_type)
319{
320 switch(data_type)
321 {
322 case DataType::U8:
Chunosovd621bca2017-11-03 17:33:15 +0700323 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100324 *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 Spyrou0a8334c2017-06-14 18:00:05 +0100334 case DataType::QS16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100335 *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 Barbier6ff3b192017-09-04 18:44:23 +0100349 case DataType::F16:
Georgios Pinitas583137c2017-08-31 18:12:42 +0100350 *reinterpret_cast<half *>(ptr) = value;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100351 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100352 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 */
372template <typename U, typename T>
373T 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 */
388template <typename... T>
389struct common_promoted_signed_type
390{
Alex Gildayc357c472018-03-21 13:54:09 +0000391 /** 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 Barbier6ff3b192017-09-04 18:44:23 +0100396 using intermediate_type = typename traits::make_signed_conditional_t<promoted_type>::type;
397};
398
John Richardson3c5f9492017-10-04 15:27:37 +0100399/** Find the unsigned promoted common type.
400 */
401template <typename... T>
402struct common_promoted_unsigned_type
403{
Alex Gildayc357c472018-03-21 13:54:09 +0000404 /** 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 Richardson3c5f9492017-10-04 15:27:37 +0100409 using intermediate_type = typename traits::make_unsigned_conditional_t<promoted_type>::type;
410};
411
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100412/** 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 */
419inline 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 */
448inline 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 Pflanzera1848362017-08-25 12:30:03 +0100466inline bool is_in_valid_region(const ValidRegion &valid_region, Coordinates coord)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100467{
Moritz Pflanzer219c6912017-09-23 19:22:51 +0100468 for(size_t d = 0; d < Coordinates::num_max_dimensions; ++d)
Moritz Pflanzera1848362017-08-25 12:30:03 +0100469 {
470 if(coord[d] < valid_region.start(d) || coord[d] >= valid_region.end(d))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100471 {
472 return false;
473 }
474 }
Moritz Pflanzera1848362017-08-25 12:30:03 +0100475
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100476 return true;
477}
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100478
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.
Chunosovd621bca2017-11-03 17:33:15 +0700485 * @param[in] quantization_info (Optional) Quantization info for asymmetric quantized types.
Michalis Spyroucf581f52018-03-02 10:25:59 +0000486 * @param[in] data_layout (Optional) Data layout. Default is NCHW.
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100487 *
488 * @return Initialized tensor of given type.
489 */
490template <typename T>
Chunosovd621bca2017-11-03 17:33:15 +0700491inline T create_tensor(const TensorShape &shape, DataType data_type, int num_channels = 1,
Michalis Spyroucf581f52018-03-02 10:25:59 +0000492 int fixed_point_position = 0, QuantizationInfo quantization_info = QuantizationInfo(), DataLayout data_layout = DataLayout::NCHW)
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100493{
Chunosovd621bca2017-11-03 17:33:15 +0700494 T tensor;
495 TensorInfo info(shape, num_channels, data_type, fixed_point_position);
496 info.set_quantization_info(quantization_info);
Michalis Spyroucf581f52018-03-02 10:25:59 +0000497 info.set_data_layout(data_layout);
Chunosovd621bca2017-11-03 17:33:15 +0700498 tensor.allocator()->init(info);
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100499
500 return tensor;
501}
SiCong Li3e363692017-07-04 15:02:10 +0100502
Ioan-Cristian Szabo2c350182017-12-20 16:27:37 +0000503/** 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 */
510template <typename T>
511inline 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 Szabo9414f642017-10-27 17:35:40 +0100521/** 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 */
528template <typename T>
529inline 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 Richardson25f23682017-11-27 14:35:09 +0000537/** Create and initialize a HOG (Histogram of Oriented Gradients) of the given type.
538 *
John Richardson684cb0f2018-01-09 11:17:00 +0000539 * @param[in] hog_info HOGInfo object
John Richardson25f23682017-11-27 14:35:09 +0000540 *
541 * @return Initialized HOG of given type.
542 */
543template <typename T>
John Richardson684cb0f2018-01-09 11:17:00 +0000544inline T create_HOG(const HOGInfo &hog_info)
John Richardson25f23682017-11-27 14:35:09 +0000545{
John Richardson684cb0f2018-01-09 11:17:00 +0000546 T hog;
John Richardson25f23682017-11-27 14:35:09 +0000547 hog.init(hog_info);
548
549 return hog;
550}
551
John Richardson8de92612018-02-22 14:09:31 +0000552/** 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 */
558template <typename T>
559inline 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 Li3e363692017-07-04 15:02:10 +0100567/** 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 */
576inline 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 Richardson684cb0f2018-01-09 11:17:00 +0000613/** 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 */
622template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
623inline 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 Richardson8de92612018-02-22 14:09:31 +0000637/** 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 */
646inline 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 Li3e363692017-07-04 15:02:10 +0100675template <typename T, typename ArrayAccessor_T>
676inline 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 Li86b53332017-08-23 11:02:43 +0100681
682/** Obtain numpy type string from DataType.
683 *
684 * @param[in] data_type Data type.
685 *
686 * @return numpy type string.
687 */
688inline 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 Liang1c5ffd62017-12-28 10:09:51 +0800732
733/** Sync if necessary.
734 */
735template <typename TensorType>
736inline 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 */
752template <typename TensorType>
753inline 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 Barbier6ff3b192017-09-04 18:44:23 +0100765} // namespace test
766} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100767#endif /* __ARM_COMPUTE_TEST_UTILS_H__ */