From 70f946b02c09632c5b4550939d62840e20a9caa1 Mon Sep 17 00:00:00 2001 From: John Richardson Date: Mon, 2 Oct 2017 16:52:16 +0100 Subject: COMPMID-489: Move FixedPoint to new validation Change-Id: Ibb94f44550469ec22553b1f947a7753d3ff90d93 Reviewed-on: http://mpd-gerrit.cambridge.arm.com/90138 Tested-by: Kaizen Reviewed-by: Anthony Barbier --- tests/AssetsLibrary.cpp | 5 + tests/AssetsLibrary.h | 7 +- tests/SConscript | 3 + tests/Types.h | 3 + tests/validation/UNIT/FixedPoint.cpp | 202 +++++++++++++++++++++++++++++++ tests/validation_old/UNIT/FixedPoint.cpp | 164 ------------------------- utils/TypePrinter.h | 10 ++ 7 files changed, 228 insertions(+), 166 deletions(-) create mode 100644 tests/validation/UNIT/FixedPoint.cpp delete mode 100644 tests/validation_old/UNIT/FixedPoint.cpp diff --git a/tests/AssetsLibrary.cpp b/tests/AssetsLibrary.cpp index 7e380fa8f3..87b0389039 100644 --- a/tests/AssetsLibrary.cpp +++ b/tests/AssetsLibrary.cpp @@ -191,6 +191,11 @@ AssetsLibrary::AssetsLibrary(std::string path, std::random_device::result_type s { } +std::string AssetsLibrary::path() const +{ + return _library_path; +} + std::random_device::result_type AssetsLibrary::seed() const { return _seed; diff --git a/tests/AssetsLibrary.h b/tests/AssetsLibrary.h index 6199a09012..ee136447ee 100644 --- a/tests/AssetsLibrary.h +++ b/tests/AssetsLibrary.h @@ -58,14 +58,17 @@ namespace test class AssetsLibrary final { public: - /** Initialises the library with a @p path to the image directory. + /** Initialises the library with a @p path to the assets directory. * Furthermore, sets the seed for the random generator to @p seed. * - * @param[in] path Path to load images from. + * @param[in] path Path to load assets from. * @param[in] seed Seed used to initialise the random number generator. */ AssetsLibrary(std::string path, std::random_device::result_type seed); + /** Path to assets directory used to initialise library. */ + std::string path() const; + /** Seed that is used to fill tensors with random values. */ std::random_device::result_type seed() const; diff --git a/tests/SConscript b/tests/SConscript index 9e6da9367d..007a0aa66a 100644 --- a/tests/SConscript +++ b/tests/SConscript @@ -81,6 +81,9 @@ common_objects = [test_env.StaticObject(f) for f in common_files] files_benchmark = Glob('benchmark/*.cpp') files_validation = Glob('validation/*.cpp') +# Add unit tests +files_validation += Glob('validation/UNIT/*.cpp') + # Always compile reference for validation files_validation += Glob('validation/CPP/*.cpp') diff --git a/tests/Types.h b/tests/Types.h index 516f61c44b..8f47902e3e 100644 --- a/tests/Types.h +++ b/tests/Types.h @@ -33,6 +33,9 @@ namespace arm_compute /** Fixed point operation */ enum class FixedPointOp { + ADD, /**< Addition */ + SUB, /**< Subtraction */ + MUL, /**< Multiplication */ EXP, /**< Exponential */ LOG, /**< Logarithm */ INV_SQRT, /**< Inverse square root */ diff --git a/tests/validation/UNIT/FixedPoint.cpp b/tests/validation/UNIT/FixedPoint.cpp new file mode 100644 index 0000000000..627a779f5f --- /dev/null +++ b/tests/validation/UNIT/FixedPoint.cpp @@ -0,0 +1,202 @@ +/* + * 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 "tests/validation/FixedPoint.h" + +#include "tests/Globals.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Macros.h" +#include "tests/framework/datasets/Datasets.h" +#include "tests/validation/Validation.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +const auto FuncNamesDataset = framework::dataset::make("FunctionNames", { FixedPointOp::ADD, + FixedPointOp::SUB, + FixedPointOp::MUL, + FixedPointOp::EXP, + FixedPointOp::LOG, + FixedPointOp::INV_SQRT + }); +} // namespace + +TEST_SUITE(UNIT) +TEST_SUITE(FixedPoint) + +// *INDENT-OFF* +// clang-format off +DATA_TEST_CASE(FixedPointQS8Inputs, framework::DatasetMode::ALL, combine( + FuncNamesDataset, + framework::dataset::make("FractionalBits", 1, 7)), + func_name, frac_bits) +// clang-format on +// *INDENT-ON* +{ + std::vector data; + std::vector shape; //NOLINT + + std::string func_name_lower = to_string(func_name); + std::transform(func_name_lower.begin(), func_name_lower.end(), func_name_lower.begin(), ::tolower); + + const std::string inputs_file = library->path() + + "fixed_point/" + + func_name_lower + + "_Q8." + + support::cpp11::to_string(frac_bits) + + ".in.npy"; + + ARM_COMPUTE_TEST_INFO(inputs_file); + npy::LoadArrayFromNumpy(inputs_file, shape, data); + + // Values stored as doubles so reinterpret as floats + const auto *float_val = reinterpret_cast(&data[0]); + const size_t num_elements = data.size() * sizeof(double) / sizeof(float); + + for(unsigned int i = 0; i < num_elements; ++i) + { + // Convert to fixed point + fixed_point_arithmetic::fixed_point in_val(float_val[i], frac_bits); + + // Check that the value didn't change + ARM_COMPUTE_EXPECT(static_cast(in_val) == float_val[i], framework::LogLevel::ERRORS); + } +} + +//FIXME: Figure out how to handle expected failures properly +// The last input argument specifies the expected number of failures for a +// given combination of (function name, number of fractional bits) as defined +// by the first two arguments. + +// *INDENT-OFF* +// clang-format off +DATA_TEST_CASE(FixedPointQS8Outputs, framework::DatasetMode::ALL, zip(combine( + FuncNamesDataset, + framework::dataset::make("FractionalBits", 1, 7)), + framework::dataset::make("ExpectedFailures", { 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 7, 8, 13, 2, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 5, 33, 96 })), + func_name, frac_bits, expected_failures) +// clang-format on +// *INDENT-ON* +{ + std::vector in_data; + std::vector in_shape; //NOLINT + + std::vector out_data; + std::vector out_shape; //NOLINT + + std::string func_name_lower = to_string(func_name); + std::transform(func_name_lower.begin(), func_name_lower.end(), func_name_lower.begin(), ::tolower); + + const std::string base_file_name = library->path() + + "fixed_point/" + + func_name_lower + + "_Q8." + + support::cpp11::to_string(frac_bits); + + const std::string inputs_file = base_file_name + ".in.npy"; + const std::string reference_file = base_file_name + ".out.npy"; + + ARM_COMPUTE_TEST_INFO(base_file_name + ".in"); + npy::LoadArrayFromNumpy(inputs_file, in_shape, in_data); + + ARM_COMPUTE_TEST_INFO(base_file_name + ".out"); + npy::LoadArrayFromNumpy(reference_file, out_shape, out_data); + + ARM_COMPUTE_EXPECT(in_shape.front() == out_shape.front(), framework::LogLevel::ERRORS); + + const float step_size = std::pow(2.f, -frac_bits); + int64_t num_mismatches = 0; + + // Values stored as doubles so reinterpret as floats + const auto *float_val = reinterpret_cast(&in_data[0]); + const auto *ref_val = reinterpret_cast(&out_data[0]); + + const size_t num_elements = in_data.size() * sizeof(double) / sizeof(float); + + for(unsigned int i = 0; i < num_elements; ++i) + { + fixed_point_arithmetic::fixed_point in_val(float_val[i], frac_bits); + fixed_point_arithmetic::fixed_point out_val(0.f, frac_bits); + + float tolerance = 0.f; + + if(func_name == FixedPointOp::ADD) + { + out_val = in_val + in_val; + } + else if(func_name == FixedPointOp::SUB) + { + out_val = in_val - in_val; //NOLINT + } + else if(func_name == FixedPointOp::MUL) + { + tolerance = 1.f * step_size; + out_val = in_val * in_val; + } + else if(func_name == FixedPointOp::EXP) + { + tolerance = 2.f * step_size; + out_val = fixed_point_arithmetic::exp(in_val); + } + else if(func_name == FixedPointOp::LOG) + { + tolerance = 4.f * step_size; + out_val = fixed_point_arithmetic::log(in_val); + } + else if(func_name == FixedPointOp::INV_SQRT) + { + tolerance = 5.f * step_size; + out_val = fixed_point_arithmetic::inv_sqrt(in_val); + } + + if(std::abs(static_cast(out_val) - ref_val[i]) > tolerance) + { + ARM_COMPUTE_TEST_INFO("input = " << in_val); + ARM_COMPUTE_TEST_INFO("output = " << out_val); + ARM_COMPUTE_TEST_INFO("reference = " << ref_val[i]); + ARM_COMPUTE_TEST_INFO("tolerance = " << tolerance); + + ARM_COMPUTE_TEST_INFO((std::abs(static_cast(out_val) - ref_val[i]) <= tolerance)); + + ++num_mismatches; + } + } + + ARM_COMPUTE_EXPECT(num_mismatches == expected_failures, framework::LogLevel::ERRORS); +} + +TEST_SUITE_END() +TEST_SUITE_END() +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation_old/UNIT/FixedPoint.cpp b/tests/validation_old/UNIT/FixedPoint.cpp deleted file mode 100644 index 26cf905abf..0000000000 --- a/tests/validation_old/UNIT/FixedPoint.cpp +++ /dev/null @@ -1,164 +0,0 @@ -/* - * 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 "tests/validation_old/FixedPoint.h" - -#include "Utils.h" -#include "support/ToolchainSupport.h" -#include "tests/validation_old/Validation.h" -#include "tests/validation_old/ValidationUserConfiguration.h" -#include "utils/TypePrinter.h" - -#include "tests/validation_old/boost_wrapper.h" - -#include -#include - -using namespace arm_compute; -using namespace arm_compute::test; -using namespace arm_compute::test::validation; - -namespace -{ -std::string func_names[] = -{ - "add", "sub", "mul", "exp", "log", "inv_sqrt" -}; -} // namespace - -#ifndef DOXYGEN_SKIP_THIS -BOOST_AUTO_TEST_SUITE(UNIT) -BOOST_AUTO_TEST_SUITE(FixedPoint) - -BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly")) -BOOST_DATA_TEST_CASE(FixedPointQS8Inputs, boost::unit_test::data::make(func_names) * boost::unit_test::data::xrange(1, 7), func_name, frac_bits) -{ - const std::string base_file_name = user_config.path.get() + "/dumps/" + func_name + "_Q8." + support::cpp11::to_string(frac_bits); - std::ifstream inputs_file{ base_file_name + ".in", std::ios::binary | std::ios::in }; - - BOOST_TEST_INFO(base_file_name + ".in"); - BOOST_TEST_REQUIRE(inputs_file.good()); //FIXME: When moving to new framework: throw a FileNotFound exception - - float float_val = 0.f; - - // Read first value - inputs_file.read(reinterpret_cast(&float_val), sizeof(float_val)); - - while(inputs_file.good()) - { - // Convert to fixed point - fixed_point_arithmetic::fixed_point in_val(float_val, frac_bits); - - // Check that the value didn't change - BOOST_TEST(static_cast(in_val) == float_val); - - // Read next value - inputs_file.read(reinterpret_cast(&float_val), sizeof(float_val)); - } -} - -BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly")) -//FIXME: Figure out how to handle expected failures properly -// The last input argument specifies the expected number of failures for a -// given combination of (function name, number of fractional bits) as defined -// by the first two arguments. -BOOST_DATA_TEST_CASE(FixedPointQS8Outputs, (boost::unit_test::data::make(func_names) * boost::unit_test::data::xrange(1, 7)) ^ (boost::unit_test::data::make({ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 8, 13, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 33, 96 })), - func_name, frac_bits, expected_failures) -{ - const std::string base_file_name = user_config.path.get() + "/dumps/" + func_name + "_Q8." + support::cpp11::to_string(frac_bits); - std::ifstream inputs_file{ base_file_name + ".in", std::ios::binary | std::ios::in }; - std::ifstream reference_file{ base_file_name + ".out", std::ios::binary | std::ios::in }; - - BOOST_TEST_INFO(base_file_name + ".in"); - BOOST_TEST_REQUIRE(inputs_file.good()); //FIXME: When moving to new framework: throw a FileNotFound exception - BOOST_TEST_INFO(base_file_name + ".out"); - BOOST_TEST_REQUIRE(reference_file.good()); //FIXME: When moving to new framework: throw a FileNotFound exception - - const float step_size = std::pow(2.f, -frac_bits); - - float float_val = 0.f; - float ref_val = 0.f; - int64_t num_mismatches = 0; - - // Read first values - inputs_file.read(reinterpret_cast(&float_val), sizeof(float_val)); - reference_file.read(reinterpret_cast(&ref_val), sizeof(ref_val)); - - while(inputs_file.good() && reference_file.good()) - { - fixed_point_arithmetic::fixed_point in_val(float_val, frac_bits); - fixed_point_arithmetic::fixed_point out_val(0.f, frac_bits); - - float tolerance = 0.f; - - if(func_name == "add") - { - out_val = in_val + in_val; - } - else if(func_name == "sub") - { - out_val = in_val - in_val; //NOLINT - } - else if(func_name == "mul") - { - tolerance = 1.f * step_size; - out_val = in_val * in_val; - } - else if(func_name == "exp") - { - tolerance = 2.f * step_size; - out_val = fixed_point_arithmetic::exp(in_val); - } - else if(func_name == "log") - { - tolerance = 4.f * step_size; - out_val = fixed_point_arithmetic::log(in_val); - } - else if(func_name == "inv_sqrt") - { - tolerance = 5.f * step_size; - out_val = fixed_point_arithmetic::inv_sqrt(in_val); - } - - if(std::abs(static_cast(out_val) - ref_val) > tolerance) - { - BOOST_TEST_INFO("input = " << in_val); - BOOST_TEST_INFO("output = " << out_val); - BOOST_TEST_INFO("reference = " << ref_val); - BOOST_TEST_INFO("tolerance = " << tolerance); - BOOST_TEST_WARN((std::abs(static_cast(out_val) - ref_val) <= tolerance)); - - ++num_mismatches; - } - - // Read next values - inputs_file.read(reinterpret_cast(&float_val), sizeof(float_val)); - reference_file.read(reinterpret_cast(&ref_val), sizeof(ref_val)); - } - - BOOST_TEST(num_mismatches == expected_failures); -} - -BOOST_AUTO_TEST_SUITE_END() -BOOST_AUTO_TEST_SUITE_END() -#endif /* DOXYGEN_SKIP_THIS */ diff --git a/utils/TypePrinter.h b/utils/TypePrinter.h index c955584c76..4f424d9438 100644 --- a/utils/TypePrinter.h +++ b/utils/TypePrinter.h @@ -153,6 +153,15 @@ inline ::std::ostream &operator<<(::std::ostream &os, const FixedPointOp &op) { switch(op) { + case FixedPointOp::ADD: + os << "ADD"; + break; + case FixedPointOp::SUB: + os << "SUB"; + break; + case FixedPointOp::MUL: + os << "MUL"; + break; case FixedPointOp::EXP: os << "EXP"; break; @@ -171,6 +180,7 @@ inline ::std::ostream &operator<<(::std::ostream &os, const FixedPointOp &op) return os; } + inline std::string to_string(const FixedPointOp &op) { std::stringstream str; -- cgit v1.2.1