blob: 7c55a3ef5064e9285b537e7e83605bed7926474d [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Manuel Bottinicc5171b2019-01-09 17:04:39 +00002 * Copyright (c) 2017-2019 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"
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"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010035#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
Joel Liang1c5ffd62017-12-28 10:09:51 +080037#ifdef ARM_COMPUTE_CL
38#include "arm_compute/core/CL/OpenCL.h"
39#include "arm_compute/runtime/CL/CLScheduler.h"
40#endif /* ARM_COMPUTE_CL */
41
42#ifdef ARM_COMPUTE_GC
43#include "arm_compute/core/GLES_COMPUTE/OpenGLES.h"
44#include "arm_compute/runtime/GLES_COMPUTE/GCTensor.h"
45#endif /* ARM_COMPUTE_GC */
46
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047#include <cmath>
48#include <cstddef>
49#include <limits>
50#include <memory>
SiCong Li3e363692017-07-04 15:02:10 +010051#include <random>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052#include <sstream>
53#include <string>
54#include <type_traits>
SiCong Li3e363692017-07-04 15:02:10 +010055#include <vector>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056
57namespace arm_compute
58{
Joel Liang1c5ffd62017-12-28 10:09:51 +080059#ifdef ARM_COMPUTE_CL
60class CLTensor;
61#endif /* ARM_COMPUTE_CL */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062namespace test
63{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064/** Round floating-point value with half value rounding to positive infinity.
65 *
66 * @param[in] value floating-point value to be rounded.
67 *
68 * @return Floating-point value of rounded @p value.
69 */
70template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
71inline T round_half_up(T value)
72{
73 return std::floor(value + 0.5f);
74}
75
76/** Round floating-point value with half value rounding to nearest even.
77 *
78 * @param[in] value floating-point value to be rounded.
79 * @param[in] epsilon precision.
80 *
81 * @return Floating-point value of rounded @p value.
82 */
83template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
84inline T round_half_even(T value, T epsilon = std::numeric_limits<T>::epsilon())
85{
86 T positive_value = std::abs(value);
87 T ipart = 0;
88 std::modf(positive_value, &ipart);
89 // If 'value' is exactly halfway between two integers
90 if(std::abs(positive_value - (ipart + 0.5f)) < epsilon)
91 {
92 // If 'ipart' is even then return 'ipart'
93 if(std::fmod(ipart, 2.f) < epsilon)
94 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010095 return support::cpp11::copysign(ipart, value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096 }
97 // Else return the nearest even integer
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010098 return support::cpp11::copysign(std::ceil(ipart + 0.5f), value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 }
100 // Otherwise use the usual round to closest
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100101 return support::cpp11::copysign(support::cpp11::round(positive_value), value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103
104namespace traits
105{
106// *INDENT-OFF*
107// clang-format off
Alex Gildayc357c472018-03-21 13:54:09 +0000108/** Promote a type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109template <typename T> struct promote { };
Alex Gildayc357c472018-03-21 13:54:09 +0000110/** Promote uint8_t to uint16_t */
111template <> struct promote<uint8_t> { using type = uint16_t; /**< Promoted type */ };
112/** Promote int8_t to int16_t */
113template <> struct promote<int8_t> { using type = int16_t; /**< Promoted type */ };
114/** Promote uint16_t to uint32_t */
115template <> struct promote<uint16_t> { using type = uint32_t; /**< Promoted type */ };
116/** Promote int16_t to int32_t */
117template <> struct promote<int16_t> { using type = int32_t; /**< Promoted type */ };
118/** Promote uint32_t to uint64_t */
119template <> struct promote<uint32_t> { using type = uint64_t; /**< Promoted type */ };
120/** Promote int32_t to int64_t */
121template <> struct promote<int32_t> { using type = int64_t; /**< Promoted type */ };
122/** Promote float to float */
123template <> struct promote<float> { using type = float; /**< Promoted type */ };
124/** Promote half to half */
125template <> struct promote<half> { using type = half; /**< Promoted type */ };
Pablo Tello383deec2017-06-23 10:40:05 +0100126
Alex Gildayc357c472018-03-21 13:54:09 +0000127/** Get promoted type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128template <typename T>
129using promote_t = typename promote<T>::type;
130
131template <typename T>
132using 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 +0100133
134template <typename T>
135using make_unsigned_conditional_t = typename std::conditional<std::is_integral<T>::value, std::make_unsigned<T>, std::common_type<T>>::type;
136
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137// clang-format on
138// *INDENT-ON*
139}
140
141/** Look up the format corresponding to a channel.
142 *
143 * @param[in] channel Channel type.
144 *
145 * @return Format that contains the given channel.
146 */
147inline Format get_format_for_channel(Channel channel)
148{
149 switch(channel)
150 {
151 case Channel::R:
152 case Channel::G:
153 case Channel::B:
154 return Format::RGB888;
155 default:
156 throw std::runtime_error("Unsupported channel");
157 }
158}
159
160/** Return the format of a channel.
161 *
162 * @param[in] channel Channel type.
163 *
164 * @return Format of the given channel.
165 */
166inline Format get_channel_format(Channel channel)
167{
168 switch(channel)
169 {
170 case Channel::R:
171 case Channel::G:
172 case Channel::B:
173 return Format::U8;
174 default:
175 throw std::runtime_error("Unsupported channel");
176 }
177}
178
179/** Base case of foldl.
180 *
181 * @return value.
182 */
183template <typename F, typename T>
184inline T foldl(F &&, const T &value)
185{
186 return value;
187}
188
189/** Base case of foldl.
190 *
191 * @return func(value1, value2).
192 */
193template <typename F, typename T, typename U>
194inline auto foldl(F &&func, T &&value1, U &&value2) -> decltype(func(value1, value2))
195{
196 return func(value1, value2);
197}
198
199/** Fold left.
200 *
201 * @param[in] func Binary function to be called.
202 * @param[in] initial Initial value.
203 * @param[in] value Argument passed to the function.
204 * @param[in] values Remaining arguments.
205 */
206template <typename F, typename I, typename T, typename... Vs>
207inline I foldl(F &&func, I &&initial, T &&value, Vs &&... values)
208{
209 return foldl(std::forward<F>(func), func(std::forward<I>(initial), std::forward<T>(value)), std::forward<Vs>(values)...);
210}
211
SiCong Libacaf9a2017-06-19 13:41:45 +0100212/** Create a valid region based on tensor shape, border mode and border size
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100213 *
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000214 * @param[in] a_shape Shape used as size of the valid region.
SiCong Libacaf9a2017-06-19 13:41:45 +0100215 * @param[in] border_undefined (Optional) Boolean indicating if the border mode is undefined.
216 * @param[in] border_size (Optional) Border size used to specify the region to exclude.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100217 *
SiCong Libacaf9a2017-06-19 13:41:45 +0100218 * @return A valid region starting at (0, 0, ...) with size of @p shape if @p border_undefined is false; otherwise
219 * 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 +0100220 */
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000221inline 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 +0100222{
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000223 ValidRegion valid_region{ Coordinates(), a_shape };
224
225 Coordinates &anchor = valid_region.anchor;
226 TensorShape &shape = valid_region.shape;
Moritz Pflanzera1848362017-08-25 12:30:03 +0100227
SiCong Libacaf9a2017-06-19 13:41:45 +0100228 if(border_undefined)
229 {
230 ARM_COMPUTE_ERROR_ON(shape.num_dimensions() < 2);
Moritz Pflanzera1848362017-08-25 12:30:03 +0100231
SiCong Libacaf9a2017-06-19 13:41:45 +0100232 anchor.set(0, border_size.left);
233 anchor.set(1, border_size.top);
Moritz Pflanzera1848362017-08-25 12:30:03 +0100234
235 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));
236 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));
237
238 shape.set(0, valid_shape_x);
239 shape.set(1, valid_shape_y);
SiCong Libacaf9a2017-06-19 13:41:45 +0100240 }
Moritz Pflanzera1848362017-08-25 12:30:03 +0100241
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000242 return valid_region;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100243}
244
Gian Marco37908d92017-11-07 14:38:22 +0000245/** Create a valid region for Gaussian Pyramid Half based on tensor shape and valid region at level "i - 1" and border mode
246 *
247 * @note The border size is 2 in case of Gaussian Pyramid Half
248 *
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000249 * @param[in] a_shape Shape used at level "i - 1" of Gaussian Pyramid Half
250 * @param[in] a_valid_region Valid region used at level "i - 1" of Gaussian Pyramid Half
Gian Marco37908d92017-11-07 14:38:22 +0000251 * @param[in] border_undefined (Optional) Boolean indicating if the border mode is undefined.
252 *
253 * return The valid region for the level "i" of Gaussian Pyramid Half
254 */
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000255inline 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 +0000256{
257 constexpr int border_size = 2;
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000258
259 ValidRegion valid_region{ Coordinates(), a_shape };
260
261 Coordinates &anchor = valid_region.anchor;
262 TensorShape &shape = valid_region.shape;
Gian Marco37908d92017-11-07 14:38:22 +0000263
264 // Compute tensor shape for level "i" of Gaussian Pyramid Half
265 // dst_width = (src_width + 1) * 0.5f
266 // dst_height = (src_height + 1) * 0.5f
Gian Marco Iodice2abb2162018-04-11 10:49:04 +0100267 shape.set(0, (a_shape[0] + 1) * 0.5f);
268 shape.set(1, (a_shape[1] + 1) * 0.5f);
Gian Marco37908d92017-11-07 14:38:22 +0000269
270 if(border_undefined)
271 {
272 ARM_COMPUTE_ERROR_ON(shape.num_dimensions() < 2);
273
274 // Compute the left and top invalid borders
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000275 float invalid_border_left = static_cast<float>(a_valid_region.anchor.x() + border_size) / 2.0f;
276 float invalid_border_top = static_cast<float>(a_valid_region.anchor.y() + border_size) / 2.0f;
Gian Marco37908d92017-11-07 14:38:22 +0000277
278 // For the new anchor point we can have 2 cases:
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000279 // 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
280 // 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 +0000281 // 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 +0100282 invalid_border_left = (a_shape[0] % 2) ? std::ceil(invalid_border_left) : std::floor(invalid_border_left);
283 invalid_border_top = (a_shape[1] % 2) ? std::ceil(invalid_border_top) : std::floor(invalid_border_top);
Gian Marco37908d92017-11-07 14:38:22 +0000284
285 // Set the anchor point
286 anchor.set(0, static_cast<int>(invalid_border_left));
287 anchor.set(1, static_cast<int>(invalid_border_top));
288
289 // Compute shape
290 // Calculate the right and bottom invalid borders at the previous level of the pyramid
Gian Marco Iodice2abb2162018-04-11 10:49:04 +0100291 const float prev_invalid_border_right = static_cast<float>(a_shape[0] - (a_valid_region.anchor.x() + a_valid_region.shape[0]));
292 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 +0000293
294 // Calculate the right and bottom invalid borders at the current level of the pyramid
295 const float invalid_border_right = std::ceil((prev_invalid_border_right + static_cast<float>(border_size)) / 2.0f);
296 const float invalid_border_bottom = std::ceil((prev_invalid_border_bottom + static_cast<float>(border_size)) / 2.0f);
297
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000298 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));
299 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 +0000300
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000301 shape.set(0, valid_shape_x);
302 shape.set(1, valid_shape_y);
Gian Marco37908d92017-11-07 14:38:22 +0000303 }
304
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000305 return valid_region;
Gian Marco37908d92017-11-07 14:38:22 +0000306}
307
John Richardson2d008a42018-03-22 13:48:41 +0000308/** Create a valid region for Laplacian Pyramid based on tensor shape and valid region at level "i - 1" and border mode
309 *
310 * @note The border size is 2 in case of Laplacian Pyramid
311 *
312 * @param[in] a_shape Shape used at level "i - 1" of Laplacian Pyramid
313 * @param[in] a_valid_region Valid region used at level "i - 1" of Laplacian Pyramid
314 * @param[in] border_undefined (Optional) Boolean indicating if the border mode is undefined.
315 *
316 * return The valid region for the level "i" of Laplacian Pyramid
317 */
318inline ValidRegion shape_to_valid_region_laplacian_pyramid(const TensorShape &a_shape, const ValidRegion &a_valid_region, bool border_undefined = false)
319{
320 ValidRegion valid_region = shape_to_valid_region_gaussian_pyramid_half(a_shape, a_valid_region, border_undefined);
321
322 if(border_undefined)
323 {
324 const BorderSize gaussian5x5_border(2);
325
326 auto border_left = static_cast<int>(gaussian5x5_border.left);
327 auto border_right = static_cast<int>(gaussian5x5_border.right);
328 auto border_top = static_cast<int>(gaussian5x5_border.top);
329 auto border_bottom = static_cast<int>(gaussian5x5_border.bottom);
330
331 valid_region.anchor.set(0, valid_region.anchor[0] + border_left);
332 valid_region.anchor.set(1, valid_region.anchor[1] + border_top);
333 valid_region.shape.set(0, std::max(0, static_cast<int>(valid_region.shape[0]) - border_right - border_left));
334 valid_region.shape.set(1, std::max(0, static_cast<int>(valid_region.shape[1]) - border_top - border_bottom));
335 }
336
337 return valid_region;
338}
339
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100340/** Write the value after casting the pointer according to @p data_type.
341 *
342 * @warning The type of the value must match the specified data type.
343 *
344 * @param[out] ptr Pointer to memory where the @p value will be written.
345 * @param[in] value Value that will be written.
346 * @param[in] data_type Data type that will be written.
347 */
348template <typename T>
349void store_value_with_data_type(void *ptr, T value, DataType data_type)
350{
351 switch(data_type)
352 {
353 case DataType::U8:
Chunosovd621bca2017-11-03 17:33:15 +0700354 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100355 *reinterpret_cast<uint8_t *>(ptr) = value;
356 break;
357 case DataType::S8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100358 *reinterpret_cast<int8_t *>(ptr) = value;
359 break;
360 case DataType::U16:
361 *reinterpret_cast<uint16_t *>(ptr) = value;
362 break;
363 case DataType::S16:
364 *reinterpret_cast<int16_t *>(ptr) = value;
365 break;
366 case DataType::U32:
367 *reinterpret_cast<uint32_t *>(ptr) = value;
368 break;
369 case DataType::S32:
370 *reinterpret_cast<int32_t *>(ptr) = value;
371 break;
372 case DataType::U64:
373 *reinterpret_cast<uint64_t *>(ptr) = value;
374 break;
375 case DataType::S64:
376 *reinterpret_cast<int64_t *>(ptr) = value;
377 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100378 case DataType::F16:
Georgios Pinitas583137c2017-08-31 18:12:42 +0100379 *reinterpret_cast<half *>(ptr) = value;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100380 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100381 case DataType::F32:
382 *reinterpret_cast<float *>(ptr) = value;
383 break;
384 case DataType::F64:
385 *reinterpret_cast<double *>(ptr) = value;
386 break;
387 case DataType::SIZET:
388 *reinterpret_cast<size_t *>(ptr) = value;
389 break;
390 default:
391 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
392 }
393}
394
395/** Saturate a value of type T against the numeric limits of type U.
396 *
397 * @param[in] val Value to be saturated.
398 *
399 * @return saturated value.
400 */
401template <typename U, typename T>
402T saturate_cast(T val)
403{
404 if(val > static_cast<T>(std::numeric_limits<U>::max()))
405 {
406 val = static_cast<T>(std::numeric_limits<U>::max());
407 }
408 if(val < static_cast<T>(std::numeric_limits<U>::lowest()))
409 {
410 val = static_cast<T>(std::numeric_limits<U>::lowest());
411 }
412 return val;
413}
414
415/** Find the signed promoted common type.
416 */
417template <typename... T>
418struct common_promoted_signed_type
419{
Alex Gildayc357c472018-03-21 13:54:09 +0000420 /** Common type */
421 using common_type = typename std::common_type<T...>::type;
422 /** Promoted type */
423 using promoted_type = traits::promote_t<common_type>;
424 /** Intermediate type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100425 using intermediate_type = typename traits::make_signed_conditional_t<promoted_type>::type;
426};
427
John Richardson3c5f9492017-10-04 15:27:37 +0100428/** Find the unsigned promoted common type.
429 */
430template <typename... T>
431struct common_promoted_unsigned_type
432{
Alex Gildayc357c472018-03-21 13:54:09 +0000433 /** Common type */
434 using common_type = typename std::common_type<T...>::type;
435 /** Promoted type */
436 using promoted_type = traits::promote_t<common_type>;
437 /** Intermediate type */
John Richardson3c5f9492017-10-04 15:27:37 +0100438 using intermediate_type = typename traits::make_unsigned_conditional_t<promoted_type>::type;
439};
440
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100441/** Convert a linear index into n-dimensional coordinates.
442 *
443 * @param[in] shape Shape of the n-dimensional tensor.
444 * @param[in] index Linear index specifying the i-th element.
445 *
446 * @return n-dimensional coordinates.
447 */
448inline Coordinates index2coord(const TensorShape &shape, int index)
449{
450 int num_elements = shape.total_size();
451
452 ARM_COMPUTE_ERROR_ON_MSG(index < 0 || index >= num_elements, "Index has to be in [0, num_elements]");
453 ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create coordinate from empty shape");
454
455 Coordinates coord{ 0 };
456
457 for(int d = shape.num_dimensions() - 1; d >= 0; --d)
458 {
459 num_elements /= shape[d];
460 coord.set(d, index / num_elements);
461 index %= num_elements;
462 }
463
464 return coord;
465}
466
467/** Linearise the given coordinate.
468 *
469 * Transforms the given coordinate into a linear offset in terms of
470 * elements.
471 *
472 * @param[in] shape Shape of the n-dimensional tensor.
473 * @param[in] coord The to be converted coordinate.
474 *
475 * @return Linear offset to the element.
476 */
477inline int coord2index(const TensorShape &shape, const Coordinates &coord)
478{
479 ARM_COMPUTE_ERROR_ON_MSG(shape.total_size() == 0, "Cannot get index from empty shape");
480 ARM_COMPUTE_ERROR_ON_MSG(coord.num_dimensions() == 0, "Cannot get index of empty coordinate");
481
482 int index = 0;
483 int dim_size = 1;
484
485 for(unsigned int i = 0; i < coord.num_dimensions(); ++i)
486 {
487 index += coord[i] * dim_size;
488 dim_size *= shape[i];
489 }
490
491 return index;
492}
493
494/** Check if a coordinate is within a valid region */
Moritz Pflanzera1848362017-08-25 12:30:03 +0100495inline bool is_in_valid_region(const ValidRegion &valid_region, Coordinates coord)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100496{
Moritz Pflanzer219c6912017-09-23 19:22:51 +0100497 for(size_t d = 0; d < Coordinates::num_max_dimensions; ++d)
Moritz Pflanzera1848362017-08-25 12:30:03 +0100498 {
499 if(coord[d] < valid_region.start(d) || coord[d] >= valid_region.end(d))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100500 {
501 return false;
502 }
503 }
Moritz Pflanzera1848362017-08-25 12:30:03 +0100504
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100505 return true;
506}
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100507
508/** Create and initialize a tensor of the given type.
509 *
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100510 * @param[in] shape Tensor shape.
511 * @param[in] data_type Data type.
512 * @param[in] num_channels (Optional) Number of channels.
513 * @param[in] quantization_info (Optional) Quantization info for asymmetric quantized types.
514 * @param[in] data_layout (Optional) Data layout. Default is NCHW.
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100515 *
516 * @return Initialized tensor of given type.
517 */
518template <typename T>
Chunosovd621bca2017-11-03 17:33:15 +0700519inline T create_tensor(const TensorShape &shape, DataType data_type, int num_channels = 1,
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100520 QuantizationInfo quantization_info = QuantizationInfo(), DataLayout data_layout = DataLayout::NCHW)
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100521{
Chunosovd621bca2017-11-03 17:33:15 +0700522 T tensor;
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100523 TensorInfo info(shape, num_channels, data_type);
Chunosovd621bca2017-11-03 17:33:15 +0700524 info.set_quantization_info(quantization_info);
Michalis Spyroucf581f52018-03-02 10:25:59 +0000525 info.set_data_layout(data_layout);
Chunosovd621bca2017-11-03 17:33:15 +0700526 tensor.allocator()->init(info);
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100527
528 return tensor;
529}
SiCong Li3e363692017-07-04 15:02:10 +0100530
Ioan-Cristian Szabo2c350182017-12-20 16:27:37 +0000531/** Create and initialize a tensor of the given type.
532 *
533 * @param[in] shape Tensor shape.
534 * @param[in] format Format type.
535 *
536 * @return Initialized tensor of given type.
537 */
538template <typename T>
539inline T create_tensor(const TensorShape &shape, Format format)
540{
541 TensorInfo info(shape, format);
542
543 T tensor;
544 tensor.allocator()->init(info);
545
546 return tensor;
547}
548
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100549/** Create and initialize a multi-image of the given type.
550 *
551 * @param[in] shape Tensor shape.
552 * @param[in] format Format type.
553 *
554 * @return Initialized tensor of given type.
555 */
556template <typename T>
557inline T create_multi_image(const TensorShape &shape, Format format)
558{
559 T multi_image;
560 multi_image.init(shape.x(), shape.y(), format);
561
562 return multi_image;
563}
564
John Richardson25f23682017-11-27 14:35:09 +0000565/** Create and initialize a HOG (Histogram of Oriented Gradients) of the given type.
566 *
John Richardson684cb0f2018-01-09 11:17:00 +0000567 * @param[in] hog_info HOGInfo object
John Richardson25f23682017-11-27 14:35:09 +0000568 *
569 * @return Initialized HOG of given type.
570 */
571template <typename T>
John Richardson684cb0f2018-01-09 11:17:00 +0000572inline T create_HOG(const HOGInfo &hog_info)
John Richardson25f23682017-11-27 14:35:09 +0000573{
John Richardson684cb0f2018-01-09 11:17:00 +0000574 T hog;
John Richardson25f23682017-11-27 14:35:09 +0000575 hog.init(hog_info);
576
577 return hog;
578}
579
John Richardson8de92612018-02-22 14:09:31 +0000580/** Create and initialize a Pyramid of the given type.
581 *
582 * @param[in] pyramid_info The PyramidInfo object.
583 *
584 * @return Initialized Pyramid of given type.
585 */
586template <typename T>
587inline T create_pyramid(const PyramidInfo &pyramid_info)
588{
589 T pyramid;
590 pyramid.init_auto_padding(pyramid_info);
591
592 return pyramid;
593}
594
John Richardson32af1f82018-06-05 12:47:20 +0100595/** Initialize a convolution matrix.
596 *
597 * @param[in, out] conv The input convolution matrix.
598 * @param[in] width The width of the convolution matrix.
599 * @param[in] height The height of the convolution matrix.
600 * @param[in] seed The random seed to be used.
601 */
602inline void init_conv(int16_t *conv, unsigned int width, unsigned int height, std::random_device::result_type seed)
603{
604 std::mt19937 gen(seed);
605 std::uniform_int_distribution<int16_t> distribution_int16(-32768, 32767);
606
607 for(unsigned int i = 0; i < width * height; ++i)
608 {
609 conv[i] = distribution_int16(gen);
610 }
611}
612
613/** Initialize a separable convolution matrix.
614 *
615 * @param[in, out] conv The input convolution matrix.
616 * @param[in] width The width of the convolution matrix.
617 * @param[in] height The height of the convolution matrix.
618 * @param[in] seed The random seed to be used.
619 */
620inline void init_separable_conv(int16_t *conv, unsigned int width, unsigned int height, std::random_device::result_type seed)
621{
622 std::mt19937 gen(seed);
623 // Set it between -128 and 127 to ensure the matrix does not overflow
624 std::uniform_int_distribution<int16_t> distribution_int16(-128, 127);
625
626 int16_t conv_row[width];
627 int16_t conv_col[height];
628
629 conv_row[0] = conv_col[0] = 1;
630 for(unsigned int i = 1; i < width; ++i)
631 {
632 conv_row[i] = distribution_int16(gen);
633 }
634
635 for(unsigned int i = 1; i < height; ++i)
636 {
637 conv_col[i] = distribution_int16(gen);
638 }
639
640 // Multiply two matrices
641 for(unsigned int i = 0; i < width; ++i)
642 {
643 for(unsigned int j = 0; j < height; ++j)
644 {
645 conv[i * width + j] = conv_col[i] * conv_row[j];
646 }
647 }
648}
649
John Richardson684cb0f2018-01-09 11:17:00 +0000650/** Create a vector with a uniform distribution of floating point values across the specified range.
651 *
652 * @param[in] num_values The number of values to be created.
653 * @param[in] min The minimum value in distribution (inclusive).
654 * @param[in] max The maximum value in distribution (inclusive).
655 * @param[in] seed The random seed to be used.
656 *
657 * @return A vector that contains the requested number of random floating point values
658 */
659template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
660inline std::vector<T> generate_random_real(unsigned int num_values, T min, T max, std::random_device::result_type seed)
661{
662 std::vector<T> v(num_values);
663 std::mt19937 gen(seed);
664 std::uniform_real_distribution<T> dist(min, max);
665
666 for(unsigned int i = 0; i < num_values; ++i)
667 {
668 v.at(i) = dist(gen);
669 }
670
671 return v;
672}
673
John Richardson8de92612018-02-22 14:09:31 +0000674/** Create a vector of random keypoints for pyramid representation.
675 *
676 * @param[in] shape The shape of the input tensor.
677 * @param[in] num_keypoints The number of keypoints to be created.
678 * @param[in] seed The random seed to be used.
679 * @param[in] num_levels The number of pyramid levels.
680 *
681 * @return A vector that contains the requested number of random keypoints
682 */
683inline std::vector<KeyPoint> generate_random_keypoints(const TensorShape &shape, size_t num_keypoints, std::random_device::result_type seed, size_t num_levels = 1)
684{
685 std::vector<KeyPoint> keypoints;
686 std::mt19937 gen(seed);
687
688 // Calculate distribution bounds
689 const auto min = static_cast<int>(std::pow(2, num_levels));
690 const auto max_width = static_cast<int>(shape.x());
691 const auto max_height = static_cast<int>(shape.y());
692
693 ARM_COMPUTE_ERROR_ON(min > max_width || min > max_height);
694
695 // Create distributions
696 std::uniform_int_distribution<> dist_w(min, max_width);
697 std::uniform_int_distribution<> dist_h(min, max_height);
698
699 for(unsigned int i = 0; i < num_keypoints; i++)
700 {
701 KeyPoint keypoint;
702 keypoint.x = dist_w(gen);
703 keypoint.y = dist_h(gen);
704 keypoint.tracking_status = 1;
705
706 keypoints.push_back(keypoint);
707 }
708
709 return keypoints;
710}
711
SiCong Li3e363692017-07-04 15:02:10 +0100712template <typename T, typename ArrayAccessor_T>
713inline void fill_array(ArrayAccessor_T &&array, const std::vector<T> &v)
714{
715 array.resize(v.size());
716 std::memcpy(array.buffer(), v.data(), v.size() * sizeof(T));
717}
SiCong Li86b53332017-08-23 11:02:43 +0100718
719/** Obtain numpy type string from DataType.
720 *
721 * @param[in] data_type Data type.
722 *
723 * @return numpy type string.
724 */
725inline std::string get_typestring(DataType data_type)
726{
727 // Check endianness
728 const unsigned int i = 1;
729 const char *c = reinterpret_cast<const char *>(&i);
730 std::string endianness;
731 if(*c == 1)
732 {
733 endianness = std::string("<");
734 }
735 else
736 {
737 endianness = std::string(">");
738 }
739 const std::string no_endianness("|");
740
741 switch(data_type)
742 {
743 case DataType::U8:
744 return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t));
745 case DataType::S8:
746 return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t));
747 case DataType::U16:
748 return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t));
749 case DataType::S16:
750 return endianness + "i" + support::cpp11::to_string(sizeof(int16_t));
751 case DataType::U32:
752 return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t));
753 case DataType::S32:
754 return endianness + "i" + support::cpp11::to_string(sizeof(int32_t));
755 case DataType::U64:
756 return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t));
757 case DataType::S64:
758 return endianness + "i" + support::cpp11::to_string(sizeof(int64_t));
759 case DataType::F32:
760 return endianness + "f" + support::cpp11::to_string(sizeof(float));
761 case DataType::F64:
762 return endianness + "f" + support::cpp11::to_string(sizeof(double));
763 case DataType::SIZET:
764 return endianness + "u" + support::cpp11::to_string(sizeof(size_t));
765 default:
766 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
767 }
768}
Joel Liang1c5ffd62017-12-28 10:09:51 +0800769
770/** Sync if necessary.
771 */
772template <typename TensorType>
773inline void sync_if_necessary()
774{
775#ifdef ARM_COMPUTE_CL
776 if(opencl_is_available() && std::is_same<typename std::decay<TensorType>::type, arm_compute::CLTensor>::value)
777 {
778 CLScheduler::get().sync();
779 }
780#endif /* ARM_COMPUTE_CL */
781}
782
783/** Sync tensor if necessary.
784 *
785 * @note: If the destination tensor not being used on OpenGL ES, GPU will optimize out the operation.
786 *
787 * @param[in] tensor Tensor to be sync.
788 */
789template <typename TensorType>
790inline void sync_tensor_if_necessary(TensorType &tensor)
791{
792#ifdef ARM_COMPUTE_GC
793 if(opengles31_is_available() && std::is_same<typename std::decay<TensorType>::type, arm_compute::GCTensor>::value)
794 {
795 // Force sync the tensor by calling map and unmap.
796 IGCTensor &t = dynamic_cast<IGCTensor &>(tensor);
797 t.map();
798 t.unmap();
799 }
800#endif /* ARM_COMPUTE_GC */
801}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100802} // namespace test
803} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100804#endif /* __ARM_COMPUTE_TEST_UTILS_H__ */