xref: /aosp_15_r20/external/ComputeLibrary/tests/Utils.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2023 Arm Limited.
3  *
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/Size2D.h"
30 #include "arm_compute/core/TensorInfo.h"
31 #include "arm_compute/core/TensorShape.h"
32 #include "arm_compute/core/Types.h"
33 #include "support/StringSupport.h"
34 #include "support/ToolchainSupport.h"
35 
36 #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 
41 #include <cmath>
42 #include <cstddef>
43 #include <limits>
44 #include <memory>
45 #include <random>
46 #include <sstream>
47 #include <string>
48 #include <type_traits>
49 #include <vector>
50 
51 #include "arm_compute/dynamic_fusion/sketch/attributes/Conv2dAttributes.h"
52 #include "arm_compute/runtime/CPP/CPPScheduler.h"
53 #include "arm_compute/runtime/RuntimeContext.h"
54 
55 namespace arm_compute
56 {
57 #ifdef ARM_COMPUTE_CL
58 class CLTensor;
59 #endif /* ARM_COMPUTE_CL */
60 namespace test
61 {
62 /** 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  */
68 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
round_half_up(T value)69 inline 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  */
81 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
82 inline 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         {
93             return support::cpp11::copysign(ipart, value);
94         }
95         // Else return the nearest even integer
96         return support::cpp11::copysign(std::ceil(ipart + 0.5f), value);
97     }
98     // Otherwise use the usual round to closest
99     return support::cpp11::copysign(support::cpp11::round(positive_value), value);
100 }
101 
102 namespace traits
103 {
104 // *INDENT-OFF*
105 // clang-format off
106 /** Promote a type */
107 template <typename T> struct promote { };
108 /** Promote uint8_t to uint16_t */
109 template <> struct promote<uint8_t> { using type = uint16_t; /**< Promoted type */ };
110 /** Promote int8_t to int16_t */
111 template <> struct promote<int8_t> { using type = int16_t; /**< Promoted type */ };
112 /** Promote uint16_t to uint32_t */
113 template <> struct promote<uint16_t> { using type = uint32_t; /**< Promoted type */ };
114 /** Promote int16_t to int32_t */
115 template <> struct promote<int16_t> { using type = int32_t; /**< Promoted type */ };
116 /** Promote uint32_t to uint64_t */
117 template <> struct promote<uint32_t> { using type = uint64_t; /**< Promoted type */ };
118 /** Promote int32_t to int64_t */
119 template <> struct promote<int32_t> { using type = int64_t; /**< Promoted type */ };
120 /** Promote float to float */
121 template <> struct promote<float> { using type = float; /**< Promoted type */ };
122 /** Promote half to half */
123 template <> struct promote<half> { using type = half; /**< Promoted type */ };
124 
125 /** Get promoted type */
126 template <typename T>
127 using promote_t = typename promote<T>::type;
128 
129 template <typename T>
130 using make_signed_conditional_t = typename std::conditional<std::is_integral<T>::value, std::make_signed<T>, std::common_type<T>>::type;
131 
132 template <typename T>
133 using make_unsigned_conditional_t = typename std::conditional<std::is_integral<T>::value, std::make_unsigned<T>, std::common_type<T>>::type;
134 
135 // clang-format on
136 // *INDENT-ON*
137 } // namespace traits
138 
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  */
145 inline 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  */
164 inline 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  */
181 template <typename F, typename T>
182 inline T foldl(F &&, const T &value)
183 {
184     return value;
185 }
186 
187 /** Base case of foldl.
188  *
189  * @return func(value1, value2).
190  */
191 template <typename F, typename T, typename U>
192 inline 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  */
204 template <typename F, typename I, typename T, typename... Vs>
205 inline 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 
210 /** Create a valid region based on tensor shape, border mode and border size
211  *
212  * @param[in] a_shape          Shape used as size of the valid region.
213  * @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.
215  *
216  * @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.
218  */
219 inline ValidRegion shape_to_valid_region(const TensorShape &a_shape, bool border_undefined = false, BorderSize border_size = BorderSize(0))
220 {
221     ValidRegion valid_region{ Coordinates(), a_shape };
222 
223     Coordinates &anchor = valid_region.anchor;
224     TensorShape &shape  = valid_region.shape;
225 
226     if(border_undefined)
227     {
228         ARM_COMPUTE_ERROR_ON(shape.num_dimensions() < 2);
229 
230         anchor.set(0, border_size.left);
231         anchor.set(1, border_size.top);
232 
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);
238     }
239 
240     return valid_region;
241 }
242 
243 /** 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  */
251 template <typename T>
252 void store_value_with_data_type(void *ptr, T value, DataType data_type)
253 {
254     switch(data_type)
255     {
256         case DataType::U8:
257         case DataType::QASYMM8:
258             *reinterpret_cast<uint8_t *>(ptr) = value;
259             break;
260         case DataType::S8:
261         case DataType::QASYMM8_SIGNED:
262         case DataType::QSYMM8:
263         case DataType::QSYMM8_PER_CHANNEL:
264             *reinterpret_cast<int8_t *>(ptr) = value;
265             break;
266         case DataType::U16:
267         case DataType::QASYMM16:
268             *reinterpret_cast<uint16_t *>(ptr) = value;
269             break;
270         case DataType::S16:
271         case DataType::QSYMM16:
272             *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;
286         case DataType::BFLOAT16:
287             *reinterpret_cast<bfloat16 *>(ptr) = bfloat16(value);
288             break;
289         case DataType::F16:
290             *reinterpret_cast<half *>(ptr) = value;
291             break;
292         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  */
312 template <typename U, typename T>
313 T 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  */
328 template <typename... T>
329 struct common_promoted_signed_type
330 {
331     /** 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 */
336     using intermediate_type = typename traits::make_signed_conditional_t<promoted_type>::type;
337 };
338 
339 /** Find the unsigned promoted common type.
340  */
341 template <typename... T>
342 struct common_promoted_unsigned_type
343 {
344     /** 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 */
349     using intermediate_type = typename traits::make_unsigned_conditional_t<promoted_type>::type;
350 };
351 
352 /** 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  */
359 inline 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  */
388 inline 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 */
406 inline bool is_in_valid_region(const ValidRegion &valid_region, Coordinates coord)
407 {
408     for(size_t d = 0; d < Coordinates::num_max_dimensions; ++d)
409     {
410         if(coord[d] < valid_region.start(d) || coord[d] >= valid_region.end(d))
411         {
412             return false;
413         }
414     }
415 
416     return true;
417 }
418 
419 /** Create and initialize a tensor of the given type.
420  *
421  * @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  */
426 template <typename T>
427 inline 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  *
436  * @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.
441  * @param[in] ctx               (Optional) Pointer to the runtime context.
442  *
443  * @return Initialized tensor of given type.
444  */
445 template <typename T>
446 inline T create_tensor(const TensorShape &shape, DataType data_type, int num_channels = 1,
447                        QuantizationInfo quantization_info = QuantizationInfo(), DataLayout data_layout = DataLayout::NCHW, IRuntimeContext *ctx = nullptr)
448 {
449     T          tensor(ctx);
450     TensorInfo info(shape, num_channels, data_type);
451     info.set_quantization_info(quantization_info);
452     info.set_data_layout(data_layout);
453 
454     return create_tensor<T>(info, ctx);
455 }
456 
457 /** Create and initialize a tensor of the given type.
458  *
459  * @param[in] shape  Tensor shape.
460  * @param[in] format Format type.
461  * @param[in] ctx    (Optional) Pointer to the runtime context.
462  *
463  * @return Initialized tensor of given type.
464  */
465 template <typename T>
466 inline T create_tensor(const TensorShape &shape, Format format, IRuntimeContext *ctx = nullptr)
467 {
468     TensorInfo info(shape, format);
469 
470     return create_tensor<T>(info, ctx);
471 }
472 
473 /** 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  */
482 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
483 inline 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 
497 template <typename T, typename ArrayAccessor_T>
498 inline 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 }
503 
504 /** Obtain numpy type string from DataType.
505  *
506  * @param[in] data_type Data type.
507  *
508  * @return numpy type string.
509  */
510 inline 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 }
554 
555 /** Sync if necessary.
556  */
557 template <typename TensorType>
558 inline 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  */
574 template <typename TensorType>
575 inline void sync_tensor_if_necessary(TensorType &tensor)
576 {
577     ARM_COMPUTE_UNUSED(tensor);
578 }
579 
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  */
586 inline 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  */
597 inline 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  */
606 inline 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  */
616 template <typename TensorType>
617 void 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  */
627 template <typename TensorType>
628 void set_tensor_static(TensorType &t)
629 {
630     t.info()->set_tensor_dims_state(construct_static_dims_state());
631 }
632 
633 inline 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 
640 } // namespace test
641 } // namespace arm_compute
642 #endif /* ARM_COMPUTE_TEST_UTILS_H */
643