blob: 289aca4d08f2c9fbaa8cf4f8342c4f5aba598512 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
SiCongLib99e54e2022-01-05 12:18:03 +00002 * Copyright (c) 2017-2022 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{
SiCongLicb869562021-12-22 15:37:20 +000048namespace
49{
50// Compare if 2 values are both infinities and if they are "equal" (has the same sign)
51template <typename T>
SiCongLic4270cf2021-12-22 11:22:40 +000052inline bool are_equal_infs(T val0, T val1)
SiCongLicb869562021-12-22 15:37:20 +000053{
SiCongLib99e54e2022-01-05 12:18:03 +000054 const auto same_sign = support::cpp11::signbit(val0) == support::cpp11::signbit(val1);
55 return (!support::cpp11::isfinite(val0)) && (!support::cpp11::isfinite(val1)) && same_sign;
SiCongLicb869562021-12-22 15:37:20 +000056}
57} // namespace
58
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010059/** Class reprensenting an absolute tolerance value. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010061class AbsoluteTolerance
62{
63public:
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 */
77 explicit constexpr AbsoluteTolerance(T value)
78 : _value{ value }
79 {
80 }
81
Alex Gildayc357c472018-03-21 13:54:09 +000082 /** Implicit conversion to the underlying type.
83 *
84 * @return the underlying type.
85 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010086 constexpr operator T() const
87 {
88 return _value;
89 }
90
91private:
92 T _value{ std::numeric_limits<T>::epsilon() };
93};
94
95/** Class reprensenting a relative tolerance value. */
steniu013e05e4e2017-08-25 17:18:01 +010096template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010097class RelativeTolerance
98{
99public:
100 /** Underlying type. */
steniu013e05e4e2017-08-25 17:18:01 +0100101 using value_type = T;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100102
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 */
113 explicit constexpr RelativeTolerance(value_type value)
114 : _value{ value }
115 {
116 }
117
Alex Gildayc357c472018-03-21 13:54:09 +0000118 /** Implicit conversion to the underlying type.
119 *
120 * @return the underlying type.
121 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100122 constexpr operator value_type() const
123 {
124 return _value;
125 }
126
127private:
steniu013e05e4e2017-08-25 17:18:01 +0100128 value_type _value{ std::numeric_limits<T>::epsilon() };
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100129};
130
131/** Print AbsoluteTolerance type. */
132template <typename T>
133inline ::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. */
steniu013e05e4e2017-08-25 17:18:01 +0100141template <typename T>
142inline ::std::ostream &operator<<(::std::ostream &os, const RelativeTolerance<T> &tolerance)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100143{
steniu013e05e4e2017-08-25 17:18:01 +0100144 os << static_cast<typename RelativeTolerance<T>::value_type>(tolerance);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100145
146 return os;
147}
148
149template <typename T>
Giorgio Arena563494c2018-04-30 17:29:41 +0100150bool compare_dimensions(const Dimensions<T> &dimensions1, const Dimensions<T> &dimensions2, const DataLayout &data_layout = DataLayout::NCHW)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151{
Giorgio Arena563494c2018-04-30 17:29:41 +0100152 ARM_COMPUTE_ERROR_ON(data_layout == DataLayout::UNKNOWN);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153
Giorgio Arena5c002ec2021-10-12 16:00:40 +0100154 if(data_layout != DataLayout::NHWC)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100156 if(dimensions1.num_dimensions() != dimensions2.num_dimensions())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100158 return false;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159 }
Giorgio Arena563494c2018-04-30 17:29:41 +0100160
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 {
SiCong Li37d65e42020-11-06 09:55:04 +0000171 // 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
Gian Marco Iodice40471d12021-04-26 08:39:28 +0100173 const auto max_dims = std::max(dimensions1.num_dimensions(), dimensions2.num_dimensions());
174 for(unsigned int i = 3; i < max_dims; ++i)
Giorgio Arena563494c2018-04-30 17:29:41 +0100175 {
Gian Marco Iodice40471d12021-04-26 08:39:28 +0100176 if(dimensions1[i] != dimensions2[i])
177 {
178 return false;
179 }
Giorgio Arena563494c2018-04-30 17:29:41 +0100180 }
SiCong Li37d65e42020-11-06 09:55:04 +0000181 // clang-format on
Giorgio Arena563494c2018-04-30 17:29:41 +0100182
183 if((dimensions1[0] != dimensions2[2]) || (dimensions1[1] != dimensions2[0]) || (dimensions1[2] != dimensions2[1]))
184 {
185 return false;
186 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187 }
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 */
198void 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 */
204void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &reference);
205
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000206/** Validate padding.
207 *
208 * Padding on all sides has to be the same.
209 */
210void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &width_reference, const arm_compute::PaddingSize &height_reference);
211
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100212/** 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 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100222template <typename T, typename U = AbsoluteTolerance<T>>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000223void 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 +0100224
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 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100235template <typename T, typename U = AbsoluteTolerance<T>>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000236void 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 +0100237
Isabella Gottardi83be7452017-08-29 13:47:03 +0100238/** 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 */
248template <typename T, typename U = AbsoluteTolerance<T>>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000249void 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);
Isabella Gottardi83be7452017-08-29 13:47:03 +0100251
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100252/** Validate tensors against constant value.
253 *
254 * - All values have to match.
255 */
256void 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 */
264void 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 */
270void validate(std::vector<unsigned int> classified_labels, std::vector<unsigned int> expected_labels);
steniu01960b0842017-06-23 11:44:34 +0100271
272/** Validate float value.
273 *
274 * - All values should match
275 */
Moritz Pflanzer7655a672017-09-23 11:57:33 +0100276template <typename T, typename U = AbsoluteTolerance<T>>
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100277bool validate(T target, T reference, U tolerance = AbsoluteTolerance<T>());
278
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100279template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100280struct compare_base
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100281{
Alex Gildayc357c472018-03-21 13:54:09 +0000282 /** Construct a comparison object.
283 *
284 * @param[in] target Target value.
285 * @param[in] reference Reference value.
286 * @param[in] tolerance Allowed tolerance.
287 */
John Richardson9c450cc2017-11-22 12:00:41 +0000288 compare_base(typename T::value_type target, typename T::value_type reference, T tolerance = T(0))
289 : _target{ target }, _reference{ reference }, _tolerance{ tolerance }
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100290 {
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100291 }
292
Alex Gildayc357c472018-03-21 13:54:09 +0000293 typename T::value_type _target{}; /**< Target value */
294 typename T::value_type _reference{}; /**< Reference value */
295 T _tolerance{}; /**< Tolerance value */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100296};
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100297
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100298template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100299struct compare;
300
Alex Gildayc357c472018-03-21 13:54:09 +0000301/** Compare values with an absolute tolerance */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100302template <typename U>
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100303struct compare<AbsoluteTolerance<U>> : public compare_base<AbsoluteTolerance<U>>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100304{
305 using compare_base<AbsoluteTolerance<U>>::compare_base;
306
Alex Gildayc357c472018-03-21 13:54:09 +0000307 /** Perform comparison */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100308 operator bool() const
309 {
SiCongLicb869562021-12-22 15:37:20 +0000310 if(are_equal_infs(this->_target, this->_reference))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100311 {
SiCongLicb869562021-12-22 15:37:20 +0000312 return true;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100313 }
314 else if(this->_target == this->_reference)
315 {
316 return true;
317 }
318
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100319 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);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100324 }
325};
326
Alex Gildayc357c472018-03-21 13:54:09 +0000327/** Compare values with a relative tolerance */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100328template <typename U>
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100329struct compare<RelativeTolerance<U>> : public compare_base<RelativeTolerance<U>>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100330{
steniu013e05e4e2017-08-25 17:18:01 +0100331 using compare_base<RelativeTolerance<U>>::compare_base;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100332
Alex Gildayc357c472018-03-21 13:54:09 +0000333 /** Perform comparison */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100334 operator bool() const
335 {
SiCongLicb869562021-12-22 15:37:20 +0000336 if(are_equal_infs(this->_target, this->_reference))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100337 {
SiCongLicb869562021-12-22 15:37:20 +0000338 return true;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100339 }
steniu013e05e4e2017-08-25 17:18:01 +0100340 else if(this->_target == this->_reference)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100341 {
342 return true;
343 }
344
Moritz Pflanzerff1c3602017-09-22 12:41:25 +0100345 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 +0100346
steniu013e05e4e2017-08-25 17:18:01 +0100347 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
Isabella Gottardi8df6c452018-03-12 13:26:28 +0000358 const double relative_change = std::abs((static_cast<double>(this->_target) - static_cast<double>(this->_reference)) / this->_reference);
steniu013e05e4e2017-08-25 17:18:01 +0100359
360 return relative_change <= static_cast<U>(this->_tolerance);
361 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100362 }
363};
364
365template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000366void 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 +0100367{
368 // Validate with valid region covering the entire shape
Giorgio Arena563494c2018-04-30 17:29:41 +0100369 validate(tensor, reference, shape_to_valid_region(reference.shape()), tolerance_value, tolerance_number, absolute_tolerance_value);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100370}
371
John Richardson9c450cc2017-11-22 12:00:41 +0000372template <typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type>
John Richardsondd715f22017-09-18 16:10:48 +0100373void 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
Giorgio Arena563494c2018-04-30 17:29:41 +0100376 validate_wrap(tensor, reference, shape_to_valid_region(reference.shape()), tolerance_value, tolerance_number);
John Richardsondd715f22017-09-18 16:10:48 +0100377}
378
379template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000380void 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 +0100381{
Giorgio Arena68e29da2021-02-08 16:31:10 +0000382 if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call())
383 {
384 return;
385 }
386
Michalis Spyroufae513c2019-10-16 17:41:33 +0100387 uint64_t num_mismatches = 0;
388 uint64_t num_elements = 0;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100389
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);
Giorgio Arena563494c2018-04-30 17:29:41 +0100399 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100400
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
Giorgio Arena563494c2018-04-30 17:29:41 +0100409 Coordinates target_id(id);
410 if(tensor.data_layout() == DataLayout::NHWC)
411 {
412 permute(target_id, PermutationVector(2U, 0U, 1U));
413 }
414
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100415 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 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100420 const T &target_value = reinterpret_cast<const T *>(tensor(target_id))[c];
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100421 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
422
Michalis Spyrou23fe7c22018-02-26 10:42:01 +0000423 if(!compare<U>(target_value, reference_value, tolerance_value))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100424 {
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000425 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 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100433 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)));
steniu01172c58d2017-08-31 13:49:08 +0100438 framework::ARM_COMPUTE_PRINT_INFO();
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100439
440 ++num_mismatches;
441 }
442
443 ++num_elements;
444 }
445 }
446 }
447
Michalis Spyroufae513c2019-10-16 17:41:33 +0100448 if(num_elements != 0)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100449 {
Michalis Spyroufae513c2019-10-16 17:41:33 +0100450 const uint64_t absolute_tolerance_number = tolerance_number * num_elements;
451 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100452
453 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
Abe Mbise1b993382017-12-19 13:51:59 +0000454 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number * 100 << "%)");
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100455 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100456 }
457}
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100458
John Richardson9c450cc2017-11-22 12:00:41 +0000459template <typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type>
John Richardsondd715f22017-09-18 16:10:48 +0100460void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number)
461{
Giorgio Arena68e29da2021-02-08 16:31:10 +0000462 if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call())
463 {
464 return;
465 }
466
Michalis Spyroufae513c2019-10-16 17:41:33 +0100467 uint64_t num_mismatches = 0;
468 uint64_t num_elements = 0;
John Richardsondd715f22017-09-18 16:10:48 +0100469
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);
Giorgio Arena563494c2018-04-30 17:29:41 +0100479 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS);
John Richardsondd715f22017-09-18 16:10:48 +0100480
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
Giorgio Arena563494c2018-04-30 17:29:41 +0100489 Coordinates target_id(id);
490 if(tensor.data_layout() == DataLayout::NHWC)
491 {
492 permute(target_id, PermutationVector(2U, 0U, 1U));
493 }
494
John Richardsondd715f22017-09-18 16:10:48 +0100495 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 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100500 const T &target_value = reinterpret_cast<const T *>(tensor(target_id))[c];
John Richardsondd715f22017-09-18 16:10:48 +0100501 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
502
503 bool equal = compare<U>(target_value, reference_value, tolerance_value);
504
John Richardson9c450cc2017-11-22 12:00:41 +0000505 // check for wrapping
John Richardsondd715f22017-09-18 16:10:48 +0100506 if(!equal)
507 {
SiCongLicb869562021-12-22 15:37:20 +0000508 if(are_equal_infs(target_value, reference_value))
John Richardson9c450cc2017-11-22 12:00:41 +0000509 {
SiCongLicb869562021-12-22 15:37:20 +0000510 equal = true;
John Richardson9c450cc2017-11-22 12:00:41 +0000511 }
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 }
John Richardsondd715f22017-09-18 16:10:48 +0100522 }
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));
John Richardson9c450cc2017-11-22 12:00:41 +0000530 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 +0100531 framework::ARM_COMPUTE_PRINT_INFO();
532
533 ++num_mismatches;
534 }
535
536 ++num_elements;
537 }
538 }
539 }
540
Michalis Spyroufae513c2019-10-16 17:41:33 +0100541 if(num_elements != 0)
John Richardsondd715f22017-09-18 16:10:48 +0100542 {
Michalis Spyroufae513c2019-10-16 17:41:33 +0100543 const uint64_t absolute_tolerance_number = tolerance_number * num_elements;
544 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
John Richardsondd715f22017-09-18 16:10:48 +0100545
546 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
Abe Mbise1b993382017-12-19 13:51:59 +0000547 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number * 100 << "%)");
John Richardsondd715f22017-09-18 16:10:48 +0100548 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
549 }
550}
551
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100552template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000553void 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 +0100554{
Giorgio Arena68e29da2021-02-08 16:31:10 +0000555 if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call())
556 {
557 return;
558 }
559
Michalis Spyroufae513c2019-10-16 17:41:33 +0100560 uint64_t num_mismatches = 0;
561 uint64_t num_elements = 0;
Isabella Gottardi83be7452017-08-29 13:47:03 +0100562
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);
Giorgio Arena563494c2018-04-30 17:29:41 +0100572 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS);
Isabella Gottardi83be7452017-08-29 13:47:03 +0100573
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
Giorgio Arena563494c2018-04-30 17:29:41 +0100582 Coordinates target_id(id);
583 if(tensor.data_layout() == DataLayout::NHWC)
584 {
585 permute(target_id, PermutationVector(2U, 0U, 1U));
586 }
587
Isabella Gottardi83be7452017-08-29 13:47:03 +0100588 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 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100593 const T &target_value = reinterpret_cast<const T *>(tensor(target_id))[c];
Isabella Gottardi83be7452017-08-29 13:47:03 +0100594 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
595
596 if(!compare<U>(target_value, reference_value, tolerance_value))
597 {
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000598 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 }
Isabella Gottardi83be7452017-08-29 13:47:03 +0100606 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
Michalis Spyroufae513c2019-10-16 17:41:33 +0100625 if(num_elements != 0)
Isabella Gottardi83be7452017-08-29 13:47:03 +0100626 {
Michalis Spyroufae513c2019-10-16 17:41:33 +0100627 const uint64_t absolute_tolerance_number = tolerance_number * num_elements;
628 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
Isabella Gottardi83be7452017-08-29 13:47:03 +0100629
630 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
Abe Mbise1b993382017-12-19 13:51:59 +0000631 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number * 100 << "%)");
Isabella Gottardi83be7452017-08-29 13:47:03 +0100632 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
633 }
634}
635
636template <typename T, typename U>
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100637bool validate(T target, T reference, U tolerance)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100638{
Giorgio Arena68e29da2021-02-08 16:31:10 +0000639 if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call())
640 {
641 return true;
642 }
643
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100644 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)));
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100647
648 const bool equal = compare<U>(target, reference, tolerance);
649
650 ARM_COMPUTE_EXPECT(equal, framework::LogLevel::ERRORS);
651
652 return equal;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100653}
John Richardsonf89a49f2017-09-05 11:21:56 +0100654
655template <typename T, typename U>
656void validate_min_max_loc(const MinMaxLocationValues<T> &target, const MinMaxLocationValues<U> &reference)
657{
Giorgio Arena68e29da2021-02-08 16:31:10 +0000658 if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call())
659 {
660 return;
661 }
662
John Richardsonf89a49f2017-09-05 11:21:56 +0100663 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}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100689} // namespace validation
690} // namespace test
691} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000692#endif /* ARM_COMPUTE_TEST_REFERENCE_VALIDATION_H */