blob: 02f144ea1203b2b519d5ecfc169897850bc78d9e [file] [log] [blame]
SiCong Liea803482019-09-26 16:55:49 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 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
173 CLScheduler::get().default_init(&tuner);
174
Eren Kopuz350099e2020-06-09 15:37:43 +0100175 lhs.allocator()->init(TensorInfo(TensorShape(params.K, params.M, params.B), 1, params.data_type));
176 rhs.allocator()->init(TensorInfo(TensorShape(params.N, params.K, params.B), 1, params.data_type));
177 bias.allocator()->init(TensorInfo(TensorShape(params.N, 1, params.B), 1, params.data_type));
SiCong Liea803482019-09-26 16:55:49 +0100178
179 GEMMLHSMatrixInfo lhs_info;
180 lhs_info.m0 = configs.m0;
181 lhs_info.k0 = configs.k0;
182
183 GEMMRHSMatrixInfo rhs_info;
184 rhs_info.n0 = configs.n0;
185 rhs_info.k0 = configs.k0;
186
187 GEMMKernelInfo kernel_info;
188 kernel_info.m = params.M;
189 kernel_info.n = params.N;
190 kernel_info.k = params.K;
191 kernel_info.depth_output_gemm3d = 0;
192 kernel_info.reinterpret_input_as_3d = false;
193 kernel_info.broadcast_bias = true;
194 kernel_info.activation_info = act_info;
195
Eren Kopuz15205d92020-07-17 15:13:39 +0100196 // Validate argments
197 Status status{};
198 status = gemm.validate((&lhs)->info(), (&rhs)->info(), (&bias)->info(), (&dst)->info(), alpha, beta, lhs_info, rhs_info, kernel_info);
199 if(!status)
200 {
201 // Unsupported arguments
202 std::cerr << "Unsupported arguments." << std::endl;
203 std::cerr << "Check documentation for supported/unsupported combinations" << std::endl;
204 return false;
205 }
206
SiCong Liea803482019-09-26 16:55:49 +0100207 // Configure function
208 gemm.configure(&lhs, &rhs, &bias, &dst, alpha, beta, lhs_info, rhs_info, kernel_info);
209
210 // Allocate tensors
211 lhs.allocator()->allocate();
212 rhs.allocator()->allocate();
213 bias.allocator()->allocate();
214 dst.allocator()->allocate();
215
216 return true;
217 }
218 void do_run() override
219 {
220 // Execute the function
221 gemm.run();
222
223 // Make sure all the OpenCL jobs are done executing:
224 CLScheduler::get().sync();
225 }
226
227 void do_teardown() override
228 {
229 }
230
231private:
232 CLTensor lhs{};
233 CLTensor rhs{};
234 CLTensor bias{};
235 CLTensor dst{};
236 CLTuner tuner{};
237 CLGEMMMatrixMultiplyNative gemm{};
238};
239
240/** Main program for gemm native test
241 *
242 * @param[in] argc Number of arguments
243 * @param[in] argv Arguments ( [optional] M, [optional] N, [optional] K, [optional] B, [optional] m0, [optional] n0, [optional] k0 )
244 */
245int main(int argc, char **argv)
246{
247 return run_example<CLGEMMMatrixMultiplyNativeExample>(argc, argv);
248}