blob: 09454990d71dbdd5a32cf1183f3d09d13994a53d [file] [log] [blame]
SiCong Li8b4c7302019-09-19 12:18:15 +01001/*
Eren Kopuz350099e2020-06-09 15:37:43 +01002 * Copyright (c) 2019-2020 ARM Limited.
SiCong Li8b4c7302019-09-19 12:18:15 +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 Li240b79d2019-09-24 15:50:34 +010028#include "CommonGemmExampleOptions.h"
SiCong Li8b4c7302019-09-19 12:18:15 +010029#include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyReshapedOnlyRHSKernel.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"
SiCong Li240b79d2019-09-24 15:50:34 +010039#include "utils/command_line/CommandLineOptions.h"
40#include "utils/command_line/CommandLineParser.h"
SiCong Li8b4c7302019-09-19 12:18:15 +010041
42#include <cstdlib>
43
44using namespace arm_compute;
45using namespace utils;
46using namespace arm_compute::misc::shape_calculator;
SiCong Li240b79d2019-09-24 15:50:34 +010047using namespace gemm_tuner;
SiCong Li8b4c7302019-09-19 12:18:15 +010048
49namespace
50{
SiCong Li8b4c7302019-09-19 12:18:15 +010051/** Structure holding all tunable gemm configs specific to this example/strategy */
52struct GemmConfigs
53{
SiCong Li240b79d2019-09-24 15:50:34 +010054 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 size_t h0{ 1 }; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row */
58 bool interleave_rhs{ true }; /**< Interleave rhs matrix */
59 bool transpose_rhs{ true }; /**< Transpose rhs matrix */
SiCong Li8b4c7302019-09-19 12:18:15 +010060};
61
62/** Formatted output of the GemmConfigs type
63 *
64 * @param[out] os Output stream.
65 * @param[in] configs Tunable configurations to output
66 *
67 * @return Modified output stream.
68 */
69::std::ostream &operator<<(::std::ostream &os, const GemmConfigs &configs)
70{
71 std::string false_str = std::string("false");
72 std::string true_str = std::string("true");
73
74 os << "m0 : " << configs.m0 << std::endl;
75 os << "n0 : " << configs.n0 << std::endl;
76 os << "k0 : " << configs.k0 << std::endl;
77 os << "h0 : " << configs.h0 << std::endl;
78 os << "interleave_rhs : " << (configs.interleave_rhs ? true_str : false_str) << std::endl;
79 os << "transpose_rhs : " << (configs.transpose_rhs ? true_str : false_str) << std::endl;
80 return os;
81}
SiCong Li240b79d2019-09-24 15:50:34 +010082
83/** Command line options for gemm configs */
84class GemmConfigOptions
85{
86public:
87 /** Constructor
88 *
89 * @param[in,out] parser A parser on which "parse()" hasn't been called yet.
90 */
91 GemmConfigOptions(CommandLineParser &parser)
92 : m0(parser.add_positional_option<SimpleOption<size_t>>("m0", 4)),
93 n0(parser.add_positional_option<SimpleOption<size_t>>("n0", 4)),
94 k0(parser.add_positional_option<SimpleOption<size_t>>("k0", 4)),
95 h0(parser.add_positional_option<SimpleOption<size_t>>("h0", 1)),
96 interleave_rhs(parser.add_positional_option<SimpleOption<size_t>>("interleave_rhs", 1)),
97 transpose_rhs(parser.add_positional_option<SimpleOption<size_t>>("transpose_rhs", 1))
98 {
99 m0->set_help("Number of rows processed by the matrix multiplication");
100 n0->set_help("Number of columns processed by the matrix multiplication");
101 k0->set_help("Number of partial accumulations performed by the matrix multiplication");
102 h0->set_help("Number of horizontal blocks of size (k0xn0) stored on the same output row");
103 interleave_rhs->set_help("Interleave rhs matrix (1) / Do not interleave rhs matrix (0)");
104 transpose_rhs->set_help("Transpose rhs matrix (1) / Do not transpose rhs matrix (0)");
105 }
106 /** Prevent instances of this class from being copied (As this class contains pointers) */
107 GemmConfigOptions(const GemmConfigOptions &) = delete;
108 /** Prevent instances of this class from being copied (As this class contains pointers) */
109 GemmConfigOptions &operator=(const GemmConfigOptions &) = delete;
110 /** Allow instances of this class to be moved */
111 GemmConfigOptions(GemmConfigOptions &&) = default;
112 /** Allow instances of this class to be moved */
113 GemmConfigOptions &operator=(GemmConfigOptions &&) = default;
114 /** Default destructor */
115 ~GemmConfigOptions() = default;
116
117 SimpleOption<size_t> *m0; /**< Number of rows processed by the matrix multiplication option */
118 SimpleOption<size_t> *n0; /**< Number of columns processed by the matrix multiplication option */
119 SimpleOption<size_t> *k0; /**< Number of partial accumulations performed by the matrix multiplication option */
120 SimpleOption<size_t> *h0; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row option */
121 SimpleOption<size_t> *interleave_rhs; /**< Interleave rhs matrix option (1 enable; 0 disable) */
122 SimpleOption<size_t> *transpose_rhs; /**< Transpose rhs matrix option (1 enable; 0 disable) */
123};
124
125/** Consumes the gemm configuration options and creates a structure containing all information
126 *
127 * @param[in] options Options to consume
128 *
129 * @return Structure containing the gemm configurations
130 */
131GemmConfigs consume_gemm_configs(const GemmConfigOptions &options)
132{
133 GemmConfigs configs;
134 configs.m0 = options.m0->value();
135 configs.n0 = options.n0->value();
136 configs.k0 = options.k0->value();
137 configs.h0 = options.h0->value();
138 configs.interleave_rhs = options.interleave_rhs->value() != 0;
139 configs.transpose_rhs = options.transpose_rhs->value() != 0;
140 return configs;
141}
142
SiCong Li8b4c7302019-09-19 12:18:15 +0100143} // namespace
144// Create function for CLGEMMMatrixMultiplyReshapedOnlyRHSKernel
145using CLGEMMMatrixMultiplyReshapedOnlyRHS = test::CLSynthetizeFunction<CLGEMMMatrixMultiplyReshapedOnlyRHSKernel>;
146
147class CLGEMMMatrixMultiplyReshapedOnlyRHSExample : public Example
148{
149public:
150 bool do_setup(int argc, char **argv) override
151 {
152 // Default parameters
Eren Kopuz350099e2020-06-09 15:37:43 +0100153 const float alpha = 1.0f;
154 const float beta = 0.0f;
155 const ActivationLayerInfo act_info = ActivationLayerInfo();
SiCong Li8b4c7302019-09-19 12:18:15 +0100156 CommonGemmExampleParams params;
157 GemmConfigs configs;
SiCong Li240b79d2019-09-24 15:50:34 +0100158
159 // Set up command line parser and options
160 CommandLineParser parser;
161 CommonGemmExampleOptions param_options(parser);
162 GemmConfigOptions config_options(parser);
163
164 // Parse command line options
165 parser.parse(argc, argv);
166 if(param_options.help->is_set() && param_options.help->value())
SiCong Li8b4c7302019-09-19 12:18:15 +0100167 {
SiCong Li240b79d2019-09-24 15:50:34 +0100168 // Print help message
169 parser.print_help(argv[0]);
170 return false;
171 }
172 if(!parser.validate())
173 {
174 // Invalid arguments. Use default parameters and configs
175 std::cerr << "Invalid arguments." << std::endl;
176 parser.print_help(argv[0]);
SiCong Li8b4c7302019-09-19 12:18:15 +0100177 std::cerr << "Falling back to default parameters and configs" << std::endl;
178 }
179 else
180 {
SiCong Li240b79d2019-09-24 15:50:34 +0100181 // Get parameters and configs from command-line options
182 params = consume_common_gemm_example_parameters(param_options);
183 configs = consume_gemm_configs(config_options);
SiCong Li8b4c7302019-09-19 12:18:15 +0100184 }
SiCong Li240b79d2019-09-24 15:50:34 +0100185
186 // Print gemm parameters and configurations
SiCong Li8b4c7302019-09-19 12:18:15 +0100187 std::cerr << "Gemm parameters:" << std::endl;
188 std::cerr << params << std::endl;
189 std::cerr << "Gemm configurations:" << std::endl;
190 std::cerr << configs << std::endl;
191
192 CLScheduler::get().default_init(&tuner);
193
Eren Kopuz350099e2020-06-09 15:37:43 +0100194 lhs.allocator()->init(TensorInfo(TensorShape(params.K, params.M, params.B), 1, params.data_type));
195 rhs.allocator()->init(TensorInfo(TensorShape(params.N, params.K, params.B), 1, params.data_type));
196 bias.allocator()->init(TensorInfo(TensorShape(params.N, 1, params.B), 1, params.data_type));
SiCong Li8b4c7302019-09-19 12:18:15 +0100197
198 GEMMLHSMatrixInfo lhs_info;
199 lhs_info.m0 = configs.m0;
200 lhs_info.k0 = configs.k0;
201
202 GEMMRHSMatrixInfo rhs_info;
203 rhs_info.n0 = configs.n0;
204 rhs_info.k0 = configs.k0;
205 rhs_info.h0 = configs.h0;
206 rhs_info.interleave = configs.interleave_rhs;
207 rhs_info.transpose = configs.transpose_rhs;
208
209 GEMMKernelInfo kernel_info;
210 kernel_info.m = params.M;
211 kernel_info.n = params.N;
212 kernel_info.k = params.K;
213 kernel_info.depth_output_gemm3d = 0;
214 kernel_info.reinterpret_input_as_3d = false;
215 kernel_info.broadcast_bias = true;
216 kernel_info.activation_info = act_info;
217
218 // Initialise rhs_reshaped tensor info
219 auto_init_if_empty(*rhs_reshaped.info(), rhs.info()->clone()->set_tensor_shape(compute_rhs_reshaped_shape(*rhs.info(), rhs_info)));
220
221 // Configure function
222 gemm.configure(&lhs, &rhs_reshaped, &bias, &dst, alpha, beta, lhs_info, rhs_info, kernel_info);
223
224 // Allocate tensors
225 lhs.allocator()->allocate();
226 rhs.allocator()->allocate();
227 rhs_reshaped.allocator()->allocate();
228 bias.allocator()->allocate();
229 dst.allocator()->allocate();
230
231 return true;
232 }
233 void do_run() override
234 {
235 // Execute the function
236 gemm.run();
237
238 // Make sure all the OpenCL jobs are done executing:
239 CLScheduler::get().sync();
240 }
241
242 void do_teardown() override
243 {
244 }
245
246private:
247 CLTensor lhs{};
248 CLTensor rhs{};
249 CLTensor rhs_reshaped{};
250 CLTensor bias{};
251 CLTensor dst{};
252 CLTuner tuner{};
253 CLGEMMMatrixMultiplyReshapedOnlyRHS gemm{};
254};
255
256/** Main program for gemm reshaped rhs only test
257 *
258 * @param[in] argc Number of arguments
SiCong Li240b79d2019-09-24 15:50:34 +0100259 * @param[in] argv Arguments ( [optional] M, [optional] N, [optional] K, [optional] B, [optional] m0, [optional] n0, [optional] k0, [optional] h0, [optional] interleave_rhs, [optional] transpose_rhs )
SiCong Li8b4c7302019-09-19 12:18:15 +0100260 */
261int main(int argc, char **argv)
262{
SiCong Li240b79d2019-09-24 15:50:34 +0100263 return run_example<CLGEMMMatrixMultiplyReshapedOnlyRHSExample>(argc, argv);
SiCong Li8b4c7302019-09-19 12:18:15 +0100264}