1 /*
2  * Copyright (c) 2018-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_OUTPUT_STAGE_KERNEL_H
25 #define ARM_COMPUTE_CL_GEMMLOWP_OFFSET_CONTRIBUTION_OUTPUT_STAGE_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 and perform the output stage.
38  *
39  * This kernel takes a final int32 accumulator value (the output of the matrix multiplication), adds to it the offset contribution
40  * of matrix A and matrix B and performs the output stage defined by the output_stage argument
41  *
42  * @note For quantized computations the output data type for auto-initialization must be passed as part of the @ref GEMMLowpOutputStageInfo.
43  */
44 class ClGemmLowpOffsetContributionOutputStageKernel : public IClKernel
45 {
46 public:
47     ClGemmLowpOffsetContributionOutputStageKernel();
48     ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(ClGemmLowpOffsetContributionOutputStageKernel);
49     /** Initialise the kernel's input and output.
50      *
51      * @param[in]  compile_context    The compile context to be used.
52      * @param[in]  mm_result          Input tensor containing the result of the matrix multiplication. Data type supported: S32
53      * @param[in]  vector_sum_col     Input row-vector of sums of all the entries in each column of matrix B.
54      *                                Note: vector_sum_col can be a nullptr in case a_offset = 0. Data type supported: same as @p mm_result
55      * @param[in]  vector_sum_row     Input row-vector of sums of all the entries in each row of matrix A.
56      *                                Note: vector_sum_row can be a nullptr in case b_offset = 0. Data type supported: same as @p mm_result
57      * @param[in]  bias               Biases tensor. Only shared biases supported and it can be a nullptr if the addition of biases is not required.
58      *                                Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p mm_result.
59      * @param[out] dst                Destination tensor. Data type supported: QASYMM8/QASYMM8_SIGNED.
60      * @param[in]  k                  Number of matrix A columns or Matrix B rows
61      * @param[in]  a_offset           Offset to be added to each element of the matrix A.
62      * @param[in]  b_offset           Offset to be added to each element of the matrix B.
63      * @param[in]  output_stage       GEMMLowp output stage info
64      * @param[in]  output_multipliers Output multipliers tensor. In case of per-channel quantization, the number of multipliers must be equal to the number of filters (OFM).
65      *                                Supported data types: S32
66      * @param[in]  output_shifts      Output shifts tensor. In case of per-channel quantization, the number of multipliers must be equal to the number of filters (OFM).
67      *                                Supported data types: S32
68      */
69     void configure(const CLCompileContext &compile_context, const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias, ITensorInfo *dst,
70                    int32_t k, int32_t a_offset, int32_t b_offset, const GEMMLowpOutputStageInfo &output_stage,
71                    const ITensorInfo *output_multipliers, const ITensorInfo *output_shifts);
72     /** Static function to check if given info will lead to a valid configuration
73      *
74      * Similar to @ref ClGemmLowpOffsetContributionOutputStageKernel::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, const ITensorInfo *dst, int32_t a_offset,
79                            int32_t b_offset, const GEMMLowpOutputStageInfo &output_stage, const ITensorInfo *output_multipliers, const ITensorInfo *output_shifts);
80 
81     // Inherited methods overridden:
82     void run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) override;
83 
84 private:
85     bool _is_quantized_per_channel{ false };
86 };
87 } // namespace kernels
88 } // namespace opencl
89 } // namespace arm_compute
90 #endif /* ARM_COMPUTE_CL_GEMMLOWP_OFFSET_CONTRIBUTION_OUTPUT_STAGE_KERNEL_H */
91