blob: 7bad1a22860fe514a5a2e8a0ef14e012c2e83d8f [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{
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{
SiCongLic4270cf2021-12-22 11:22:40 +000054 return (!support::cpp11::isfinite(val0)) && (!support::cpp11::isfinite(val1)) && (std::signbit(val0) == std::signbit(val1));
SiCongLicb869562021-12-22 15:37:20 +000055}
56} // namespace
57
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010058/** Class reprensenting an absolute tolerance value. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010060class AbsoluteTolerance
61{
62public:
63 /** Underlying type. */
64 using value_type = T;
65
66 /* Default constructor.
67 *
68 * Initialises the tolerance to 0.
69 */
70 AbsoluteTolerance() = default;
71
72 /** Constructor.
73 *
74 * @param[in] value Absolute tolerance value.
75 */
76 explicit constexpr AbsoluteTolerance(T value)
77 : _value{ value }
78 {
79 }
80
Alex Gildayc357c472018-03-21 13:54:09 +000081 /** Implicit conversion to the underlying type.
82 *
83 * @return the underlying type.
84 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010085 constexpr operator T() const
86 {
87 return _value;
88 }
89
90private:
91 T _value{ std::numeric_limits<T>::epsilon() };
92};
93
94/** Class reprensenting a relative tolerance value. */
steniu013e05e4e2017-08-25 17:18:01 +010095template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010096class RelativeTolerance
97{
98public:
99 /** Underlying type. */
steniu013e05e4e2017-08-25 17:18:01 +0100100 using value_type = T;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100101
102 /* Default constructor.
103 *
104 * Initialises the tolerance to 0.
105 */
106 RelativeTolerance() = default;
107
108 /** Constructor.
109 *
110 * @param[in] value Relative tolerance value.
111 */
112 explicit constexpr RelativeTolerance(value_type value)
113 : _value{ value }
114 {
115 }
116
Alex Gildayc357c472018-03-21 13:54:09 +0000117 /** Implicit conversion to the underlying type.
118 *
119 * @return the underlying type.
120 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100121 constexpr operator value_type() const
122 {
123 return _value;
124 }
125
126private:
steniu013e05e4e2017-08-25 17:18:01 +0100127 value_type _value{ std::numeric_limits<T>::epsilon() };
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100128};
129
130/** Print AbsoluteTolerance type. */
131template <typename T>
132inline ::std::ostream &operator<<(::std::ostream &os, const AbsoluteTolerance<T> &tolerance)
133{
134 os << static_cast<typename AbsoluteTolerance<T>::value_type>(tolerance);
135
136 return os;
137}
138
139/** Print RelativeTolerance type. */
steniu013e05e4e2017-08-25 17:18:01 +0100140template <typename T>
141inline ::std::ostream &operator<<(::std::ostream &os, const RelativeTolerance<T> &tolerance)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100142{
steniu013e05e4e2017-08-25 17:18:01 +0100143 os << static_cast<typename RelativeTolerance<T>::value_type>(tolerance);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100144
145 return os;
146}
147
148template <typename T>
Giorgio Arena563494c2018-04-30 17:29:41 +0100149bool compare_dimensions(const Dimensions<T> &dimensions1, const Dimensions<T> &dimensions2, const DataLayout &data_layout = DataLayout::NCHW)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150{
Giorgio Arena563494c2018-04-30 17:29:41 +0100151 ARM_COMPUTE_ERROR_ON(data_layout == DataLayout::UNKNOWN);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152
Giorgio Arena5c002ec2021-10-12 16:00:40 +0100153 if(data_layout != DataLayout::NHWC)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100155 if(dimensions1.num_dimensions() != dimensions2.num_dimensions())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100157 return false;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158 }
Giorgio Arena563494c2018-04-30 17:29:41 +0100159
160 for(unsigned int i = 0; i < dimensions1.num_dimensions(); ++i)
161 {
162 if(dimensions1[i] != dimensions2[i])
163 {
164 return false;
165 }
166 }
167 }
168 else
169 {
SiCong Li37d65e42020-11-06 09:55:04 +0000170 // 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
171 // clang-format off
Gian Marco Iodice40471d12021-04-26 08:39:28 +0100172 const auto max_dims = std::max(dimensions1.num_dimensions(), dimensions2.num_dimensions());
173 for(unsigned int i = 3; i < max_dims; ++i)
Giorgio Arena563494c2018-04-30 17:29:41 +0100174 {
Gian Marco Iodice40471d12021-04-26 08:39:28 +0100175 if(dimensions1[i] != dimensions2[i])
176 {
177 return false;
178 }
Giorgio Arena563494c2018-04-30 17:29:41 +0100179 }
SiCong Li37d65e42020-11-06 09:55:04 +0000180 // clang-format on
Giorgio Arena563494c2018-04-30 17:29:41 +0100181
182 if((dimensions1[0] != dimensions2[2]) || (dimensions1[1] != dimensions2[0]) || (dimensions1[2] != dimensions2[1]))
183 {
184 return false;
185 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186 }
187
188 return true;
189}
190
191/** Validate valid regions.
192 *
193 * - Dimensionality has to be the same.
194 * - Anchors have to match.
195 * - Shapes have to match.
196 */
197void validate(const arm_compute::ValidRegion &region, const arm_compute::ValidRegion &reference);
198
199/** Validate padding.
200 *
201 * Padding on all sides has to be the same.
202 */
203void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &reference);
204
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000205/** Validate padding.
206 *
207 * Padding on all sides has to be the same.
208 */
209void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &width_reference, const arm_compute::PaddingSize &height_reference);
210
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211/** Validate tensors.
212 *
213 * - Dimensionality has to be the same.
214 * - All values have to match.
215 *
216 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
217 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
218 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
219 * other test cases.
220 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100221template <typename T, typename U = AbsoluteTolerance<T>>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000222void 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 +0100223
224/** Validate tensors with valid region.
225 *
226 * - Dimensionality has to be the same.
227 * - All values have to match.
228 *
229 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
230 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
231 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
232 * other test cases.
233 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100234template <typename T, typename U = AbsoluteTolerance<T>>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000235void 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 +0100236
Isabella Gottardi83be7452017-08-29 13:47:03 +0100237/** Validate tensors with valid mask.
238 *
239 * - Dimensionality has to be the same.
240 * - All values have to match.
241 *
242 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
243 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
244 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
245 * other test cases.
246 */
247template <typename T, typename U = AbsoluteTolerance<T>>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000248void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const SimpleTensor<T> &valid_mask, U tolerance_value = U(), float tolerance_number = 0.f,
249 float absolute_tolerance_value = 0.f);
Isabella Gottardi83be7452017-08-29 13:47:03 +0100250
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100251/** Validate tensors against constant value.
252 *
253 * - All values have to match.
254 */
255void validate(const IAccessor &tensor, const void *reference_value);
256
257/** Validate border against a constant value.
258 *
259 * - All border values have to match the specified value if mode is CONSTANT.
260 * - All border values have to be replicated if mode is REPLICATE.
261 * - Nothing is validated for mode UNDEFINED.
262 */
263void validate(const IAccessor &tensor, BorderSize border_size, const BorderMode &border_mode, const void *border_value);
264
265/** Validate classified labels against expected ones.
266 *
267 * - All values should match
268 */
269void validate(std::vector<unsigned int> classified_labels, std::vector<unsigned int> expected_labels);
steniu01960b0842017-06-23 11:44:34 +0100270
271/** Validate float value.
272 *
273 * - All values should match
274 */
Moritz Pflanzer7655a672017-09-23 11:57:33 +0100275template <typename T, typename U = AbsoluteTolerance<T>>
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100276bool validate(T target, T reference, U tolerance = AbsoluteTolerance<T>());
277
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100278template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100279struct compare_base
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100280{
Alex Gildayc357c472018-03-21 13:54:09 +0000281 /** Construct a comparison object.
282 *
283 * @param[in] target Target value.
284 * @param[in] reference Reference value.
285 * @param[in] tolerance Allowed tolerance.
286 */
John Richardson9c450cc2017-11-22 12:00:41 +0000287 compare_base(typename T::value_type target, typename T::value_type reference, T tolerance = T(0))
288 : _target{ target }, _reference{ reference }, _tolerance{ tolerance }
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100289 {
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100290 }
291
Alex Gildayc357c472018-03-21 13:54:09 +0000292 typename T::value_type _target{}; /**< Target value */
293 typename T::value_type _reference{}; /**< Reference value */
294 T _tolerance{}; /**< Tolerance value */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100295};
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100296
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100297template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100298struct compare;
299
Alex Gildayc357c472018-03-21 13:54:09 +0000300/** Compare values with an absolute tolerance */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100301template <typename U>
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100302struct compare<AbsoluteTolerance<U>> : public compare_base<AbsoluteTolerance<U>>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100303{
304 using compare_base<AbsoluteTolerance<U>>::compare_base;
305
Alex Gildayc357c472018-03-21 13:54:09 +0000306 /** Perform comparison */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100307 operator bool() const
308 {
SiCongLicb869562021-12-22 15:37:20 +0000309 if(are_equal_infs(this->_target, this->_reference))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100310 {
SiCongLicb869562021-12-22 15:37:20 +0000311 return true;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100312 }
313 else if(this->_target == this->_reference)
314 {
315 return true;
316 }
317
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100318 using comparison_type = typename std::conditional<std::is_integral<U>::value, int64_t, U>::type;
319
320 const comparison_type abs_difference(std::abs(static_cast<comparison_type>(this->_target) - static_cast<comparison_type>(this->_reference)));
321
322 return abs_difference <= static_cast<comparison_type>(this->_tolerance);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100323 }
324};
325
Alex Gildayc357c472018-03-21 13:54:09 +0000326/** Compare values with a relative tolerance */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100327template <typename U>
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100328struct compare<RelativeTolerance<U>> : public compare_base<RelativeTolerance<U>>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100329{
steniu013e05e4e2017-08-25 17:18:01 +0100330 using compare_base<RelativeTolerance<U>>::compare_base;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100331
Alex Gildayc357c472018-03-21 13:54:09 +0000332 /** Perform comparison */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100333 operator bool() const
334 {
SiCongLicb869562021-12-22 15:37:20 +0000335 if(are_equal_infs(this->_target, this->_reference))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100336 {
SiCongLicb869562021-12-22 15:37:20 +0000337 return true;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100338 }
steniu013e05e4e2017-08-25 17:18:01 +0100339 else if(this->_target == this->_reference)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100340 {
341 return true;
342 }
343
Moritz Pflanzerff1c3602017-09-22 12:41:25 +0100344 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 +0100345
steniu013e05e4e2017-08-25 17:18:01 +0100346 if(std::abs(static_cast<double>(this->_reference) - static_cast<double>(this->_target)) <= epsilon)
347 {
348 return true;
349 }
350 else
351 {
352 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
353 {
354 return false;
355 }
356
Isabella Gottardi8df6c452018-03-12 13:26:28 +0000357 const double relative_change = std::abs((static_cast<double>(this->_target) - static_cast<double>(this->_reference)) / this->_reference);
steniu013e05e4e2017-08-25 17:18:01 +0100358
359 return relative_change <= static_cast<U>(this->_tolerance);
360 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100361 }
362};
363
364template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000365void 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 +0100366{
367 // Validate with valid region covering the entire shape
Giorgio Arena563494c2018-04-30 17:29:41 +0100368 validate(tensor, reference, shape_to_valid_region(reference.shape()), tolerance_value, tolerance_number, absolute_tolerance_value);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100369}
370
John Richardson9c450cc2017-11-22 12:00:41 +0000371template <typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type>
John Richardsondd715f22017-09-18 16:10:48 +0100372void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number)
373{
374 // Validate with valid region covering the entire shape
Giorgio Arena563494c2018-04-30 17:29:41 +0100375 validate_wrap(tensor, reference, shape_to_valid_region(reference.shape()), tolerance_value, tolerance_number);
John Richardsondd715f22017-09-18 16:10:48 +0100376}
377
378template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000379void 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 +0100380{
Giorgio Arena68e29da2021-02-08 16:31:10 +0000381 if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call())
382 {
383 return;
384 }
385
Michalis Spyroufae513c2019-10-16 17:41:33 +0100386 uint64_t num_mismatches = 0;
387 uint64_t num_elements = 0;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100388
389 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
390 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
391
392 if(reference.format() != Format::UNKNOWN)
393 {
394 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
395 }
396
397 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
Giorgio Arena563494c2018-04-30 17:29:41 +0100398 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100399
400 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
401 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
402
403 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
404 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
405 {
406 const Coordinates id = index2coord(reference.shape(), element_idx);
407
Giorgio Arena563494c2018-04-30 17:29:41 +0100408 Coordinates target_id(id);
409 if(tensor.data_layout() == DataLayout::NHWC)
410 {
411 permute(target_id, PermutationVector(2U, 0U, 1U));
412 }
413
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100414 if(is_in_valid_region(valid_region, id))
415 {
416 // Iterate over all channels within one element
417 for(int c = 0; c < min_channels; ++c)
418 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100419 const T &target_value = reinterpret_cast<const T *>(tensor(target_id))[c];
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100420 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
421
Michalis Spyrou23fe7c22018-02-26 10:42:01 +0000422 if(!compare<U>(target_value, reference_value, tolerance_value))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100423 {
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000424 if(absolute_tolerance_value != 0.f)
425 {
426 const AbsoluteTolerance<float> abs_tolerance(absolute_tolerance_value);
427 if(compare<AbsoluteTolerance<float>>(target_value, reference_value, abs_tolerance))
428 {
429 continue;
430 }
431 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100432 ARM_COMPUTE_TEST_INFO("id = " << id);
433 ARM_COMPUTE_TEST_INFO("channel = " << c);
434 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
435 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
436 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 +0100437 framework::ARM_COMPUTE_PRINT_INFO();
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100438
439 ++num_mismatches;
440 }
441
442 ++num_elements;
443 }
444 }
445 }
446
Michalis Spyroufae513c2019-10-16 17:41:33 +0100447 if(num_elements != 0)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100448 {
Michalis Spyroufae513c2019-10-16 17:41:33 +0100449 const uint64_t absolute_tolerance_number = tolerance_number * num_elements;
450 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100451
452 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
Abe Mbise1b993382017-12-19 13:51:59 +0000453 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number * 100 << "%)");
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100454 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100455 }
456}
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100457
John Richardson9c450cc2017-11-22 12:00:41 +0000458template <typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type>
John Richardsondd715f22017-09-18 16:10:48 +0100459void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number)
460{
Giorgio Arena68e29da2021-02-08 16:31:10 +0000461 if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call())
462 {
463 return;
464 }
465
Michalis Spyroufae513c2019-10-16 17:41:33 +0100466 uint64_t num_mismatches = 0;
467 uint64_t num_elements = 0;
John Richardsondd715f22017-09-18 16:10:48 +0100468
469 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
470 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
471
472 if(reference.format() != Format::UNKNOWN)
473 {
474 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
475 }
476
477 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
Giorgio Arena563494c2018-04-30 17:29:41 +0100478 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS);
John Richardsondd715f22017-09-18 16:10:48 +0100479
480 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
481 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
482
483 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
484 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
485 {
486 const Coordinates id = index2coord(reference.shape(), element_idx);
487
Giorgio Arena563494c2018-04-30 17:29:41 +0100488 Coordinates target_id(id);
489 if(tensor.data_layout() == DataLayout::NHWC)
490 {
491 permute(target_id, PermutationVector(2U, 0U, 1U));
492 }
493
John Richardsondd715f22017-09-18 16:10:48 +0100494 if(is_in_valid_region(valid_region, id))
495 {
496 // Iterate over all channels within one element
497 for(int c = 0; c < min_channels; ++c)
498 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100499 const T &target_value = reinterpret_cast<const T *>(tensor(target_id))[c];
John Richardsondd715f22017-09-18 16:10:48 +0100500 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
501
502 bool equal = compare<U>(target_value, reference_value, tolerance_value);
503
John Richardson9c450cc2017-11-22 12:00:41 +0000504 // check for wrapping
John Richardsondd715f22017-09-18 16:10:48 +0100505 if(!equal)
506 {
SiCongLicb869562021-12-22 15:37:20 +0000507 if(are_equal_infs(target_value, reference_value))
John Richardson9c450cc2017-11-22 12:00:41 +0000508 {
SiCongLicb869562021-12-22 15:37:20 +0000509 equal = true;
John Richardson9c450cc2017-11-22 12:00:41 +0000510 }
511 else
512 {
513 using limits_type = typename std::make_unsigned<T>::type;
514
515 uint64_t max = std::numeric_limits<limits_type>::max();
516 uint64_t abs_sum = std::abs(static_cast<int64_t>(target_value)) + std::abs(static_cast<int64_t>(reference_value));
517 uint64_t wrap_difference = max - abs_sum;
518
519 equal = wrap_difference < static_cast<uint64_t>(tolerance_value);
520 }
John Richardsondd715f22017-09-18 16:10:48 +0100521 }
522
523 if(!equal)
524 {
525 ARM_COMPUTE_TEST_INFO("id = " << id);
526 ARM_COMPUTE_TEST_INFO("channel = " << c);
527 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
528 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
John Richardson9c450cc2017-11-22 12:00:41 +0000529 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 +0100530 framework::ARM_COMPUTE_PRINT_INFO();
531
532 ++num_mismatches;
533 }
534
535 ++num_elements;
536 }
537 }
538 }
539
Michalis Spyroufae513c2019-10-16 17:41:33 +0100540 if(num_elements != 0)
John Richardsondd715f22017-09-18 16:10:48 +0100541 {
Michalis Spyroufae513c2019-10-16 17:41:33 +0100542 const uint64_t absolute_tolerance_number = tolerance_number * num_elements;
543 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
John Richardsondd715f22017-09-18 16:10:48 +0100544
545 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
Abe Mbise1b993382017-12-19 13:51:59 +0000546 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number * 100 << "%)");
John Richardsondd715f22017-09-18 16:10:48 +0100547 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
548 }
549}
550
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100551template <typename T, typename U>
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000552void 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 +0100553{
Giorgio Arena68e29da2021-02-08 16:31:10 +0000554 if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call())
555 {
556 return;
557 }
558
Michalis Spyroufae513c2019-10-16 17:41:33 +0100559 uint64_t num_mismatches = 0;
560 uint64_t num_elements = 0;
Isabella Gottardi83be7452017-08-29 13:47:03 +0100561
562 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
563 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
564
565 if(reference.format() != Format::UNKNOWN)
566 {
567 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
568 }
569
570 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
Giorgio Arena563494c2018-04-30 17:29:41 +0100571 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS);
Isabella Gottardi83be7452017-08-29 13:47:03 +0100572
573 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
574 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
575
576 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
577 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
578 {
579 const Coordinates id = index2coord(reference.shape(), element_idx);
580
Giorgio Arena563494c2018-04-30 17:29:41 +0100581 Coordinates target_id(id);
582 if(tensor.data_layout() == DataLayout::NHWC)
583 {
584 permute(target_id, PermutationVector(2U, 0U, 1U));
585 }
586
Isabella Gottardi83be7452017-08-29 13:47:03 +0100587 if(valid_mask[element_idx] == 1)
588 {
589 // Iterate over all channels within one element
590 for(int c = 0; c < min_channels; ++c)
591 {
Giorgio Arena563494c2018-04-30 17:29:41 +0100592 const T &target_value = reinterpret_cast<const T *>(tensor(target_id))[c];
Isabella Gottardi83be7452017-08-29 13:47:03 +0100593 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
594
595 if(!compare<U>(target_value, reference_value, tolerance_value))
596 {
Michele Di Giorgioff6c2602018-02-26 15:22:16 +0000597 if(absolute_tolerance_value != 0.f)
598 {
599 const AbsoluteTolerance<float> abs_tolerance(absolute_tolerance_value);
600 if(compare<AbsoluteTolerance<float>>(target_value, reference_value, abs_tolerance))
601 {
602 continue;
603 }
604 }
Isabella Gottardi83be7452017-08-29 13:47:03 +0100605 ARM_COMPUTE_TEST_INFO("id = " << id);
606 ARM_COMPUTE_TEST_INFO("channel = " << c);
607 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
608 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
609 ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value)));
610 framework::ARM_COMPUTE_PRINT_INFO();
611
612 ++num_mismatches;
613 }
614
615 ++num_elements;
616 }
617 }
618 else
619 {
620 ++num_elements;
621 }
622 }
623
Michalis Spyroufae513c2019-10-16 17:41:33 +0100624 if(num_elements != 0)
Isabella Gottardi83be7452017-08-29 13:47:03 +0100625 {
Michalis Spyroufae513c2019-10-16 17:41:33 +0100626 const uint64_t absolute_tolerance_number = tolerance_number * num_elements;
627 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
Isabella Gottardi83be7452017-08-29 13:47:03 +0100628
629 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
Abe Mbise1b993382017-12-19 13:51:59 +0000630 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number * 100 << "%)");
Isabella Gottardi83be7452017-08-29 13:47:03 +0100631 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
632 }
633}
634
635template <typename T, typename U>
Moritz Pflanzer6c6597c2017-09-24 12:09:41 +0100636bool validate(T target, T reference, U tolerance)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100637{
Giorgio Arena68e29da2021-02-08 16:31:10 +0000638 if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call())
639 {
640 return true;
641 }
642
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100643 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference));
644 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target));
645 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 +0100646
647 const bool equal = compare<U>(target, reference, tolerance);
648
649 ARM_COMPUTE_EXPECT(equal, framework::LogLevel::ERRORS);
650
651 return equal;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100652}
John Richardsonf89a49f2017-09-05 11:21:56 +0100653
654template <typename T, typename U>
655void validate_min_max_loc(const MinMaxLocationValues<T> &target, const MinMaxLocationValues<U> &reference)
656{
Giorgio Arena68e29da2021-02-08 16:31:10 +0000657 if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call())
658 {
659 return;
660 }
661
John Richardsonf89a49f2017-09-05 11:21:56 +0100662 ARM_COMPUTE_EXPECT_EQUAL(target.min, reference.min, framework::LogLevel::ERRORS);
663 ARM_COMPUTE_EXPECT_EQUAL(target.max, reference.max, framework::LogLevel::ERRORS);
664
665 ARM_COMPUTE_EXPECT_EQUAL(target.min_loc.size(), reference.min_loc.size(), framework::LogLevel::ERRORS);
666 ARM_COMPUTE_EXPECT_EQUAL(target.max_loc.size(), reference.max_loc.size(), framework::LogLevel::ERRORS);
667
668 for(uint32_t i = 0; i < target.min_loc.size(); ++i)
669 {
670 const auto same_coords = std::find_if(reference.min_loc.begin(), reference.min_loc.end(), [&target, i](Coordinates2D coord)
671 {
672 return coord.x == target.min_loc.at(i).x && coord.y == target.min_loc.at(i).y;
673 });
674
675 ARM_COMPUTE_EXPECT(same_coords != reference.min_loc.end(), framework::LogLevel::ERRORS);
676 }
677
678 for(uint32_t i = 0; i < target.max_loc.size(); ++i)
679 {
680 const auto same_coords = std::find_if(reference.max_loc.begin(), reference.max_loc.end(), [&target, i](Coordinates2D coord)
681 {
682 return coord.x == target.max_loc.at(i).x && coord.y == target.max_loc.at(i).y;
683 });
684
685 ARM_COMPUTE_EXPECT(same_coords != reference.max_loc.end(), framework::LogLevel::ERRORS);
686 }
687}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100688} // namespace validation
689} // namespace test
690} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000691#endif /* ARM_COMPUTE_TEST_REFERENCE_VALIDATION_H */