Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
SiCong Li | 1b6377b | 2023-01-09 15:34:20 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2021, 2023 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 | |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 27 | #include "arm_compute/function_info/GEMMInfo.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/runtime/CL/CLTensor.h" |
Gian Marco Iodice | 5a4fe19 | 2020-03-16 12:22:37 +0000 | [diff] [blame] | 29 | #include "arm_compute/runtime/CL/CLTypes.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/IFunction.h" |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 31 | #include "arm_compute/runtime/IMemoryManager.h" |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 32 | #include "arm_compute/runtime/IWeightsManager.h" |
Georgios Pinitas | 26014cf | 2019-09-09 19:00:57 +0100 | [diff] [blame] | 33 | #include "arm_compute/runtime/MemoryGroup.h" |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 34 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 35 | #include <memory> |
| 36 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | namespace arm_compute |
| 38 | { |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 39 | // Forward declarations |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 40 | class CLCompileContext; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 41 | class ICLTensor; |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 42 | class ITensorInfo; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 43 | |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 44 | /** Basic function to execute GEMM on OpenCL */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 45 | class CLGEMM : public IFunction |
| 46 | { |
| 47 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 48 | /** Default constructor. |
| 49 | * |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 50 | * @param[in] memory_manager (Optional) Memory manager. |
| 51 | * @param[in] weights_manager (Optional) Weights manager. |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 52 | */ |
Michalis Spyrou | b27e13a | 2019-09-27 11:04:27 +0100 | [diff] [blame] | 53 | CLGEMM(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 54 | /** Default destructor */ |
| 55 | ~CLGEMM(); |
Georgios Pinitas | 82b5148 | 2018-04-24 15:14:12 +0100 | [diff] [blame] | 56 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 57 | CLGEMM(const CLGEMM &) = delete; |
| 58 | /** Default move constructor */ |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 59 | CLGEMM(CLGEMM &&); |
Georgios Pinitas | 82b5148 | 2018-04-24 15:14:12 +0100 | [diff] [blame] | 60 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 61 | CLGEMM &operator=(const CLGEMM &) = delete; |
| 62 | /** Default move assignment operator */ |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 63 | CLGEMM &operator=(CLGEMM &&); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | /** Initialise the kernel's inputs and output |
| 65 | * |
Teresa Charlin | 6268742 | 2021-04-28 10:58:49 +0100 | [diff] [blame] | 66 | * Valid data layouts: |
| 67 | * - All |
| 68 | * |
| 69 | * Valid data type configurations: |
| 70 | * |src0 |src1 |src2 |dst | |
| 71 | * |:------------|:-----------|:---------|:--------------| |
| 72 | * |F32 |F32 |F32 |F32 | |
| 73 | * |F16 |F16 |F16 |F16 | |
| 74 | * |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 75 | * @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C]. |
| 76 | * |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 77 | * @note All tensors must have the same data type. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 78 | * |
| 79 | * @note Whilst the first input tensor can be a vector, the second input tensor must be at least a matrix |
| 80 | * |
SiCong Li | 1b6377b | 2023-01-09 15:34:20 +0000 | [diff] [blame] | 81 | * @note Batched GEMM only allows RHS tensor's rank to be <= 3 |
| 82 | * @note Batched GEMM only supports broadcasting cases where RHS rank < LHS rank but not the other way around |
| 83 | * |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 84 | * @param[in] compile_context The compile context to be used. |
| 85 | * @param[in] a First input tensor (Matrix or Vector A). Data types supported: F16/F32 |
| 86 | * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a. |
| 87 | * @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. |
| 88 | * @param[out] output Output tensor. Data type supported: same as @p a |
| 89 | * @param[in] alpha Weight of the matrix product |
| 90 | * @param[in] beta Weight of matrix C |
| 91 | * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 92 | * if the reshape of matrix B should happen only for the first run. GEMMInfo also contains information about the reshaping |
| 93 | * in case matrix A and matrix B have been already transformed. |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 94 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 95 | void configure(const CLCompileContext &compile_context, |
| 96 | const ICLTensor *a, |
| 97 | const ICLTensor *b, |
| 98 | const ICLTensor *c, |
| 99 | ICLTensor *output, |
| 100 | float alpha, |
| 101 | float beta, |
| 102 | const GEMMInfo &gemm_info = GEMMInfo()); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 103 | |
| 104 | /** Initialise the kernel's inputs and output |
| 105 | * |
| 106 | * Similar to @ref CLGEMM::configure() |
| 107 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 108 | void configure(const ICLTensor *a, |
| 109 | const ICLTensor *b, |
| 110 | const ICLTensor *c, |
| 111 | ICLTensor *output, |
| 112 | float alpha, |
| 113 | float beta, |
| 114 | const GEMMInfo &gemm_info = GEMMInfo()); |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 115 | |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 116 | /** Static function to check if given info will lead to a valid configuration of @ref CLGEMM. |
| 117 | * |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 118 | * Similar to @ref CLGEMM::configure() |
Georgios Pinitas | 78c0090 | 2018-01-09 17:33:11 +0000 | [diff] [blame] | 119 | * |
| 120 | * @return a status |
| 121 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 122 | static Status validate(const ITensorInfo *a, |
| 123 | const ITensorInfo *b, |
| 124 | const ITensorInfo *c, |
| 125 | const ITensorInfo *output, |
| 126 | float alpha, |
| 127 | float beta, |
| 128 | const GEMMInfo &gemm_info = GEMMInfo()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 129 | |
| 130 | // Inherited methods overridden: |
| 131 | void run() override; |
Georgios Pinitas | e043767 | 2018-05-02 14:07:55 +0100 | [diff] [blame] | 132 | void prepare() override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 133 | |
| 134 | private: |
Georgios Pinitas | 856f66e | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 135 | struct Impl; |
| 136 | std::unique_ptr<Impl> _impl; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 137 | }; |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 138 | } // namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 139 | |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 140 | #endif /* ARM_COMPUTE_CLGEMM_H */ |