blob: f29d5d464bfaa4d36b866667478959e9a3f7d6b1 [file] [log] [blame]
Gian Marcoe75a02b2017-11-08 12:24:09 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Gian Marcoe75a02b2017-11-08 12:24:09 +00003 *
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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_NEGEMMLOWPOUTPUTSTAGE_H
25#define ARM_COMPUTE_NEGEMMLOWPOUTPUTSTAGE_H
Gian Marcoe75a02b2017-11-08 12:24:09 +000026
Michalis Spyrou95abfdd2018-11-28 14:59:47 +000027#include "arm_compute/runtime/NEON/INESimpleFunctionNoBorder.h"
Gian Marcoe75a02b2017-11-08 12:24:09 +000028
29/** This file contains all available output stages for GEMMLowp on NEON.
30 *
31 * In gemmlowp, the "output stage" is the process that takes a final int32 accumulator value (the output of @ref NEGEMMLowpMatrixMultiplyCore),
32 * and processes it to obtain the final ASYMM8 value.
33 *
34 * More information about the GEMMLowp output stage can be found at https://github.com/google/gemmlowp/blob/master/doc/output.md
35 */
36
37namespace arm_compute
38{
39class ITensor;
40
Gian Marco58c57942017-11-28 09:10:03 +000041/** Basic function to execute NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint on NEON.
42 *
43 * NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint depends on 3 parameters:
44 *
45 * result_fixedpoint_multiplier, result_shift, result_offset_after_shift
46 *
47 * The final result is:
48 *
49 * (FixedPointMul(input[i][k], result_fixedpoint_multiplier) >> result_shift) + result_offset_after_shift
50 *
51 * where FixedPointMul(x, y) is the nearest integer to the following
52 * mathematical expression, evaluated without overflow or intermediate rounding:
53 *
54 * (x * y) / 2^31
55 *
56 * For more information: https://github.com/google/gemmlowp/blob/master/public/output_stages.h#L68
57 *
58 * In case the bias tensor is provided, the final result is:
59 *
60 * ((FixedPointMul(input[i][k] + bias[k], result_fixedpoint_multiplier)) >> result_shift) + result_offset_after_shift
61 *
62 * This function calls the following NEON kernels:
63 *
64 * -# @ref NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel
65 *
66 * @note The function accepts also 2 optional input arguments (min and max) which can be used to implement "rectified linear unit" activation functions
67 * after the result is shifted right by result_shift
68*/
Michalis Spyrou95abfdd2018-11-28 14:59:47 +000069class NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint : public INESimpleFunctionNoBorder
Gian Marco58c57942017-11-28 09:10:03 +000070{
71public:
72 /** Initialise the kernel's inputs, output
73 *
74 * @param[in] input Input tensor. Data type supported: S32
75 * @param[in] bias Biases tensor. Only shared biases supported and it can be a nullptr if the biases addition is not required.
76 * Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p input.
77 * @param[out] output Output tensor. Data type supported: Data type supported: QASYMM8
78 * @param[in] result_fixedpoint_multiplier Fixed point value to be multiplied to each element of the input matrix when once the result_offset has been add
79 * @param[in] result_shift Number of bits to shift right the result after the fixed point multiplication
80 * @param[in] result_offset_after_shift Offset to be applied to result before converting it back to QASYMM8
Giorgio Arena1856ff72020-02-07 13:46:45 +000081 * @param[in] min (Optional) Min value used to saturate down the output result before converting back to QASYMM8. Defaults to the minimum possible 32-bit signed integer.
Gian Marco58c57942017-11-28 09:10:03 +000082 * @param[in] max (Optional) Max value used to saturate up the output result before converting back to QASYMM8,
Giorgio Arena1856ff72020-02-07 13:46:45 +000083 * Along with @p min, this value can be used to implement "rectified linear unit" activation functions. Defaults to the maximum possible 32-bit signed integer.
Gian Marco58c57942017-11-28 09:10:03 +000084 */
Giorgio Arena1856ff72020-02-07 13:46:45 +000085 void configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift,
86 int min = std::numeric_limits<int32_t>::lowest(), int max = std::numeric_limits<int32_t>::max());
Gian Marco58c57942017-11-28 09:10:03 +000087 /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint
88 *
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000089 * @param[in] input Input tensor. It is the output of @ref NEGEMMLowpMatrixMultiplyCore function. Data type supported: S32
90 * @param[in] bias Biases tensor. Only shared biases supported and it can be a nullptr if the addition of biases is not required.
91 * Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p input.
92 * @param[in] output Output tensor. Data type supported: Data type supported: QASYMM8
Giorgio Arena1856ff72020-02-07 13:46:45 +000093 * @param[in] min (Optional) Min value used to saturate down the output result before converting back to QASYMM8. Defaults to the minimum possible 32-bit signed integer.
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000094 * @param[in] max (Optional) Max value used to saturate up the output result before converting back to QASYMM8,
Giorgio Arena1856ff72020-02-07 13:46:45 +000095 * Along with @p min, this value can be used to implement "rectified linear unit" activation functions. Defaults to the maximum possible 32-bit signed integer.
Gian Marco58c57942017-11-28 09:10:03 +000096 *
Georgios Pinitas631c41a2017-12-06 11:53:03 +000097 * @return a status
Anthony Barbierf202e502017-11-23 18:02:04 +000098 */
Giorgio Arena1856ff72020-02-07 13:46:45 +000099 static Status validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min = std::numeric_limits<int32_t>::lowest(), int max = std::numeric_limits<int32_t>::max());
Gian Marcoe75a02b2017-11-08 12:24:09 +0000100};
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000101/** Basic function to execute NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPoint on NEON.
102 *
103 * NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPoint depends on 3 parameters:
104 *
105 * result_fixedpoint_multiplier, result_shift, result_offset_after_shift
106 *
107 * The final result is:
108 *
109 * (FixedPointMul(input[i][k], result_fixedpoint_multiplier) >> result_shift) + result_offset_after_shift
110 *
111 * where FixedPointMul(x, y) is the nearest integer to the following
112 * mathematical expression, evaluated without overflow or intermediate rounding:
113 *
114 * (x * y) / 2^31
115 *
116 * For more information: https://github.com/google/gemmlowp/blob/master/public/output_stages.h#L68
117 *
118 * In case the bias tensor is provided, the final result is:
119 *
120 * ((FixedPointMul(input[i][k] + bias[k], result_fixedpoint_multiplier)) >> result_shift) + result_offset_after_shift
121 *
122 * This function calls the following NEON kernels:
123 *
124 * -# @ref NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel
125 *
126 * @note The function accepts also 2 optional input arguments (min and max) which can be used to implement "rectified linear unit" activation functions
127 * after the result is shifted right by result_shift
128*/
129class NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPoint : public INESimpleFunctionNoBorder
130{
131public:
132 /** Initialise the kernel's inputs, output
133 *
134 * @param[in] input Input tensor. Data type supported: S32
135 * @param[in] bias Biases tensor. Only shared biases supported and it can be a nullptr if the biases addition is not required.
136 * Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p input.
137 * @param[out] output Output tensor. Data type supported: Data type supported: QASYMM8_SIGNED
138 * @param[in] result_fixedpoint_multiplier Fixed point value to be multiplied to each element of the input matrix when once the result_offset has been add
139 * @param[in] result_shift Number of bits to shift right the result after the fixed point multiplication
140 * @param[in] result_offset_after_shift Offset to be applied to result before converting it back to QASYMM8_SIGNED
Giorgio Arena1856ff72020-02-07 13:46:45 +0000141 * @param[in] min (Optional) Min value used to saturate down the output result before converting back to QASYMM8_SIGNED. Defaults to the minimum possible 32-bit signed integer.
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000142 * @param[in] max (Optional) Max value used to saturate up the output result before converting back to QASYMM8_SIGNED,
Giorgio Arena1856ff72020-02-07 13:46:45 +0000143 * Along with @p min, this value can be used to implement "rectified linear unit" activation functions. Defaults to the maximum possible 32-bit signed integer.
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000144 */
Giorgio Arena1856ff72020-02-07 13:46:45 +0000145 void configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift,
146 int min = std::numeric_limits<int32_t>::lowest(), int max = std::numeric_limits<int32_t>::max());
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000147 /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPoint
148 *
149 * @param[in] input Input tensor. It is the output of @ref NEGEMMLowpMatrixMultiplyCore function. Data type supported: S32
150 * @param[in] bias Biases tensor. Only shared biases supported and it can be a nullptr if the addition of biases is not required.
151 * Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p input.
152 * @param[in] output Output tensor. Data type supported: Data type supported: QASYMM8_SIGNED
Giorgio Arena1856ff72020-02-07 13:46:45 +0000153 * @param[in] min (Optional) Min value used to saturate down the output result before converting back to QASYMM8_SIGNED. Defaults to the minimum possible 32-bit signed integer.
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000154 * @param[in] max (Optional) Max value used to saturate up the output result before converting back to QASYMM8_SIGNED,
Giorgio Arena1856ff72020-02-07 13:46:45 +0000155 * Along with @p min, this value can be used to implement "rectified linear unit" activation functions. Defaults to the maximum possible 32-bit signed integer.
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000156 *
157 * @return a status
158 */
Giorgio Arena1856ff72020-02-07 13:46:45 +0000159 static Status validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min = std::numeric_limits<int32_t>::lowest(), int max = std::numeric_limits<int32_t>::max());
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000160};
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100161/** Basic function to execute NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint on NEON.
162 *
163 * NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint depends on 2 parameters:
164 *
165 * result_fixedpoint_multiplier, result_shift
166 *
167 * The final result is:
168 *
169 * (FixedPointMul(input[i][k], result_fixedpoint_multiplier) >> result_shift)
170 *
171 * where FixedPointMul(x, y) is the nearest integer to the following
172 * mathematical expression, evaluated without overflow or intermediate rounding:
173 *
174 * (x * y) / 2^31
175 *
176 * For more information: https://github.com/google/gemmlowp/blob/master/public/output_stages.h#L68
177 *
178 * In case the bias tensor is provided, the final result is:
179 *
180 * ((FixedPointMul(input[i][k] + bias[k], result_fixedpoint_multiplier)) >> result_shift) + result_offset_after_shift
181 *
182 * This function calls the following NEON kernels:
183 *
184 * -# @ref NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel
185 *
186 * @note The function accepts also 2 optional input arguments (min and max) which can be used to implement "rectified linear unit" activation functions
187 * after the result is shifted right by result_shift
188*/
189class NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint : public INESimpleFunctionNoBorder
190{
191public:
192 /** Initialise the kernel's inputs, output
193 *
194 * @param[in] input Input tensor. Data type supported: S32
195 * @param[in] bias Biases tensor. Only shared biases supported and it can be a nullptr if the biases addition is not required.
196 * Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p input.
197 * @param[out] output Output tensor. Data type supported: Data type supported: QSYMM16
198 * @param[in] result_fixedpoint_multiplier Fixed point value to be multiplied to each element of the input matrix when once the result_offset has been add
199 * @param[in] result_shift Number of bits to shift right the result after the fixed point multiplication
Giorgio Arena1856ff72020-02-07 13:46:45 +0000200 * @param[in] min (Optional) Min value used to saturate down the output result before converting back to QSYMM16. Defaults to the minimum possible 32-bit signed integer.
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100201 * @param[in] max (Optional) Max value used to saturate up the output result before converting back to QSYMM16.
Giorgio Arena1856ff72020-02-07 13:46:45 +0000202 * Along with @p min, this value can be used to implement "rectified linear unit" activation functions. Defaults to the maximum possible 32-bit signed integer.
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100203 */
Giorgio Arena1856ff72020-02-07 13:46:45 +0000204 void configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift, int min = std::numeric_limits<int32_t>::lowest(),
205 int max = std::numeric_limits<int32_t>::max());
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100206 /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint
207 *
208 * @param[in] input Input tensor info. It is the output of @ref NEGEMMLowpMatrixMultiplyCore function. Data type supported: S32
209 * @param[in] bias Biases tensor info. Only shared biases supported and it can be a nullptr if the addition of biases is not required.
210 * Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p input.
211 * @param[in] output Output tensor info. Data type supported: Data type supported: QSYMM16
Giorgio Arena1856ff72020-02-07 13:46:45 +0000212 * @param[in] min (Optional) Min value used to saturate down the output result before converting back to QSYMM16. Defaults to the minimum possible 32-bit signed integer.
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100213 * @param[in] max (Optional) Max value used to saturate up the output result before converting back to QSYMM16,
Giorgio Arena1856ff72020-02-07 13:46:45 +0000214 * Along with @p min, this value can be used to implement "rectified linear unit" activation functions. Defaults to the maximum possible 32-bit signed integer.
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100215 *
216 * @return a status
217 */
Giorgio Arena1856ff72020-02-07 13:46:45 +0000218 static Status validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min = std::numeric_limits<int32_t>::lowest(), int max = std::numeric_limits<int32_t>::max());
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100219};
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000220
221/** Basic function to execute GEMMLowpQuantizeDown kernels on NEON.
222 *
223 * This function calls the following NEON kernels:
224 *
Luca Foschiani4b869532020-02-13 15:07:36 +0000225 * -# @ref NEGEMMLowpQuantizeDownInt32ScaleKernel
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000226 * -# @ref NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel
227 * -# @ref NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel
228 * -# @ref NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel
229*/
230class NEGEMMLowpOutputStage : public INESimpleFunctionNoBorder
231{
232public:
233 /** Initialise the kernel's inputs, output
234 *
235 * @param[in] input Input tensor. Data type supported: S32
236 * @param[in] bias Biases tensor. Only shared biases supported and it can be a nullptr if the biases addition is not required.
237 * Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p input.
238 * @param[out] output Output tensor. Data type supported: Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM16
239 * @param[in] info GEMMLowp output stage metadata.
240 */
241 void configure(const ITensor *input, const ITensor *bias, ITensor *output, const GEMMLowpOutputStageInfo &info);
242 /** Static function to check if given info will lead to a valid configuration of @ref NEGEMMLowpOutputStage
243 *
244 * @param[in] input Input tensor info. It is the output of @ref NEGEMMLowpMatrixMultiplyCore function. Data type supported: S32
245 * @param[in] bias Biases tensor info. Only shared biases supported and it can be a nullptr if the addition of biases is not required.
246 * Biases are 1D tensor with dimensions [OFM]. Data type supported: Same as @p input.
247 * @param[in] output Output tensor info. Data type supported: Data type supported: QASYMM8/QASYMM8_SIGNED/QSYMM16
248 * @param[in] info GEMMLowp output stage metadata.
249 *
250 * @return a status
251 */
252 static Status validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo &info);
253};
Georgios Pinitas041f36d2018-09-18 18:38:37 +0100254} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000255#endif /*ARM_COMPUTE_NEGEMMLOWPOUTPUTSTAGE_H */