SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | 60ab4e6 | 2023-04-28 10:40:07 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2021, 2023 Arm Limited. |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +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 | |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 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" |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 32 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 33 | #include "arm_compute/runtime/CL/CLTuner.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 34 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 35 | #include "examples/gemm_tuner/CommonGemmExampleOptions.h" |
| 36 | #include "examples/gemm_tuner/GemmTunerHelpers.h" |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 37 | #include "src/gpu/cl/kernels/ClGemmMatrixMultiplyReshapedKernel.h" |
| 38 | #include "src/gpu/cl/kernels/ClGemmReshapeLhsMatrixKernel.h" |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 39 | #include "tests/CL/Helper.h" |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 40 | #include "utils/command_line/CommandLineOptions.h" |
| 41 | #include "utils/command_line/CommandLineParser.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 42 | #include "utils/Utils.h" |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 43 | |
| 44 | #include <cstdlib> |
| 45 | |
| 46 | using namespace arm_compute; |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 47 | using namespace arm_compute::opencl::kernels; |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 48 | using namespace utils; |
| 49 | using namespace arm_compute::misc::shape_calculator; |
| 50 | using namespace gemm_tuner; |
| 51 | |
| 52 | namespace |
| 53 | { |
| 54 | /** Structure holding all tunable gemm configs specific to this example/strategy */ |
| 55 | struct GemmConfigs |
| 56 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 57 | size_t m0{4}; /**< Number of rows processed by the matrix multiplication */ |
| 58 | size_t n0{4}; /**< Number of columns processed by the matrix multiplication */ |
| 59 | size_t k0{4}; /**< Number of partial accumulations performed by the matrix multiplication */ |
| 60 | size_t v0{1}; /**< Number of vertical blocks of size (m0xk0) stored on the same output row */ |
| 61 | size_t h0{1}; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row */ |
| 62 | bool interleave_lhs{true}; /**< Interleave lhs matrix */ |
| 63 | bool transpose_lhs{true}; /**< Transpose lhs matrix. */ |
| 64 | bool interleave_rhs{true}; /**< Interleave rhs matrix */ |
| 65 | bool transpose_rhs{true}; /**< Transpose rhs matrix. */ |
| 66 | bool export_to_cl_image_rhs{true}; /**< Export rhs matrix to cl_image. */ |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | /** Formatted output of the GemmConfigs type |
| 70 | * |
| 71 | * @param[out] os Output stream. |
| 72 | * @param[in] configs Tunable configurations to output |
| 73 | * |
| 74 | * @return Modified output stream. |
| 75 | */ |
| 76 | ::std::ostream &operator<<(::std::ostream &os, const GemmConfigs &configs) |
| 77 | { |
| 78 | std::string false_str = std::string("false"); |
| 79 | std::string true_str = std::string("true"); |
| 80 | |
| 81 | os << "m0 : " << configs.m0 << std::endl; |
| 82 | os << "n0 : " << configs.n0 << std::endl; |
| 83 | os << "k0 : " << configs.k0 << std::endl; |
| 84 | os << "v0 : " << configs.v0 << std::endl; |
| 85 | os << "h0 : " << configs.h0 << std::endl; |
| 86 | os << "interleave_lhs : " << (configs.interleave_lhs ? true_str : false_str) << std::endl; |
| 87 | os << "transpose_lhs : " << (configs.transpose_lhs ? true_str : false_str) << std::endl; |
| 88 | os << "interleave_rhs : " << (configs.interleave_rhs ? true_str : false_str) << std::endl; |
| 89 | os << "transpose_rhs : " << (configs.transpose_rhs ? true_str : false_str) << std::endl; |
Manuel Bottini | c8e6e2c | 2020-07-02 11:27:27 +0100 | [diff] [blame] | 90 | os << "export_to_cl_image_rhs : " << (configs.export_to_cl_image_rhs ? true_str : false_str) << std::endl; |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 91 | return os; |
| 92 | } |
| 93 | |
| 94 | /** Command line options for gemm configs */ |
| 95 | class GemmConfigOptions |
| 96 | { |
| 97 | public: |
| 98 | /** Constructor |
| 99 | * |
| 100 | * @param[in,out] parser A parser on which "parse()" hasn't been called yet. |
| 101 | */ |
| 102 | GemmConfigOptions(CommandLineParser &parser) |
| 103 | : m0(parser.add_positional_option<SimpleOption<size_t>>("m0", 4)), |
| 104 | n0(parser.add_positional_option<SimpleOption<size_t>>("n0", 4)), |
| 105 | k0(parser.add_positional_option<SimpleOption<size_t>>("k0", 4)), |
| 106 | v0(parser.add_positional_option<SimpleOption<size_t>>("v0", 1)), |
| 107 | h0(parser.add_positional_option<SimpleOption<size_t>>("h0", 1)), |
| 108 | interleave_lhs(parser.add_positional_option<SimpleOption<size_t>>("interleave_lhs", 1)), |
| 109 | interleave_rhs(parser.add_positional_option<SimpleOption<size_t>>("interleave_rhs", 1)), |
Manuel Bottini | c8e6e2c | 2020-07-02 11:27:27 +0100 | [diff] [blame] | 110 | transpose_rhs(parser.add_positional_option<SimpleOption<size_t>>("transpose_rhs", 1)), |
| 111 | export_to_cl_image_rhs(parser.add_positional_option<SimpleOption<size_t>>("export_to_cl_image_rhs", 1)) |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 112 | { |
| 113 | m0->set_help("Number of rows processed by the matrix multiplication"); |
| 114 | n0->set_help("Number of columns processed by the matrix multiplication"); |
| 115 | k0->set_help("Number of partial accumulations performed by the matrix multiplication"); |
| 116 | v0->set_help("Number of vertical blocks of size (m0xk0) stored on the same output row"); |
| 117 | h0->set_help("Number of horizontal blocks of size (k0xn0) stored on the same output row"); |
| 118 | interleave_lhs->set_help("Interleave lhs matrix (1) / Do not interleave lhs matrix (0)"); |
| 119 | interleave_rhs->set_help("Interleave rhs matrix (1) / Do not interleave rhs matrix (0)"); |
| 120 | // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and |
| 121 | // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other |
| 122 | // 2 variants (both transposed and none transposed) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 123 | transpose_rhs->set_help("Transpose rhs matrix but not lhs matrix (1) / Do not transpose rhs matrix but do " |
| 124 | "transpose lhs matrix (0)"); |
| 125 | export_to_cl_image_rhs->set_help( |
| 126 | "Export rhs matrix to cl_image (1) / Do not export rhs matrix to cl_image (0)"); |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 127 | } |
| 128 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 129 | GemmConfigOptions(const GemmConfigOptions &) = delete; |
| 130 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 131 | GemmConfigOptions &operator=(const GemmConfigOptions &) = delete; |
| 132 | /** Allow instances of this class to be moved */ |
| 133 | GemmConfigOptions(GemmConfigOptions &&) = default; |
| 134 | /** Allow instances of this class to be moved */ |
| 135 | GemmConfigOptions &operator=(GemmConfigOptions &&) = default; |
| 136 | /** Default destructor */ |
| 137 | ~GemmConfigOptions() = default; |
| 138 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 139 | SimpleOption<size_t> *m0; /**< Number of rows processed by the matrix multiplication option */ |
| 140 | SimpleOption<size_t> *n0; /**< Number of columns processed by the matrix multiplication option */ |
| 141 | SimpleOption<size_t> *k0; /**< Number of partial accumulations performed by the matrix multiplication option */ |
| 142 | SimpleOption<size_t> *v0; /**< Number of vertical blocks of size (m0xk0) stored on the same output row option */ |
| 143 | SimpleOption<size_t> *h0; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row option */ |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 144 | SimpleOption<size_t> *interleave_lhs; /**< Interleave lhs matrix option (1 enable; 0 disable) */ |
| 145 | SimpleOption<size_t> *interleave_rhs; /**< Interleave rhs matrix option (1 enable; 0 disable) */ |
| 146 | // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and |
| 147 | // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other |
| 148 | // 2 variants (both transposed and none transposed) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 149 | SimpleOption<size_t> * |
| 150 | transpose_rhs; /**< Transpose rhs matrix option (1 enable; 0 disable). Also set the lhs matrix transpose option to the opposite. */ |
Manuel Bottini | c8e6e2c | 2020-07-02 11:27:27 +0100 | [diff] [blame] | 151 | SimpleOption<size_t> *export_to_cl_image_rhs; /**< Export rhs matrix to cl_image.*/ |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | /** Consumes the gemm configuration options and creates a structure containing all information |
| 155 | * |
| 156 | * @param[in] options Options to consume |
| 157 | * |
| 158 | * @return Structure containing the gemm configurations |
| 159 | */ |
| 160 | GemmConfigs consume_gemm_configs(const GemmConfigOptions &options) |
| 161 | { |
| 162 | GemmConfigs configs; |
| 163 | configs.m0 = options.m0->value(); |
| 164 | configs.n0 = options.n0->value(); |
| 165 | configs.k0 = options.k0->value(); |
| 166 | configs.v0 = options.v0->value(); |
| 167 | configs.h0 = options.h0->value(); |
| 168 | configs.interleave_lhs = options.interleave_lhs->value() != 0; |
| 169 | // FIXME: Currently we only support 2 variants of the gemm reshaped kernels in which transpose_lhs and |
| 170 | // transpose_rhs are the opposites of each other. In the future we may extend the kernels to include the other |
| 171 | // 2 variants (both transposed and none transposed) |
Manuel Bottini | c8e6e2c | 2020-07-02 11:27:27 +0100 | [diff] [blame] | 172 | configs.transpose_lhs = options.transpose_rhs->value() == 0; |
| 173 | configs.interleave_rhs = options.interleave_rhs->value() != 0; |
| 174 | configs.transpose_rhs = options.transpose_rhs->value() != 0; |
| 175 | configs.export_to_cl_image_rhs = options.export_to_cl_image_rhs->value() != 0; |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 176 | return configs; |
| 177 | } |
| 178 | |
| 179 | } // namespace |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 180 | |
| 181 | // Create function for ClGemmReshapeLhsMatrixKernel |
| 182 | using CLGEMMReshapeLHSMatrix = test::CLSynthetizeOperator<ClGemmReshapeLhsMatrixKernel>; |
| 183 | // Create function for ClGemmMatrixMultiplyReshapedKernel |
| 184 | using CLGEMMMatrixMultiplyReshaped = test::CLSynthetizeOperator<ClGemmMatrixMultiplyReshapedKernel>; |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 185 | |
| 186 | class CLGEMMMatrixMultiplyReshapedExample : public Example |
| 187 | { |
| 188 | public: |
| 189 | bool do_setup(int argc, char **argv) override |
| 190 | { |
| 191 | // Default parameters |
Eren Kopuz | 350099e | 2020-06-09 15:37:43 +0100 | [diff] [blame] | 192 | const float alpha = 1.0f; |
| 193 | const float beta = 0.0f; |
| 194 | const ActivationLayerInfo act_info = ActivationLayerInfo(); |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 195 | CommonGemmExampleParams params; |
| 196 | GemmConfigs configs; |
| 197 | |
| 198 | // Set up command line parser and options |
| 199 | CommandLineParser parser; |
| 200 | CommonGemmExampleOptions param_options(parser); |
| 201 | GemmConfigOptions config_options(parser); |
| 202 | |
| 203 | // Parse command line options |
| 204 | parser.parse(argc, argv); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 205 | if (param_options.help->is_set() && param_options.help->value()) |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 206 | { |
| 207 | // Print help message |
| 208 | parser.print_help(argv[0]); |
| 209 | return false; |
| 210 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 211 | if (!parser.validate()) |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 212 | { |
| 213 | // Invalid arguments. Use default parameters and configs |
| 214 | std::cerr << "Invalid arguments." << std::endl; |
| 215 | parser.print_help(argv[0]); |
| 216 | std::cerr << "Falling back to default parameters and configs" << std::endl; |
| 217 | } |
| 218 | else |
| 219 | { |
| 220 | // Get parameters and configs from command-line options |
| 221 | params = consume_common_gemm_example_parameters(param_options); |
| 222 | configs = consume_gemm_configs(config_options); |
| 223 | } |
| 224 | |
| 225 | // Print gemm parameters and configurations |
Eren Kopuz | 15205d9 | 2020-07-17 15:13:39 +0100 | [diff] [blame] | 226 | std::cout << "Gemm parameters:" << std::endl; |
| 227 | std::cout << params << std::endl; |
| 228 | std::cout << "Gemm configurations:" << std::endl; |
| 229 | std::cout << configs << std::endl; |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 230 | |
Gian Marco Iodice | ca419dd | 2021-03-03 17:25:07 +0000 | [diff] [blame] | 231 | tuner.set_tuner_mode(params.tuner_mode); |
| 232 | |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 233 | CLScheduler::get().default_init(&tuner); |
| 234 | |
Eren Kopuz | 350099e | 2020-06-09 15:37:43 +0100 | [diff] [blame] | 235 | lhs.allocator()->init(TensorInfo(TensorShape(params.K, params.M, params.B), 1, params.data_type)); |
| 236 | rhs.allocator()->init(TensorInfo(TensorShape(params.N, params.K, params.B), 1, params.data_type)); |
| 237 | bias.allocator()->init(TensorInfo(TensorShape(params.N, 1, params.B), 1, params.data_type)); |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 238 | |
| 239 | GEMMLHSMatrixInfo lhs_info; |
| 240 | lhs_info.m0 = configs.m0; |
| 241 | lhs_info.k0 = configs.k0; |
| 242 | lhs_info.v0 = configs.v0; |
| 243 | lhs_info.interleave = configs.interleave_lhs; |
| 244 | lhs_info.transpose = configs.transpose_lhs; |
| 245 | |
| 246 | GEMMRHSMatrixInfo rhs_info; |
Manuel Bottini | c8e6e2c | 2020-07-02 11:27:27 +0100 | [diff] [blame] | 247 | rhs_info.n0 = configs.n0; |
| 248 | rhs_info.k0 = configs.k0; |
| 249 | rhs_info.h0 = configs.h0; |
| 250 | rhs_info.interleave = configs.interleave_rhs; |
| 251 | rhs_info.transpose = configs.transpose_rhs; |
| 252 | rhs_info.export_to_cl_image = configs.export_to_cl_image_rhs; |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 253 | |
| 254 | GEMMKernelInfo kernel_info; |
| 255 | kernel_info.m = params.M; |
| 256 | kernel_info.n = params.N; |
| 257 | kernel_info.k = params.K; |
| 258 | kernel_info.depth_output_gemm3d = 0; |
| 259 | kernel_info.reinterpret_input_as_3d = false; |
| 260 | kernel_info.broadcast_bias = true; |
| 261 | kernel_info.activation_info = act_info; |
| 262 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 263 | if (rhs_info.h0 == 0) |
Gian Marco Iodice | 60ab4e6 | 2023-04-28 10:40:07 +0100 | [diff] [blame] | 264 | { |
| 265 | rhs_info.h0 = std::max(kernel_info.n / rhs_info.n0, 1U); |
| 266 | } |
| 267 | |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 268 | // Initialise lhs_reshaped tensor info |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 269 | lhs_reshaped.allocator()->init( |
| 270 | TensorInfo(compute_lhs_reshaped_shape(*lhs.info(), lhs_info), 1, params.data_type)); |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 271 | |
| 272 | // Initialise rhs_reshaped tensor info |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 273 | rhs_reshaped.allocator()->init( |
| 274 | TensorInfo(compute_rhs_reshaped_shape(*rhs.info(), rhs_info), 1, params.data_type)); |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 275 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 276 | if (rhs_info.export_to_cl_image) |
Manuel Bottini | c8e6e2c | 2020-07-02 11:27:27 +0100 | [diff] [blame] | 277 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 278 | if (!examples::gemm_tuner_helpers::update_padding_for_cl_image(rhs_reshaped.info())) |
Manuel Bottini | 7b42786 | 2021-02-08 13:45:19 +0000 | [diff] [blame] | 279 | { |
| 280 | std::cerr << "cl_image is not supported on the device, disable export_to_cl_image" << std::endl; |
| 281 | return false; |
| 282 | } |
Manuel Bottini | c8e6e2c | 2020-07-02 11:27:27 +0100 | [diff] [blame] | 283 | } |
| 284 | |
Eren Kopuz | 15205d9 | 2020-07-17 15:13:39 +0100 | [diff] [blame] | 285 | // Validate argments |
| 286 | Status status{}; |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 287 | status = reshape_lhs.validate(lhs.info(), lhs_reshaped.info(), lhs_info, kernel_info.reinterpret_input_as_3d); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 288 | if (!status) |
Eren Kopuz | 15205d9 | 2020-07-17 15:13:39 +0100 | [diff] [blame] | 289 | { |
| 290 | // Unsupported arguments |
| 291 | std::cerr << "Unsupported arguments." << std::endl; |
| 292 | std::cerr << "Check documentation for supported/unsupported combinations" << std::endl; |
| 293 | return false; |
| 294 | } |
| 295 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 296 | status = gemm.validate(lhs_reshaped.info(), rhs_reshaped.info(), bias.info(), dst.info(), alpha, beta, lhs_info, |
| 297 | rhs_info, kernel_info); |
| 298 | if (!status) |
Eren Kopuz | 15205d9 | 2020-07-17 15:13:39 +0100 | [diff] [blame] | 299 | { |
| 300 | // Unsupported arguments |
| 301 | std::cerr << "Unsupported arguments." << std::endl; |
| 302 | std::cerr << "Check documentation for supported/unsupported combinations" << std::endl; |
| 303 | return false; |
| 304 | } |
| 305 | |
SiCong Li | e36b526 | 2019-10-01 19:26:00 +0100 | [diff] [blame] | 306 | // Configure reshape lhs function |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 307 | reshape_lhs.configure(lhs.info(), lhs_reshaped.info(), lhs_info); |
SiCong Li | e36b526 | 2019-10-01 19:26:00 +0100 | [diff] [blame] | 308 | |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 309 | // Configure function |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 310 | gemm.configure(lhs_reshaped.info(), rhs_reshaped.info(), bias.info(), dst.info(), alpha, beta, lhs_info, |
| 311 | rhs_info, kernel_info); |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 312 | |
| 313 | // Allocate tensors |
| 314 | lhs.allocator()->allocate(); |
| 315 | rhs.allocator()->allocate(); |
| 316 | lhs_reshaped.allocator()->allocate(); |
| 317 | rhs_reshaped.allocator()->allocate(); |
| 318 | bias.allocator()->allocate(); |
| 319 | dst.allocator()->allocate(); |
| 320 | |
| 321 | return true; |
| 322 | } |
| 323 | void do_run() override |
| 324 | { |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 325 | // Execute the functions |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 326 | ITensorPack reshape_lsh_pack({{ACL_SRC, &lhs}, {ACL_DST, &lhs_reshaped}}); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 327 | reshape_lhs.run(reshape_lsh_pack); |
| 328 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 329 | ITensorPack gemm_pack( |
| 330 | {{ACL_SRC_0, &lhs_reshaped}, {ACL_SRC_1, &rhs_reshaped}, {ACL_SRC_2, &bias}, {ACL_DST, &dst}}); |
Gian Marco Iodice | 3cfd329 | 2021-08-24 11:58:25 +0100 | [diff] [blame] | 331 | gemm.run(gemm_pack); |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 332 | |
| 333 | // Make sure all the OpenCL jobs are done executing: |
| 334 | CLScheduler::get().sync(); |
| 335 | } |
| 336 | |
| 337 | void do_teardown() override |
| 338 | { |
| 339 | } |
| 340 | |
| 341 | private: |
| 342 | CLTensor lhs{}; |
| 343 | CLTensor rhs{}; |
| 344 | CLTensor lhs_reshaped{}; |
| 345 | CLTensor rhs_reshaped{}; |
| 346 | CLTensor bias{}; |
| 347 | CLTensor dst{}; |
| 348 | CLTuner tuner{}; |
SiCong Li | e36b526 | 2019-10-01 19:26:00 +0100 | [diff] [blame] | 349 | CLGEMMReshapeLHSMatrix reshape_lhs{}; |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 350 | CLGEMMMatrixMultiplyReshaped gemm{}; |
| 351 | }; |
| 352 | |
| 353 | /** Main program for gemm reshaped test |
| 354 | * |
| 355 | * @param[in] argc Number of arguments |
SiCongLi | 282f324 | 2020-11-24 15:24:16 +0000 | [diff] [blame] | 356 | * @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, [optional] export_to_cl_image ) |
SiCong Li | bc166d5 | 2019-09-26 14:58:53 +0100 | [diff] [blame] | 357 | */ |
| 358 | int main(int argc, char **argv) |
| 359 | { |
| 360 | return run_example<CLGEMMMatrixMultiplyReshapedExample>(argc, argv); |
| 361 | } |