blob: 10fd2984cf72ebfbc918c806d1befdad940f25b2 [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"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010036#include "src/core/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;
45using namespace utils;
46using namespace arm_compute::misc::shape_calculator;
SiCong Li240b79d2019-09-24 15:50:34 +010047using namespace gemm_tuner;
SiCong Li8b4c7302019-09-19 12:18:15 +010048
49namespace
50{
SiCong Li8b4c7302019-09-19 12:18:15 +010051/** Structure holding all tunable gemm configs specific to this example/strategy */
52struct GemmConfigs
53{
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +010054 size_t m0{ 4 }; /**< Number of rows processed by the matrix multiplication */
55 size_t n0{ 4 }; /**< Number of columns processed by the matrix multiplication */
56 size_t k0{ 4 }; /**< Number of partial accumulations performed by the matrix multiplication */
57 size_t h0{ 1 }; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row */
58 bool interleave_rhs{ true }; /**< Interleave rhs matrix */
59 bool transpose_rhs{ true }; /**< Transpose rhs matrix */
60 bool export_to_cl_image_rhs{ true }; /**< Export rhs matrix to cl_image.*/
SiCong Li8b4c7302019-09-19 12:18:15 +010061};
62
63/** Formatted output of the GemmConfigs type
64 *
65 * @param[out] os Output stream.
66 * @param[in] configs Tunable configurations to output
67 *
68 * @return Modified output stream.
69 */
70::std::ostream &operator<<(::std::ostream &os, const GemmConfigs &configs)
71{
72 std::string false_str = std::string("false");
73 std::string true_str = std::string("true");
74
75 os << "m0 : " << configs.m0 << std::endl;
76 os << "n0 : " << configs.n0 << std::endl;
77 os << "k0 : " << configs.k0 << std::endl;
78 os << "h0 : " << configs.h0 << std::endl;
79 os << "interleave_rhs : " << (configs.interleave_rhs ? true_str : false_str) << std::endl;
80 os << "transpose_rhs : " << (configs.transpose_rhs ? true_str : false_str) << std::endl;
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +010081 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 +010082 return os;
83}
SiCong Li240b79d2019-09-24 15:50:34 +010084
85/** Command line options for gemm configs */
86class GemmConfigOptions
87{
88public:
89 /** Constructor
90 *
91 * @param[in,out] parser A parser on which "parse()" hasn't been called yet.
92 */
93 GemmConfigOptions(CommandLineParser &parser)
94 : m0(parser.add_positional_option<SimpleOption<size_t>>("m0", 4)),
95 n0(parser.add_positional_option<SimpleOption<size_t>>("n0", 4)),
96 k0(parser.add_positional_option<SimpleOption<size_t>>("k0", 4)),
97 h0(parser.add_positional_option<SimpleOption<size_t>>("h0", 1)),
98 interleave_rhs(parser.add_positional_option<SimpleOption<size_t>>("interleave_rhs", 1)),
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +010099 transpose_rhs(parser.add_positional_option<SimpleOption<size_t>>("transpose_rhs", 1)),
100 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 +0100101 {
102 m0->set_help("Number of rows processed by the matrix multiplication");
103 n0->set_help("Number of columns processed by the matrix multiplication");
104 k0->set_help("Number of partial accumulations performed by the matrix multiplication");
105 h0->set_help("Number of horizontal blocks of size (k0xn0) stored on the same output row");
106 interleave_rhs->set_help("Interleave rhs matrix (1) / Do not interleave rhs matrix (0)");
107 transpose_rhs->set_help("Transpose rhs matrix (1) / Do not transpose rhs matrix (0)");
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100108 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 +0100109 }
110 /** Prevent instances of this class from being copied (As this class contains pointers) */
111 GemmConfigOptions(const GemmConfigOptions &) = delete;
112 /** Prevent instances of this class from being copied (As this class contains pointers) */
113 GemmConfigOptions &operator=(const GemmConfigOptions &) = delete;
114 /** Allow instances of this class to be moved */
115 GemmConfigOptions(GemmConfigOptions &&) = default;
116 /** Allow instances of this class to be moved */
117 GemmConfigOptions &operator=(GemmConfigOptions &&) = default;
118 /** Default destructor */
119 ~GemmConfigOptions() = default;
120
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100121 SimpleOption<size_t> *m0; /**< Number of rows processed by the matrix multiplication option */
122 SimpleOption<size_t> *n0; /**< Number of columns processed by the matrix multiplication option */
123 SimpleOption<size_t> *k0; /**< Number of partial accumulations performed by the matrix multiplication option */
124 SimpleOption<size_t> *h0; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row option */
125 SimpleOption<size_t> *interleave_rhs; /**< Interleave rhs matrix option (1 enable; 0 disable) */
126 SimpleOption<size_t> *transpose_rhs; /**< Transpose rhs matrix option (1 enable; 0 disable) */
127 SimpleOption<size_t> *export_to_cl_image_rhs; /**< Export rhs matrix to cl_image.*/
SiCong Li240b79d2019-09-24 15:50:34 +0100128};
129
130/** Consumes the gemm configuration options and creates a structure containing all information
131 *
132 * @param[in] options Options to consume
133 *
134 * @return Structure containing the gemm configurations
135 */
136GemmConfigs consume_gemm_configs(const GemmConfigOptions &options)
137{
138 GemmConfigs configs;
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100139 configs.m0 = options.m0->value();
140 configs.n0 = options.n0->value();
141 configs.k0 = options.k0->value();
142 configs.h0 = options.h0->value();
143 configs.interleave_rhs = options.interleave_rhs->value() != 0;
144 configs.transpose_rhs = options.transpose_rhs->value() != 0;
145 configs.export_to_cl_image_rhs = options.export_to_cl_image_rhs->value() != 0;
SiCong Li240b79d2019-09-24 15:50:34 +0100146 return configs;
147}
148
SiCong Li8b4c7302019-09-19 12:18:15 +0100149} // namespace
150// Create function for CLGEMMMatrixMultiplyReshapedOnlyRHSKernel
151using CLGEMMMatrixMultiplyReshapedOnlyRHS = test::CLSynthetizeFunction<CLGEMMMatrixMultiplyReshapedOnlyRHSKernel>;
152
153class CLGEMMMatrixMultiplyReshapedOnlyRHSExample : public Example
154{
155public:
156 bool do_setup(int argc, char **argv) override
157 {
158 // Default parameters
Eren Kopuz350099e2020-06-09 15:37:43 +0100159 const float alpha = 1.0f;
160 const float beta = 0.0f;
161 const ActivationLayerInfo act_info = ActivationLayerInfo();
SiCong Li8b4c7302019-09-19 12:18:15 +0100162 CommonGemmExampleParams params;
163 GemmConfigs configs;
SiCong Li240b79d2019-09-24 15:50:34 +0100164
165 // Set up command line parser and options
166 CommandLineParser parser;
167 CommonGemmExampleOptions param_options(parser);
168 GemmConfigOptions config_options(parser);
169
170 // Parse command line options
171 parser.parse(argc, argv);
172 if(param_options.help->is_set() && param_options.help->value())
SiCong Li8b4c7302019-09-19 12:18:15 +0100173 {
SiCong Li240b79d2019-09-24 15:50:34 +0100174 // Print help message
175 parser.print_help(argv[0]);
176 return false;
177 }
178 if(!parser.validate())
179 {
180 // Invalid arguments. Use default parameters and configs
181 std::cerr << "Invalid arguments." << std::endl;
182 parser.print_help(argv[0]);
SiCong Li8b4c7302019-09-19 12:18:15 +0100183 std::cerr << "Falling back to default parameters and configs" << std::endl;
184 }
185 else
186 {
SiCong Li240b79d2019-09-24 15:50:34 +0100187 // Get parameters and configs from command-line options
188 params = consume_common_gemm_example_parameters(param_options);
189 configs = consume_gemm_configs(config_options);
SiCong Li8b4c7302019-09-19 12:18:15 +0100190 }
SiCong Li240b79d2019-09-24 15:50:34 +0100191
192 // Print gemm parameters and configurations
Eren Kopuz15205d92020-07-17 15:13:39 +0100193 std::cout << "Gemm parameters:" << std::endl;
194 std::cout << params << std::endl;
195 std::cout << "Gemm configurations:" << std::endl;
196 std::cout << configs << std::endl;
SiCong Li8b4c7302019-09-19 12:18:15 +0100197
198 CLScheduler::get().default_init(&tuner);
199
Eren Kopuz350099e2020-06-09 15:37:43 +0100200 lhs.allocator()->init(TensorInfo(TensorShape(params.K, params.M, params.B), 1, params.data_type));
201 rhs.allocator()->init(TensorInfo(TensorShape(params.N, params.K, params.B), 1, params.data_type));
202 bias.allocator()->init(TensorInfo(TensorShape(params.N, 1, params.B), 1, params.data_type));
SiCong Li8b4c7302019-09-19 12:18:15 +0100203
204 GEMMLHSMatrixInfo lhs_info;
205 lhs_info.m0 = configs.m0;
206 lhs_info.k0 = configs.k0;
207
208 GEMMRHSMatrixInfo rhs_info;
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100209 rhs_info.n0 = configs.n0;
210 rhs_info.k0 = configs.k0;
211 rhs_info.h0 = configs.h0;
212 rhs_info.interleave = configs.interleave_rhs;
213 rhs_info.transpose = configs.transpose_rhs;
214 rhs_info.export_to_cl_image = configs.export_to_cl_image_rhs;
SiCong Li8b4c7302019-09-19 12:18:15 +0100215
216 GEMMKernelInfo kernel_info;
217 kernel_info.m = params.M;
218 kernel_info.n = params.N;
219 kernel_info.k = params.K;
220 kernel_info.depth_output_gemm3d = 0;
221 kernel_info.reinterpret_input_as_3d = false;
222 kernel_info.broadcast_bias = true;
223 kernel_info.activation_info = act_info;
224
225 // Initialise rhs_reshaped tensor info
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +0100226 rhs_reshaped.allocator()->init(TensorInfo(compute_rhs_reshaped_shape(*rhs.info(), rhs_info), 1, params.data_type));
SiCong Li8b4c7302019-09-19 12:18:15 +0100227
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100228 if(rhs_info.export_to_cl_image)
229 {
Manuel Bottini7b427862021-02-08 13:45:19 +0000230 if(!examples::gemm_tuner_helpers::update_padding_for_cl_image(rhs_reshaped.info()))
231 {
232 std::cerr << "cl_image is not supported on the device, disable export_to_cl_image" << std::endl;
233 return false;
234 }
Manuel Bottinic8e6e2c2020-07-02 11:27:27 +0100235 }
Eren Kopuz15205d92020-07-17 15:13:39 +0100236
237 // Validate argments
238 Status status{};
239 status = gemm.validate((&lhs)->info(), (&rhs_reshaped)->info(), (&bias)->info(), (&dst)->info(), alpha, beta, lhs_info, rhs_info, kernel_info);
240 if(!status)
241 {
242 // Unsupported arguments
243 std::cerr << "Unsupported arguments." << std::endl;
244 std::cerr << "Check documentation for supported/unsupported combinations" << std::endl;
245 return false;
246 }
247
SiCong Li8b4c7302019-09-19 12:18:15 +0100248 // Configure function
249 gemm.configure(&lhs, &rhs_reshaped, &bias, &dst, alpha, beta, lhs_info, rhs_info, kernel_info);
250
251 // Allocate tensors
252 lhs.allocator()->allocate();
253 rhs.allocator()->allocate();
254 rhs_reshaped.allocator()->allocate();
255 bias.allocator()->allocate();
256 dst.allocator()->allocate();
257
258 return true;
259 }
260 void do_run() override
261 {
262 // Execute the function
263 gemm.run();
264
265 // Make sure all the OpenCL jobs are done executing:
266 CLScheduler::get().sync();
267 }
268
269 void do_teardown() override
270 {
271 }
272
273private:
274 CLTensor lhs{};
275 CLTensor rhs{};
276 CLTensor rhs_reshaped{};
277 CLTensor bias{};
278 CLTensor dst{};
279 CLTuner tuner{};
280 CLGEMMMatrixMultiplyReshapedOnlyRHS gemm{};
281};
282
283/** Main program for gemm reshaped rhs only test
284 *
285 * @param[in] argc Number of arguments
SiCongLi282f3242020-11-24 15:24:16 +0000286 * @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 +0100287 */
288int main(int argc, char **argv)
289{
SiCong Li240b79d2019-09-24 15:50:34 +0100290 return run_example<CLGEMMMatrixMultiplyReshapedOnlyRHSExample>(argc, argv);
SiCong Li8b4c7302019-09-19 12:18:15 +0100291}