blob: 9ae5d5121c8088350e4ebb9f3f81bdc799dccd3f [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
Matthew Bentham92046462020-03-07 22:15:55 +00002 * Copyright (c) 2017-2020 ARM Limited.
Gian Marco05288a22017-11-21 10:57:50 +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 */
24#include "arm_compute/runtime/CL/functions/CLGEMMLowpOutputStage.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
Sheri Zhang1b14c752020-03-09 14:29:52 +000027#include "arm_compute/core/CL/kernels/CLGEMMLowpQuantizeDownInt32ScaleByFloatKernel.h"
Luca Foschiani689c9682020-02-26 14:30:14 +000028#include "arm_compute/core/CL/kernels/CLGEMMLowpQuantizeDownInt32ScaleKernel.h"
Manuel Bottini9c9b70b2019-07-01 17:35:56 +010029#include "arm_compute/core/CL/kernels/CLGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel.h"
Manuel Bottini1f332d42019-11-29 17:25:25 +000030#include "arm_compute/core/CL/kernels/CLGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel.h"
Gian Marco58c57942017-11-28 09:10:03 +000031#include "arm_compute/core/CL/kernels/CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel.h"
Matthew Bentham92046462020-03-07 22:15:55 +000032#include "support/MemorySupport.h"
Gian Marco05288a22017-11-21 10:57:50 +000033
Georgios Pinitas932491f2018-09-21 16:33:15 +010034namespace arm_compute
35{
Gian Marco05288a22017-11-21 10:57:50 +000036void CLGEMMLowpQuantizeDownInt32ToUint8Scale::configure(const ICLTensor *input, const ICLTensor *bias, ICLTensor *output, int result_offset, int result_mult_int, int result_shift, int min, int max)
37{
Luca Foschiani689c9682020-02-26 14:30:14 +000038 GEMMLowpOutputStageInfo info = GEMMLowpOutputStageInfo();
39 info.gemmlowp_offset = result_offset;
40 info.gemmlowp_multiplier = result_mult_int;
41 info.gemmlowp_shift = result_shift;
42 info.gemmlowp_min_bound = min;
43 info.gemmlowp_max_bound = max;
44
45 auto k = arm_compute::support::cpp14::make_unique<CLGEMMLowpQuantizeDownInt32ScaleKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +010046 k->configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, &info);
47 _kernel = std::move(k);
48}
49
50void CLGEMMLowpQuantizeDownInt32ToUint8Scale::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *bias, ICLTensor *output, int result_offset,
51 int result_mult_int,
52 int result_shift, int min, int max)
53{
54 GEMMLowpOutputStageInfo info = GEMMLowpOutputStageInfo();
55 info.gemmlowp_offset = result_offset;
56 info.gemmlowp_multiplier = result_mult_int;
57 info.gemmlowp_shift = result_shift;
58 info.gemmlowp_min_bound = min;
59 info.gemmlowp_max_bound = max;
60
61 auto k = arm_compute::support::cpp14::make_unique<CLGEMMLowpQuantizeDownInt32ScaleKernel>();
62 k->configure(compile_context, input, bias, output, &info);
Gian Marco05288a22017-11-21 10:57:50 +000063 _kernel = std::move(k);
Gian Marco58c57942017-11-28 09:10:03 +000064}
65
Georgios Pinitas631c41a2017-12-06 11:53:03 +000066Status CLGEMMLowpQuantizeDownInt32ToUint8Scale::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +000067{
Luca Foschiani689c9682020-02-26 14:30:14 +000068 GEMMLowpOutputStageInfo info = GEMMLowpOutputStageInfo();
69 info.gemmlowp_min_bound = min;
70 info.gemmlowp_max_bound = max;
71
72 return CLGEMMLowpQuantizeDownInt32ScaleKernel::validate(input, bias, output, &info);
Gian Marco58c57942017-11-28 09:10:03 +000073}
74
Georgios Pinitas932491f2018-09-21 16:33:15 +010075void CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::configure(const ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
76 int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift,
Gian Marco Iodice4b908652018-10-18 10:21:02 +010077 int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +000078{
Manuel Bottini2b84be52020-04-08 10:15:51 +010079 configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max);
80}
81
82void CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
83 int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift,
84 int min, int max)
85{
Gian Marco58c57942017-11-28 09:10:03 +000086 auto k = arm_compute::support::cpp14::make_unique<CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +010087 k->configure(compile_context, input, bias, output, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max);
Gian Marco58c57942017-11-28 09:10:03 +000088 _kernel = std::move(k);
89}
90
Georgios Pinitas932491f2018-09-21 16:33:15 +010091Status CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output,
Gian Marco Iodice4b908652018-10-18 10:21:02 +010092 int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +000093{
Gian Marco Iodice4b908652018-10-18 10:21:02 +010094 return CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::validate(input, bias, output, min, max);
Georgios Pinitas932491f2018-09-21 16:33:15 +010095}
Georgios Pinitas51e53a32018-10-22 13:49:08 +010096
Manuel Bottini1f332d42019-11-29 17:25:25 +000097void CLGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPoint::configure(const ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
98 int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift,
99 int min, int max)
100{
101 auto k = arm_compute::support::cpp14::make_unique<CLGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100102 k->configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max);
103 _kernel = std::move(k);
104}
105
106void CLGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPoint::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
107 int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift,
108 int min, int max)
109{
110 auto k = arm_compute::support::cpp14::make_unique<CLGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel>();
111 k->configure(compile_context, input, bias, output, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max);
Manuel Bottini1f332d42019-11-29 17:25:25 +0000112 _kernel = std::move(k);
113}
114
115Status CLGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPoint::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output,
116 int min, int max)
117{
118 return CLGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::validate(input, bias, output, min, max);
119}
120
Georgios Pinitas51e53a32018-10-22 13:49:08 +0100121void CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFloat::configure(const ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
122 float multiplier, int offset,
Gian Marco Iodice0c54a622018-10-30 12:20:03 +0000123 int min, int max)
Georgios Pinitas51e53a32018-10-22 13:49:08 +0100124{
Sheri Zhang1b14c752020-03-09 14:29:52 +0000125 GEMMLowpOutputStageInfo info = GEMMLowpOutputStageInfo();
126 info.gemmlowp_offset = offset;
127 info.gemmlowp_real_multiplier = multiplier;
128 info.gemmlowp_min_bound = min;
129 info.gemmlowp_max_bound = max;
130
131 auto k = arm_compute::support::cpp14::make_unique<CLGEMMLowpQuantizeDownInt32ScaleByFloatKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100132 k->configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, &info);
133 _kernel = std::move(k);
134}
135
136void CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFloat::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
137 float multiplier, int offset,
138 int min, int max)
139{
140 GEMMLowpOutputStageInfo info = GEMMLowpOutputStageInfo();
141 info.gemmlowp_offset = offset;
142 info.gemmlowp_real_multiplier = multiplier;
143 info.gemmlowp_min_bound = min;
144 info.gemmlowp_max_bound = max;
145
146 auto k = arm_compute::support::cpp14::make_unique<CLGEMMLowpQuantizeDownInt32ScaleByFloatKernel>();
147 k->configure(compile_context, input, bias, output, &info);
Georgios Pinitas51e53a32018-10-22 13:49:08 +0100148 _kernel = std::move(k);
149}
150
151Status CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFloat::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output,
Gian Marco Iodice0c54a622018-10-30 12:20:03 +0000152 int min, int max)
Georgios Pinitas51e53a32018-10-22 13:49:08 +0100153{
Sheri Zhang1b14c752020-03-09 14:29:52 +0000154 GEMMLowpOutputStageInfo info = GEMMLowpOutputStageInfo();
155 info.gemmlowp_min_bound = min;
156 info.gemmlowp_max_bound = max;
157 return CLGEMMLowpQuantizeDownInt32ScaleByFloatKernel::validate(input, bias, output, &info);
Georgios Pinitas51e53a32018-10-22 13:49:08 +0100158}
Manuel Bottini9c9b70b2019-07-01 17:35:56 +0100159
160void CLGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint::configure(const ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
161 int result_fixedpoint_multiplier, int result_shift,
162 int min, int max)
163{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100164 configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, result_fixedpoint_multiplier, result_shift, min, max);
165}
166
167void CLGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
168 int result_fixedpoint_multiplier, int result_shift,
169 int min, int max)
170{
Manuel Bottini9c9b70b2019-07-01 17:35:56 +0100171 auto k = arm_compute::support::cpp14::make_unique<CLGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100172 k->configure(compile_context, input, bias, output, result_fixedpoint_multiplier, result_shift, min, max);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +0100173 _kernel = std::move(k);
174}
175
176Status CLGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output,
177 int min, int max)
178{
179 return CLGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::validate(input, bias, output, min, max);
180}
181
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000182void CLGEMMLowpOutputStage::configure(const ICLTensor *input, const ICLTensor *bias, ICLTensor *output, const GEMMLowpOutputStageInfo &info)
183{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100184 configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, info);
185}
186
187void CLGEMMLowpOutputStage::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *bias, ICLTensor *output, const GEMMLowpOutputStageInfo &info)
188{
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000189 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000190
Luca Foschiani689c9682020-02-26 14:30:14 +0000191 switch(info.type)
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000192 {
Luca Foschiani689c9682020-02-26 14:30:14 +0000193 case GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT:
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000194 {
Luca Foschiani689c9682020-02-26 14:30:14 +0000195 switch(info.output_data_type)
196 {
197 case DataType::QASYMM8:
198 {
199 auto k = arm_compute::support::cpp14::make_unique<CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100200 k->configure(compile_context, input, bias, output, info.gemmlowp_multiplier, info.gemmlowp_shift, info.gemmlowp_offset, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
Luca Foschiani689c9682020-02-26 14:30:14 +0000201 _kernel = std::move(k);
202 break;
203 }
204 case DataType::QASYMM8_SIGNED:
205 {
206 auto k = arm_compute::support::cpp14::make_unique<CLGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100207 k->configure(compile_context, input, bias, output, info.gemmlowp_multiplier, info.gemmlowp_shift, info.gemmlowp_offset, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
Luca Foschiani689c9682020-02-26 14:30:14 +0000208 _kernel = std::move(k);
209 break;
210 }
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100211 case DataType::QSYMM16:
212 {
213 auto k = arm_compute::support::cpp14::make_unique<CLGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel>();
214 k->configure(input, bias, output, info.gemmlowp_multiplier, info.gemmlowp_shift, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
215 _kernel = std::move(k);
216 break;
217 }
Luca Foschiani689c9682020-02-26 14:30:14 +0000218 default:
219 ARM_COMPUTE_ERROR("Unsupported output data type.");
220 }
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000221 break;
222 }
Luca Foschiani689c9682020-02-26 14:30:14 +0000223 case GEMMLowpOutputStageType::QUANTIZE_DOWN:
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000224 {
Luca Foschiani4b869532020-02-13 15:07:36 +0000225 auto k = arm_compute::support::cpp14::make_unique<CLGEMMLowpQuantizeDownInt32ScaleKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100226 k->configure(compile_context, input, bias, output, &info);
Luca Foschiani4b869532020-02-13 15:07:36 +0000227 _kernel = std::move(k);
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000228 break;
229 }
Sheri Zhang1b14c752020-03-09 14:29:52 +0000230 case GEMMLowpOutputStageType::QUANTIZE_DOWN_FLOAT:
231 {
232 auto k = arm_compute::support::cpp14::make_unique<CLGEMMLowpQuantizeDownInt32ScaleByFloatKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100233 k->configure(compile_context, input, bias, output, &info);
Sheri Zhang1b14c752020-03-09 14:29:52 +0000234 _kernel = std::move(k);
235 break;
236 }
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000237 default:
Luca Foschiani689c9682020-02-26 14:30:14 +0000238 ARM_COMPUTE_ERROR("Unsupported GEMMLowpOutputStage type.");
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000239 }
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000240}
241
242Status CLGEMMLowpOutputStage::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo &info)
243{
244 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100245 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM16);
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000246
Luca Foschiani689c9682020-02-26 14:30:14 +0000247 switch(info.type)
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000248 {
Luca Foschiani689c9682020-02-26 14:30:14 +0000249 case GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT:
250 {
251 switch(output->data_type())
252 {
253 case DataType::QASYMM8:
254 return CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::validate(input, bias, output, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
255 case DataType::QASYMM8_SIGNED:
256 return CLGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::validate(input, bias, output, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100257 case DataType::QSYMM16:
258 return CLGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::validate(input, bias, output, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
Luca Foschiani689c9682020-02-26 14:30:14 +0000259 default:
260 return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported output data type.");
261 }
262 }
263 case GEMMLowpOutputStageType::QUANTIZE_DOWN:
Luca Foschiani4b869532020-02-13 15:07:36 +0000264 return CLGEMMLowpQuantizeDownInt32ScaleKernel::validate(input, bias, output, &info);
Sheri Zhang1b14c752020-03-09 14:29:52 +0000265 case GEMMLowpOutputStageType::QUANTIZE_DOWN_FLOAT:
Sheri Zhang1b14c752020-03-09 14:29:52 +0000266 return CLGEMMLowpQuantizeDownInt32ScaleByFloatKernel::validate(input, bias, output, &info);
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000267 default:
Luca Foschiani689c9682020-02-26 14:30:14 +0000268 return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported GEMMLowpOutputStage type.");
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000269 }
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000270}
Luca Foschiani689c9682020-02-26 14:30:14 +0000271} // namespace arm_compute