blob: 7b831468d35e734e2d9a432c68621fb7c60fc19d [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
SiCong Li5a63d1e2023-01-06 16:28:57 +00002 * Copyright (c) 2017-2023 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
SiCong Li5a63d1e2023-01-06 16:28:57 +000051#include "arm_compute/dynamic_fusion/sketch/attributes/Conv2dAttributes.h"
Georgios Pinitas12833d02019-07-25 13:31:10 +010052#include "arm_compute/runtime/CPP/CPPScheduler.h"
53#include "arm_compute/runtime/RuntimeContext.h"
54
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055namespace arm_compute
56{
Joel Liang1c5ffd62017-12-28 10:09:51 +080057#ifdef ARM_COMPUTE_CL
58class CLTensor;
59#endif /* ARM_COMPUTE_CL */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060namespace test
61{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062/** Round floating-point value with half value rounding to positive infinity.
63 *
64 * @param[in] value floating-point value to be rounded.
65 *
66 * @return Floating-point value of rounded @p value.
67 */
68template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
69inline T round_half_up(T value)
70{
71 return std::floor(value + 0.5f);
72}
73
74/** Round floating-point value with half value rounding to nearest even.
75 *
76 * @param[in] value floating-point value to be rounded.
77 * @param[in] epsilon precision.
78 *
79 * @return Floating-point value of rounded @p value.
80 */
81template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
82inline T round_half_even(T value, T epsilon = std::numeric_limits<T>::epsilon())
83{
84 T positive_value = std::abs(value);
85 T ipart = 0;
86 std::modf(positive_value, &ipart);
87 // If 'value' is exactly halfway between two integers
88 if(std::abs(positive_value - (ipart + 0.5f)) < epsilon)
89 {
90 // If 'ipart' is even then return 'ipart'
91 if(std::fmod(ipart, 2.f) < epsilon)
92 {
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010093 return support::cpp11::copysign(ipart, value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094 }
95 // Else return the nearest even integer
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010096 return support::cpp11::copysign(std::ceil(ipart + 0.5f), value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097 }
98 // Otherwise use the usual round to closest
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010099 return support::cpp11::copysign(support::cpp11::round(positive_value), value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101
102namespace traits
103{
104// *INDENT-OFF*
105// clang-format off
Alex Gildayc357c472018-03-21 13:54:09 +0000106/** Promote a type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107template <typename T> struct promote { };
Alex Gildayc357c472018-03-21 13:54:09 +0000108/** Promote uint8_t to uint16_t */
109template <> struct promote<uint8_t> { using type = uint16_t; /**< Promoted type */ };
110/** Promote int8_t to int16_t */
111template <> struct promote<int8_t> { using type = int16_t; /**< Promoted type */ };
112/** Promote uint16_t to uint32_t */
113template <> struct promote<uint16_t> { using type = uint32_t; /**< Promoted type */ };
114/** Promote int16_t to int32_t */
115template <> struct promote<int16_t> { using type = int32_t; /**< Promoted type */ };
116/** Promote uint32_t to uint64_t */
117template <> struct promote<uint32_t> { using type = uint64_t; /**< Promoted type */ };
118/** Promote int32_t to int64_t */
119template <> struct promote<int32_t> { using type = int64_t; /**< Promoted type */ };
120/** Promote float to float */
121template <> struct promote<float> { using type = float; /**< Promoted type */ };
122/** Promote half to half */
123template <> struct promote<half> { using type = half; /**< Promoted type */ };
Pablo Tello383deec2017-06-23 10:40:05 +0100124
Alex Gildayc357c472018-03-21 13:54:09 +0000125/** Get promoted type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126template <typename T>
127using promote_t = typename promote<T>::type;
128
129template <typename T>
130using 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 +0100131
132template <typename T>
133using make_unsigned_conditional_t = typename std::conditional<std::is_integral<T>::value, std::make_unsigned<T>, std::common_type<T>>::type;
134
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135// clang-format on
136// *INDENT-ON*
Ramy Elgammal73f19af2022-10-23 11:44:49 +0100137} // namespace traits
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138
139/** Look up the format corresponding to a channel.
140 *
141 * @param[in] channel Channel type.
142 *
143 * @return Format that contains the given channel.
144 */
145inline Format get_format_for_channel(Channel channel)
146{
147 switch(channel)
148 {
149 case Channel::R:
150 case Channel::G:
151 case Channel::B:
152 return Format::RGB888;
153 default:
154 throw std::runtime_error("Unsupported channel");
155 }
156}
157
158/** Return the format of a channel.
159 *
160 * @param[in] channel Channel type.
161 *
162 * @return Format of the given channel.
163 */
164inline Format get_channel_format(Channel channel)
165{
166 switch(channel)
167 {
168 case Channel::R:
169 case Channel::G:
170 case Channel::B:
171 return Format::U8;
172 default:
173 throw std::runtime_error("Unsupported channel");
174 }
175}
176
177/** Base case of foldl.
178 *
179 * @return value.
180 */
181template <typename F, typename T>
182inline T foldl(F &&, const T &value)
183{
184 return value;
185}
186
187/** Base case of foldl.
188 *
189 * @return func(value1, value2).
190 */
191template <typename F, typename T, typename U>
192inline auto foldl(F &&func, T &&value1, U &&value2) -> decltype(func(value1, value2))
193{
194 return func(value1, value2);
195}
196
197/** Fold left.
198 *
199 * @param[in] func Binary function to be called.
200 * @param[in] initial Initial value.
201 * @param[in] value Argument passed to the function.
202 * @param[in] values Remaining arguments.
203 */
204template <typename F, typename I, typename T, typename... Vs>
205inline I foldl(F &&func, I &&initial, T &&value, Vs &&... values)
206{
207 return foldl(std::forward<F>(func), func(std::forward<I>(initial), std::forward<T>(value)), std::forward<Vs>(values)...);
208}
209
SiCong Libacaf9a2017-06-19 13:41:45 +0100210/** Create a valid region based on tensor shape, border mode and border size
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211 *
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000212 * @param[in] a_shape Shape used as size of the valid region.
SiCong Libacaf9a2017-06-19 13:41:45 +0100213 * @param[in] border_undefined (Optional) Boolean indicating if the border mode is undefined.
214 * @param[in] border_size (Optional) Border size used to specify the region to exclude.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215 *
SiCong Libacaf9a2017-06-19 13:41:45 +0100216 * @return A valid region starting at (0, 0, ...) with size of @p shape if @p border_undefined is false; otherwise
217 * 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 +0100218 */
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000219inline 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 +0100220{
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000221 ValidRegion valid_region{ Coordinates(), a_shape };
222
223 Coordinates &anchor = valid_region.anchor;
224 TensorShape &shape = valid_region.shape;
Moritz Pflanzera1848362017-08-25 12:30:03 +0100225
SiCong Libacaf9a2017-06-19 13:41:45 +0100226 if(border_undefined)
227 {
228 ARM_COMPUTE_ERROR_ON(shape.num_dimensions() < 2);
Moritz Pflanzera1848362017-08-25 12:30:03 +0100229
SiCong Libacaf9a2017-06-19 13:41:45 +0100230 anchor.set(0, border_size.left);
231 anchor.set(1, border_size.top);
Moritz Pflanzera1848362017-08-25 12:30:03 +0100232
233 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));
234 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));
235
236 shape.set(0, valid_shape_x);
237 shape.set(1, valid_shape_y);
SiCong Libacaf9a2017-06-19 13:41:45 +0100238 }
Moritz Pflanzera1848362017-08-25 12:30:03 +0100239
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000240 return valid_region;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100241}
242
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100243/** Write the value after casting the pointer according to @p data_type.
244 *
245 * @warning The type of the value must match the specified data type.
246 *
247 * @param[out] ptr Pointer to memory where the @p value will be written.
248 * @param[in] value Value that will be written.
249 * @param[in] data_type Data type that will be written.
250 */
251template <typename T>
252void store_value_with_data_type(void *ptr, T value, DataType data_type)
253{
254 switch(data_type)
255 {
256 case DataType::U8:
Chunosovd621bca2017-11-03 17:33:15 +0700257 case DataType::QASYMM8:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100258 *reinterpret_cast<uint8_t *>(ptr) = value;
259 break;
260 case DataType::S8:
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100261 case DataType::QASYMM8_SIGNED:
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100262 case DataType::QSYMM8:
263 case DataType::QSYMM8_PER_CHANNEL:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100264 *reinterpret_cast<int8_t *>(ptr) = value;
265 break;
266 case DataType::U16:
Michele Di Giorgio35ea9a72019-08-23 12:02:06 +0100267 case DataType::QASYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100268 *reinterpret_cast<uint16_t *>(ptr) = value;
269 break;
270 case DataType::S16:
Manuel Bottini3689fcd2019-06-14 17:18:12 +0100271 case DataType::QSYMM16:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100272 *reinterpret_cast<int16_t *>(ptr) = value;
273 break;
274 case DataType::U32:
275 *reinterpret_cast<uint32_t *>(ptr) = value;
276 break;
277 case DataType::S32:
278 *reinterpret_cast<int32_t *>(ptr) = value;
279 break;
280 case DataType::U64:
281 *reinterpret_cast<uint64_t *>(ptr) = value;
282 break;
283 case DataType::S64:
284 *reinterpret_cast<int64_t *>(ptr) = value;
285 break;
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000286 case DataType::BFLOAT16:
287 *reinterpret_cast<bfloat16 *>(ptr) = bfloat16(value);
288 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100289 case DataType::F16:
Georgios Pinitas583137c2017-08-31 18:12:42 +0100290 *reinterpret_cast<half *>(ptr) = value;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100291 break;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100292 case DataType::F32:
293 *reinterpret_cast<float *>(ptr) = value;
294 break;
295 case DataType::F64:
296 *reinterpret_cast<double *>(ptr) = value;
297 break;
298 case DataType::SIZET:
299 *reinterpret_cast<size_t *>(ptr) = value;
300 break;
301 default:
302 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
303 }
304}
305
306/** Saturate a value of type T against the numeric limits of type U.
307 *
308 * @param[in] val Value to be saturated.
309 *
310 * @return saturated value.
311 */
312template <typename U, typename T>
313T saturate_cast(T val)
314{
315 if(val > static_cast<T>(std::numeric_limits<U>::max()))
316 {
317 val = static_cast<T>(std::numeric_limits<U>::max());
318 }
319 if(val < static_cast<T>(std::numeric_limits<U>::lowest()))
320 {
321 val = static_cast<T>(std::numeric_limits<U>::lowest());
322 }
323 return val;
324}
325
326/** Find the signed promoted common type.
327 */
328template <typename... T>
329struct common_promoted_signed_type
330{
Alex Gildayc357c472018-03-21 13:54:09 +0000331 /** Common type */
332 using common_type = typename std::common_type<T...>::type;
333 /** Promoted type */
334 using promoted_type = traits::promote_t<common_type>;
335 /** Intermediate type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100336 using intermediate_type = typename traits::make_signed_conditional_t<promoted_type>::type;
337};
338
John Richardson3c5f9492017-10-04 15:27:37 +0100339/** Find the unsigned promoted common type.
340 */
341template <typename... T>
342struct common_promoted_unsigned_type
343{
Alex Gildayc357c472018-03-21 13:54:09 +0000344 /** Common type */
345 using common_type = typename std::common_type<T...>::type;
346 /** Promoted type */
347 using promoted_type = traits::promote_t<common_type>;
348 /** Intermediate type */
John Richardson3c5f9492017-10-04 15:27:37 +0100349 using intermediate_type = typename traits::make_unsigned_conditional_t<promoted_type>::type;
350};
351
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100352/** Convert a linear index into n-dimensional coordinates.
353 *
354 * @param[in] shape Shape of the n-dimensional tensor.
355 * @param[in] index Linear index specifying the i-th element.
356 *
357 * @return n-dimensional coordinates.
358 */
359inline Coordinates index2coord(const TensorShape &shape, int index)
360{
361 int num_elements = shape.total_size();
362
363 ARM_COMPUTE_ERROR_ON_MSG(index < 0 || index >= num_elements, "Index has to be in [0, num_elements]");
364 ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create coordinate from empty shape");
365
366 Coordinates coord{ 0 };
367
368 for(int d = shape.num_dimensions() - 1; d >= 0; --d)
369 {
370 num_elements /= shape[d];
371 coord.set(d, index / num_elements);
372 index %= num_elements;
373 }
374
375 return coord;
376}
377
378/** Linearise the given coordinate.
379 *
380 * Transforms the given coordinate into a linear offset in terms of
381 * elements.
382 *
383 * @param[in] shape Shape of the n-dimensional tensor.
384 * @param[in] coord The to be converted coordinate.
385 *
386 * @return Linear offset to the element.
387 */
388inline int coord2index(const TensorShape &shape, const Coordinates &coord)
389{
390 ARM_COMPUTE_ERROR_ON_MSG(shape.total_size() == 0, "Cannot get index from empty shape");
391 ARM_COMPUTE_ERROR_ON_MSG(coord.num_dimensions() == 0, "Cannot get index of empty coordinate");
392
393 int index = 0;
394 int dim_size = 1;
395
396 for(unsigned int i = 0; i < coord.num_dimensions(); ++i)
397 {
398 index += coord[i] * dim_size;
399 dim_size *= shape[i];
400 }
401
402 return index;
403}
404
405/** Check if a coordinate is within a valid region */
Moritz Pflanzera1848362017-08-25 12:30:03 +0100406inline bool is_in_valid_region(const ValidRegion &valid_region, Coordinates coord)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100407{
Moritz Pflanzer219c6912017-09-23 19:22:51 +0100408 for(size_t d = 0; d < Coordinates::num_max_dimensions; ++d)
Moritz Pflanzera1848362017-08-25 12:30:03 +0100409 {
410 if(coord[d] < valid_region.start(d) || coord[d] >= valid_region.end(d))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100411 {
412 return false;
413 }
414 }
Moritz Pflanzera1848362017-08-25 12:30:03 +0100415
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100416 return true;
417}
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100418
419/** Create and initialize a tensor of the given type.
420 *
Sang-Hoon Park5ff38da2021-03-02 09:41:13 +0000421 * @param[in] info Tensor information to be used to create the tensor
422 * @param[in] ctx (Optional) Pointer to the runtime context.
423 *
424 * @return Initialized tensor of given type.
425 */
426template <typename T>
427inline T create_tensor(const TensorInfo &info, IRuntimeContext *ctx = nullptr)
428{
429 T tensor(ctx);
430 tensor.allocator()->init(info);
431 return tensor;
432}
433
434/** Create and initialize a tensor of the given type.
435 *
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100436 * @param[in] shape Tensor shape.
437 * @param[in] data_type Data type.
438 * @param[in] num_channels (Optional) Number of channels.
439 * @param[in] quantization_info (Optional) Quantization info for asymmetric quantized types.
440 * @param[in] data_layout (Optional) Data layout. Default is NCHW.
Pablo Tellodb8485a2019-09-24 11:03:47 +0100441 * @param[in] ctx (Optional) Pointer to the runtime context.
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100442 *
443 * @return Initialized tensor of given type.
444 */
445template <typename T>
Chunosovd621bca2017-11-03 17:33:15 +0700446inline T create_tensor(const TensorShape &shape, DataType data_type, int num_channels = 1,
Pablo Tellodb8485a2019-09-24 11:03:47 +0100447 QuantizationInfo quantization_info = QuantizationInfo(), DataLayout data_layout = DataLayout::NCHW, IRuntimeContext *ctx = nullptr)
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100448{
Pablo Tellodb8485a2019-09-24 11:03:47 +0100449 T tensor(ctx);
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100450 TensorInfo info(shape, num_channels, data_type);
Chunosovd621bca2017-11-03 17:33:15 +0700451 info.set_quantization_info(quantization_info);
Michalis Spyroucf581f52018-03-02 10:25:59 +0000452 info.set_data_layout(data_layout);
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100453
Sang-Hoon Park5ff38da2021-03-02 09:41:13 +0000454 return create_tensor<T>(info, ctx);
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100455}
SiCong Li3e363692017-07-04 15:02:10 +0100456
Ioan-Cristian Szabo2c350182017-12-20 16:27:37 +0000457/** Create and initialize a tensor of the given type.
458 *
459 * @param[in] shape Tensor shape.
460 * @param[in] format Format type.
Pablo Tellodb8485a2019-09-24 11:03:47 +0100461 * @param[in] ctx (Optional) Pointer to the runtime context.
Ioan-Cristian Szabo2c350182017-12-20 16:27:37 +0000462 *
463 * @return Initialized tensor of given type.
464 */
465template <typename T>
Pablo Tellodb8485a2019-09-24 11:03:47 +0100466inline T create_tensor(const TensorShape &shape, Format format, IRuntimeContext *ctx = nullptr)
Ioan-Cristian Szabo2c350182017-12-20 16:27:37 +0000467{
468 TensorInfo info(shape, format);
469
Sang-Hoon Park5ff38da2021-03-02 09:41:13 +0000470 return create_tensor<T>(info, ctx);
Ioan-Cristian Szabo2c350182017-12-20 16:27:37 +0000471}
472
John Richardson684cb0f2018-01-09 11:17:00 +0000473/** Create a vector with a uniform distribution of floating point values across the specified range.
474 *
475 * @param[in] num_values The number of values to be created.
476 * @param[in] min The minimum value in distribution (inclusive).
477 * @param[in] max The maximum value in distribution (inclusive).
478 * @param[in] seed The random seed to be used.
479 *
480 * @return A vector that contains the requested number of random floating point values
481 */
482template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
483inline std::vector<T> generate_random_real(unsigned int num_values, T min, T max, std::random_device::result_type seed)
484{
485 std::vector<T> v(num_values);
486 std::mt19937 gen(seed);
487 std::uniform_real_distribution<T> dist(min, max);
488
489 for(unsigned int i = 0; i < num_values; ++i)
490 {
491 v.at(i) = dist(gen);
492 }
493
494 return v;
495}
496
SiCong Li3e363692017-07-04 15:02:10 +0100497template <typename T, typename ArrayAccessor_T>
498inline void fill_array(ArrayAccessor_T &&array, const std::vector<T> &v)
499{
500 array.resize(v.size());
501 std::memcpy(array.buffer(), v.data(), v.size() * sizeof(T));
502}
SiCong Li86b53332017-08-23 11:02:43 +0100503
504/** Obtain numpy type string from DataType.
505 *
506 * @param[in] data_type Data type.
507 *
508 * @return numpy type string.
509 */
510inline std::string get_typestring(DataType data_type)
511{
512 // Check endianness
513 const unsigned int i = 1;
514 const char *c = reinterpret_cast<const char *>(&i);
515 std::string endianness;
516 if(*c == 1)
517 {
518 endianness = std::string("<");
519 }
520 else
521 {
522 endianness = std::string(">");
523 }
524 const std::string no_endianness("|");
525
526 switch(data_type)
527 {
528 case DataType::U8:
529 return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t));
530 case DataType::S8:
531 return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t));
532 case DataType::U16:
533 return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t));
534 case DataType::S16:
535 return endianness + "i" + support::cpp11::to_string(sizeof(int16_t));
536 case DataType::U32:
537 return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t));
538 case DataType::S32:
539 return endianness + "i" + support::cpp11::to_string(sizeof(int32_t));
540 case DataType::U64:
541 return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t));
542 case DataType::S64:
543 return endianness + "i" + support::cpp11::to_string(sizeof(int64_t));
544 case DataType::F32:
545 return endianness + "f" + support::cpp11::to_string(sizeof(float));
546 case DataType::F64:
547 return endianness + "f" + support::cpp11::to_string(sizeof(double));
548 case DataType::SIZET:
549 return endianness + "u" + support::cpp11::to_string(sizeof(size_t));
550 default:
551 ARM_COMPUTE_ERROR("NOT SUPPORTED!");
552 }
553}
Joel Liang1c5ffd62017-12-28 10:09:51 +0800554
555/** Sync if necessary.
556 */
557template <typename TensorType>
558inline void sync_if_necessary()
559{
560#ifdef ARM_COMPUTE_CL
561 if(opencl_is_available() && std::is_same<typename std::decay<TensorType>::type, arm_compute::CLTensor>::value)
562 {
563 CLScheduler::get().sync();
564 }
565#endif /* ARM_COMPUTE_CL */
566}
567
568/** Sync tensor if necessary.
569 *
570 * @note: If the destination tensor not being used on OpenGL ES, GPU will optimize out the operation.
571 *
572 * @param[in] tensor Tensor to be sync.
573 */
574template <typename TensorType>
575inline void sync_tensor_if_necessary(TensorType &tensor)
576{
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100577 ARM_COMPUTE_UNUSED(tensor);
Joel Liang1c5ffd62017-12-28 10:09:51 +0800578}
Sang-Hoon Park668ccdc2021-02-03 10:32:59 +0000579
580/** Construct and return object for dimensions' state filled with the given value
581 *
582 * @param[in] value The value to fill
583 *
584 * @return Constructed class
585 */
586inline ITensorInfo::TensorDimsState construct_dims_state(int32_t value)
587{
588 auto states = ITensorInfo::TensorDimsState{};
589 std::fill(states.begin(), states.end(), value);
590 return states;
591}
592
593/** Construct and return object for dimensions' state filled with the value for dynamic state
594 *
595 * @return Constructed class filled with the value for dynamic state
596 */
597inline ITensorInfo::TensorDimsState construct_dynamic_dims_state()
598{
599 return construct_dims_state(ITensorInfo::get_dynamic_state_value());
600}
601
602/** Construct and return object for dimensions' state filled with the value for non-dynamic state
603 *
604 * @return Constructed class filled with the value for non-dynamic state
605 */
606inline ITensorInfo::TensorDimsState construct_static_dims_state()
607{
608 return construct_dims_state(ITensorInfo::get_static_state_value());
609}
610
611/** Set the dimension states of the given tensor to dynamic
612 *
613 * @param[in] t The tensor to set to dynamic state
614 *
615 */
616template <typename TensorType>
617void set_tensor_dynamic(TensorType &t)
618{
619 t.info()->set_tensor_dims_state(construct_dynamic_dims_state());
620}
621
622/** Set the dimension states of the given tensor to state
623 *
624 * @param[in] t The tensor to set to static state
625 *
626 */
627template <typename TensorType>
628void set_tensor_static(TensorType &t)
629{
630 t.info()->set_tensor_dims_state(construct_static_dims_state());
631}
Ramy Elgammal73f19af2022-10-23 11:44:49 +0100632
633inline experimental::dynamic_fusion::Conv2dAttributes convert_pad_stride_info_to_conv_attr(const PadStrideInfo &info, const Size2D &dialation)
634{
635 const Padding2D info_pad(info.pad_left(), info.pad_right(), info.pad_top(), info.pad_bottom());
636 const Size2D info_stride(info.stride().first, info.stride().second);
637 return arm_compute::experimental::dynamic_fusion::Conv2dAttributes().pad(info_pad).stride(info_stride).dilation(dialation);
638}
639
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100640} // namespace test
641} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000642#endif /* ARM_COMPUTE_TEST_UTILS_H */