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