Compute Library
 22.11
ClGemm.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2022 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 
30 
32 #include "src/gpu/cl/IClKernel.h"
33 #include "src/gpu/cl/IClOperator.h"
40 
41 #include <memory>
42 
43 namespace arm_compute
44 {
45 namespace 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 is selected by the heuristic model)
50  * -# @ref kernels::ClGemmReshapeRhsMatrixKernel (only if either the RESHAPED or RESHAPED_ONLY_RHS is selected by the select_gemm_kernel method())
51  * -# @ref kernels::ClGemmMatrixMultiplyNativeKernel (only if NATIVE is selected by the select_gemm_kernel method())
52  * -# @ref kernels::ClGemmMatrixMultiplyReshapedKernel (only if RESHAPED 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  * -# @ref kernels::ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel (only if RESHAPED_ONLY_RHS_MMUL is selected by the select_gemm_kernel method())
55  */
56 class ClGemm : public IClOperator
57 {
58 public:
59  /** Constructor */
60  ClGemm();
61  /** Initialise the kernel's inputs and output
62  *
63  * Valid data layouts:
64  * - All
65  *
66  * Valid data type configurations:
67  * |src0 |src1 |src2 |dst |
68  * |:------------|:-----------|:---------|:--------------|
69  * |F32 |F32 |F32 |F32 |
70  * |F16 |F16 |F16 |F16 |
71  *
72  * @note GEMM: General Matrix Multiply - [alpha * A * B + beta * C].
73  *
74  * @note All tensors must have the same data type.
75  *
76  * @note Whilst the first input tensor can be a vector, the second input tensor must be at least a matrix
77  *
78  * @param[in] compile_context The compile context to be used.
79  * @param[in] a First input tensor (Matrix or Vector A). Data types supported: F16/F32
80  * @param[in] b Second input tensor (Matrix B). Data type supported: same as @p a.
81  * @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.
82  * @param[out] output Output tensor. Data type supported: same as @p a
83  * @param[in] alpha Weight of the matrix product
84  * @param[in] beta Weight of matrix C
85  * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
86  * if the reshape of matrix B should happen only for the first run. GEMMInfo also contains information about the reshaping
87  * in case matrix A and matrix B have been already transformed.
88  */
89  void configure(const CLCompileContext &compile_context, ITensorInfo *a, ITensorInfo *b, ITensorInfo *c, ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info);
90  /** Static function to check if given info will lead to a valid configuration
91  *
92  * Similar to ClGemm::configure()
93  *
94  * @return a status
95  */
96  static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info);
97 
98  // Inherited methods overridden:
99  void run(ITensorPack &tensors) override;
100  void prepare(ITensorPack &constants) override;
102 
103 private:
104  void configure_native(const CLCompileContext &compile_context, ITensorInfo *a, ITensorInfo *b, ITensorInfo *c, ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info);
105  void configure_reshaped(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  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);
108 
109  static Status validate_native(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  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);
113 
114 private:
115  enum AuxTensorIdx
116  {
117  LhsReshape = 0,
118  RhsReshape,
119  Count
120  };
121 
122 private:
123  std::unique_ptr<kernels::ClGemmReshapeLhsMatrixKernel> _reshape_lhs_kernel;
124  std::unique_ptr<kernels::ClGemmReshapeRhsMatrixKernel> _reshape_rhs_kernel;
125  std::unique_ptr<kernels::ClGemmMatrixMultiplyNativeKernel> _mm_native_kernel;
126  std::unique_ptr<kernels::ClGemmMatrixMultiplyReshapedKernel> _mm_reshaped_kernel;
127  std::unique_ptr<kernels::ClGemmMatrixMultiplyReshapedOnlyRhsKernel> _mm_reshaped_only_rhs_kernel;
128  std::unique_ptr<kernels::ClGemmMatrixMultiplyReshapedOnlyRhsMMULKernel> _mm_reshaped_only_rhs_mmul_kernel;
129  TensorInfo _tmp_a;
130  TensorInfo _tmp_b;
131  bool _reshape_b_only_on_first_run;
132  CLGEMMKernelType _gemm_kernel_type;
133  bool _is_prepared;
135 };
136 } // namespace opencl
137 } // namespace arm_compute
138 #endif /* ARM_COMPUTE_CLGEMM_H */
SimpleTensor< float > b
Definition: DFT.cpp:157
void configure(const CLCompileContext &compile_context, ITensorInfo *a, ITensorInfo *b, ITensorInfo *c, ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
Initialise the kernel&#39;s inputs and output.
Definition: ClGemm.cpp:557
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
Status class.
Definition: Error.h:52
CLGEMMKernelType
OpenCL GEMM kernel types.
Definition: CLTypes.h:31
Copyright (c) 2017-2022 Arm Limited.
std::vector< MemoryInfo > MemoryRequirements
Definition: Types.h:134
CLCompileContext class.
void run(ITensorPack &tensors) override
Run the kernels contained in the function.
Definition: ClGemm.cpp:663
Tensor packing service.
Definition: ITensorPack.h:39
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:43
void prepare(ITensorPack &constants) override
Prepare the function for executing.
Definition: ClGemm.cpp:771
GEMM information class.
Definition: Types.h:2339
static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
Static function to check if given info will lead to a valid configuration.
Definition: ClGemm.cpp:612
Basic interface for functions which have a single async CL kernel.
Definition: ICLOperator.h:41
ClGemm()
Constructor.
Definition: ClGemm.cpp:188
Basic function to execute GEMM on OpenCL.
Definition: ClGemm.h:56
experimental::MemoryRequirements workspace() const override
Return the memory requirements required by the workspace.
Definition: ClGemm.cpp:793