blob: 6bc42a4ed626819b0f18ccc93a8cf3c8be8b40ae [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010024#ifndef __ARM_COMPUTE_TEST_VALIDATION_H__
25#define __ARM_COMPUTE_TEST_VALIDATION_H__
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010027#include "arm_compute/core/FixedPoint.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/Types.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010029#include "tests/IAccessor.h"
30#include "tests/SimpleTensor.h"
31#include "tests/TypePrinter.h"
32#include "tests/Utils.h"
33#include "tests/framework/Asserts.h"
34#include "tests/framework/Exceptions.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010036#include <iomanip>
37#include <ios>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038#include <vector>
39
40namespace arm_compute
41{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042namespace test
43{
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044namespace validation
45{
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010046/** Class reprensenting an absolute tolerance value. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010048class AbsoluteTolerance
49{
50public:
51 /** Underlying type. */
52 using value_type = T;
53
54 /* Default constructor.
55 *
56 * Initialises the tolerance to 0.
57 */
58 AbsoluteTolerance() = default;
59
60 /** Constructor.
61 *
62 * @param[in] value Absolute tolerance value.
63 */
64 explicit constexpr AbsoluteTolerance(T value)
65 : _value{ value }
66 {
67 }
68
69 /** Implicit conversion to the underlying type. */
70 constexpr operator T() const
71 {
72 return _value;
73 }
74
75private:
76 T _value{ std::numeric_limits<T>::epsilon() };
77};
78
79/** Class reprensenting a relative tolerance value. */
steniu013e05e4e2017-08-25 17:18:01 +010080template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010081class RelativeTolerance
82{
83public:
84 /** Underlying type. */
steniu013e05e4e2017-08-25 17:18:01 +010085 using value_type = T;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010086
87 /* Default constructor.
88 *
89 * Initialises the tolerance to 0.
90 */
91 RelativeTolerance() = default;
92
93 /** Constructor.
94 *
95 * @param[in] value Relative tolerance value.
96 */
97 explicit constexpr RelativeTolerance(value_type value)
98 : _value{ value }
99 {
100 }
101
102 /** Implicit conversion to the underlying type. */
103 constexpr operator value_type() const
104 {
105 return _value;
106 }
107
108private:
steniu013e05e4e2017-08-25 17:18:01 +0100109 value_type _value{ std::numeric_limits<T>::epsilon() };
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100110};
111
112/** Print AbsoluteTolerance type. */
113template <typename T>
114inline ::std::ostream &operator<<(::std::ostream &os, const AbsoluteTolerance<T> &tolerance)
115{
116 os << static_cast<typename AbsoluteTolerance<T>::value_type>(tolerance);
117
118 return os;
119}
120
121/** Print RelativeTolerance type. */
steniu013e05e4e2017-08-25 17:18:01 +0100122template <typename T>
123inline ::std::ostream &operator<<(::std::ostream &os, const RelativeTolerance<T> &tolerance)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100124{
steniu013e05e4e2017-08-25 17:18:01 +0100125 os << static_cast<typename RelativeTolerance<T>::value_type>(tolerance);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100126
127 return os;
128}
129
130template <typename T>
131bool compare_dimensions(const Dimensions<T> &dimensions1, const Dimensions<T> &dimensions2)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132{
133 if(dimensions1.num_dimensions() != dimensions2.num_dimensions())
134 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100135 return false;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136 }
137
138 for(unsigned int i = 0; i < dimensions1.num_dimensions(); ++i)
139 {
140 if(dimensions1[i] != dimensions2[i])
141 {
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100142 return false;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143 }
144 }
145
146 return true;
147}
148
149/** Validate valid regions.
150 *
151 * - Dimensionality has to be the same.
152 * - Anchors have to match.
153 * - Shapes have to match.
154 */
155void validate(const arm_compute::ValidRegion &region, const arm_compute::ValidRegion &reference);
156
157/** Validate padding.
158 *
159 * Padding on all sides has to be the same.
160 */
161void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &reference);
162
163/** Validate tensors.
164 *
165 * - Dimensionality has to be the same.
166 * - All values have to match.
167 *
168 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
169 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
170 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
171 * other test cases.
172 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100173template <typename T, typename U = AbsoluteTolerance<T>>
174void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value = U(), float tolerance_number = 0.f);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100175
176/** Validate tensors with valid region.
177 *
178 * - Dimensionality has to be the same.
179 * - All values have to match.
180 *
181 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
182 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
183 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
184 * other test cases.
185 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100186template <typename T, typename U = AbsoluteTolerance<T>>
187void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value = U(), float tolerance_number = 0.f);
Isabella Gottardi62031532017-07-04 11:21:28 +0100188
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100189/** Validate tensors against constant value.
190 *
191 * - All values have to match.
192 */
193void validate(const IAccessor &tensor, const void *reference_value);
194
195/** Validate border against a constant value.
196 *
197 * - All border values have to match the specified value if mode is CONSTANT.
198 * - All border values have to be replicated if mode is REPLICATE.
199 * - Nothing is validated for mode UNDEFINED.
200 */
201void validate(const IAccessor &tensor, BorderSize border_size, const BorderMode &border_mode, const void *border_value);
202
203/** Validate classified labels against expected ones.
204 *
205 * - All values should match
206 */
207void validate(std::vector<unsigned int> classified_labels, std::vector<unsigned int> expected_labels);
steniu01960b0842017-06-23 11:44:34 +0100208
209/** Validate float value.
210 *
211 * - All values should match
212 */
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100213template <typename T, typename U>
214void validate(T target, T reference, U tolerance = AbsoluteTolerance<T>());
steniu01960b0842017-06-23 11:44:34 +0100215
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100216template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100217struct compare_base
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100218{
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100219 compare_base(typename T::value_type target, typename T::value_type reference, T tolerance = T(0))
220 : _target{ target }, _reference{ reference }, _tolerance{ tolerance }
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100221 {
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100222 }
223
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100224 typename T::value_type _target{};
225 typename T::value_type _reference{};
226 T _tolerance{};
227};
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100228
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100229template <typename T>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100230struct compare;
231
232template <typename U>
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100233struct compare<AbsoluteTolerance<U>> : public compare_base<AbsoluteTolerance<U>>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100234{
235 using compare_base<AbsoluteTolerance<U>>::compare_base;
236
237 operator bool() const
238 {
239 if(!std::isfinite(this->_target) || !std::isfinite(this->_reference))
240 {
241 return false;
242 }
243 else if(this->_target == this->_reference)
244 {
245 return true;
246 }
247
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100248 using comparison_type = typename std::conditional<std::is_integral<U>::value, int64_t, U>::type;
249
250 const comparison_type abs_difference(std::abs(static_cast<comparison_type>(this->_target) - static_cast<comparison_type>(this->_reference)));
251
252 return abs_difference <= static_cast<comparison_type>(this->_tolerance);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100253 }
254};
255
256template <typename U>
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100257struct compare<RelativeTolerance<U>> : public compare_base<RelativeTolerance<U>>
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100258{
steniu013e05e4e2017-08-25 17:18:01 +0100259 using compare_base<RelativeTolerance<U>>::compare_base;
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100260
261 operator bool() const
262 {
steniu013e05e4e2017-08-25 17:18:01 +0100263 if(!std::isfinite(this->_target) || !std::isfinite(this->_reference))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100264 {
265 return false;
266 }
steniu013e05e4e2017-08-25 17:18:01 +0100267 else if(this->_target == this->_reference)
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100268 {
269 return true;
270 }
271
steniu013e05e4e2017-08-25 17:18:01 +0100272 const U epsilon = (std::is_same<half_float::half, typename std::remove_cv<U>::type>::value || (this->_reference == 0)) ? static_cast<U>(0.01) : std::numeric_limits<U>::epsilon();
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100273
steniu013e05e4e2017-08-25 17:18:01 +0100274 if(std::abs(static_cast<double>(this->_reference) - static_cast<double>(this->_target)) <= epsilon)
275 {
276 return true;
277 }
278 else
279 {
280 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
281 {
282 return false;
283 }
284
285 const double relative_change = std::abs(static_cast<double>(this->_target) - static_cast<double>(this->_reference)) / this->_reference;
286
287 return relative_change <= static_cast<U>(this->_tolerance);
288 }
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100289 }
290};
291
292template <typename T, typename U>
293void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number)
294{
295 // Validate with valid region covering the entire shape
296 validate(tensor, reference, shape_to_valid_region(tensor.shape()), tolerance_value, tolerance_number);
297}
298
299template <typename T, typename U>
300void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number)
301{
302 int64_t num_mismatches = 0;
303 int64_t num_elements = 0;
304
305 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
306 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
307
308 if(reference.format() != Format::UNKNOWN)
309 {
310 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
311 }
312
313 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
314 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape()), framework::LogLevel::ERRORS);
315
316 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
317 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
318
319 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
320 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
321 {
322 const Coordinates id = index2coord(reference.shape(), element_idx);
323
324 if(is_in_valid_region(valid_region, id))
325 {
326 // Iterate over all channels within one element
327 for(int c = 0; c < min_channels; ++c)
328 {
329 const T &target_value = reinterpret_cast<const T *>(tensor(id))[c];
330 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
331
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100332 if(!compare<U>(target_value, reference_value, tolerance_value))
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100333 {
334 ARM_COMPUTE_TEST_INFO("id = " << id);
335 ARM_COMPUTE_TEST_INFO("channel = " << c);
336 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
337 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
338 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 +0100339 framework::ARM_COMPUTE_PRINT_INFO();
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100340
341 ++num_mismatches;
342 }
343
344 ++num_elements;
345 }
346 }
347 }
348
349 if(num_elements > 0)
350 {
351 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
352 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
353
354 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
355 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
356 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +0100357 }
358}
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100359
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100360template <typename T, typename U>
361void validate(T target, T reference, U tolerance)
362{
363 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference));
364 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target));
365 ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance)));
Moritz Pflanzer5b61fd32017-09-12 15:51:33 +0100366 ARM_COMPUTE_EXPECT((compare<U>(target, reference, tolerance)), framework::LogLevel::ERRORS);
Moritz Pflanzera09de0c2017-09-01 20:41:12 +0100367}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100368} // namespace validation
369} // namespace test
370} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100371#endif /* __ARM_COMPUTE_TEST_REFERENCE_VALIDATION_H__ */