blob: 9c6568cffb7d10d3106ee9d2fc20cb8755cd0ffd [file] [log] [blame]
SiCong Libc166d52019-09-26 14:58:53 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 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
28#include "CommonGemmExampleOptions.h"
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +010029#include "arm_compute/core/CL/gemm/CLGEMMHelpers.h"
SiCong Libc166d52019-09-26 14:58:53 +010030#include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyReshapedKernel.h"
SiCong Lie36b5262019-10-01 19:26:00 +010031#include "arm_compute/core/CL/kernels/CLGEMMReshapeLHSMatrixKernel.h"
SiCong Libc166d52019-09-26 14:58:53 +010032#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/KernelDescriptors.h"
34#include "arm_compute/core/Types.h"
35#include "arm_compute/core/utils/misc/ShapeCalculator.h"
36#include "arm_compute/runtime/CL/CLFunctions.h"
37#include "arm_compute/runtime/CL/CLScheduler.h"
38#include "arm_compute/runtime/CL/CLTuner.h"
39#include "tests/CL/Helper.h"
40#include "utils/Utils.h"
41#include "utils/command_line/CommandLineOptions.h"
42#include "utils/command_line/CommandLineParser.h"
43
44#include <cstdlib>
45
46using namespace arm_compute;
47using 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
SiCong Lie36b5262019-10-01 19:26:00 +0100176// Create function for CLGEMMReshapeLHSMatrixKernel
177using CLGEMMReshapeLHSMatrix = test::CLSynthetizeFunction<CLGEMMReshapeLHSMatrixKernel>;
SiCong Libc166d52019-09-26 14:58:53 +0100178// Create function for CLGEMMMatrixMultiplyReshapedKernel
179using CLGEMMMatrixMultiplyReshaped = test::CLSynthetizeFunction<CLGEMMMatrixMultiplyReshapedKernel>;
180
181class CLGEMMMatrixMultiplyReshapedExample : public Example
182{
183public:
184 bool do_setup(int argc, char **argv) override
185 {
186 // Default parameters
Eren Kopuz350099e2020-06-09 15:37:43 +0100187 const float alpha = 1.0f;
188 const float beta = 0.0f;
189 const ActivationLayerInfo act_info = ActivationLayerInfo();
SiCong Libc166d52019-09-26 14:58:53 +0100190 CommonGemmExampleParams params;
191 GemmConfigs configs;
192
193 // Set up command line parser and options
194 CommandLineParser parser;
195 CommonGemmExampleOptions param_options(parser);
196 GemmConfigOptions config_options(parser);
197
198 // Parse command line options
199 parser.parse(argc, argv);
200 if(param_options.help->is_set() && param_options.help->value())
201 {
202 // Print help message
203 parser.print_help(argv[0]);
204 return false;
205 }
206 if(!parser.validate())
207 {
208 // Invalid arguments. Use default parameters and configs
209 std::cerr << "Invalid arguments." << std::endl;
210 parser.print_help(argv[0]);
211 std::cerr << "Falling back to default parameters and configs" << std::endl;
212 }
213 else
214 {
215 // Get parameters and configs from command-line options
216 params = consume_common_gemm_example_parameters(param_options);
217 configs = consume_gemm_configs(config_options);
218 }
219
220 // Print gemm parameters and configurations
Eren Kopuz15205d92020-07-17 15:13:39 +0100221 std::cout << "Gemm parameters:" << std::endl;
222 std::cout << params << std::endl;
223 std::cout << "Gemm configurations:" << std::endl;
224 std::cout << configs << std::endl;
SiCong Libc166d52019-09-26 14:58:53 +0100225
226 CLScheduler::get().default_init(&tuner);
227
Eren Kopuz350099e2020-06-09 15:37:43 +0100228 lhs.allocator()->init(TensorInfo(TensorShape(params.K, params.M, params.B), 1, params.data_type));
229 rhs.allocator()->init(TensorInfo(TensorShape(params.N, params.K, params.B), 1, params.data_type));
230 bias.allocator()->init(TensorInfo(TensorShape(params.N, 1, params.B), 1, params.data_type));
SiCong Libc166d52019-09-26 14:58:53 +0100231
232 GEMMLHSMatrixInfo lhs_info;
233 lhs_info.m0 = configs.m0;
234 lhs_info.k0 = configs.k0;
235 lhs_info.v0 = configs.v0;
236 lhs_info.interleave = configs.interleave_lhs;
237 lhs_info.transpose = configs.transpose_lhs;
238
239 GEMMRHSMatrixInfo rhs_info;
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100240 rhs_info.n0 = configs.n0;
241 rhs_info.k0 = configs.k0;
242 rhs_info.h0 = configs.h0;
243 rhs_info.interleave = configs.interleave_rhs;
244 rhs_info.transpose = configs.transpose_rhs;
245 rhs_info.export_to_cl_image = configs.export_to_cl_image_rhs;
SiCong Libc166d52019-09-26 14:58:53 +0100246
247 GEMMKernelInfo kernel_info;
248 kernel_info.m = params.M;
249 kernel_info.n = params.N;
250 kernel_info.k = params.K;
251 kernel_info.depth_output_gemm3d = 0;
252 kernel_info.reinterpret_input_as_3d = false;
253 kernel_info.broadcast_bias = true;
254 kernel_info.activation_info = act_info;
255
256 // Initialise lhs_reshaped tensor info
257 auto_init_if_empty(*lhs_reshaped.info(), lhs.info()->clone()->set_tensor_shape(compute_lhs_reshaped_shape(*lhs.info(), lhs_info)));
258
259 // Initialise rhs_reshaped tensor info
260 auto_init_if_empty(*rhs_reshaped.info(), rhs.info()->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*rhs.info(), rhs_info)));
261
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100262 if(rhs_info.export_to_cl_image)
263 {
264 arm_compute::cl_gemm::update_padding_for_cl_image(rhs_reshaped.info());
265 }
266
Eren Kopuz15205d92020-07-17 15:13:39 +0100267 // Validate argments
268 Status status{};
269 status = reshape_lhs.validate((&lhs)->info(), (&lhs_reshaped)->info(), lhs_info, kernel_info.reinterpret_input_as_3d);
270 if(!status)
271 {
272 // Unsupported arguments
273 std::cerr << "Unsupported arguments." << std::endl;
274 std::cerr << "Check documentation for supported/unsupported combinations" << std::endl;
275 return false;
276 }
277
278 status = gemm.validate((&lhs_reshaped)->info(), (&rhs_reshaped)->info(), (&bias)->info(), (&dst)->info(), alpha, beta, lhs_info, rhs_info, kernel_info);
279 if(!status)
280 {
281 // Unsupported arguments
282 std::cerr << "Unsupported arguments." << std::endl;
283 std::cerr << "Check documentation for supported/unsupported combinations" << std::endl;
284 return false;
285 }
286
SiCong Lie36b5262019-10-01 19:26:00 +0100287 // Configure reshape lhs function
288 reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info);
289
SiCong Libc166d52019-09-26 14:58:53 +0100290 // Configure function
291 gemm.configure(&lhs_reshaped, &rhs_reshaped, &bias, &dst, alpha, beta, lhs_info, rhs_info, kernel_info);
292
293 // Allocate tensors
294 lhs.allocator()->allocate();
295 rhs.allocator()->allocate();
296 lhs_reshaped.allocator()->allocate();
297 rhs_reshaped.allocator()->allocate();
298 bias.allocator()->allocate();
299 dst.allocator()->allocate();
300
301 return true;
302 }
303 void do_run() override
304 {
305 // Execute the function
SiCong Lie36b5262019-10-01 19:26:00 +0100306 reshape_lhs.run();
SiCong Libc166d52019-09-26 14:58:53 +0100307 gemm.run();
308
309 // Make sure all the OpenCL jobs are done executing:
310 CLScheduler::get().sync();
311 }
312
313 void do_teardown() override
314 {
315 }
316
317private:
318 CLTensor lhs{};
319 CLTensor rhs{};
320 CLTensor lhs_reshaped{};
321 CLTensor rhs_reshaped{};
322 CLTensor bias{};
323 CLTensor dst{};
324 CLTuner tuner{};
SiCong Lie36b5262019-10-01 19:26:00 +0100325 CLGEMMReshapeLHSMatrix reshape_lhs{};
SiCong Libc166d52019-09-26 14:58:53 +0100326 CLGEMMMatrixMultiplyReshaped gemm{};
327};
328
329/** Main program for gemm reshaped test
330 *
331 * @param[in] argc Number of arguments
332 * @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 )
333 */
334int main(int argc, char **argv)
335{
336 return run_example<CLGEMMMatrixMultiplyReshapedExample>(argc, argv);
337}