blob: 79f251ae7d3538a531b69b6662febd7aab9dee35 [file] [log] [blame]
SiCong Li8b4c7302019-09-19 12:18:15 +01001/*
Manuel Bottini7b427862021-02-08 13:45:19 +00002 * Copyright (c) 2019-2021 Arm Limited.
SiCong Li8b4c7302019-09-19 12:18:15 +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
SiCong Li240b79d2019-09-24 15:50:34 +010028#include "CommonGemmExampleOptions.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010029#include "GemmTunerHelpers.h"
SiCong Li8b4c7302019-09-19 12:18:15 +010030#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/KernelDescriptors.h"
32#include "arm_compute/core/Types.h"
33#include "arm_compute/core/utils/misc/ShapeCalculator.h"
SiCong Li8b4c7302019-09-19 12:18:15 +010034#include "arm_compute/runtime/CL/CLScheduler.h"
35#include "arm_compute/runtime/CL/CLTuner.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010036#include "src/gpu/cl/kernels/ClGemmMatrixMultiplyReshapedOnlyRhsKernel.h"
SiCong Li8b4c7302019-09-19 12:18:15 +010037#include "tests/CL/Helper.h"
38#include "utils/Utils.h"
SiCong Li240b79d2019-09-24 15:50:34 +010039#include "utils/command_line/CommandLineOptions.h"
40#include "utils/command_line/CommandLineParser.h"
SiCong Li8b4c7302019-09-19 12:18:15 +010041
42#include <cstdlib>
43
44using namespace arm_compute;
Georgios Pinitas856f66e2021-04-22 21:13:21 +010045using namespace arm_compute::opencl::kernels;
SiCong Li8b4c7302019-09-19 12:18:15 +010046using namespace utils;
47using namespace arm_compute::misc::shape_calculator;
SiCong Li240b79d2019-09-24 15:50:34 +010048using namespace gemm_tuner;
SiCong Li8b4c7302019-09-19 12:18:15 +010049
50namespace
51{
SiCong Li8b4c7302019-09-19 12:18:15 +010052/** Structure holding all tunable gemm configs specific to this example/strategy */
53struct GemmConfigs
54{
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +010055 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 h0{ 1 }; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row */
59 bool interleave_rhs{ true }; /**< Interleave rhs matrix */
60 bool transpose_rhs{ true }; /**< Transpose rhs matrix */
61 bool export_to_cl_image_rhs{ true }; /**< Export rhs matrix to cl_image.*/
SiCong Li8b4c7302019-09-19 12:18:15 +010062};
63
64/** Formatted output of the GemmConfigs type
65 *
66 * @param[out] os Output stream.
67 * @param[in] configs Tunable configurations to output
68 *
69 * @return Modified output stream.
70 */
71::std::ostream &operator<<(::std::ostream &os, const GemmConfigs &configs)
72{
73 std::string false_str = std::string("false");
74 std::string true_str = std::string("true");
75
76 os << "m0 : " << configs.m0 << std::endl;
77 os << "n0 : " << configs.n0 << std::endl;
78 os << "k0 : " << configs.k0 << std::endl;
79 os << "h0 : " << configs.h0 << std::endl;
80 os << "interleave_rhs : " << (configs.interleave_rhs ? true_str : false_str) << std::endl;
81 os << "transpose_rhs : " << (configs.transpose_rhs ? true_str : false_str) << std::endl;
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +010082 os << "export_to_cl_image_rhs : " << (configs.export_to_cl_image_rhs ? true_str : false_str) << std::endl;
SiCong Li8b4c7302019-09-19 12:18:15 +010083 return os;
84}
SiCong Li240b79d2019-09-24 15:50:34 +010085
86/** Command line options for gemm configs */
87class GemmConfigOptions
88{
89public:
90 /** Constructor
91 *
92 * @param[in,out] parser A parser on which "parse()" hasn't been called yet.
93 */
94 GemmConfigOptions(CommandLineParser &parser)
95 : m0(parser.add_positional_option<SimpleOption<size_t>>("m0", 4)),
96 n0(parser.add_positional_option<SimpleOption<size_t>>("n0", 4)),
97 k0(parser.add_positional_option<SimpleOption<size_t>>("k0", 4)),
98 h0(parser.add_positional_option<SimpleOption<size_t>>("h0", 1)),
99 interleave_rhs(parser.add_positional_option<SimpleOption<size_t>>("interleave_rhs", 1)),
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100100 transpose_rhs(parser.add_positional_option<SimpleOption<size_t>>("transpose_rhs", 1)),
101 export_to_cl_image_rhs(parser.add_positional_option<SimpleOption<size_t>>("export_to_cl_image_rhs", 1))
SiCong Li240b79d2019-09-24 15:50:34 +0100102 {
103 m0->set_help("Number of rows processed by the matrix multiplication");
104 n0->set_help("Number of columns processed by the matrix multiplication");
105 k0->set_help("Number of partial accumulations performed by the matrix multiplication");
106 h0->set_help("Number of horizontal blocks of size (k0xn0) stored on the same output row");
107 interleave_rhs->set_help("Interleave rhs matrix (1) / Do not interleave rhs matrix (0)");
108 transpose_rhs->set_help("Transpose rhs matrix (1) / Do not transpose rhs matrix (0)");
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100109 export_to_cl_image_rhs->set_help("Export rhs matrix to cl_image (1) / Do not export rhs matrix to cl_image (0)");
SiCong Li240b79d2019-09-24 15:50:34 +0100110 }
111 /** Prevent instances of this class from being copied (As this class contains pointers) */
112 GemmConfigOptions(const GemmConfigOptions &) = delete;
113 /** Prevent instances of this class from being copied (As this class contains pointers) */
114 GemmConfigOptions &operator=(const GemmConfigOptions &) = delete;
115 /** Allow instances of this class to be moved */
116 GemmConfigOptions(GemmConfigOptions &&) = default;
117 /** Allow instances of this class to be moved */
118 GemmConfigOptions &operator=(GemmConfigOptions &&) = default;
119 /** Default destructor */
120 ~GemmConfigOptions() = default;
121
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100122 SimpleOption<size_t> *m0; /**< Number of rows processed by the matrix multiplication option */
123 SimpleOption<size_t> *n0; /**< Number of columns processed by the matrix multiplication option */
124 SimpleOption<size_t> *k0; /**< Number of partial accumulations performed by the matrix multiplication option */
125 SimpleOption<size_t> *h0; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row option */
126 SimpleOption<size_t> *interleave_rhs; /**< Interleave rhs matrix option (1 enable; 0 disable) */
127 SimpleOption<size_t> *transpose_rhs; /**< Transpose rhs matrix option (1 enable; 0 disable) */
128 SimpleOption<size_t> *export_to_cl_image_rhs; /**< Export rhs matrix to cl_image.*/
SiCong Li240b79d2019-09-24 15:50:34 +0100129};
130
131/** Consumes the gemm configuration options and creates a structure containing all information
132 *
133 * @param[in] options Options to consume
134 *
135 * @return Structure containing the gemm configurations
136 */
137GemmConfigs consume_gemm_configs(const GemmConfigOptions &options)
138{
139 GemmConfigs configs;
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100140 configs.m0 = options.m0->value();
141 configs.n0 = options.n0->value();
142 configs.k0 = options.k0->value();
143 configs.h0 = options.h0->value();
144 configs.interleave_rhs = options.interleave_rhs->value() != 0;
145 configs.transpose_rhs = options.transpose_rhs->value() != 0;
146 configs.export_to_cl_image_rhs = options.export_to_cl_image_rhs->value() != 0;
SiCong Li240b79d2019-09-24 15:50:34 +0100147 return configs;
148}
149
SiCong Li8b4c7302019-09-19 12:18:15 +0100150} // namespace
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100151// Create function for ClGemmMatrixMultiplyReshapedOnlyRhsKernel
152using CLGEMMMatrixMultiplyReshapedOnlyRHS = test::CLSynthetizeOperator<ClGemmMatrixMultiplyReshapedOnlyRhsKernel>;
SiCong Li8b4c7302019-09-19 12:18:15 +0100153
154class CLGEMMMatrixMultiplyReshapedOnlyRHSExample : public Example
155{
156public:
157 bool do_setup(int argc, char **argv) override
158 {
159 // Default parameters
Eren Kopuz350099e2020-06-09 15:37:43 +0100160 const float alpha = 1.0f;
161 const float beta = 0.0f;
162 const ActivationLayerInfo act_info = ActivationLayerInfo();
SiCong Li8b4c7302019-09-19 12:18:15 +0100163 CommonGemmExampleParams params;
164 GemmConfigs configs;
SiCong Li240b79d2019-09-24 15:50:34 +0100165
166 // Set up command line parser and options
167 CommandLineParser parser;
168 CommonGemmExampleOptions param_options(parser);
169 GemmConfigOptions config_options(parser);
170
171 // Parse command line options
172 parser.parse(argc, argv);
173 if(param_options.help->is_set() && param_options.help->value())
SiCong Li8b4c7302019-09-19 12:18:15 +0100174 {
SiCong Li240b79d2019-09-24 15:50:34 +0100175 // Print help message
176 parser.print_help(argv[0]);
177 return false;
178 }
179 if(!parser.validate())
180 {
181 // Invalid arguments. Use default parameters and configs
182 std::cerr << "Invalid arguments." << std::endl;
183 parser.print_help(argv[0]);
SiCong Li8b4c7302019-09-19 12:18:15 +0100184 std::cerr << "Falling back to default parameters and configs" << std::endl;
185 }
186 else
187 {
SiCong Li240b79d2019-09-24 15:50:34 +0100188 // Get parameters and configs from command-line options
189 params = consume_common_gemm_example_parameters(param_options);
190 configs = consume_gemm_configs(config_options);
SiCong Li8b4c7302019-09-19 12:18:15 +0100191 }
SiCong Li240b79d2019-09-24 15:50:34 +0100192
193 // Print gemm parameters and configurations
Eren Kopuz15205d92020-07-17 15:13:39 +0100194 std::cout << "Gemm parameters:" << std::endl;
195 std::cout << params << std::endl;
196 std::cout << "Gemm configurations:" << std::endl;
197 std::cout << configs << std::endl;
SiCong Li8b4c7302019-09-19 12:18:15 +0100198
Gian Marco Iodiceca419dd2021-03-03 17:25:07 +0000199 tuner.set_tuner_mode(params.tuner_mode);
200
SiCong Li8b4c7302019-09-19 12:18:15 +0100201 CLScheduler::get().default_init(&tuner);
202
Eren Kopuz350099e2020-06-09 15:37:43 +0100203 lhs.allocator()->init(TensorInfo(TensorShape(params.K, params.M, params.B), 1, params.data_type));
204 rhs.allocator()->init(TensorInfo(TensorShape(params.N, params.K, params.B), 1, params.data_type));
205 bias.allocator()->init(TensorInfo(TensorShape(params.N, 1, params.B), 1, params.data_type));
SiCong Li8b4c7302019-09-19 12:18:15 +0100206
207 GEMMLHSMatrixInfo lhs_info;
208 lhs_info.m0 = configs.m0;
209 lhs_info.k0 = configs.k0;
210
211 GEMMRHSMatrixInfo rhs_info;
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100212 rhs_info.n0 = configs.n0;
213 rhs_info.k0 = configs.k0;
214 rhs_info.h0 = configs.h0;
215 rhs_info.interleave = configs.interleave_rhs;
216 rhs_info.transpose = configs.transpose_rhs;
217 rhs_info.export_to_cl_image = configs.export_to_cl_image_rhs;
SiCong Li8b4c7302019-09-19 12:18:15 +0100218
219 GEMMKernelInfo kernel_info;
220 kernel_info.m = params.M;
221 kernel_info.n = params.N;
222 kernel_info.k = params.K;
223 kernel_info.depth_output_gemm3d = 0;
224 kernel_info.reinterpret_input_as_3d = false;
225 kernel_info.broadcast_bias = true;
226 kernel_info.activation_info = act_info;
227
228 // Initialise rhs_reshaped tensor info
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100229 rhs_reshaped.allocator()->init(TensorInfo(compute_rhs_reshaped_shape(*rhs.info(), rhs_info), 1, params.data_type));
SiCong Li8b4c7302019-09-19 12:18:15 +0100230
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100231 if(rhs_info.export_to_cl_image)
232 {
Manuel Bottini7b427862021-02-08 13:45:19 +0000233 if(!examples::gemm_tuner_helpers::update_padding_for_cl_image(rhs_reshaped.info()))
234 {
235 std::cerr << "cl_image is not supported on the device, disable export_to_cl_image" << std::endl;
236 return false;
237 }
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100238 }
Eren Kopuz15205d92020-07-17 15:13:39 +0100239
240 // Validate argments
241 Status status{};
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100242 status = gemm.validate(lhs.info(), rhs_reshaped.info(), bias.info(), dst.info(), alpha, beta, lhs_info, rhs_info, kernel_info);
Eren Kopuz15205d92020-07-17 15:13:39 +0100243 if(!status)
244 {
245 // Unsupported arguments
246 std::cerr << "Unsupported arguments." << std::endl;
247 std::cerr << "Check documentation for supported/unsupported combinations" << std::endl;
248 return false;
249 }
250
SiCong Li8b4c7302019-09-19 12:18:15 +0100251 // Configure function
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100252 gemm.configure(lhs.info(), rhs_reshaped.info(), bias.info(), dst.info(), alpha, beta, lhs_info, rhs_info, kernel_info);
SiCong Li8b4c7302019-09-19 12:18:15 +0100253
254 // Allocate tensors
255 lhs.allocator()->allocate();
256 rhs.allocator()->allocate();
257 rhs_reshaped.allocator()->allocate();
258 bias.allocator()->allocate();
259 dst.allocator()->allocate();
260
261 return true;
262 }
263 void do_run() override
264 {
265 // Execute the function
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100266 ITensorPack gemm_pack({ { ACL_SRC_0, &lhs },
267 { ACL_SRC_1, &rhs_reshaped },
268 { ACL_SRC_2, &bias },
269 { ACL_DST, &dst }
270 });
271 gemm.run(gemm_pack);
SiCong Li8b4c7302019-09-19 12:18:15 +0100272
273 // Make sure all the OpenCL jobs are done executing:
274 CLScheduler::get().sync();
275 }
276
277 void do_teardown() override
278 {
279 }
280
281private:
282 CLTensor lhs{};
283 CLTensor rhs{};
284 CLTensor rhs_reshaped{};
285 CLTensor bias{};
286 CLTensor dst{};
287 CLTuner tuner{};
288 CLGEMMMatrixMultiplyReshapedOnlyRHS gemm{};
289};
290
291/** Main program for gemm reshaped rhs only test
292 *
293 * @param[in] argc Number of arguments
SiCongLi282f3242020-11-24 15:24:16 +0000294 * @param[in] argv Arguments ( [optional] M, [optional] N, [optional] K, [optional] B, [optional] m0, [optional] n0, [optional] k0, [optional] h0, [optional] interleave_rhs, [optional] transpose_rhs, [optional] export_to_cl_image)
SiCong Li8b4c7302019-09-19 12:18:15 +0100295 */
296int main(int argc, char **argv)
297{
SiCong Li240b79d2019-09-24 15:50:34 +0100298 return run_example<CLGEMMMatrixMultiplyReshapedOnlyRHSExample>(argc, argv);
SiCong Li8b4c7302019-09-19 12:18:15 +0100299}