xref: /aosp_15_r20/external/ComputeLibrary/arm_compute/runtime/CL/functions/CLGEMMLowpMatrixMultiplyCore.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-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_CLGEMMLOWPMATRIXMULTIPLYCORE_H
25 #define ARM_COMPUTE_CLGEMMLOWPMATRIXMULTIPLYCORE_H
26 
27 #include "arm_compute/runtime/CL/CLTensor.h"
28 #include "arm_compute/runtime/IFunction.h"
29 #include "arm_compute/runtime/MemoryGroup.h"
30 
31 #include <memory>
32 
33 namespace arm_compute
34 {
35 class CLCompileContext;
36 class IMemoryManager;
37 class ICLTensor;
38 class ITensorInfo;
39 
40 /** Basic function to execute GEMMLowpMatrixMultiplyCore on OpenCL. */
41 class CLGEMMLowpMatrixMultiplyCore : public IFunction
42 {
43 public:
44     /** Constructor */
45     CLGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
46     /** Prevent instances of this class from being copied (As this class contains pointers) */
47     CLGEMMLowpMatrixMultiplyCore(const CLGEMMLowpMatrixMultiplyCore &) = delete;
48     /** Default move constructor */
49     CLGEMMLowpMatrixMultiplyCore(CLGEMMLowpMatrixMultiplyCore &&) = default;
50     /** Prevent instances of this class from being copied (As this class contains pointers) */
51     CLGEMMLowpMatrixMultiplyCore &operator=(const CLGEMMLowpMatrixMultiplyCore &) = delete;
52     /** Default move assignment operator */
53     CLGEMMLowpMatrixMultiplyCore &operator=(CLGEMMLowpMatrixMultiplyCore &&) = default;
54     /** Default destructor */
55     ~CLGEMMLowpMatrixMultiplyCore();
56     /** Initialise the kernel's inputs, output
57      *
58      * Valid data layouts:
59      * - NHWC
60      * - NCHW
61      *
62      * Valid data type configurations:
63      * |src0           |src1               |src2     |dst            |
64      * |:--------------|:------------------|:--------|:--------------|
65      * |QASYMM8        |QASYMM8            |S32      |QASYMM8        |
66      * |QASYMM8        |QSYMM8_PER_CHANNEL |S32      |QASYMM8        |
67      * |QASYMM8        |QSYMM8             |S32      |QASYMM8        |
68      * |QASYMM8        |QASYMM8            |S32      |S32            |
69      * |QASYMM8        |QSYMM8_PER_CHANNEL |S32      |S32            |
70      * |QASYMM8        |QSYMM8             |S32      |S32            |
71      * |QASYMM8_SIGNED |QASYMM8_SIGNED     |S32      |QASYMM8_SIGNED |
72      * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32      |QASYMM8_SIGNED |
73      * |QASYMM8_SIGNED |QSYMM8             |S32      |QASYMM8_SIGNED |
74      * |QASYMM8_SIGNED |QASYMM8_SIGNED     |S32      |S32            |
75      * |QASYMM8_SIGNED |QSYMM8_PER_CHANNEL |S32      |S32            |
76      * |QASYMM8_SIGNED |QSYMM8             |S32      |S32            |
77      *
78      * @note GEMMLowp:  low precision GEMM kernel. [A * B + C]
79      *  This kernel performs the following computations:
80      *
81      *  -# Convert a values from 8-bit quantized to int32 and add a_offset to each of them.
82      *  -# Convert b values from 8-bit quantized to int32 and add b_offset to each of them.
83      *  -# Compute the matrix product of the resulting a * b in int32.
84      *  -# Quantize to uint8 if gemm_info.gemmlowp_output_stage != NONE
85      *
86      * @param[in]  a         First input tensor  (Matrix A). Data type supported: QASYMM8/QASYMM8_SIGNED.
87      * @param[in]  b         Second input tensor (Matrix B). Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8/QSYMM8_PER_CHANNEL
88      * @param[in]  c         Third input tensor  (Matrix C). It can be a nullptr. Data type supported: S32
89      * @param[out] output    Output tensor. Data type supported: S32 or QASYMM8/QASYMM8_SIGNED if gemm_info.gemmlowp_output_stage != NONE
90      * @param[in]  gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
91      *                       if the reshape of matrix B should be executed only for the first run
92      */
93     void configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, const GEMMInfo &gemm_info = GEMMInfo());
94     /** Initialise the kernel's inputs, output
95      *
96      * @note GEMMLowp:  low precision GEMM kernel. [A * B + C]
97      *  This kernel performs the following computations:
98      *
99      *  -# Convert a values from 8-bit quantized to int32 and add a_offset to each of them.
100      *  -# Convert b values from 8-bit quantized to int32 and add b_offset to each of them.
101      *  -# Compute the matrix product of the resulting a * b in int32.
102      *  -# Quantize to uint8 if gemm_info.gemmlowp_output_stage != NONE
103      *
104      * @param[in]  compile_context The compile context to be used.
105      * @param[in]  a               First input tensor  (Matrix A). Data type supported: QASYMM8/QASYMM8_SIGNED.
106      * @param[in]  b               Second input tensor (Matrix B). Data type supported: same as @p a
107      * @param[in]  c               Third input tensor  (Matrix C). It can be a nullptr. Data type supported: S32
108      * @param[out] output          Output tensor. Data type supported: S32 or QASYMM8/QASYMM8_SIGNED if gemm_info.gemmlowp_output_stage != NONE
109      * @param[in]  gemm_info       (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
110      *                       if the reshape of matrix B should be executed only for the first run
111      */
112     void configure(const CLCompileContext &compile_context, const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, const GEMMInfo &gemm_info = GEMMInfo());
113     /** Static function to check if given info will lead to a valid configuration of @ref CLGEMMLowpMatrixMultiplyCore
114      *
115      * @param[in] a         First input tensor info (Matrix A). Data type supported: QASYMM8.
116      * @param[in] b         Second input tensor info (Matrix B). Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM8/QSYMM8_PER_CHANNEL
117      * @param[in] c         Third input tensor info (Matrix C). It can be a nullptr. Data type supported: S32
118      * @param[in] output    Output tensor info. Data type supported: S32 or QASYMM8/QASYMM8_SIGNED if gemm_info.gemmlowp_output_stage != NONE
119      * @param[in] gemm_info (Optional) Specifies if the matrix A and/or matrix B have been reshaped and
120      *                      if the reshape of matrix B should be executed only for the first run
121      *
122      * @return a status
123      */
124     static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, const GEMMInfo &gemm_info = GEMMInfo());
125 
126     // Inherited methods overridden:
127     void run() override;
128     void prepare() override;
129 
130 private:
131     struct Impl;
132     std::unique_ptr<Impl> _impl;
133 };
134 } // namespace arm_compute
135 #endif /*ARM_COMPUTE_CLGEMMLOWPMATRIXMULTIPLYCORE_H */