blob: 332c91649870a5a0954a8cae010c6fa388d754f5 [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
John Richardson2d008a42018-03-22 13:48:41 +0000309/** Create a valid region for Laplacian Pyramid based on tensor shape and valid region at level "i - 1" and border mode
310 *
311 * @note The border size is 2 in case of Laplacian Pyramid
312 *
313 * @param[in] a_shape Shape used at level "i - 1" of Laplacian Pyramid
314 * @param[in] a_valid_region Valid region used at level "i - 1" of Laplacian Pyramid
315 * @param[in] border_undefined (Optional) Boolean indicating if the border mode is undefined.
316 *
317 * return The valid region for the level "i" of Laplacian Pyramid
318 */
319inline ValidRegion shape_to_valid_region_laplacian_pyramid(const TensorShape &a_shape, const ValidRegion &a_valid_region, bool border_undefined = false)
320{
321 ValidRegion valid_region = shape_to_valid_region_gaussian_pyramid_half(a_shape, a_valid_region, border_undefined);
322
323 if(border_undefined)
324 {
325 const BorderSize gaussian5x5_border(2);
326
327 auto border_left = static_cast<int>(gaussian5x5_border.left);
328 auto border_right = static_cast<int>(gaussian5x5_border.right);
329 auto border_top = static_cast<int>(gaussian5x5_border.top);
330 auto border_bottom = static_cast<int>(gaussian5x5_border.bottom);
331
332 valid_region.anchor.set(0, valid_region.anchor[0] + border_left);
333 valid_region.anchor.set(1, valid_region.anchor[1] + border_top);
334 valid_region.shape.set(0, std::max(0, static_cast<int>(valid_region.shape[0]) - border_right - border_left));
335 valid_region.shape.set(1, std::max(0, static_cast<int>(valid_region.shape[1]) - border_top - border_bottom));
336 }
337
338 return valid_region;
339}
340
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100341/** Write the value after casting the pointer according to @p data_type.
342 *
343 * @warning The type of the value must match the specified data type.
344 *
345 * @param[out] ptr Pointer to memory where the @p value will be written.
346 * @param[in] value Value that will be written.
347 * @param[in] data_type Data type that will be written.
348 */
349template <typename T>
350void store_value_with_data_type(void *ptr, T value, DataType data_type)
351{
352 switch(data_type)
353 {
354 case DataType::U8:
Chunosovd621bca2017-11-03 17:33:15 +0700355 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100356 *reinterpret_cast<uint8_t *>(ptr) = value;
357 break;
358 case DataType::S8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100359 *reinterpret_cast<int8_t *>(ptr) = value;
360 break;
361 case DataType::U16:
362 *reinterpret_cast<uint16_t *>(ptr) = value;
363 break;
364 case DataType::S16:
365 *reinterpret_cast<int16_t *>(ptr) = value;
366 break;
367 case DataType::U32:
368 *reinterpret_cast<uint32_t *>(ptr) = value;
369 break;
370 case DataType::S32:
371 *reinterpret_cast<int32_t *>(ptr) = value;
372 break;
373 case DataType::U64:
374 *reinterpret_cast<uint64_t *>(ptr) = value;
375 break;
376 case DataType::S64:
377 *reinterpret_cast<int64_t *>(ptr) = value;
378 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100379 case DataType::F16:
Georgios Pinitas583137c2017-08-31 18:12:42 +0100380 *reinterpret_cast<half *>(ptr) = value;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100381 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100382 case DataType::F32:
383 *reinterpret_cast<float *>(ptr) = value;
384 break;
385 case DataType::F64:
386 *reinterpret_cast<double *>(ptr) = value;
387 break;
388 case DataType::SIZET:
389 *reinterpret_cast<size_t *>(ptr) = value;
390 break;
391 default:
392 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
393 }
394}
395
396/** Saturate a value of type T against the numeric limits of type U.
397 *
398 * @param[in] val Value to be saturated.
399 *
400 * @return saturated value.
401 */
402template <typename U, typename T>
403T saturate_cast(T val)
404{
405 if(val > static_cast<T>(std::numeric_limits<U>::max()))
406 {
407 val = static_cast<T>(std::numeric_limits<U>::max());
408 }
409 if(val < static_cast<T>(std::numeric_limits<U>::lowest()))
410 {
411 val = static_cast<T>(std::numeric_limits<U>::lowest());
412 }
413 return val;
414}
415
416/** Find the signed promoted common type.
417 */
418template <typename... T>
419struct common_promoted_signed_type
420{
Alex Gildayc357c472018-03-21 13:54:09 +0000421 /** Common type */
422 using common_type = typename std::common_type<T...>::type;
423 /** Promoted type */
424 using promoted_type = traits::promote_t<common_type>;
425 /** Intermediate type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100426 using intermediate_type = typename traits::make_signed_conditional_t<promoted_type>::type;
427};
428
John Richardson3c5f9492017-10-04 15:27:37 +0100429/** Find the unsigned promoted common type.
430 */
431template <typename... T>
432struct common_promoted_unsigned_type
433{
Alex Gildayc357c472018-03-21 13:54:09 +0000434 /** Common type */
435 using common_type = typename std::common_type<T...>::type;
436 /** Promoted type */
437 using promoted_type = traits::promote_t<common_type>;
438 /** Intermediate type */
John Richardson3c5f9492017-10-04 15:27:37 +0100439 using intermediate_type = typename traits::make_unsigned_conditional_t<promoted_type>::type;
440};
441
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100442/** Convert a linear index into n-dimensional coordinates.
443 *
444 * @param[in] shape Shape of the n-dimensional tensor.
445 * @param[in] index Linear index specifying the i-th element.
446 *
447 * @return n-dimensional coordinates.
448 */
449inline Coordinates index2coord(const TensorShape &shape, int index)
450{
451 int num_elements = shape.total_size();
452
453 ARM_COMPUTE_ERROR_ON_MSG(index < 0 || index >= num_elements, "Index has to be in [0, num_elements]");
454 ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create coordinate from empty shape");
455
456 Coordinates coord{ 0 };
457
458 for(int d = shape.num_dimensions() - 1; d >= 0; --d)
459 {
460 num_elements /= shape[d];
461 coord.set(d, index / num_elements);
462 index %= num_elements;
463 }
464
465 return coord;
466}
467
468/** Linearise the given coordinate.
469 *
470 * Transforms the given coordinate into a linear offset in terms of
471 * elements.
472 *
473 * @param[in] shape Shape of the n-dimensional tensor.
474 * @param[in] coord The to be converted coordinate.
475 *
476 * @return Linear offset to the element.
477 */
478inline int coord2index(const TensorShape &shape, const Coordinates &coord)
479{
480 ARM_COMPUTE_ERROR_ON_MSG(shape.total_size() == 0, "Cannot get index from empty shape");
481 ARM_COMPUTE_ERROR_ON_MSG(coord.num_dimensions() == 0, "Cannot get index of empty coordinate");
482
483 int index = 0;
484 int dim_size = 1;
485
486 for(unsigned int i = 0; i < coord.num_dimensions(); ++i)
487 {
488 index += coord[i] * dim_size;
489 dim_size *= shape[i];
490 }
491
492 return index;
493}
494
495/** Check if a coordinate is within a valid region */
Moritz Pflanzera1848362017-08-25 12:30:03 +0100496inline bool is_in_valid_region(const ValidRegion &valid_region, Coordinates coord)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100497{
Moritz Pflanzer219c6912017-09-23 19:22:51 +0100498 for(size_t d = 0; d < Coordinates::num_max_dimensions; ++d)
Moritz Pflanzera1848362017-08-25 12:30:03 +0100499 {
500 if(coord[d] < valid_region.start(d) || coord[d] >= valid_region.end(d))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100501 {
502 return false;
503 }
504 }
Moritz Pflanzera1848362017-08-25 12:30:03 +0100505
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100506 return true;
507}
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100508
509/** Create and initialize a tensor of the given type.
510 *
511 * @param[in] shape Tensor shape.
512 * @param[in] data_type Data type.
513 * @param[in] num_channels (Optional) Number of channels.
514 * @param[in] fixed_point_position (Optional) Number of fractional bits.
Chunosovd621bca2017-11-03 17:33:15 +0700515 * @param[in] quantization_info (Optional) Quantization info for asymmetric quantized types.
Michalis Spyroucf581f52018-03-02 10:25:59 +0000516 * @param[in] data_layout (Optional) Data layout. Default is NCHW.
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100517 *
518 * @return Initialized tensor of given type.
519 */
520template <typename T>
Chunosovd621bca2017-11-03 17:33:15 +0700521inline T create_tensor(const TensorShape &shape, DataType data_type, int num_channels = 1,
Michalis Spyroucf581f52018-03-02 10:25:59 +0000522 int fixed_point_position = 0, QuantizationInfo quantization_info = QuantizationInfo(), DataLayout data_layout = DataLayout::NCHW)
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100523{
Chunosovd621bca2017-11-03 17:33:15 +0700524 T tensor;
525 TensorInfo info(shape, num_channels, data_type, fixed_point_position);
526 info.set_quantization_info(quantization_info);
Michalis Spyroucf581f52018-03-02 10:25:59 +0000527 info.set_data_layout(data_layout);
Chunosovd621bca2017-11-03 17:33:15 +0700528 tensor.allocator()->init(info);
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100529
530 return tensor;
531}
SiCong Li3e363692017-07-04 15:02:10 +0100532
Ioan-Cristian Szabo2c350182017-12-20 16:27:37 +0000533/** Create and initialize a tensor of the given type.
534 *
535 * @param[in] shape Tensor shape.
536 * @param[in] format Format type.
537 *
538 * @return Initialized tensor of given type.
539 */
540template <typename T>
541inline T create_tensor(const TensorShape &shape, Format format)
542{
543 TensorInfo info(shape, format);
544
545 T tensor;
546 tensor.allocator()->init(info);
547
548 return tensor;
549}
550
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100551/** Create and initialize a multi-image of the given type.
552 *
553 * @param[in] shape Tensor shape.
554 * @param[in] format Format type.
555 *
556 * @return Initialized tensor of given type.
557 */
558template <typename T>
559inline T create_multi_image(const TensorShape &shape, Format format)
560{
561 T multi_image;
562 multi_image.init(shape.x(), shape.y(), format);
563
564 return multi_image;
565}
566
John Richardson25f23682017-11-27 14:35:09 +0000567/** Create and initialize a HOG (Histogram of Oriented Gradients) of the given type.
568 *
John Richardson684cb0f2018-01-09 11:17:00 +0000569 * @param[in] hog_info HOGInfo object
John Richardson25f23682017-11-27 14:35:09 +0000570 *
571 * @return Initialized HOG of given type.
572 */
573template <typename T>
John Richardson684cb0f2018-01-09 11:17:00 +0000574inline T create_HOG(const HOGInfo &hog_info)
John Richardson25f23682017-11-27 14:35:09 +0000575{
John Richardson684cb0f2018-01-09 11:17:00 +0000576 T hog;
John Richardson25f23682017-11-27 14:35:09 +0000577 hog.init(hog_info);
578
579 return hog;
580}
581
John Richardson8de92612018-02-22 14:09:31 +0000582/** Create and initialize a Pyramid of the given type.
583 *
584 * @param[in] pyramid_info The PyramidInfo object.
585 *
586 * @return Initialized Pyramid of given type.
587 */
588template <typename T>
589inline T create_pyramid(const PyramidInfo &pyramid_info)
590{
591 T pyramid;
592 pyramid.init_auto_padding(pyramid_info);
593
594 return pyramid;
595}
596
John Richardson32af1f82018-06-05 12:47:20 +0100597/** Initialize a convolution matrix.
598 *
599 * @param[in, out] conv The input convolution matrix.
600 * @param[in] width The width of the convolution matrix.
601 * @param[in] height The height of the convolution matrix.
602 * @param[in] seed The random seed to be used.
603 */
604inline void init_conv(int16_t *conv, unsigned int width, unsigned int height, std::random_device::result_type seed)
605{
606 std::mt19937 gen(seed);
607 std::uniform_int_distribution<int16_t> distribution_int16(-32768, 32767);
608
609 for(unsigned int i = 0; i < width * height; ++i)
610 {
611 conv[i] = distribution_int16(gen);
612 }
613}
614
615/** Initialize a separable convolution matrix.
616 *
617 * @param[in, out] conv The input convolution matrix.
618 * @param[in] width The width of the convolution matrix.
619 * @param[in] height The height of the convolution matrix.
620 * @param[in] seed The random seed to be used.
621 */
622inline void init_separable_conv(int16_t *conv, unsigned int width, unsigned int height, std::random_device::result_type seed)
623{
624 std::mt19937 gen(seed);
625 // Set it between -128 and 127 to ensure the matrix does not overflow
626 std::uniform_int_distribution<int16_t> distribution_int16(-128, 127);
627
628 int16_t conv_row[width];
629 int16_t conv_col[height];
630
631 conv_row[0] = conv_col[0] = 1;
632 for(unsigned int i = 1; i < width; ++i)
633 {
634 conv_row[i] = distribution_int16(gen);
635 }
636
637 for(unsigned int i = 1; i < height; ++i)
638 {
639 conv_col[i] = distribution_int16(gen);
640 }
641
642 // Multiply two matrices
643 for(unsigned int i = 0; i < width; ++i)
644 {
645 for(unsigned int j = 0; j < height; ++j)
646 {
647 conv[i * width + j] = conv_col[i] * conv_row[j];
648 }
649 }
650}
651
SiCong Li3e363692017-07-04 15:02:10 +0100652/** Create a vector of random ROIs.
653 *
654 * @param[in] shape The shape of the input tensor.
655 * @param[in] pool_info The ROI pooling information.
656 * @param[in] num_rois The number of ROIs to be created.
657 * @param[in] seed The random seed to be used.
658 *
659 * @return A vector that contains the requested number of random ROIs
660 */
661inline std::vector<ROI> generate_random_rois(const TensorShape &shape, const ROIPoolingLayerInfo &pool_info, unsigned int num_rois, std::random_device::result_type seed)
662{
663 ARM_COMPUTE_ERROR_ON((pool_info.pooled_width() < 4) || (pool_info.pooled_height() < 4));
664
665 std::vector<ROI> rois;
666 std::mt19937 gen(seed);
667 const int pool_width = pool_info.pooled_width();
668 const int pool_height = pool_info.pooled_height();
669 const float roi_scale = pool_info.spatial_scale();
670
671 // Calculate distribution bounds
672 const auto scaled_width = static_cast<int>((shape.x() / roi_scale) / pool_width);
673 const auto scaled_height = static_cast<int>((shape.y() / roi_scale) / pool_height);
674 const auto min_width = static_cast<int>(pool_width / roi_scale);
675 const auto min_height = static_cast<int>(pool_height / roi_scale);
676
677 // Create distributions
678 std::uniform_int_distribution<int> dist_batch(0, shape[3] - 1);
679 std::uniform_int_distribution<int> dist_x(0, scaled_width);
680 std::uniform_int_distribution<int> dist_y(0, scaled_height);
681 std::uniform_int_distribution<int> dist_w(min_width, std::max(min_width, (pool_width - 2) * scaled_width));
682 std::uniform_int_distribution<int> dist_h(min_height, std::max(min_height, (pool_height - 2) * scaled_height));
683
684 for(unsigned int r = 0; r < num_rois; ++r)
685 {
686 ROI roi;
687 roi.batch_idx = dist_batch(gen);
688 roi.rect.x = dist_x(gen);
689 roi.rect.y = dist_y(gen);
690 roi.rect.width = dist_w(gen);
691 roi.rect.height = dist_h(gen);
692 rois.push_back(roi);
693 }
694
695 return rois;
696}
697
John Richardson684cb0f2018-01-09 11:17:00 +0000698/** Create a vector with a uniform distribution of floating point values across the specified range.
699 *
700 * @param[in] num_values The number of values to be created.
701 * @param[in] min The minimum value in distribution (inclusive).
702 * @param[in] max The maximum value in distribution (inclusive).
703 * @param[in] seed The random seed to be used.
704 *
705 * @return A vector that contains the requested number of random floating point values
706 */
707template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
708inline std::vector<T> generate_random_real(unsigned int num_values, T min, T max, std::random_device::result_type seed)
709{
710 std::vector<T> v(num_values);
711 std::mt19937 gen(seed);
712 std::uniform_real_distribution<T> dist(min, max);
713
714 for(unsigned int i = 0; i < num_values; ++i)
715 {
716 v.at(i) = dist(gen);
717 }
718
719 return v;
720}
721
John Richardson8de92612018-02-22 14:09:31 +0000722/** Create a vector of random keypoints for pyramid representation.
723 *
724 * @param[in] shape The shape of the input tensor.
725 * @param[in] num_keypoints The number of keypoints to be created.
726 * @param[in] seed The random seed to be used.
727 * @param[in] num_levels The number of pyramid levels.
728 *
729 * @return A vector that contains the requested number of random keypoints
730 */
731inline std::vector<KeyPoint> generate_random_keypoints(const TensorShape &shape, size_t num_keypoints, std::random_device::result_type seed, size_t num_levels = 1)
732{
733 std::vector<KeyPoint> keypoints;
734 std::mt19937 gen(seed);
735
736 // Calculate distribution bounds
737 const auto min = static_cast<int>(std::pow(2, num_levels));
738 const auto max_width = static_cast<int>(shape.x());
739 const auto max_height = static_cast<int>(shape.y());
740
741 ARM_COMPUTE_ERROR_ON(min > max_width || min > max_height);
742
743 // Create distributions
744 std::uniform_int_distribution<> dist_w(min, max_width);
745 std::uniform_int_distribution<> dist_h(min, max_height);
746
747 for(unsigned int i = 0; i < num_keypoints; i++)
748 {
749 KeyPoint keypoint;
750 keypoint.x = dist_w(gen);
751 keypoint.y = dist_h(gen);
752 keypoint.tracking_status = 1;
753
754 keypoints.push_back(keypoint);
755 }
756
757 return keypoints;
758}
759
SiCong Li3e363692017-07-04 15:02:10 +0100760template <typename T, typename ArrayAccessor_T>
761inline void fill_array(ArrayAccessor_T &&array, const std::vector<T> &v)
762{
763 array.resize(v.size());
764 std::memcpy(array.buffer(), v.data(), v.size() * sizeof(T));
765}
SiCong Li86b53332017-08-23 11:02:43 +0100766
767/** Obtain numpy type string from DataType.
768 *
769 * @param[in] data_type Data type.
770 *
771 * @return numpy type string.
772 */
773inline std::string get_typestring(DataType data_type)
774{
775 // Check endianness
776 const unsigned int i = 1;
777 const char *c = reinterpret_cast<const char *>(&i);
778 std::string endianness;
779 if(*c == 1)
780 {
781 endianness = std::string("<");
782 }
783 else
784 {
785 endianness = std::string(">");
786 }
787 const std::string no_endianness("|");
788
789 switch(data_type)
790 {
791 case DataType::U8:
792 return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t));
793 case DataType::S8:
794 return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t));
795 case DataType::U16:
796 return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t));
797 case DataType::S16:
798 return endianness + "i" + support::cpp11::to_string(sizeof(int16_t));
799 case DataType::U32:
800 return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t));
801 case DataType::S32:
802 return endianness + "i" + support::cpp11::to_string(sizeof(int32_t));
803 case DataType::U64:
804 return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t));
805 case DataType::S64:
806 return endianness + "i" + support::cpp11::to_string(sizeof(int64_t));
807 case DataType::F32:
808 return endianness + "f" + support::cpp11::to_string(sizeof(float));
809 case DataType::F64:
810 return endianness + "f" + support::cpp11::to_string(sizeof(double));
811 case DataType::SIZET:
812 return endianness + "u" + support::cpp11::to_string(sizeof(size_t));
813 default:
814 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
815 }
816}
Joel Liang1c5ffd62017-12-28 10:09:51 +0800817
818/** Sync if necessary.
819 */
820template <typename TensorType>
821inline void sync_if_necessary()
822{
823#ifdef ARM_COMPUTE_CL
824 if(opencl_is_available() && std::is_same<typename std::decay<TensorType>::type, arm_compute::CLTensor>::value)
825 {
826 CLScheduler::get().sync();
827 }
828#endif /* ARM_COMPUTE_CL */
829}
830
831/** Sync tensor if necessary.
832 *
833 * @note: If the destination tensor not being used on OpenGL ES, GPU will optimize out the operation.
834 *
835 * @param[in] tensor Tensor to be sync.
836 */
837template <typename TensorType>
838inline void sync_tensor_if_necessary(TensorType &tensor)
839{
840#ifdef ARM_COMPUTE_GC
841 if(opengles31_is_available() && std::is_same<typename std::decay<TensorType>::type, arm_compute::GCTensor>::value)
842 {
843 // Force sync the tensor by calling map and unmap.
844 IGCTensor &t = dynamic_cast<IGCTensor &>(tensor);
845 t.map();
846 t.unmap();
847 }
848#endif /* ARM_COMPUTE_GC */
849}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100850} // namespace test
851} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100852#endif /* __ARM_COMPUTE_TEST_UTILS_H__ */