blob: 018d88d36669cb4043b28869f6add01c14d4454b [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CLGEMM_H
25#define ARM_COMPUTE_CLGEMM_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyKernel.h"
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000028#include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyReshapedKernel.h"
Gian Marco Iodice926afe12019-03-19 11:44:13 +000029#include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyReshapedOnlyRHSKernel.h"
Gian Marco Iodicebf9731e2018-12-12 10:18:04 +000030#include "arm_compute/core/CL/kernels/CLGEMMReshapeLHSMatrixKernel.h"
31#include "arm_compute/core/CL/kernels/CLGEMMReshapeRHSMatrixKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/runtime/CL/CLTensor.h"
Gian Marco Iodice5a4fe192020-03-16 12:22:37 +000033#include "arm_compute/runtime/CL/CLTypes.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/runtime/IFunction.h"
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010035#include "arm_compute/runtime/IMemoryManager.h"
Michalis Spyroub27e13a2019-09-27 11:04:27 +010036#include "arm_compute/runtime/IWeightsManager.h"
Georgios Pinitas26014cf2019-09-09 19:00:57 +010037#include "arm_compute/runtime/MemoryGroup.h"
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010038
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039namespace arm_compute
40{
41class ICLTensor;
42
Michalis Spyroub27e13a2019-09-27 11:04:27 +010043namespace weights_transformations
44{
45/** Basic function to manage the reshape weights generated from @ref CLGEMMReshapeRHSMatrixKernel */
46class CLGEMMReshapeRHSMatrixKernelManaged : public ITransformWeights
47{
48public:
49 //Inherited method override
50 void run() override
51 {
52 _output.allocator()->allocate();
53 CLScheduler::get().enqueue(_kernel, false);
54 _reshape_run = true;
55 }
56
57 //Inherited method override
58 void release() override
59 {
60 _output.allocator()->free();
61 }
62
63 //Inherited method override
64 ICLTensor *get_weights() override
65 {
66 return &_output;
67 }
68
69 //Inherited method override
70 uint32_t uid() override
71 {
72 return _uid;
73 }
74
75 /** Configures the @ref CLGEMMReshapeRHSMatrixKernel kernel
76 *
77 * @param[in] input Input tensor. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32
78 * @param[in] info RHS matrix information to be used for reshaping.
79 */
80 void configure(const ICLTensor *input, GEMMRHSMatrixInfo info)
81 {
Manuel Bottini2b84be52020-04-08 10:15:51 +010082 configure(CLKernelLibrary::get().get_compile_context(), input, info);
83 }
84
85 /** Configures the @ref CLGEMMReshapeRHSMatrixKernel kernel
86 *
87 * @param[in] compile_context The compile context to be used.
88 * @param[in] input Input tensor. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32
89 * @param[in] info RHS matrix information to be used for reshaping.
90 */
91 void configure(const CLCompileContext &compile_context, const ICLTensor *input, GEMMRHSMatrixInfo info)
92 {
93 _kernel.configure(compile_context, input, &_output, info);
Michalis Spyroub27e13a2019-09-27 11:04:27 +010094 }
95
96private:
97 static constexpr uint32_t _uid = 0x15;
98 CLTensor _output{};
99 CLGEMMReshapeRHSMatrixKernel _kernel{};
100};
101} // namespace weights_transformations
102
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100103/** Basic function to execute GEMM on OpenCL. This function calls the following OpenCL kernels:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104 *
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000105 * -# @ref CLGEMMReshapeLHSMatrixKernel (only if the RESHAPED_V1 is selected by the heuristic model)
Gian Marco Iodice5a4fe192020-03-16 12:22:37 +0000106 * -# @ref CLGEMMReshapeRHSMatrixKernel (only if either the RESHAPED_V1 or RESHAPED_ONLY_RHS is selected by the select_gemm_kernel method())
107 * -# @ref CLGEMMMatrixMultiplyKernel (only if either the NATIVE or RESHAPED_V1 is selected by the select_gemm_kernel method())
108 * -# @ref CLGEMMMatrixMultiplyReshapedKernel (only if RESHAPED_V1 is selected by the select_gemm_kernel method())
109 * -# @ref CLGEMMMatrixMultiplyReshapedOnlyRHSKernel (only if RESHAPED_ONLY_RHS is selected by the select_gemm_kernel method())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110 *
111 */
112class CLGEMM : public IFunction
113{
114public:
Alex Gildayc357c472018-03-21 13:54:09 +0000115 /** Default constructor.
116 *
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100117 * @param[in] memory_manager (Optional) Memory manager.
118 * @param[in] weights_manager (Optional) Weights manager.
Alex Gildayc357c472018-03-21 13:54:09 +0000119 */
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100120 CLGEMM(std::shared_ptr<IMemoryManager> memory_manager = nullptr, IWeightsManager *weights_manager = nullptr);
Georgios Pinitas82b51482018-04-24 15:14:12 +0100121 /** Prevent instances of this class from being copied (As this class contains pointers) */
122 CLGEMM(const CLGEMM &) = delete;
123 /** Default move constructor */
124 CLGEMM(CLGEMM &&) = default;
125 /** Prevent instances of this class from being copied (As this class contains pointers) */
126 CLGEMM &operator=(const CLGEMM &) = delete;
127 /** Default move assignment operator */
128 CLGEMM &operator=(CLGEMM &&) = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129 /** Initialise the kernel's inputs and output
130 *
131 * @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C].
132 *
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100133 * @note All tensors must have the same data type.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134 *
135 * @note Whilst the first input tensor can be a vector, the second input tensor must be at least a matrix
136 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100137 * @param[in] a First input tensor (Matrix or Vector A). Data types supported: F16/F32
Gian Marco1d25ed52017-12-16 19:33:50 +0000138 * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a.
139 * @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.
140 * @param[out] output Output tensor. Data type supported: same as @p a
141 * @param[in] alpha Weight of the matrix product
142 * @param[in] beta Weight of matrix C
143 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
Gian Marco36a0a462018-01-12 10:21:40 +0000144 * if the reshape of matrix B should happen only for the first run. GEMMInfo also contains information about the reshaping
145 * in case matrix A and matrix B have been already transformed.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146 */
Gian Marco1d25ed52017-12-16 19:33:50 +0000147 void configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info = GEMMInfo());
Manuel Bottini2b84be52020-04-08 10:15:51 +0100148 /** Initialise the kernel's inputs and output
149 *
150 * @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C].
151 *
152 * @note All tensors must have the same data type.
153 *
154 * @note Whilst the first input tensor can be a vector, the second input tensor must be at least a matrix
155 *
156 * @param[in] compile_context The compile context to be used.
157 * @param[in] a First input tensor (Matrix or Vector A). Data types supported: F16/F32
158 * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a.
159 * @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.
160 * @param[out] output Output tensor. Data type supported: same as @p a
161 * @param[in] alpha Weight of the matrix product
162 * @param[in] beta Weight of matrix C
163 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
164 * if the reshape of matrix B should happen only for the first run. GEMMInfo also contains information about the reshaping
165 * in case matrix A and matrix B have been already transformed.
166 */
167 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 Pinitas78c00902018-01-09 17:33:11 +0000168 /** Static function to check if given info will lead to a valid configuration of @ref CLGEMM.
169 *
giuros011c9efeb2019-01-11 14:04:43 +0000170 * @param[in] a First input tensor info (Matrix or Vector A). Data types supported: F16/F32
171 * @param[in] b Second input tensor info (Matrix B). Data type supported: same as @p a.
172 * @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.
173 * @param[in] output Output tensor info. Data type supported: same as @p a
174 * @param[in] alpha Weight of the matrix product
175 * @param[in] beta Weight of matrix C
176 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
Georgios Pinitas78c00902018-01-09 17:33:11 +0000177 * if the reshape of matrix B should happen only for the first run
178 *
179 * @return a status
180 */
Gian Marco Iodice750641d2018-05-08 12:01:57 +0100181 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 Barbier6ff3b192017-09-04 18:44:23 +0100182
183 // Inherited methods overridden:
184 void run() override;
Georgios Pinitase0437672018-05-02 14:07:55 +0100185 void prepare() override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186
187private:
Gian Marco Iodice5a4fe192020-03-16 12:22:37 +0000188 static CLGEMMKernelType select_gemm_kernel(unsigned int m, unsigned int n, unsigned int k, DataType data_type, bool reshape_b_only_on_first_run);
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000189
Manuel Bottini2b84be52020-04-08 10:15:51 +0100190 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);
191 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);
192 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);
193 void configure_reshaped_only_rhs(const CLCompileContext &compile_context, const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta,
194 const GEMMInfo &gemm_info);
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000195
Gian Marco Iodice5a4fe192020-03-16 12:22:37 +0000196 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 Iodice926afe12019-03-19 11:44:13 +0000197 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 Iodice5a4fe192020-03-16 12:22:37 +0000198 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 Iodice926afe12019-03-19 11:44:13 +0000199 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);
200
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100201 MemoryGroup _memory_group;
202 IWeightsManager *_weights_manager;
203 CLGEMMMatrixMultiplyKernel _mm_kernel;
204 CLGEMMReshapeLHSMatrixKernel _reshape_lhs_kernel;
205 CLGEMMReshapeRHSMatrixKernel _reshape_rhs_kernel;
206 weights_transformations::CLGEMMReshapeRHSMatrixKernelManaged _reshape_rhs_kernel_managed;
207 CLGEMMMatrixMultiplyReshapedKernel _mm_reshaped_kernel;
208 CLGEMMMatrixMultiplyReshapedOnlyRHSKernel _mm_reshaped_only_rhs_kernel;
209 CLTensor _tmp_a;
210 CLTensor _tmp_b;
211 const ICLTensor *_original_b;
212 bool _reshape_b_only_on_first_run;
213 bool _is_prepared;
Gian Marco Iodice5a4fe192020-03-16 12:22:37 +0000214 CLGEMMKernelType _gemm_kernel_type;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215};
giuros011c9efeb2019-01-11 14:04:43 +0000216} // namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100217
Michalis Spyrouf4643372019-11-29 16:17:13 +0000218#endif /* ARM_COMPUTE_CLGEMM_H */