Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_CLGEMM_H |
| 25 | #define ARM_COMPUTE_CLGEMM_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/runtime/CL/CLTensor.h" |
Gian Marco Iodice | 5a4fe19 | 2020-03-16 12:22:37 +0000 | [diff] [blame] | 28 | #include "arm_compute/runtime/CL/CLTypes.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/IFunction.h" |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/IMemoryManager.h" |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 31 | #include "arm_compute/runtime/IWeightsManager.h" |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 32 | #include "arm_compute/runtime/MemoryGroup.h" |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 33 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame^] | 34 | #include <memory> |
| 35 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | namespace arm_compute |
| 37 | { |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame^] | 38 | class CLCompileContext; |
| 39 | class CLGEMMReshapeRHSMatrixKernel; |
| 40 | class CLGEMMMatrixMultiplyKernel; |
| 41 | class CLGEMMMatrixMultiplyReshapedKernel; |
| 42 | class CLGEMMMatrixMultiplyReshapedOnlyRHSKernel; |
| 43 | class CLGEMMReshapeLHSMatrixKernel; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | class ICLTensor; |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame^] | 45 | class ITensorInfo; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 47 | namespace weights_transformations |
| 48 | { |
| 49 | /** Basic function to manage the reshape weights generated from @ref CLGEMMReshapeRHSMatrixKernel */ |
| 50 | class CLGEMMReshapeRHSMatrixKernelManaged : public ITransformWeights |
| 51 | { |
| 52 | public: |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame^] | 53 | /** Default constructor */ |
| 54 | CLGEMMReshapeRHSMatrixKernelManaged(); |
| 55 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 56 | CLGEMMReshapeRHSMatrixKernelManaged(const CLGEMMReshapeRHSMatrixKernelManaged &) = delete; |
| 57 | /** Default move constructor */ |
| 58 | CLGEMMReshapeRHSMatrixKernelManaged(CLGEMMReshapeRHSMatrixKernelManaged &&) = default; |
| 59 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 60 | CLGEMMReshapeRHSMatrixKernelManaged &operator=(const CLGEMMReshapeRHSMatrixKernelManaged &) = delete; |
| 61 | /** Default move assignment operator */ |
| 62 | CLGEMMReshapeRHSMatrixKernelManaged &operator=(CLGEMMReshapeRHSMatrixKernelManaged &&) = default; |
| 63 | /** Default desctructor */ |
| 64 | ~CLGEMMReshapeRHSMatrixKernelManaged(); |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 65 | //Inherited method override |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame^] | 66 | void run() override; |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 67 | |
| 68 | //Inherited method override |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame^] | 69 | void release() override; |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 70 | |
| 71 | //Inherited method override |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame^] | 72 | ICLTensor *get_weights() override; |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 73 | |
| 74 | //Inherited method override |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame^] | 75 | uint32_t uid() override; |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 76 | |
| 77 | /** Configures the @ref CLGEMMReshapeRHSMatrixKernel kernel |
| 78 | * |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 79 | * @param[in] input Input tensor. Data types supported: All |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 80 | * @param[in] info RHS matrix information to be used for reshaping. |
| 81 | */ |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame^] | 82 | void configure(const ICLTensor *input, GEMMRHSMatrixInfo info); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 83 | |
| 84 | /** Configures the @ref CLGEMMReshapeRHSMatrixKernel kernel |
| 85 | * |
| 86 | * @param[in] compile_context The compile context to be used. |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 87 | * @param[in] input Input tensor. Data types supported: All |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 88 | * @param[in] info RHS matrix information to be used for reshaping. |
| 89 | */ |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame^] | 90 | void configure(const CLCompileContext &compile_context, const ICLTensor *input, GEMMRHSMatrixInfo info); |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 91 | |
| 92 | private: |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame^] | 93 | static constexpr uint32_t _uid{ 0x15 }; |
| 94 | CLTensor _output{}; |
| 95 | std::unique_ptr<CLGEMMReshapeRHSMatrixKernel> _kernel; |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 96 | }; |
| 97 | } // namespace weights_transformations |
| 98 | |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 99 | /** Basic function to execute GEMM on OpenCL. This function calls the following OpenCL kernels: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | * |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 101 | * -# @ref CLGEMMReshapeLHSMatrixKernel (only if the RESHAPED_V1 is selected by the heuristic model) |
Gian Marco Iodice | 5a4fe19 | 2020-03-16 12:22:37 +0000 | [diff] [blame] | 102 | * -# @ref CLGEMMReshapeRHSMatrixKernel (only if either the RESHAPED_V1 or RESHAPED_ONLY_RHS is selected by the select_gemm_kernel method()) |
| 103 | * -# @ref CLGEMMMatrixMultiplyKernel (only if either the NATIVE or RESHAPED_V1 is selected by the select_gemm_kernel method()) |
| 104 | * -# @ref CLGEMMMatrixMultiplyReshapedKernel (only if RESHAPED_V1 is selected by the select_gemm_kernel method()) |
| 105 | * -# @ref CLGEMMMatrixMultiplyReshapedOnlyRHSKernel (only if RESHAPED_ONLY_RHS is selected by the select_gemm_kernel method()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 106 | * |
| 107 | */ |
| 108 | class CLGEMM : public IFunction |
| 109 | { |
| 110 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 111 | /** Default constructor. |
| 112 | * |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 113 | * @param[in] memory_manager (Optional) Memory manager. |
| 114 | * @param[in] weights_manager (Optional) Weights manager. |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 115 | */ |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 116 | CLGEMM(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr); |
Georgios Pinitas | 82b5148 | 2018-04-24 15:14:12 +0100 | [diff] [blame] | 117 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 118 | CLGEMM(const CLGEMM &) = delete; |
| 119 | /** Default move constructor */ |
| 120 | CLGEMM(CLGEMM &&) = default; |
| 121 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 122 | CLGEMM &operator=(const CLGEMM &) = delete; |
| 123 | /** Default move assignment operator */ |
| 124 | CLGEMM &operator=(CLGEMM &&) = default; |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame^] | 125 | /** Default destructor */ |
| 126 | ~CLGEMM(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 127 | /** Initialise the kernel's inputs and output |
| 128 | * |
| 129 | * @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C]. |
| 130 | * |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 131 | * @note All tensors must have the same data type. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 132 | * |
| 133 | * @note Whilst the first input tensor can be a vector, the second input tensor must be at least a matrix |
| 134 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 135 | * @param[in] a First input tensor (Matrix or Vector A). Data types supported: F16/F32 |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 136 | * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a. |
| 137 | * @param[in] c Third input tensor (Matrix C). It can be a nullptr if just the multiplication between @p a and @p b is needed. Data type supported: same as @p a. |
| 138 | * @param[out] output Output tensor. Data type supported: same as @p a |
| 139 | * @param[in] alpha Weight of the matrix product |
| 140 | * @param[in] beta Weight of matrix C |
| 141 | * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 142 | * if the reshape of matrix B should happen only for the first run. GEMMInfo also contains information about the reshaping |
| 143 | * in case matrix A and matrix B have been already transformed. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 144 | */ |
Gian Marco | 1d25ed5 | 2017-12-16 19:33:50 +0000 | [diff] [blame] | 145 | void configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo()); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 146 | /** Initialise the kernel's inputs and output |
| 147 | * |
| 148 | * @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C]. |
| 149 | * |
| 150 | * @note All tensors must have the same data type. |
| 151 | * |
| 152 | * @note Whilst the first input tensor can be a vector, the second input tensor must be at least a matrix |
| 153 | * |
| 154 | * @param[in] compile_context The compile context to be used. |
| 155 | * @param[in] a First input tensor (Matrix or Vector A). Data types supported: F16/F32 |
| 156 | * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a. |
| 157 | * @param[in] c Third input tensor (Matrix C). It can be a nullptr if just the multiplication between @p a and @p b is needed. Data type supported: same as @p a. |
| 158 | * @param[out] output Output tensor. Data type supported: same as @p a |
| 159 | * @param[in] alpha Weight of the matrix product |
| 160 | * @param[in] beta Weight of matrix C |
| 161 | * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and |
| 162 | * if the reshape of matrix B should happen only for the first run. GEMMInfo also contains information about the reshaping |
| 163 | * in case matrix A and matrix B have been already transformed. |
| 164 | */ |
| 165 | void configure(const CLCompileContext &compile_context, const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo()); |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 166 | /** Static function to check if given info will lead to a valid configuration of @ref CLGEMM. |
| 167 | * |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 168 | * @param[in] a First input tensor info (Matrix or Vector A). Data types supported: F16/F32 |
| 169 | * @param[in] b Second input tensor info (Matrix B). Data type supported: same as @p a. |
| 170 | * @param[in] c Third input tensor info (Matrix C). It can be a nullptr if just the multiplication between @p a and @p b is needed. Data type supported: same as @p a. |
| 171 | * @param[in] output Output tensor info. Data type supported: same as @p a |
| 172 | * @param[in] alpha Weight of the matrix product |
| 173 | * @param[in] beta Weight of matrix C |
| 174 | * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 175 | * if the reshape of matrix B should happen only for the first run |
| 176 | * |
| 177 | * @return a status |
| 178 | */ |
Gian Marco Iodice | 750641d | 2018-05-08 12:01:57 +0100 | [diff] [blame] | 179 | static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 180 | |
| 181 | // Inherited methods overridden: |
| 182 | void run() override; |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 183 | void prepare() override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 184 | |
| 185 | private: |
Gian Marco Iodice | 026d045 | 2020-08-28 13:52:12 +0100 | [diff] [blame] | 186 | static CLGEMMKernelType select_gemm_kernel(unsigned int m, unsigned int n, unsigned int k, unsigned int b, DataType data_type, bool reshape_b_only_on_first_run); |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 187 | |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 188 | void configure_native_v1(const CLCompileContext &compile_context, const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info); |
| 189 | void configure_reshaped_v1(const CLCompileContext &compile_context, const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info); |
| 190 | void configure_reshaped_v2(const CLCompileContext &compile_context, const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info); |
| 191 | void configure_reshaped_only_rhs(const CLCompileContext &compile_context, const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, |
| 192 | const GEMMInfo &gemm_info); |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 193 | |
Gian Marco Iodice | 5a4fe19 | 2020-03-16 12:22:37 +0000 | [diff] [blame] | 194 | static Status validate_native_v1(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info); |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 195 | static Status validate_reshaped_v1(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info); |
Gian Marco Iodice | 5a4fe19 | 2020-03-16 12:22:37 +0000 | [diff] [blame] | 196 | static Status validate_reshaped(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info); |
Gian Marco Iodice | 926afe1 | 2019-03-19 11:44:13 +0000 | [diff] [blame] | 197 | static Status validate_reshaped_only_rhs(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info); |
| 198 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame^] | 199 | MemoryGroup _memory_group; |
| 200 | IWeightsManager *_weights_manager; |
| 201 | std::unique_ptr<CLGEMMMatrixMultiplyKernel> _mm_kernel; |
| 202 | std::unique_ptr<CLGEMMReshapeLHSMatrixKernel> _reshape_lhs_kernel; |
| 203 | std::unique_ptr<CLGEMMReshapeRHSMatrixKernel> _reshape_rhs_kernel; |
| 204 | std::unique_ptr<weights_transformations::CLGEMMReshapeRHSMatrixKernelManaged> _reshape_rhs_kernel_managed; |
| 205 | std::unique_ptr<CLGEMMMatrixMultiplyReshapedKernel> _mm_reshaped_kernel; |
| 206 | std::unique_ptr<CLGEMMMatrixMultiplyReshapedOnlyRHSKernel> _mm_reshaped_only_rhs_kernel; |
| 207 | std::unique_ptr<CLGEMMMatrixMultiplyReshapedOnlyRHSKernel> _mm_reshaped_only_rhs_fallback_kernel; |
| 208 | CLTensor _tmp_a; |
| 209 | CLTensor _tmp_b; |
| 210 | const ICLTensor *_original_b; |
| 211 | const ICLTensor *_lhs; |
| 212 | ICLTensor *_dst; |
| 213 | bool _reshape_b_only_on_first_run; |
| 214 | bool _is_prepared; |
| 215 | bool _has_pad_y; |
| 216 | CLGEMMKernelType _gemm_kernel_type; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 217 | }; |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 218 | } // namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 219 | |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 220 | #endif /* ARM_COMPUTE_CLGEMM_H */ |