Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 1 | /* |
| 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 Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 31 | #include "framework/Exceptions.h" |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 32 | #include "tests/IAccessor.h" |
| 33 | #include "tests/TypePrinter.h" |
| 34 | #include "tests/Utils.h" |
| 35 | |
| 36 | #include <iomanip> |
Moritz Pflanzer | b9e9cff | 2017-07-27 15:15:55 +0100 | [diff] [blame] | 37 | #include <ios> |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 38 | #include <vector> |
| 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace test |
| 43 | { |
| 44 | namespace validation |
| 45 | { |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 46 | /** Class reprensenting an absolute tolerance value. */ |
| 47 | template <typename T> |
| 48 | class AbsoluteTolerance |
| 49 | { |
| 50 | public: |
| 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 | |
| 75 | private: |
| 76 | T _value{ std::numeric_limits<T>::epsilon() }; |
| 77 | }; |
| 78 | |
| 79 | /** Class reprensenting a relative tolerance value. */ |
| 80 | class RelativeTolerance |
| 81 | { |
| 82 | public: |
| 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 | |
| 107 | private: |
| 108 | value_type _value{ 0 }; |
| 109 | }; |
| 110 | |
| 111 | /** Print AbsoluteTolerance type. */ |
| 112 | template <typename T> |
| 113 | inline ::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. */ |
| 121 | inline ::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 Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 128 | template <typename T> |
| 129 | bool 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 | */ |
| 153 | void validate(const arm_compute::ValidRegion ®ion, const arm_compute::ValidRegion &reference); |
| 154 | |
| 155 | /** Validate padding. |
| 156 | * |
| 157 | * Padding on all sides has to be the same. |
| 158 | */ |
| 159 | void 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 Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 171 | template <typename T, typename U = AbsoluteTolerance<T>> |
| 172 | void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, U tolerance_value = U(), float tolerance_number = 0.f); |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 173 | |
| 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 Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 184 | template <typename T, typename U = AbsoluteTolerance<T>> |
| 185 | void validate(const IAccessor &tensor, const SimpleTensor<T> &reference, const ValidRegion &valid_region, U tolerance_value = U(), float tolerance_number = 0.f); |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 186 | |
| 187 | /** Validate tensors against constant value. |
| 188 | * |
| 189 | * - All values have to match. |
| 190 | */ |
| 191 | void 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 | */ |
| 199 | void 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 | */ |
| 205 | void 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 Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 211 | template <typename T, typename U> |
| 212 | void validate(T target, T reference, U tolerance = AbsoluteTolerance<T>()); |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 213 | |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 214 | template <typename T> |
| 215 | struct compare_base |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 216 | { |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 217 | compare_base(typename T::value_type target, typename T::value_type reference, T tolerance = T(0)) |
| 218 | : _target{ target }, _reference{ reference }, _tolerance{ tolerance } |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 219 | { |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 220 | } |
| 221 | |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 222 | typename T::value_type _target{}; |
| 223 | typename T::value_type _reference{}; |
| 224 | T _tolerance{}; |
| 225 | }; |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 226 | |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 227 | template <typename T, typename U> |
| 228 | struct compare; |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 229 | |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 230 | template <typename U> |
| 231 | struct compare<AbsoluteTolerance<U>, U> : public compare_base<AbsoluteTolerance<U>> |
| 232 | { |
| 233 | using compare_base<AbsoluteTolerance<U>>::compare_base; |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 234 | |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 235 | operator bool() |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 236 | { |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 237 | 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 Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 245 | |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 246 | return static_cast<U>(std::abs(this->_target - this->_reference)) <= static_cast<U>(this->_tolerance); |
| 247 | } |
| 248 | }; |
| 249 | |
| 250 | template <typename U> |
| 251 | struct 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 Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 271 | |
| 272 | template <typename T, typename U> |
| 273 | void 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 | |
| 279 | template <typename T, typename U> |
| 280 | void 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 Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 285 | ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size(), framework::LogLevel::ERRORS); |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 286 | ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type(), framework::LogLevel::ERRORS); |
Moritz Pflanzer | 62eada2 | 2017-07-28 14:02:55 +0100 | [diff] [blame^] | 287 | |
| 288 | if(reference.format() != Format::UNKNOWN) |
| 289 | { |
| 290 | ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format(), framework::LogLevel::ERRORS); |
| 291 | } |
| 292 | |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 293 | 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 Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 295 | |
| 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 Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 312 | if(!compare<U, typename U::value_type>(target_value, reference_value, tolerance_value)) |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 313 | { |
| 314 | ARM_COMPUTE_TEST_INFO("id = " << id); |
| 315 | ARM_COMPUTE_TEST_INFO("channel = " << c); |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 316 | 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 Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 319 | ARM_COMPUTE_EXPECT_EQUAL(target_value, reference_value, framework::LogLevel::DEBUG); |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 320 | |
| 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 Pflanzer | b9e9cff | 2017-07-27 15:15:55 +0100 | [diff] [blame] | 334 | ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::fixed << std::setprecision(2) << percent_mismatches |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 335 | << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)"); |
Moritz Pflanzer | 2ac5040 | 2017-07-24 15:52:54 +0100 | [diff] [blame] | 336 | ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number, framework::LogLevel::ERRORS); |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
| 340 | template <typename T, typename U> |
Moritz Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 341 | void validate(T target, T reference, U tolerance) |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 342 | { |
Moritz Pflanzer | 24a8246 | 2017-08-04 11:34:44 +0100 | [diff] [blame] | 343 | 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 Pflanzer | 6106a4d | 2017-08-02 09:42:27 +0100 | [diff] [blame] | 346 | ARM_COMPUTE_EXPECT((compare<U, typename U::value_type>(target, reference, tolerance)), framework::LogLevel::ERRORS); |
Moritz Pflanzer | c7d1503 | 2017-07-18 16:21:16 +0100 | [diff] [blame] | 347 | } |
| 348 | } // namespace validation |
| 349 | } // namespace test |
| 350 | } // namespace arm_compute |
| 351 | #endif /* __ARM_COMPUTE_TEST_REFERENCE_VALIDATION_H__ */ |