blob: dadc5c221b2600fe3f36a11356f0541f98c7e749 [file] [log] [blame]
George Wort2d7e6832019-02-22 16:37:41 +00001/*
2 * Copyright (c) 2019 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_NEGEMMLOWPOFFSETCONTRIBUTIONOUTPUTSTAGEKERNEL_H__
25#define __ARM_COMPUTE_NEGEMMLOWPOFFSETCONTRIBUTIONOUTPUTSTAGEKERNEL_H__
26
27#include "arm_compute/core/NEON/INEKernel.h"
28
29namespace arm_compute
30{
31class ITensor;
32
33/** NEON kernel used to add the offset contribution and perform the output stage after @ref NEGEMMLowpMatrixMultiplyKernel.
34 *
35 * The computation is performed in-place
36 *
37 * This kernel takes a final int32 accumulator value (the output of @ref NEGEMMLowpMatrixMultiplyKernel),
38 * and adds to it the offset contribution of matrix A and matrix B in-place.
39 *
40 * The output stage can perform either QuantizeDownInt32ToUint8Scale or QuantizeDownInt32ToUint8ScaleByFixedPoint.
41 *
42 * For QuantizeDownInt32ToUint8Scale the final result is:
43 *
44 * ((mm_result'[i][k] + result_offset) * result_mult_int) >> result_shift
45 *
46 * For QuantizeDownInt32ToUint8ScaleByFixedPoint the final result is:
47 *
48 * (FixedPointMul(mm_result'[i][k], result_fixedpoint_multiplier) >> result_shift) + result_offset_after_shift
49 *
50 * where FixedPointMul(x, y) is the nearest integer to the following
51 * mathematical expression, evaluated without overflow or intermediate rounding:
52 *
53 * (x * y) / 2^31
54 *
55 * and mm_result'[i][k] = mm_result[i][k] +
56 * (vector_sum_col[k] * a_offset) +
57 * (vector_sum_row[i] * b_offset) +
58 * (a_offset * b_offset * k)
59 */
60
61class NEGEMMLowpOffsetContributionOutputStageKernel : public INEKernel
62{
63public:
64 const char *name() const override
65 {
66 return "NEGEMMLowpOffsetContributionOutputStageKernel";
67 }
68 /** Constructor */
69 NEGEMMLowpOffsetContributionOutputStageKernel();
70 /** Prevent instances of this class from being copied (As this class contains pointers)*/
71 NEGEMMLowpOffsetContributionOutputStageKernel(const NEGEMMLowpOffsetContributionOutputStageKernel &) = delete;
72 /** Prevent instances of this class from being copied (As this class contains pointers)*/
73 NEGEMMLowpOffsetContributionOutputStageKernel &operator=(const NEGEMMLowpOffsetContributionOutputStageKernel &) = delete;
74 /** Allow instances of this class to be moved */
75 NEGEMMLowpOffsetContributionOutputStageKernel(NEGEMMLowpOffsetContributionOutputStageKernel &&) = default;
76 /** Allow instances of this class to be moved */
77 NEGEMMLowpOffsetContributionOutputStageKernel &operator=(NEGEMMLowpOffsetContributionOutputStageKernel &&) = default;
78 /** Initialise the kernel's input and output.
79 *
80 * @param[in] mm_result Input tensor containing the result of @ref NEGEMMLowpMatrixMultiplyKernel. Data type supported: S32
81 * @param[in] vector_sum_col Input row-vector of sums of all the entries in each column of matrix B.
82 * Note: vector_sum_col can be a nullptr in case a_offset = 0. Data type supported: same as @p mm_result
83 * @param[in] vector_sum_row Input row-vector of sums of all the entries in each row of matrix A.
84 * @param[in] bias Biases tensor. Only shared biases supported and it can be a nullptr if the addition of biases is not required.
85 * Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p mm_result.
Georgios Pinitas448a81f2019-11-21 14:10:25 +000086 * @param[out] output Output tensor containing the final quantized result. Data type supported: QASYMM8/QASYMM8_SIGNED
George Wort2d7e6832019-02-22 16:37:41 +000087 * @param[in] k Number of matrix A columns or Matrix B rows
88 * @param[in] a_offset Offset to be added to each element of the matrix A.
89 * @param[in] b_offset Offset to be added to each element of the matrix B.
90 * @param[in] output_stage GEMMLowp output stage info, providing the type of quantization and the necessary parameters.
91 */
92 void configure(const ITensor *mm_result, const ITensor *vector_sum_col, const ITensor *vector_sum_row, const ITensor *bias, ITensor *output, int32_t k, int32_t a_offset, int32_t b_offset,
93 GEMMLowpOutputStageInfo output_stage);
94 /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMLowpOffsetContributionOutputStageKernel
95 *
96 * @param[in] mm_result Input tensor info containing the result of @ref NEGEMMLowpMatrixMultiplyKernel. Data type supported: S32
97 * @param[in] vector_sum_col Tensor info for the input row-vector of sums of all the entries in each column of matrix B.
98 * Note: vector_sum_col can be a nullptr in case a_offset = 0. Data type supported: same as @p mm_result
99 * @param[in] vector_sum_row Tensor info for the input row-vector of sums of all the entries in each row of matrix A.
100 * Note: vector_sum_row can be a nullptr in case b_offset = 0. Data type supported: same as @p mm_result
101 * @param[in] bias Biases tensor info. Only shared biases supported and it can be a nullptr if the addition of biases is not required.
102 * Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p mm_result.
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000103 * @param[in] output Output tensor info containing the final quantized result. Data type supported: QASYMM8/QASYMM8_SIGNED
George Wort2d7e6832019-02-22 16:37:41 +0000104 * @param[in] a_offset Offset to be added to each element of the matrix A.
105 * @param[in] b_offset Offset to be added to each element of the matrix B.
106 * @param[in] output_stage GEMMLowp output stage info, providing the type of quantization and the necessary parameters.
107 *
108 * @return a status
109 */
110 static Status validate(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias, const ITensorInfo *output, int32_t a_offset,
111 int32_t b_offset,
112 GEMMLowpOutputStageInfo output_stage);
113
114 // Inherited methods overridden:
115 void run(const Window &window, const ThreadInfo &info) override;
116
117 using NEGEMMLowpOffsetContributionOutputStageFunction = std::function<void(const Window, const ITensor *, const ITensor *, const ITensor *, const ITensor *,
118 ITensor *, int32_t, int32_t, int32_t, bool, GEMMLowpOutputStageInfo)>;
119
120private:
121 /** Function to use for the particular tensors passed to configure() */
122 NEGEMMLowpOffsetContributionOutputStageFunction _function;
123 const ITensor *_vector_sum_col;
124 const ITensor *_vector_sum_row;
125 const ITensor *_bias;
126 const ITensor *_mm_result;
127 ITensor *_output;
128 int32_t _a_offset;
129 int32_t _b_offset;
130 int32_t _k_offset;
131 bool _slide_vector_sum_col;
132 GEMMLowpOutputStageInfo _output_stage;
133};
134} // namespace arm_compute
135
136#endif /* __ARM_COMPUTE_NEGEMMLOWPOFFSETCONTRIBUTIONOUTPUTSTAGEKERNEL_H__ */