xref: /aosp_15_r20/external/ComputeLibrary/src/gpu/cl/kernels/ClGemmLowpOffsetContributionKernel.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_CL_GEMMLOWP_OFFSET_CONTRIBUTION_KERNEL_H
25 #define ARM_COMPUTE_CL_GEMMLOWP_OFFSET_CONTRIBUTION_KERNEL_H
26 
27 #include "src/core/common/Macros.h"
28 #include "src/gpu/cl/ClCompileContext.h"
29 #include "src/gpu/cl/IClKernel.h"
30 
31 namespace arm_compute
32 {
33 namespace opencl
34 {
35 namespace kernels
36 {
37 /** OpenCL kernel used to add the offset contribution after the matrix multiplication. The computation is performed in-place
38  *
39  * This kernel takes a final int32 accumulator value (the output of the matrix multiplication),
40  * and adds to it the offset contribution of matrix A and matrix B in-place.
41  *
42  * The final result is:
43  *
44  * mm_result[i][k] = mm_result[i][k] +
45  *                   (vector_sum_col[k] * a_offset) +
46  *                   (vector_sum_row[i] * b_offset) +
47  *                   (a_offset * b_offset * k)
48  *
49  */
50 class ClGemmLowpOffsetContributionKernel : public IClKernel
51 {
52 public:
53     ClGemmLowpOffsetContributionKernel();
54     ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClGemmLowpOffsetContributionKernel);
55     /** Initialise the kernel's input and output.
56      *
57      * @param[in]      compile_context The compile context to be used.
58      * @param[in, out] mm_result       Input tensor containing the result of the matrix multiplication. Data type supported: S32
59      * @param[in]      vector_sum_col  Input row-vector of sums of all the entries in each column of matrix B.
60      *                                 Note: vector_sum_col can be a nullptr in case a_offset = 0. Data type supported: same as @p mm_result
61      * @param[in]      vector_sum_row  Input row-vector of sums of all the entries in each row of matrix A.
62      *                                 Note: vector_sum_row can be a nullptr in case b_offset = 0. Data type supported: same as @p mm_result
63      * @param[in]      bias            Biases tensor. Only shared biases supported and it can be a nullptr if the addition of biases is not required.
64      *                                 Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p input.
65      * @param[in]      k               Number of matrix A columns or Matrix B rows
66      * @param[in]      a_offset        Offset to be added to each element of the matrix A.
67      * @param[in]      b_offset        Offset to be added to each element of the matrix B.
68      */
69     void configure(const CLCompileContext &compile_context,
70                    const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias,
71                    int32_t k, int32_t a_offset, int32_t b_offset);
72     /** Static function to check if given info will lead to a valid configuration
73      *
74      * Similar to @ref ClGemmLowpOffsetContributionKernel::configure()
75      *
76      * @return a status
77      */
78     static Status validate(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias, int32_t a_offset, int32_t b_offset);
79 
80     // Inherited methods overridden:
81     void run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) override;
82 };
83 } // namespace kernels
84 } // namespace opencl
85 } // namespace arm_compute
86 #endif /* ARM_COMPUTE_CL_GEMMLOWP_OFFSET_CONTRIBUTION_KERNEL_H */
87