blob: 0e71f9d5bf149bdc3bd2e35497d746d3d3352ce8 [file] [log] [blame]
Isabella Gottardi01a214a2018-04-09 16:00:52 +01001/*
SiCong Lie357a252020-08-09 20:05:52 +01002 * Copyright (c) 2017-2020 Arm Limited.
Isabella Gottardi01a214a2018-04-09 16:00:52 +01003 *
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#ifndef ARM_COMPUTE_CL /* Needed by Utils.cpp to handle OpenCL exceptions properly */
25#error "This example needs to be built with -DARM_COMPUTE_CL"
26#endif /* ARM_COMPUTE_CL */
27
28#include "arm_compute/core/Types.h"
SiCong Lie357a252020-08-09 20:05:52 +010029#include "arm_compute/core/Utils.h"
Isabella Gottardi01a214a2018-04-09 16:00:52 +010030#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
31#include "arm_compute/runtime/CL/CLFunctions.h"
32#include "arm_compute/runtime/CL/CLScheduler.h"
33
34#include "tests/AssetsLibrary.h"
35#include "tests/CL/CLAccessor.h"
36#include "tests/Globals.h"
37#include "tests/IAccessor.h"
38#include "tests/SimpleTensor.h"
39#include "tests/validation/Validation.h"
40#include "tests/validation/reference/GEMM.h"
41#include "tests/validation/reference/GEMMLowp.h"
42
Georgios Pinitas108a95e2019-03-27 13:55:59 +000043#include "utils/TypePrinter.h"
Isabella Gottardi01a214a2018-04-09 16:00:52 +010044#include "utils/Utils.h"
Georgios Pinitas108a95e2019-03-27 13:55:59 +000045#include "utils/command_line/CommandLineOptions.h"
46#include "utils/command_line/CommandLineParser.h"
47
48#include "ValidateExample.h"
Isabella Gottardi01a214a2018-04-09 16:00:52 +010049
50#include <cstdlib>
51
52using namespace arm_compute;
53using namespace utils;
54using namespace arm_compute::test;
55using namespace arm_compute::test::validation;
56
57constexpr float abs_tolerance_f32(0.0001f); /**< F32 Absolute tolerance value for comparing reference's output against implementation's output for
58 * floating point data types in case using relative tolerance fails because of small values */
59RelativeTolerance<float> tolerance_f32(0.001f); /**< F32 Tolerance value for comparing reference's output against implementation's output for floating point data types */
60RelativeTolerance<half_float::half> tolerance_f16(half(0.2)); /**< F16 Tolerance value for comparing reference's output against implementation's output for floating point data types */
61constexpr float tolerance_num_f16 = 0.02f; /**< F16 Tolerance number */
62
Georgios Pinitas108a95e2019-03-27 13:55:59 +000063namespace
64{
65class GEMMCommandLineOptions final
66{
67public:
68 explicit GEMMCommandLineOptions(CommandLineParser &parser) noexcept
69 : help(parser.add_option<ToggleOption>("help")),
70 add_bias(parser.add_option<ToggleOption>("add_bias")),
71 M(parser.add_option<SimpleOption<int>>("m", 7)),
72 N(parser.add_option<SimpleOption<int>>("n", 3)),
73 K(parser.add_option<SimpleOption<int>>("k", 5)),
74 B(parser.add_option<SimpleOption<int>>("b", 1)),
75 alpha(parser.add_option<SimpleOption<float>>("alpha", 1.f)),
76 beta(parser.add_option<SimpleOption<float>>("beta", 0.f)),
77 offset_src0(parser.add_option<SimpleOption<int>>("offset_i0", 10)),
78 offset_src1(parser.add_option<SimpleOption<int>>("offset_i1", 10)),
79 offset_dst(parser.add_option<SimpleOption<int>>("offset_o", 10)),
80 scale_src0(parser.add_option<SimpleOption<float>>("scale_i0", 1.f / 255)),
81 scale_src1(parser.add_option<SimpleOption<float>>("scale_i1", 1.f / 255)),
82 scale_dst(parser.add_option<SimpleOption<float>>("scale_o", 1.f / 255)),
83 data_type()
84 {
85 // Setup data type
86 const std::set<arm_compute::DataType> supported_data_types
87 {
88 DataType::F16,
89 DataType::F32,
90 DataType::QASYMM8,
91 };
92 data_type = parser.add_option<EnumOption<DataType>>("type", supported_data_types, DataType::F32);
93
94 // Setup help strings
95 help->set_help("Show this help message");
96 add_bias->set_help("Add bias to the GEMM. Used when running in QASYMM8");
97 M->set_help("M value");
98 N->set_help("N value");
99 K->set_help("K value");
100 B->set_help("B value - number of batches");
101 alpha->set_help("Alpha value");
102 beta->set_help("Beta value");
103 offset_src0->set_help("Offset of first input. Used when running in QASYMM8");
104 offset_src1->set_help("Offset of second input. Used when running in QASYMM8");
105 offset_dst->set_help("Offset of output. Used when running in QASYMM8");
106 scale_src0->set_help("Scale of first input. Used when running in QASYMM8");
107 scale_src1->set_help("Scale of second input. Used when running in QASYMM8");
108 scale_dst->set_help("Scale of output. Used when running in QASYMM8");
109 data_type->set_help("Data type to use");
110 }
111 /** Prevent instances of this class from being copied (As this class contains pointers) */
112 GEMMCommandLineOptions(const GEMMCommandLineOptions &) = delete;
113 /** Prevent instances of this class from being copied (As this class contains pointers) */
114 GEMMCommandLineOptions &operator=(const GEMMCommandLineOptions &) = delete;
115 /** Allow instances of this class to be moved */
116 GEMMCommandLineOptions(GEMMCommandLineOptions &&) noexcept(true) = default;
117 /** Allow instances of this class to be moved */
118 GEMMCommandLineOptions &operator=(GEMMCommandLineOptions &&) noexcept(true) = default;
119 /** Default destructor */
120 ~GEMMCommandLineOptions() = default;
121
122public:
123 ToggleOption *help;
124 ToggleOption *add_bias;
125 SimpleOption<int> *M;
126 SimpleOption<int> *N;
127 SimpleOption<int> *K;
128 SimpleOption<int> *B;
129 SimpleOption<float> *alpha;
130 SimpleOption<float> *beta;
131 SimpleOption<int> *offset_src0;
132 SimpleOption<int> *offset_src1;
133 SimpleOption<int> *offset_dst;
134 SimpleOption<float> *scale_src0;
135 SimpleOption<float> *scale_src1;
136 SimpleOption<float> *scale_dst;
137 EnumOption<arm_compute::DataType> *data_type;
138};
139} // namespace
140
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100141class CLGEMMValidateExample : public ValidateExample
142{
143public:
Anthony Barbiere88b9bb2018-07-12 13:26:27 +0100144 bool do_setup(int argc, char **argv) override
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100145 {
146 CLScheduler::get().default_init();
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000147
148 // Parse options
149 CommandLineParser parser;
150 GEMMCommandLineOptions gemm_options(parser);
151 parser.parse(argc, argv);
152
153 // Print help
154 const bool print_help = gemm_options.help->is_set() ? gemm_options.help->value() : false;
155 if(print_help)
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100156 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000157 parser.print_help(argv[0]);
158 return false;
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100159 }
160
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000161 // Consume parameters
162 consume_params(gemm_options);
163 print_parameters_internal();
164
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100165 const bool is_quantized = is_data_type_quantized(data_type);
166
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000167 // Calculate re-quantization parameters
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100168 if(is_quantized)
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100169 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000170 float multiplier = scale_src0 * scale_src1 / scale_dst;
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100171 quantization::calculate_quantized_multiplier(multiplier, &dst_multiplier, &dst_shift);
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100172 }
173
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000174 // Initialize GEMM inputs/outputs
175 src0.allocator()->init(TensorInfo(TensorShape(K, M, B), 1, data_type));
176 src1.allocator()->init(TensorInfo(TensorShape(N, K, B), 1, data_type));
177 src2.allocator()->init(TensorInfo(TensorShape(N, M, B), 1, data_type));
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100178 init_sgemm_output(dst, src0, src1, data_type);
179
180 // Configure function
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100181 if(is_quantized)
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100182 {
183 src0.info()->set_quantization_info(QuantizationInfo(scale_src0, offset_src0));
184 src1.info()->set_quantization_info(QuantizationInfo(scale_src1, offset_src1));
185 dst.info()->set_quantization_info(QuantizationInfo(scale_dst, offset_dst));
186 biases.allocator()->init(TensorInfo(TensorShape(N), 1, DataType::S32));
187 init_sgemm_output(tmp_dst, src0, src1, DataType::S32);
188
189 // Configure GEMMlowp matrix multiply function
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100190 mm_gemmlowp.configure(&src0, &src1, nullptr, &tmp_dst);
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100191
192 // Configure GEMMlowp output stage
193 mm_gemmlowp_output_stage.configure(&tmp_dst, add_bias ? &biases : nullptr, &dst, dst_multiplier, dst_shift, offset_dst);
194 tmp_dst.allocator()->allocate();
195 biases.allocator()->allocate();
196 fill(CLAccessor(biases), 3);
197 }
198 else
199 {
200 // Configure matrix multiply function
201 mm_gemm.configure(&src0, &src1, &src2, &dst, alpha, beta);
202 }
203
204 // Allocate all the tensors
205 src0.allocator()->allocate();
206 src1.allocator()->allocate();
207 dst.allocator()->allocate();
208 src2.allocator()->allocate();
209
210 fill(CLAccessor(src0), 0);
211 fill(CLAccessor(src1), 1);
212 fill(CLAccessor(src2), 2);
Anthony Barbiere88b9bb2018-07-12 13:26:27 +0100213
214 return true;
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100215 }
216
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000217 void print_parameters_internal()
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100218 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000219 std::cout << "Datatype : " << string_from_data_type(data_type) << "\n";
220 std::cout << "M : " << support::cpp11::to_string(M) << "\n";
221 std::cout << "N : " << support::cpp11::to_string(N) << "\n";
222 std::cout << "K : " << support::cpp11::to_string(K) << "\n";
223 std::cout << "B : " << support::cpp11::to_string(B) << "\n";
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100224 if(data_type == DataType::QASYMM8)
225 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000226 std::cout << "Scale_Src0 : " << support::cpp11::to_string(scale_src0) << "\n";
227 std::cout << "Offset_Src0 : " << support::cpp11::to_string(offset_src0) << "\n";
228 std::cout << "Scale_Scr1 : " << support::cpp11::to_string(scale_src1) << "\n";
229 std::cout << "Offset_Src1 : " << support::cpp11::to_string(offset_src1) << "\n";
230 std::cout << "Scale_Dst : " << support::cpp11::to_string(scale_dst) << "\n";
231 std::cout << "Offset_Dst : " << support::cpp11::to_string(offset_dst) << "\n";
232 std::cout << "Bias : " << support::cpp11::to_string(add_bias) << "\n";
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100233 }
234 else
235 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000236 std::cout << "Alpha : " << support::cpp11::to_string(alpha) << "\n";
237 std::cout << "Beta : " << support::cpp11::to_string(beta) << "\n";
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100238 }
239 }
240
241 void do_validate() override
242 {
243 switch(data_type)
244 {
245 case DataType::F16:
246 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000247 SimpleTensor<half> ref_src0 = { TensorShape(K, M, B), data_type, 1 };
248 SimpleTensor<half> ref_src1 = { TensorShape(N, K, B), data_type, 1 };
249 SimpleTensor<half> ref_src2 = { TensorShape(N, M, B), data_type, 1 };
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100250
251 fill(ref_src0, 0);
252 fill(ref_src1, 1);
253 fill(ref_src2, 2);
254
255 SimpleTensor<half> ref_dst = reference::gemm<half>(ref_src0, ref_src1, ref_src2, alpha, beta);
256 validate(CLAccessor(dst), ref_dst, tolerance_f16, tolerance_num_f16);
257 break;
258 }
259 case DataType::F32:
260 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000261 SimpleTensor<float> ref_src0 = { TensorShape(K, M, B), data_type, 1 };
262 SimpleTensor<float> ref_src1 = { TensorShape(N, K, B), data_type, 1 };
263 SimpleTensor<float> ref_src2 = { TensorShape(N, M, B), data_type, 1 };
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100264
265 fill(ref_src0, 0);
266 fill(ref_src1, 1);
267 fill(ref_src2, 2);
268
269 SimpleTensor<float> ref_dst = reference::gemm<float>(ref_src0, ref_src1, ref_src2, alpha, beta);
270 validate(CLAccessor(dst), ref_dst, tolerance_f32, 0.f, abs_tolerance_f32);
271 break;
272 }
273 case DataType::QASYMM8:
274 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000275 SimpleTensor<uint8_t> ref_src0{ TensorShape(K, M, B), data_type, 1 };
276 SimpleTensor<uint8_t> ref_src1{ TensorShape(N, K, B), data_type, 1 };
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100277 SimpleTensor<uint8_t> ref_dst;
278
279 // Fill reference
280 fill(ref_src0, 0);
281 fill(ref_src1, 1);
282
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000283 SimpleTensor<int32_t> ref_tmp_dst = reference::gemmlowp_matrix_multiply_core<int32_t, uint8_t>(ref_src0, ref_src1, TensorShape(N, M, B), offset_src0, offset_src1);
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100284
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000285 const std::vector<int32_t> dst_multiplier_vec = { dst_multiplier };
286 const std::vector<int32_t> dst_shift_vec = { dst_shift };
287
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100288 if(add_bias)
289 {
290 SimpleTensor<int32_t> biases{ TensorShape(N), DataType::S32, 1 };
291 // Fill bias
292 fill(biases, 3);
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000293 ref_dst = reference::gemmlowp_quantize_down_scale_by_fixedpoint<int32_t, uint8_t>(ref_tmp_dst, biases, dst_multiplier_vec, dst_shift_vec, offset_dst);
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100294 }
295 else
296 {
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000297 ref_dst = reference::gemmlowp_quantize_down_scale_by_fixedpoint<int32_t, uint8_t>(ref_tmp_dst, dst_multiplier_vec, dst_shift_vec, offset_dst);
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100298 }
299 validate(CLAccessor(dst), ref_dst);
300 break;
301 }
302 default:
303 break;
304 }
305 }
306 void do_run() override
307 {
308 // Execute the function
309 if(data_type == DataType::QASYMM8)
310 {
311 // Run gemmlowp
312 mm_gemmlowp.run();
313 // Run output stage
314 mm_gemmlowp_output_stage.run();
315 }
316 else
317 {
318 // Run gemm
319 mm_gemm.run();
320 }
321
322 // Make sure all the OpenCL jobs are done executing:
323 CLScheduler::get().sync();
324 }
325
326private:
327 template <typename U>
328 void fill(U &&tensor, int i)
329 {
330 switch(tensor.data_type())
331 {
332 case DataType::F16:
333 case DataType::F32:
334 {
335 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
336 library->fill(tensor, distribution, i);
337 break;
338 }
339 case DataType::S32:
340 case DataType::QASYMM8:
341 {
342 std::uniform_int_distribution<> distribution(-6000, 6000);
343 library->fill(tensor, distribution, i);
344 break;
345 }
346 default:
347 library->fill_tensor_uniform(tensor, i);
348 }
349 }
350
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000351 void consume_params(const GEMMCommandLineOptions &opts)
352 {
353 ARM_COMPUTE_ERROR_ON(opts.M->value() <= 0);
354 ARM_COMPUTE_ERROR_ON(opts.N->value() <= 0);
355 ARM_COMPUTE_ERROR_ON(opts.K->value() <= 0);
356 ARM_COMPUTE_ERROR_ON(opts.B->value() <= 0);
357 M = opts.M->value();
358 N = opts.N->value();
359 K = opts.K->value();
360 B = opts.B->value();
361 alpha = opts.alpha->value();
362 beta = opts.beta->value();
363 offset_src0 = opts.offset_src0->value();
364 offset_src1 = opts.offset_src1->value();
365 offset_dst = opts.offset_dst->value();
366 scale_src0 = opts.scale_src0->value();
367 scale_src1 = opts.scale_src1->value();
368 scale_dst = opts.scale_dst->value();
369 add_bias = opts.add_bias->is_set() ? opts.add_bias->value() : true;
370 data_type = opts.data_type->value();
371 }
372
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100373 CLTensor src0{}, src1{}, src2{}, dst{};
374 CLTensor tmp_dst{}, biases{};
375
376 CLGEMM mm_gemm{};
377 CLGEMMLowpMatrixMultiplyCore mm_gemmlowp{};
378 CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint mm_gemmlowp_output_stage{};
379
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000380 size_t M{ 7 }, N{ 3 }, K{ 5 }, B{ 1 };
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100381 DataType data_type{ DataType::F32 };
382 float alpha{ 1.0 }, beta{ 0.0 };
383 int offset_src0{ 10 }, offset_src1{ 10 }, offset_dst{ 10 };
384 float scale_src0{ 1.0f / 255 }, scale_src1{ 1.0f / 255 }, scale_dst{ 1.0f / 255 };
385 int32_t dst_multiplier{ 0 }, dst_shift{ 0 };
386 bool add_bias{ true };
387};
388
389/** Main program for gemm test
390 *
391 * @param[in] argc Number of arguments
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000392 * @param[in] argv Arguments
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100393 *
394 */
395int main(int argc, char **argv)
396{
397 return utils::run_example<CLGEMMValidateExample>(argc, argv);
398}