blob: 43ca7b3fbbe991e52a00a59f304bc92936983f6f [file] [log] [blame]
Gian Marcoe75a02b2017-11-08 12:24:09 +00001/*
Michele Di Giorgio9c700372020-01-08 11:33:44 +00002 * 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 */
24#include "arm_compute/runtime/NEON/functions/NEGEMMLowpOutputStage.h"
25
26#include "arm_compute/core/ITensor.h"
Luca Foschiani4b869532020-02-13 15:07:36 +000027#include "arm_compute/core/NEON/kernels/NEGEMMLowpQuantizeDownInt32ScaleKernel.h"
Gian Marco Iodicebc415af2019-06-13 15:58:32 +010028#include "arm_compute/core/NEON/kernels/NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel.h"
Georgios Pinitas448a81f2019-11-21 14:10:25 +000029#include "arm_compute/core/NEON/kernels/NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel.h"
Gian Marco58c57942017-11-28 09:10:03 +000030#include "arm_compute/core/NEON/kernels/NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel.h"
Michele Di Giorgio9c700372020-01-08 11:33:44 +000031#include "arm_compute/core/Validate.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000032#include "support/MemorySupport.h"
Gian Marcoe75a02b2017-11-08 12:24:09 +000033
Gian Marco Iodicebc415af2019-06-13 15:58:32 +010034namespace arm_compute
35{
Gian Marco6b77e912017-11-17 09:27:57 +000036void NEGEMMLowpQuantizeDownInt32ToUint8Scale::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_offset, int result_mult_int, int result_shift, int min, int max)
Gian Marcoe75a02b2017-11-08 12:24:09 +000037{
Luca Foschiani4b869532020-02-13 15:07:36 +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<NEGEMMLowpQuantizeDownInt32ScaleKernel>();
46 k->configure(input, bias, output, &info);
Gian Marcoe75a02b2017-11-08 12:24:09 +000047 _kernel = std::move(k);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000048}
49
Georgios Pinitas631c41a2017-12-06 11:53:03 +000050Status NEGEMMLowpQuantizeDownInt32ToUint8Scale::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000051{
Luca Foschiani4b869532020-02-13 15:07:36 +000052 GEMMLowpOutputStageInfo info = GEMMLowpOutputStageInfo();
53 info.gemmlowp_min_bound = min;
54 info.gemmlowp_max_bound = max;
55
56 return NEGEMMLowpQuantizeDownInt32ScaleKernel::validate(input, bias, output, &info);
Gian Marco58c57942017-11-28 09:10:03 +000057}
58
59void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift,
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000060 int result_offset_after_shift, int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +000061{
62 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel>();
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000063 k->configure(input, bias, output, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max);
Gian Marco58c57942017-11-28 09:10:03 +000064 _kernel = std::move(k);
65}
66
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000067Status NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +000068{
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000069 return NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::validate(input, bias, output, min, max);
Gian Marco Iodicebc415af2019-06-13 15:58:32 +010070}
71
Georgios Pinitas448a81f2019-11-21 14:10:25 +000072void NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPoint::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift,
73 int result_offset_after_shift, int min, int max)
74{
75 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel>();
76 k->configure(input, bias, output, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max);
77 _kernel = std::move(k);
78}
79
80Status NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPoint::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
81{
82 return NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::validate(input, bias, output, min, max);
83}
84
Gian Marco Iodicebc415af2019-06-13 15:58:32 +010085void NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift, int min, int max)
86{
87 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel>();
88 k->configure(input, bias, output, result_fixedpoint_multiplier, result_shift, min, max);
89 _kernel = std::move(k);
90}
91
92Status NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
93{
94 return NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::validate(input, bias, output, min, max);
95}
Michele Di Giorgio9c700372020-01-08 11:33:44 +000096
97void NEGEMMLowpOutputStage::configure(const ITensor *input, const ITensor *bias, ITensor *output, const GEMMLowpOutputStageInfo &info)
98{
99 // Perform validate step
100 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
101 ARM_COMPUTE_ERROR_THROW_ON(NEGEMMLowpOutputStage::validate(input->info(), bias != nullptr ? bias->info() : nullptr, output->info(), info));
102
Luca Foschiani4b869532020-02-13 15:07:36 +0000103 switch(info.type)
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000104 {
Luca Foschiani4b869532020-02-13 15:07:36 +0000105 case GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT:
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000106 {
Luca Foschiani4b869532020-02-13 15:07:36 +0000107 switch(info.output_data_type)
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000108 {
Luca Foschiani4b869532020-02-13 15:07:36 +0000109 case DataType::QASYMM8:
110 {
111 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel>();
112 k->configure(input, bias, output, info.gemmlowp_multiplier, info.gemmlowp_shift, info.gemmlowp_offset, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
113 _kernel = std::move(k);
114 break;
115 }
116 case DataType::QASYMM8_SIGNED:
117 {
118 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel>();
119 k->configure(input, bias, output, info.gemmlowp_multiplier, info.gemmlowp_shift, info.gemmlowp_offset, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
120 _kernel = std::move(k);
121 break;
122 }
123 case DataType::QSYMM16:
124 {
125 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel>();
126 k->configure(input, bias, output, info.gemmlowp_multiplier, info.gemmlowp_shift, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
127 _kernel = std::move(k);
128 break;
129 }
130 default:
131 {
132 ARM_COMPUTE_ERROR("Unsupported output data type.");
133 break;
134 }
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000135 }
Luca Foschiani4b869532020-02-13 15:07:36 +0000136 break;
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000137 }
Luca Foschiani4b869532020-02-13 15:07:36 +0000138 case GEMMLowpOutputStageType::QUANTIZE_DOWN:
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000139 {
Luca Foschiani4b869532020-02-13 15:07:36 +0000140 switch(info.output_data_type)
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000141 {
Luca Foschiani4b869532020-02-13 15:07:36 +0000142 case DataType::QASYMM8:
143 case DataType::QASYMM8_SIGNED:
144 {
145 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpQuantizeDownInt32ScaleKernel>();
146 k->configure(input, bias, output, &info);
147 _kernel = std::move(k);
148 break;
149 }
150 default:
151 {
152 ARM_COMPUTE_ERROR("Unsupported output data type.");
153 break;
154 }
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000155 }
Luca Foschiani4b869532020-02-13 15:07:36 +0000156 break;
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000157 }
Luca Foschiani4b869532020-02-13 15:07:36 +0000158 default:
159 ARM_COMPUTE_ERROR("Unsupported GEMMLowpOutputStage type.");
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000160 }
161}
162
163Status NEGEMMLowpOutputStage::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo &info)
164{
165 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
166 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::UNKNOWN, "NEGEMMLowpQuantizeDownScaleByFixedPoint cannot be used with UNKNOWN output data type.");
167 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM16);
168
169 ARM_COMPUTE_RETURN_ERROR_ON((info.type != GEMMLowpOutputStageType::QUANTIZE_DOWN) && (info.type != GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT));
170
Luca Foschiani4b869532020-02-13 15:07:36 +0000171 switch(info.type)
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000172 {
Luca Foschiani4b869532020-02-13 15:07:36 +0000173 case GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT:
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000174 {
Luca Foschiani4b869532020-02-13 15:07:36 +0000175 switch(output->data_type())
176 {
177 case DataType::QASYMM8:
178 return NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::validate(input, bias, output, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
179 case DataType::QASYMM8_SIGNED:
180 return NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::validate(input, bias, output, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
181 case DataType::QSYMM16:
182 return NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::validate(input, bias, output, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
183 default:
184 return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported output data type.");
185 }
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000186 }
Luca Foschiani4b869532020-02-13 15:07:36 +0000187 case GEMMLowpOutputStageType::QUANTIZE_DOWN:
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000188 {
Luca Foschiani4b869532020-02-13 15:07:36 +0000189 switch(output->data_type())
190 {
191 case DataType::QASYMM8:
192 case DataType::QASYMM8_SIGNED:
193 return NEGEMMLowpQuantizeDownInt32ScaleKernel::validate(input, bias, output, &info);
194 default:
195 return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported output data type.");
196 }
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000197 }
Luca Foschiani4b869532020-02-13 15:07:36 +0000198 default:
199 return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported GEMMLowpOutputStage type.");
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000200 }
201}
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100202} // namespace arm_compute