blob: fabd9ad16100216f4e4e6282dde0011ef2fdf9df [file] [log] [blame]
Michalis Spyroud7e82812017-06-20 15:00:14 +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 "CL/CLAccessor.h"
25#include "Globals.h"
26#include "TensorLibrary.h"
27#include "TypePrinter.h"
28#include "Utils.h"
29#include "validation/Datasets.h"
30#include "validation/Reference.h"
31#include "validation/Validation.h"
32
33#include "arm_compute/core/CL/CLKernelLibrary.h"
34#include "arm_compute/core/CL/ICLKernel.h"
35#include "arm_compute/core/CL/OpenCL.h"
36#include "arm_compute/core/Helpers.h"
37#include "arm_compute/core/Types.h"
38#include "arm_compute/core/Window.h"
39#include "arm_compute/runtime/CL/CLScheduler.h"
40#include "arm_compute/runtime/CL/CLSubTensor.h"
41#include "arm_compute/runtime/CL/CLTensor.h"
42#include "arm_compute/runtime/CL/CLTensorAllocator.h"
43
44#include "arm_compute/core/CL/ICLTensor.h"
45
46#include "boost_wrapper.h"
47
48#include <random>
49#include <string>
50
51using namespace arm_compute;
52using namespace arm_compute::test;
53using namespace arm_compute::test::cl;
54using namespace arm_compute::test::validation;
55
56namespace
57{
58const float tolerance_exp = 1.0f; /**< Tolerance value for comparing reference's output against implementation's output (exponential)*/
59const float tolerance_invsqrt = 4.0f; /**< Tolerance value for comparing reference's output against implementation's output (inverse square-root) */
60const float tolerance_log = 5.0f; /**< Tolerance value for comparing reference's output against implementation's output (logarithm) */
61
62/** Compute Neon fixed point operation for signed 8bit fixed point.
63 *
64 * @param[in] shape Shape of the input and output tensors.
65 *
66 * @return Computed output tensor.
67 */
68CLTensor compute_fixed_point_op(const TensorShape &shape, int fixed_point_position, FixedPointOp op)
69{
70 std::string fixed_point_operation_kernel;
71#ifndef EMBEDDED_KERNELS
72 fixed_point_operation_kernel += "#include \"fixed_point.h\"\n";
73#endif
74 fixed_point_operation_kernel +=
75 "__kernel void fixed_point_operation_qs8( \n"
76 " __global char* src, \n"
77 " __global char* dst) \n"
78 "{ \n"
79 " char16 in = vload16(0, src + get_global_id(0) * 16); \n"
80 " if(FIXED_POINT_OP == 0) \n"
81 " { \n"
82 " vstore16(EXP_OP_EXPAND(in, DATA_TYPE, 16, FIXED_POINT_POS), 0, dst + get_global_id(0) * 16); \n"
83 " } \n"
84 " else if(FIXED_POINT_OP == 1) \n"
85 " { \n"
86 " vstore16(INVSQRT_OP_EXPAND(in, DATA_TYPE, 16, FIXED_POINT_POS), 0, dst + get_global_id(0) * 16); \n"
87 " } \n"
88 " else \n"
89 " { \n"
90 " vstore16(LOG_OP_EXPAND(in, DATA_TYPE, 16, FIXED_POINT_POS), 0, dst + get_global_id(0) * 16); \n"
91 " } \n"
92 "} \n"
93 "\n";
94
95 // Create tensors
96 CLTensor src = create_tensor<CLTensor>(shape, DataType::QS8, 1, fixed_point_position);
97 CLTensor dst = create_tensor<CLTensor>(shape, DataType::QS8, 1, fixed_point_position);
98
99 // Allocate tensors
100 src.allocator()->allocate();
101 dst.allocator()->allocate();
102
103 BOOST_TEST(!src.info()->is_resizable());
104 BOOST_TEST(!dst.info()->is_resizable());
105
106 // Set build options
107 std::string build_opts = "-DFIXED_POINT_POS=" + val_to_string<int>(fixed_point_position);
108 build_opts += " -DDATA_TYPE=qs8";
109
110 // Fill tensors.
111 int min = 0;
112 int max = 0;
113 switch(op)
114 {
115 case FixedPointOp::EXP:
116 min = -(1 << (fixed_point_position - 1));
117 max = (1 << (fixed_point_position - 1));
118 build_opts += " -DFIXED_POINT_OP=0";
119 break;
120 case FixedPointOp::INV_SQRT:
121 min = 1;
122 max = 0x7F;
123 build_opts += " -DFIXED_POINT_OP=1";
124 break;
125 case FixedPointOp::LOG:
126 min = (1 << (fixed_point_position - 1));
127 max = 0x3F;
128 build_opts += " -DFIXED_POINT_OP=2";
129 break;
130 default:
131 ARM_COMPUTE_ERROR("Operation not supported");
132 }
133
134 std::uniform_int_distribution<> distribution(min, max);
135 library->fill(CLAccessor(src), distribution, 0);
136
137 std::vector<std::string> sources;
138
139#ifndef EMBEDDED_KERNELS
140 build_opts += " -I" + CLKernelLibrary::get().get_kernel_path();
141#else
142 sources.push_back(CLKernelLibrary::get().get_program_source("fixed_point.h"));
143#endif /* EMBEDDED_KERNELS */
144
145 sources.push_back(fixed_point_operation_kernel);
146
147 // Create program
148 ::cl::Program program = ::cl::Program(sources);
149
150 // Build program
151 program.build(build_opts.c_str());
152
153 ::cl::Kernel kernel = ::cl::Kernel(program, "fixed_point_operation_qs8", nullptr);
154
155 unsigned int idx = 0;
156 kernel.setArg(idx++, src.cl_buffer());
157 kernel.setArg(idx++, dst.cl_buffer());
158
159 ::cl::NDRange gws(shape[0] / 16, 1, 1);
160 CLScheduler::get().queue().enqueueNDRangeKernel(kernel, 0, gws);
161
162 return dst;
163}
164} // namespace
165
166#ifndef DOXYGEN_SKIP_THIS
167BOOST_AUTO_TEST_SUITE(CL)
168BOOST_AUTO_TEST_SUITE(FixedPoint)
169BOOST_AUTO_TEST_SUITE(QS8)
170
171BOOST_AUTO_TEST_SUITE(Exp)
172
173BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly"))
174BOOST_DATA_TEST_CASE(RunSmall, Small1DShape() * boost::unit_test::data::xrange(1, 6), shape, fixed_point_position)
175{
176 // Compute function
177 CLTensor dst = compute_fixed_point_op(shape, fixed_point_position, FixedPointOp::EXP);
178
179 // Compute reference
180 RawTensor ref_dst = Reference::compute_reference_fixed_point_operation(shape, DataType::QS8, DataType::QS8, FixedPointOp::EXP, fixed_point_position);
181
182 // Validate output
183 validate(CLAccessor(dst), ref_dst, tolerance_exp);
184}
185
186BOOST_AUTO_TEST_SUITE_END()
187
188BOOST_AUTO_TEST_SUITE(Log)
189
190BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly"))
191BOOST_DATA_TEST_CASE(RunSmall, Small1DShape() * boost::unit_test::data::xrange(3, 6), shape, fixed_point_position)
192{
193 // Compute function
194 CLTensor dst = compute_fixed_point_op(shape, fixed_point_position, FixedPointOp::LOG);
195
196 // Compute reference
197 RawTensor ref_dst = Reference::compute_reference_fixed_point_operation(shape, DataType::QS8, DataType::QS8, FixedPointOp::LOG, fixed_point_position);
198
199 // Validate output
200 validate(CLAccessor(dst), ref_dst, tolerance_log);
201}
202
203BOOST_AUTO_TEST_SUITE_END()
204
205BOOST_AUTO_TEST_SUITE(Invsqrt)
206
207BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly"))
208BOOST_DATA_TEST_CASE(RunSmall, Small1DShape() * boost::unit_test::data::xrange(1, 6), shape, fixed_point_position)
209{
210 // Compute function
211 CLTensor dst = compute_fixed_point_op(shape, fixed_point_position, FixedPointOp::INV_SQRT);
212
213 // Compute reference
214 RawTensor ref_dst = Reference::compute_reference_fixed_point_operation(shape, DataType::QS8, DataType::QS8, FixedPointOp::INV_SQRT, fixed_point_position);
215
216 // Validate output
217 validate(CLAccessor(dst), ref_dst, tolerance_invsqrt);
218}
219
220BOOST_AUTO_TEST_SUITE_END()
221
222BOOST_AUTO_TEST_SUITE_END()
223BOOST_AUTO_TEST_SUITE_END()
224BOOST_AUTO_TEST_SUITE_END()
225#endif