blob: 7c26e0f389dbd9a1168ea5890a28d8ef7e398c22 [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"
36#include "src/core/CL/kernels/CLGEMMLowpMatrixMultiplyReshapedKernel.h"
37#include "src/core/CL/kernels/CLGEMMReshapeLHSMatrixKernel.h"
38#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;
46using namespace utils;
47using namespace arm_compute::misc::shape_calculator;
48using namespace gemm_tuner;
49
50namespace
51{
52/** Structure holding all tunable gemm configs specific to this example/strategy */
53struct GemmConfigs
54{
55 size_t m0{ 4 }; /**< Number of rows processed by the matrix multiplication */
56 size_t n0{ 4 }; /**< Number of columns processed by the matrix multiplication */
57 size_t k0{ 4 }; /**< Number of partial accumulations performed by the matrix multiplication */
58 size_t v0{ 1 }; /**< Number of vertical blocks of size (m0xk0) stored on the same output row */
59 size_t h0{ 1 }; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row */
60 bool interleave_lhs{ true }; /**< Interleave lhs matrix */
61 bool transpose_lhs{ true }; /**< Transpose lhs matrix. */
62 bool interleave_rhs{ true }; /**< Interleave rhs matrix */
63 bool transpose_rhs{ true }; /**< Transpose rhs matrix. */
64};
65
66/** Formatted output of the GemmConfigs type
67 *
68 * @param[out] os Output stream.
69 * @param[in] configs Tunable configurations to output
70 *
71 * @return Modified output stream.
72 */
73::std::ostream &operator<<(::std::ostream &os, const GemmConfigs &configs)
74{
75 std::string false_str = std::string("false");
76 std::string true_str = std::string("true");
77
78 os << "m0 : " << configs.m0 << std::endl;
79 os << "n0 : " << configs.n0 << std::endl;
80 os << "k0 : " << configs.k0 << std::endl;
81 os << "v0 : " << configs.v0 << std::endl;
82 os << "h0 : " << configs.h0 << std::endl;
83 os << "interleave_lhs : " << (configs.interleave_lhs ? true_str : false_str) << std::endl;
84 os << "transpose_lhs : " << (configs.transpose_lhs ? true_str : false_str) << std::endl;
85 os << "interleave_rhs : " << (configs.interleave_rhs ? true_str : false_str) << std::endl;
86 os << "transpose_rhs : " << (configs.transpose_rhs ? true_str : false_str) << std::endl;
87 return os;
88}
89
90/** Command line options for gemm configs */
91class GemmConfigOptions
92{
93public:
94 /** Constructor
95 *
96 * @param[in,out] parser A parser on which "parse()" hasn't been called yet.
97 */
98 GemmConfigOptions(CommandLineParser &parser)
99 : m0(parser.add_positional_option<SimpleOption<size_t>>("m0", 4)),
100 n0(parser.add_positional_option<SimpleOption<size_t>>("n0", 4)),
101 k0(parser.add_positional_option<SimpleOption<size_t>>("k0", 4)),
102 v0(parser.add_positional_option<SimpleOption<size_t>>("v0", 1)),
103 h0(parser.add_positional_option<SimpleOption<size_t>>("h0", 1)),
104 interleave_lhs(parser.add_positional_option<SimpleOption<size_t>>("interleave_lhs", 1)),
105 interleave_rhs(parser.add_positional_option<SimpleOption<size_t>>("interleave_rhs", 1)),
106 transpose_rhs(parser.add_positional_option<SimpleOption<size_t>>("transpose_rhs", 1))
107 {
108 m0->set_help("Number of rows processed by the matrix multiplication");
109 n0->set_help("Number of columns processed by the matrix multiplication");
110 k0->set_help("Number of partial accumulations performed by the matrix multiplication");
111 v0->set_help("Number of vertical blocks of size (m0xk0) stored on the same output row");
112 h0->set_help("Number of horizontal blocks of size (k0xn0) stored on the same output row");
113 interleave_lhs->set_help("Interleave lhs matrix (1) / Do not interleave lhs matrix (0)");
114 interleave_rhs->set_help("Interleave rhs matrix (1) / Do not interleave rhs matrix (0)");
115 // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and
116 // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other
117 // 2 variants (both transposed and none transposed)
118 transpose_rhs->set_help("Transpose rhs matrix but not lhs matrix (1) / Do not transpose rhs matrix but do transpose lhs matrix (0)");
119 }
120 /** Prevent instances of this class from being copied (As this class contains pointers) */
121 GemmConfigOptions(const GemmConfigOptions &) = delete;
122 /** Prevent instances of this class from being copied (As this class contains pointers) */
123 GemmConfigOptions &operator=(const GemmConfigOptions &) = delete;
124 /** Allow instances of this class to be moved */
125 GemmConfigOptions(GemmConfigOptions &&) = default;
126 /** Allow instances of this class to be moved */
127 GemmConfigOptions &operator=(GemmConfigOptions &&) = default;
128 /** Default destructor */
129 ~GemmConfigOptions() = default;
130
131 SimpleOption<size_t> *m0; /**< Number of rows processed by the matrix multiplication option */
132 SimpleOption<size_t> *n0; /**< Number of columns processed by the matrix multiplication option */
133 SimpleOption<size_t> *k0; /**< Number of partial accumulations performed by the matrix multiplication option */
134 SimpleOption<size_t> *v0; /**< Number of vertical blocks of size (m0xk0) stored on the same output row option */
135 SimpleOption<size_t> *h0; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row option */
136 SimpleOption<size_t> *interleave_lhs; /**< Interleave lhs matrix option (1 enable; 0 disable) */
137 SimpleOption<size_t> *interleave_rhs; /**< Interleave rhs matrix option (1 enable; 0 disable) */
138 // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and
139 // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other
140 // 2 variants (both transposed and none transposed)
141 SimpleOption<size_t> *transpose_rhs; /**< Transpose rhs matrix option (1 enable; 0 disable). Also set the lhs matrix transpose option to the opposite. */
142};
143
144/** Consumes the gemm configuration options and creates a structure containing all information
145 *
146 * @param[in] options Options to consume
147 *
148 * @return Structure containing the gemm configurations
149 */
150GemmConfigs consume_gemm_configs(const GemmConfigOptions &options)
151{
152 GemmConfigs configs;
153 configs.m0 = options.m0->value();
154 configs.n0 = options.n0->value();
155 configs.k0 = options.k0->value();
156 configs.v0 = options.v0->value();
157 configs.h0 = options.h0->value();
158 configs.interleave_lhs = options.interleave_lhs->value() != 0;
159 // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and
160 // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other
161 // 2 variants (both transposed and none transposed)
162 configs.transpose_lhs = options.transpose_rhs->value() == 0;
163 configs.interleave_rhs = options.interleave_rhs->value() != 0;
164 configs.transpose_rhs = options.transpose_rhs->value() != 0;
165 return configs;
166}
167
168} // namespace
169
170using CLGEMMReshapeLHSMatrix = test::CLSynthetizeFunction<CLGEMMReshapeLHSMatrixKernel>;
171using CLGEMMLowpMatrixMultiplyReshaped = test::CLSynthetizeFunction<CLGEMMLowpMatrixMultiplyReshapedKernel>;
172
173class CLGEMMLowpMatrixMultiplyReshapedExample : public Example
174{
175public:
176 bool do_setup(int argc, char **argv) override
177 {
178 // Default parameters
179 CommonGemmExampleParams params;
180 GemmConfigs configs;
181
182 // Parse command line options
183 CommandLineParser parser;
SiCong Li98e33b92020-12-03 14:52:53 +0000184 CommonGemmExampleOptions param_options(parser, DataType::QASYMM8);
SiCongLi282f3242020-11-24 15:24:16 +0000185 GemmConfigOptions config_options(parser);
186
187 parser.parse(argc, argv);
188 if(param_options.help->is_set() && param_options.help->value())
189 {
190 parser.print_help(argv[0]);
191 return false;
192 }
193 if(!parser.validate())
194 {
195 // Invalid arguments. Use default parameters and configs
196 std::cerr << "Invalid arguments." << std::endl;
197 parser.print_help(argv[0]);
198 std::cerr << "Falling back to default parameters and configs" << std::endl;
199 }
200 else
201 {
202 params = consume_common_gemm_example_parameters(param_options);
203 configs = consume_gemm_configs(config_options);
204 }
205
206 std::cout << "Gemm parameters:" << std::endl;
207 std::cout << params << std::endl;
208 std::cout << "Gemm configurations:" << std::endl;
209 std::cout << configs << std::endl;
210
211 CLScheduler::get().default_init(&tuner);
212
213 lhs.allocator()->init(TensorInfo(TensorShape(params.K, params.M, params.B), 1, params.data_type));
214 rhs.allocator()->init(TensorInfo(TensorShape(params.N, params.K, params.B), 1, params.data_type));
215
216 // Set arbitrary quantization information
SiCong Li98e33b92020-12-03 14:52:53 +0000217 const QuantizationInfo q_info
218 {
219 0.012, 3
220 };
221 lhs.info()->set_quantization_info(q_info);
222 rhs.info()->set_quantization_info(q_info);
223 dst.info()->set_quantization_info(q_info);
SiCongLi282f3242020-11-24 15:24:16 +0000224
225 GEMMLHSMatrixInfo lhs_info;
226 lhs_info.m0 = configs.m0;
227 lhs_info.k0 = configs.k0;
228 lhs_info.v0 = configs.v0;
229 lhs_info.interleave = configs.interleave_lhs;
230 lhs_info.transpose = configs.transpose_lhs;
231
232 GEMMRHSMatrixInfo rhs_info;
233 rhs_info.n0 = configs.n0;
234 rhs_info.k0 = configs.k0;
235 rhs_info.h0 = configs.h0;
236 rhs_info.interleave = configs.interleave_rhs;
237 rhs_info.transpose = configs.transpose_rhs;
238 rhs_info.export_to_cl_image = false; // CL image not supported for quantized cases yet
239
240 lhs_reshaped.allocator()->init(TensorInfo(compute_lhs_reshaped_shape(*lhs.info(), lhs_info), 1, params.data_type));
SiCongLi282f3242020-11-24 15:24:16 +0000241 rhs_reshaped.allocator()->init(TensorInfo(compute_rhs_reshaped_shape(*rhs.info(), rhs_info), 1, params.data_type));
SiCong Li98e33b92020-12-03 14:52:53 +0000242 lhs_reshaped.info()->set_quantization_info(q_info);
243 rhs_reshaped.info()->set_quantization_info(q_info);
SiCongLi282f3242020-11-24 15:24:16 +0000244
245 if(rhs_info.export_to_cl_image)
246 {
Manuel Bottini7b427862021-02-08 13:45:19 +0000247 if(!examples::gemm_tuner_helpers::update_padding_for_cl_image(rhs_reshaped.info()))
248 {
249 std::cerr << "cl_image is not supported on the device, disable export_to_cl_image" << std::endl;
250 return false;
251 }
SiCongLi282f3242020-11-24 15:24:16 +0000252 }
253
254 GEMMReshapeInfo gemm_info
255 {
256 static_cast<int>(params.M),
257 static_cast<int>(params.N),
258 static_cast<int>(params.K),
259 static_cast<int>(configs.h0),
260 static_cast<int>(configs.v0),
261 0,
262 false,
263 true
264 };
265
266 // Validate argments
267 if(!reshape_lhs.validate(lhs.info(), lhs_reshaped.info(), lhs_info, gemm_info.reinterpret_input_as_3d()))
268 {
269 std::cerr << "Invalid arguments for CLGEMMReshapeLHSMatrixKernel." << std::endl;
270 return false;
271 }
272
273 if(!gemm.validate(lhs_reshaped.info(), rhs_reshaped.info(), dst.info(), lhs_info, rhs_info, gemm_info))
274 {
275 std::cerr << "Invalid arguments for CLGEMMLowpMatrixMultiplyReshapedKernel." << std::endl;
276 return false;
277 }
278
279 // Configure functions
280 reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info);
281
282 gemm.configure(&lhs_reshaped, &rhs_reshaped, &dst, lhs_info, rhs_info, gemm_info);
283
284 // Allocate tensors
285 lhs.allocator()->allocate();
286 rhs.allocator()->allocate();
287 lhs_reshaped.allocator()->allocate();
288 rhs_reshaped.allocator()->allocate();
289 dst.allocator()->allocate();
290
291 return true;
292 }
293 void do_run() override
294 {
295 reshape_lhs.run();
296 gemm.run();
297
298 // Make sure all the OpenCL jobs are done executing:
299 CLScheduler::get().sync();
300 }
301
302 void do_teardown() override
303 {
304 }
305
306private:
307 CLTensor lhs{};
308 CLTensor rhs{};
309 CLTensor lhs_reshaped{};
310 CLTensor rhs_reshaped{};
311 CLTensor dst{};
312 CLTuner tuner{};
313 CLGEMMReshapeLHSMatrix reshape_lhs{};
314 CLGEMMLowpMatrixMultiplyReshaped gemm{};
315};
316
317/** Main test program for gemmlowp reshaped
318 *
319 * @param[in] argc Number of arguments
320 * @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 )
321 */
322int main(int argc, char **argv)
323{
324 return run_example<CLGEMMLowpMatrixMultiplyReshapedExample>(argc, argv);
325}