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