blob: 115ce6ba23a2ac4a871b0be64a12ce2bcdeb6837 [file] [log] [blame]
SiCongLi282f3242020-11-24 15:24:16 +00001/*
Manuel Bottini7b427862021-02-08 13:45:19 +00002 * Copyright (c) 2020-2021 Arm Limited.
SiCongLi282f3242020-11-24 15:24:16 +00003 *
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/Helpers.h"
29#include "arm_compute/core/KernelDescriptors.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/utils/misc/ShapeCalculator.h"
32#include "arm_compute/runtime/CL/CLScheduler.h"
33#include "arm_compute/runtime/CL/CLTuner.h"
34#include "examples/gemm_tuner/CommonGemmExampleOptions.h"
35#include "examples/gemm_tuner/GemmTunerHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010036#include "src/gpu/cl/kernels/ClGemmLowpMatrixMultiplyReshapedKernel.h"
37#include "src/gpu/cl/kernels/ClGemmReshapeLhsMatrixKernel.h"
SiCongLi282f3242020-11-24 15:24:16 +000038#include "tests/CL/Helper.h"
39#include "utils/Utils.h"
40#include "utils/command_line/CommandLineOptions.h"
41#include "utils/command_line/CommandLineParser.h"
42
43#include <cstdlib>
44
45using namespace arm_compute;
Georgios Pinitas856f66e2021-04-22 21:13:21 +010046using namespace arm_compute::opencl::kernels;
SiCongLi282f3242020-11-24 15:24:16 +000047using namespace utils;
48using namespace arm_compute::misc::shape_calculator;
49using namespace gemm_tuner;
50
51namespace
52{
53/** Structure holding all tunable gemm configs specific to this example/strategy */
54struct GemmConfigs
55{
56 size_t m0{ 4 }; /**< Number of rows processed by the matrix multiplication */
57 size_t n0{ 4 }; /**< Number of columns processed by the matrix multiplication */
58 size_t k0{ 4 }; /**< Number of partial accumulations performed by the matrix multiplication */
59 size_t v0{ 1 }; /**< Number of vertical blocks of size (m0xk0) stored on the same output row */
60 size_t h0{ 1 }; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row */
61 bool interleave_lhs{ true }; /**< Interleave lhs matrix */
62 bool transpose_lhs{ true }; /**< Transpose lhs matrix. */
63 bool interleave_rhs{ true }; /**< Interleave rhs matrix */
64 bool transpose_rhs{ true }; /**< Transpose rhs matrix. */
65};
66
67/** Formatted output of the GemmConfigs type
68 *
69 * @param[out] os Output stream.
70 * @param[in] configs Tunable configurations to output
71 *
72 * @return Modified output stream.
73 */
74::std::ostream &operator<<(::std::ostream &os, const GemmConfigs &configs)
75{
76 std::string false_str = std::string("false");
77 std::string true_str = std::string("true");
78
79 os << "m0 : " << configs.m0 << std::endl;
80 os << "n0 : " << configs.n0 << std::endl;
81 os << "k0 : " << configs.k0 << std::endl;
82 os << "v0 : " << configs.v0 << std::endl;
83 os << "h0 : " << configs.h0 << std::endl;
84 os << "interleave_lhs : " << (configs.interleave_lhs ? true_str : false_str) << std::endl;
85 os << "transpose_lhs : " << (configs.transpose_lhs ? true_str : false_str) << std::endl;
86 os << "interleave_rhs : " << (configs.interleave_rhs ? true_str : false_str) << std::endl;
87 os << "transpose_rhs : " << (configs.transpose_rhs ? true_str : false_str) << std::endl;
88 return os;
89}
90
91/** Command line options for gemm configs */
92class GemmConfigOptions
93{
94public:
95 /** Constructor
96 *
97 * @param[in,out] parser A parser on which "parse()" hasn't been called yet.
98 */
99 GemmConfigOptions(CommandLineParser &parser)
100 : m0(parser.add_positional_option<SimpleOption<size_t>>("m0", 4)),
101 n0(parser.add_positional_option<SimpleOption<size_t>>("n0", 4)),
102 k0(parser.add_positional_option<SimpleOption<size_t>>("k0", 4)),
103 v0(parser.add_positional_option<SimpleOption<size_t>>("v0", 1)),
104 h0(parser.add_positional_option<SimpleOption<size_t>>("h0", 1)),
105 interleave_lhs(parser.add_positional_option<SimpleOption<size_t>>("interleave_lhs", 1)),
106 interleave_rhs(parser.add_positional_option<SimpleOption<size_t>>("interleave_rhs", 1)),
107 transpose_rhs(parser.add_positional_option<SimpleOption<size_t>>("transpose_rhs", 1))
108 {
109 m0->set_help("Number of rows processed by the matrix multiplication");
110 n0->set_help("Number of columns processed by the matrix multiplication");
111 k0->set_help("Number of partial accumulations performed by the matrix multiplication");
112 v0->set_help("Number of vertical blocks of size (m0xk0) stored on the same output row");
113 h0->set_help("Number of horizontal blocks of size (k0xn0) stored on the same output row");
114 interleave_lhs->set_help("Interleave lhs matrix (1) / Do not interleave lhs matrix (0)");
115 interleave_rhs->set_help("Interleave rhs matrix (1) / Do not interleave rhs matrix (0)");
116 // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and
117 // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other
118 // 2 variants (both transposed and none transposed)
119 transpose_rhs->set_help("Transpose rhs matrix but not lhs matrix (1) / Do not transpose rhs matrix but do transpose lhs matrix (0)");
120 }
121 /** Prevent instances of this class from being copied (As this class contains pointers) */
122 GemmConfigOptions(const GemmConfigOptions &) = delete;
123 /** Prevent instances of this class from being copied (As this class contains pointers) */
124 GemmConfigOptions &operator=(const GemmConfigOptions &) = delete;
125 /** Allow instances of this class to be moved */
126 GemmConfigOptions(GemmConfigOptions &&) = default;
127 /** Allow instances of this class to be moved */
128 GemmConfigOptions &operator=(GemmConfigOptions &&) = default;
129 /** Default destructor */
130 ~GemmConfigOptions() = default;
131
132 SimpleOption<size_t> *m0; /**< Number of rows processed by the matrix multiplication option */
133 SimpleOption<size_t> *n0; /**< Number of columns processed by the matrix multiplication option */
134 SimpleOption<size_t> *k0; /**< Number of partial accumulations performed by the matrix multiplication option */
135 SimpleOption<size_t> *v0; /**< Number of vertical blocks of size (m0xk0) stored on the same output row option */
136 SimpleOption<size_t> *h0; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row option */
137 SimpleOption<size_t> *interleave_lhs; /**< Interleave lhs matrix option (1 enable; 0 disable) */
138 SimpleOption<size_t> *interleave_rhs; /**< Interleave rhs matrix option (1 enable; 0 disable) */
139 // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and
140 // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other
141 // 2 variants (both transposed and none transposed)
142 SimpleOption<size_t> *transpose_rhs; /**< Transpose rhs matrix option (1 enable; 0 disable). Also set the lhs matrix transpose option to the opposite. */
143};
144
145/** Consumes the gemm configuration options and creates a structure containing all information
146 *
147 * @param[in] options Options to consume
148 *
149 * @return Structure containing the gemm configurations
150 */
151GemmConfigs consume_gemm_configs(const GemmConfigOptions &options)
152{
153 GemmConfigs configs;
154 configs.m0 = options.m0->value();
155 configs.n0 = options.n0->value();
156 configs.k0 = options.k0->value();
157 configs.v0 = options.v0->value();
158 configs.h0 = options.h0->value();
159 configs.interleave_lhs = options.interleave_lhs->value() != 0;
160 // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and
161 // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other
162 // 2 variants (both transposed and none transposed)
163 configs.transpose_lhs = options.transpose_rhs->value() == 0;
164 configs.interleave_rhs = options.interleave_rhs->value() != 0;
165 configs.transpose_rhs = options.transpose_rhs->value() != 0;
166 return configs;
167}
168
169} // namespace
170
Georgios Pinitas4a578b92021-06-25 12:13:49 +0100171using ClGemmReshapeLHSMatrix = test::CLSynthetizeOperator<ClGemmReshapeLhsMatrixKernel>;
172using ClGemmLowpMatrixMultiplyReshaped = test::CLSynthetizeOperator<ClGemmLowpMatrixMultiplyReshapedKernel>;
SiCongLi282f3242020-11-24 15:24:16 +0000173
174class CLGEMMLowpMatrixMultiplyReshapedExample : public Example
175{
176public:
177 bool do_setup(int argc, char **argv) override
178 {
179 // Default parameters
180 CommonGemmExampleParams params;
181 GemmConfigs configs;
182
183 // Parse command line options
184 CommandLineParser parser;
SiCong Li98e33b92020-12-03 14:52:53 +0000185 CommonGemmExampleOptions param_options(parser, DataType::QASYMM8);
SiCongLi282f3242020-11-24 15:24:16 +0000186 GemmConfigOptions config_options(parser);
187
188 parser.parse(argc, argv);
189 if(param_options.help->is_set() && param_options.help->value())
190 {
191 parser.print_help(argv[0]);
192 return false;
193 }
194 if(!parser.validate())
195 {
196 // Invalid arguments. Use default parameters and configs
197 std::cerr << "Invalid arguments." << std::endl;
198 parser.print_help(argv[0]);
199 std::cerr << "Falling back to default parameters and configs" << std::endl;
200 }
201 else
202 {
203 params = consume_common_gemm_example_parameters(param_options);
204 configs = consume_gemm_configs(config_options);
205 }
206
207 std::cout << "Gemm parameters:" << std::endl;
208 std::cout << params << std::endl;
209 std::cout << "Gemm configurations:" << std::endl;
210 std::cout << configs << std::endl;
211
Gian Marco Iodiceca419dd2021-03-03 17:25:07 +0000212 tuner.set_tuner_mode(params.tuner_mode);
213
SiCongLi282f3242020-11-24 15:24:16 +0000214 CLScheduler::get().default_init(&tuner);
215
216 lhs.allocator()->init(TensorInfo(TensorShape(params.K, params.M, params.B), 1, params.data_type));
217 rhs.allocator()->init(TensorInfo(TensorShape(params.N, params.K, params.B), 1, params.data_type));
218
219 // Set arbitrary quantization information
SiCong Li98e33b92020-12-03 14:52:53 +0000220 const QuantizationInfo q_info
221 {
222 0.012, 3
223 };
224 lhs.info()->set_quantization_info(q_info);
225 rhs.info()->set_quantization_info(q_info);
226 dst.info()->set_quantization_info(q_info);
SiCongLi282f3242020-11-24 15:24:16 +0000227
228 GEMMLHSMatrixInfo lhs_info;
229 lhs_info.m0 = configs.m0;
230 lhs_info.k0 = configs.k0;
231 lhs_info.v0 = configs.v0;
232 lhs_info.interleave = configs.interleave_lhs;
233 lhs_info.transpose = configs.transpose_lhs;
234
235 GEMMRHSMatrixInfo rhs_info;
236 rhs_info.n0 = configs.n0;
237 rhs_info.k0 = configs.k0;
238 rhs_info.h0 = configs.h0;
239 rhs_info.interleave = configs.interleave_rhs;
240 rhs_info.transpose = configs.transpose_rhs;
241 rhs_info.export_to_cl_image = false; // CL image not supported for quantized cases yet
242
243 lhs_reshaped.allocator()->init(TensorInfo(compute_lhs_reshaped_shape(*lhs.info(), lhs_info), 1, params.data_type));
SiCongLi282f3242020-11-24 15:24:16 +0000244 rhs_reshaped.allocator()->init(TensorInfo(compute_rhs_reshaped_shape(*rhs.info(), rhs_info), 1, params.data_type));
SiCong Li98e33b92020-12-03 14:52:53 +0000245 lhs_reshaped.info()->set_quantization_info(q_info);
246 rhs_reshaped.info()->set_quantization_info(q_info);
SiCongLi282f3242020-11-24 15:24:16 +0000247
248 if(rhs_info.export_to_cl_image)
249 {
Manuel Bottini7b427862021-02-08 13:45:19 +0000250 if(!examples::gemm_tuner_helpers::update_padding_for_cl_image(rhs_reshaped.info()))
251 {
252 std::cerr << "cl_image is not supported on the device, disable export_to_cl_image" << std::endl;
253 return false;
254 }
SiCongLi282f3242020-11-24 15:24:16 +0000255 }
256
257 GEMMReshapeInfo gemm_info
258 {
259 static_cast<int>(params.M),
260 static_cast<int>(params.N),
261 static_cast<int>(params.K),
262 static_cast<int>(configs.h0),
263 static_cast<int>(configs.v0),
264 0,
265 false,
266 true
267 };
268
269 // Validate argments
270 if(!reshape_lhs.validate(lhs.info(), lhs_reshaped.info(), lhs_info, gemm_info.reinterpret_input_as_3d()))
271 {
Georgios Pinitas4a578b92021-06-25 12:13:49 +0100272 std::cerr << "Invalid arguments for ClGemmReshapeLHSMatrixKernel." << std::endl;
SiCongLi282f3242020-11-24 15:24:16 +0000273 return false;
274 }
275
276 if(!gemm.validate(lhs_reshaped.info(), rhs_reshaped.info(), dst.info(), lhs_info, rhs_info, gemm_info))
277 {
Georgios Pinitas4a578b92021-06-25 12:13:49 +0100278 std::cerr << "Invalid arguments for ClGemmLowpMatrixMultiplyReshapedKernel." << std::endl;
SiCongLi282f3242020-11-24 15:24:16 +0000279 return false;
280 }
281
282 // Configure functions
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100283 reshape_lhs.configure(lhs.info(), lhs_reshaped.info(), lhs_info);
SiCongLi282f3242020-11-24 15:24:16 +0000284
Georgios Pinitas4a578b92021-06-25 12:13:49 +0100285 gemm.configure(lhs_reshaped.info(), rhs_reshaped.info(), dst.info(), lhs_info, rhs_info, gemm_info);
SiCongLi282f3242020-11-24 15:24:16 +0000286
287 // Allocate tensors
288 lhs.allocator()->allocate();
289 rhs.allocator()->allocate();
290 lhs_reshaped.allocator()->allocate();
291 rhs_reshaped.allocator()->allocate();
292 dst.allocator()->allocate();
293
294 return true;
295 }
296 void do_run() override
297 {
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100298 ITensorPack reshape_lsh_pack({ { ACL_SRC, &lhs }, { ACL_DST, &lhs_reshaped } });
299 reshape_lhs.run(reshape_lsh_pack);
300
Georgios Pinitas4a578b92021-06-25 12:13:49 +0100301 ITensorPack gemm_pack({ { ACL_SRC_0, &lhs_reshaped }, { ACL_SRC_1, &rhs_reshaped }, { ACL_DST, &dst } });
302 gemm.run(gemm_pack);
SiCongLi282f3242020-11-24 15:24:16 +0000303
304 // Make sure all the OpenCL jobs are done executing:
305 CLScheduler::get().sync();
306 }
307
308 void do_teardown() override
309 {
310 }
311
312private:
313 CLTensor lhs{};
314 CLTensor rhs{};
315 CLTensor lhs_reshaped{};
316 CLTensor rhs_reshaped{};
317 CLTensor dst{};
318 CLTuner tuner{};
Georgios Pinitas4a578b92021-06-25 12:13:49 +0100319 ClGemmReshapeLHSMatrix reshape_lhs{};
320 ClGemmLowpMatrixMultiplyReshaped gemm{};
SiCongLi282f3242020-11-24 15:24:16 +0000321};
322
323/** Main test program for gemmlowp reshaped
324 *
325 * @param[in] argc Number of arguments
326 * @param[in] argv Arguments ( [optional] M, [optional] N, [optional] K, [optional] B, [optional] m0, [optional] n0, [optional] k0, [optional] v0, [optional] h0, [optional] interleave_lhs, [optional] interleave_rhs, [optional] transpose_rhs )
327 */
328int main(int argc, char **argv)
329{
330 return run_example<CLGEMMLowpMatrixMultiplyReshapedExample>(argc, argv);
331}