SiCong Li | ea80348 | 2019-09-26 16:55:49 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020 Arm Limited. |
SiCong Li | ea80348 | 2019-09-26 16:55:49 +0100 | [diff] [blame] | 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 | |
| 44 | using namespace arm_compute; |
| 45 | using namespace utils; |
| 46 | using namespace arm_compute::misc::shape_calculator; |
| 47 | using namespace gemm_tuner; |
| 48 | |
| 49 | namespace |
| 50 | { |
| 51 | /** Structure holding all tunable gemm configs specific to this example/strategy */ |
| 52 | struct 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 */ |
| 78 | class GemmConfigOptions |
| 79 | { |
| 80 | public: |
| 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 | */ |
| 116 | GemmConfigs 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 |
| 127 | using CLGEMMMatrixMultiplyNative = test::CLSynthetizeFunction<CLGEMMMatrixMultiplyNativeKernel>; |
| 128 | |
| 129 | class CLGEMMMatrixMultiplyNativeExample : public Example |
| 130 | { |
| 131 | public: |
| 132 | bool do_setup(int argc, char **argv) override |
| 133 | { |
| 134 | // Default parameters |
Eren Kopuz | 350099e | 2020-06-09 15:37:43 +0100 | [diff] [blame] | 135 | const float alpha = 1.0f; |
| 136 | const float beta = 0.0f; |
| 137 | const ActivationLayerInfo act_info = ActivationLayerInfo(); |
SiCong Li | ea80348 | 2019-09-26 16:55:49 +0100 | [diff] [blame] | 138 | 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 |
Eren Kopuz | 15205d9 | 2020-07-17 15:13:39 +0100 | [diff] [blame] | 169 | std::cout << "Gemm parameters:" << std::endl; |
| 170 | std::cout << params << std::endl; |
| 171 | std::cout << "Gemm configurations:" << std::endl; |
| 172 | std::cout << configs << std::endl; |
SiCong Li | ea80348 | 2019-09-26 16:55:49 +0100 | [diff] [blame] | 173 | |
| 174 | CLScheduler::get().default_init(&tuner); |
| 175 | |
Eren Kopuz | 350099e | 2020-06-09 15:37:43 +0100 | [diff] [blame] | 176 | 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 Li | ea80348 | 2019-09-26 16:55:49 +0100 | [diff] [blame] | 179 | |
| 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 | |
Eren Kopuz | 15205d9 | 2020-07-17 15:13:39 +0100 | [diff] [blame] | 197 | // Validate argments |
| 198 | Status status{}; |
| 199 | status = gemm.validate((&lhs)->info(), (&rhs)->info(), (&bias)->info(), (&dst)->info(), alpha, beta, lhs_info, rhs_info, kernel_info); |
| 200 | if(!status) |
| 201 | { |
| 202 | // Unsupported arguments |
| 203 | std::cerr << "Unsupported arguments." << std::endl; |
| 204 | std::cerr << "Check documentation for supported/unsupported combinations" << std::endl; |
| 205 | return false; |
| 206 | } |
| 207 | |
SiCong Li | ea80348 | 2019-09-26 16:55:49 +0100 | [diff] [blame] | 208 | // Configure function |
| 209 | gemm.configure(&lhs, &rhs, &bias, &dst, alpha, beta, lhs_info, rhs_info, kernel_info); |
| 210 | |
| 211 | // Allocate tensors |
| 212 | lhs.allocator()->allocate(); |
| 213 | rhs.allocator()->allocate(); |
| 214 | bias.allocator()->allocate(); |
| 215 | dst.allocator()->allocate(); |
| 216 | |
| 217 | return true; |
| 218 | } |
| 219 | void do_run() override |
| 220 | { |
| 221 | // Execute the function |
| 222 | gemm.run(); |
| 223 | |
| 224 | // Make sure all the OpenCL jobs are done executing: |
| 225 | CLScheduler::get().sync(); |
| 226 | } |
| 227 | |
| 228 | void do_teardown() override |
| 229 | { |
| 230 | } |
| 231 | |
| 232 | private: |
| 233 | CLTensor lhs{}; |
| 234 | CLTensor rhs{}; |
| 235 | CLTensor bias{}; |
| 236 | CLTensor dst{}; |
| 237 | CLTuner tuner{}; |
| 238 | CLGEMMMatrixMultiplyNative gemm{}; |
| 239 | }; |
| 240 | |
| 241 | /** Main program for gemm native test |
| 242 | * |
| 243 | * @param[in] argc Number of arguments |
| 244 | * @param[in] argv Arguments ( [optional] M, [optional] N, [optional] K, [optional] B, [optional] m0, [optional] n0, [optional] k0 ) |
| 245 | */ |
| 246 | int main(int argc, char **argv) |
| 247 | { |
| 248 | return run_example<CLGEMMMatrixMultiplyNativeExample>(argc, argv); |
| 249 | } |