blob: 0e534762d1e6c0034d13f5f5a7af373aad9290e9 [file] [log] [blame]
Anthony Barbierdbdab852017-06-23 15:42:00 +01001/*
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#include "validation/FixedPoint.h"
25
26#include "TypePrinter.h"
27#include "Utils.h"
28#include "validation/Validation.h"
29#include "validation/ValidationUserConfiguration.h"
30
31#include "boost_wrapper.h"
32
33#include <fstream>
34#include <vector>
35
36using namespace arm_compute;
37using namespace arm_compute::test;
38using namespace arm_compute::test::validation;
39
40namespace
41{
42std::string func_names[] =
43{
44 "add", "sub", "mul", "exp", "log", "inv_sqrt"
45};
46} // namespace
47
48#ifndef DOXYGEN_SKIP_THIS
49BOOST_AUTO_TEST_SUITE(UNIT)
50BOOST_AUTO_TEST_SUITE(FixedPoint)
51
52BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly"))
53BOOST_DATA_TEST_CASE(FixedPointQS8Inputs, boost::unit_test::data::make(func_names) * boost::unit_test::data::xrange(1, 7), func_name, frac_bits)
54{
55 const std::string base_file_name = user_config.path.get() + "/dumps/" + func_name + "_Q8." + cpp11::to_string(frac_bits);
56 std::ifstream inputs_file{ base_file_name + ".in", std::ios::binary | std::ios::in };
57
58 BOOST_TEST_INFO(base_file_name + ".in");
59 BOOST_TEST_REQUIRE(inputs_file.good());
60
61 float float_val = 0.f;
62
63 // Read first value
64 inputs_file.read(reinterpret_cast<char *>(&float_val), sizeof(float_val));
65
66 while(inputs_file.good())
67 {
68 // Convert to fixed point
69 fixed_point_arithmetic::fixed_point<int8_t> in_val(float_val, frac_bits);
70
71 // Check that the value didn't change
72 BOOST_TEST(static_cast<float>(in_val) == float_val);
73
74 // Read next value
75 inputs_file.read(reinterpret_cast<char *>(&float_val), sizeof(float_val));
76 }
77}
78
79BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly"))
80// The last input argument specifies the expected number of failures for a
81// given combination of (function name, number of fractional bits) as defined
82// by the first two arguments.
83BOOST_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, 11, 32, 67 })),
84 func_name, frac_bits, expected_failures)
85{
86 const std::string base_file_name = user_config.path.get() + "/dumps/" + func_name + "_Q8." + cpp11::to_string(frac_bits);
87 std::ifstream inputs_file{ base_file_name + ".in", std::ios::binary | std::ios::in };
88 std::ifstream reference_file{ base_file_name + ".out", std::ios::binary | std::ios::in };
89
90 BOOST_TEST_INFO(base_file_name + ".in");
91 BOOST_TEST_REQUIRE(inputs_file.good());
92 BOOST_TEST_INFO(base_file_name + ".out");
93 BOOST_TEST_REQUIRE(reference_file.good());
94
95 const float step_size = std::pow(2.f, -frac_bits);
96
97 float float_val = 0.f;
98 float ref_val = 0.f;
99 int64_t num_mismatches = 0;
100
101 // Read first values
102 inputs_file.read(reinterpret_cast<char *>(&float_val), sizeof(float_val));
103 reference_file.read(reinterpret_cast<char *>(&ref_val), sizeof(ref_val));
104
105 while(inputs_file.good() && reference_file.good())
106 {
107 fixed_point_arithmetic::fixed_point<int8_t> in_val(float_val, frac_bits);
108 fixed_point_arithmetic::fixed_point<int8_t> out_val(0.f, frac_bits);
109
110 float tolerance = 0.f;
111
112 if(func_name == "add")
113 {
114 out_val = in_val + in_val;
115 }
116 else if(func_name == "sub")
117 {
118 out_val = in_val - in_val; //NOLINT
119 }
120 else if(func_name == "mul")
121 {
122 tolerance = 1.f * step_size;
123 out_val = in_val * in_val;
124 }
125 else if(func_name == "exp")
126 {
127 tolerance = 2.f * step_size;
128 out_val = fixed_point_arithmetic::exp(in_val);
129 }
130 else if(func_name == "log")
131 {
132 tolerance = 4.f * step_size;
133 out_val = fixed_point_arithmetic::log(in_val);
134 }
135 else if(func_name == "inv_sqrt")
136 {
137 tolerance = 5.f * step_size;
138 out_val = fixed_point_arithmetic::inv_sqrt(in_val);
139 }
140
141 BOOST_TEST_INFO("input = " << in_val);
142 BOOST_TEST_INFO("output = " << out_val);
143 BOOST_TEST_INFO("reference = " << ref_val);
144 BOOST_TEST_INFO("tolerance = " << tolerance);
145 BOOST_TEST_WARN((std::abs(static_cast<float>(out_val) - ref_val) <= tolerance));
146
147 if(std::abs(static_cast<float>(out_val) - ref_val) > tolerance)
148 {
149 ++num_mismatches;
150 }
151
152 // Read next values
153 inputs_file.read(reinterpret_cast<char *>(&float_val), sizeof(float_val));
154 reference_file.read(reinterpret_cast<char *>(&ref_val), sizeof(ref_val));
155 }
156
157 BOOST_TEST(num_mismatches == expected_failures);
158}
159
160BOOST_AUTO_TEST_SUITE_END()
161BOOST_AUTO_TEST_SUITE_END()
162#endif