blob: 4e406cbd9b8e49f3ed6bc39bd3b343ad027454f6 [file] [log] [blame]
Isabella Gottardi01a214a2018-04-09 16:00:52 +01001/*
Georgios Pinitas108a95e2019-03-27 13:55:59 +00002 * 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
198 // Calculate re-quantization parameters
199 if(data_type == DataType::QASYMM8)
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100200 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000201 float multiplier = scale_src0 * scale_src1 / scale_dst;
202 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &dst_multiplier, &dst_shift);
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100203 }
204
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000205 // Initialize GEMM inputs/outputs
206 src0.allocator()->init(TensorInfo(TensorShape(K, M, B), 1, data_type));
207 src1.allocator()->init(TensorInfo(TensorShape(N, K, B), 1, data_type));
208 src2.allocator()->init(TensorInfo(TensorShape(N, M, B), 1, data_type));
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100209 init_sgemm_output(dst, src0, src1, data_type);
210
211 // Configure function
212 if(data_type == DataType::QASYMM8)
213 {
214 src0.info()->set_quantization_info(QuantizationInfo(scale_src0, offset_src0));
215 src1.info()->set_quantization_info(QuantizationInfo(scale_src1, offset_src1));
216 dst.info()->set_quantization_info(QuantizationInfo(scale_dst, offset_dst));
217 biases.allocator()->init(TensorInfo(TensorShape(N), 1, DataType::S32));
218 init_sgemm_output(tmp_dst, src0, src1, DataType::S32);
219
220 // Configure GEMMlowp matrix multiply function
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100221 mm_gemmlowp.configure(&src0, &src1, nullptr, &tmp_dst);
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100222
223 // Configure GEMMlowp output stage
224 mm_gemmlowp_output_stage.configure(&tmp_dst, add_bias ? &biases : nullptr, &dst, dst_multiplier, dst_shift, offset_dst);
225 tmp_dst.allocator()->allocate();
226 biases.allocator()->allocate();
227 fill(CLAccessor(biases), 3);
228 }
229 else
230 {
231 // Configure matrix multiply function
232 mm_gemm.configure(&src0, &src1, &src2, &dst, alpha, beta);
233 }
234
235 // Allocate all the tensors
236 src0.allocator()->allocate();
237 src1.allocator()->allocate();
238 dst.allocator()->allocate();
239 src2.allocator()->allocate();
240
241 fill(CLAccessor(src0), 0);
242 fill(CLAccessor(src1), 1);
243 fill(CLAccessor(src2), 2);
Anthony Barbiere88b9bb2018-07-12 13:26:27 +0100244
245 return true;
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100246 }
247
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000248 void print_parameters_internal()
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100249 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000250 std::cout << "Datatype : " << string_from_data_type(data_type) << "\n";
251 std::cout << "M : " << support::cpp11::to_string(M) << "\n";
252 std::cout << "N : " << support::cpp11::to_string(N) << "\n";
253 std::cout << "K : " << support::cpp11::to_string(K) << "\n";
254 std::cout << "B : " << support::cpp11::to_string(B) << "\n";
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100255 if(data_type == DataType::QASYMM8)
256 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000257 std::cout << "Scale_Src0 : " << support::cpp11::to_string(scale_src0) << "\n";
258 std::cout << "Offset_Src0 : " << support::cpp11::to_string(offset_src0) << "\n";
259 std::cout << "Scale_Scr1 : " << support::cpp11::to_string(scale_src1) << "\n";
260 std::cout << "Offset_Src1 : " << support::cpp11::to_string(offset_src1) << "\n";
261 std::cout << "Scale_Dst : " << support::cpp11::to_string(scale_dst) << "\n";
262 std::cout << "Offset_Dst : " << support::cpp11::to_string(offset_dst) << "\n";
263 std::cout << "Bias : " << support::cpp11::to_string(add_bias) << "\n";
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100264 }
265 else
266 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000267 std::cout << "Alpha : " << support::cpp11::to_string(alpha) << "\n";
268 std::cout << "Beta : " << support::cpp11::to_string(beta) << "\n";
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100269 }
270 }
271
272 void do_validate() override
273 {
274 switch(data_type)
275 {
276 case DataType::F16:
277 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000278 SimpleTensor<half> ref_src0 = { TensorShape(K, M, B), data_type, 1 };
279 SimpleTensor<half> ref_src1 = { TensorShape(N, K, B), data_type, 1 };
280 SimpleTensor<half> ref_src2 = { TensorShape(N, M, B), data_type, 1 };
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100281
282 fill(ref_src0, 0);
283 fill(ref_src1, 1);
284 fill(ref_src2, 2);
285
286 SimpleTensor<half> ref_dst = reference::gemm<half>(ref_src0, ref_src1, ref_src2, alpha, beta);
287 validate(CLAccessor(dst), ref_dst, tolerance_f16, tolerance_num_f16);
288 break;
289 }
290 case DataType::F32:
291 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000292 SimpleTensor<float> ref_src0 = { TensorShape(K, M, B), data_type, 1 };
293 SimpleTensor<float> ref_src1 = { TensorShape(N, K, B), data_type, 1 };
294 SimpleTensor<float> ref_src2 = { TensorShape(N, M, B), data_type, 1 };
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100295
296 fill(ref_src0, 0);
297 fill(ref_src1, 1);
298 fill(ref_src2, 2);
299
300 SimpleTensor<float> ref_dst = reference::gemm<float>(ref_src0, ref_src1, ref_src2, alpha, beta);
301 validate(CLAccessor(dst), ref_dst, tolerance_f32, 0.f, abs_tolerance_f32);
302 break;
303 }
304 case DataType::QASYMM8:
305 {
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000306 SimpleTensor<uint8_t> ref_src0{ TensorShape(K, M, B), data_type, 1 };
307 SimpleTensor<uint8_t> ref_src1{ TensorShape(N, K, B), data_type, 1 };
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100308 SimpleTensor<uint8_t> ref_dst;
309
310 // Fill reference
311 fill(ref_src0, 0);
312 fill(ref_src1, 1);
313
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000314 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 +0100315
316 if(add_bias)
317 {
318 SimpleTensor<int32_t> biases{ TensorShape(N), DataType::S32, 1 };
319 // Fill bias
320 fill(biases, 3);
321 ref_dst = reference::gemmlowp_quantize_down_int32_to_uint8_scale_by_fixedpoint<int32_t>(ref_tmp_dst, biases, dst_multiplier, dst_shift, offset_dst);
322 }
323 else
324 {
325 ref_dst = reference::gemmlowp_quantize_down_int32_to_uint8_scale_by_fixedpoint<int32_t>(ref_tmp_dst, dst_multiplier, dst_shift, offset_dst);
326 }
327 validate(CLAccessor(dst), ref_dst);
328 break;
329 }
330 default:
331 break;
332 }
333 }
334 void do_run() override
335 {
336 // Execute the function
337 if(data_type == DataType::QASYMM8)
338 {
339 // Run gemmlowp
340 mm_gemmlowp.run();
341 // Run output stage
342 mm_gemmlowp_output_stage.run();
343 }
344 else
345 {
346 // Run gemm
347 mm_gemm.run();
348 }
349
350 // Make sure all the OpenCL jobs are done executing:
351 CLScheduler::get().sync();
352 }
353
354private:
355 template <typename U>
356 void fill(U &&tensor, int i)
357 {
358 switch(tensor.data_type())
359 {
360 case DataType::F16:
361 case DataType::F32:
362 {
363 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
364 library->fill(tensor, distribution, i);
365 break;
366 }
367 case DataType::S32:
368 case DataType::QASYMM8:
369 {
370 std::uniform_int_distribution<> distribution(-6000, 6000);
371 library->fill(tensor, distribution, i);
372 break;
373 }
374 default:
375 library->fill_tensor_uniform(tensor, i);
376 }
377 }
378
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000379 void consume_params(const GEMMCommandLineOptions &opts)
380 {
381 ARM_COMPUTE_ERROR_ON(opts.M->value() <= 0);
382 ARM_COMPUTE_ERROR_ON(opts.N->value() <= 0);
383 ARM_COMPUTE_ERROR_ON(opts.K->value() <= 0);
384 ARM_COMPUTE_ERROR_ON(opts.B->value() <= 0);
385 M = opts.M->value();
386 N = opts.N->value();
387 K = opts.K->value();
388 B = opts.B->value();
389 alpha = opts.alpha->value();
390 beta = opts.beta->value();
391 offset_src0 = opts.offset_src0->value();
392 offset_src1 = opts.offset_src1->value();
393 offset_dst = opts.offset_dst->value();
394 scale_src0 = opts.scale_src0->value();
395 scale_src1 = opts.scale_src1->value();
396 scale_dst = opts.scale_dst->value();
397 add_bias = opts.add_bias->is_set() ? opts.add_bias->value() : true;
398 data_type = opts.data_type->value();
399 }
400
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100401 CLTensor src0{}, src1{}, src2{}, dst{};
402 CLTensor tmp_dst{}, biases{};
403
404 CLGEMM mm_gemm{};
405 CLGEMMLowpMatrixMultiplyCore mm_gemmlowp{};
406 CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint mm_gemmlowp_output_stage{};
407
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000408 size_t M{ 7 }, N{ 3 }, K{ 5 }, B{ 1 };
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100409 DataType data_type{ DataType::F32 };
410 float alpha{ 1.0 }, beta{ 0.0 };
411 int offset_src0{ 10 }, offset_src1{ 10 }, offset_dst{ 10 };
412 float scale_src0{ 1.0f / 255 }, scale_src1{ 1.0f / 255 }, scale_dst{ 1.0f / 255 };
413 int32_t dst_multiplier{ 0 }, dst_shift{ 0 };
414 bool add_bias{ true };
415};
416
417/** Main program for gemm test
418 *
419 * @param[in] argc Number of arguments
Georgios Pinitas108a95e2019-03-27 13:55:59 +0000420 * @param[in] argv Arguments
Isabella Gottardi01a214a2018-04-09 16:00:52 +0100421 *
422 */
423int main(int argc, char **argv)
424{
425 return utils::run_example<CLGEMMValidateExample>(argc, argv);
426}