blob: e579ed762c5d65c84b9722585a01f35be14152f6 [file] [log] [blame]
SiCong Libc166d52019-09-26 14:58:53 +01001/*
2 * Copyright (c) 2019 ARM Limited.
3 *
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
180 const DataType data_type = DataType::F32;
181 const float alpha = 1.0f;
182 const float beta = 0.0f;
183 const ActivationLayerInfo act_info = ActivationLayerInfo();
184 CommonGemmExampleParams params;
185 GemmConfigs configs;
186
187 // Set up command line parser and options
188 CommandLineParser parser;
189 CommonGemmExampleOptions param_options(parser);
190 GemmConfigOptions config_options(parser);
191
192 // Parse command line options
193 parser.parse(argc, argv);
194 if(param_options.help->is_set() && param_options.help->value())
195 {
196 // Print help message
197 parser.print_help(argv[0]);
198 return false;
199 }
200 if(!parser.validate())
201 {
202 // Invalid arguments. Use default parameters and configs
203 std::cerr << "Invalid arguments." << std::endl;
204 parser.print_help(argv[0]);
205 std::cerr << "Falling back to default parameters and configs" << std::endl;
206 }
207 else
208 {
209 // Get parameters and configs from command-line options
210 params = consume_common_gemm_example_parameters(param_options);
211 configs = consume_gemm_configs(config_options);
212 }
213
214 // Print gemm parameters and configurations
215 std::cerr << "Gemm parameters:" << std::endl;
216 std::cerr << params << std::endl;
217 std::cerr << "Gemm configurations:" << std::endl;
218 std::cerr << configs << std::endl;
219
220 CLScheduler::get().default_init(&tuner);
221
222 lhs.allocator()->init(TensorInfo(TensorShape(params.K, params.M, params.B), 1, data_type));
223 rhs.allocator()->init(TensorInfo(TensorShape(params.N, params.K, params.B), 1, data_type));
224 bias.allocator()->init(TensorInfo(TensorShape(params.N, 1, params.B), 1, data_type));
225
226 GEMMLHSMatrixInfo lhs_info;
227 lhs_info.m0 = configs.m0;
228 lhs_info.k0 = configs.k0;
229 lhs_info.v0 = configs.v0;
230 lhs_info.interleave = configs.interleave_lhs;
231 lhs_info.transpose = configs.transpose_lhs;
232
233 GEMMRHSMatrixInfo rhs_info;
234 rhs_info.n0 = configs.n0;
235 rhs_info.k0 = configs.k0;
236 rhs_info.h0 = configs.h0;
237 rhs_info.interleave = configs.interleave_rhs;
238 rhs_info.transpose = configs.transpose_rhs;
239
240 GEMMKernelInfo kernel_info;
241 kernel_info.m = params.M;
242 kernel_info.n = params.N;
243 kernel_info.k = params.K;
244 kernel_info.depth_output_gemm3d = 0;
245 kernel_info.reinterpret_input_as_3d = false;
246 kernel_info.broadcast_bias = true;
247 kernel_info.activation_info = act_info;
248
249 // Initialise lhs_reshaped tensor info
250 auto_init_if_empty(*lhs_reshaped.info(), lhs.info()->clone()->set_tensor_shape(compute_lhs_reshaped_shape(*lhs.info(), lhs_info)));
251
252 // Initialise rhs_reshaped tensor info
253 auto_init_if_empty(*rhs_reshaped.info(), rhs.info()->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*rhs.info(), rhs_info)));
254
SiCong Lie36b5262019-10-01 19:26:00 +0100255 // Configure reshape lhs function
256 reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info);
257
SiCong Libc166d52019-09-26 14:58:53 +0100258 // Configure function
259 gemm.configure(&lhs_reshaped, &rhs_reshaped, &bias, &dst, alpha, beta, lhs_info, rhs_info, kernel_info);
260
261 // Allocate tensors
262 lhs.allocator()->allocate();
263 rhs.allocator()->allocate();
264 lhs_reshaped.allocator()->allocate();
265 rhs_reshaped.allocator()->allocate();
266 bias.allocator()->allocate();
267 dst.allocator()->allocate();
268
269 return true;
270 }
271 void do_run() override
272 {
273 // Execute the function
SiCong Lie36b5262019-10-01 19:26:00 +0100274 reshape_lhs.run();
SiCong Libc166d52019-09-26 14:58:53 +0100275 gemm.run();
276
277 // Make sure all the OpenCL jobs are done executing:
278 CLScheduler::get().sync();
279 }
280
281 void do_teardown() override
282 {
283 }
284
285private:
286 CLTensor lhs{};
287 CLTensor rhs{};
288 CLTensor lhs_reshaped{};
289 CLTensor rhs_reshaped{};
290 CLTensor bias{};
291 CLTensor dst{};
292 CLTuner tuner{};
SiCong Lie36b5262019-10-01 19:26:00 +0100293 CLGEMMReshapeLHSMatrix reshape_lhs{};
SiCong Libc166d52019-09-26 14:58:53 +0100294 CLGEMMMatrixMultiplyReshaped gemm{};
295};
296
297/** Main program for gemm reshaped test
298 *
299 * @param[in] argc Number of arguments
300 * @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 )
301 */
302int main(int argc, char **argv)
303{
304 return run_example<CLGEMMMatrixMultiplyReshapedExample>(argc, argv);
305}