1 /*
2 * Copyright (c) 2017-2020 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 #include "arm_compute/runtime/NEON/functions/NEGEMMLowpOutputStage.h"
25
26 #include "arm_compute/core/ITensor.h"
27 #include "arm_compute/core/Validate.h"
28 #include "src/core/NEON/kernels/NEGEMMLowpQuantizeDownInt32ScaleKernel.h"
29 #include "src/core/NEON/kernels/NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel.h"
30 #include "src/core/NEON/kernels/NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel.h"
31 #include "src/core/NEON/kernels/NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel.h"
32 #include "support/MemorySupport.h"
33
34 namespace arm_compute
35 {
36 NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::~NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint() = default;
37
configure(const ITensor * input,const ITensor * bias,ITensor * output,int result_fixedpoint_multiplier,int result_shift,int result_offset_after_shift,int min,int max)38 void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift,
39 int result_offset_after_shift, int min, int max)
40 {
41 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel>();
42 k->configure(input, bias, output, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max);
43 _kernel = std::move(k);
44 }
45
validate(const ITensorInfo * input,const ITensorInfo * bias,const ITensorInfo * output,int min,int max)46 Status NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
47 {
48 return NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::validate(input, bias, output, min, max);
49 }
50
51 NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPoint::~NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPoint() = default;
52
configure(const ITensor * input,const ITensor * bias,ITensor * output,int result_fixedpoint_multiplier,int result_shift,int result_offset_after_shift,int min,int max)53 void NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPoint::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift,
54 int result_offset_after_shift, int min, int max)
55 {
56 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel>();
57 k->configure(input, bias, output, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max);
58 _kernel = std::move(k);
59 }
60
validate(const ITensorInfo * input,const ITensorInfo * bias,const ITensorInfo * output,int min,int max)61 Status NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPoint::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
62 {
63 return NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::validate(input, bias, output, min, max);
64 }
65
66 NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint::~NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint() = default;
67
configure(const ITensor * input,const ITensor * bias,ITensor * output,int result_fixedpoint_multiplier,int result_shift,int min,int max)68 void NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift, int min, int max)
69 {
70 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel>();
71 k->configure(input, bias, output, result_fixedpoint_multiplier, result_shift, min, max);
72 _kernel = std::move(k);
73 }
74
validate(const ITensorInfo * input,const ITensorInfo * bias,const ITensorInfo * output,int min,int max)75 Status NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max)
76 {
77 return NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::validate(input, bias, output, min, max);
78 }
79
80 NEGEMMLowpOutputStage::~NEGEMMLowpOutputStage() = default;
81
configure(const ITensor * input,const ITensor * bias,ITensor * output,const GEMMLowpOutputStageInfo & info)82 void NEGEMMLowpOutputStage::configure(const ITensor *input, const ITensor *bias, ITensor *output, const GEMMLowpOutputStageInfo &info)
83 {
84 // Perform validate step
85 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
86 ARM_COMPUTE_ERROR_THROW_ON(NEGEMMLowpOutputStage::validate(input->info(), bias != nullptr ? bias->info() : nullptr, output->info(), info));
87
88 switch(info.type)
89 {
90 case GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT:
91 {
92 switch(info.output_data_type)
93 {
94 case DataType::QASYMM8:
95 {
96 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel>();
97 k->configure(input, bias, output, info.gemmlowp_multiplier, info.gemmlowp_shift, info.gemmlowp_offset, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
98 _kernel = std::move(k);
99 break;
100 }
101 case DataType::QASYMM8_SIGNED:
102 {
103 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel>();
104 k->configure(input, bias, output, info.gemmlowp_multiplier, info.gemmlowp_shift, info.gemmlowp_offset, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
105 _kernel = std::move(k);
106 break;
107 }
108 case DataType::QSYMM16:
109 {
110 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel>();
111 k->configure(input, bias, output, info.gemmlowp_multiplier, info.gemmlowp_shift, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
112 _kernel = std::move(k);
113 break;
114 }
115 default:
116 {
117 ARM_COMPUTE_ERROR("Unsupported output data type.");
118 break;
119 }
120 }
121 break;
122 }
123 case GEMMLowpOutputStageType::QUANTIZE_DOWN:
124 {
125 switch(info.output_data_type)
126 {
127 case DataType::QASYMM8:
128 case DataType::QASYMM8_SIGNED:
129 {
130 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpQuantizeDownInt32ScaleKernel>();
131 k->configure(input, bias, output, &info);
132 _kernel = std::move(k);
133 break;
134 }
135 default:
136 {
137 ARM_COMPUTE_ERROR("Unsupported output data type.");
138 break;
139 }
140 }
141 break;
142 }
143 default:
144 ARM_COMPUTE_ERROR("Unsupported GEMMLowpOutputStage type.");
145 }
146 }
147
validate(const ITensorInfo * input,const ITensorInfo * bias,const ITensorInfo * output,const GEMMLowpOutputStageInfo & info)148 Status NEGEMMLowpOutputStage::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo &info)
149 {
150 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
151 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::UNKNOWN, "NEGEMMLowpQuantizeDownScaleByFixedPoint cannot be used with UNKNOWN output data type.");
152 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM16);
153
154 ARM_COMPUTE_RETURN_ERROR_ON((info.type != GEMMLowpOutputStageType::QUANTIZE_DOWN) && (info.type != GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT));
155
156 switch(info.type)
157 {
158 case GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT:
159 {
160 switch(output->data_type())
161 {
162 case DataType::QASYMM8:
163 return NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::validate(input, bias, output, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
164 case DataType::QASYMM8_SIGNED:
165 return NEGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::validate(input, bias, output, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
166 case DataType::QSYMM16:
167 return NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::validate(input, bias, output, info.gemmlowp_min_bound, info.gemmlowp_max_bound);
168 default:
169 return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported output data type.");
170 }
171 }
172 case GEMMLowpOutputStageType::QUANTIZE_DOWN:
173 {
174 switch(output->data_type())
175 {
176 case DataType::QASYMM8:
177 case DataType::QASYMM8_SIGNED:
178 return NEGEMMLowpQuantizeDownInt32ScaleKernel::validate(input, bias, output, &info);
179 default:
180 return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported output data type.");
181 }
182 }
183 default:
184 return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported GEMMLowpOutputStage type.");
185 }
186 }
187 } // namespace arm_compute
188