blob: 75f3539cb969d433c14a18300f34b6ed210ff57c [file] [log] [blame]
SiCong Libc166d52019-09-26 14:58:53 +01001/*
Gian Marco Iodice60ab4e62023-04-28 10:40:07 +01002 * Copyright (c) 2019-2021, 2023 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"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010034
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "examples/gemm_tuner/CommonGemmExampleOptions.h"
36#include "examples/gemm_tuner/GemmTunerHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010037#include "src/gpu/cl/kernels/ClGemmMatrixMultiplyReshapedKernel.h"
38#include "src/gpu/cl/kernels/ClGemmReshapeLhsMatrixKernel.h"
SiCong Libc166d52019-09-26 14:58:53 +010039#include "tests/CL/Helper.h"
SiCong Libc166d52019-09-26 14:58:53 +010040#include "utils/command_line/CommandLineOptions.h"
41#include "utils/command_line/CommandLineParser.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010042#include "utils/Utils.h"
SiCong Libc166d52019-09-26 14:58:53 +010043
44#include <cstdlib>
45
46using namespace arm_compute;
Georgios Pinitas856f66e2021-04-22 21:13:21 +010047using namespace arm_compute::opencl::kernels;
SiCong Libc166d52019-09-26 14:58:53 +010048using namespace utils;
49using namespace arm_compute::misc::shape_calculator;
50using namespace gemm_tuner;
51
52namespace
53{
54/** Structure holding all tunable gemm configs specific to this example/strategy */
55struct GemmConfigs
56{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 size_t m0{4}; /**< Number of rows processed by the matrix multiplication */
58 size_t n0{4}; /**< Number of columns processed by the matrix multiplication */
59 size_t k0{4}; /**< Number of partial accumulations performed by the matrix multiplication */
60 size_t v0{1}; /**< Number of vertical blocks of size (m0xk0) stored on the same output row */
61 size_t h0{1}; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row */
62 bool interleave_lhs{true}; /**< Interleave lhs matrix */
63 bool transpose_lhs{true}; /**< Transpose lhs matrix. */
64 bool interleave_rhs{true}; /**< Interleave rhs matrix */
65 bool transpose_rhs{true}; /**< Transpose rhs matrix. */
66 bool export_to_cl_image_rhs{true}; /**< Export rhs matrix to cl_image. */
SiCong Libc166d52019-09-26 14:58:53 +010067};
68
69/** Formatted output of the GemmConfigs type
70 *
71 * @param[out] os Output stream.
72 * @param[in] configs Tunable configurations to output
73 *
74 * @return Modified output stream.
75 */
76::std::ostream &operator<<(::std::ostream &os, const GemmConfigs &configs)
77{
78 std::string false_str = std::string("false");
79 std::string true_str = std::string("true");
80
81 os << "m0 : " << configs.m0 << std::endl;
82 os << "n0 : " << configs.n0 << std::endl;
83 os << "k0 : " << configs.k0 << std::endl;
84 os << "v0 : " << configs.v0 << std::endl;
85 os << "h0 : " << configs.h0 << std::endl;
86 os << "interleave_lhs : " << (configs.interleave_lhs ? true_str : false_str) << std::endl;
87 os << "transpose_lhs : " << (configs.transpose_lhs ? true_str : false_str) << std::endl;
88 os << "interleave_rhs : " << (configs.interleave_rhs ? true_str : false_str) << std::endl;
89 os << "transpose_rhs : " << (configs.transpose_rhs ? true_str : false_str) << std::endl;
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +010090 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 +010091 return os;
92}
93
94/** Command line options for gemm configs */
95class GemmConfigOptions
96{
97public:
98 /** Constructor
99 *
100 * @param[in,out] parser A parser on which "parse()" hasn't been called yet.
101 */
102 GemmConfigOptions(CommandLineParser &parser)
103 : m0(parser.add_positional_option<SimpleOption<size_t>>("m0", 4)),
104 n0(parser.add_positional_option<SimpleOption<size_t>>("n0", 4)),
105 k0(parser.add_positional_option<SimpleOption<size_t>>("k0", 4)),
106 v0(parser.add_positional_option<SimpleOption<size_t>>("v0", 1)),
107 h0(parser.add_positional_option<SimpleOption<size_t>>("h0", 1)),
108 interleave_lhs(parser.add_positional_option<SimpleOption<size_t>>("interleave_lhs", 1)),
109 interleave_rhs(parser.add_positional_option<SimpleOption<size_t>>("interleave_rhs", 1)),
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100110 transpose_rhs(parser.add_positional_option<SimpleOption<size_t>>("transpose_rhs", 1)),
111 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 +0100112 {
113 m0->set_help("Number of rows processed by the matrix multiplication");
114 n0->set_help("Number of columns processed by the matrix multiplication");
115 k0->set_help("Number of partial accumulations performed by the matrix multiplication");
116 v0->set_help("Number of vertical blocks of size (m0xk0) stored on the same output row");
117 h0->set_help("Number of horizontal blocks of size (k0xn0) stored on the same output row");
118 interleave_lhs->set_help("Interleave lhs matrix (1) / Do not interleave lhs matrix (0)");
119 interleave_rhs->set_help("Interleave rhs matrix (1) / Do not interleave rhs matrix (0)");
120 // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and
121 // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other
122 // 2 variants (both transposed and none transposed)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100123 transpose_rhs->set_help("Transpose rhs matrix but not lhs matrix (1) / Do not transpose rhs matrix but do "
124 "transpose lhs matrix (0)");
125 export_to_cl_image_rhs->set_help(
126 "Export rhs matrix to cl_image (1) / Do not export rhs matrix to cl_image (0)");
SiCong Libc166d52019-09-26 14:58:53 +0100127 }
128 /** Prevent instances of this class from being copied (As this class contains pointers) */
129 GemmConfigOptions(const GemmConfigOptions &) = delete;
130 /** Prevent instances of this class from being copied (As this class contains pointers) */
131 GemmConfigOptions &operator=(const GemmConfigOptions &) = delete;
132 /** Allow instances of this class to be moved */
133 GemmConfigOptions(GemmConfigOptions &&) = default;
134 /** Allow instances of this class to be moved */
135 GemmConfigOptions &operator=(GemmConfigOptions &&) = default;
136 /** Default destructor */
137 ~GemmConfigOptions() = default;
138
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100139 SimpleOption<size_t> *m0; /**< Number of rows processed by the matrix multiplication option */
140 SimpleOption<size_t> *n0; /**< Number of columns processed by the matrix multiplication option */
141 SimpleOption<size_t> *k0; /**< Number of partial accumulations performed by the matrix multiplication option */
142 SimpleOption<size_t> *v0; /**< Number of vertical blocks of size (m0xk0) stored on the same output row option */
143 SimpleOption<size_t> *h0; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row option */
SiCong Libc166d52019-09-26 14:58:53 +0100144 SimpleOption<size_t> *interleave_lhs; /**< Interleave lhs matrix option (1 enable; 0 disable) */
145 SimpleOption<size_t> *interleave_rhs; /**< Interleave rhs matrix option (1 enable; 0 disable) */
146 // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and
147 // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other
148 // 2 variants (both transposed and none transposed)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100149 SimpleOption<size_t> *
150 transpose_rhs; /**< Transpose rhs matrix option (1 enable; 0 disable). Also set the lhs matrix transpose option to the opposite. */
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100151 SimpleOption<size_t> *export_to_cl_image_rhs; /**< Export rhs matrix to cl_image.*/
SiCong Libc166d52019-09-26 14:58:53 +0100152};
153
154/** Consumes the gemm configuration options and creates a structure containing all information
155 *
156 * @param[in] options Options to consume
157 *
158 * @return Structure containing the gemm configurations
159 */
160GemmConfigs consume_gemm_configs(const GemmConfigOptions &options)
161{
162 GemmConfigs configs;
163 configs.m0 = options.m0->value();
164 configs.n0 = options.n0->value();
165 configs.k0 = options.k0->value();
166 configs.v0 = options.v0->value();
167 configs.h0 = options.h0->value();
168 configs.interleave_lhs = options.interleave_lhs->value() != 0;
169 // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and
170 // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other
171 // 2 variants (both transposed and none transposed)
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100172 configs.transpose_lhs = options.transpose_rhs->value() == 0;
173 configs.interleave_rhs = options.interleave_rhs->value() != 0;
174 configs.transpose_rhs = options.transpose_rhs->value() != 0;
175 configs.export_to_cl_image_rhs = options.export_to_cl_image_rhs->value() != 0;
SiCong Libc166d52019-09-26 14:58:53 +0100176 return configs;
177}
178
179} // namespace
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100180
181// Create function for ClGemmReshapeLhsMatrixKernel
182using CLGEMMReshapeLHSMatrix = test::CLSynthetizeOperator<ClGemmReshapeLhsMatrixKernel>;
183// Create function for ClGemmMatrixMultiplyReshapedKernel
184using CLGEMMMatrixMultiplyReshaped = test::CLSynthetizeOperator<ClGemmMatrixMultiplyReshapedKernel>;
SiCong Libc166d52019-09-26 14:58:53 +0100185
186class CLGEMMMatrixMultiplyReshapedExample : public Example
187{
188public:
189 bool do_setup(int argc, char **argv) override
190 {
191 // Default parameters
Eren Kopuz350099e2020-06-09 15:37:43 +0100192 const float alpha = 1.0f;
193 const float beta = 0.0f;
194 const ActivationLayerInfo act_info = ActivationLayerInfo();
SiCong Libc166d52019-09-26 14:58:53 +0100195 CommonGemmExampleParams params;
196 GemmConfigs configs;
197
198 // Set up command line parser and options
199 CommandLineParser parser;
200 CommonGemmExampleOptions param_options(parser);
201 GemmConfigOptions config_options(parser);
202
203 // Parse command line options
204 parser.parse(argc, argv);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100205 if (param_options.help->is_set() && param_options.help->value())
SiCong Libc166d52019-09-26 14:58:53 +0100206 {
207 // Print help message
208 parser.print_help(argv[0]);
209 return false;
210 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100211 if (!parser.validate())
SiCong Libc166d52019-09-26 14:58:53 +0100212 {
213 // Invalid arguments. Use default parameters and configs
214 std::cerr << "Invalid arguments." << std::endl;
215 parser.print_help(argv[0]);
216 std::cerr << "Falling back to default parameters and configs" << std::endl;
217 }
218 else
219 {
220 // Get parameters and configs from command-line options
221 params = consume_common_gemm_example_parameters(param_options);
222 configs = consume_gemm_configs(config_options);
223 }
224
225 // Print gemm parameters and configurations
Eren Kopuz15205d92020-07-17 15:13:39 +0100226 std::cout << "Gemm parameters:" << std::endl;
227 std::cout << params << std::endl;
228 std::cout << "Gemm configurations:" << std::endl;
229 std::cout << configs << std::endl;
SiCong Libc166d52019-09-26 14:58:53 +0100230
Gian Marco Iodiceca419dd2021-03-03 17:25:07 +0000231 tuner.set_tuner_mode(params.tuner_mode);
232
SiCong Libc166d52019-09-26 14:58:53 +0100233 CLScheduler::get().default_init(&tuner);
234
Eren Kopuz350099e2020-06-09 15:37:43 +0100235 lhs.allocator()->init(TensorInfo(TensorShape(params.K, params.M, params.B), 1, params.data_type));
236 rhs.allocator()->init(TensorInfo(TensorShape(params.N, params.K, params.B), 1, params.data_type));
237 bias.allocator()->init(TensorInfo(TensorShape(params.N, 1, params.B), 1, params.data_type));
SiCong Libc166d52019-09-26 14:58:53 +0100238
239 GEMMLHSMatrixInfo lhs_info;
240 lhs_info.m0 = configs.m0;
241 lhs_info.k0 = configs.k0;
242 lhs_info.v0 = configs.v0;
243 lhs_info.interleave = configs.interleave_lhs;
244 lhs_info.transpose = configs.transpose_lhs;
245
246 GEMMRHSMatrixInfo rhs_info;
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100247 rhs_info.n0 = configs.n0;
248 rhs_info.k0 = configs.k0;
249 rhs_info.h0 = configs.h0;
250 rhs_info.interleave = configs.interleave_rhs;
251 rhs_info.transpose = configs.transpose_rhs;
252 rhs_info.export_to_cl_image = configs.export_to_cl_image_rhs;
SiCong Libc166d52019-09-26 14:58:53 +0100253
254 GEMMKernelInfo kernel_info;
255 kernel_info.m = params.M;
256 kernel_info.n = params.N;
257 kernel_info.k = params.K;
258 kernel_info.depth_output_gemm3d = 0;
259 kernel_info.reinterpret_input_as_3d = false;
260 kernel_info.broadcast_bias = true;
261 kernel_info.activation_info = act_info;
262
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100263 if (rhs_info.h0 == 0)
Gian Marco Iodice60ab4e62023-04-28 10:40:07 +0100264 {
265 rhs_info.h0 = std::max(kernel_info.n / rhs_info.n0, 1U);
266 }
267
SiCong Libc166d52019-09-26 14:58:53 +0100268 // Initialise lhs_reshaped tensor info
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100269 lhs_reshaped.allocator()->init(
270 TensorInfo(compute_lhs_reshaped_shape(*lhs.info(), lhs_info), 1, params.data_type));
SiCong Libc166d52019-09-26 14:58:53 +0100271
272 // Initialise rhs_reshaped tensor info
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100273 rhs_reshaped.allocator()->init(
274 TensorInfo(compute_rhs_reshaped_shape(*rhs.info(), rhs_info), 1, params.data_type));
SiCong Libc166d52019-09-26 14:58:53 +0100275
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100276 if (rhs_info.export_to_cl_image)
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100277 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100278 if (!examples::gemm_tuner_helpers::update_padding_for_cl_image(rhs_reshaped.info()))
Manuel Bottini7b427862021-02-08 13:45:19 +0000279 {
280 std::cerr << "cl_image is not supported on the device, disable export_to_cl_image" << std::endl;
281 return false;
282 }
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100283 }
284
Eren Kopuz15205d92020-07-17 15:13:39 +0100285 // Validate argments
286 Status status{};
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100287 status = reshape_lhs.validate(lhs.info(), lhs_reshaped.info(), lhs_info, kernel_info.reinterpret_input_as_3d);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100288 if (!status)
Eren Kopuz15205d92020-07-17 15:13:39 +0100289 {
290 // Unsupported arguments
291 std::cerr << "Unsupported arguments." << std::endl;
292 std::cerr << "Check documentation for supported/unsupported combinations" << std::endl;
293 return false;
294 }
295
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100296 status = gemm.validate(lhs_reshaped.info(), rhs_reshaped.info(), bias.info(), dst.info(), alpha, beta, lhs_info,
297 rhs_info, kernel_info);
298 if (!status)
Eren Kopuz15205d92020-07-17 15:13:39 +0100299 {
300 // Unsupported arguments
301 std::cerr << "Unsupported arguments." << std::endl;
302 std::cerr << "Check documentation for supported/unsupported combinations" << std::endl;
303 return false;
304 }
305
SiCong Lie36b5262019-10-01 19:26:00 +0100306 // Configure reshape lhs function
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100307 reshape_lhs.configure(lhs.info(), lhs_reshaped.info(), lhs_info);
SiCong Lie36b5262019-10-01 19:26:00 +0100308
SiCong Libc166d52019-09-26 14:58:53 +0100309 // Configure function
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100310 gemm.configure(lhs_reshaped.info(), rhs_reshaped.info(), bias.info(), dst.info(), alpha, beta, lhs_info,
311 rhs_info, kernel_info);
SiCong Libc166d52019-09-26 14:58:53 +0100312
313 // Allocate tensors
314 lhs.allocator()->allocate();
315 rhs.allocator()->allocate();
316 lhs_reshaped.allocator()->allocate();
317 rhs_reshaped.allocator()->allocate();
318 bias.allocator()->allocate();
319 dst.allocator()->allocate();
320
321 return true;
322 }
323 void do_run() override
324 {
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100325 // Execute the functions
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100326 ITensorPack reshape_lsh_pack({{ACL_SRC, &lhs}, {ACL_DST, &lhs_reshaped}});
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100327 reshape_lhs.run(reshape_lsh_pack);
328
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100329 ITensorPack gemm_pack(
330 {{ACL_SRC_0, &lhs_reshaped}, {ACL_SRC_1, &rhs_reshaped}, {ACL_SRC_2, &bias}, {ACL_DST, &dst}});
Gian Marco Iodice3cfd3292021-08-24 11:58:25 +0100331 gemm.run(gemm_pack);
SiCong Libc166d52019-09-26 14:58:53 +0100332
333 // Make sure all the OpenCL jobs are done executing:
334 CLScheduler::get().sync();
335 }
336
337 void do_teardown() override
338 {
339 }
340
341private:
342 CLTensor lhs{};
343 CLTensor rhs{};
344 CLTensor lhs_reshaped{};
345 CLTensor rhs_reshaped{};
346 CLTensor bias{};
347 CLTensor dst{};
348 CLTuner tuner{};
SiCong Lie36b5262019-10-01 19:26:00 +0100349 CLGEMMReshapeLHSMatrix reshape_lhs{};
SiCong Libc166d52019-09-26 14:58:53 +0100350 CLGEMMMatrixMultiplyReshaped gemm{};
351};
352
353/** Main program for gemm reshaped test
354 *
355 * @param[in] argc Number of arguments
SiCongLi282f3242020-11-24 15:24:16 +0000356 * @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 +0100357 */
358int main(int argc, char **argv)
359{
360 return run_example<CLGEMMMatrixMultiplyReshapedExample>(argc, argv);
361}