blob: 11f9f2b3d8ab30381a33d4690bab43b1f204fbf1 [file] [log] [blame]
Georgios Pinitas856f66e2021-04-22 21:13:21 +01001/*
SiCong Li1b6377b2023-01-09 15:34:20 +00002 * Copyright (c) 2016-2023 Arm Limited.
Georgios Pinitas856f66e2021-04-22 21:13:21 +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 */
24#ifndef ARM_COMPUTE_CL_GEMM_H
25#define ARM_COMPUTE_CL_GEMM_H
26
27#include "arm_compute/core/TensorInfo.h"
SiCong Li91295492023-07-21 18:16:13 +010028#include "arm_compute/function_info/GEMMInfo.h"
Georgios Pinitas856f66e2021-04-22 21:13:21 +010029#include "arm_compute/runtime/CL/CLTensor.h"
30#include "arm_compute/runtime/CL/CLTypes.h"
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +010031
Georgios Pinitas7891a732021-08-20 21:39:25 +010032#include "src/gpu/cl/ClCompileContext.h"
33#include "src/gpu/cl/IClKernel.h"
34#include "src/gpu/cl/IClOperator.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010035#include "src/gpu/cl/kernels/ClGemmMatrixMultiplyNativeKernel.h"
36#include "src/gpu/cl/kernels/ClGemmMatrixMultiplyReshapedKernel.h"
37#include "src/gpu/cl/kernels/ClGemmMatrixMultiplyReshapedOnlyRhsKernel.h"
Gunes Bayir4bfc70e2021-12-10 16:17:56 +000038#include "src/gpu/cl/kernels/ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010039#include "src/gpu/cl/kernels/ClGemmReshapeLhsMatrixKernel.h"
40#include "src/gpu/cl/kernels/ClGemmReshapeRhsMatrixKernel.h"
Georgios Pinitas856f66e2021-04-22 21:13:21 +010041
42#include <memory>
43
44namespace arm_compute
45{
46namespace opencl
47{
48/** Basic function to execute GEMM on OpenCL. This function calls the following OpenCL kernels:
49 *
Gian Marco Iodicec9cecc02021-10-15 10:23:24 +010050 * -# @ref kernels::ClGemmReshapeLhsMatrixKernel (only if the RESHAPED is selected by the heuristic model)
51 * -# @ref kernels::ClGemmReshapeRhsMatrixKernel (only if either the RESHAPED or RESHAPED_ONLY_RHS is selected by the select_gemm_kernel method())
52 * -# @ref kernels::ClGemmMatrixMultiplyNativeKernel (only if NATIVE is selected by the select_gemm_kernel method())
53 * -# @ref kernels::ClGemmMatrixMultiplyReshapedKernel (only if RESHAPED is selected by the select_gemm_kernel method())
Georgios Pinitas856f66e2021-04-22 21:13:21 +010054 * -# @ref kernels::ClGemmMatrixMultiplyReshapedOnlyRhsKernel (only if RESHAPED_ONLY_RHS is selected by the select_gemm_kernel method())
Gunes Bayir4bfc70e2021-12-10 16:17:56 +000055 * -# @ref kernels::ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel (only if RESHAPED_ONLY_RHS_MMUL is selected by the select_gemm_kernel method())
Georgios Pinitas856f66e2021-04-22 21:13:21 +010056 */
57class ClGemm : public IClOperator
58{
59public:
60 /** Constructor */
61 ClGemm();
62 /** Initialise the kernel's inputs and output
63 *
64 * Valid data layouts:
65 * - All
66 *
67 * Valid data type configurations:
68 * |src0 |src1 |src2 |dst |
69 * |:------------|:-----------|:---------|:--------------|
70 * |F32 |F32 |F32 |F32 |
71 * |F16 |F16 |F16 |F16 |
72 *
73 * @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C].
74 *
75 * @note All tensors must have the same data type.
76 *
77 * @note Whilst the first input tensor can be a vector, the second input tensor must be at least a matrix
78 *
SiCong Li1b6377b2023-01-09 15:34:20 +000079 * @note Batched GEMM only allows RHS tensor's rank to be <= 3
80 * @note Batched GEMM only supports broadcasting cases where RHS rank < LHS rank but not the other way around
81 *
Georgios Pinitas856f66e2021-04-22 21:13:21 +010082 * @param[in] compile_context The compile context to be used.
83 * @param[in] a First input tensor (Matrix or Vector A). Data types supported: F16/F32
84 * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a.
85 * @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.
86 * @param[out] output Output tensor. Data type supported: same as @p a
87 * @param[in] alpha Weight of the matrix product
88 * @param[in] beta Weight of matrix C
89 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
SiCongLi579ca842021-10-18 09:38:33 +010090 * if the reshape of matrix B should happen only for the first run. GEMMInfo also contains information about the reshaping
91 * in case matrix A and matrix B have been already transformed.
Georgios Pinitas856f66e2021-04-22 21:13:21 +010092 */
93 void configure(const CLCompileContext &compile_context, ITensorInfo *a, ITensorInfo *b, ITensorInfo *c, ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info);
94 /** Static function to check if given info will lead to a valid configuration
95 *
96 * Similar to ClGemm::configure()
97 *
98 * @return a status
99 */
100 static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info);
101
102 // Inherited methods overridden:
103 void run(ITensorPack &tensors) override;
104 void prepare(ITensorPack &constants) override;
105 experimental::MemoryRequirements workspace() const override;
106
107private:
Gian Marco Iodicec9cecc02021-10-15 10:23:24 +0100108 void configure_native(const CLCompileContext &compile_context, ITensorInfo *a, ITensorInfo *b, ITensorInfo *c, ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info);
109 void configure_reshaped(const CLCompileContext &compile_context, ITensorInfo *a, ITensorInfo *b, ITensorInfo *c, ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100110 void configure_reshaped_only_rhs(const CLCompileContext &compile_context, ITensorInfo *a, ITensorInfo *b, ITensorInfo *c, ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info);
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000111 void configure_reshaped_only_rhs_mmul(const CLCompileContext &compile_context, ITensorInfo *a, ITensorInfo *b, ITensorInfo *c, ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100112
Gian Marco Iodicec9cecc02021-10-15 10:23:24 +0100113 static Status validate_native(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100114 static Status validate_reshaped(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info);
115 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);
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000116 static Status validate_reshaped_only_rhs_mmul(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info);
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100117
118private:
119 enum AuxTensorIdx
120 {
121 LhsReshape = 0,
122 RhsReshape,
123 Count
124 };
125
126private:
Gunes Bayir4bfc70e2021-12-10 16:17:56 +0000127 std::unique_ptr<kernels::ClGemmReshapeLhsMatrixKernel> _reshape_lhs_kernel;
128 std::unique_ptr<kernels::ClGemmReshapeRhsMatrixKernel> _reshape_rhs_kernel;
129 std::unique_ptr<kernels::ClGemmMatrixMultiplyNativeKernel> _mm_native_kernel;
130 std::unique_ptr<kernels::ClGemmMatrixMultiplyReshapedKernel> _mm_reshaped_kernel;
131 std::unique_ptr<kernels::ClGemmMatrixMultiplyReshapedOnlyRhsKernel> _mm_reshaped_only_rhs_kernel;
132 std::unique_ptr<kernels::ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel> _mm_reshaped_only_rhs_mmul_kernel;
133 TensorInfo _tmp_a;
134 TensorInfo _tmp_b;
135 bool _reshape_b_only_on_first_run;
136 CLGEMMKernelType _gemm_kernel_type;
137 bool _is_prepared;
138 experimental::MemoryRequirements _aux_mem{};
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100139};
140} // namespace opencl
141} // namespace arm_compute
142#endif /* ARM_COMPUTE_CLGEMM_H */