blob: be452aaf3d94af187adf2a28244848ae7db74549 [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * 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"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010027#include "arm_compute/core/Types.h"
28#include "src/core/CL/kernels/CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel.h"
29#include "src/core/CL/kernels/CLGEMMLowpQuantizeDownInt32ScaleByFloatKernel.h"
30#include "src/core/CL/kernels/CLGEMMLowpQuantizeDownInt32ScaleKernel.h"
Gian Marco05288a22017-11-21 10:57:50 +000031
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010032#include <algorithm>
33
Georgios Pinitas932491f2018-09-21 16:33:15 +010034namespace arm_compute
35{
Georgios Pinitas932491f2018-09-21 16:33:15 +010036void CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::configure(const ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
37 int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift,
Gian Marco Iodice4b908652018-10-18 10:21:02 +010038 int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +000039{
Manuel Bottini2b84be52020-04-08 10:15:51 +010040 configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max);
41}
42
43void CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
44 int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift,
45 int min, int max)
46{
Michele Di Giorgioba14c922020-10-12 13:27:57 +010047 GEMMLowpOutputStageInfo info{};
48 info.gemmlowp_multiplier = result_fixedpoint_multiplier;
49 info.gemmlowp_shift = result_shift;
50 info.gemmlowp_offset = result_offset_after_shift;
51 info.gemmlowp_min_bound = min;
52 info.gemmlowp_max_bound = max;
53 info.output_data_type = DataType::QASYMM8;
Georgios Pinitas40f51a62020-11-21 03:04:18 +000054 auto k = std::make_unique<CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel>();
Michele Di Giorgioba14c922020-10-12 13:27:57 +010055 k->configure(compile_context, input, bias, output, &info);
Gian Marco58c57942017-11-28 09:10:03 +000056 _kernel = std::move(k);
57}
58
Georgios Pinitas932491f2018-09-21 16:33:15 +010059Status CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output,
Gian Marco Iodice4b908652018-10-18 10:21:02 +010060 int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +000061{
Michele Di Giorgioba14c922020-10-12 13:27:57 +010062 GEMMLowpOutputStageInfo info{};
63 info.gemmlowp_min_bound = min;
64 info.gemmlowp_max_bound = max;
65 info.output_data_type = DataType::QASYMM8;
66 return CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel::validate(input, bias, output, &info);
Georgios Pinitas932491f2018-09-21 16:33:15 +010067}
Georgios Pinitas51e53a32018-10-22 13:49:08 +010068
Manuel Bottini1f332d42019-11-29 17:25:25 +000069void CLGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPoint::configure(const ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
70 int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift,
71 int min, int max)
72{
Michele Di Giorgioba14c922020-10-12 13:27:57 +010073 configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max);
Manuel Bottini2b84be52020-04-08 10:15:51 +010074}
75
76void CLGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPoint::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
77 int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift,
78 int min, int max)
79{
Michele Di Giorgioba14c922020-10-12 13:27:57 +010080 GEMMLowpOutputStageInfo info{};
81 info.gemmlowp_multiplier = result_fixedpoint_multiplier;
82 info.gemmlowp_shift = result_shift;
83 info.gemmlowp_offset = result_offset_after_shift;
84 info.gemmlowp_min_bound = min;
85 info.gemmlowp_max_bound = max;
86 info.output_data_type = DataType::QASYMM8_SIGNED;
Georgios Pinitas40f51a62020-11-21 03:04:18 +000087 auto k = std::make_unique<CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel>();
Michele Di Giorgioba14c922020-10-12 13:27:57 +010088 k->configure(compile_context, input, bias, output, &info);
Manuel Bottini1f332d42019-11-29 17:25:25 +000089 _kernel = std::move(k);
90}
91
92Status CLGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPoint::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output,
93 int min, int max)
94{
Michele Di Giorgioba14c922020-10-12 13:27:57 +010095 GEMMLowpOutputStageInfo info{};
96 info.gemmlowp_min_bound = min;
97 info.gemmlowp_max_bound = max;
98 info.output_data_type = DataType::QASYMM8_SIGNED;
99 return CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel::validate(input, bias, output, &info);
Manuel Bottini1f332d42019-11-29 17:25:25 +0000100}
101
Manuel Bottini9c9b70b2019-07-01 17:35:56 +0100102void CLGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint::configure(const ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
103 int result_fixedpoint_multiplier, int result_shift,
104 int min, int max)
105{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100106 configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, result_fixedpoint_multiplier, result_shift, min, max);
107}
108
109void CLGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
110 int result_fixedpoint_multiplier, int result_shift,
111 int min, int max)
112{
Michele Di Giorgioba14c922020-10-12 13:27:57 +0100113 GEMMLowpOutputStageInfo info{};
114 info.gemmlowp_multiplier = result_fixedpoint_multiplier;
115 info.gemmlowp_shift = result_shift;
116 info.gemmlowp_min_bound = min;
117 info.gemmlowp_max_bound = max;
118 info.output_data_type = DataType::QSYMM16;
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000119 auto k = std::make_unique<CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel>();
Michele Di Giorgioba14c922020-10-12 13:27:57 +0100120 k->configure(compile_context, input, bias, output, &info);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +0100121 _kernel = std::move(k);
122}
123
124Status CLGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output,
125 int min, int max)
126{
Michele Di Giorgioba14c922020-10-12 13:27:57 +0100127 GEMMLowpOutputStageInfo info{};
128 info.gemmlowp_min_bound = min;
129 info.gemmlowp_max_bound = max;
130 info.output_data_type = DataType::QSYMM16;
131 return CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel::validate(input, bias, output, &info);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +0100132}
133
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000134void CLGEMMLowpOutputStage::configure(const ICLTensor *input, const ICLTensor *bias, ICLTensor *output, const GEMMLowpOutputStageInfo &info)
135{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100136 configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, info);
137}
138
139void CLGEMMLowpOutputStage::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *bias, ICLTensor *output, const GEMMLowpOutputStageInfo &info)
140{
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000141 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000142
Luca Foschiani689c9682020-02-26 14:30:14 +0000143 switch(info.type)
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000144 {
Luca Foschiani689c9682020-02-26 14:30:14 +0000145 case GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT:
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000146 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000147 auto k = std::make_unique<CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel>();
Michele Di Giorgioba14c922020-10-12 13:27:57 +0100148 k->configure(compile_context, input, bias, output, &info);
149 _kernel = std::move(k);
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000150 break;
151 }
Luca Foschiani689c9682020-02-26 14:30:14 +0000152 case GEMMLowpOutputStageType::QUANTIZE_DOWN:
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000153 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000154 auto k = std::make_unique<CLGEMMLowpQuantizeDownInt32ScaleKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100155 k->configure(compile_context, input, bias, output, &info);
Luca Foschiani4b869532020-02-13 15:07:36 +0000156 _kernel = std::move(k);
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000157 break;
158 }
Sheri Zhang1b14c752020-03-09 14:29:52 +0000159 case GEMMLowpOutputStageType::QUANTIZE_DOWN_FLOAT:
160 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000161 auto k = std::make_unique<CLGEMMLowpQuantizeDownInt32ScaleByFloatKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100162 k->configure(compile_context, input, bias, output, &info);
Sheri Zhang1b14c752020-03-09 14:29:52 +0000163 _kernel = std::move(k);
164 break;
165 }
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000166 default:
Luca Foschiani689c9682020-02-26 14:30:14 +0000167 ARM_COMPUTE_ERROR("Unsupported GEMMLowpOutputStage type.");
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000168 }
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000169}
170
171Status CLGEMMLowpOutputStage::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo &info)
172{
173 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100174 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 +0000175
Luca Foschiani689c9682020-02-26 14:30:14 +0000176 switch(info.type)
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000177 {
Luca Foschiani689c9682020-02-26 14:30:14 +0000178 case GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT:
Michele Di Giorgioba14c922020-10-12 13:27:57 +0100179 return CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel::validate(input, bias, output, &info);
Luca Foschiani689c9682020-02-26 14:30:14 +0000180 case GEMMLowpOutputStageType::QUANTIZE_DOWN:
Luca Foschiani4b869532020-02-13 15:07:36 +0000181 return CLGEMMLowpQuantizeDownInt32ScaleKernel::validate(input, bias, output, &info);
Sheri Zhang1b14c752020-03-09 14:29:52 +0000182 case GEMMLowpOutputStageType::QUANTIZE_DOWN_FLOAT:
Sheri Zhang1b14c752020-03-09 14:29:52 +0000183 return CLGEMMLowpQuantizeDownInt32ScaleByFloatKernel::validate(input, bias, output, &info);
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000184 default:
Luca Foschiani689c9682020-02-26 14:30:14 +0000185 return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported GEMMLowpOutputStage type.");
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000186 }
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000187}
Sang-Hoon Parka45abfd2020-08-17 13:50:15 +0100188} // namespace arm_compute