Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
SiCongLi | b99e54e | 2022-01-05 12:18:03 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2022 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_TEST_VALIDATION_H |
| 25 | #define ARM_COMPUTE_TEST_VALIDATION_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/IArray.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Types.h" |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 29 | #include "support/ToolchainSupport.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 30 | #include "tests/IAccessor.h" |
| 31 | #include "tests/SimpleTensor.h" |
John Richardson | f89a49f | 2017-09-05 11:21:56 +0100 | [diff] [blame] | 32 | #include "tests/Types.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 33 | #include "tests/Utils.h" |
| 34 | #include "tests/framework/Asserts.h" |
| 35 | #include "tests/framework/Exceptions.h" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 36 | #include "utils/TypePrinter.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 38 | #include <iomanip> |
| 39 | #include <ios> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 40 | #include <vector> |
| 41 | |
| 42 | namespace arm_compute |
| 43 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | namespace test |
| 45 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | namespace validation |
| 47 | { |
SiCongLi | cb86956 | 2021-12-22 15:37:20 +0000 | [diff] [blame] | 48 | namespace |
| 49 | { |
| 50 | // Compare if 2 values are both infinities and if they are "equal" (has the same sign) |
| 51 | template <typename T> |
SiCongLi | c4270cf | 2021-12-22 11:22:40 +0000 | [diff] [blame] | 52 | inline bool are_equal_infs(T val0, T val1) |
SiCongLi | cb86956 | 2021-12-22 15:37:20 +0000 | [diff] [blame] | 53 | { |
SiCongLi | b99e54e | 2022-01-05 12:18:03 +0000 | [diff] [blame] | 54 | const auto same_sign = support::cpp11::signbit(val0) == support::cpp11::signbit(val1); |
| 55 | return (!support::cpp11::isfinite(val0)) && (!support::cpp11::isfinite(val1)) && same_sign; |
SiCongLi | cb86956 | 2021-12-22 15:37:20 +0000 | [diff] [blame] | 56 | } |
| 57 | } // namespace |
| 58 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 59 | /** Class reprensenting an absolute tolerance value. */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 60 | template <typename T> |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 61 | class AbsoluteTolerance |
| 62 | { |
| 63 | public: |
| 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 Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 82 | /** Implicit conversion to the underlying type. |
| 83 | * |
| 84 | * @return the underlying type. |
| 85 | */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 86 | constexpr operator T() const |
| 87 | { |
| 88 | return _value; |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | T _value{ std::numeric_limits<T>::epsilon() }; |
| 93 | }; |
| 94 | |
| 95 | /** Class reprensenting a relative tolerance value. */ |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 96 | template <typename T> |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 97 | class RelativeTolerance |
| 98 | { |
| 99 | public: |
| 100 | /** Underlying type. */ |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 101 | using value_type = T; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 102 | |
| 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 Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 118 | /** Implicit conversion to the underlying type. |
| 119 | * |
| 120 | * @return the underlying type. |
| 121 | */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 122 | constexpr operator value_type() const |
| 123 | { |
| 124 | return _value; |
| 125 | } |
| 126 | |
| 127 | private: |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 128 | value_type _value{ std::numeric_limits<T>::epsilon() }; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | /** Print AbsoluteTolerance type. */ |
| 132 | template <typename T> |
| 133 | inline ::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. */ |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 141 | template <typename T> |
| 142 | inline ::std::ostream &operator<<(::std::ostream &os, const RelativeTolerance<T> &tolerance) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 143 | { |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 144 | os << static_cast<typename RelativeTolerance<T>::value_type>(tolerance); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 145 | |
| 146 | return os; |
| 147 | } |
| 148 | |
| 149 | template <typename T> |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 150 | bool compare_dimensions(const Dimensions<T> &dimensions1, const Dimensions<T> &dimensions2, const DataLayout &data_layout = DataLayout::NCHW) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 151 | { |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 152 | ARM_COMPUTE_ERROR_ON(data_layout == DataLayout::UNKNOWN); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 153 | |
Giorgio Arena | 5c002ec | 2021-10-12 16:00:40 +0100 | [diff] [blame] | 154 | if(data_layout != DataLayout::NHWC) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 155 | { |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 156 | if(dimensions1.num_dimensions() != dimensions2.num_dimensions()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 157 | { |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 158 | return false; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | } |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 160 | |
| 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 Li | 37d65e4 | 2020-11-06 09:55:04 +0000 | [diff] [blame] | 171 | // 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 Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame] | 173 | const auto max_dims = std::max(dimensions1.num_dimensions(), dimensions2.num_dimensions()); |
| 174 | for(unsigned int i = 3; i < max_dims; ++i) |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 175 | { |
Gian Marco Iodice | 40471d1 | 2021-04-26 08:39:28 +0100 | [diff] [blame] | 176 | if(dimensions1[i] != dimensions2[i]) |
| 177 | { |
| 178 | return false; |
| 179 | } |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 180 | } |
SiCong Li | 37d65e4 | 2020-11-06 09:55:04 +0000 | [diff] [blame] | 181 | // clang-format on |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 182 | |
| 183 | if((dimensions1[0] != dimensions2[2]) || (dimensions1[1] != dimensions2[0]) || (dimensions1[2] != dimensions2[1])) |
| 184 | { |
| 185 | return false; |
| 186 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 187 | } |
| 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 | */ |
| 198 | void validate(const arm_compute::ValidRegion ®ion, const arm_compute::ValidRegion &reference); |
| 199 | |
| 200 | /** Validate padding. |
| 201 | * |
| 202 | * Padding on all sides has to be the same. |
| 203 | */ |
| 204 | void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &reference); |
| 205 | |
Sanghoon Lee | d7ba539 | 2017-12-13 11:28:50 +0000 | [diff] [blame] | 206 | /** Validate padding. |
| 207 | * |
| 208 | * Padding on all sides has to be the same. |
| 209 | */ |
| 210 | void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &width_reference, const arm_compute::PaddingSize &height_reference); |
| 211 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 212 | /** 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 Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 222 | template <typename T, typename U = AbsoluteTolerance<T>> |
Michele Di Giorgio | ff6c260 | 2018-02-26 15:22:16 +0000 | [diff] [blame] | 223 | void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value = U(), float tolerance_number = 0.f, float absolute_tolerance_value = 0.f); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 224 | |
| 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 Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 235 | template <typename T, typename U = AbsoluteTolerance<T>> |
Michele Di Giorgio | ff6c260 | 2018-02-26 15:22:16 +0000 | [diff] [blame] | 236 | void 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 Gottardi | 6203153 | 2017-07-04 11:21:28 +0100 | [diff] [blame] | 237 | |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 238 | /** 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 | */ |
| 248 | template <typename T, typename U = AbsoluteTolerance<T>> |
Michele Di Giorgio | ff6c260 | 2018-02-26 15:22:16 +0000 | [diff] [blame] | 249 | void 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 Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 251 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 252 | /** Validate tensors against constant value. |
| 253 | * |
| 254 | * - All values have to match. |
| 255 | */ |
| 256 | void 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 | */ |
| 264 | void 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 | */ |
| 270 | void validate(std::vector<unsigned int> classified_labels, std::vector<unsigned int> expected_labels); |
steniu01 | 960b084 | 2017-06-23 11:44:34 +0100 | [diff] [blame] | 271 | |
| 272 | /** Validate float value. |
| 273 | * |
| 274 | * - All values should match |
| 275 | */ |
Moritz Pflanzer | 7655a67 | 2017-09-23 11:57:33 +0100 | [diff] [blame] | 276 | template <typename T, typename U = AbsoluteTolerance<T>> |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 277 | bool validate(T target, T reference, U tolerance = AbsoluteTolerance<T>()); |
| 278 | |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 279 | template <typename T> |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 280 | struct compare_base |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 281 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 282 | /** 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 Richardson | 9c450cc | 2017-11-22 12:00:41 +0000 | [diff] [blame] | 288 | 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 Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 290 | { |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 291 | } |
| 292 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 293 | typename T::value_type _target{}; /**< Target value */ |
| 294 | typename T::value_type _reference{}; /**< Reference value */ |
| 295 | T _tolerance{}; /**< Tolerance value */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 296 | }; |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 297 | |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 298 | template <typename T> |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 299 | struct compare; |
| 300 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 301 | /** Compare values with an absolute tolerance */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 302 | template <typename U> |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 303 | struct compare<AbsoluteTolerance<U>> : public compare_base<AbsoluteTolerance<U>> |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 304 | { |
| 305 | using compare_base<AbsoluteTolerance<U>>::compare_base; |
| 306 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 307 | /** Perform comparison */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 308 | operator bool() const |
| 309 | { |
SiCongLi | cb86956 | 2021-12-22 15:37:20 +0000 | [diff] [blame] | 310 | if(are_equal_infs(this->_target, this->_reference)) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 311 | { |
SiCongLi | cb86956 | 2021-12-22 15:37:20 +0000 | [diff] [blame] | 312 | return true; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 313 | } |
| 314 | else if(this->_target == this->_reference) |
| 315 | { |
| 316 | return true; |
| 317 | } |
| 318 | |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 319 | 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 Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 324 | } |
| 325 | }; |
| 326 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 327 | /** Compare values with a relative tolerance */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 328 | template <typename U> |
Moritz Pflanzer | 5b61fd3 | 2017-09-12 15:51:33 +0100 | [diff] [blame] | 329 | struct compare<RelativeTolerance<U>> : public compare_base<RelativeTolerance<U>> |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 330 | { |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 331 | using compare_base<RelativeTolerance<U>>::compare_base; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 332 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 333 | /** Perform comparison */ |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 334 | operator bool() const |
| 335 | { |
SiCongLi | cb86956 | 2021-12-22 15:37:20 +0000 | [diff] [blame] | 336 | if(are_equal_infs(this->_target, this->_reference)) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 337 | { |
SiCongLi | cb86956 | 2021-12-22 15:37:20 +0000 | [diff] [blame] | 338 | return true; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 339 | } |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 340 | else if(this->_target == this->_reference) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 341 | { |
| 342 | return true; |
| 343 | } |
| 344 | |
Moritz Pflanzer | ff1c360 | 2017-09-22 12:41:25 +0100 | [diff] [blame] | 345 | 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 Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 346 | |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 347 | 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 Gottardi | 8df6c45 | 2018-03-12 13:26:28 +0000 | [diff] [blame] | 358 | const double relative_change = std::abs((static_cast<double>(this->_target) - static_cast<double>(this->_reference)) / this->_reference); |
steniu01 | 3e05e4e | 2017-08-25 17:18:01 +0100 | [diff] [blame] | 359 | |
| 360 | return relative_change <= static_cast<U>(this->_tolerance); |
| 361 | } |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 362 | } |
| 363 | }; |
| 364 | |
| 365 | template <typename T, typename U> |
Michele Di Giorgio | ff6c260 | 2018-02-26 15:22:16 +0000 | [diff] [blame] | 366 | void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value, float tolerance_number, float absolute_tolerance_value) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 367 | { |
| 368 | // Validate with valid region covering the entire shape |
Giorgio Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 369 | validate(tensor, reference, shape_to_valid_region(reference.shape()), tolerance_value, tolerance_number, absolute_tolerance_value); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 370 | } |
| 371 | |
John Richardson | 9c450cc | 2017-11-22 12:00:41 +0000 | [diff] [blame] | 372 | template <typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type> |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 373 | void 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 Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 376 | validate_wrap(tensor, reference, shape_to_valid_region(reference.shape()), tolerance_value, tolerance_number); |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | template <typename T, typename U> |
Michele Di Giorgio | ff6c260 | 2018-02-26 15:22:16 +0000 | [diff] [blame] | 380 | void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number, float absolute_tolerance_value) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 381 | { |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 382 | if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call()) |
| 383 | { |
| 384 | return; |
| 385 | } |
| 386 | |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 387 | uint64_t num_mismatches = 0; |
| 388 | uint64_t num_elements = 0; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 389 | |
| 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 Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 399 | ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 400 | |
| 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 Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 409 | Coordinates target_id(id); |
| 410 | if(tensor.data_layout() == DataLayout::NHWC) |
| 411 | { |
| 412 | permute(target_id, PermutationVector(2U, 0U, 1U)); |
| 413 | } |
| 414 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 415 | 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 Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 420 | const T &target_value = reinterpret_cast<const T *>(tensor(target_id))[c]; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 421 | const T &reference_value = reinterpret_cast<const T *>(reference(id))[c]; |
| 422 | |
Michalis Spyrou | 23fe7c2 | 2018-02-26 10:42:01 +0000 | [diff] [blame] | 423 | if(!compare<U>(target_value, reference_value, tolerance_value)) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 424 | { |
Michele Di Giorgio | ff6c260 | 2018-02-26 15:22:16 +0000 | [diff] [blame] | 425 | 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 Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 433 | 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))); |
steniu01 | 172c58d | 2017-08-31 13:49:08 +0100 | [diff] [blame] | 438 | framework::ARM_COMPUTE_PRINT_INFO(); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 439 | |
| 440 | ++num_mismatches; |
| 441 | } |
| 442 | |
| 443 | ++num_elements; |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 448 | if(num_elements != 0) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 449 | { |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 450 | 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 Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 452 | |
| 453 | ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 454 | << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number * 100 << "%)"); |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 455 | ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS); |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 456 | } |
| 457 | } |
Giorgio Arena | fc2817d | 2017-06-27 17:26:37 +0100 | [diff] [blame] | 458 | |
John Richardson | 9c450cc | 2017-11-22 12:00:41 +0000 | [diff] [blame] | 459 | template <typename T, typename U, typename = typename std::enable_if<std::is_integral<T>::value>::type> |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 460 | void validate_wrap(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number) |
| 461 | { |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 462 | if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call()) |
| 463 | { |
| 464 | return; |
| 465 | } |
| 466 | |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 467 | uint64_t num_mismatches = 0; |
| 468 | uint64_t num_elements = 0; |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 469 | |
| 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 Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 479 | ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS); |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 480 | |
| 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 Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 489 | Coordinates target_id(id); |
| 490 | if(tensor.data_layout() == DataLayout::NHWC) |
| 491 | { |
| 492 | permute(target_id, PermutationVector(2U, 0U, 1U)); |
| 493 | } |
| 494 | |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 495 | 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 Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 500 | const T &target_value = reinterpret_cast<const T *>(tensor(target_id))[c]; |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 501 | 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 Richardson | 9c450cc | 2017-11-22 12:00:41 +0000 | [diff] [blame] | 505 | // check for wrapping |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 506 | if(!equal) |
| 507 | { |
SiCongLi | cb86956 | 2021-12-22 15:37:20 +0000 | [diff] [blame] | 508 | if(are_equal_infs(target_value, reference_value)) |
John Richardson | 9c450cc | 2017-11-22 12:00:41 +0000 | [diff] [blame] | 509 | { |
SiCongLi | cb86956 | 2021-12-22 15:37:20 +0000 | [diff] [blame] | 510 | equal = true; |
John Richardson | 9c450cc | 2017-11-22 12:00:41 +0000 | [diff] [blame] | 511 | } |
| 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 Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 522 | } |
| 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 Richardson | 9c450cc | 2017-11-22 12:00:41 +0000 | [diff] [blame] | 530 | ARM_COMPUTE_TEST_INFO("wrap_tolerance = " << std::setprecision(5) << framework::make_printable(static_cast<typename U::value_type>(tolerance_value))); |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 531 | framework::ARM_COMPUTE_PRINT_INFO(); |
| 532 | |
| 533 | ++num_mismatches; |
| 534 | } |
| 535 | |
| 536 | ++num_elements; |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 541 | if(num_elements != 0) |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 542 | { |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 543 | 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 Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 545 | |
| 546 | ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 547 | << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number * 100 << "%)"); |
John Richardson | dd715f2 | 2017-09-18 16:10:48 +0100 | [diff] [blame] | 548 | ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS); |
| 549 | } |
| 550 | } |
| 551 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 552 | template <typename T, typename U> |
Michele Di Giorgio | ff6c260 | 2018-02-26 15:22:16 +0000 | [diff] [blame] | 553 | void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const SimpleTensor<T> &valid_mask, U tolerance_value, float tolerance_number, float absolute_tolerance_value) |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 554 | { |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 555 | if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call()) |
| 556 | { |
| 557 | return; |
| 558 | } |
| 559 | |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 560 | uint64_t num_mismatches = 0; |
| 561 | uint64_t num_elements = 0; |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 562 | |
| 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 Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 572 | ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape(), tensor.data_layout()), framework::LogLevel::ERRORS); |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 573 | |
| 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 Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 582 | Coordinates target_id(id); |
| 583 | if(tensor.data_layout() == DataLayout::NHWC) |
| 584 | { |
| 585 | permute(target_id, PermutationVector(2U, 0U, 1U)); |
| 586 | } |
| 587 | |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 588 | 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 Arena | 563494c | 2018-04-30 17:29:41 +0100 | [diff] [blame] | 593 | const T &target_value = reinterpret_cast<const T *>(tensor(target_id))[c]; |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 594 | 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 Giorgio | ff6c260 | 2018-02-26 15:22:16 +0000 | [diff] [blame] | 598 | 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 Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 606 | 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 Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 625 | if(num_elements != 0) |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 626 | { |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 627 | 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 Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 629 | |
| 630 | ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches |
Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [diff] [blame] | 631 | << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number * 100 << "%)"); |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 632 | ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | template <typename T, typename U> |
Moritz Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 637 | bool validate(T target, T reference, U tolerance) |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 638 | { |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 639 | if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call()) |
| 640 | { |
| 641 | return true; |
| 642 | } |
| 643 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 644 | 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 Pflanzer | 6c6597c | 2017-09-24 12:09:41 +0100 | [diff] [blame] | 647 | |
| 648 | const bool equal = compare<U>(target, reference, tolerance); |
| 649 | |
| 650 | ARM_COMPUTE_EXPECT(equal, framework::LogLevel::ERRORS); |
| 651 | |
| 652 | return equal; |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 653 | } |
John Richardson | f89a49f | 2017-09-05 11:21:56 +0100 | [diff] [blame] | 654 | |
| 655 | template <typename T, typename U> |
| 656 | void validate_min_max_loc(const MinMaxLocationValues<T> &target, const MinMaxLocationValues<U> &reference) |
| 657 | { |
Giorgio Arena | 68e29da | 2021-02-08 16:31:10 +0000 | [diff] [blame] | 658 | if(framework::Framework::get().configure_only() && framework::Framework::get().new_fixture_call()) |
| 659 | { |
| 660 | return; |
| 661 | } |
| 662 | |
John Richardson | f89a49f | 2017-09-05 11:21:56 +0100 | [diff] [blame] | 663 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 689 | } // namespace validation |
| 690 | } // namespace test |
| 691 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 692 | #endif /* ARM_COMPUTE_TEST_REFERENCE_VALIDATION_H */ |