blob: b62ad4a6777b67ff091575e3762693d23217f108 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Manuel Bottiniceaa0bf2021-02-16 15:15:19 +00002 * Copyright (c) 2017-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_TEST_UTILS_H
25#define ARM_COMPUTE_TEST_UTILS_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
27#include "arm_compute/core/Coordinates.h"
28#include "arm_compute/core/Error.h"
John Richardson25f23682017-11-27 14:35:09 +000029#include "arm_compute/core/Size2D.h"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010030#include "arm_compute/core/TensorInfo.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/core/TensorShape.h"
32#include "arm_compute/core/Types.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000033#include "support/StringSupport.h"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010034#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
Joel Liang1c5ffd62017-12-28 10:09:51 +080036#ifdef ARM_COMPUTE_CL
37#include "arm_compute/core/CL/OpenCL.h"
38#include "arm_compute/runtime/CL/CLScheduler.h"
39#endif /* ARM_COMPUTE_CL */
40
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041#include <cmath>
42#include <cstddef>
43#include <limits>
44#include <memory>
SiCong Li3e363692017-07-04 15:02:10 +010045#include <random>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046#include <sstream>
47#include <string>
48#include <type_traits>
SiCong Li3e363692017-07-04 15:02:10 +010049#include <vector>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010050
Georgios Pinitas12833d02019-07-25 13:31:10 +010051#include "arm_compute/runtime/CPP/CPPScheduler.h"
52#include "arm_compute/runtime/RuntimeContext.h"
53
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054namespace arm_compute
55{
Joel Liang1c5ffd62017-12-28 10:09:51 +080056#ifdef ARM_COMPUTE_CL
57class CLTensor;
58#endif /* ARM_COMPUTE_CL */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059namespace test
60{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061/** Round floating-point value with half value rounding to positive infinity.
62 *
63 * @param[in] value floating-point value to be rounded.
64 *
65 * @return Floating-point value of rounded @p value.
66 */
67template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
68inline T round_half_up(T value)
69{
70 return std::floor(value + 0.5f);
71}
72
73/** Round floating-point value with half value rounding to nearest even.
74 *
75 * @param[in] value floating-point value to be rounded.
76 * @param[in] epsilon precision.
77 *
78 * @return Floating-point value of rounded @p value.
79 */
80template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
81inline T round_half_even(T value, T epsilon = std::numeric_limits<T>::epsilon())
82{
83 T positive_value = std::abs(value);
84 T ipart = 0;
85 std::modf(positive_value, &ipart);
86 // If 'value' is exactly halfway between two integers
87 if(std::abs(positive_value - (ipart + 0.5f)) < epsilon)
88 {
89 // If 'ipart' is even then return 'ipart'
90 if(std::fmod(ipart, 2.f) < epsilon)
91 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010092 return support::cpp11::copysign(ipart, value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093 }
94 // Else return the nearest even integer
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010095 return support::cpp11::copysign(std::ceil(ipart + 0.5f), value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096 }
97 // Otherwise use the usual round to closest
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010098 return support::cpp11::copysign(support::cpp11::round(positive_value), value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100
101namespace traits
102{
103// *INDENT-OFF*
104// clang-format off
Alex Gildayc357c472018-03-21 13:54:09 +0000105/** Promote a type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106template <typename T> struct promote { };
Alex Gildayc357c472018-03-21 13:54:09 +0000107/** Promote uint8_t to uint16_t */
108template <> struct promote<uint8_t> { using type = uint16_t; /**< Promoted type */ };
109/** Promote int8_t to int16_t */
110template <> struct promote<int8_t> { using type = int16_t; /**< Promoted type */ };
111/** Promote uint16_t to uint32_t */
112template <> struct promote<uint16_t> { using type = uint32_t; /**< Promoted type */ };
113/** Promote int16_t to int32_t */
114template <> struct promote<int16_t> { using type = int32_t; /**< Promoted type */ };
115/** Promote uint32_t to uint64_t */
116template <> struct promote<uint32_t> { using type = uint64_t; /**< Promoted type */ };
117/** Promote int32_t to int64_t */
118template <> struct promote<int32_t> { using type = int64_t; /**< Promoted type */ };
119/** Promote float to float */
120template <> struct promote<float> { using type = float; /**< Promoted type */ };
121/** Promote half to half */
122template <> struct promote<half> { using type = half; /**< Promoted type */ };
Pablo Tello383deec2017-06-23 10:40:05 +0100123
Alex Gildayc357c472018-03-21 13:54:09 +0000124/** Get promoted type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125template <typename T>
126using promote_t = typename promote<T>::type;
127
128template <typename T>
129using 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 +0100130
131template <typename T>
132using make_unsigned_conditional_t = typename std::conditional<std::is_integral<T>::value, std::make_unsigned<T>, std::common_type<T>>::type;
133
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134// clang-format on
135// *INDENT-ON*
136}
137
138/** Look up the format corresponding to a channel.
139 *
140 * @param[in] channel Channel type.
141 *
142 * @return Format that contains the given channel.
143 */
144inline Format get_format_for_channel(Channel channel)
145{
146 switch(channel)
147 {
148 case Channel::R:
149 case Channel::G:
150 case Channel::B:
151 return Format::RGB888;
152 default:
153 throw std::runtime_error("Unsupported channel");
154 }
155}
156
157/** Return the format of a channel.
158 *
159 * @param[in] channel Channel type.
160 *
161 * @return Format of the given channel.
162 */
163inline Format get_channel_format(Channel channel)
164{
165 switch(channel)
166 {
167 case Channel::R:
168 case Channel::G:
169 case Channel::B:
170 return Format::U8;
171 default:
172 throw std::runtime_error("Unsupported channel");
173 }
174}
175
176/** Base case of foldl.
177 *
178 * @return value.
179 */
180template <typename F, typename T>
181inline T foldl(F &&, const T &value)
182{
183 return value;
184}
185
186/** Base case of foldl.
187 *
188 * @return func(value1, value2).
189 */
190template <typename F, typename T, typename U>
191inline auto foldl(F &&func, T &&value1, U &&value2) -> decltype(func(value1, value2))
192{
193 return func(value1, value2);
194}
195
196/** Fold left.
197 *
198 * @param[in] func Binary function to be called.
199 * @param[in] initial Initial value.
200 * @param[in] value Argument passed to the function.
201 * @param[in] values Remaining arguments.
202 */
203template <typename F, typename I, typename T, typename... Vs>
204inline I foldl(F &&func, I &&initial, T &&value, Vs &&... values)
205{
206 return foldl(std::forward<F>(func), func(std::forward<I>(initial), std::forward<T>(value)), std::forward<Vs>(values)...);
207}
208
SiCong Libacaf9a2017-06-19 13:41:45 +0100209/** Create a valid region based on tensor shape, border mode and border size
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100210 *
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000211 * @param[in] a_shape Shape used as size of the valid region.
SiCong Libacaf9a2017-06-19 13:41:45 +0100212 * @param[in] border_undefined (Optional) Boolean indicating if the border mode is undefined.
213 * @param[in] border_size (Optional) Border size used to specify the region to exclude.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100214 *
SiCong Libacaf9a2017-06-19 13:41:45 +0100215 * @return A valid region starting at (0, 0, ...) with size of @p shape if @p border_undefined is false; otherwise
216 * 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 +0100217 */
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000218inline 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 +0100219{
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000220 ValidRegion valid_region{ Coordinates(), a_shape };
221
222 Coordinates &anchor = valid_region.anchor;
223 TensorShape &shape = valid_region.shape;
Moritz Pflanzera1848362017-08-25 12:30:03 +0100224
SiCong Libacaf9a2017-06-19 13:41:45 +0100225 if(border_undefined)
226 {
227 ARM_COMPUTE_ERROR_ON(shape.num_dimensions() < 2);
Moritz Pflanzera1848362017-08-25 12:30:03 +0100228
SiCong Libacaf9a2017-06-19 13:41:45 +0100229 anchor.set(0, border_size.left);
230 anchor.set(1, border_size.top);
Moritz Pflanzera1848362017-08-25 12:30:03 +0100231
232 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));
233 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));
234
235 shape.set(0, valid_shape_x);
236 shape.set(1, valid_shape_y);
SiCong Libacaf9a2017-06-19 13:41:45 +0100237 }
Moritz Pflanzera1848362017-08-25 12:30:03 +0100238
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000239 return valid_region;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100240}
241
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100242/** Write the value after casting the pointer according to @p data_type.
243 *
244 * @warning The type of the value must match the specified data type.
245 *
246 * @param[out] ptr Pointer to memory where the @p value will be written.
247 * @param[in] value Value that will be written.
248 * @param[in] data_type Data type that will be written.
249 */
250template <typename T>
251void store_value_with_data_type(void *ptr, T value, DataType data_type)
252{
253 switch(data_type)
254 {
255 case DataType::U8:
Chunosovd621bca2017-11-03 17:33:15 +0700256 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100257 *reinterpret_cast<uint8_t *>(ptr) = value;
258 break;
259 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100260 case DataType::QASYMM8_SIGNED:
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100261 case DataType::QSYMM8:
262 case DataType::QSYMM8_PER_CHANNEL:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100263 *reinterpret_cast<int8_t *>(ptr) = value;
264 break;
265 case DataType::U16:
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +0100266 case DataType::QASYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100267 *reinterpret_cast<uint16_t *>(ptr) = value;
268 break;
269 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100270 case DataType::QSYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100271 *reinterpret_cast<int16_t *>(ptr) = value;
272 break;
273 case DataType::U32:
274 *reinterpret_cast<uint32_t *>(ptr) = value;
275 break;
276 case DataType::S32:
277 *reinterpret_cast<int32_t *>(ptr) = value;
278 break;
279 case DataType::U64:
280 *reinterpret_cast<uint64_t *>(ptr) = value;
281 break;
282 case DataType::S64:
283 *reinterpret_cast<int64_t *>(ptr) = value;
284 break;
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000285 case DataType::BFLOAT16:
286 *reinterpret_cast<bfloat16 *>(ptr) = bfloat16(value);
287 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100288 case DataType::F16:
Georgios Pinitas583137c2017-08-31 18:12:42 +0100289 *reinterpret_cast<half *>(ptr) = value;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100290 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100291 case DataType::F32:
292 *reinterpret_cast<float *>(ptr) = value;
293 break;
294 case DataType::F64:
295 *reinterpret_cast<double *>(ptr) = value;
296 break;
297 case DataType::SIZET:
298 *reinterpret_cast<size_t *>(ptr) = value;
299 break;
300 default:
301 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
302 }
303}
304
305/** Saturate a value of type T against the numeric limits of type U.
306 *
307 * @param[in] val Value to be saturated.
308 *
309 * @return saturated value.
310 */
311template <typename U, typename T>
312T saturate_cast(T val)
313{
314 if(val > static_cast<T>(std::numeric_limits<U>::max()))
315 {
316 val = static_cast<T>(std::numeric_limits<U>::max());
317 }
318 if(val < static_cast<T>(std::numeric_limits<U>::lowest()))
319 {
320 val = static_cast<T>(std::numeric_limits<U>::lowest());
321 }
322 return val;
323}
324
325/** Find the signed promoted common type.
326 */
327template <typename... T>
328struct common_promoted_signed_type
329{
Alex Gildayc357c472018-03-21 13:54:09 +0000330 /** Common type */
331 using common_type = typename std::common_type<T...>::type;
332 /** Promoted type */
333 using promoted_type = traits::promote_t<common_type>;
334 /** Intermediate type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100335 using intermediate_type = typename traits::make_signed_conditional_t<promoted_type>::type;
336};
337
John Richardson3c5f9492017-10-04 15:27:37 +0100338/** Find the unsigned promoted common type.
339 */
340template <typename... T>
341struct common_promoted_unsigned_type
342{
Alex Gildayc357c472018-03-21 13:54:09 +0000343 /** Common type */
344 using common_type = typename std::common_type<T...>::type;
345 /** Promoted type */
346 using promoted_type = traits::promote_t<common_type>;
347 /** Intermediate type */
John Richardson3c5f9492017-10-04 15:27:37 +0100348 using intermediate_type = typename traits::make_unsigned_conditional_t<promoted_type>::type;
349};
350
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100351/** Convert a linear index into n-dimensional coordinates.
352 *
353 * @param[in] shape Shape of the n-dimensional tensor.
354 * @param[in] index Linear index specifying the i-th element.
355 *
356 * @return n-dimensional coordinates.
357 */
358inline Coordinates index2coord(const TensorShape &shape, int index)
359{
360 int num_elements = shape.total_size();
361
362 ARM_COMPUTE_ERROR_ON_MSG(index < 0 || index >= num_elements, "Index has to be in [0, num_elements]");
363 ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create coordinate from empty shape");
364
365 Coordinates coord{ 0 };
366
367 for(int d = shape.num_dimensions() - 1; d >= 0; --d)
368 {
369 num_elements /= shape[d];
370 coord.set(d, index / num_elements);
371 index %= num_elements;
372 }
373
374 return coord;
375}
376
377/** Linearise the given coordinate.
378 *
379 * Transforms the given coordinate into a linear offset in terms of
380 * elements.
381 *
382 * @param[in] shape Shape of the n-dimensional tensor.
383 * @param[in] coord The to be converted coordinate.
384 *
385 * @return Linear offset to the element.
386 */
387inline int coord2index(const TensorShape &shape, const Coordinates &coord)
388{
389 ARM_COMPUTE_ERROR_ON_MSG(shape.total_size() == 0, "Cannot get index from empty shape");
390 ARM_COMPUTE_ERROR_ON_MSG(coord.num_dimensions() == 0, "Cannot get index of empty coordinate");
391
392 int index = 0;
393 int dim_size = 1;
394
395 for(unsigned int i = 0; i < coord.num_dimensions(); ++i)
396 {
397 index += coord[i] * dim_size;
398 dim_size *= shape[i];
399 }
400
401 return index;
402}
403
404/** Check if a coordinate is within a valid region */
Moritz Pflanzera1848362017-08-25 12:30:03 +0100405inline bool is_in_valid_region(const ValidRegion &valid_region, Coordinates coord)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100406{
Moritz Pflanzer219c6912017-09-23 19:22:51 +0100407 for(size_t d = 0; d < Coordinates::num_max_dimensions; ++d)
Moritz Pflanzera1848362017-08-25 12:30:03 +0100408 {
409 if(coord[d] < valid_region.start(d) || coord[d] >= valid_region.end(d))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100410 {
411 return false;
412 }
413 }
Moritz Pflanzera1848362017-08-25 12:30:03 +0100414
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100415 return true;
416}
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100417
418/** Create and initialize a tensor of the given type.
419 *
Sang-Hoon Park5ff38da2021-03-02 09:41:13 +0000420 * @param[in] info Tensor information to be used to create the tensor
421 * @param[in] ctx (Optional) Pointer to the runtime context.
422 *
423 * @return Initialized tensor of given type.
424 */
425template <typename T>
426inline T create_tensor(const TensorInfo &info, IRuntimeContext *ctx = nullptr)
427{
428 T tensor(ctx);
429 tensor.allocator()->init(info);
430 return tensor;
431}
432
433/** Create and initialize a tensor of the given type.
434 *
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100435 * @param[in] shape Tensor shape.
436 * @param[in] data_type Data type.
437 * @param[in] num_channels (Optional) Number of channels.
438 * @param[in] quantization_info (Optional) Quantization info for asymmetric quantized types.
439 * @param[in] data_layout (Optional) Data layout. Default is NCHW.
Pablo Tellodb8485a2019-09-24 11:03:47 +0100440 * @param[in] ctx (Optional) Pointer to the runtime context.
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100441 *
442 * @return Initialized tensor of given type.
443 */
444template <typename T>
Chunosovd621bca2017-11-03 17:33:15 +0700445inline T create_tensor(const TensorShape &shape, DataType data_type, int num_channels = 1,
Pablo Tellodb8485a2019-09-24 11:03:47 +0100446 QuantizationInfo quantization_info = QuantizationInfo(), DataLayout data_layout = DataLayout::NCHW, IRuntimeContext *ctx = nullptr)
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100447{
Pablo Tellodb8485a2019-09-24 11:03:47 +0100448 T tensor(ctx);
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100449 TensorInfo info(shape, num_channels, data_type);
Chunosovd621bca2017-11-03 17:33:15 +0700450 info.set_quantization_info(quantization_info);
Michalis Spyroucf581f52018-03-02 10:25:59 +0000451 info.set_data_layout(data_layout);
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100452
Sang-Hoon Park5ff38da2021-03-02 09:41:13 +0000453 return create_tensor<T>(info, ctx);
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100454}
SiCong Li3e363692017-07-04 15:02:10 +0100455
Ioan-Cristian Szabo2c350182017-12-20 16:27:37 +0000456/** Create and initialize a tensor of the given type.
457 *
458 * @param[in] shape Tensor shape.
459 * @param[in] format Format type.
Pablo Tellodb8485a2019-09-24 11:03:47 +0100460 * @param[in] ctx (Optional) Pointer to the runtime context.
Ioan-Cristian Szabo2c350182017-12-20 16:27:37 +0000461 *
462 * @return Initialized tensor of given type.
463 */
464template <typename T>
Pablo Tellodb8485a2019-09-24 11:03:47 +0100465inline T create_tensor(const TensorShape &shape, Format format, IRuntimeContext *ctx = nullptr)
Ioan-Cristian Szabo2c350182017-12-20 16:27:37 +0000466{
467 TensorInfo info(shape, format);
468
Sang-Hoon Park5ff38da2021-03-02 09:41:13 +0000469 return create_tensor<T>(info, ctx);
Ioan-Cristian Szabo2c350182017-12-20 16:27:37 +0000470}
471
John Richardson684cb0f2018-01-09 11:17:00 +0000472/** Create a vector with a uniform distribution of floating point values across the specified range.
473 *
474 * @param[in] num_values The number of values to be created.
475 * @param[in] min The minimum value in distribution (inclusive).
476 * @param[in] max The maximum value in distribution (inclusive).
477 * @param[in] seed The random seed to be used.
478 *
479 * @return A vector that contains the requested number of random floating point values
480 */
481template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
482inline std::vector<T> generate_random_real(unsigned int num_values, T min, T max, std::random_device::result_type seed)
483{
484 std::vector<T> v(num_values);
485 std::mt19937 gen(seed);
486 std::uniform_real_distribution<T> dist(min, max);
487
488 for(unsigned int i = 0; i < num_values; ++i)
489 {
490 v.at(i) = dist(gen);
491 }
492
493 return v;
494}
495
SiCong Li3e363692017-07-04 15:02:10 +0100496template <typename T, typename ArrayAccessor_T>
497inline void fill_array(ArrayAccessor_T &&array, const std::vector<T> &v)
498{
499 array.resize(v.size());
500 std::memcpy(array.buffer(), v.data(), v.size() * sizeof(T));
501}
SiCong Li86b53332017-08-23 11:02:43 +0100502
503/** Obtain numpy type string from DataType.
504 *
505 * @param[in] data_type Data type.
506 *
507 * @return numpy type string.
508 */
509inline std::string get_typestring(DataType data_type)
510{
511 // Check endianness
512 const unsigned int i = 1;
513 const char *c = reinterpret_cast<const char *>(&i);
514 std::string endianness;
515 if(*c == 1)
516 {
517 endianness = std::string("<");
518 }
519 else
520 {
521 endianness = std::string(">");
522 }
523 const std::string no_endianness("|");
524
525 switch(data_type)
526 {
527 case DataType::U8:
528 return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t));
529 case DataType::S8:
530 return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t));
531 case DataType::U16:
532 return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t));
533 case DataType::S16:
534 return endianness + "i" + support::cpp11::to_string(sizeof(int16_t));
535 case DataType::U32:
536 return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t));
537 case DataType::S32:
538 return endianness + "i" + support::cpp11::to_string(sizeof(int32_t));
539 case DataType::U64:
540 return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t));
541 case DataType::S64:
542 return endianness + "i" + support::cpp11::to_string(sizeof(int64_t));
543 case DataType::F32:
544 return endianness + "f" + support::cpp11::to_string(sizeof(float));
545 case DataType::F64:
546 return endianness + "f" + support::cpp11::to_string(sizeof(double));
547 case DataType::SIZET:
548 return endianness + "u" + support::cpp11::to_string(sizeof(size_t));
549 default:
550 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
551 }
552}
Joel Liang1c5ffd62017-12-28 10:09:51 +0800553
554/** Sync if necessary.
555 */
556template <typename TensorType>
557inline void sync_if_necessary()
558{
559#ifdef ARM_COMPUTE_CL
560 if(opencl_is_available() && std::is_same<typename std::decay<TensorType>::type, arm_compute::CLTensor>::value)
561 {
562 CLScheduler::get().sync();
563 }
564#endif /* ARM_COMPUTE_CL */
565}
566
567/** Sync tensor if necessary.
568 *
569 * @note: If the destination tensor not being used on OpenGL ES, GPU will optimize out the operation.
570 *
571 * @param[in] tensor Tensor to be sync.
572 */
573template <typename TensorType>
574inline void sync_tensor_if_necessary(TensorType &tensor)
575{
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100576 ARM_COMPUTE_UNUSED(tensor);
Joel Liang1c5ffd62017-12-28 10:09:51 +0800577}
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000578
579/** Construct and return object for dimensions' state filled with the given value
580 *
581 * @param[in] value The value to fill
582 *
583 * @return Constructed class
584 */
585inline ITensorInfo::TensorDimsState construct_dims_state(int32_t value)
586{
587 auto states = ITensorInfo::TensorDimsState{};
588 std::fill(states.begin(), states.end(), value);
589 return states;
590}
591
592/** Construct and return object for dimensions' state filled with the value for dynamic state
593 *
594 * @return Constructed class filled with the value for dynamic state
595 */
596inline ITensorInfo::TensorDimsState construct_dynamic_dims_state()
597{
598 return construct_dims_state(ITensorInfo::get_dynamic_state_value());
599}
600
601/** Construct and return object for dimensions' state filled with the value for non-dynamic state
602 *
603 * @return Constructed class filled with the value for non-dynamic state
604 */
605inline ITensorInfo::TensorDimsState construct_static_dims_state()
606{
607 return construct_dims_state(ITensorInfo::get_static_state_value());
608}
609
610/** Set the dimension states of the given tensor to dynamic
611 *
612 * @param[in] t The tensor to set to dynamic state
613 *
614 */
615template <typename TensorType>
616void set_tensor_dynamic(TensorType &t)
617{
618 t.info()->set_tensor_dims_state(construct_dynamic_dims_state());
619}
620
621/** Set the dimension states of the given tensor to state
622 *
623 * @param[in] t The tensor to set to static state
624 *
625 */
626template <typename TensorType>
627void set_tensor_static(TensorType &t)
628{
629 t.info()->set_tensor_dims_state(construct_static_dims_state());
630}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100631} // namespace test
632} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000633#endif /* ARM_COMPUTE_TEST_UTILS_H */