blob: da72dfd58d9f0b1e116c471a7d803ae0d7d11abd [file] [log] [blame]
SiCong Libc166d52019-09-26 14:58:53 +01001/*
Eren Kopuz350099e2020-06-09 15:37:43 +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"
29#include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyReshapedKernel.h"
SiCong Lie36b5262019-10-01 19:26:00 +010030#include "arm_compute/core/CL/kernels/CLGEMMReshapeLHSMatrixKernel.h"
SiCong Libc166d52019-09-26 14:58:53 +010031#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/KernelDescriptors.h"
33#include "arm_compute/core/Types.h"
34#include "arm_compute/core/utils/misc/ShapeCalculator.h"
35#include "arm_compute/runtime/CL/CLFunctions.h"
36#include "arm_compute/runtime/CL/CLScheduler.h"
37#include "arm_compute/runtime/CL/CLTuner.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
SiCong Lie36b5262019-10-01 19:26:00 +0100169// Create function for CLGEMMReshapeLHSMatrixKernel
170using CLGEMMReshapeLHSMatrix = test::CLSynthetizeFunction<CLGEMMReshapeLHSMatrixKernel>;
SiCong Libc166d52019-09-26 14:58:53 +0100171// Create function for CLGEMMMatrixMultiplyReshapedKernel
172using CLGEMMMatrixMultiplyReshaped = test::CLSynthetizeFunction<CLGEMMMatrixMultiplyReshapedKernel>;
173
174class CLGEMMMatrixMultiplyReshapedExample : public Example
175{
176public:
177 bool do_setup(int argc, char **argv) override
178 {
179 // Default parameters
Eren Kopuz350099e2020-06-09 15:37:43 +0100180 const float alpha = 1.0f;
181 const float beta = 0.0f;
182 const ActivationLayerInfo act_info = ActivationLayerInfo();
SiCong Libc166d52019-09-26 14:58:53 +0100183 CommonGemmExampleParams params;
184 GemmConfigs configs;
185
186 // Set up command line parser and options
187 CommandLineParser parser;
188 CommonGemmExampleOptions param_options(parser);
189 GemmConfigOptions config_options(parser);
190
191 // Parse command line options
192 parser.parse(argc, argv);
193 if(param_options.help->is_set() && param_options.help->value())
194 {
195 // Print help message
196 parser.print_help(argv[0]);
197 return false;
198 }
199 if(!parser.validate())
200 {
201 // Invalid arguments. Use default parameters and configs
202 std::cerr << "Invalid arguments." << std::endl;
203 parser.print_help(argv[0]);
204 std::cerr << "Falling back to default parameters and configs" << std::endl;
205 }
206 else
207 {
208 // Get parameters and configs from command-line options
209 params = consume_common_gemm_example_parameters(param_options);
210 configs = consume_gemm_configs(config_options);
211 }
212
213 // Print gemm parameters and configurations
214 std::cerr << "Gemm parameters:" << std::endl;
215 std::cerr << params << std::endl;
216 std::cerr << "Gemm configurations:" << std::endl;
217 std::cerr << configs << std::endl;
218
219 CLScheduler::get().default_init(&tuner);
220
Eren Kopuz350099e2020-06-09 15:37:43 +0100221 lhs.allocator()->init(TensorInfo(TensorShape(params.K, params.M, params.B), 1, params.data_type));
222 rhs.allocator()->init(TensorInfo(TensorShape(params.N, params.K, params.B), 1, params.data_type));
223 bias.allocator()->init(TensorInfo(TensorShape(params.N, 1, params.B), 1, params.data_type));
SiCong Libc166d52019-09-26 14:58:53 +0100224
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
239 GEMMKernelInfo kernel_info;
240 kernel_info.m = params.M;
241 kernel_info.n = params.N;
242 kernel_info.k = params.K;
243 kernel_info.depth_output_gemm3d = 0;
244 kernel_info.reinterpret_input_as_3d = false;
245 kernel_info.broadcast_bias = true;
246 kernel_info.activation_info = act_info;
247
248 // Initialise lhs_reshaped tensor info
249 auto_init_if_empty(*lhs_reshaped.info(), lhs.info()->clone()->set_tensor_shape(compute_lhs_reshaped_shape(*lhs.info(), lhs_info)));
250
251 // Initialise rhs_reshaped tensor info
252 auto_init_if_empty(*rhs_reshaped.info(), rhs.info()->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*rhs.info(), rhs_info)));
253
SiCong Lie36b5262019-10-01 19:26:00 +0100254 // Configure reshape lhs function
255 reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info);
256
SiCong Libc166d52019-09-26 14:58:53 +0100257 // Configure function
258 gemm.configure(&lhs_reshaped, &rhs_reshaped, &bias, &dst, alpha, beta, lhs_info, rhs_info, kernel_info);
259
260 // Allocate tensors
261 lhs.allocator()->allocate();
262 rhs.allocator()->allocate();
263 lhs_reshaped.allocator()->allocate();
264 rhs_reshaped.allocator()->allocate();
265 bias.allocator()->allocate();
266 dst.allocator()->allocate();
267
268 return true;
269 }
270 void do_run() override
271 {
272 // Execute the function
SiCong Lie36b5262019-10-01 19:26:00 +0100273 reshape_lhs.run();
SiCong Libc166d52019-09-26 14:58:53 +0100274 gemm.run();
275
276 // Make sure all the OpenCL jobs are done executing:
277 CLScheduler::get().sync();
278 }
279
280 void do_teardown() override
281 {
282 }
283
284private:
285 CLTensor lhs{};
286 CLTensor rhs{};
287 CLTensor lhs_reshaped{};
288 CLTensor rhs_reshaped{};
289 CLTensor bias{};
290 CLTensor dst{};
291 CLTuner tuner{};
SiCong Lie36b5262019-10-01 19:26:00 +0100292 CLGEMMReshapeLHSMatrix reshape_lhs{};
SiCong Libc166d52019-09-26 14:58:53 +0100293 CLGEMMMatrixMultiplyReshaped gemm{};
294};
295
296/** Main program for gemm reshaped test
297 *
298 * @param[in] argc Number of arguments
299 * @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 )
300 */
301int main(int argc, char **argv)
302{
303 return run_example<CLGEMMMatrixMultiplyReshapedExample>(argc, argv);
304}