SiCongLi | 282f324 | 2020-11-24 15:24:16 +0000 | [diff] [blame] | 1 | /* |
Manuel Bottini | 7b42786 | 2021-02-08 13:45:19 +0000 | [diff] [blame] | 2 | * Copyright (c) 2020-2021 Arm Limited. |
SiCongLi | 282f324 | 2020-11-24 15:24:16 +0000 | [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 "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/KernelDescriptors.h" |
| 30 | #include "arm_compute/core/Types.h" |
| 31 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 32 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 33 | #include "arm_compute/runtime/CL/CLTuner.h" |
| 34 | #include "examples/gemm_tuner/CommonGemmExampleOptions.h" |
| 35 | #include "examples/gemm_tuner/GemmTunerHelpers.h" |
| 36 | #include "src/core/CL/kernels/CLGEMMLowpMatrixMultiplyReshapedKernel.h" |
| 37 | #include "src/core/CL/kernels/CLGEMMReshapeLHSMatrixKernel.h" |
| 38 | #include "tests/CL/Helper.h" |
| 39 | #include "utils/Utils.h" |
| 40 | #include "utils/command_line/CommandLineOptions.h" |
| 41 | #include "utils/command_line/CommandLineParser.h" |
| 42 | |
| 43 | #include <cstdlib> |
| 44 | |
| 45 | using namespace arm_compute; |
| 46 | using namespace utils; |
| 47 | using namespace arm_compute::misc::shape_calculator; |
| 48 | using namespace gemm_tuner; |
| 49 | |
| 50 | namespace |
| 51 | { |
| 52 | /** Structure holding all tunable gemm configs specific to this example/strategy */ |
| 53 | struct GemmConfigs |
| 54 | { |
| 55 | size_t m0{ 4 }; /**< Number of rows processed by the matrix multiplication */ |
| 56 | size_t n0{ 4 }; /**< Number of columns processed by the matrix multiplication */ |
| 57 | size_t k0{ 4 }; /**< Number of partial accumulations performed by the matrix multiplication */ |
| 58 | size_t v0{ 1 }; /**< Number of vertical blocks of size (m0xk0) stored on the same output row */ |
| 59 | size_t h0{ 1 }; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row */ |
| 60 | bool interleave_lhs{ true }; /**< Interleave lhs matrix */ |
| 61 | bool transpose_lhs{ true }; /**< Transpose lhs matrix. */ |
| 62 | bool interleave_rhs{ true }; /**< Interleave rhs matrix */ |
| 63 | bool transpose_rhs{ true }; /**< Transpose rhs matrix. */ |
| 64 | }; |
| 65 | |
| 66 | /** Formatted output of the GemmConfigs type |
| 67 | * |
| 68 | * @param[out] os Output stream. |
| 69 | * @param[in] configs Tunable configurations to output |
| 70 | * |
| 71 | * @return Modified output stream. |
| 72 | */ |
| 73 | ::std::ostream &operator<<(::std::ostream &os, const GemmConfigs &configs) |
| 74 | { |
| 75 | std::string false_str = std::string("false"); |
| 76 | std::string true_str = std::string("true"); |
| 77 | |
| 78 | os << "m0 : " << configs.m0 << std::endl; |
| 79 | os << "n0 : " << configs.n0 << std::endl; |
| 80 | os << "k0 : " << configs.k0 << std::endl; |
| 81 | os << "v0 : " << configs.v0 << std::endl; |
| 82 | os << "h0 : " << configs.h0 << std::endl; |
| 83 | os << "interleave_lhs : " << (configs.interleave_lhs ? true_str : false_str) << std::endl; |
| 84 | os << "transpose_lhs : " << (configs.transpose_lhs ? true_str : false_str) << std::endl; |
| 85 | os << "interleave_rhs : " << (configs.interleave_rhs ? true_str : false_str) << std::endl; |
| 86 | os << "transpose_rhs : " << (configs.transpose_rhs ? true_str : false_str) << std::endl; |
| 87 | return os; |
| 88 | } |
| 89 | |
| 90 | /** Command line options for gemm configs */ |
| 91 | class GemmConfigOptions |
| 92 | { |
| 93 | public: |
| 94 | /** Constructor |
| 95 | * |
| 96 | * @param[in,out] parser A parser on which "parse()" hasn't been called yet. |
| 97 | */ |
| 98 | GemmConfigOptions(CommandLineParser &parser) |
| 99 | : m0(parser.add_positional_option<SimpleOption<size_t>>("m0", 4)), |
| 100 | n0(parser.add_positional_option<SimpleOption<size_t>>("n0", 4)), |
| 101 | k0(parser.add_positional_option<SimpleOption<size_t>>("k0", 4)), |
| 102 | v0(parser.add_positional_option<SimpleOption<size_t>>("v0", 1)), |
| 103 | h0(parser.add_positional_option<SimpleOption<size_t>>("h0", 1)), |
| 104 | interleave_lhs(parser.add_positional_option<SimpleOption<size_t>>("interleave_lhs", 1)), |
| 105 | interleave_rhs(parser.add_positional_option<SimpleOption<size_t>>("interleave_rhs", 1)), |
| 106 | transpose_rhs(parser.add_positional_option<SimpleOption<size_t>>("transpose_rhs", 1)) |
| 107 | { |
| 108 | m0->set_help("Number of rows processed by the matrix multiplication"); |
| 109 | n0->set_help("Number of columns processed by the matrix multiplication"); |
| 110 | k0->set_help("Number of partial accumulations performed by the matrix multiplication"); |
| 111 | v0->set_help("Number of vertical blocks of size (m0xk0) stored on the same output row"); |
| 112 | h0->set_help("Number of horizontal blocks of size (k0xn0) stored on the same output row"); |
| 113 | interleave_lhs->set_help("Interleave lhs matrix (1) / Do not interleave lhs matrix (0)"); |
| 114 | interleave_rhs->set_help("Interleave rhs matrix (1) / Do not interleave rhs matrix (0)"); |
| 115 | // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and |
| 116 | // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other |
| 117 | // 2 variants (both transposed and none transposed) |
| 118 | transpose_rhs->set_help("Transpose rhs matrix but not lhs matrix (1) / Do not transpose rhs matrix but do transpose lhs matrix (0)"); |
| 119 | } |
| 120 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 121 | GemmConfigOptions(const GemmConfigOptions &) = delete; |
| 122 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 123 | GemmConfigOptions &operator=(const GemmConfigOptions &) = delete; |
| 124 | /** Allow instances of this class to be moved */ |
| 125 | GemmConfigOptions(GemmConfigOptions &&) = default; |
| 126 | /** Allow instances of this class to be moved */ |
| 127 | GemmConfigOptions &operator=(GemmConfigOptions &&) = default; |
| 128 | /** Default destructor */ |
| 129 | ~GemmConfigOptions() = default; |
| 130 | |
| 131 | SimpleOption<size_t> *m0; /**< Number of rows processed by the matrix multiplication option */ |
| 132 | SimpleOption<size_t> *n0; /**< Number of columns processed by the matrix multiplication option */ |
| 133 | SimpleOption<size_t> *k0; /**< Number of partial accumulations performed by the matrix multiplication option */ |
| 134 | SimpleOption<size_t> *v0; /**< Number of vertical blocks of size (m0xk0) stored on the same output row option */ |
| 135 | SimpleOption<size_t> *h0; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row option */ |
| 136 | SimpleOption<size_t> *interleave_lhs; /**< Interleave lhs matrix option (1 enable; 0 disable) */ |
| 137 | SimpleOption<size_t> *interleave_rhs; /**< Interleave rhs matrix option (1 enable; 0 disable) */ |
| 138 | // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and |
| 139 | // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other |
| 140 | // 2 variants (both transposed and none transposed) |
| 141 | SimpleOption<size_t> *transpose_rhs; /**< Transpose rhs matrix option (1 enable; 0 disable). Also set the lhs matrix transpose option to the opposite. */ |
| 142 | }; |
| 143 | |
| 144 | /** Consumes the gemm configuration options and creates a structure containing all information |
| 145 | * |
| 146 | * @param[in] options Options to consume |
| 147 | * |
| 148 | * @return Structure containing the gemm configurations |
| 149 | */ |
| 150 | GemmConfigs consume_gemm_configs(const GemmConfigOptions &options) |
| 151 | { |
| 152 | GemmConfigs configs; |
| 153 | configs.m0 = options.m0->value(); |
| 154 | configs.n0 = options.n0->value(); |
| 155 | configs.k0 = options.k0->value(); |
| 156 | configs.v0 = options.v0->value(); |
| 157 | configs.h0 = options.h0->value(); |
| 158 | configs.interleave_lhs = options.interleave_lhs->value() != 0; |
| 159 | // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and |
| 160 | // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other |
| 161 | // 2 variants (both transposed and none transposed) |
| 162 | configs.transpose_lhs = options.transpose_rhs->value() == 0; |
| 163 | configs.interleave_rhs = options.interleave_rhs->value() != 0; |
| 164 | configs.transpose_rhs = options.transpose_rhs->value() != 0; |
| 165 | return configs; |
| 166 | } |
| 167 | |
| 168 | } // namespace |
| 169 | |
| 170 | using CLGEMMReshapeLHSMatrix = test::CLSynthetizeFunction<CLGEMMReshapeLHSMatrixKernel>; |
| 171 | using CLGEMMLowpMatrixMultiplyReshaped = test::CLSynthetizeFunction<CLGEMMLowpMatrixMultiplyReshapedKernel>; |
| 172 | |
| 173 | class CLGEMMLowpMatrixMultiplyReshapedExample : public Example |
| 174 | { |
| 175 | public: |
| 176 | bool do_setup(int argc, char **argv) override |
| 177 | { |
| 178 | // Default parameters |
| 179 | CommonGemmExampleParams params; |
| 180 | GemmConfigs configs; |
| 181 | |
| 182 | // Parse command line options |
| 183 | CommandLineParser parser; |
SiCong Li | 98e33b9 | 2020-12-03 14:52:53 +0000 | [diff] [blame] | 184 | CommonGemmExampleOptions param_options(parser, DataType::QASYMM8); |
SiCongLi | 282f324 | 2020-11-24 15:24:16 +0000 | [diff] [blame] | 185 | GemmConfigOptions config_options(parser); |
| 186 | |
| 187 | parser.parse(argc, argv); |
| 188 | if(param_options.help->is_set() && param_options.help->value()) |
| 189 | { |
| 190 | parser.print_help(argv[0]); |
| 191 | return false; |
| 192 | } |
| 193 | if(!parser.validate()) |
| 194 | { |
| 195 | // Invalid arguments. Use default parameters and configs |
| 196 | std::cerr << "Invalid arguments." << std::endl; |
| 197 | parser.print_help(argv[0]); |
| 198 | std::cerr << "Falling back to default parameters and configs" << std::endl; |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | params = consume_common_gemm_example_parameters(param_options); |
| 203 | configs = consume_gemm_configs(config_options); |
| 204 | } |
| 205 | |
| 206 | std::cout << "Gemm parameters:" << std::endl; |
| 207 | std::cout << params << std::endl; |
| 208 | std::cout << "Gemm configurations:" << std::endl; |
| 209 | std::cout << configs << std::endl; |
| 210 | |
Gian Marco Iodice | ca419dd | 2021-03-03 17:25:07 +0000 | [diff] [blame] | 211 | tuner.set_tuner_mode(params.tuner_mode); |
| 212 | |
SiCongLi | 282f324 | 2020-11-24 15:24:16 +0000 | [diff] [blame] | 213 | CLScheduler::get().default_init(&tuner); |
| 214 | |
| 215 | lhs.allocator()->init(TensorInfo(TensorShape(params.K, params.M, params.B), 1, params.data_type)); |
| 216 | rhs.allocator()->init(TensorInfo(TensorShape(params.N, params.K, params.B), 1, params.data_type)); |
| 217 | |
| 218 | // Set arbitrary quantization information |
SiCong Li | 98e33b9 | 2020-12-03 14:52:53 +0000 | [diff] [blame] | 219 | const QuantizationInfo q_info |
| 220 | { |
| 221 | 0.012, 3 |
| 222 | }; |
| 223 | lhs.info()->set_quantization_info(q_info); |
| 224 | rhs.info()->set_quantization_info(q_info); |
| 225 | dst.info()->set_quantization_info(q_info); |
SiCongLi | 282f324 | 2020-11-24 15:24:16 +0000 | [diff] [blame] | 226 | |
| 227 | GEMMLHSMatrixInfo lhs_info; |
| 228 | lhs_info.m0 = configs.m0; |
| 229 | lhs_info.k0 = configs.k0; |
| 230 | lhs_info.v0 = configs.v0; |
| 231 | lhs_info.interleave = configs.interleave_lhs; |
| 232 | lhs_info.transpose = configs.transpose_lhs; |
| 233 | |
| 234 | GEMMRHSMatrixInfo rhs_info; |
| 235 | rhs_info.n0 = configs.n0; |
| 236 | rhs_info.k0 = configs.k0; |
| 237 | rhs_info.h0 = configs.h0; |
| 238 | rhs_info.interleave = configs.interleave_rhs; |
| 239 | rhs_info.transpose = configs.transpose_rhs; |
| 240 | rhs_info.export_to_cl_image = false; // CL image not supported for quantized cases yet |
| 241 | |
| 242 | lhs_reshaped.allocator()->init(TensorInfo(compute_lhs_reshaped_shape(*lhs.info(), lhs_info), 1, params.data_type)); |
SiCongLi | 282f324 | 2020-11-24 15:24:16 +0000 | [diff] [blame] | 243 | rhs_reshaped.allocator()->init(TensorInfo(compute_rhs_reshaped_shape(*rhs.info(), rhs_info), 1, params.data_type)); |
SiCong Li | 98e33b9 | 2020-12-03 14:52:53 +0000 | [diff] [blame] | 244 | lhs_reshaped.info()->set_quantization_info(q_info); |
| 245 | rhs_reshaped.info()->set_quantization_info(q_info); |
SiCongLi | 282f324 | 2020-11-24 15:24:16 +0000 | [diff] [blame] | 246 | |
| 247 | if(rhs_info.export_to_cl_image) |
| 248 | { |
Manuel Bottini | 7b42786 | 2021-02-08 13:45:19 +0000 | [diff] [blame] | 249 | if(!examples::gemm_tuner_helpers::update_padding_for_cl_image(rhs_reshaped.info())) |
| 250 | { |
| 251 | std::cerr << "cl_image is not supported on the device, disable export_to_cl_image" << std::endl; |
| 252 | return false; |
| 253 | } |
SiCongLi | 282f324 | 2020-11-24 15:24:16 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | GEMMReshapeInfo gemm_info |
| 257 | { |
| 258 | static_cast<int>(params.M), |
| 259 | static_cast<int>(params.N), |
| 260 | static_cast<int>(params.K), |
| 261 | static_cast<int>(configs.h0), |
| 262 | static_cast<int>(configs.v0), |
| 263 | 0, |
| 264 | false, |
| 265 | true |
| 266 | }; |
| 267 | |
| 268 | // Validate argments |
| 269 | if(!reshape_lhs.validate(lhs.info(), lhs_reshaped.info(), lhs_info, gemm_info.reinterpret_input_as_3d())) |
| 270 | { |
| 271 | std::cerr << "Invalid arguments for CLGEMMReshapeLHSMatrixKernel." << std::endl; |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | if(!gemm.validate(lhs_reshaped.info(), rhs_reshaped.info(), dst.info(), lhs_info, rhs_info, gemm_info)) |
| 276 | { |
| 277 | std::cerr << "Invalid arguments for CLGEMMLowpMatrixMultiplyReshapedKernel." << std::endl; |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | // Configure functions |
| 282 | reshape_lhs.configure(&lhs, &lhs_reshaped, lhs_info); |
| 283 | |
| 284 | gemm.configure(&lhs_reshaped, &rhs_reshaped, &dst, lhs_info, rhs_info, gemm_info); |
| 285 | |
| 286 | // Allocate tensors |
| 287 | lhs.allocator()->allocate(); |
| 288 | rhs.allocator()->allocate(); |
| 289 | lhs_reshaped.allocator()->allocate(); |
| 290 | rhs_reshaped.allocator()->allocate(); |
| 291 | dst.allocator()->allocate(); |
| 292 | |
| 293 | return true; |
| 294 | } |
| 295 | void do_run() override |
| 296 | { |
| 297 | reshape_lhs.run(); |
| 298 | gemm.run(); |
| 299 | |
| 300 | // Make sure all the OpenCL jobs are done executing: |
| 301 | CLScheduler::get().sync(); |
| 302 | } |
| 303 | |
| 304 | void do_teardown() override |
| 305 | { |
| 306 | } |
| 307 | |
| 308 | private: |
| 309 | CLTensor lhs{}; |
| 310 | CLTensor rhs{}; |
| 311 | CLTensor lhs_reshaped{}; |
| 312 | CLTensor rhs_reshaped{}; |
| 313 | CLTensor dst{}; |
| 314 | CLTuner tuner{}; |
| 315 | CLGEMMReshapeLHSMatrix reshape_lhs{}; |
| 316 | CLGEMMLowpMatrixMultiplyReshaped gemm{}; |
| 317 | }; |
| 318 | |
| 319 | /** Main test program for gemmlowp reshaped |
| 320 | * |
| 321 | * @param[in] argc Number of arguments |
| 322 | * @param[in] argv Arguments ( [optional] M, [optional] N, [optional] K, [optional] B, [optional] m0, [optional] n0, [optional] k0, [optional] v0, [optional] h0, [optional] interleave_lhs, [optional] interleave_rhs, [optional] transpose_rhs ) |
| 323 | */ |
| 324 | int main(int argc, char **argv) |
| 325 | { |
| 326 | return run_example<CLGEMMLowpMatrixMultiplyReshapedExample>(argc, argv); |
| 327 | } |