blob: a75562bac25da25d62f172d5a14215db14c09641 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Giorgio Arena68e29da2021-02-08 16:31:10 +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_VALIDATION_H
25#define ARM_COMPUTE_TEST_VALIDATION_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +010027#include "arm_compute/core/IArray.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/Types.h"
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +010029#include "support/ToolchainSupport.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010030#include "tests/IAccessor.h"
31#include "tests/SimpleTensor.h"
John Richardsonf89a49f2017-09-05 11:21:56 +010032#include "tests/Types.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010033#include "tests/Utils.h"
34#include "tests/framework/Asserts.h"
35#include "tests/framework/Exceptions.h"
Anthony Barbier2a07e182017-08-04 18:20:27 +010036#include "utils/TypePrinter.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010038#include <iomanip>
39#include <ios>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040#include <vector>
41
42namespace arm_compute
43{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044namespace test
45{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046namespace validation
47{
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010048/** Class reprensenting an absolute tolerance value. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010050class AbsoluteTolerance
51{
52public:
53 /** Underlying type. */
54 using value_type = T;
55
56 /* Default constructor.
57 *
58 * Initialises the tolerance to 0.
59 */
60 AbsoluteTolerance() = default;
61
62 /** Constructor.
63 *
64 * @param[in] value Absolute tolerance value.
65 */
66 explicit constexpr AbsoluteTolerance(T value)
67 : _value{ value }
68 {
69 }
70
Alex Gildayc357c472018-03-21 13:54:09 +000071 /** Implicit conversion to the underlying type.
72 *
73 * @return the underlying type.
74 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010075 constexpr operator T() const
76 {
77 return _value;
78 }
79
80private:
81 T _value{ std::numeric_limits<T>::epsilon() };
82};
83
84/** Class reprensenting a relative tolerance value. */
steniu013e05e4e2017-08-25 17:18:01 +010085template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010086class RelativeTolerance
87{
88public:
89 /** Underlying type. */
steniu013e05e4e2017-08-25 17:18:01 +010090 using value_type = T;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010091
92 /* Default constructor.
93 *
94 * Initialises the tolerance to 0.
95 */
96 RelativeTolerance() = default;
97
98 /** Constructor.
99 *
100 * @param[in] value Relative tolerance value.
101 */
102 explicit constexpr RelativeTolerance(value_type value)
103 : _value{ value }
104 {
105 }
106
Alex Gildayc357c472018-03-21 13:54:09 +0000107 /** Implicit conversion to the underlying type.
108 *
109 * @return the underlying type.
110 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100111 constexpr operator value_type() const
112 {
113 return _value;
114 }
115
116private:
steniu013e05e4e2017-08-25 17:18:01 +0100117 value_type _value{ std::numeric_limits<T>::epsilon() };
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100118};
119
120/** Print AbsoluteTolerance type. */
121template <typename T>
122inline ::std::ostream &operator<<(::std::ostream &os, const AbsoluteTolerance<T> &tolerance)
123{
124 os << static_cast<typename AbsoluteTolerance<T>::value_type>(tolerance);
125
126 return os;
127}
128
129/** Print RelativeTolerance type. */
steniu013e05e4e2017-08-25 17:18:01 +0100130template <typename T>
131inline ::std::ostream &operator<<(::std::ostream &os, const RelativeTolerance<T> &tolerance)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100132{
steniu013e05e4e2017-08-25 17:18:01 +0100133 os << static_cast<typename RelativeTolerance<T>::value_type>(tolerance);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100134
135 return os;
136}
137
138template <typename T>
Giorgio Arena563494c2018-04-30 17:29:41 +0100139bool compare_dimensions(const Dimensions<T> &dimensions1, const Dimensions<T> &dimensions2, const DataLayout &data_layout = DataLayout::NCHW)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140{
Giorgio Arena563494c2018-04-30 17:29:41 +0100141 ARM_COMPUTE_ERROR_ON(data_layout == DataLayout::UNKNOWN);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142
Giorgio Arena563494c2018-04-30 17:29:41 +0100143 if(data_layout == DataLayout::NCHW)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100145 if(dimensions1.num_dimensions() != dimensions2.num_dimensions())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100147 return false;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148 }
Giorgio Arena563494c2018-04-30 17:29:41 +0100149
150 for(unsigned int i = 0; i < dimensions1.num_dimensions(); ++i)
151 {
152 if(dimensions1[i] != dimensions2[i])
153 {
154 return false;
155 }
156 }
157 }
158 else
159 {
SiCong Li37d65e42020-11-06 09:55:04 +0000160 // 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
161 // clang-format off
162 if((dimensions1.num_dimensions() != dimensions2.num_dimensions()) &&
163 ((dimensions1.num_dimensions() != (dimensions2.num_dimensions() + 1)) || (dimensions1.x() != 1)) &&
164 ((dimensions1.num_dimensions() != (dimensions2.num_dimensions() + 2)) || (dimensions1.x() != 1) || (dimensions1.y() != 1)))
Giorgio Arena563494c2018-04-30 17:29:41 +0100165 {
166 return false;
167 }
SiCong Li37d65e42020-11-06 09:55:04 +0000168 // clang-format on
Giorgio Arena563494c2018-04-30 17:29:41 +0100169
170 if((dimensions1[0] != dimensions2[2]) || (dimensions1[1] != dimensions2[0]) || (dimensions1[2] != dimensions2[1]))
171 {
172 return false;
173 }
174
175 for(unsigned int i = 3; i < dimensions1.num_dimensions(); ++i)
176 {
177 if(dimensions1[i] != dimensions2[i])
178 {
179 return false;
180 }
181 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182 }
183
184 return true;
185}
186
187/** Validate valid regions.
188 *
189 * - Dimensionality has to be the same.
190 * - Anchors have to match.
191 * - Shapes have to match.
192 */
193void validate(const arm_compute::ValidRegion &region, const arm_compute::ValidRegion &reference);
194
195/** Validate padding.
196 *
197 * Padding on all sides has to be the same.
198 */
199void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &reference);
200
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000201/** Validate padding.
202 *
203 * Padding on all sides has to be the same.
204 */
205void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &width_reference, const arm_compute::PaddingSize &height_reference);
206
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100207/** Validate tensors.
208 *
209 * - Dimensionality has to be the same.
210 * - All values have to match.
211 *
212 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
213 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
214 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
215 * other test cases.
216 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100217template <typename T, typename U = AbsoluteTolerance<T>>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000218void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value = U(), float tolerance_number = 0.f, float absolute_tolerance_value = 0.f);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100219
220/** Validate tensors with valid region.
221 *
222 * - Dimensionality has to be the same.
223 * - All values have to match.
224 *
225 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
226 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
227 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
228 * other test cases.
229 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100230template <typename T, typename U = AbsoluteTolerance<T>>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000231void 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);
Isabella Gottardi62031532017-07-04 11:21:28 +0100232
Isabella Gottardi83be7452017-08-29 13:47:03 +0100233/** Validate tensors with valid mask.
234 *
235 * - Dimensionality has to be the same.
236 * - All values have to match.
237 *
238 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
239 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
240 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
241 * other test cases.
242 */
243template <typename T, typename U = AbsoluteTolerance<T>>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000244void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const SimpleTensor<T> &valid_mask, U tolerance_value = U(), float tolerance_number = 0.f,
245 float absolute_tolerance_value = 0.f);
Isabella Gottardi83be7452017-08-29 13:47:03 +0100246
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100247/** Validate tensors against constant value.
248 *
249 * - All values have to match.
250 */
251void validate(const IAccessor &tensor, const void *reference_value);
252
253/** Validate border against a constant value.
254 *
255 * - All border values have to match the specified value if mode is CONSTANT.
256 * - All border values have to be replicated if mode is REPLICATE.
257 * - Nothing is validated for mode UNDEFINED.
258 */
259void validate(const IAccessor &tensor, BorderSize border_size, const BorderMode &border_mode, const void *border_value);
260
261/** Validate classified labels against expected ones.
262 *
263 * - All values should match
264 */
265void validate(std::vector<unsigned int> classified_labels, std::vector<unsigned int> expected_labels);
steniu01960b0842017-06-23 11:44:34 +0100266
267/** Validate float value.
268 *
269 * - All values should match
270 */
Moritz Pflanzer7655a672017-09-23 11:57:33 +0100271template <typename T, typename U = AbsoluteTolerance<T>>
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100272bool validate(T target, T reference, U tolerance = AbsoluteTolerance<T>());
273
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100274template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100275struct compare_base
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100276{
Alex Gildayc357c472018-03-21 13:54:09 +0000277 /** Construct a comparison object.
278 *
279 * @param[in] target Target value.
280 * @param[in] reference Reference value.
281 * @param[in] tolerance Allowed tolerance.
282 */
John Richardson9c450cc2017-11-22 12:00:41 +0000283 compare_base(typename T::value_type target, typename T::value_type reference, T tolerance = T(0))
284 : _target{ target }, _reference{ reference }, _tolerance{ tolerance }
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100285 {
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100286 }
287
Alex Gildayc357c472018-03-21 13:54:09 +0000288 typename T::value_type _target{}; /**< Target value */
289 typename T::value_type _reference{}; /**< Reference value */
290 T _tolerance{}; /**< Tolerance value */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100291};
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100292
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100293template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100294struct compare;
295
Alex Gildayc357c472018-03-21 13:54:09 +0000296/** Compare values with an absolute tolerance */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100297template <typename U>
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100298struct compare<AbsoluteTolerance<U>> : public compare_base<AbsoluteTolerance<U>>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100299{
300 using compare_base<AbsoluteTolerance<U>>::compare_base;
301
Alex Gildayc357c472018-03-21 13:54:09 +0000302 /** Perform comparison */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100303 operator bool() const
304 {
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100305 if(!support::cpp11::isfinite(this->_target) || !support::cpp11::isfinite(this->_reference))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100306 {
307 return false;
308 }
309 else if(this->_target == this->_reference)
310 {
311 return true;
312 }
313
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100314 using comparison_type = typename std::conditional<std::is_integral<U>::value, int64_t, U>::type;
315
316 const comparison_type abs_difference(std::abs(static_cast<comparison_type>(this->_target) - static_cast<comparison_type>(this->_reference)));
317
318 return abs_difference <= static_cast<comparison_type>(this->_tolerance);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100319 }
320};
321
Alex Gildayc357c472018-03-21 13:54:09 +0000322/** Compare values with a relative tolerance */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100323template <typename U>
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100324struct compare<RelativeTolerance<U>> : public compare_base<RelativeTolerance<U>>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100325{
steniu013e05e4e2017-08-25 17:18:01 +0100326 using compare_base<RelativeTolerance<U>>::compare_base;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100327
Alex Gildayc357c472018-03-21 13:54:09 +0000328 /** Perform comparison */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100329 operator bool() const
330 {
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100331 if(!support::cpp11::isfinite(this->_target) || !support::cpp11::isfinite(this->_reference))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100332 {
333 return false;
334 }
steniu013e05e4e2017-08-25 17:18:01 +0100335 else if(this->_target == this->_reference)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100336 {
337 return true;
338 }
339
Moritz Pflanzerff1c3602017-09-22 12:41:25 +0100340 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);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100341
steniu013e05e4e2017-08-25 17:18:01 +0100342 if(std::abs(static_cast<double>(this->_reference) - static_cast<double>(this->_target)) <= epsilon)
343 {
344 return true;
345 }
346 else
347 {
348 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
349 {
350 return false;
351 }
352
Isabella Gottardi8df6c452018-03-12 13:26:28 +0000353 const double relative_change = std::abs((static_cast<double>(this->_target) - static_cast<double>(this->_reference)) / this->_reference);
steniu013e05e4e2017-08-25 17:18:01 +0100354
355 return relative_change <= static_cast<U>(this->_tolerance);
356 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100357 }
358};
359
360template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000361void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number, float absolute_tolerance_value)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100362{
363 // Validate with valid region covering the entire shape
Giorgio Arena563494c2018-04-30 17:29:41 +0100364 validate(tensor, reference, shape_to_valid_region(reference.shape()), tolerance_value, tolerance_number, absolute_tolerance_value);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100365}
366
John Richardson9c450cc2017-11-22 12:00:41 +0000367template <typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type>
John Richardsondd715f22017-09-18 16:10:48 +0100368void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number)
369{
370 // Validate with valid region covering the entire shape
Giorgio Arena563494c2018-04-30 17:29:41 +0100371 validate_wrap(tensor, reference, shape_to_valid_region(reference.shape()), tolerance_value, tolerance_number);
John Richardsondd715f22017-09-18 16:10:48 +0100372}
373
374template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000375void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number, float absolute_tolerance_value)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100376{
Giorgio Arena68e29da2021-02-08 16:31:10 +0000377 if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call())
378 {
379 return;
380 }
381
Michalis Spyroufae513c2019-10-16 17:41:33 +0100382 uint64_t num_mismatches = 0;
383 uint64_t num_elements = 0;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100384
385 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
386 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
387
388 if(reference.format() != Format::UNKNOWN)
389 {
390 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
391 }
392
393 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
Giorgio Arena563494c2018-04-30 17:29:41 +0100394 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100395
396 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
397 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
398
399 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
400 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
401 {
402 const Coordinates id = index2coord(reference.shape(), element_idx);
403
Giorgio Arena563494c2018-04-30 17:29:41 +0100404 Coordinates target_id(id);
405 if(tensor.data_layout() == DataLayout::NHWC)
406 {
407 permute(target_id, PermutationVector(2U, 0U, 1U));
408 }
409
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100410 if(is_in_valid_region(valid_region, id))
411 {
412 // Iterate over all channels within one element
413 for(int c = 0; c < min_channels; ++c)
414 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100415 const T &target_value = reinterpret_cast<const T *>(tensor(target_id))[c];
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100416 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
417
Michalis Spyrou23fe7c22018-02-26 10:42:01 +0000418 if(!compare<U>(target_value, reference_value, tolerance_value))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100419 {
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000420 if(absolute_tolerance_value != 0.f)
421 {
422 const AbsoluteTolerance<float> abs_tolerance(absolute_tolerance_value);
423 if(compare<AbsoluteTolerance<float>>(target_value, reference_value, abs_tolerance))
424 {
425 continue;
426 }
427 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100428 ARM_COMPUTE_TEST_INFO("id = " << id);
429 ARM_COMPUTE_TEST_INFO("channel = " << c);
430 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
431 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
432 ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value)));
steniu01172c58d2017-08-31 13:49:08 +0100433 framework::ARM_COMPUTE_PRINT_INFO();
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100434
435 ++num_mismatches;
436 }
437
438 ++num_elements;
439 }
440 }
441 }
442
Michalis Spyroufae513c2019-10-16 17:41:33 +0100443 if(num_elements != 0)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100444 {
Michalis Spyroufae513c2019-10-16 17:41:33 +0100445 const uint64_t absolute_tolerance_number = tolerance_number * num_elements;
446 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100447
448 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
Abe Mbise1b993382017-12-19 13:51:59 +0000449 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number * 100 << "%)");
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100450 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100451 }
452}
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100453
John Richardson9c450cc2017-11-22 12:00:41 +0000454template <typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type>
John Richardsondd715f22017-09-18 16:10:48 +0100455void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number)
456{
Giorgio Arena68e29da2021-02-08 16:31:10 +0000457 if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call())
458 {
459 return;
460 }
461
Michalis Spyroufae513c2019-10-16 17:41:33 +0100462 uint64_t num_mismatches = 0;
463 uint64_t num_elements = 0;
John Richardsondd715f22017-09-18 16:10:48 +0100464
465 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
466 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
467
468 if(reference.format() != Format::UNKNOWN)
469 {
470 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
471 }
472
473 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
Giorgio Arena563494c2018-04-30 17:29:41 +0100474 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS);
John Richardsondd715f22017-09-18 16:10:48 +0100475
476 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
477 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
478
479 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
480 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
481 {
482 const Coordinates id = index2coord(reference.shape(), element_idx);
483
Giorgio Arena563494c2018-04-30 17:29:41 +0100484 Coordinates target_id(id);
485 if(tensor.data_layout() == DataLayout::NHWC)
486 {
487 permute(target_id, PermutationVector(2U, 0U, 1U));
488 }
489
John Richardsondd715f22017-09-18 16:10:48 +0100490 if(is_in_valid_region(valid_region, id))
491 {
492 // Iterate over all channels within one element
493 for(int c = 0; c < min_channels; ++c)
494 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100495 const T &target_value = reinterpret_cast<const T *>(tensor(target_id))[c];
John Richardsondd715f22017-09-18 16:10:48 +0100496 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
497
498 bool equal = compare<U>(target_value, reference_value, tolerance_value);
499
John Richardson9c450cc2017-11-22 12:00:41 +0000500 // check for wrapping
John Richardsondd715f22017-09-18 16:10:48 +0100501 if(!equal)
502 {
John Richardson9c450cc2017-11-22 12:00:41 +0000503 if(!support::cpp11::isfinite(target_value) || !support::cpp11::isfinite(reference_value))
504 {
505 equal = false;
506 }
507 else
508 {
509 using limits_type = typename std::make_unsigned<T>::type;
510
511 uint64_t max = std::numeric_limits<limits_type>::max();
512 uint64_t abs_sum = std::abs(static_cast<int64_t>(target_value)) + std::abs(static_cast<int64_t>(reference_value));
513 uint64_t wrap_difference = max - abs_sum;
514
515 equal = wrap_difference < static_cast<uint64_t>(tolerance_value);
516 }
John Richardsondd715f22017-09-18 16:10:48 +0100517 }
518
519 if(!equal)
520 {
521 ARM_COMPUTE_TEST_INFO("id = " << id);
522 ARM_COMPUTE_TEST_INFO("channel = " << c);
523 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
524 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
John Richardson9c450cc2017-11-22 12:00:41 +0000525 ARM_COMPUTE_TEST_INFO("wrap_tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value)));
John Richardsondd715f22017-09-18 16:10:48 +0100526 framework::ARM_COMPUTE_PRINT_INFO();
527
528 ++num_mismatches;
529 }
530
531 ++num_elements;
532 }
533 }
534 }
535
Michalis Spyroufae513c2019-10-16 17:41:33 +0100536 if(num_elements != 0)
John Richardsondd715f22017-09-18 16:10:48 +0100537 {
Michalis Spyroufae513c2019-10-16 17:41:33 +0100538 const uint64_t absolute_tolerance_number = tolerance_number * num_elements;
539 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
John Richardsondd715f22017-09-18 16:10:48 +0100540
541 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
Abe Mbise1b993382017-12-19 13:51:59 +0000542 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number * 100 << "%)");
John Richardsondd715f22017-09-18 16:10:48 +0100543 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
544 }
545}
546
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100547template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000548void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const SimpleTensor<T> &valid_mask, U tolerance_value, float tolerance_number, float absolute_tolerance_value)
Isabella Gottardi83be7452017-08-29 13:47:03 +0100549{
Giorgio Arena68e29da2021-02-08 16:31:10 +0000550 if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call())
551 {
552 return;
553 }
554
Michalis Spyroufae513c2019-10-16 17:41:33 +0100555 uint64_t num_mismatches = 0;
556 uint64_t num_elements = 0;
Isabella Gottardi83be7452017-08-29 13:47:03 +0100557
558 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
559 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
560
561 if(reference.format() != Format::UNKNOWN)
562 {
563 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
564 }
565
566 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
Giorgio Arena563494c2018-04-30 17:29:41 +0100567 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS);
Isabella Gottardi83be7452017-08-29 13:47:03 +0100568
569 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
570 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
571
572 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
573 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
574 {
575 const Coordinates id = index2coord(reference.shape(), element_idx);
576
Giorgio Arena563494c2018-04-30 17:29:41 +0100577 Coordinates target_id(id);
578 if(tensor.data_layout() == DataLayout::NHWC)
579 {
580 permute(target_id, PermutationVector(2U, 0U, 1U));
581 }
582
Isabella Gottardi83be7452017-08-29 13:47:03 +0100583 if(valid_mask[element_idx] == 1)
584 {
585 // Iterate over all channels within one element
586 for(int c = 0; c < min_channels; ++c)
587 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100588 const T &target_value = reinterpret_cast<const T *>(tensor(target_id))[c];
Isabella Gottardi83be7452017-08-29 13:47:03 +0100589 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
590
591 if(!compare<U>(target_value, reference_value, tolerance_value))
592 {
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000593 if(absolute_tolerance_value != 0.f)
594 {
595 const AbsoluteTolerance<float> abs_tolerance(absolute_tolerance_value);
596 if(compare<AbsoluteTolerance<float>>(target_value, reference_value, abs_tolerance))
597 {
598 continue;
599 }
600 }
Isabella Gottardi83be7452017-08-29 13:47:03 +0100601 ARM_COMPUTE_TEST_INFO("id = " << id);
602 ARM_COMPUTE_TEST_INFO("channel = " << c);
603 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
604 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
605 ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value)));
606 framework::ARM_COMPUTE_PRINT_INFO();
607
608 ++num_mismatches;
609 }
610
611 ++num_elements;
612 }
613 }
614 else
615 {
616 ++num_elements;
617 }
618 }
619
Michalis Spyroufae513c2019-10-16 17:41:33 +0100620 if(num_elements != 0)
Isabella Gottardi83be7452017-08-29 13:47:03 +0100621 {
Michalis Spyroufae513c2019-10-16 17:41:33 +0100622 const uint64_t absolute_tolerance_number = tolerance_number * num_elements;
623 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
Isabella Gottardi83be7452017-08-29 13:47:03 +0100624
625 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
Abe Mbise1b993382017-12-19 13:51:59 +0000626 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number * 100 << "%)");
Isabella Gottardi83be7452017-08-29 13:47:03 +0100627 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
628 }
629}
630
631template <typename T, typename U>
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100632bool validate(T target, T reference, U tolerance)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100633{
Giorgio Arena68e29da2021-02-08 16:31:10 +0000634 if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call())
635 {
636 return true;
637 }
638
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100639 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference));
640 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target));
641 ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance)));
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100642
643 const bool equal = compare<U>(target, reference, tolerance);
644
645 ARM_COMPUTE_EXPECT(equal, framework::LogLevel::ERRORS);
646
647 return equal;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100648}
John Richardsonf89a49f2017-09-05 11:21:56 +0100649
650template <typename T, typename U>
651void validate_min_max_loc(const MinMaxLocationValues<T> &target, const MinMaxLocationValues<U> &reference)
652{
Giorgio Arena68e29da2021-02-08 16:31:10 +0000653 if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call())
654 {
655 return;
656 }
657
John Richardsonf89a49f2017-09-05 11:21:56 +0100658 ARM_COMPUTE_EXPECT_EQUAL(target.min, reference.min, framework::LogLevel::ERRORS);
659 ARM_COMPUTE_EXPECT_EQUAL(target.max, reference.max, framework::LogLevel::ERRORS);
660
661 ARM_COMPUTE_EXPECT_EQUAL(target.min_loc.size(), reference.min_loc.size(), framework::LogLevel::ERRORS);
662 ARM_COMPUTE_EXPECT_EQUAL(target.max_loc.size(), reference.max_loc.size(), framework::LogLevel::ERRORS);
663
664 for(uint32_t i = 0; i < target.min_loc.size(); ++i)
665 {
666 const auto same_coords = std::find_if(reference.min_loc.begin(), reference.min_loc.end(), [&target, i](Coordinates2D coord)
667 {
668 return coord.x == target.min_loc.at(i).x && coord.y == target.min_loc.at(i).y;
669 });
670
671 ARM_COMPUTE_EXPECT(same_coords != reference.min_loc.end(), framework::LogLevel::ERRORS);
672 }
673
674 for(uint32_t i = 0; i < target.max_loc.size(); ++i)
675 {
676 const auto same_coords = std::find_if(reference.max_loc.begin(), reference.max_loc.end(), [&target, i](Coordinates2D coord)
677 {
678 return coord.x == target.max_loc.at(i).x && coord.y == target.max_loc.at(i).y;
679 });
680
681 ARM_COMPUTE_EXPECT(same_coords != reference.max_loc.end(), framework::LogLevel::ERRORS);
682 }
683}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100684} // namespace validation
685} // namespace test
686} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000687#endif /* ARM_COMPUTE_TEST_REFERENCE_VALIDATION_H */