xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/Validation.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2022 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_VALIDATION_H
25 #define ARM_COMPUTE_TEST_VALIDATION_H
26 
27 #include "arm_compute/core/IArray.h"
28 #include "arm_compute/core/Types.h"
29 #include "support/ToolchainSupport.h"
30 #include "tests/IAccessor.h"
31 #include "tests/SimpleTensor.h"
32 #include "tests/Types.h"
33 #include "tests/Utils.h"
34 #include "tests/framework/Asserts.h"
35 #include "tests/framework/Exceptions.h"
36 #include "utils/TypePrinter.h"
37 
38 #include <iomanip>
39 #include <ios>
40 #include <vector>
41 
42 namespace arm_compute
43 {
44 namespace test
45 {
46 namespace validation
47 {
48 namespace
49 {
50 // Compare if 2 values are both infinities and if they are "equal" (has the same sign)
51 template <typename T>
are_equal_infs(T val0,T val1)52 inline bool are_equal_infs(T val0, T val1)
53 {
54     const auto same_sign = support::cpp11::signbit(val0) == support::cpp11::signbit(val1);
55     return (!support::cpp11::isfinite(val0)) && (!support::cpp11::isfinite(val1)) && same_sign;
56 }
57 } // namespace
58 
59 /** Class reprensenting an absolute tolerance value. */
60 template <typename T>
61 class AbsoluteTolerance
62 {
63 public:
64     /** Underlying type. */
65     using value_type = T;
66 
67     /* Default constructor.
68      *
69      * Initialises the tolerance to 0.
70      */
71     AbsoluteTolerance() = default;
72 
73     /** Constructor.
74      *
75      * @param[in] value Absolute tolerance value.
76      */
AbsoluteTolerance(T value)77     explicit constexpr AbsoluteTolerance(T value)
78         : _value{ value }
79     {
80     }
81 
82     /** Implicit conversion to the underlying type.
83      *
84      * @return the underlying type.
85      */
T()86     constexpr operator T() const
87     {
88         return _value;
89     }
90 
91 private:
92     T _value{ std::numeric_limits<T>::epsilon() };
93 };
94 
95 /** Class reprensenting a relative tolerance value. */
96 template <typename T>
97 class RelativeTolerance
98 {
99 public:
100     /** Underlying type. */
101     using value_type = T;
102 
103     /* Default constructor.
104      *
105      * Initialises the tolerance to 0.
106      */
107     RelativeTolerance() = default;
108 
109     /** Constructor.
110      *
111      * @param[in] value Relative tolerance value.
112      */
RelativeTolerance(value_type value)113     explicit constexpr RelativeTolerance(value_type value)
114         : _value{ value }
115     {
116     }
117 
118     /** Implicit conversion to the underlying type.
119      *
120      * @return the underlying type.
121      */
value_type()122     constexpr operator value_type() const
123     {
124         return _value;
125     }
126 
127 private:
128     value_type _value{ std::numeric_limits<T>::epsilon() };
129 };
130 
131 /** Print AbsoluteTolerance type. */
132 template <typename T>
133 inline ::std::ostream &operator<<(::std::ostream &os, const AbsoluteTolerance<T> &tolerance)
134 {
135     os << static_cast<typename AbsoluteTolerance<T>::value_type>(tolerance);
136 
137     return os;
138 }
139 
140 /** Print RelativeTolerance type. */
141 template <typename T>
142 inline ::std::ostream &operator<<(::std::ostream &os, const RelativeTolerance<T> &tolerance)
143 {
144     os << static_cast<typename RelativeTolerance<T>::value_type>(tolerance);
145 
146     return os;
147 }
148 
149 template <typename T>
150 bool compare_dimensions(const Dimensions<T> &dimensions1, const Dimensions<T> &dimensions2, const DataLayout &data_layout = DataLayout::NCHW)
151 {
152     ARM_COMPUTE_ERROR_ON(data_layout == DataLayout::UNKNOWN);
153 
154     if(data_layout != DataLayout::NHWC)
155     {
156         if(dimensions1.num_dimensions() != dimensions2.num_dimensions())
157         {
158             return false;
159         }
160 
161         for(unsigned int i = 0; i < dimensions1.num_dimensions(); ++i)
162         {
163             if(dimensions1[i] != dimensions2[i])
164             {
165                 return false;
166             }
167         }
168     }
169     else
170     {
171         // In case a 1D/2D shape becomes 3D after permutation, the permuted tensor will have two/one dimension(s) more and the first (two) value(s) will be 1
172         // clang-format off
173         const auto max_dims = std::max(dimensions1.num_dimensions(), dimensions2.num_dimensions());
174         for(unsigned int i = 3; i < max_dims; ++i)
175         {
176             if(dimensions1[i] != dimensions2[i])
177             {
178                 return false;
179             }
180         }
181         // clang-format on
182 
183         if((dimensions1[0] != dimensions2[2]) || (dimensions1[1] != dimensions2[0]) || (dimensions1[2] != dimensions2[1]))
184         {
185             return false;
186         }
187     }
188 
189     return true;
190 }
191 
192 /** Validate valid regions.
193  *
194  * - Dimensionality has to be the same.
195  * - Anchors have to match.
196  * - Shapes have to match.
197  */
198 void validate(const arm_compute::ValidRegion &region, const arm_compute::ValidRegion &reference);
199 
200 /** Validate padding.
201  *
202  * Padding on all sides has to be the same.
203  */
204 void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &reference);
205 
206 /** Validate padding.
207  *
208  * Padding on all sides has to be the same.
209  */
210 void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &width_reference, const arm_compute::PaddingSize &height_reference);
211 
212 /** Validate tensors.
213  *
214  * - Dimensionality has to be the same.
215  * - All values have to match.
216  *
217  * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
218  * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
219  * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
220  * other test cases.
221  */
222 template <typename T, typename U = AbsoluteTolerance<T>>
223 void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value = U(), float tolerance_number = 0.f, float absolute_tolerance_value = 0.f);
224 
225 /** Validate tensors with valid region.
226  *
227  * - Dimensionality has to be the same.
228  * - All values have to match.
229  *
230  * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
231  * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
232  * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
233  * other test cases.
234  */
235 template <typename T, typename U = AbsoluteTolerance<T>>
236 void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value = U(), float tolerance_number = 0.f, float absolute_tolerance_value = 0.f);
237 
238 /** Validate tensors with valid mask.
239  *
240  * - Dimensionality has to be the same.
241  * - All values have to match.
242  *
243  * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
244  * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
245  * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
246  * other test cases.
247  */
248 template <typename T, typename U = AbsoluteTolerance<T>>
249 void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const SimpleTensor<T> &valid_mask, U tolerance_value = U(), float tolerance_number = 0.f,
250               float absolute_tolerance_value = 0.f);
251 
252 /** Validate tensors against constant value.
253  *
254  * - All values have to match.
255  */
256 void validate(const IAccessor &tensor, const void *reference_value);
257 
258 /** Validate border against a constant value.
259  *
260  * - All border values have to match the specified value if mode is CONSTANT.
261  * - All border values have to be replicated if mode is REPLICATE.
262  * - Nothing is validated for mode UNDEFINED.
263  */
264 void validate(const IAccessor &tensor, BorderSize border_size, const BorderMode &border_mode, const void *border_value);
265 
266 /** Validate classified labels against expected ones.
267  *
268  * - All values should match
269  */
270 void validate(std::vector<unsigned int> classified_labels, std::vector<unsigned int> expected_labels);
271 
272 /** Validate float value.
273  *
274  * - All values should match
275  */
276 template <typename T, typename U = AbsoluteTolerance<T>>
277 bool validate(T target, T reference, U tolerance = AbsoluteTolerance<T>());
278 
279 template <typename T>
280 struct compare_base
281 {
282     /** Construct a comparison object.
283      *
284      * @param[in] target    Target value.
285      * @param[in] reference Reference value.
286      * @param[in] tolerance Allowed tolerance.
287      */
288     compare_base(typename T::value_type target, typename T::value_type reference, T tolerance = T(0))
289         : _target{ target }, _reference{ reference }, _tolerance{ tolerance }
290     {
291     }
292 
293     typename T::value_type _target{};    /**< Target value */
294     typename T::value_type _reference{}; /**< Reference value */
295     T                      _tolerance{}; /**< Tolerance value */
296 };
297 
298 template <typename T>
299 struct compare;
300 
301 /** Compare values with an absolute tolerance */
302 template <typename U>
303 struct compare<AbsoluteTolerance<U>> : public compare_base<AbsoluteTolerance<U>>
304 {
305     using compare_base<AbsoluteTolerance<U>>::compare_base;
306 
307     /** Perform comparison */
308     operator bool() const
309     {
310         if(are_equal_infs(this->_target, this->_reference))
311         {
312             return true;
313         }
314         else if(this->_target == this->_reference)
315         {
316             return true;
317         }
318 
319         using comparison_type = typename std::conditional<std::is_integral<U>::value, int64_t, U>::type;
320 
321         const comparison_type abs_difference(std::abs(static_cast<comparison_type>(this->_target) - static_cast<comparison_type>(this->_reference)));
322 
323         return abs_difference <= static_cast<comparison_type>(this->_tolerance);
324     }
325 };
326 
327 /** Compare values with a relative tolerance */
328 template <typename U>
329 struct compare<RelativeTolerance<U>> : public compare_base<RelativeTolerance<U>>
330 {
331     using compare_base<RelativeTolerance<U>>::compare_base;
332 
333     /** Perform comparison */
334     operator bool() const
335     {
336         if(are_equal_infs(this->_target, this->_reference))
337         {
338             return true;
339         }
340         else if(this->_target == this->_reference)
341         {
342             return true;
343         }
344 
345         const U epsilon = (std::is_same<half, typename std::remove_cv<U>::type>::value || (this->_reference == 0)) ? static_cast<U>(0.01) : static_cast<U>(1e-05);
346 
347         if(std::abs(static_cast<double>(this->_reference) - static_cast<double>(this->_target)) <= epsilon)
348         {
349             return true;
350         }
351         else
352         {
353             if(static_cast<double>(this->_reference) == 0.0f) // We have checked whether _reference and _target is closing. If _reference is 0 but not closed to _target, it should return false
354             {
355                 return false;
356             }
357 
358             const double relative_change = std::abs((static_cast<double>(this->_target) - static_cast<double>(this->_reference)) / this->_reference);
359 
360             return relative_change <= static_cast<U>(this->_tolerance);
361         }
362     }
363 };
364 
365 template <typename T, typename U>
366 void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number, float absolute_tolerance_value)
367 {
368     // Validate with valid region covering the entire shape
369     validate(tensor, reference, shape_to_valid_region(reference.shape()), tolerance_value, tolerance_number, absolute_tolerance_value);
370 }
371 
372 template <typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type>
373 void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number)
374 {
375     // Validate with valid region covering the entire shape
376     validate_wrap(tensor, reference, shape_to_valid_region(reference.shape()), tolerance_value, tolerance_number);
377 }
378 
379 template <typename T, typename U>
380 void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number, float absolute_tolerance_value)
381 {
382     if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call())
383     {
384         return;
385     }
386 
387     uint64_t num_mismatches = 0;
388     uint64_t num_elements   = 0;
389 
390     ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
391     ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
392 
393     if(reference.format() != Format::UNKNOWN)
394     {
395         ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
396     }
397 
398     ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
399     ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS);
400 
401     const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
402     const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
403 
404     // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
405     for(int element_idx = 0; element_idx < min_elements; ++element_idx)
406     {
407         const Coordinates id = index2coord(reference.shape(), element_idx);
408 
409         Coordinates target_id(id);
410         if(tensor.data_layout() == DataLayout::NHWC)
411         {
412             permute(target_id, PermutationVector(2U, 0U, 1U));
413         }
414 
415         if(is_in_valid_region(valid_region, id))
416         {
417             // Iterate over all channels within one element
418             for(int c = 0; c < min_channels; ++c)
419             {
420                 const T &target_value    = reinterpret_cast<const T *>(tensor(target_id))[c];
421                 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
422 
423                 if(!compare<U>(target_value, reference_value, tolerance_value))
424                 {
425                     if(absolute_tolerance_value != 0.f)
426                     {
427                         const AbsoluteTolerance<float> abs_tolerance(absolute_tolerance_value);
428                         if(compare<AbsoluteTolerance<float>>(target_value, reference_value, abs_tolerance))
429                         {
430                             continue;
431                         }
432                     }
433                     ARM_COMPUTE_TEST_INFO("id = " << id);
434                     ARM_COMPUTE_TEST_INFO("channel = " << c);
435                     ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
436                     ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
437                     ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value)));
438                     framework::ARM_COMPUTE_PRINT_INFO();
439 
440                     ++num_mismatches;
441                 }
442 
443                 ++num_elements;
444             }
445         }
446     }
447 
448     if(num_elements != 0)
449     {
450         const uint64_t absolute_tolerance_number = tolerance_number * num_elements;
451         const float    percent_mismatches        = static_cast<float>(num_mismatches) / num_elements * 100.f;
452 
453         ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
454                               << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number * 100 << "%)");
455         ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
456     }
457 }
458 
459 template <typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type>
460 void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number)
461 {
462     if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call())
463     {
464         return;
465     }
466 
467     uint64_t num_mismatches = 0;
468     uint64_t num_elements   = 0;
469 
470     ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
471     ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
472 
473     if(reference.format() != Format::UNKNOWN)
474     {
475         ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
476     }
477 
478     ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
479     ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS);
480 
481     const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
482     const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
483 
484     // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
485     for(int element_idx = 0; element_idx < min_elements; ++element_idx)
486     {
487         const Coordinates id = index2coord(reference.shape(), element_idx);
488 
489         Coordinates target_id(id);
490         if(tensor.data_layout() == DataLayout::NHWC)
491         {
492             permute(target_id, PermutationVector(2U, 0U, 1U));
493         }
494 
495         if(is_in_valid_region(valid_region, id))
496         {
497             // Iterate over all channels within one element
498             for(int c = 0; c < min_channels; ++c)
499             {
500                 const T &target_value    = reinterpret_cast<const T *>(tensor(target_id))[c];
501                 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
502 
503                 bool equal = compare<U>(target_value, reference_value, tolerance_value);
504 
505                 // check for wrapping
506                 if(!equal)
507                 {
508                     if(are_equal_infs(target_value, reference_value))
509                     {
510                         equal = true;
511                     }
512                     else
513                     {
514                         using limits_type = typename std::make_unsigned<T>::type;
515 
516                         uint64_t max             = std::numeric_limits<limits_type>::max();
517                         uint64_t abs_sum         = std::abs(static_cast<int64_t>(target_value)) + std::abs(static_cast<int64_t>(reference_value));
518                         uint64_t wrap_difference = max - abs_sum;
519 
520                         equal = wrap_difference < static_cast<uint64_t>(tolerance_value);
521                     }
522                 }
523 
524                 if(!equal)
525                 {
526                     ARM_COMPUTE_TEST_INFO("id = " << id);
527                     ARM_COMPUTE_TEST_INFO("channel = " << c);
528                     ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
529                     ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
530                     ARM_COMPUTE_TEST_INFO("wrap_tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value)));
531                     framework::ARM_COMPUTE_PRINT_INFO();
532 
533                     ++num_mismatches;
534                 }
535 
536                 ++num_elements;
537             }
538         }
539     }
540 
541     if(num_elements != 0)
542     {
543         const uint64_t absolute_tolerance_number = tolerance_number * num_elements;
544         const float    percent_mismatches        = static_cast<float>(num_mismatches) / num_elements * 100.f;
545 
546         ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
547                               << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number * 100 << "%)");
548         ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
549     }
550 }
551 
552 template <typename T, typename U>
553 void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const SimpleTensor<T> &valid_mask, U tolerance_value, float tolerance_number, float absolute_tolerance_value)
554 {
555     if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call())
556     {
557         return;
558     }
559 
560     uint64_t num_mismatches = 0;
561     uint64_t num_elements   = 0;
562 
563     ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
564     ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
565 
566     if(reference.format() != Format::UNKNOWN)
567     {
568         ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
569     }
570 
571     ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
572     ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS);
573 
574     const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
575     const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
576 
577     // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
578     for(int element_idx = 0; element_idx < min_elements; ++element_idx)
579     {
580         const Coordinates id = index2coord(reference.shape(), element_idx);
581 
582         Coordinates target_id(id);
583         if(tensor.data_layout() == DataLayout::NHWC)
584         {
585             permute(target_id, PermutationVector(2U, 0U, 1U));
586         }
587 
588         if(valid_mask[element_idx] == 1)
589         {
590             // Iterate over all channels within one element
591             for(int c = 0; c < min_channels; ++c)
592             {
593                 const T &target_value    = reinterpret_cast<const T *>(tensor(target_id))[c];
594                 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
595 
596                 if(!compare<U>(target_value, reference_value, tolerance_value))
597                 {
598                     if(absolute_tolerance_value != 0.f)
599                     {
600                         const AbsoluteTolerance<float> abs_tolerance(absolute_tolerance_value);
601                         if(compare<AbsoluteTolerance<float>>(target_value, reference_value, abs_tolerance))
602                         {
603                             continue;
604                         }
605                     }
606                     ARM_COMPUTE_TEST_INFO("id = " << id);
607                     ARM_COMPUTE_TEST_INFO("channel = " << c);
608                     ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
609                     ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
610                     ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value)));
611                     framework::ARM_COMPUTE_PRINT_INFO();
612 
613                     ++num_mismatches;
614                 }
615 
616                 ++num_elements;
617             }
618         }
619         else
620         {
621             ++num_elements;
622         }
623     }
624 
625     if(num_elements != 0)
626     {
627         const uint64_t absolute_tolerance_number = tolerance_number * num_elements;
628         const float    percent_mismatches        = static_cast<float>(num_mismatches) / num_elements * 100.f;
629 
630         ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
631                               << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number * 100 << "%)");
632         ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
633     }
634 }
635 
636 template <typename T, typename U>
637 bool validate(T target, T reference, U tolerance)
638 {
639     if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call())
640     {
641         return true;
642     }
643 
644     ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference));
645     ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target));
646     ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance)));
647 
648     const bool equal = compare<U>(target, reference, tolerance);
649 
650     ARM_COMPUTE_EXPECT(equal, framework::LogLevel::ERRORS);
651 
652     return equal;
653 }
654 
655 template <typename T, typename U>
656 void validate_min_max_loc(const MinMaxLocationValues<T> &target, const MinMaxLocationValues<U> &reference)
657 {
658     if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call())
659     {
660         return;
661     }
662 
663     ARM_COMPUTE_EXPECT_EQUAL(target.min, reference.min, framework::LogLevel::ERRORS);
664     ARM_COMPUTE_EXPECT_EQUAL(target.max, reference.max, framework::LogLevel::ERRORS);
665 
666     ARM_COMPUTE_EXPECT_EQUAL(target.min_loc.size(), reference.min_loc.size(), framework::LogLevel::ERRORS);
667     ARM_COMPUTE_EXPECT_EQUAL(target.max_loc.size(), reference.max_loc.size(), framework::LogLevel::ERRORS);
668 
669     for(uint32_t i = 0; i < target.min_loc.size(); ++i)
670     {
671         const auto same_coords = std::find_if(reference.min_loc.begin(), reference.min_loc.end(), [&target, i](Coordinates2D coord)
672         {
673             return coord.x == target.min_loc.at(i).x && coord.y == target.min_loc.at(i).y;
674         });
675 
676         ARM_COMPUTE_EXPECT(same_coords != reference.min_loc.end(), framework::LogLevel::ERRORS);
677     }
678 
679     for(uint32_t i = 0; i < target.max_loc.size(); ++i)
680     {
681         const auto same_coords = std::find_if(reference.max_loc.begin(), reference.max_loc.end(), [&target, i](Coordinates2D coord)
682         {
683             return coord.x == target.max_loc.at(i).x && coord.y == target.max_loc.at(i).y;
684         });
685 
686         ARM_COMPUTE_EXPECT(same_coords != reference.max_loc.end(), framework::LogLevel::ERRORS);
687     }
688 }
689 } // namespace validation
690 } // namespace test
691 } // namespace arm_compute
692 #endif /* ARM_COMPUTE_TEST_REFERENCE_VALIDATION_H */
693