blob: 0cacd820872051cc5b182f3e8c720428a6be177a [file] [log] [blame]
SiCong Liea803482019-09-26 16:55:49 +01001/*
2 * Copyright (c) 2019 ARM Limited.
3 *
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"
29#include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyNativeKernel.h"
30#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"
34#include "arm_compute/runtime/CL/CLFunctions.h"
35#include "arm_compute/runtime/CL/CLScheduler.h"
36#include "arm_compute/runtime/CL/CLTuner.h"
37#include "tests/CL/Helper.h"
38#include "utils/Utils.h"
39#include "utils/command_line/CommandLineOptions.h"
40#include "utils/command_line/CommandLineParser.h"
41
42#include <cstdlib>
43
44using namespace arm_compute;
45using 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
126// Create function for CLGEMMMatrixMultiplyNativeKernel
127using CLGEMMMatrixMultiplyNative = test::CLSynthetizeFunction<CLGEMMMatrixMultiplyNativeKernel>;
128
129class CLGEMMMatrixMultiplyNativeExample : public Example
130{
131public:
132 bool do_setup(int argc, char **argv) override
133 {
134 // Default parameters
135 const DataType data_type = DataType::F32;
136 const float alpha = 1.0f;
137 const float beta = 0.0f;
138 const ActivationLayerInfo act_info = ActivationLayerInfo();
139 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);
149 if(param_options.help->is_set() && param_options.help->value())
150 {
151 // Print help message
152 parser.print_help(argv[0]);
153 return false;
154 }
155 if(!parser.validate())
156 {
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
170 std::cerr << "Gemm parameters:" << std::endl;
171 std::cerr << params << std::endl;
172 std::cerr << "Gemm configurations:" << std::endl;
173 std::cerr << configs << std::endl;
174
175 CLScheduler::get().default_init(&tuner);
176
177 lhs.allocator()->init(TensorInfo(TensorShape(params.K, params.M, params.B), 1, data_type));
178 rhs.allocator()->init(TensorInfo(TensorShape(params.N, params.K, params.B), 1, data_type));
179 bias.allocator()->init(TensorInfo(TensorShape(params.N, 1, params.B), 1, data_type));
180
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
198 // Configure function
199 gemm.configure(&lhs, &rhs, &bias, &dst, alpha, beta, lhs_info, rhs_info, kernel_info);
200
201 // Allocate tensors
202 lhs.allocator()->allocate();
203 rhs.allocator()->allocate();
204 bias.allocator()->allocate();
205 dst.allocator()->allocate();
206
207 return true;
208 }
209 void do_run() override
210 {
211 // Execute the function
212 gemm.run();
213
214 // Make sure all the OpenCL jobs are done executing:
215 CLScheduler::get().sync();
216 }
217
218 void do_teardown() override
219 {
220 }
221
222private:
223 CLTensor lhs{};
224 CLTensor rhs{};
225 CLTensor bias{};
226 CLTensor dst{};
227 CLTuner tuner{};
228 CLGEMMMatrixMultiplyNative gemm{};
229};
230
231/** Main program for gemm native test
232 *
233 * @param[in] argc Number of arguments
234 * @param[in] argv Arguments ( [optional] M, [optional] N, [optional] K, [optional] B, [optional] m0, [optional] n0, [optional] k0 )
235 */
236int main(int argc, char **argv)
237{
238 return run_example<CLGEMMMatrixMultiplyNativeExample>(argc, argv);
239}