blob: 3cb99faee8f0164281389cdf9110f5205dacccf4 [file] [log] [blame]
Manuel Bottinicfac51c2021-06-18 15:47:28 +01001/*
Giorgio Arena5ae8d802021-11-18 18:02:13 +00002 * Copyright (c) 2019-2022 Arm Limited.
Manuel Bottinicfac51c2021-06-18 15:47:28 +01003 *
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"
Georgios Pinitas7891a732021-08-20 21:39:25 +010029#include "src/cpu/ICpuKernel.h"
Manuel Bottinicfac51c2021-06-18 15:47:28 +010030
31namespace arm_compute
32{
33namespace cpu
34{
35namespace 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
Yair Schwarzbaum46d44d22022-01-12 16:38:58 +020066class CpuGemmLowpOffsetContributionOutputStageKernel : public ICpuKernel<CpuGemmLowpOffsetContributionOutputStageKernel>
Manuel Bottinicfac51c2021-06-18 15:47:28 +010067{
68public:
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.
Mohammed Suhail Munshi5b9d2232023-01-25 11:51:50 +000076 * Can be a 1D or 2D tensor, in case of 2D, y dim is the batch dimension
Manuel Bottinicfac51c2021-06-18 15:47:28 +010077 * 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.
Mohammed Suhail Munshi5b9d2232023-01-25 11:51:50 +000079 * Can be a 1D or 2D tensor, in case of 2D, y dim is the batch dimension
Manuel Bottinicfac51c2021-06-18 15:47:28 +010080 * @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,
Georgios Pinitas7891a732021-08-20 21:39:25 +010089 int32_t b_offset,
Manuel Bottinicfac51c2021-06-18 15:47:28 +010090 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
105private:
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 };
Mohammed Suhail Munshi5b9d2232023-01-25 11:51:50 +0000110 bool _is_vector_sum_col_batched{ true };
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100111 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 */