blob: 7daa0b07d3c000e567e3a03cc0f6d09397b93acd [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
SiCong Liea803482019-09-26 16:55:49 +010028#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/KernelDescriptors.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/utils/misc/ShapeCalculator.h"
SiCong Liea803482019-09-26 16:55:49 +010032#include "arm_compute/runtime/CL/CLScheduler.h"
33#include "arm_compute/runtime/CL/CLTuner.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010034
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"
SiCong Liea803482019-09-26 16:55:49 +010037#include "utils/command_line/CommandLineOptions.h"
38#include "utils/command_line/CommandLineParser.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010039#include "utils/Utils.h"
SiCong Liea803482019-09-26 16:55:49 +010040
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010041#include "CommonGemmExampleOptions.h"
SiCong Liea803482019-09-26 16:55:49 +010042#include <cstdlib>
43
44using namespace arm_compute;
Georgios Pinitas856f66e2021-04-22 21:13:21 +010045using namespace arm_compute::opencl::kernels;
SiCong Liea803482019-09-26 16:55:49 +010046using 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{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +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 */
SiCong Liea803482019-09-26 16:55:49 +010058};
59
60/** Formatted output of the GemmConfigs type
61 *
62 * @param[out] os Output stream.
63 * @param[in] configs Tunable configurations to output
64 *
65 * @return Modified output stream.
66 */
67::std::ostream &operator<<(::std::ostream &os, const GemmConfigs &configs)
68{
69 std::string false_str = std::string("false");
70 std::string true_str = std::string("true");
71
72 os << "m0 : " << configs.m0 << std::endl;
73 os << "n0 : " << configs.n0 << std::endl;
74 os << "k0 : " << configs.k0 << std::endl;
75 return os;
76}
77
78/** Command line options for gemm configs */
79class GemmConfigOptions
80{
81public:
82 /** Constructor
83 *
84 * @param[in,out] parser A parser on which "parse()" hasn't been called yet.
85 */
86 GemmConfigOptions(CommandLineParser &parser)
87 : m0(parser.add_positional_option<SimpleOption<size_t>>("m0", 4)),
88 n0(parser.add_positional_option<SimpleOption<size_t>>("n0", 4)),
89 k0(parser.add_positional_option<SimpleOption<size_t>>("k0", 4))
90 {
91 m0->set_help("Number of rows processed by the matrix multiplication");
92 n0->set_help("Number of columns processed by the matrix multiplication");
93 k0->set_help("Number of partial accumulations performed by the matrix multiplication");
94 }
95 /** Prevent instances of this class from being copied (As this class contains pointers) */
96 GemmConfigOptions(const GemmConfigOptions &) = delete;
97 /** Prevent instances of this class from being copied (As this class contains pointers) */
98 GemmConfigOptions &operator=(const GemmConfigOptions &) = delete;
99 /** Allow instances of this class to be moved */
100 GemmConfigOptions(GemmConfigOptions &&) = default;
101 /** Allow instances of this class to be moved */
102 GemmConfigOptions &operator=(GemmConfigOptions &&) = default;
103 /** Default destructor */
104 ~GemmConfigOptions() = default;
105
106 SimpleOption<size_t> *m0; /**< Number of rows processed by the matrix multiplication option */
107 SimpleOption<size_t> *n0; /**< Number of columns processed by the matrix multiplication option */
108 SimpleOption<size_t> *k0; /**< Number of partial accumulations performed by the matrix multiplication option */
109};
110
111/** Consumes the gemm configuration options and creates a structure containing all information
112 *
113 * @param[in] options Options to consume
114 *
115 * @return Structure containing the gemm configurations
116 */
117GemmConfigs consume_gemm_configs(const GemmConfigOptions &options)
118{
119 GemmConfigs configs;
120 configs.m0 = options.m0->value();
121 configs.n0 = options.n0->value();
122 configs.k0 = options.k0->value();
123 return configs;
124}
125
126} // namespace
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100127// Create function for ClGemmMatrixMultiplyNativeKernel
128using CLGEMMMatrixMultiplyNative = test::CLSynthetizeOperator<ClGemmMatrixMultiplyNativeKernel>;
SiCong Liea803482019-09-26 16:55:49 +0100129
130class CLGEMMMatrixMultiplyNativeExample : public Example
131{
132public:
133 bool do_setup(int argc, char **argv) override
134 {
135 // Default parameters
Eren Kopuz350099e2020-06-09 15:37:43 +0100136 const float alpha = 1.0f;
137 const float beta = 0.0f;
138 const ActivationLayerInfo act_info = ActivationLayerInfo();
SiCong Liea803482019-09-26 16:55:49 +0100139 CommonGemmExampleParams params;
140 GemmConfigs configs;
141
142 // Set up command line parser and options
143 CommandLineParser parser;
144 CommonGemmExampleOptions param_options(parser);
145 GemmConfigOptions config_options(parser);
146
147 // Parse command line options
148 parser.parse(argc, argv);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100149 if (param_options.help->is_set() && param_options.help->value())
SiCong Liea803482019-09-26 16:55:49 +0100150 {
151 // Print help message
152 parser.print_help(argv[0]);
153 return false;
154 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100155 if (!parser.validate())
SiCong Liea803482019-09-26 16:55:49 +0100156 {
157 // Invalid arguments. Use default parameters and configs
158 std::cerr << "Invalid arguments." << std::endl;
159 parser.print_help(argv[0]);
160 std::cerr << "Falling back to default parameters and configs" << std::endl;
161 }
162 else
163 {
164 // Get parameters and configs from command-line options
165 params = consume_common_gemm_example_parameters(param_options);
166 configs = consume_gemm_configs(config_options);
167 }
168
169 // Print gemm parameters and configurations
Eren Kopuz15205d92020-07-17 15:13:39 +0100170 std::cout << "Gemm parameters:" << std::endl;
171 std::cout << params << std::endl;
172 std::cout << "Gemm configurations:" << std::endl;
173 std::cout << configs << std::endl;
SiCong Liea803482019-09-26 16:55:49 +0100174
Gian Marco Iodiceca419dd2021-03-03 17:25:07 +0000175 tuner.set_tuner_mode(params.tuner_mode);
176
SiCong Liea803482019-09-26 16:55:49 +0100177 CLScheduler::get().default_init(&tuner);
178
Eren Kopuz350099e2020-06-09 15:37:43 +0100179 lhs.allocator()->init(TensorInfo(TensorShape(params.K, params.M, params.B), 1, params.data_type));
180 rhs.allocator()->init(TensorInfo(TensorShape(params.N, params.K, params.B), 1, params.data_type));
181 bias.allocator()->init(TensorInfo(TensorShape(params.N, 1, params.B), 1, params.data_type));
SiCong Liea803482019-09-26 16:55:49 +0100182
183 GEMMLHSMatrixInfo lhs_info;
184 lhs_info.m0 = configs.m0;
185 lhs_info.k0 = configs.k0;
186
187 GEMMRHSMatrixInfo rhs_info;
188 rhs_info.n0 = configs.n0;
189 rhs_info.k0 = configs.k0;
190
191 GEMMKernelInfo kernel_info;
192 kernel_info.m = params.M;
193 kernel_info.n = params.N;
194 kernel_info.k = params.K;
195 kernel_info.depth_output_gemm3d = 0;
196 kernel_info.reinterpret_input_as_3d = false;
197 kernel_info.broadcast_bias = true;
198 kernel_info.activation_info = act_info;
199
Eren Kopuz15205d92020-07-17 15:13:39 +0100200 // Validate argments
201 Status status{};
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100202 status = gemm.validate(lhs.info(), rhs.info(), bias.info(), dst.info(), alpha, beta, lhs_info, rhs_info,
203 kernel_info);
204 if (!status)
Eren Kopuz15205d92020-07-17 15:13:39 +0100205 {
206 // Unsupported arguments
207 std::cerr << "Unsupported arguments." << std::endl;
208 std::cerr << "Check documentation for supported/unsupported combinations" << std::endl;
209 return false;
210 }
211
SiCong Liea803482019-09-26 16:55:49 +0100212 // Configure function
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100213 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 +0100214
215 // Allocate tensors
216 lhs.allocator()->allocate();
217 rhs.allocator()->allocate();
218 bias.allocator()->allocate();
219 dst.allocator()->allocate();
220
221 return true;
222 }
223 void do_run() override
224 {
225 // Execute the function
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100226 ITensorPack gemm_pack({{ACL_SRC_0, &lhs}, {ACL_SRC_1, &rhs}, {ACL_SRC_2, &bias}, {ACL_DST, &dst}});
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100227 gemm.run(gemm_pack);
SiCong Liea803482019-09-26 16:55:49 +0100228
229 // Make sure all the OpenCL jobs are done executing:
230 CLScheduler::get().sync();
231 }
232
233 void do_teardown() override
234 {
235 }
236
237private:
238 CLTensor lhs{};
239 CLTensor rhs{};
240 CLTensor bias{};
241 CLTensor dst{};
242 CLTuner tuner{};
243 CLGEMMMatrixMultiplyNative gemm{};
244};
245
246/** Main program for gemm native test
247 *
248 * @param[in] argc Number of arguments
249 * @param[in] argv Arguments ( [optional] M, [optional] N, [optional] K, [optional] B, [optional] m0, [optional] n0, [optional] k0 )
250 */
251int main(int argc, char **argv)
252{
253 return run_example<CLGEMMMatrixMultiplyNativeExample>(argc, argv);
254}