blob: 5a144dabf714826b8c829e1cc1e70068c5b7feed [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"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010035#include "src/core/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;
44using namespace utils;
45using namespace arm_compute::misc::shape_calculator;
46using namespace gemm_tuner;
47
48namespace
49{
50/** Structure holding all tunable gemm configs specific to this example/strategy */
51struct GemmConfigs
52{
53 size_t m0{ 4 }; /**< Number of rows processed by the matrix multiplication */
54 size_t n0{ 4 }; /**< Number of columns processed by the matrix multiplication */
55 size_t k0{ 4 }; /**< Number of partial accumulations performed by the matrix multiplication */
56};
57
58/** Formatted output of the GemmConfigs type
59 *
60 * @param[out] os Output stream.
61 * @param[in] configs Tunable configurations to output
62 *
63 * @return Modified output stream.
64 */
65::std::ostream &operator<<(::std::ostream &os, const GemmConfigs &configs)
66{
67 std::string false_str = std::string("false");
68 std::string true_str = std::string("true");
69
70 os << "m0 : " << configs.m0 << std::endl;
71 os << "n0 : " << configs.n0 << std::endl;
72 os << "k0 : " << configs.k0 << std::endl;
73 return os;
74}
75
76/** Command line options for gemm configs */
77class GemmConfigOptions
78{
79public:
80 /** Constructor
81 *
82 * @param[in,out] parser A parser on which "parse()" hasn't been called yet.
83 */
84 GemmConfigOptions(CommandLineParser &parser)
85 : m0(parser.add_positional_option<SimpleOption<size_t>>("m0", 4)),
86 n0(parser.add_positional_option<SimpleOption<size_t>>("n0", 4)),
87 k0(parser.add_positional_option<SimpleOption<size_t>>("k0", 4))
88 {
89 m0->set_help("Number of rows processed by the matrix multiplication");
90 n0->set_help("Number of columns processed by the matrix multiplication");
91 k0->set_help("Number of partial accumulations performed by the matrix multiplication");
92 }
93 /** Prevent instances of this class from being copied (As this class contains pointers) */
94 GemmConfigOptions(const GemmConfigOptions &) = delete;
95 /** Prevent instances of this class from being copied (As this class contains pointers) */
96 GemmConfigOptions &operator=(const GemmConfigOptions &) = delete;
97 /** Allow instances of this class to be moved */
98 GemmConfigOptions(GemmConfigOptions &&) = default;
99 /** Allow instances of this class to be moved */
100 GemmConfigOptions &operator=(GemmConfigOptions &&) = default;
101 /** Default destructor */
102 ~GemmConfigOptions() = default;
103
104 SimpleOption<size_t> *m0; /**< Number of rows processed by the matrix multiplication option */
105 SimpleOption<size_t> *n0; /**< Number of columns processed by the matrix multiplication option */
106 SimpleOption<size_t> *k0; /**< Number of partial accumulations performed by the matrix multiplication option */
107};
108
109/** Consumes the gemm configuration options and creates a structure containing all information
110 *
111 * @param[in] options Options to consume
112 *
113 * @return Structure containing the gemm configurations
114 */
115GemmConfigs consume_gemm_configs(const GemmConfigOptions &options)
116{
117 GemmConfigs configs;
118 configs.m0 = options.m0->value();
119 configs.n0 = options.n0->value();
120 configs.k0 = options.k0->value();
121 return configs;
122}
123
124} // namespace
125// Create function for CLGEMMMatrixMultiplyNativeKernel
126using CLGEMMMatrixMultiplyNative = test::CLSynthetizeFunction<CLGEMMMatrixMultiplyNativeKernel>;
127
128class CLGEMMMatrixMultiplyNativeExample : public Example
129{
130public:
131 bool do_setup(int argc, char **argv) override
132 {
133 // Default parameters
Eren Kopuz350099e2020-06-09 15:37:43 +0100134 const float alpha = 1.0f;
135 const float beta = 0.0f;
136 const ActivationLayerInfo act_info = ActivationLayerInfo();
SiCong Liea803482019-09-26 16:55:49 +0100137 CommonGemmExampleParams params;
138 GemmConfigs configs;
139
140 // Set up command line parser and options
141 CommandLineParser parser;
142 CommonGemmExampleOptions param_options(parser);
143 GemmConfigOptions config_options(parser);
144
145 // Parse command line options
146 parser.parse(argc, argv);
147 if(param_options.help->is_set() && param_options.help->value())
148 {
149 // Print help message
150 parser.print_help(argv[0]);
151 return false;
152 }
153 if(!parser.validate())
154 {
155 // Invalid arguments. Use default parameters and configs
156 std::cerr << "Invalid arguments." << std::endl;
157 parser.print_help(argv[0]);
158 std::cerr << "Falling back to default parameters and configs" << std::endl;
159 }
160 else
161 {
162 // Get parameters and configs from command-line options
163 params = consume_common_gemm_example_parameters(param_options);
164 configs = consume_gemm_configs(config_options);
165 }
166
167 // Print gemm parameters and configurations
Eren Kopuz15205d92020-07-17 15:13:39 +0100168 std::cout << "Gemm parameters:" << std::endl;
169 std::cout << params << std::endl;
170 std::cout << "Gemm configurations:" << std::endl;
171 std::cout << configs << std::endl;
SiCong Liea803482019-09-26 16:55:49 +0100172
Gian Marco Iodiceca419dd2021-03-03 17:25:07 +0000173 tuner.set_tuner_mode(params.tuner_mode);
174
SiCong Liea803482019-09-26 16:55:49 +0100175 CLScheduler::get().default_init(&tuner);
176
Eren Kopuz350099e2020-06-09 15:37:43 +0100177 lhs.allocator()->init(TensorInfo(TensorShape(params.K, params.M, params.B), 1, params.data_type));
178 rhs.allocator()->init(TensorInfo(TensorShape(params.N, params.K, params.B), 1, params.data_type));
179 bias.allocator()->init(TensorInfo(TensorShape(params.N, 1, params.B), 1, params.data_type));
SiCong Liea803482019-09-26 16:55:49 +0100180
181 GEMMLHSMatrixInfo lhs_info;
182 lhs_info.m0 = configs.m0;
183 lhs_info.k0 = configs.k0;
184
185 GEMMRHSMatrixInfo rhs_info;
186 rhs_info.n0 = configs.n0;
187 rhs_info.k0 = configs.k0;
188
189 GEMMKernelInfo kernel_info;
190 kernel_info.m = params.M;
191 kernel_info.n = params.N;
192 kernel_info.k = params.K;
193 kernel_info.depth_output_gemm3d = 0;
194 kernel_info.reinterpret_input_as_3d = false;
195 kernel_info.broadcast_bias = true;
196 kernel_info.activation_info = act_info;
197
Eren Kopuz15205d92020-07-17 15:13:39 +0100198 // Validate argments
199 Status status{};
200 status = gemm.validate((&lhs)->info(), (&rhs)->info(), (&bias)->info(), (&dst)->info(), alpha, beta, lhs_info, rhs_info, kernel_info);
201 if(!status)
202 {
203 // Unsupported arguments
204 std::cerr << "Unsupported arguments." << std::endl;
205 std::cerr << "Check documentation for supported/unsupported combinations" << std::endl;
206 return false;
207 }
208
SiCong Liea803482019-09-26 16:55:49 +0100209 // Configure function
210 gemm.configure(&lhs, &rhs, &bias, &dst, alpha, beta, lhs_info, rhs_info, kernel_info);
211
212 // Allocate tensors
213 lhs.allocator()->allocate();
214 rhs.allocator()->allocate();
215 bias.allocator()->allocate();
216 dst.allocator()->allocate();
217
218 return true;
219 }
220 void do_run() override
221 {
222 // Execute the function
223 gemm.run();
224
225 // Make sure all the OpenCL jobs are done executing:
226 CLScheduler::get().sync();
227 }
228
229 void do_teardown() override
230 {
231 }
232
233private:
234 CLTensor lhs{};
235 CLTensor rhs{};
236 CLTensor bias{};
237 CLTensor dst{};
238 CLTuner tuner{};
239 CLGEMMMatrixMultiplyNative gemm{};
240};
241
242/** Main program for gemm native test
243 *
244 * @param[in] argc Number of arguments
245 * @param[in] argv Arguments ( [optional] M, [optional] N, [optional] K, [optional] B, [optional] m0, [optional] n0, [optional] k0 )
246 */
247int main(int argc, char **argv)
248{
249 return run_example<CLGEMMMatrixMultiplyNativeExample>(argc, argv);
250}