blob: 017de5cb02ad45c58f03f6dee6c21cc19770ece2 [file] [log] [blame]
SiCong Liea803482019-09-26 16:55:49 +01001/*
Eren Kopuz350099e2020-06-09 15:37:43 +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"
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
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
169 std::cerr << "Gemm parameters:" << std::endl;
170 std::cerr << params << std::endl;
171 std::cerr << "Gemm configurations:" << std::endl;
172 std::cerr << configs << std::endl;
173
174 CLScheduler::get().default_init(&tuner);
175
Eren Kopuz350099e2020-06-09 15:37:43 +0100176 lhs.allocator()->init(TensorInfo(TensorShape(params.K, params.M, params.B), 1, params.data_type));
177 rhs.allocator()->init(TensorInfo(TensorShape(params.N, params.K, params.B), 1, params.data_type));
178 bias.allocator()->init(TensorInfo(TensorShape(params.N, 1, params.B), 1, params.data_type));
SiCong Liea803482019-09-26 16:55:49 +0100179
180 GEMMLHSMatrixInfo lhs_info;
181 lhs_info.m0 = configs.m0;
182 lhs_info.k0 = configs.k0;
183
184 GEMMRHSMatrixInfo rhs_info;
185 rhs_info.n0 = configs.n0;
186 rhs_info.k0 = configs.k0;
187
188 GEMMKernelInfo kernel_info;
189 kernel_info.m = params.M;
190 kernel_info.n = params.N;
191 kernel_info.k = params.K;
192 kernel_info.depth_output_gemm3d = 0;
193 kernel_info.reinterpret_input_as_3d = false;
194 kernel_info.broadcast_bias = true;
195 kernel_info.activation_info = act_info;
196
197 // Configure function
198 gemm.configure(&lhs, &rhs, &bias, &dst, alpha, beta, lhs_info, rhs_info, kernel_info);
199
200 // Allocate tensors
201 lhs.allocator()->allocate();
202 rhs.allocator()->allocate();
203 bias.allocator()->allocate();
204 dst.allocator()->allocate();
205
206 return true;
207 }
208 void do_run() override
209 {
210 // Execute the function
211 gemm.run();
212
213 // Make sure all the OpenCL jobs are done executing:
214 CLScheduler::get().sync();
215 }
216
217 void do_teardown() override
218 {
219 }
220
221private:
222 CLTensor lhs{};
223 CLTensor rhs{};
224 CLTensor bias{};
225 CLTensor dst{};
226 CLTuner tuner{};
227 CLGEMMMatrixMultiplyNative gemm{};
228};
229
230/** Main program for gemm native test
231 *
232 * @param[in] argc Number of arguments
233 * @param[in] argv Arguments ( [optional] M, [optional] N, [optional] K, [optional] B, [optional] m0, [optional] n0, [optional] k0 )
234 */
235int main(int argc, char **argv)
236{
237 return run_example<CLGEMMMatrixMultiplyNativeExample>(argc, argv);
238}