1 /*
2  * Copyright (c) 2019-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_CPU_GEMMLOWP_OFFSETCONTRIBUTION_OUTPUTSTAGE_KERNEL_H
25 #define ARM_COMPUTE_CPU_GEMMLOWP_OFFSETCONTRIBUTION_OUTPUTSTAGE_KERNEL_H
26 
27 #include "arm_compute/core/KernelDescriptors.h"
28 #include "src/core/common/Macros.h"
29 #include "src/cpu/ICpuKernel.h"
30 
31 namespace arm_compute
32 {
33 namespace cpu
34 {
35 namespace kernels
36 {
37 /** Kernel used to add the offset contribution and perform the output stage after @ref CpuGemmLowpMatrixMultiplyKernel.
38  *
39  * The computation is performed in-place
40  *
41  * This kernel takes a final int32 accumulator value (the output of @ref CpuGemmLowpMatrixMultiplyKernel),
42  * and adds to it the offset contribution of matrix A and matrix B in-place.
43  *
44  * The output stage can perform either QuantizeDownInt32ToUint8Scale or QuantizeDownInt32ToUint8ScaleByFixedPoint for Uint8.
45  * The output stage can perform either QuantizeDownInt32ToInt8Scale or QuantizeDownInt32ToInt8ScaleByFixedPoint for Int8.
46  *
47  * For QuantizeDownInt32ToUint8Scale/QuantizeDownInt32ToInt8Scale the final result is:
48  *
49  * ((mm_result'[i][k] + result_offset) * result_mult_int) >> result_shift
50  *
51  * For QuantizeDownInt32ToUint8ScaleByFixedPoint/QuantizeDownInt32ToInt8ScaleByFixedPoint the final result is:
52  *
53  * (FixedPointMul(mm_result'[i][k], result_fixedpoint_multiplier) >> result_shift) + result_offset_after_shift
54  *
55  * where FixedPointMul(x, y) is the nearest integer to the following
56  * mathematical expression, evaluated without overflow or intermediate rounding:
57  *
58  * (x * y) / 2^31
59  *
60  * and mm_result'[i][k] = mm_result[i][k] +
61  *                        (vector_sum_col[k] * a_offset) +
62  *                        (vector_sum_row[i] * b_offset) +
63  *                        (a_offset * b_offset * k)
64  */
65 
66 class CpuGemmLowpOffsetContributionOutputStageKernel : public ICpuKernel<CpuGemmLowpOffsetContributionOutputStageKernel>
67 {
68 public:
69     /** Default constructor */
70     CpuGemmLowpOffsetContributionOutputStageKernel() = default;
71     ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(CpuGemmLowpOffsetContributionOutputStageKernel);
72     /** Initialise the kernel inputs and output.
73      *
74      * @param[in]  mm_result      Input tensor info containing the result of @ref CpuGemmLowpMatrixMultiplyKernel. Data type supported: S32
75      * @param[in]  vector_sum_col Input row-vector tensor info of sums of all the entries in each column of matrix B.
76      *                            Can be a 1D or 2D tensor, in case of 2D, y dim is the batch dimension
77      *                            Note: vector_sum_col can be a nullptr in case a_offset = 0. Data type supported: same as @p mm_result
78      * @param[in]  vector_sum_row Input row-vector tensor info of sums of all the entries in each row of matrix A.
79      *                            Can be a 1D or 2D tensor, in case of 2D, y dim is the batch dimension
80      * @param[in]  bias           Biases tensor info. Only shared biases supported and it can be a nullptr if the addition of biases is not required.
81      *                            Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p mm_result.
82      * @param[out] dst            Output tensor info containing the final quantized result. Data type supported: QASYMM8/QASYMM8_SIGNED
83      * @param[in]  k              Number of matrix A columns or Matrix B rows
84      * @param[in]  a_offset       Offset to be added to each element of the matrix A.
85      * @param[in]  b_offset       Offset to be added to each element of the matrix B.
86      * @param[in]  output_stage   GEMMLowp output stage info, providing the type of quantization and the necessary parameters.
87      */
88     void configure(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias, ITensorInfo *dst, int32_t k, int32_t a_offset,
89                    int32_t                 b_offset,
90                    GEMMLowpOutputStageInfo output_stage);
91     /** Static function to check if given info will lead to a valid configuration
92      *
93      * Similar to CpuGemmLowpOffsetContributionOutputStageKernel::configure()
94      *
95      * @return a status
96      */
97     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,
98                            int32_t                 b_offset,
99                            GEMMLowpOutputStageInfo output_stage);
100 
101     // Inherited methods overridden:
102     void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override;
103     const char *name() const override;
104 
105 private:
106     /** Function to use for the particular tensors passed to configure() */
107     int32_t                 _a_offset{ 0 };
108     int32_t                 _b_offset{ 0 };
109     int32_t                 _k_offset{ 0 };
110     bool                    _is_vector_sum_col_batched{ true };
111     GEMMLowpOutputStageInfo _output_stage{ GEMMLowpOutputStageInfo() };
112 };
113 } // namespace kernels
114 } // namespace cpu
115 } // namespace arm_compute
116 #endif /* ARM_COMPUTE_CPU_GEMMLOWP_OFFSETCONTRIBUTION_OUTPUTSTAGE_KERNEL_H */
117