blob: bbc42a7e55100451e9fa9dba7db36957e1ec36ad [file] [log] [blame]
SiCong Libc166d52019-09-26 14:58:53 +01001/*
Manuel Bottini7b427862021-02-08 13:45:19 +00002 * Copyright (c) 2019-2021 Arm Limited.
SiCong Libc166d52019-09-26 14:58:53 +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
SiCong Libc166d52019-09-26 14:58:53 +010028#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"
SiCong Libc166d52019-09-26 14:58:53 +010032#include "arm_compute/runtime/CL/CLScheduler.h"
33#include "arm_compute/runtime/CL/CLTuner.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#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/ClGemmMatrixMultiplyReshapedKernel.h"
37#include "src/gpu/cl/kernels/ClGemmReshapeLhsMatrixKernel.h"
SiCong Libc166d52019-09-26 14:58:53 +010038#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;
SiCong Libc166d52019-09-26 14:58:53 +010047using 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{
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +010056 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 bool export_to_cl_image_rhs{ true }; /**< Export rhs matrix to cl_image. */
SiCong Libc166d52019-09-26 14:58:53 +010066};
67
68/** Formatted output of the GemmConfigs type
69 *
70 * @param[out] os Output stream.
71 * @param[in] configs Tunable configurations to output
72 *
73 * @return Modified output stream.
74 */
75::std::ostream &operator<<(::std::ostream &os, const GemmConfigs &configs)
76{
77 std::string false_str = std::string("false");
78 std::string true_str = std::string("true");
79
80 os << "m0 : " << configs.m0 << std::endl;
81 os << "n0 : " << configs.n0 << std::endl;
82 os << "k0 : " << configs.k0 << std::endl;
83 os << "v0 : " << configs.v0 << std::endl;
84 os << "h0 : " << configs.h0 << std::endl;
85 os << "interleave_lhs : " << (configs.interleave_lhs ? true_str : false_str) << std::endl;
86 os << "transpose_lhs : " << (configs.transpose_lhs ? true_str : false_str) << std::endl;
87 os << "interleave_rhs : " << (configs.interleave_rhs ? true_str : false_str) << std::endl;
88 os << "transpose_rhs : " << (configs.transpose_rhs ? true_str : false_str) << std::endl;
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +010089 os << "export_to_cl_image_rhs : " << (configs.export_to_cl_image_rhs ? true_str : false_str) << std::endl;
SiCong Libc166d52019-09-26 14:58:53 +010090 return os;
91}
92
93/** Command line options for gemm configs */
94class GemmConfigOptions
95{
96public:
97 /** Constructor
98 *
99 * @param[in,out] parser A parser on which "parse()" hasn't been called yet.
100 */
101 GemmConfigOptions(CommandLineParser &parser)
102 : m0(parser.add_positional_option<SimpleOption<size_t>>("m0", 4)),
103 n0(parser.add_positional_option<SimpleOption<size_t>>("n0", 4)),
104 k0(parser.add_positional_option<SimpleOption<size_t>>("k0", 4)),
105 v0(parser.add_positional_option<SimpleOption<size_t>>("v0", 1)),
106 h0(parser.add_positional_option<SimpleOption<size_t>>("h0", 1)),
107 interleave_lhs(parser.add_positional_option<SimpleOption<size_t>>("interleave_lhs", 1)),
108 interleave_rhs(parser.add_positional_option<SimpleOption<size_t>>("interleave_rhs", 1)),
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100109 transpose_rhs(parser.add_positional_option<SimpleOption<size_t>>("transpose_rhs", 1)),
110 export_to_cl_image_rhs(parser.add_positional_option<SimpleOption<size_t>>("export_to_cl_image_rhs", 1))
SiCong Libc166d52019-09-26 14:58:53 +0100111 {
112 m0->set_help("Number of rows processed by the matrix multiplication");
113 n0->set_help("Number of columns processed by the matrix multiplication");
114 k0->set_help("Number of partial accumulations performed by the matrix multiplication");
115 v0->set_help("Number of vertical blocks of size (m0xk0) stored on the same output row");
116 h0->set_help("Number of horizontal blocks of size (k0xn0) stored on the same output row");
117 interleave_lhs->set_help("Interleave lhs matrix (1) / Do not interleave lhs matrix (0)");
118 interleave_rhs->set_help("Interleave rhs matrix (1) / Do not interleave rhs matrix (0)");
119 // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and
120 // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other
121 // 2 variants (both transposed and none transposed)
122 transpose_rhs->set_help("Transpose rhs matrix but not lhs matrix (1) / Do not transpose rhs matrix but do transpose lhs matrix (0)");
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100123 export_to_cl_image_rhs->set_help("Export rhs matrix to cl_image (1) / Do not export rhs matrix to cl_image (0)");
SiCong Libc166d52019-09-26 14:58:53 +0100124 }
125 /** Prevent instances of this class from being copied (As this class contains pointers) */
126 GemmConfigOptions(const GemmConfigOptions &) = delete;
127 /** Prevent instances of this class from being copied (As this class contains pointers) */
128 GemmConfigOptions &operator=(const GemmConfigOptions &) = delete;
129 /** Allow instances of this class to be moved */
130 GemmConfigOptions(GemmConfigOptions &&) = default;
131 /** Allow instances of this class to be moved */
132 GemmConfigOptions &operator=(GemmConfigOptions &&) = default;
133 /** Default destructor */
134 ~GemmConfigOptions() = default;
135
136 SimpleOption<size_t> *m0; /**< Number of rows processed by the matrix multiplication option */
137 SimpleOption<size_t> *n0; /**< Number of columns processed by the matrix multiplication option */
138 SimpleOption<size_t> *k0; /**< Number of partial accumulations performed by the matrix multiplication option */
139 SimpleOption<size_t> *v0; /**< Number of vertical blocks of size (m0xk0) stored on the same output row option */
140 SimpleOption<size_t> *h0; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row option */
141 SimpleOption<size_t> *interleave_lhs; /**< Interleave lhs matrix option (1 enable; 0 disable) */
142 SimpleOption<size_t> *interleave_rhs; /**< Interleave rhs matrix option (1 enable; 0 disable) */
143 // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and
144 // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other
145 // 2 variants (both transposed and none transposed)
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100146 SimpleOption<size_t> *transpose_rhs; /**< Transpose rhs matrix option (1 enable; 0 disable). Also set the lhs matrix transpose option to the opposite. */
147 SimpleOption<size_t> *export_to_cl_image_rhs; /**< Export rhs matrix to cl_image.*/
SiCong Libc166d52019-09-26 14:58:53 +0100148};
149
150/** Consumes the gemm configuration options and creates a structure containing all information
151 *
152 * @param[in] options Options to consume
153 *
154 * @return Structure containing the gemm configurations
155 */
156GemmConfigs consume_gemm_configs(const GemmConfigOptions &options)
157{
158 GemmConfigs configs;
159 configs.m0 = options.m0->value();
160 configs.n0 = options.n0->value();
161 configs.k0 = options.k0->value();
162 configs.v0 = options.v0->value();
163 configs.h0 = options.h0->value();
164 configs.interleave_lhs = options.interleave_lhs->value() != 0;
165 // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and
166 // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other
167 // 2 variants (both transposed and none transposed)
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100168 configs.transpose_lhs = options.transpose_rhs->value() == 0;
169 configs.interleave_rhs = options.interleave_rhs->value() != 0;
170 configs.transpose_rhs = options.transpose_rhs->value() != 0;
171 configs.export_to_cl_image_rhs = options.export_to_cl_image_rhs->value() != 0;
SiCong Libc166d52019-09-26 14:58:53 +0100172 return configs;
173}
174
175} // namespace
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100176
177// Create function for ClGemmReshapeLhsMatrixKernel
178using CLGEMMReshapeLHSMatrix = test::CLSynthetizeOperator<ClGemmReshapeLhsMatrixKernel>;
179// Create function for ClGemmMatrixMultiplyReshapedKernel
180using CLGEMMMatrixMultiplyReshaped = test::CLSynthetizeOperator<ClGemmMatrixMultiplyReshapedKernel>;
SiCong Libc166d52019-09-26 14:58:53 +0100181
182class CLGEMMMatrixMultiplyReshapedExample : public Example
183{
184public:
185 bool do_setup(int argc, char **argv) override
186 {
187 // Default parameters
Eren Kopuz350099e2020-06-09 15:37:43 +0100188 const float alpha = 1.0f;
189 const float beta = 0.0f;
190 const ActivationLayerInfo act_info = ActivationLayerInfo();
SiCong Libc166d52019-09-26 14:58:53 +0100191 CommonGemmExampleParams params;
192 GemmConfigs configs;
193
194 // Set up command line parser and options
195 CommandLineParser parser;
196 CommonGemmExampleOptions param_options(parser);
197 GemmConfigOptions config_options(parser);
198
199 // Parse command line options
200 parser.parse(argc, argv);
201 if(param_options.help->is_set() && param_options.help->value())
202 {
203 // Print help message
204 parser.print_help(argv[0]);
205 return false;
206 }
207 if(!parser.validate())
208 {
209 // Invalid arguments. Use default parameters and configs
210 std::cerr << "Invalid arguments." << std::endl;
211 parser.print_help(argv[0]);
212 std::cerr << "Falling back to default parameters and configs" << std::endl;
213 }
214 else
215 {
216 // Get parameters and configs from command-line options
217 params = consume_common_gemm_example_parameters(param_options);
218 configs = consume_gemm_configs(config_options);
219 }
220
221 // Print gemm parameters and configurations
Eren Kopuz15205d92020-07-17 15:13:39 +0100222 std::cout << "Gemm parameters:" << std::endl;
223 std::cout << params << std::endl;
224 std::cout << "Gemm configurations:" << std::endl;
225 std::cout << configs << std::endl;
SiCong Libc166d52019-09-26 14:58:53 +0100226
Gian Marco Iodiceca419dd2021-03-03 17:25:07 +0000227 tuner.set_tuner_mode(params.tuner_mode);
228
SiCong Libc166d52019-09-26 14:58:53 +0100229 CLScheduler::get().default_init(&tuner);
230
Eren Kopuz350099e2020-06-09 15:37:43 +0100231 lhs.allocator()->init(TensorInfo(TensorShape(params.K, params.M, params.B), 1, params.data_type));
232 rhs.allocator()->init(TensorInfo(TensorShape(params.N, params.K, params.B), 1, params.data_type));
233 bias.allocator()->init(TensorInfo(TensorShape(params.N, 1, params.B), 1, params.data_type));
SiCong Libc166d52019-09-26 14:58:53 +0100234
235 GEMMLHSMatrixInfo lhs_info;
236 lhs_info.m0 = configs.m0;
237 lhs_info.k0 = configs.k0;
238 lhs_info.v0 = configs.v0;
239 lhs_info.interleave = configs.interleave_lhs;
240 lhs_info.transpose = configs.transpose_lhs;
241
242 GEMMRHSMatrixInfo rhs_info;
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100243 rhs_info.n0 = configs.n0;
244 rhs_info.k0 = configs.k0;
245 rhs_info.h0 = configs.h0;
246 rhs_info.interleave = configs.interleave_rhs;
247 rhs_info.transpose = configs.transpose_rhs;
248 rhs_info.export_to_cl_image = configs.export_to_cl_image_rhs;
SiCong Libc166d52019-09-26 14:58:53 +0100249
250 GEMMKernelInfo kernel_info;
251 kernel_info.m = params.M;
252 kernel_info.n = params.N;
253 kernel_info.k = params.K;
254 kernel_info.depth_output_gemm3d = 0;
255 kernel_info.reinterpret_input_as_3d = false;
256 kernel_info.broadcast_bias = true;
257 kernel_info.activation_info = act_info;
258
259 // Initialise lhs_reshaped tensor info
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100260 lhs_reshaped.allocator()->init(TensorInfo(compute_lhs_reshaped_shape(*lhs.info(), lhs_info), 1, params.data_type));
SiCong Libc166d52019-09-26 14:58:53 +0100261
262 // Initialise rhs_reshaped tensor info
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100263 rhs_reshaped.allocator()->init(TensorInfo(compute_rhs_reshaped_shape(*rhs.info(), rhs_info), 1, params.data_type));
SiCong Libc166d52019-09-26 14:58:53 +0100264
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100265 if(rhs_info.export_to_cl_image)
266 {
Manuel Bottini7b427862021-02-08 13:45:19 +0000267 if(!examples::gemm_tuner_helpers::update_padding_for_cl_image(rhs_reshaped.info()))
268 {
269 std::cerr << "cl_image is not supported on the device, disable export_to_cl_image" << std::endl;
270 return false;
271 }
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100272 }
273
Eren Kopuz15205d92020-07-17 15:13:39 +0100274 // Validate argments
275 Status status{};
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100276 status = reshape_lhs.validate(lhs.info(), lhs_reshaped.info(), lhs_info, kernel_info.reinterpret_input_as_3d);
Eren Kopuz15205d92020-07-17 15:13:39 +0100277 if(!status)
278 {
279 // Unsupported arguments
280 std::cerr << "Unsupported arguments." << std::endl;
281 std::cerr << "Check documentation for supported/unsupported combinations" << std::endl;
282 return false;
283 }
284
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100285 status = gemm.validate(lhs_reshaped.info(), rhs_reshaped.info(), bias.info(), dst.info(), alpha, beta, lhs_info, rhs_info, kernel_info);
Eren Kopuz15205d92020-07-17 15:13:39 +0100286 if(!status)
287 {
288 // Unsupported arguments
289 std::cerr << "Unsupported arguments." << std::endl;
290 std::cerr << "Check documentation for supported/unsupported combinations" << std::endl;
291 return false;
292 }
293
SiCong Lie36b5262019-10-01 19:26:00 +0100294 // Configure reshape lhs function
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100295 reshape_lhs.configure(lhs.info(), lhs_reshaped.info(), lhs_info);
SiCong Lie36b5262019-10-01 19:26:00 +0100296
SiCong Libc166d52019-09-26 14:58:53 +0100297 // Configure function
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100298 gemm.configure(lhs_reshaped.info(), rhs_reshaped.info(), bias.info(), dst.info(), alpha, beta, lhs_info, rhs_info, kernel_info);
SiCong Libc166d52019-09-26 14:58:53 +0100299
300 // Allocate tensors
301 lhs.allocator()->allocate();
302 rhs.allocator()->allocate();
303 lhs_reshaped.allocator()->allocate();
304 rhs_reshaped.allocator()->allocate();
305 bias.allocator()->allocate();
306 dst.allocator()->allocate();
307
308 return true;
309 }
310 void do_run() override
311 {
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100312 // Execute the functions
313 ITensorPack reshape_lsh_pack({ { ACL_SRC, &lhs }, { ACL_DST, &lhs_reshaped } });
314 reshape_lhs.run(reshape_lsh_pack);
315
316 ITensorPack gemm_pack({ { ACL_SRC_0, &lhs_reshaped },
317 { ACL_SRC_1, &rhs_reshaped },
318 { ACL_SRC_2, &bias },
319 { ACL_DST, &dst }
320 });
Gian Marco Iodice3cfd3292021-08-24 11:58:25 +0100321 gemm.run(gemm_pack);
SiCong Libc166d52019-09-26 14:58:53 +0100322
323 // Make sure all the OpenCL jobs are done executing:
324 CLScheduler::get().sync();
325 }
326
327 void do_teardown() override
328 {
329 }
330
331private:
332 CLTensor lhs{};
333 CLTensor rhs{};
334 CLTensor lhs_reshaped{};
335 CLTensor rhs_reshaped{};
336 CLTensor bias{};
337 CLTensor dst{};
338 CLTuner tuner{};
SiCong Lie36b5262019-10-01 19:26:00 +0100339 CLGEMMReshapeLHSMatrix reshape_lhs{};
SiCong Libc166d52019-09-26 14:58:53 +0100340 CLGEMMMatrixMultiplyReshaped gemm{};
341};
342
343/** Main program for gemm reshaped test
344 *
345 * @param[in] argc Number of arguments
SiCongLi282f3242020-11-24 15:24:16 +0000346 * @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, [optional] export_to_cl_image )
SiCong Libc166d52019-09-26 14:58:53 +0100347 */
348int main(int argc, char **argv)
349{
350 return run_example<CLGEMMMatrixMultiplyReshapedExample>(argc, argv);
351}