blob: 2892fb3e4298c4477441ece04dfd6eb0d4ae6680 [file] [log] [blame]
Moritz Pflanzerc7d15032017-07-18 16:21:16 +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 */
24#ifndef __ARM_COMPUTE_TEST_VALIDATION_H__
25#define __ARM_COMPUTE_TEST_VALIDATION_H__
26
27#include "SimpleTensor.h"
28#include "arm_compute/core/FixedPoint.h"
29#include "arm_compute/core/Types.h"
30#include "framework/Asserts.h"
Moritz Pflanzer2ac50402017-07-24 15:52:54 +010031#include "framework/Exceptions.h"
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010032#include "tests/IAccessor.h"
33#include "tests/TypePrinter.h"
34#include "tests/Utils.h"
35
36#include <iomanip>
Moritz Pflanzerb9e9cff2017-07-27 15:15:55 +010037#include <ios>
Moritz Pflanzerc7d15032017-07-18 16:21:16 +010038#include <vector>
39
40namespace arm_compute
41{
42namespace test
43{
44namespace validation
45{
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010046/** Class reprensenting an absolute tolerance value. */
47template <typename T>
48class 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. */
80class RelativeTolerance
81{
82public:
83 /** Underlying type. */
84 using value_type = double;
85
86 /* Default constructor.
87 *
88 * Initialises the tolerance to 0.
89 */
90 RelativeTolerance() = default;
91
92 /** Constructor.
93 *
94 * @param[in] value Relative tolerance value.
95 */
96 explicit constexpr RelativeTolerance(value_type value)
97 : _value{ value }
98 {
99 }
100
101 /** Implicit conversion to the underlying type. */
102 constexpr operator value_type() const
103 {
104 return _value;
105 }
106
107private:
108 value_type _value{ 0 };
109};
110
111/** Print AbsoluteTolerance type. */
112template <typename T>
113inline ::std::ostream &operator<<(::std::ostream &os, const AbsoluteTolerance<T> &tolerance)
114{
115 os << static_cast<typename AbsoluteTolerance<T>::value_type>(tolerance);
116
117 return os;
118}
119
120/** Print RelativeTolerance type. */
121inline ::std::ostream &operator<<(::std::ostream &os, const RelativeTolerance &tolerance)
122{
123 os << static_cast<typename RelativeTolerance::value_type>(tolerance);
124
125 return os;
126}
127
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100128template <typename T>
129bool compare_dimensions(const Dimensions<T> &dimensions1, const Dimensions<T> &dimensions2)
130{
131 if(dimensions1.num_dimensions() != dimensions2.num_dimensions())
132 {
133 return false;
134 }
135
136 for(unsigned int i = 0; i < dimensions1.num_dimensions(); ++i)
137 {
138 if(dimensions1[i] != dimensions2[i])
139 {
140 return false;
141 }
142 }
143
144 return true;
145}
146
147/** Validate valid regions.
148 *
149 * - Dimensionality has to be the same.
150 * - Anchors have to match.
151 * - Shapes have to match.
152 */
153void validate(const arm_compute::ValidRegion &region, const arm_compute::ValidRegion &reference);
154
155/** Validate padding.
156 *
157 * Padding on all sides has to be the same.
158 */
159void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &reference);
160
161/** Validate tensors.
162 *
163 * - Dimensionality has to be the same.
164 * - All values have to match.
165 *
166 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
167 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
168 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
169 * other test cases.
170 */
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100171template <typename T, typename U = AbsoluteTolerance<T>>
172void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value = U(), float tolerance_number = 0.f);
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100173
174/** Validate tensors with valid region.
175 *
176 * - Dimensionality has to be the same.
177 * - All values have to match.
178 *
179 * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to
180 * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between
181 * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by
182 * other test cases.
183 */
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100184template <typename T, typename U = AbsoluteTolerance<T>>
185void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value = U(), float tolerance_number = 0.f);
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100186
187/** Validate tensors against constant value.
188 *
189 * - All values have to match.
190 */
191void validate(const IAccessor &tensor, const void *reference_value);
192
193/** Validate border against a constant value.
194 *
195 * - All border values have to match the specified value if mode is CONSTANT.
196 * - All border values have to be replicated if mode is REPLICATE.
197 * - Nothing is validated for mode UNDEFINED.
198 */
199void validate(const IAccessor &tensor, BorderSize border_size, const BorderMode &border_mode, const void *border_value);
200
201/** Validate classified labels against expected ones.
202 *
203 * - All values should match
204 */
205void validate(std::vector<unsigned int> classified_labels, std::vector<unsigned int> expected_labels);
206
207/** Validate float value.
208 *
209 * - All values should match
210 */
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100211template <typename T, typename U>
212void validate(T target, T reference, U tolerance = AbsoluteTolerance<T>());
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100213
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100214template <typename T>
215struct compare_base
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100216{
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100217 compare_base(typename T::value_type target, typename T::value_type reference, T tolerance = T(0))
218 : _target{ target }, _reference{ reference }, _tolerance{ tolerance }
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100219 {
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100220 }
221
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100222 typename T::value_type _target{};
223 typename T::value_type _reference{};
224 T _tolerance{};
225};
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100226
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100227template <typename T, typename U>
228struct compare;
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100229
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100230template <typename U>
231struct compare<AbsoluteTolerance<U>, U> : public compare_base<AbsoluteTolerance<U>>
232{
233 using compare_base<AbsoluteTolerance<U>>::compare_base;
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100234
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100235 operator bool()
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100236 {
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100237 if(!std::isfinite(this->_target) || !std::isfinite(this->_reference))
238 {
239 return false;
240 }
241 else if(this->_target == this->_reference)
242 {
243 return true;
244 }
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100245
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100246 return static_cast<U>(std::abs(this->_target - this->_reference)) <= static_cast<U>(this->_tolerance);
247 }
248};
249
250template <typename U>
251struct compare<RelativeTolerance, U> : public compare_base<RelativeTolerance>
252{
253 using compare_base<RelativeTolerance>::compare_base;
254
255 operator bool()
256 {
257 if(!std::isfinite(_target) || !std::isfinite(_reference))
258 {
259 return false;
260 }
261 else if(_target == _reference)
262 {
263 return true;
264 }
265
266 const double relative_change = std::abs(static_cast<double>(_target - _reference)) / _reference;
267
268 return relative_change <= _tolerance;
269 }
270};
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100271
272template <typename T, typename U>
273void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number)
274{
275 // Validate with valid region covering the entire shape
276 validate(tensor, reference, shape_to_valid_region(tensor.shape()), tolerance_value, tolerance_number);
277}
278
279template <typename T, typename U>
280void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number)
281{
282 int64_t num_mismatches = 0;
283 int64_t num_elements = 0;
284
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100285 ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS);
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100286 ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS);
Moritz Pflanzer62eada22017-07-28 14:02:55 +0100287
288 if(reference.format() != Format::UNKNOWN)
289 {
290 ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS);
291 }
292
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100293 ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels(), framework::LogLevel::ERRORS);
294 ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape()), framework::LogLevel::ERRORS);
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100295
296 const int min_elements = std::min(tensor.num_elements(), reference.num_elements());
297 const int min_channels = std::min(tensor.num_channels(), reference.num_channels());
298
299 // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ...
300 for(int element_idx = 0; element_idx < min_elements; ++element_idx)
301 {
302 const Coordinates id = index2coord(reference.shape(), element_idx);
303
304 if(is_in_valid_region(valid_region, id))
305 {
306 // Iterate over all channels within one element
307 for(int c = 0; c < min_channels; ++c)
308 {
309 const T &target_value = reinterpret_cast<const T *>(tensor(id))[c];
310 const T &reference_value = reinterpret_cast<const T *>(reference(id))[c];
311
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100312 if(!compare<U, typename U::value_type>(target_value, reference_value, tolerance_value))
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100313 {
314 ARM_COMPUTE_TEST_INFO("id = " << id);
315 ARM_COMPUTE_TEST_INFO("channel = " << c);
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100316 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target_value));
317 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference_value));
318 ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value)));
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100319 ARM_COMPUTE_EXPECT_EQUAL(target_value, reference_value, framework::LogLevel::DEBUG);
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100320
321 ++num_mismatches;
322 }
323
324 ++num_elements;
325 }
326 }
327 }
328
329 if(num_elements > 0)
330 {
331 const int64_t absolute_tolerance_number = tolerance_number * num_elements;
332 const float percent_mismatches = static_cast<float>(num_mismatches) / num_elements * 100.f;
333
Moritz Pflanzerb9e9cff2017-07-27 15:15:55 +0100334 ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100335 << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)");
Moritz Pflanzer2ac50402017-07-24 15:52:54 +0100336 ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS);
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100337 }
338}
339
340template <typename T, typename U>
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100341void validate(T target, T reference, U tolerance)
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100342{
Moritz Pflanzer24a82462017-08-04 11:34:44 +0100343 ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << framework::make_printable(reference));
344 ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << framework::make_printable(target));
345 ARM_COMPUTE_TEST_INFO("tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance)));
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100346 ARM_COMPUTE_EXPECT((compare<U, typename U::value_type>(target, reference, tolerance)), framework::LogLevel::ERRORS);
Moritz Pflanzerc7d15032017-07-18 16:21:16 +0100347}
348} // namespace validation
349} // namespace test
350} // namespace arm_compute
351#endif /* __ARM_COMPUTE_TEST_REFERENCE_VALIDATION_H__ */