blob: 3808b98b7dc2dccdd52eb5a2228703694852b6e2 [file] [log] [blame]
SiCongLi282f3242020-11-24 15:24:16 +00001/*
Gian Marco Iodice60ab4e62023-04-28 10:40:07 +01002 * Copyright (c) 2020-2021, 2023 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"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010034
SiCongLi282f3242020-11-24 15:24:16 +000035#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/ClGemmLowpMatrixMultiplyReshapedKernel.h"
38#include "src/gpu/cl/kernels/ClGemmReshapeLhsMatrixKernel.h"
SiCongLi282f3242020-11-24 15:24:16 +000039#include "tests/CL/Helper.h"
SiCongLi282f3242020-11-24 15:24:16 +000040#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"
SiCongLi282f3242020-11-24 15:24:16 +000043
44#include <cstdlib>
45
46using namespace arm_compute;
Georgios Pinitas856f66e2021-04-22 21:13:21 +010047using namespace arm_compute::opencl::kernels;
SiCongLi282f3242020-11-24 15:24:16 +000048using 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. */
SiCongLi282f3242020-11-24 15:24:16 +000066};
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;
89 return os;
90}
91
92/** Command line options for gemm configs */
93class GemmConfigOptions
94{
95public:
96 /** Constructor
97 *
98 * @param[in,out] parser A parser on which "parse()" hasn't been called yet.
99 */
100 GemmConfigOptions(CommandLineParser &parser)
101 : m0(parser.add_positional_option<SimpleOption<size_t>>("m0", 4)),
102 n0(parser.add_positional_option<SimpleOption<size_t>>("n0", 4)),
103 k0(parser.add_positional_option<SimpleOption<size_t>>("k0", 4)),
104 v0(parser.add_positional_option<SimpleOption<size_t>>("v0", 1)),
105 h0(parser.add_positional_option<SimpleOption<size_t>>("h0", 1)),
106 interleave_lhs(parser.add_positional_option<SimpleOption<size_t>>("interleave_lhs", 1)),
107 interleave_rhs(parser.add_positional_option<SimpleOption<size_t>>("interleave_rhs", 1)),
108 transpose_rhs(parser.add_positional_option<SimpleOption<size_t>>("transpose_rhs", 1))
109 {
110 m0->set_help("Number of rows processed by the matrix multiplication");
111 n0->set_help("Number of columns processed by the matrix multiplication");
112 k0->set_help("Number of partial accumulations performed by the matrix multiplication");
113 v0->set_help("Number of vertical blocks of size (m0xk0) stored on the same output row");
114 h0->set_help("Number of horizontal blocks of size (k0xn0) stored on the same output row");
115 interleave_lhs->set_help("Interleave lhs matrix (1) / Do not interleave lhs matrix (0)");
116 interleave_rhs->set_help("Interleave rhs matrix (1) / Do not interleave rhs matrix (0)");
117 // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and
118 // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other
119 // 2 variants (both transposed and none transposed)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100120 transpose_rhs->set_help("Transpose rhs matrix but not lhs matrix (1) / Do not transpose rhs matrix but do "
121 "transpose lhs matrix (0)");
SiCongLi282f3242020-11-24 15:24:16 +0000122 }
123 /** Prevent instances of this class from being copied (As this class contains pointers) */
124 GemmConfigOptions(const GemmConfigOptions &) = delete;
125 /** Prevent instances of this class from being copied (As this class contains pointers) */
126 GemmConfigOptions &operator=(const GemmConfigOptions &) = delete;
127 /** Allow instances of this class to be moved */
128 GemmConfigOptions(GemmConfigOptions &&) = default;
129 /** Allow instances of this class to be moved */
130 GemmConfigOptions &operator=(GemmConfigOptions &&) = default;
131 /** Default destructor */
132 ~GemmConfigOptions() = default;
133
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100134 SimpleOption<size_t> *m0; /**< Number of rows processed by the matrix multiplication option */
135 SimpleOption<size_t> *n0; /**< Number of columns processed by the matrix multiplication option */
136 SimpleOption<size_t> *k0; /**< Number of partial accumulations performed by the matrix multiplication option */
137 SimpleOption<size_t> *v0; /**< Number of vertical blocks of size (m0xk0) stored on the same output row option */
138 SimpleOption<size_t> *h0; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row option */
SiCongLi282f3242020-11-24 15:24:16 +0000139 SimpleOption<size_t> *interleave_lhs; /**< Interleave lhs matrix option (1 enable; 0 disable) */
140 SimpleOption<size_t> *interleave_rhs; /**< Interleave rhs matrix option (1 enable; 0 disable) */
141 // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and
142 // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other
143 // 2 variants (both transposed and none transposed)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100144 SimpleOption<size_t> *
145 transpose_rhs; /**< Transpose rhs matrix option (1 enable; 0 disable). Also set the lhs matrix transpose option to the opposite. */
SiCongLi282f3242020-11-24 15:24:16 +0000146};
147
148/** Consumes the gemm configuration options and creates a structure containing all information
149 *
150 * @param[in] options Options to consume
151 *
152 * @return Structure containing the gemm configurations
153 */
154GemmConfigs consume_gemm_configs(const GemmConfigOptions &options)
155{
156 GemmConfigs configs;
157 configs.m0 = options.m0->value();
158 configs.n0 = options.n0->value();
159 configs.k0 = options.k0->value();
160 configs.v0 = options.v0->value();
161 configs.h0 = options.h0->value();
162 configs.interleave_lhs = options.interleave_lhs->value() != 0;
163 // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and
164 // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other
165 // 2 variants (both transposed and none transposed)
166 configs.transpose_lhs = options.transpose_rhs->value() == 0;
167 configs.interleave_rhs = options.interleave_rhs->value() != 0;
168 configs.transpose_rhs = options.transpose_rhs->value() != 0;
169 return configs;
170}
171
172} // namespace
173
Georgios Pinitas4a578b92021-06-25 12:13:49 +0100174using ClGemmReshapeLHSMatrix = test::CLSynthetizeOperator<ClGemmReshapeLhsMatrixKernel>;
175using ClGemmLowpMatrixMultiplyReshaped = test::CLSynthetizeOperator<ClGemmLowpMatrixMultiplyReshapedKernel>;
SiCongLi282f3242020-11-24 15:24:16 +0000176
177class CLGEMMLowpMatrixMultiplyReshapedExample : public Example
178{
179public:
180 bool do_setup(int argc, char **argv) override
181 {
182 // Default parameters
183 CommonGemmExampleParams params;
184 GemmConfigs configs;
185
186 // Parse command line options
187 CommandLineParser parser;
SiCong Li98e33b92020-12-03 14:52:53 +0000188 CommonGemmExampleOptions param_options(parser, DataType::QASYMM8);
SiCongLi282f3242020-11-24 15:24:16 +0000189 GemmConfigOptions config_options(parser);
190
191 parser.parse(argc, argv);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100192 if (param_options.help->is_set() && param_options.help->value())
SiCongLi282f3242020-11-24 15:24:16 +0000193 {
194 parser.print_help(argv[0]);
195 return false;
196 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100197 if (!parser.validate())
SiCongLi282f3242020-11-24 15:24:16 +0000198 {
199 // Invalid arguments. Use default parameters and configs
200 std::cerr << "Invalid arguments." << std::endl;
201 parser.print_help(argv[0]);
202 std::cerr << "Falling back to default parameters and configs" << std::endl;
203 }
204 else
205 {
206 params = consume_common_gemm_example_parameters(param_options);
207 configs = consume_gemm_configs(config_options);
208 }
209
210 std::cout << "Gemm parameters:" << std::endl;
211 std::cout << params << std::endl;
212 std::cout << "Gemm configurations:" << std::endl;
213 std::cout << configs << std::endl;
214
Gian Marco Iodiceca419dd2021-03-03 17:25:07 +0000215 tuner.set_tuner_mode(params.tuner_mode);
216
SiCongLi282f3242020-11-24 15:24:16 +0000217 CLScheduler::get().default_init(&tuner);
218
219 lhs.allocator()->init(TensorInfo(TensorShape(params.K, params.M, params.B), 1, params.data_type));
220 rhs.allocator()->init(TensorInfo(TensorShape(params.N, params.K, params.B), 1, params.data_type));
221
222 // Set arbitrary quantization information
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100223 const QuantizationInfo q_info{0.012, 3};
SiCong Li98e33b92020-12-03 14:52:53 +0000224 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
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100243 if (rhs_info.h0 == 0)
Gian Marco Iodice60ab4e62023-04-28 10:40:07 +0100244 {
245 rhs_info.h0 = std::max(static_cast<unsigned int>(params.N) / rhs_info.n0, 1U);
246 }
247
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100248 lhs_reshaped.allocator()->init(
249 TensorInfo(compute_lhs_reshaped_shape(*lhs.info(), lhs_info), 1, params.data_type));
250 rhs_reshaped.allocator()->init(
251 TensorInfo(compute_rhs_reshaped_shape(*rhs.info(), rhs_info), 1, params.data_type));
SiCong Li98e33b92020-12-03 14:52:53 +0000252 lhs_reshaped.info()->set_quantization_info(q_info);
253 rhs_reshaped.info()->set_quantization_info(q_info);
SiCongLi282f3242020-11-24 15:24:16 +0000254
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100255 if (rhs_info.export_to_cl_image)
SiCongLi282f3242020-11-24 15:24:16 +0000256 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100257 if (!examples::gemm_tuner_helpers::update_padding_for_cl_image(rhs_reshaped.info()))
Manuel Bottini7b427862021-02-08 13:45:19 +0000258 {
259 std::cerr << "cl_image is not supported on the device, disable export_to_cl_image" << std::endl;
260 return false;
261 }
SiCongLi282f3242020-11-24 15:24:16 +0000262 }
263
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100264 GEMMReshapeInfo gemm_info{static_cast<int>(params.M),
265 static_cast<int>(params.N),
266 static_cast<int>(params.K),
267 static_cast<int>(configs.h0),
268 static_cast<int>(configs.v0),
269 0,
270 false,
271 true};
SiCongLi282f3242020-11-24 15:24:16 +0000272
273 // Validate argments
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100274 if (!reshape_lhs.validate(lhs.info(), lhs_reshaped.info(), lhs_info, gemm_info.reinterpret_input_as_3d()))
SiCongLi282f3242020-11-24 15:24:16 +0000275 {
Georgios Pinitas4a578b92021-06-25 12:13:49 +0100276 std::cerr << "Invalid arguments for ClGemmReshapeLHSMatrixKernel." << std::endl;
SiCongLi282f3242020-11-24 15:24:16 +0000277 return false;
278 }
279
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100280 if (!gemm.validate(lhs_reshaped.info(), rhs_reshaped.info(), dst.info(), lhs_info, rhs_info, gemm_info))
SiCongLi282f3242020-11-24 15:24:16 +0000281 {
Georgios Pinitas4a578b92021-06-25 12:13:49 +0100282 std::cerr << "Invalid arguments for ClGemmLowpMatrixMultiplyReshapedKernel." << std::endl;
SiCongLi282f3242020-11-24 15:24:16 +0000283 return false;
284 }
285
286 // Configure functions
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100287 reshape_lhs.configure(lhs.info(), lhs_reshaped.info(), lhs_info);
SiCongLi282f3242020-11-24 15:24:16 +0000288
Georgios Pinitas4a578b92021-06-25 12:13:49 +0100289 gemm.configure(lhs_reshaped.info(), rhs_reshaped.info(), dst.info(), lhs_info, rhs_info, gemm_info);
SiCongLi282f3242020-11-24 15:24:16 +0000290
291 // Allocate tensors
292 lhs.allocator()->allocate();
293 rhs.allocator()->allocate();
294 lhs_reshaped.allocator()->allocate();
295 rhs_reshaped.allocator()->allocate();
296 dst.allocator()->allocate();
297
298 return true;
299 }
300 void do_run() override
301 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100302 ITensorPack reshape_lsh_pack({{ACL_SRC, &lhs}, {ACL_DST, &lhs_reshaped}});
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100303 reshape_lhs.run(reshape_lsh_pack);
304
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100305 ITensorPack gemm_pack({{ACL_SRC_0, &lhs_reshaped}, {ACL_SRC_1, &rhs_reshaped}, {ACL_DST, &dst}});
Georgios Pinitas4a578b92021-06-25 12:13:49 +0100306 gemm.run(gemm_pack);
SiCongLi282f3242020-11-24 15:24:16 +0000307
308 // Make sure all the OpenCL jobs are done executing:
309 CLScheduler::get().sync();
310 }
311
312 void do_teardown() override
313 {
314 }
315
316private:
317 CLTensor lhs{};
318 CLTensor rhs{};
319 CLTensor lhs_reshaped{};
320 CLTensor rhs_reshaped{};
321 CLTensor dst{};
322 CLTuner tuner{};
Georgios Pinitas4a578b92021-06-25 12:13:49 +0100323 ClGemmReshapeLHSMatrix reshape_lhs{};
324 ClGemmLowpMatrixMultiplyReshaped gemm{};
SiCongLi282f3242020-11-24 15:24:16 +0000325};
326
327/** Main test program for gemmlowp reshaped
328 *
329 * @param[in] argc Number of arguments
330 * @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 )
331 */
332int main(int argc, char **argv)
333{
334 return run_example<CLGEMMLowpMatrixMultiplyReshapedExample>(argc, argv);
335}