blob: 60bb78c3713ed4b3a799b825ebded3818201fb6c [file] [log] [blame]
Georgios Pinitas856f66e2021-04-22 21:13:21 +01001/*
2 * Copyright (c) 2016-2021 Arm Limited.
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_GEMM_H
25#define ARM_COMPUTE_CL_GEMM_H
26
27#include "arm_compute/core/TensorInfo.h"
28#include "arm_compute/runtime/CL/CLTensor.h"
29#include "arm_compute/runtime/CL/CLTypes.h"
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +010030
Georgios Pinitas7891a732021-08-20 21:39:25 +010031#include "src/gpu/cl/ClCompileContext.h"
32#include "src/gpu/cl/IClKernel.h"
33#include "src/gpu/cl/IClOperator.h"
34#include "src/gpu/cl/kernels/ClGemmMatrixMultiplyKernel.h"
35#include "src/gpu/cl/kernels/ClGemmMatrixMultiplyNativeKernel.h"
36#include "src/gpu/cl/kernels/ClGemmMatrixMultiplyReshapedKernel.h"
37#include "src/gpu/cl/kernels/ClGemmMatrixMultiplyReshapedOnlyRhsKernel.h"
38#include "src/gpu/cl/kernels/ClGemmReshapeLhsMatrixKernel.h"
39#include "src/gpu/cl/kernels/ClGemmReshapeRhsMatrixKernel.h"
Georgios Pinitas856f66e2021-04-22 21:13:21 +010040
41#include <memory>
42
43namespace arm_compute
44{
45namespace opencl
46{
47/** Basic function to execute GEMM on OpenCL. This function calls the following OpenCL kernels:
48 *
49 * -# @ref kernels::ClGemmReshapeLhsMatrixKernel (only if the RESHAPED_V1 is selected by the heuristic model)
50 * -# @ref kernels::ClGemmReshapeRhsMatrixKernel (only if either the RESHAPED_V1 or RESHAPED_ONLY_RHS is selected by the select_gemm_kernel method())
51 * -# @ref kernels::ClGemmMatrixMultiplyKernel (only if either the NATIVE or RESHAPED_V1 is selected by the select_gemm_kernel method())
52 * -# @ref kernels::ClGemmMatrixMultiplyReshapedKernel (only if RESHAPED_V1 is selected by the select_gemm_kernel method())
53 * -# @ref kernels::ClGemmMatrixMultiplyReshapedOnlyRhsKernel (only if RESHAPED_ONLY_RHS is selected by the select_gemm_kernel method())
54 */
55class ClGemm : public IClOperator
56{
57public:
58 /** Constructor */
59 ClGemm();
60 /** Initialise the kernel's inputs and output
61 *
62 * Valid data layouts:
63 * - All
64 *
65 * Valid data type configurations:
66 * |src0 |src1 |src2 |dst |
67 * |:------------|:-----------|:---------|:--------------|
68 * |F32 |F32 |F32 |F32 |
69 * |F16 |F16 |F16 |F16 |
70 *
71 * @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C].
72 *
73 * @note All tensors must have the same data type.
74 *
75 * @note Whilst the first input tensor can be a vector, the second input tensor must be at least a matrix
76 *
77 * @param[in] compile_context The compile context to be used.
78 * @param[in] a First input tensor (Matrix or Vector A). Data types supported: F16/F32
79 * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a.
80 * @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.
81 * @param[out] output Output tensor. Data type supported: same as @p a
82 * @param[in] alpha Weight of the matrix product
83 * @param[in] beta Weight of matrix C
84 * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
85 * if the reshape of matrix B should happen only for the first run. GEMMInfo also contains information about the reshaping
86 * in case matrix A and matrix B have been already transformed.
87 */
88 void configure(const CLCompileContext &compile_context, ITensorInfo *a, ITensorInfo *b, ITensorInfo *c, ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info);
89 /** Static function to check if given info will lead to a valid configuration
90 *
91 * Similar to ClGemm::configure()
92 *
93 * @return a status
94 */
95 static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info);
96
97 // Inherited methods overridden:
98 void run(ITensorPack &tensors) override;
99 void prepare(ITensorPack &constants) override;
100 experimental::MemoryRequirements workspace() const override;
101
102private:
103 void configure_native_v1(const CLCompileContext &compile_context, ITensorInfo *a, ITensorInfo *b, ITensorInfo *c, ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info);
104 void configure_reshaped_v1(const CLCompileContext &compile_context, ITensorInfo *a, ITensorInfo *b, ITensorInfo *c, ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info);
105 void configure_reshaped_v2(const CLCompileContext &compile_context, ITensorInfo *a, ITensorInfo *b, ITensorInfo *c, ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info);
106 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);
107
108 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);
109 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);
110 static Status validate_reshaped(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info);
111 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);
112
113private:
114 enum AuxTensorIdx
115 {
116 LhsReshape = 0,
117 RhsReshape,
118 Count
119 };
120
121private:
122 std::unique_ptr<kernels::ClGemmMatrixMultiplyKernel> _mm_kernel;
123 std::unique_ptr<kernels::ClGemmReshapeLhsMatrixKernel> _reshape_lhs_kernel;
124 std::unique_ptr<kernels::ClGemmReshapeRhsMatrixKernel> _reshape_rhs_kernel;
125 std::unique_ptr<kernels::ClGemmMatrixMultiplyReshapedKernel> _mm_reshaped_kernel;
126 std::unique_ptr<kernels::ClGemmMatrixMultiplyReshapedOnlyRhsKernel> _mm_reshaped_only_rhs_kernel;
127 std::unique_ptr<kernels::ClGemmMatrixMultiplyReshapedOnlyRhsKernel> _mm_reshaped_only_rhs_fallback_kernel;
128 TensorInfo _tmp_a;
129 TensorInfo _tmp_b;
130 bool _reshape_b_only_on_first_run;
131 CLGEMMKernelType _gemm_kernel_type;
Manuel Bottinid87aded2021-07-16 10:23:31 +0100132 bool _is_prepared;
133 experimental::MemoryRequirements _aux_mem{};
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100134};
135} // namespace opencl
136} // namespace arm_compute
137#endif /* ARM_COMPUTE_CLGEMM_H */