blob: dd038739217532e5141b2bcbd86fa74e6092a6f2 [file] [log] [blame]
SiCong Liea803482019-09-26 16:55:49 +01001/*
Gian Marco Iodiceca419dd2021-03-03 17:25:07 +00002 * Copyright (c) 2019-2021 Arm Limited.
SiCong Liea803482019-09-26 16:55:49 +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"
SiCong Liea803482019-09-26 16:55:49 +010029#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/KernelDescriptors.h"
31#include "arm_compute/core/Types.h"
32#include "arm_compute/core/utils/misc/ShapeCalculator.h"
SiCong Liea803482019-09-26 16:55:49 +010033#include "arm_compute/runtime/CL/CLScheduler.h"
34#include "arm_compute/runtime/CL/CLTuner.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010035#include "src/gpu/cl/kernels/ClGemmMatrixMultiplyNativeKernel.h"
SiCong Liea803482019-09-26 16:55:49 +010036#include "tests/CL/Helper.h"
37#include "utils/Utils.h"
38#include "utils/command_line/CommandLineOptions.h"
39#include "utils/command_line/CommandLineParser.h"
40
41#include <cstdlib>
42
43using namespace arm_compute;
Georgios Pinitas856f66e2021-04-22 21:13:21 +010044using namespace arm_compute::opencl::kernels;
SiCong Liea803482019-09-26 16:55:49 +010045using namespace utils;
46using namespace arm_compute::misc::shape_calculator;
47using namespace gemm_tuner;
48
49namespace
50{
51/** Structure holding all tunable gemm configs specific to this example/strategy */
52struct GemmConfigs
53{
54 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};
58
59/** Formatted output of the GemmConfigs type
60 *
61 * @param[out] os Output stream.
62 * @param[in] configs Tunable configurations to output
63 *
64 * @return Modified output stream.
65 */
66::std::ostream &operator<<(::std::ostream &os, const GemmConfigs &configs)
67{
68 std::string false_str = std::string("false");
69 std::string true_str = std::string("true");
70
71 os << "m0 : " << configs.m0 << std::endl;
72 os << "n0 : " << configs.n0 << std::endl;
73 os << "k0 : " << configs.k0 << std::endl;
74 return os;
75}
76
77/** Command line options for gemm configs */
78class GemmConfigOptions
79{
80public:
81 /** Constructor
82 *
83 * @param[in,out] parser A parser on which "parse()" hasn't been called yet.
84 */
85 GemmConfigOptions(CommandLineParser &parser)
86 : m0(parser.add_positional_option<SimpleOption<size_t>>("m0", 4)),
87 n0(parser.add_positional_option<SimpleOption<size_t>>("n0", 4)),
88 k0(parser.add_positional_option<SimpleOption<size_t>>("k0", 4))
89 {
90 m0->set_help("Number of rows processed by the matrix multiplication");
91 n0->set_help("Number of columns processed by the matrix multiplication");
92 k0->set_help("Number of partial accumulations performed by the matrix multiplication");
93 }
94 /** Prevent instances of this class from being copied (As this class contains pointers) */
95 GemmConfigOptions(const GemmConfigOptions &) = delete;
96 /** Prevent instances of this class from being copied (As this class contains pointers) */
97 GemmConfigOptions &operator=(const GemmConfigOptions &) = delete;
98 /** Allow instances of this class to be moved */
99 GemmConfigOptions(GemmConfigOptions &&) = default;
100 /** Allow instances of this class to be moved */
101 GemmConfigOptions &operator=(GemmConfigOptions &&) = default;
102 /** Default destructor */
103 ~GemmConfigOptions() = default;
104
105 SimpleOption<size_t> *m0; /**< Number of rows processed by the matrix multiplication option */
106 SimpleOption<size_t> *n0; /**< Number of columns processed by the matrix multiplication option */
107 SimpleOption<size_t> *k0; /**< Number of partial accumulations performed by the matrix multiplication option */
108};
109
110/** Consumes the gemm configuration options and creates a structure containing all information
111 *
112 * @param[in] options Options to consume
113 *
114 * @return Structure containing the gemm configurations
115 */
116GemmConfigs consume_gemm_configs(const GemmConfigOptions &options)
117{
118 GemmConfigs configs;
119 configs.m0 = options.m0->value();
120 configs.n0 = options.n0->value();
121 configs.k0 = options.k0->value();
122 return configs;
123}
124
125} // namespace
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100126// Create function for ClGemmMatrixMultiplyNativeKernel
127using CLGEMMMatrixMultiplyNative = test::CLSynthetizeOperator<ClGemmMatrixMultiplyNativeKernel>;
SiCong Liea803482019-09-26 16:55:49 +0100128
129class CLGEMMMatrixMultiplyNativeExample : public Example
130{
131public:
132 bool do_setup(int argc, char **argv) override
133 {
134 // Default parameters
Eren Kopuz350099e2020-06-09 15:37:43 +0100135 const float alpha = 1.0f;
136 const float beta = 0.0f;
137 const ActivationLayerInfo act_info = ActivationLayerInfo();
SiCong Liea803482019-09-26 16:55:49 +0100138 CommonGemmExampleParams params;
139 GemmConfigs configs;
140
141 // Set up command line parser and options
142 CommandLineParser parser;
143 CommonGemmExampleOptions param_options(parser);
144 GemmConfigOptions config_options(parser);
145
146 // Parse command line options
147 parser.parse(argc, argv);
148 if(param_options.help->is_set() && param_options.help->value())
149 {
150 // Print help message
151 parser.print_help(argv[0]);
152 return false;
153 }
154 if(!parser.validate())
155 {
156 // Invalid arguments. Use default parameters and configs
157 std::cerr << "Invalid arguments." << std::endl;
158 parser.print_help(argv[0]);
159 std::cerr << "Falling back to default parameters and configs" << std::endl;
160 }
161 else
162 {
163 // Get parameters and configs from command-line options
164 params = consume_common_gemm_example_parameters(param_options);
165 configs = consume_gemm_configs(config_options);
166 }
167
168 // Print gemm parameters and configurations
Eren Kopuz15205d92020-07-17 15:13:39 +0100169 std::cout << "Gemm parameters:" << std::endl;
170 std::cout << params << std::endl;
171 std::cout << "Gemm configurations:" << std::endl;
172 std::cout << configs << std::endl;
SiCong Liea803482019-09-26 16:55:49 +0100173
Gian Marco Iodiceca419dd2021-03-03 17:25:07 +0000174 tuner.set_tuner_mode(params.tuner_mode);
175
SiCong Liea803482019-09-26 16:55:49 +0100176 CLScheduler::get().default_init(&tuner);
177
Eren Kopuz350099e2020-06-09 15:37:43 +0100178 lhs.allocator()->init(TensorInfo(TensorShape(params.K, params.M, params.B), 1, params.data_type));
179 rhs.allocator()->init(TensorInfo(TensorShape(params.N, params.K, params.B), 1, params.data_type));
180 bias.allocator()->init(TensorInfo(TensorShape(params.N, 1, params.B), 1, params.data_type));
SiCong Liea803482019-09-26 16:55:49 +0100181
182 GEMMLHSMatrixInfo lhs_info;
183 lhs_info.m0 = configs.m0;
184 lhs_info.k0 = configs.k0;
185
186 GEMMRHSMatrixInfo rhs_info;
187 rhs_info.n0 = configs.n0;
188 rhs_info.k0 = configs.k0;
189
190 GEMMKernelInfo kernel_info;
191 kernel_info.m = params.M;
192 kernel_info.n = params.N;
193 kernel_info.k = params.K;
194 kernel_info.depth_output_gemm3d = 0;
195 kernel_info.reinterpret_input_as_3d = false;
196 kernel_info.broadcast_bias = true;
197 kernel_info.activation_info = act_info;
198
Eren Kopuz15205d92020-07-17 15:13:39 +0100199 // Validate argments
200 Status status{};
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100201 status = gemm.validate(lhs.info(), rhs.info(), bias.info(), dst.info(), alpha, beta, lhs_info, rhs_info, kernel_info);
Eren Kopuz15205d92020-07-17 15:13:39 +0100202 if(!status)
203 {
204 // Unsupported arguments
205 std::cerr << "Unsupported arguments." << std::endl;
206 std::cerr << "Check documentation for supported/unsupported combinations" << std::endl;
207 return false;
208 }
209
SiCong Liea803482019-09-26 16:55:49 +0100210 // Configure function
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100211 gemm.configure(lhs.info(), rhs.info(), bias.info(), dst.info(), alpha, beta, lhs_info, rhs_info, kernel_info);
SiCong Liea803482019-09-26 16:55:49 +0100212
213 // Allocate tensors
214 lhs.allocator()->allocate();
215 rhs.allocator()->allocate();
216 bias.allocator()->allocate();
217 dst.allocator()->allocate();
218
219 return true;
220 }
221 void do_run() override
222 {
223 // Execute the function
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100224 ITensorPack gemm_pack({ { ACL_SRC_0, &lhs },
225 { ACL_SRC_1, &rhs },
226 { ACL_SRC_2, &bias },
227 { ACL_DST, &dst }
228 });
229 gemm.run(gemm_pack);
SiCong Liea803482019-09-26 16:55:49 +0100230
231 // Make sure all the OpenCL jobs are done executing:
232 CLScheduler::get().sync();
233 }
234
235 void do_teardown() override
236 {
237 }
238
239private:
240 CLTensor lhs{};
241 CLTensor rhs{};
242 CLTensor bias{};
243 CLTensor dst{};
244 CLTuner tuner{};
245 CLGEMMMatrixMultiplyNative gemm{};
246};
247
248/** Main program for gemm native test
249 *
250 * @param[in] argc Number of arguments
251 * @param[in] argv Arguments ( [optional] M, [optional] N, [optional] K, [optional] B, [optional] m0, [optional] n0, [optional] k0 )
252 */
253int main(int argc, char **argv)
254{
255 return run_example<CLGEMMMatrixMultiplyNativeExample>(argc, argv);
256}