blob: 34895840e19020b00edfd12509e7ca921b45509c [file] [log] [blame]
Isabella Gottardi01a214a2018-04-09 16:00:52 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2019 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"
29#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
30#include "arm_compute/runtime/CL/CLFunctions.h"
31#include "arm_compute/runtime/CL/CLScheduler.h"
32
33#include "tests/AssetsLibrary.h"
34#include "tests/CL/CLAccessor.h"
35#include "tests/Globals.h"
36#include "tests/IAccessor.h"
37#include "tests/SimpleTensor.h"
38#include "tests/validation/Validation.h"
39#include "tests/validation/reference/GEMM.h"
40#include "tests/validation/reference/GEMMLowp.h"
41
Georgios Pinitas108a95e2019-03-27 13:55:59 +000042#include "utils/TypePrinter.h"
Isabella Gottardi01a214a2018-04-09 16:00:52 +010043#include "utils/Utils.h"
Georgios Pinitas108a95e2019-03-27 13:55:59 +000044#include "utils/command_line/CommandLineOptions.h"
45#include "utils/command_line/CommandLineParser.h"
46
47#include "ValidateExample.h"
Isabella Gottardi01a214a2018-04-09 16:00:52 +010048
49#include <cstdlib>
50
51using namespace arm_compute;
52using namespace utils;
53using namespace arm_compute::test;
54using namespace arm_compute::test::validation;
55
56constexpr float abs_tolerance_f32(0.0001f); /**< F32 Absolute tolerance value for comparing reference's output against implementation's output for
57 * floating point data types in case using relative tolerance fails because of small values */
58RelativeTolerance<float> tolerance_f32(0.001f); /**< F32 Tolerance value for comparing reference's output against implementation's output for floating point data types */
59RelativeTolerance<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 */
60constexpr float tolerance_num_f16 = 0.02f; /**< F16 Tolerance number */
61
Georgios Pinitas108a95e2019-03-27 13:55:59 +000062namespace arm_compute
63{
64DataType data_type_from_name(const std::string &name)
65{
66 static const std::map<std::string, DataType> data_types =
67 {
68 { "f16", DataType::F16 },
69 { "f32", DataType::F32 },
70 { "qasymm8", DataType::QASYMM8 },
71 };
72
73#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
74 try
75 {
76#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
77 return data_types.at(utility::tolower(name));
78
79#ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED
80 }
81 catch(const std::out_of_range &)
82 {
83 throw std::invalid_argument(name);
84 }
85#endif /* ARM_COMPUTE_EXCEPTIONS_DISABLED */
86}
87
88inline ::std::istream &operator>>(::std::istream &stream, DataType &data_type)
89{
90 std::string value;
91 stream >> value;
92 data_type = data_type_from_name(value);
93 return stream;
94}
95} // namespace arm_compute
96namespace
97{
98class GEMMCommandLineOptions final
99{
100public:
101 explicit GEMMCommandLineOptions(CommandLineParser &parser) noexcept
102 : help(parser.add_option<ToggleOption>("help")),
103 add_bias(parser.add_option<ToggleOption>("add_bias")),
104 M(parser.add_option<SimpleOption<int>>("m", 7)),
105 N(parser.add_option<SimpleOption<int>>("n", 3)),
106 K(parser.add_option<SimpleOption<int>>("k", 5)),
107 B(parser.add_option<SimpleOption<int>>("b", 1)),
108 alpha(parser.add_option<SimpleOption<float>>("alpha", 1.f)),
109 beta(parser.add_option<SimpleOption<float>>("beta", 0.f)),
110 offset_src0(parser.add_option<SimpleOption<int>>("offset_i0", 10)),
111 offset_src1(parser.add_option<SimpleOption<int>>("offset_i1", 10)),
112 offset_dst(parser.add_option<SimpleOption<int>>("offset_o", 10)),
113 scale_src0(parser.add_option<SimpleOption<float>>("scale_i0", 1.f / 255)),
114 scale_src1(parser.add_option<SimpleOption<float>>("scale_i1", 1.f / 255)),
115 scale_dst(parser.add_option<SimpleOption<float>>("scale_o", 1.f / 255)),
116 data_type()
117 {
118 // Setup data type
119 const std::set<arm_compute::DataType> supported_data_types
120 {
121 DataType::F16,
122 DataType::F32,
123 DataType::QASYMM8,
124 };
125 data_type = parser.add_option<EnumOption<DataType>>("type", supported_data_types, DataType::F32);
126
127 // Setup help strings
128 help->set_help("Show this help message");
129 add_bias->set_help("Add bias to the GEMM. Used when running in QASYMM8");
130 M->set_help("M value");
131 N->set_help("N value");
132 K->set_help("K value");
133 B->set_help("B value - number of batches");
134 alpha->set_help("Alpha value");
135 beta->set_help("Beta value");
136 offset_src0->set_help("Offset of first input. Used when running in QASYMM8");
137 offset_src1->set_help("Offset of second input. Used when running in QASYMM8");
138 offset_dst->set_help("Offset of output. Used when running in QASYMM8");
139 scale_src0->set_help("Scale of first input. Used when running in QASYMM8");
140 scale_src1->set_help("Scale of second input. Used when running in QASYMM8");
141 scale_dst->set_help("Scale of output. Used when running in QASYMM8");
142 data_type->set_help("Data type to use");
143 }
144 /** Prevent instances of this class from being copied (As this class contains pointers) */
145 GEMMCommandLineOptions(const GEMMCommandLineOptions &) = delete;
146 /** Prevent instances of this class from being copied (As this class contains pointers) */
147 GEMMCommandLineOptions &operator=(const GEMMCommandLineOptions &) = delete;
148 /** Allow instances of this class to be moved */
149 GEMMCommandLineOptions(GEMMCommandLineOptions &&) noexcept(true) = default;
150 /** Allow instances of this class to be moved */
151 GEMMCommandLineOptions &operator=(GEMMCommandLineOptions &&) noexcept(true) = default;
152 /** Default destructor */
153 ~GEMMCommandLineOptions() = default;
154
155public:
156 ToggleOption *help;
157 ToggleOption *add_bias;
158 SimpleOption<int> *M;
159 SimpleOption<int> *N;
160 SimpleOption<int> *K;
161 SimpleOption<int> *B;
162 SimpleOption<float> *alpha;
163 SimpleOption<float> *beta;
164 SimpleOption<int> *offset_src0;
165 SimpleOption<int> *offset_src1;
166 SimpleOption<int> *offset_dst;
167 SimpleOption<float> *scale_src0;
168 SimpleOption<float> *scale_src1;
169 SimpleOption<float> *scale_dst;
170 EnumOption<arm_compute::DataType> *data_type;
171};
172} // namespace
173
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100174class CLGEMMValidateExample : public ValidateExample
175{
176public:
Anthony Barbiere88b9bb2018-07-12 13:26:27 +0100177 bool do_setup(int argc, char **argv) override
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100178 {
179 CLScheduler::get().default_init();
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000180
181 // Parse options
182 CommandLineParser parser;
183 GEMMCommandLineOptions gemm_options(parser);
184 parser.parse(argc, argv);
185
186 // Print help
187 const bool print_help = gemm_options.help->is_set() ? gemm_options.help->value() : false;
188 if(print_help)
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100189 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000190 parser.print_help(argv[0]);
191 return false;
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100192 }
193
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000194 // Consume parameters
195 consume_params(gemm_options);
196 print_parameters_internal();
197
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100198 const bool is_quantized = is_data_type_quantized(data_type);
199
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000200 // Calculate re-quantization parameters
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100201 if(is_quantized)
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100202 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000203 float multiplier = scale_src0 * scale_src1 / scale_dst;
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100204 quantization::calculate_quantized_multiplier(multiplier, &dst_multiplier, &dst_shift);
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100205 }
206
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000207 // Initialize GEMM inputs/outputs
208 src0.allocator()->init(TensorInfo(TensorShape(K, M, B), 1, data_type));
209 src1.allocator()->init(TensorInfo(TensorShape(N, K, B), 1, data_type));
210 src2.allocator()->init(TensorInfo(TensorShape(N, M, B), 1, data_type));
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100211 init_sgemm_output(dst, src0, src1, data_type);
212
213 // Configure function
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100214 if(is_quantized)
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100215 {
216 src0.info()->set_quantization_info(QuantizationInfo(scale_src0, offset_src0));
217 src1.info()->set_quantization_info(QuantizationInfo(scale_src1, offset_src1));
218 dst.info()->set_quantization_info(QuantizationInfo(scale_dst, offset_dst));
219 biases.allocator()->init(TensorInfo(TensorShape(N), 1, DataType::S32));
220 init_sgemm_output(tmp_dst, src0, src1, DataType::S32);
221
222 // Configure GEMMlowp matrix multiply function
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100223 mm_gemmlowp.configure(&src0, &src1, nullptr, &tmp_dst);
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100224
225 // Configure GEMMlowp output stage
226 mm_gemmlowp_output_stage.configure(&tmp_dst, add_bias ? &biases : nullptr, &dst, dst_multiplier, dst_shift, offset_dst);
227 tmp_dst.allocator()->allocate();
228 biases.allocator()->allocate();
229 fill(CLAccessor(biases), 3);
230 }
231 else
232 {
233 // Configure matrix multiply function
234 mm_gemm.configure(&src0, &src1, &src2, &dst, alpha, beta);
235 }
236
237 // Allocate all the tensors
238 src0.allocator()->allocate();
239 src1.allocator()->allocate();
240 dst.allocator()->allocate();
241 src2.allocator()->allocate();
242
243 fill(CLAccessor(src0), 0);
244 fill(CLAccessor(src1), 1);
245 fill(CLAccessor(src2), 2);
Anthony Barbiere88b9bb2018-07-12 13:26:27 +0100246
247 return true;
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100248 }
249
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000250 void print_parameters_internal()
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100251 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000252 std::cout << "Datatype : " << string_from_data_type(data_type) << "\n";
253 std::cout << "M : " << support::cpp11::to_string(M) << "\n";
254 std::cout << "N : " << support::cpp11::to_string(N) << "\n";
255 std::cout << "K : " << support::cpp11::to_string(K) << "\n";
256 std::cout << "B : " << support::cpp11::to_string(B) << "\n";
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100257 if(data_type == DataType::QASYMM8)
258 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000259 std::cout << "Scale_Src0 : " << support::cpp11::to_string(scale_src0) << "\n";
260 std::cout << "Offset_Src0 : " << support::cpp11::to_string(offset_src0) << "\n";
261 std::cout << "Scale_Scr1 : " << support::cpp11::to_string(scale_src1) << "\n";
262 std::cout << "Offset_Src1 : " << support::cpp11::to_string(offset_src1) << "\n";
263 std::cout << "Scale_Dst : " << support::cpp11::to_string(scale_dst) << "\n";
264 std::cout << "Offset_Dst : " << support::cpp11::to_string(offset_dst) << "\n";
265 std::cout << "Bias : " << support::cpp11::to_string(add_bias) << "\n";
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100266 }
267 else
268 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000269 std::cout << "Alpha : " << support::cpp11::to_string(alpha) << "\n";
270 std::cout << "Beta : " << support::cpp11::to_string(beta) << "\n";
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100271 }
272 }
273
274 void do_validate() override
275 {
276 switch(data_type)
277 {
278 case DataType::F16:
279 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000280 SimpleTensor<half> ref_src0 = { TensorShape(K, M, B), data_type, 1 };
281 SimpleTensor<half> ref_src1 = { TensorShape(N, K, B), data_type, 1 };
282 SimpleTensor<half> ref_src2 = { TensorShape(N, M, B), data_type, 1 };
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100283
284 fill(ref_src0, 0);
285 fill(ref_src1, 1);
286 fill(ref_src2, 2);
287
288 SimpleTensor<half> ref_dst = reference::gemm<half>(ref_src0, ref_src1, ref_src2, alpha, beta);
289 validate(CLAccessor(dst), ref_dst, tolerance_f16, tolerance_num_f16);
290 break;
291 }
292 case DataType::F32:
293 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000294 SimpleTensor<float> ref_src0 = { TensorShape(K, M, B), data_type, 1 };
295 SimpleTensor<float> ref_src1 = { TensorShape(N, K, B), data_type, 1 };
296 SimpleTensor<float> ref_src2 = { TensorShape(N, M, B), data_type, 1 };
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100297
298 fill(ref_src0, 0);
299 fill(ref_src1, 1);
300 fill(ref_src2, 2);
301
302 SimpleTensor<float> ref_dst = reference::gemm<float>(ref_src0, ref_src1, ref_src2, alpha, beta);
303 validate(CLAccessor(dst), ref_dst, tolerance_f32, 0.f, abs_tolerance_f32);
304 break;
305 }
306 case DataType::QASYMM8:
307 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000308 SimpleTensor<uint8_t> ref_src0{ TensorShape(K, M, B), data_type, 1 };
309 SimpleTensor<uint8_t> ref_src1{ TensorShape(N, K, B), data_type, 1 };
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100310 SimpleTensor<uint8_t> ref_dst;
311
312 // Fill reference
313 fill(ref_src0, 0);
314 fill(ref_src1, 1);
315
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000316 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 +0100317
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000318 const std::vector<int32_t> dst_multiplier_vec = { dst_multiplier };
319 const std::vector<int32_t> dst_shift_vec = { dst_shift };
320
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100321 if(add_bias)
322 {
323 SimpleTensor<int32_t> biases{ TensorShape(N), DataType::S32, 1 };
324 // Fill bias
325 fill(biases, 3);
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000326 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 +0100327 }
328 else
329 {
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000330 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 +0100331 }
332 validate(CLAccessor(dst), ref_dst);
333 break;
334 }
335 default:
336 break;
337 }
338 }
339 void do_run() override
340 {
341 // Execute the function
342 if(data_type == DataType::QASYMM8)
343 {
344 // Run gemmlowp
345 mm_gemmlowp.run();
346 // Run output stage
347 mm_gemmlowp_output_stage.run();
348 }
349 else
350 {
351 // Run gemm
352 mm_gemm.run();
353 }
354
355 // Make sure all the OpenCL jobs are done executing:
356 CLScheduler::get().sync();
357 }
358
359private:
360 template <typename U>
361 void fill(U &&tensor, int i)
362 {
363 switch(tensor.data_type())
364 {
365 case DataType::F16:
366 case DataType::F32:
367 {
368 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
369 library->fill(tensor, distribution, i);
370 break;
371 }
372 case DataType::S32:
373 case DataType::QASYMM8:
374 {
375 std::uniform_int_distribution<> distribution(-6000, 6000);
376 library->fill(tensor, distribution, i);
377 break;
378 }
379 default:
380 library->fill_tensor_uniform(tensor, i);
381 }
382 }
383
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000384 void consume_params(const GEMMCommandLineOptions &opts)
385 {
386 ARM_COMPUTE_ERROR_ON(opts.M->value() <= 0);
387 ARM_COMPUTE_ERROR_ON(opts.N->value() <= 0);
388 ARM_COMPUTE_ERROR_ON(opts.K->value() <= 0);
389 ARM_COMPUTE_ERROR_ON(opts.B->value() <= 0);
390 M = opts.M->value();
391 N = opts.N->value();
392 K = opts.K->value();
393 B = opts.B->value();
394 alpha = opts.alpha->value();
395 beta = opts.beta->value();
396 offset_src0 = opts.offset_src0->value();
397 offset_src1 = opts.offset_src1->value();
398 offset_dst = opts.offset_dst->value();
399 scale_src0 = opts.scale_src0->value();
400 scale_src1 = opts.scale_src1->value();
401 scale_dst = opts.scale_dst->value();
402 add_bias = opts.add_bias->is_set() ? opts.add_bias->value() : true;
403 data_type = opts.data_type->value();
404 }
405
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100406 CLTensor src0{}, src1{}, src2{}, dst{};
407 CLTensor tmp_dst{}, biases{};
408
409 CLGEMM mm_gemm{};
410 CLGEMMLowpMatrixMultiplyCore mm_gemmlowp{};
411 CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint mm_gemmlowp_output_stage{};
412
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000413 size_t M{ 7 }, N{ 3 }, K{ 5 }, B{ 1 };
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100414 DataType data_type{ DataType::F32 };
415 float alpha{ 1.0 }, beta{ 0.0 };
416 int offset_src0{ 10 }, offset_src1{ 10 }, offset_dst{ 10 };
417 float scale_src0{ 1.0f / 255 }, scale_src1{ 1.0f / 255 }, scale_dst{ 1.0f / 255 };
418 int32_t dst_multiplier{ 0 }, dst_shift{ 0 };
419 bool add_bias{ true };
420};
421
422/** Main program for gemm test
423 *
424 * @param[in] argc Number of arguments
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000425 * @param[in] argv Arguments
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100426 *
427 */
428int main(int argc, char **argv)
429{
430 return utils::run_example<CLGEMMValidateExample>(argc, argv);
431}