From c7d1503008e74496836f99d64c082d4c9ae8f1ca Mon Sep 17 00:00:00 2001 From: Moritz Pflanzer Date: Tue, 18 Jul 2017 16:21:16 +0100 Subject: COMPMID-415: Build new validation Change-Id: I7409693f40ba3941b9d90f28c5d292c376e185c5 Reviewed-on: http://mpd-gerrit.cambridge.arm.com/80939 Tested-by: Kaizen Reviewed-by: Anthony Barbier --- tests/validation_new/FixedPoint.h | 984 ++++++++++++++++++++++++++++++++++++ tests/validation_new/SimpleTensor.h | 332 ++++++++++++ tests/validation_new/Validation.cpp | 314 ++++++++++++ tests/validation_new/Validation.h | 238 +++++++++ 4 files changed, 1868 insertions(+) create mode 100644 tests/validation_new/FixedPoint.h create mode 100644 tests/validation_new/SimpleTensor.h create mode 100644 tests/validation_new/Validation.cpp create mode 100644 tests/validation_new/Validation.h (limited to 'tests/validation_new') diff --git a/tests/validation_new/FixedPoint.h b/tests/validation_new/FixedPoint.h new file mode 100644 index 0000000000..61d791c54c --- /dev/null +++ b/tests/validation_new/FixedPoint.h @@ -0,0 +1,984 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_TEST_VALIDATION_FIXEDPOINT_H__ +#define __ARM_COMPUTE_TEST_VALIDATION_FIXEDPOINT_H__ + +#include "Utils.h" +#include "support/ToolchainSupport.h" + +#include +#include +#include +#include +#include +#include + +namespace arm_compute +{ +namespace test +{ +namespace fixed_point_arithmetic +{ +namespace detail +{ +// Forward declare structs +struct functions; +template +struct constant_expr; +} + +/** Fixed point traits */ +namespace traits +{ +// Promote types +// *INDENT-OFF* +// clang-format off +template struct promote { }; +template <> struct promote { using type = uint16_t; }; +template <> struct promote { using type = int16_t; }; +template <> struct promote { using type = uint32_t; }; +template <> struct promote { using type = int32_t; }; +template <> struct promote { using type = uint64_t; }; +template <> struct promote { using type = int64_t; }; +template <> struct promote { using type = uint64_t; }; +template <> struct promote { using type = int64_t; }; +template +using promote_t = typename promote::type; +// clang-format on +// *INDENT-ON* +} + +/** Strongly typed enum class representing the overflow policy */ +enum class OverflowPolicy +{ + WRAP, /**< Wrap policy */ + SATURATE /**< Saturate policy */ +}; +/** Strongly typed enum class representing the rounding policy */ +enum class RoundingPolicy +{ + TO_ZERO, /**< Round to zero policy */ + TO_NEAREST_EVEN /**< Round to nearest even policy */ +}; + +/** Arbitrary fixed-point arithmetic class */ +template +class fixed_point +{ +public: + // Static Checks + static_assert(std::is_integral::value, "Type is not an integer"); + + /** Constructor (from different fixed point type) + * + * @param[in] val Fixed point + * @param[in] p Fixed point precision + */ + template + fixed_point(fixed_point val, uint8_t p) + : _value(0), _fixed_point_position(p) + { + assert(p > 0 && p < std::numeric_limits::digits); + T v = 0; + + if(std::numeric_limits::digits < std::numeric_limits::digits) + { + val.rescale(p); + v = detail::constant_expr::saturate_cast(val.raw()); + } + else + { + auto v_cast = static_cast>(val); + v_cast.rescale(p); + v = v_cast.raw(); + } + _value = static_cast(v); + } + /** Constructor (from integer) + * + * @param[in] val Integer value to be represented as fixed point + * @param[in] p Fixed point precision + * @param[in] is_raw If true val is a raw fixed point value else an integer + */ + template ::value>::type> + fixed_point(U val, uint8_t p, bool is_raw = false) + : _value(val << p), _fixed_point_position(p) + { + if(is_raw) + { + _value = val; + } + } + /** Constructor (from float) + * + * @param[in] val Float value to be represented as fixed point + * @param[in] p Fixed point precision + */ + fixed_point(float val, uint8_t p) + : _value(detail::constant_expr::to_fixed(val, p)), _fixed_point_position(p) + { + assert(p > 0 && p < std::numeric_limits::digits); + } + /** Constructor (from float string) + * + * @param[in] str Float string to be represented as fixed point + * @param[in] p Fixed point precision + */ + fixed_point(std::string str, uint8_t p) + : _value(detail::constant_expr::to_fixed(support::cpp11::stof(str), p)), _fixed_point_position(p) + { + assert(p > 0 && p < std::numeric_limits::digits); + } + /** Default copy constructor */ + fixed_point &operator=(const fixed_point &) = default; + /** Default move constructor */ + fixed_point &operator=(fixed_point &&) = default; + /** Default copy assignment operator */ + fixed_point(const fixed_point &) = default; + /** Default move assignment operator */ + fixed_point(fixed_point &&) = default; + + /** Float conversion operator + * + * @return Float representation of fixed point + */ + operator float() const + { + return detail::constant_expr::to_float(_value, _fixed_point_position); + } + /** Integer conversion operator + * + * @return Integer representation of fixed point + */ + template ::value>::type> + operator U() const + { + return detail::constant_expr::to_int(_value, _fixed_point_position); + } + /** Convert to different fixed point of different type but same precision + * + * @note Down-conversion might fail. + */ + template + operator fixed_point() + { + U val = static_cast(_value); + if(std::numeric_limits::digits < std::numeric_limits::digits) + { + val = detail::constant_expr::saturate_cast(_value); + } + return fixed_point(val, _fixed_point_position, true); + } + + /** Arithmetic += assignment operator + * + * @param[in] rhs Fixed point operand + * + * @return Reference to this fixed point + */ + template + fixed_point &operator+=(const fixed_point &rhs) + { + fixed_point val(rhs, _fixed_point_position); + _value += val.raw(); + return *this; + } + /** Arithmetic -= assignment operator + * + * @param[in] rhs Fixed point operand + * + * @return Reference to this fixed point + */ + template + fixed_point &operator-=(const fixed_point &rhs) + { + fixed_point val(rhs, _fixed_point_position); + _value -= val.raw(); + return *this; + } + + /** Raw value accessor + * + * @return Raw fixed point value + */ + T raw() const + { + return _value; + } + /** Precision accessor + * + * @return Precision of fixed point + */ + uint8_t precision() const + { + return _fixed_point_position; + } + /** Rescale a fixed point to a new precision + * + * @param[in] p New fixed point precision + */ + void rescale(uint8_t p) + { + assert(p > 0 && p < std::numeric_limits::digits); + + using promoted_T = typename traits::promote::type; + promoted_T val = _value; + if(p > _fixed_point_position) + { + val <<= (p - _fixed_point_position); + } + else if(p < _fixed_point_position) + { + uint8_t pbar = _fixed_point_position - p; + val += (pbar != 0) ? (1 << (pbar - 1)) : 0; + val >>= pbar; + } + + _value = detail::constant_expr::saturate_cast(val); + _fixed_point_position = p; + } + +private: + T _value; /**< Fixed point raw value */ + uint8_t _fixed_point_position; /**< Fixed point precision */ +}; + +namespace detail +{ +/** Count the number of leading zero bits in the given value. + * + * @param[in] value Input value. + * + * @return Number of leading zero bits. + */ +template +constexpr int clz(T value) +{ + using unsigned_T = typename std::make_unsigned::type; + // __builtin_clz is available for int. Need to correct reported number to + // match the original type. + return __builtin_clz(value) - (32 - std::numeric_limits::digits); +} + +template +struct constant_expr +{ + /** Calculate representation of 1 in fixed point given a fixed point precision + * + * @param[in] p Fixed point precision + * + * @return Representation of value 1 in fixed point. + */ + static constexpr T fixed_one(uint8_t p) + { + return (1 << p); + } + /** Calculate fixed point precision step given a fixed point precision + * + * @param[in] p Fixed point precision + * + * @return Fixed point precision step + */ + static constexpr float fixed_step(uint8_t p) + { + return (1.0f / static_cast(1 << p)); + } + + /** Convert a fixed point value to float given its precision. + * + * @param[in] val Fixed point value + * @param[in] p Fixed point precision + * + * @return Float representation of the fixed point number + */ + static constexpr float to_float(T val, uint8_t p) + { + return static_cast(val * fixed_step(p)); + } + /** Convert a fixed point value to integer given its precision. + * + * @param[in] val Fixed point value + * @param[in] p Fixed point precision + * + * @return Integer of the fixed point number + */ + static constexpr T to_int(T val, uint8_t p) + { + return val >> p; + } + /** Convert a single precision floating point value to a fixed point representation given its precision. + * + * @param[in] val Floating point value + * @param[in] p Fixed point precision + * + * @return The raw fixed point representation + */ + static constexpr T to_fixed(float val, uint8_t p) + { + return static_cast(saturate_cast(val * fixed_one(p) + ((val >= 0) ? 0.5 : -0.5))); + } + /** Clamp value between two ranges + * + * @param[in] val Value to clamp + * @param[in] min Minimum value to clamp to + * @param[in] max Maximum value to clamp to + * + * @return clamped value + */ + static constexpr T clamp(T val, T min, T max) + { + return std::min(std::max(val, min), max); + } + /** Saturate given number + * + * @param[in] val Value to saturate + * + * @return Saturated value + */ + template + static constexpr T saturate_cast(U val) + { + return static_cast(std::min(std::max(val, static_cast(std::numeric_limits::min())), static_cast(std::numeric_limits::max()))); + } +}; +struct functions +{ + /** Output stream operator + * + * @param[in] s Output stream + * @param[in] x Fixed point value + * + * @return Reference output to updated stream + */ + template + static std::basic_ostream &write(std::basic_ostream &s, fixed_point &x) + { + return s << static_cast(x); + } + /** Signbit of a fixed point number. + * + * @param[in] x Fixed point number + * + * @return True if negative else false. + */ + template + static bool signbit(fixed_point x) + { + return ((x.raw() >> std::numeric_limits::digits) != 0); + } + /** Checks if two fixed point numbers are equal + * + * @param[in] x First fixed point operand + * @param[in] y Second fixed point operand + * + * @return True if fixed points are equal else false + */ + template + static bool isequal(fixed_point x, fixed_point y) + { + uint8_t p = std::min(x.precision(), y.precision()); + x.rescale(p); + y.rescale(p); + return (x.raw() == y.raw()); + } + /** Checks if two fixed point number are not equal + * + * @param[in] x First fixed point operand + * @param[in] y Second fixed point operand + * + * @return True if fixed points are not equal else false + */ + template + static bool isnotequal(fixed_point x, fixed_point y) + { + return !isequal(x, y); + } + /** Checks if one fixed point is greater than the other + * + * @param[in] x First fixed point operand + * @param[in] y Second fixed point operand + * + * @return True if fixed point is greater than other + */ + template + static bool isgreater(fixed_point x, fixed_point y) + { + uint8_t p = std::min(x.precision(), y.precision()); + x.rescale(p); + y.rescale(p); + return (x.raw() > y.raw()); + } + /** Checks if one fixed point is greater or equal than the other + * + * @param[in] x First fixed point operand + * @param[in] y Second fixed point operand + * + * @return True if fixed point is greater or equal than other + */ + template + static bool isgreaterequal(fixed_point x, fixed_point y) + { + uint8_t p = std::min(x.precision(), y.precision()); + x.rescale(p); + y.rescale(p); + return (x.raw() >= y.raw()); + } + /** Checks if one fixed point is less than the other + * + * @param[in] x First fixed point operand + * @param[in] y Second fixed point operand + * + * @return True if fixed point is less than other + */ + template + static bool isless(fixed_point x, fixed_point y) + { + uint8_t p = std::min(x.precision(), y.precision()); + x.rescale(p); + y.rescale(p); + return (x.raw() < y.raw()); + } + /** Checks if one fixed point is less or equal than the other + * + * @param[in] x First fixed point operand + * @param[in] y Second fixed point operand + * + * @return True if fixed point is less or equal than other + */ + template + static bool islessequal(fixed_point x, fixed_point y) + { + uint8_t p = std::min(x.precision(), y.precision()); + x.rescale(p); + y.rescale(p); + return (x.raw() <= y.raw()); + } + /** Checks if one fixed point is less or greater than the other + * + * @param[in] x First fixed point operand + * @param[in] y Second fixed point operand + * + * @return True if fixed point is less or greater than other + */ + template + static bool islessgreater(fixed_point x, fixed_point y) + { + return isnotequal(x, y); + } + /** Clamp fixed point to specific range. + * + * @param[in] x Fixed point operand + * @param[in] min Minimum value to clamp to + * @param[in] max Maximum value to clamp to + * + * @return Clamped result + */ + template + static fixed_point clamp(fixed_point x, T min, T max) + { + return fixed_point(constant_expr::clamp(x.raw(), min, max), x.precision(), true); + } + /** Negate number + * + * @param[in] x Fixed point operand + * + * @return Negated fixed point result + */ + template + static fixed_point negate(fixed_point x) + { + using promoted_T = typename traits::promote::type; + promoted_T val = -x.raw(); + if(OP == OverflowPolicy::SATURATE) + { + val = constant_expr::saturate_cast(val); + } + return fixed_point(static_cast(val), x.precision(), true); + } + /** Perform addition among two fixed point numbers + * + * @param[in] x First fixed point operand + * @param[in] y Second fixed point operand + * + * @return Result fixed point with precision equal to minimum precision of both operands + */ + template + static fixed_point add(fixed_point x, fixed_point y) + { + uint8_t p = std::min(x.precision(), y.precision()); + x.rescale(p); + y.rescale(p); + if(OP == OverflowPolicy::SATURATE) + { + using type = typename traits::promote::type; + type val = static_cast(x.raw()) + static_cast(y.raw()); + val = constant_expr::saturate_cast(val); + return fixed_point(static_cast(val), p, true); + } + else + { + return fixed_point(x.raw() + y.raw(), p, true); + } + } + /** Perform subtraction among two fixed point numbers + * + * @param[in] x First fixed point operand + * @param[in] y Second fixed point operand + * + * @return Result fixed point with precision equal to minimum precision of both operands + */ + template + static fixed_point sub(fixed_point x, fixed_point y) + { + uint8_t p = std::min(x.precision(), y.precision()); + x.rescale(p); + y.rescale(p); + if(OP == OverflowPolicy::SATURATE) + { + using type = typename traits::promote::type; + type val = static_cast(x.raw()) - static_cast(y.raw()); + val = constant_expr::saturate_cast(val); + return fixed_point(static_cast(val), p, true); + } + else + { + return fixed_point(x.raw() - y.raw(), p, true); + } + } + /** Perform multiplication among two fixed point numbers + * + * @param[in] x First fixed point operand + * @param[in] y Second fixed point operand + * + * @return Result fixed point with precision equal to minimum precision of both operands + */ + template + static fixed_point mul(fixed_point x, fixed_point y) + { + using promoted_T = typename traits::promote::type; + uint8_t p_min = std::min(x.precision(), y.precision()); + uint8_t p_max = std::max(x.precision(), y.precision()); + promoted_T round_factor = (1 << (p_max - 1)); + promoted_T val = ((static_cast(x.raw()) * static_cast(y.raw())) + round_factor) >> p_max; + if(OP == OverflowPolicy::SATURATE) + { + val = constant_expr::saturate_cast(val); + } + return fixed_point(static_cast(val), p_min, true); + } + /** Perform division among two fixed point numbers + * + * @param[in] x First fixed point operand + * @param[in] y Second fixed point operand + * + * @return Result fixed point with precision equal to minimum precision of both operands + */ + template + static fixed_point div(fixed_point x, fixed_point y) + { + using promoted_T = typename traits::promote::type; + uint8_t p = std::min(x.precision(), y.precision()); + promoted_T denom = static_cast(y.raw()); + if(denom != 0) + { + promoted_T val = (static_cast(x.raw()) << std::max(x.precision(), y.precision())) / denom; + if(OP == OverflowPolicy::SATURATE) + { + val = constant_expr::saturate_cast(val); + } + return fixed_point(static_cast(val), p, true); + } + else + { + T val = (x.raw() < 0) ? std::numeric_limits::min() : std::numeric_limits::max(); + return fixed_point(val, p, true); + } + } + /** Shift left + * + * @param[in] x Fixed point operand + * @param[in] shift Shift value + * + * @return Shifted value + */ + template + static fixed_point shift_left(fixed_point x, size_t shift) + { + using promoted_T = typename traits::promote::type; + promoted_T val = static_cast(x.raw()) << shift; + if(OP == OverflowPolicy::SATURATE) + { + val = constant_expr::saturate_cast(val); + } + return fixed_point(static_cast(val), x.precision(), true); + } + /** Shift right + * + * @param[in] x Fixed point operand + * @param[in] shift Shift value + * + * @return Shifted value + */ + template + static fixed_point shift_right(fixed_point x, size_t shift) + { + return fixed_point(x.raw() >> shift, x.precision(), true); + } + /** Calculate absolute value + * + * @param[in] x Fixed point operand + * + * @return Absolute value of operand + */ + template + static fixed_point abs(fixed_point x) + { + using promoted_T = typename traits::promote::type; + T val = (x.raw() < 0) ? constant_expr::saturate_cast(-static_cast(x.raw())) : x.raw(); + return fixed_point(val, x.precision(), true); + } + /** Calculate the logarithm of a fixed point number + * + * @param[in] x Fixed point operand + * + * @return Logarithm value of operand + */ + template + static fixed_point log(fixed_point x) + { + uint8_t p = x.precision(); + auto const_one = fixed_point(static_cast(1), p); + + // Logarithm of 1 is zero and logarithm of negative values is not defined in R, so return 0. + // Also, log(x) == -log(1/x) for 0 < x < 1. + if(isequal(x, const_one) || islessequal(x, fixed_point(static_cast(0), p))) + { + return fixed_point(static_cast(0), p, true); + } + else if(isless(x, const_one)) + { + return mul(log(div(const_one, x)), fixed_point(-1, p)); + } + + // Remove even powers of 2 + T shift_val = 31 - __builtin_clz(x.raw() >> p); + x = shift_right(x, shift_val); + x = sub(x, const_one); + + // Constants + auto ln2 = fixed_point(0.6931471, p); + auto A = fixed_point(1.4384189, p); + auto B = fixed_point(-0.67719, p); + auto C = fixed_point(0.3218538, p); + auto D = fixed_point(-0.0832229, p); + + // Polynomial expansion + auto sum = add(mul(x, D), C); + sum = add(mul(x, sum), B); + sum = add(mul(x, sum), A); + sum = mul(x, sum); + + return mul(add(sum, fixed_point(static_cast(shift_val), p)), ln2); + } + /** Calculate the exponential of a fixed point number. + * + * exp(x) = exp(floor(x)) * exp(x - floor(x)) + * = pow(2, floor(x) / ln(2)) * exp(x - floor(x)) + * = exp(x - floor(x)) << (floor(x) / ln(2)) + * + * @param[in] x Fixed point operand + * + * @return Exponential value of operand + */ + template + static fixed_point exp(fixed_point x) + { + uint8_t p = x.precision(); + // Constants + auto const_one = fixed_point(1, p); + auto ln2 = fixed_point(0.6931471, p); + auto inv_ln2 = fixed_point(1.442695, p); + auto A = fixed_point(0.9978546, p); + auto B = fixed_point(0.4994721, p); + auto C = fixed_point(0.1763723, p); + auto D = fixed_point(0.0435108, p); + + T scaled_int_part = detail::constant_expr::to_int(mul(x, inv_ln2).raw(), p); + + // Polynomial expansion + auto frac_part = sub(x, mul(ln2, fixed_point(scaled_int_part, p))); + auto taylor = add(mul(frac_part, D), C); + taylor = add(mul(frac_part, taylor), B); + taylor = add(mul(frac_part, taylor), A); + taylor = mul(frac_part, taylor); + taylor = add(taylor, const_one); + + // Saturate value + if(static_cast(clz(taylor.raw())) <= scaled_int_part) + { + return fixed_point(std::numeric_limits::max(), p, true); + } + + return (scaled_int_part < 0) ? shift_right(taylor, -scaled_int_part) : shift_left(taylor, scaled_int_part); + } + /** Calculate the inverse square root of a fixed point number + * + * @param[in] x Fixed point operand + * + * @return Inverse square root value of operand + */ + template + static fixed_point inv_sqrt(fixed_point x) + { + const uint8_t p = x.precision(); + int8_t shift = std::numeric_limits::digits - (p + detail::clz(x.raw())); + + shift += std::numeric_limits::is_signed ? 1 : 0; + + // Use volatile to restrict compiler optimizations on shift as compiler reports maybe-uninitialized error on Android + volatile int8_t *shift_ptr = &shift; + + auto const_three = fixed_point(3, p); + auto a = (*shift_ptr < 0) ? shift_left(x, -(shift)) : shift_right(x, shift); + fixed_point x2 = a; + + // We need three iterations to find the result for QS8 and five for QS16 + constexpr int num_iterations = std::is_same::value ? 3 : 5; + for(int i = 0; i < num_iterations; ++i) + { + fixed_point three_minus_dx = sub(const_three, mul(a, mul(x2, x2))); + x2 = shift_right(mul(x2, three_minus_dx), 1); + } + + return (shift < 0) ? shift_left(x2, (-shift) >> 1) : shift_right(x2, shift >> 1); + } + /** Calculate the hyperbolic tangent of a fixed point number + * + * @param[in] x Fixed point operand + * + * @return Hyperbolic tangent of the operand + */ + template + static fixed_point tanh(fixed_point x) + { + uint8_t p = x.precision(); + // Constants + auto const_one = fixed_point(1, p); + auto const_two = fixed_point(2, p); + + auto exp2x = exp(const_two * x); + auto num = exp2x - const_one; + auto den = exp2x + const_one; + auto tanh = num / den; + + return tanh; + } + /** Calculate the a-th power of a fixed point number. + * + * The power is computed as x^a = e^(log(x) * a) + * + * @param[in] x Fixed point operand + * @param[in] a Fixed point exponent + * + * @return a-th power of the operand + */ + template + static fixed_point pow(fixed_point x, fixed_point a) + { + return exp(log(x) * a); + } +}; + +template +bool operator==(const fixed_point &lhs, const fixed_point &rhs) +{ + return functions::isequal(lhs, rhs); +} +template +bool operator!=(const fixed_point &lhs, const fixed_point &rhs) +{ + return !operator==(lhs, rhs); +} +template +bool operator<(const fixed_point &lhs, const fixed_point &rhs) +{ + return functions::isless(lhs, rhs); +} +template +bool operator>(const fixed_point &lhs, const fixed_point &rhs) +{ + return operator<(rhs, lhs); +} +template +bool operator<=(const fixed_point &lhs, const fixed_point &rhs) +{ + return !operator>(lhs, rhs); +} +template +bool operator>=(const fixed_point &lhs, const fixed_point &rhs) +{ + return !operator<(lhs, rhs); +} +template +fixed_point operator+(const fixed_point &lhs, const fixed_point &rhs) +{ + return functions::add(lhs, rhs); +} +template +fixed_point operator-(const fixed_point &lhs, const fixed_point &rhs) +{ + return functions::sub(lhs, rhs); +} +template +fixed_point operator-(const fixed_point &rhs) +{ + return functions::negate(rhs); +} +template +fixed_point operator*(fixed_point x, fixed_point y) +{ + return functions::mul(x, y); +} +template +fixed_point operator/(fixed_point x, fixed_point y) +{ + return functions::div(x, y); +} +template +fixed_point operator>>(fixed_point x, size_t shift) +{ + return functions::shift_right(x, shift); +} +template +fixed_point operator<<(fixed_point x, size_t shift) +{ + return functions::shift_left(x, shift); +} +template +std::basic_ostream &operator<<(std::basic_ostream &s, fixed_point x) +{ + return functions::write(s, x); +} +template +inline fixed_point min(fixed_point x, fixed_point y) +{ + return x > y ? y : x; +} +template +inline fixed_point max(fixed_point x, fixed_point y) +{ + return x > y ? x : y; +} +template +inline fixed_point add(fixed_point x, fixed_point y) +{ + return functions::add(x, y); +} +template +inline fixed_point sub(fixed_point x, fixed_point y) +{ + return functions::sub(x, y); +} +template +inline fixed_point mul(fixed_point x, fixed_point y) +{ + return functions::mul(x, y); +} +template +inline fixed_point div(fixed_point x, fixed_point y) +{ + return functions::div(x, y); +} +template +inline fixed_point abs(fixed_point x) +{ + return functions::abs(x); +} +template +inline fixed_point clamp(fixed_point x, T min, T max) +{ + return functions::clamp(x, min, max); +} +template +inline fixed_point exp(fixed_point x) +{ + return functions::exp(x); +} +template +inline fixed_point log(fixed_point x) +{ + return functions::log(x); +} +template +inline fixed_point inv_sqrt(fixed_point x) +{ + return functions::inv_sqrt(x); +} +template +inline fixed_point tanh(fixed_point x) +{ + return functions::tanh(x); +} +template +inline fixed_point pow(fixed_point x, fixed_point a) +{ + return functions::pow(x, a); +} +} // namespace detail + +// Expose operators +using detail::operator==; +using detail::operator!=; +using detail::operator<; +using detail::operator>; +using detail::operator<=; +using detail::operator>=; +using detail::operator+; +using detail::operator-; +using detail::operator*; +using detail::operator/; +using detail::operator>>; +using detail::operator<<; + +// Expose additional functions +using detail::min; +using detail::max; +using detail::add; +using detail::sub; +using detail::mul; +using detail::div; +using detail::abs; +using detail::clamp; +using detail::exp; +using detail::log; +using detail::inv_sqrt; +using detail::tanh; +using detail::pow; +// TODO: floor +// TODO: ceil +// TODO: sqrt +} // namespace fixed_point_arithmetic +} // namespace test +} // namespace arm_compute +#endif /*__ARM_COMPUTE_TEST_VALIDATION_FIXEDPOINT_H__ */ diff --git a/tests/validation_new/SimpleTensor.h b/tests/validation_new/SimpleTensor.h new file mode 100644 index 0000000000..6392e38e25 --- /dev/null +++ b/tests/validation_new/SimpleTensor.h @@ -0,0 +1,332 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_TEST_SIMPLE_TENSOR_H__ +#define __ARM_COMPUTE_TEST_SIMPLE_TENSOR_H__ + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/Utils.h" +#include "support/ToolchainSupport.h" +#include "tests/IAccessor.h" +#include "tests/Utils.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace arm_compute +{ +namespace test +{ +/** Simple tensor object that stores elements in a consecutive chunk of memory. + * + * It can be created by either loading an image from a file which also + * initialises the content of the tensor or by explcitly specifying the size. + * The latter leaves the content uninitialised. + * + * Furthermore, the class provides methods to convert the tensor's values into + * different image format. + */ +template +class SimpleTensor final : public IAccessor +{ +public: + /** Create an uninitialised tensor. */ + SimpleTensor() = default; + + /** Create an uninitialised tensor of the given @p shape and @p format. + * + * @param[in] shape Shape of the new raw tensor. + * @param[in] format Format of the new raw tensor. + * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers + */ + SimpleTensor(TensorShape shape, Format format, int fixed_point_position = 0); + + /** Create an uninitialised tensor of the given @p shape and @p data type. + * + * @param[in] shape Shape of the new raw tensor. + * @param[in] data_type Data type of the new raw tensor. + * @param[in] num_channels (Optional) Number of channels (default = 1). + * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers (default = 0). + */ + SimpleTensor(TensorShape shape, DataType data_type, int num_channels = 1, int fixed_point_position = 0); + + /** Create a deep copy of the given @p tensor. + * + * @param[in] tensor To be copied tensor. + */ + SimpleTensor(const SimpleTensor &tensor); + + /** Create a deep copy of the given @p tensor. + * + * @param[in] tensor To be copied tensor. + */ + SimpleTensor &operator =(SimpleTensor tensor); + SimpleTensor(SimpleTensor &&) = default; + ~SimpleTensor() = default; + + using value_type = T; + using Buffer = std::unique_ptr; + + /** Return value at @p offset in the buffer. + * + * @param[in] offset Offset within the buffer. + */ + T &operator[](size_t offset); + + /** Return constant value at @p offset in the buffer. + * + * @param[in] offset Offset within the buffer. + */ + const T &operator[](size_t offset) const; + + /** Shape of the tensor. */ + TensorShape shape() const override; + + /** Size of each element in the tensor in bytes. */ + size_t element_size() const override; + + /** Total size of the tensor in bytes. */ + size_t size() const override; + + /** Image format of the tensor. */ + Format format() const override; + + /** Data type of the tensor. */ + DataType data_type() const override; + + /** Number of channels of the tensor. */ + int num_channels() const override; + + /** Number of elements of the tensor. */ + int num_elements() const override; + + /** The number of bits for the fractional part of the fixed point numbers. */ + int fixed_point_position() const override; + + /** Constant pointer to the underlying buffer. */ + const T *data() const; + + /** Pointer to the underlying buffer. */ + T *data(); + + /** Read only access to the specified element. + * + * @param[in] coord Coordinates of the desired element. + * + * @return A pointer to the desired element. + */ + const void *operator()(const Coordinates &coord) const override; + + /** Access to the specified element. + * + * @param[in] coord Coordinates of the desired element. + * + * @return A pointer to the desired element. + */ + void *operator()(const Coordinates &coord) override; + + /** Swaps the content of the provided tensors. + * + * @param[in, out] tensor1 Tensor to be swapped. + * @param[in, out] tensor2 Tensor to be swapped. + */ + template + friend void swap(SimpleTensor &tensor1, SimpleTensor &tensor2); + +private: + Buffer _buffer{ nullptr }; + TensorShape _shape{}; + Format _format{ Format::UNKNOWN }; + DataType _data_type{ DataType::UNKNOWN }; + int _num_channels{ 0 }; + int _fixed_point_position{ 0 }; +}; + +template +SimpleTensor::SimpleTensor(TensorShape shape, Format format, int fixed_point_position) + : _buffer(nullptr), + _shape(shape), + _format(format), + _fixed_point_position(fixed_point_position) +{ + _buffer = support::cpp14::make_unique(num_elements() * num_channels()); +} + +template +SimpleTensor::SimpleTensor(TensorShape shape, DataType data_type, int num_channels, int fixed_point_position) + : _buffer(nullptr), + _shape(shape), + _data_type(data_type), + _num_channels(num_channels), + _fixed_point_position(fixed_point_position) +{ + _buffer = support::cpp14::make_unique(num_elements() * this->num_channels()); +} + +template +SimpleTensor::SimpleTensor(const SimpleTensor &tensor) + : _buffer(nullptr), + _shape(tensor.shape()), + _format(tensor.format()), + _fixed_point_position(tensor.fixed_point_position()) +{ + _buffer = support::cpp14::make_unique(tensor.num_elements() * num_channels()); + std::copy_n(tensor.data(), num_elements() * num_channels(), _buffer.get()); +} + +template +SimpleTensor &SimpleTensor::operator=(SimpleTensor tensor) +{ + swap(*this, tensor); + + return *this; +} + +template +T &SimpleTensor::operator[](size_t offset) +{ + return _buffer[offset]; +} + +template +const T &SimpleTensor::operator[](size_t offset) const +{ + return _buffer[offset]; +} + +template +TensorShape SimpleTensor::shape() const +{ + return _shape; +} + +template +size_t SimpleTensor::element_size() const +{ + return num_channels() * element_size_from_data_type(data_type()); +} + +template +int SimpleTensor::fixed_point_position() const +{ + return _fixed_point_position; +} + +template +size_t SimpleTensor::size() const +{ + const size_t size = std::accumulate(_shape.cbegin(), _shape.cend(), 1, std::multiplies()); + return size * element_size(); +} + +template +Format SimpleTensor::format() const +{ + return _format; +} + +template +DataType SimpleTensor::data_type() const +{ + if(_format != Format::UNKNOWN) + { + return data_type_from_format(_format); + } + else + { + return _data_type; + } +} + +template +int SimpleTensor::num_channels() const +{ + switch(_format) + { + case Format::U8: + case Format::S16: + case Format::U16: + case Format::S32: + case Format::U32: + return 1; + case Format::RGB888: + return 3; + case Format::UNKNOWN: + return _num_channels; + default: + ARM_COMPUTE_ERROR("NOT SUPPORTED!"); + } +} + +template +int SimpleTensor::num_elements() const +{ + return _shape.total_size(); +} + +template +const T *SimpleTensor::data() const +{ + return _buffer.get(); +} + +template +T *SimpleTensor::data() +{ + return _buffer.get(); +} + +template +const void *SimpleTensor::operator()(const Coordinates &coord) const +{ + return _buffer.get() + coord2index(_shape, coord); +} + +template +void *SimpleTensor::operator()(const Coordinates &coord) +{ + return _buffer.get() + coord2index(_shape, coord); +} + +template +void swap(SimpleTensor &tensor1, SimpleTensor &tensor2) +{ + // Use unqualified call to swap to enable ADL. But make std::swap available + // as backup. + using std::swap; + swap(tensor1._shape, tensor2._shape); + swap(tensor1._format, tensor2._format); + swap(tensor1._data_type, tensor2._data_type); + swap(tensor1._num_channels, tensor2._num_channels); + swap(tensor1._buffer, tensor2._buffer); +} +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_SIMPLE_TENSOR_H__ */ diff --git a/tests/validation_new/Validation.cpp b/tests/validation_new/Validation.cpp new file mode 100644 index 0000000000..9daee449c1 --- /dev/null +++ b/tests/validation_new/Validation.cpp @@ -0,0 +1,314 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "Validation.h" + +#include "arm_compute/core/Coordinates.h" +#include "arm_compute/core/Error.h" +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/runtime/Tensor.h" + +#include +#include +#include +#include + +#ifdef ARM_COMPUTE_ENABLE_FP16 +#include // needed for float16_t +#endif /* ARM_COMPUTE_ENABLE_FP16 */ + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +/** Get the data from *ptr after casting according to @p data_type and then convert the data to double. + * + * @param[in] ptr Pointer to value. + * @param[in] data_type Data type of both values. + * + * @return The data from the ptr after converted to double. + */ +double get_double_data(const void *ptr, DataType data_type) +{ + if(ptr == nullptr) + { + ARM_COMPUTE_ERROR("Can't dereference a null pointer!"); + } + + switch(data_type) + { + case DataType::U8: + return *reinterpret_cast(ptr); + case DataType::S8: + return *reinterpret_cast(ptr); + case DataType::QS8: + return *reinterpret_cast(ptr); + case DataType::U16: + return *reinterpret_cast(ptr); + case DataType::S16: + return *reinterpret_cast(ptr); + case DataType::QS16: + return *reinterpret_cast(ptr); + case DataType::U32: + return *reinterpret_cast(ptr); + case DataType::S32: + return *reinterpret_cast(ptr); + case DataType::U64: + return *reinterpret_cast(ptr); + case DataType::S64: + return *reinterpret_cast(ptr); +#ifdef ARM_COMPUTE_ENABLE_FP16 + case DataType::F16: + return *reinterpret_cast(ptr); +#endif /* ARM_COMPUTE_ENABLE_FP16 */ + case DataType::F32: + return *reinterpret_cast(ptr); + case DataType::F64: + return *reinterpret_cast(ptr); + case DataType::SIZET: + return *reinterpret_cast(ptr); + default: + ARM_COMPUTE_ERROR("NOT SUPPORTED!"); + } +} + +void check_border_element(const IAccessor &tensor, const Coordinates &id, + const BorderMode &border_mode, const void *border_value, + int64_t &num_elements, int64_t &num_mismatches) +{ + const size_t channel_size = element_size_from_data_type(tensor.data_type()); + const auto ptr = static_cast(tensor(id)); + + if(border_mode == BorderMode::REPLICATE) + { + Coordinates border_id{ id }; + + if(id.x() < 0) + { + border_id.set(0, 0); + } + else if(static_cast(id.x()) >= tensor.shape().x()) + { + border_id.set(0, tensor.shape().x() - 1); + } + + if(id.y() < 0) + { + border_id.set(1, 0); + } + else if(static_cast(id.y()) >= tensor.shape().y()) + { + border_id.set(1, tensor.shape().y() - 1); + } + + border_value = tensor(border_id); + } + + // Iterate over all channels within one element + for(int channel = 0; channel < tensor.num_channels(); ++channel) + { + const size_t channel_offset = channel * channel_size; + const double target = get_double_data(ptr + channel_offset, tensor.data_type()); + const double ref = get_double_data(static_cast(border_value) + channel_offset, tensor.data_type()); + const bool equal = is_equal(target, ref); + + ARM_COMPUTE_TEST_INFO("id = " << id); + ARM_COMPUTE_TEST_INFO("channel = " << channel); + ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << target); + ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << ref); + ARM_COMPUTE_EXPECT_EQUAL(target, ref); + + if(!equal) + { + ++num_mismatches; + } + + ++num_elements; + } +} +} // namespace + +void validate(const arm_compute::ValidRegion ®ion, const arm_compute::ValidRegion &reference) +{ + ARM_COMPUTE_EXPECT_EQUAL(region.anchor.num_dimensions(), reference.anchor.num_dimensions()); + ARM_COMPUTE_EXPECT_EQUAL(region.shape.num_dimensions(), reference.shape.num_dimensions()); + + for(unsigned int d = 0; d < region.anchor.num_dimensions(); ++d) + { + ARM_COMPUTE_EXPECT_EQUAL(region.anchor[d], reference.anchor[d]); + } + + for(unsigned int d = 0; d < region.shape.num_dimensions(); ++d) + { + ARM_COMPUTE_EXPECT_EQUAL(region.shape[d], reference.shape[d]); + } +} + +void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &reference) +{ + ARM_COMPUTE_EXPECT_EQUAL(padding.top, reference.top); + ARM_COMPUTE_EXPECT_EQUAL(padding.right, reference.right); + ARM_COMPUTE_EXPECT_EQUAL(padding.bottom, reference.bottom); + ARM_COMPUTE_EXPECT_EQUAL(padding.left, reference.left); +} + +void validate(const IAccessor &tensor, const void *reference_value) +{ + ARM_COMPUTE_ASSERT(reference_value != nullptr); + + int64_t num_mismatches = 0; + int64_t num_elements = 0; + const size_t channel_size = element_size_from_data_type(tensor.data_type()); + + // Iterate over all elements, e.g. U8, S16, RGB888, ... + for(int element_idx = 0; element_idx < tensor.num_elements(); ++element_idx) + { + const Coordinates id = index2coord(tensor.shape(), element_idx); + + const auto ptr = static_cast(tensor(id)); + + // Iterate over all channels within one element + for(int channel = 0; channel < tensor.num_channels(); ++channel) + { + const size_t channel_offset = channel * channel_size; + const double target = get_double_data(ptr + channel_offset, tensor.data_type()); + const double ref = get_double_data(reference_value, tensor.data_type()); + const bool equal = is_equal(target, ref); + + ARM_COMPUTE_TEST_INFO("id = " << id); + ARM_COMPUTE_TEST_INFO("channel = " << channel); + ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << target); + ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << ref); + ARM_COMPUTE_EXPECT_EQUAL(target, ref); + + if(!equal) + { + ++num_mismatches; + } + + ++num_elements; + } + } + + if(num_elements > 0) + { + const float percent_mismatches = static_cast(num_mismatches) / num_elements * 100.f; + + ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::setprecision(2) << percent_mismatches << "%) mismatched"); + ARM_COMPUTE_EXPECT_EQUAL(num_mismatches, 0); + } +} + +void validate(const IAccessor &tensor, BorderSize border_size, const BorderMode &border_mode, const void *border_value) +{ + if(border_mode == BorderMode::UNDEFINED) + { + return; + } + else if(border_mode == BorderMode::CONSTANT) + { + ARM_COMPUTE_ASSERT(border_value != nullptr); + } + + int64_t num_mismatches = 0; + int64_t num_elements = 0; + const int slice_size = tensor.shape()[0] * tensor.shape()[1]; + + for(int element_idx = 0; element_idx < tensor.num_elements(); element_idx += slice_size) + { + Coordinates id = index2coord(tensor.shape(), element_idx); + + // Top border + for(int y = -border_size.top; y < 0; ++y) + { + id.set(1, y); + + for(int x = -border_size.left; x < static_cast(tensor.shape()[0]) + static_cast(border_size.right); ++x) + { + id.set(0, x); + + check_border_element(tensor, id, border_mode, border_value, num_elements, num_mismatches); + } + } + + // Bottom border + for(int y = tensor.shape()[1]; y < static_cast(tensor.shape()[1]) + static_cast(border_size.bottom); ++y) + { + id.set(1, y); + + for(int x = -border_size.left; x < static_cast(tensor.shape()[0]) + static_cast(border_size.right); ++x) + { + id.set(0, x); + + check_border_element(tensor, id, border_mode, border_value, num_elements, num_mismatches); + } + } + + // Left/right border + for(int y = 0; y < static_cast(tensor.shape()[1]); ++y) + { + id.set(1, y); + + // Left border + for(int x = -border_size.left; x < 0; ++x) + { + id.set(0, x); + + check_border_element(tensor, id, border_mode, border_value, num_elements, num_mismatches); + } + + // Right border + for(int x = tensor.shape()[0]; x < static_cast(tensor.shape()[0]) + static_cast(border_size.right); ++x) + { + id.set(0, x); + + check_border_element(tensor, id, border_mode, border_value, num_elements, num_mismatches); + } + } + } + + if(num_elements > 0) + { + const float percent_mismatches = static_cast(num_mismatches) / num_elements * 100.f; + + ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::setprecision(2) << percent_mismatches << "%) mismatched"); + ARM_COMPUTE_EXPECT_EQUAL(num_mismatches, 0); + } +} + +void validate(std::vector classified_labels, std::vector expected_labels) +{ + ARM_COMPUTE_EXPECT_EQUAL(classified_labels.size(), expected_labels.size()); + + for(unsigned int i = 0; i < expected_labels.size(); ++i) + { + ARM_COMPUTE_EXPECT_EQUAL(classified_labels[i], expected_labels[i]); + } +} +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation_new/Validation.h b/tests/validation_new/Validation.h new file mode 100644 index 0000000000..fd8a79d238 --- /dev/null +++ b/tests/validation_new/Validation.h @@ -0,0 +1,238 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_TEST_VALIDATION_H__ +#define __ARM_COMPUTE_TEST_VALIDATION_H__ + +#include "SimpleTensor.h" +#include "arm_compute/core/FixedPoint.h" +#include "arm_compute/core/Types.h" +#include "framework/Asserts.h" +#include "tests/IAccessor.h" +#include "tests/TypePrinter.h" +#include "tests/Utils.h" + +#include +#include + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template +bool compare_dimensions(const Dimensions &dimensions1, const Dimensions &dimensions2) +{ + if(dimensions1.num_dimensions() != dimensions2.num_dimensions()) + { + return false; + } + + for(unsigned int i = 0; i < dimensions1.num_dimensions(); ++i) + { + if(dimensions1[i] != dimensions2[i]) + { + return false; + } + } + + return true; +} + +/** Validate valid regions. + * + * - Dimensionality has to be the same. + * - Anchors have to match. + * - Shapes have to match. + */ +void validate(const arm_compute::ValidRegion ®ion, const arm_compute::ValidRegion &reference); + +/** Validate padding. + * + * Padding on all sides has to be the same. + */ +void validate(const arm_compute::PaddingSize &padding, const arm_compute::PaddingSize &reference); + +/** Validate tensors. + * + * - Dimensionality has to be the same. + * - All values have to match. + * + * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to + * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between + * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by + * other test cases. + */ +template +void validate(const IAccessor &tensor, const SimpleTensor &reference, U tolerance_value = 0, float tolerance_number = 0.f); + +/** Validate tensors with valid region. + * + * - Dimensionality has to be the same. + * - All values have to match. + * + * @note: wrap_range allows cases where reference tensor rounds up to the wrapping point, causing it to wrap around to + * zero while the test tensor stays at wrapping point to pass. This may permit true erroneous cases (difference between + * reference tensor and test tensor is multiple of wrap_range), but such errors would be detected by + * other test cases. + */ +template +void validate(const IAccessor &tensor, const SimpleTensor &reference, const ValidRegion &valid_region, U tolerance_value = 0, float tolerance_number = 0.f); + +/** Validate tensors against constant value. + * + * - All values have to match. + */ +void validate(const IAccessor &tensor, const void *reference_value); + +/** Validate border against a constant value. + * + * - All border values have to match the specified value if mode is CONSTANT. + * - All border values have to be replicated if mode is REPLICATE. + * - Nothing is validated for mode UNDEFINED. + */ +void validate(const IAccessor &tensor, BorderSize border_size, const BorderMode &border_mode, const void *border_value); + +/** Validate classified labels against expected ones. + * + * - All values should match + */ +void validate(std::vector classified_labels, std::vector expected_labels); + +/** Validate float value. + * + * - All values should match + */ +template +void validate(T target, T ref, U tolerance_abs_error = std::numeric_limits::epsilon(), double tolerance_relative_error = 0.0001f); + +template +bool is_equal(T target, T ref, U max_absolute_error = std::numeric_limits::epsilon(), double max_relative_error = 0.0001f) +{ + if(!std::isfinite(target) || !std::isfinite(ref)) + { + return false; + } + + // No need further check if they are equal + if(ref == target) + { + return true; + } + + // Need this check for the situation when the two values close to zero but have different sign + if(std::abs(std::abs(ref) - std::abs(target)) <= max_absolute_error) + { + return true; + } + + double relative_error = 0; + + if(std::abs(target) > std::abs(ref)) + { + relative_error = std::abs(static_cast(target - ref) / target); + } + else + { + relative_error = std::abs(static_cast(ref - target) / ref); + } + + return relative_error <= max_relative_error; +} + +template +void validate(const IAccessor &tensor, const SimpleTensor &reference, U tolerance_value, float tolerance_number) +{ + // Validate with valid region covering the entire shape + validate(tensor, reference, shape_to_valid_region(tensor.shape()), tolerance_value, tolerance_number); +} + +template +void validate(const IAccessor &tensor, const SimpleTensor &reference, const ValidRegion &valid_region, U tolerance_value, float tolerance_number) +{ + int64_t num_mismatches = 0; + int64_t num_elements = 0; + + ARM_COMPUTE_EXPECT_EQUAL(tensor.element_size(), reference.element_size()); + ARM_COMPUTE_EXPECT_EQUAL(tensor.format(), reference.format()); + ARM_COMPUTE_EXPECT_EQUAL(tensor.data_type(), reference.data_type()); + ARM_COMPUTE_EXPECT_EQUAL(tensor.num_channels(), reference.num_channels()); + ARM_COMPUTE_EXPECT(compare_dimensions(tensor.shape(), reference.shape())); + + const int min_elements = std::min(tensor.num_elements(), reference.num_elements()); + const int min_channels = std::min(tensor.num_channels(), reference.num_channels()); + + // Iterate over all elements within valid region, e.g. U8, S16, RGB888, ... + for(int element_idx = 0; element_idx < min_elements; ++element_idx) + { + const Coordinates id = index2coord(reference.shape(), element_idx); + + if(is_in_valid_region(valid_region, id)) + { + // Iterate over all channels within one element + for(int c = 0; c < min_channels; ++c) + { + const T &target_value = reinterpret_cast(tensor(id))[c]; + const T &reference_value = reinterpret_cast(reference(id))[c]; + + if(!is_equal(target_value, reference_value, tolerance_value)) + { + ARM_COMPUTE_TEST_INFO("id = " << id); + ARM_COMPUTE_TEST_INFO("channel = " << c); + ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << target_value); + ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << reference_value); + ARM_COMPUTE_EXPECT_EQUAL(target_value, reference_value); + + ++num_mismatches; + } + + ++num_elements; + } + } + } + + if(num_elements > 0) + { + const int64_t absolute_tolerance_number = tolerance_number * num_elements; + const float percent_mismatches = static_cast(num_mismatches) / num_elements * 100.f; + + ARM_COMPUTE_TEST_INFO(num_mismatches << " values (" << std::setprecision(2) << percent_mismatches + << "%) mismatched (maximum tolerated " << std::setprecision(2) << tolerance_number << "%)"); + ARM_COMPUTE_EXPECT(num_mismatches <= absolute_tolerance_number); + } +} + +template +void validate(T target, T ref, U tolerance_abs_error, double tolerance_relative_error) +{ + const bool equal = is_equal(target, ref, tolerance_abs_error, tolerance_relative_error); + + ARM_COMPUTE_TEST_INFO("reference = " << std::setprecision(5) << ref); + ARM_COMPUTE_TEST_INFO("target = " << std::setprecision(5) << target); + ARM_COMPUTE_EXPECT(equal); +} +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_REFERENCE_VALIDATION_H__ */ -- cgit v1.2.1