blob: f9c5247d2d9a2a5e038d909132b01d97ecb6d0cc [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"
Matthew Bentham92046462020-03-07 22:15:55 +000031#include "support/MemorySupport.h"
Gian Marco05288a22017-11-21 10:57:50 +000032
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010033#include <algorithm>
34
Georgios Pinitas932491f2018-09-21 16:33:15 +010035namespace arm_compute
36{
Georgios Pinitas932491f2018-09-21 16:33:15 +010037void CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::configure(const ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
38 int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift,
Gian Marco Iodice4b908652018-10-18 10:21:02 +010039 int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +000040{
Manuel Bottini2b84be52020-04-08 10:15:51 +010041 configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max);
42}
43
44void CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
45 int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift,
46 int min, int max)
47{
Michele Di Giorgioba14c922020-10-12 13:27:57 +010048 GEMMLowpOutputStageInfo info{};
49 info.gemmlowp_multiplier = result_fixedpoint_multiplier;
50 info.gemmlowp_shift = result_shift;
51 info.gemmlowp_offset = result_offset_after_shift;
52 info.gemmlowp_min_bound = min;
53 info.gemmlowp_max_bound = max;
54 info.output_data_type = DataType::QASYMM8;
55 auto k = arm_compute::support::cpp14::make_unique<CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel>();
56 k->configure(compile_context, input, bias, output, &info);
Gian Marco58c57942017-11-28 09:10:03 +000057 _kernel = std::move(k);
58}
59
Georgios Pinitas932491f2018-09-21 16:33:15 +010060Status CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output,
Gian Marco Iodice4b908652018-10-18 10:21:02 +010061 int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +000062{
Michele Di Giorgioba14c922020-10-12 13:27:57 +010063 GEMMLowpOutputStageInfo info{};
64 info.gemmlowp_min_bound = min;
65 info.gemmlowp_max_bound = max;
66 info.output_data_type = DataType::QASYMM8;
67 return CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel::validate(input, bias, output, &info);
Georgios Pinitas932491f2018-09-21 16:33:15 +010068}
Georgios Pinitas51e53a32018-10-22 13:49:08 +010069
Manuel Bottini1f332d42019-11-29 17:25:25 +000070void CLGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPoint::configure(const ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
71 int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift,
72 int min, int max)
73{
Michele Di Giorgioba14c922020-10-12 13:27:57 +010074 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 +010075}
76
77void CLGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPoint::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
78 int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift,
79 int min, int max)
80{
Michele Di Giorgioba14c922020-10-12 13:27:57 +010081 GEMMLowpOutputStageInfo info{};
82 info.gemmlowp_multiplier = result_fixedpoint_multiplier;
83 info.gemmlowp_shift = result_shift;
84 info.gemmlowp_offset = result_offset_after_shift;
85 info.gemmlowp_min_bound = min;
86 info.gemmlowp_max_bound = max;
87 info.output_data_type = DataType::QASYMM8_SIGNED;
88 auto k = arm_compute::support::cpp14::make_unique<CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel>();
89 k->configure(compile_context, input, bias, output, &info);
Manuel Bottini1f332d42019-11-29 17:25:25 +000090 _kernel = std::move(k);
91}
92
93Status CLGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPoint::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output,
94 int min, int max)
95{
Michele Di Giorgioba14c922020-10-12 13:27:57 +010096 GEMMLowpOutputStageInfo info{};
97 info.gemmlowp_min_bound = min;
98 info.gemmlowp_max_bound = max;
99 info.output_data_type = DataType::QASYMM8_SIGNED;
100 return CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel::validate(input, bias, output, &info);
Manuel Bottini1f332d42019-11-29 17:25:25 +0000101}
102
Manuel Bottini9c9b70b2019-07-01 17:35:56 +0100103void CLGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint::configure(const ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
104 int result_fixedpoint_multiplier, int result_shift,
105 int min, int max)
106{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100107 configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, result_fixedpoint_multiplier, result_shift, min, max);
108}
109
110void CLGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *bias, ICLTensor *output,
111 int result_fixedpoint_multiplier, int result_shift,
112 int min, int max)
113{
Michele Di Giorgioba14c922020-10-12 13:27:57 +0100114 GEMMLowpOutputStageInfo info{};
115 info.gemmlowp_multiplier = result_fixedpoint_multiplier;
116 info.gemmlowp_shift = result_shift;
117 info.gemmlowp_min_bound = min;
118 info.gemmlowp_max_bound = max;
119 info.output_data_type = DataType::QSYMM16;
120 auto k = arm_compute::support::cpp14::make_unique<CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel>();
121 k->configure(compile_context, input, bias, output, &info);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +0100122 _kernel = std::move(k);
123}
124
125Status CLGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output,
126 int min, int max)
127{
Michele Di Giorgioba14c922020-10-12 13:27:57 +0100128 GEMMLowpOutputStageInfo info{};
129 info.gemmlowp_min_bound = min;
130 info.gemmlowp_max_bound = max;
131 info.output_data_type = DataType::QSYMM16;
132 return CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel::validate(input, bias, output, &info);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +0100133}
134
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000135void CLGEMMLowpOutputStage::configure(const ICLTensor *input, const ICLTensor *bias, ICLTensor *output, const GEMMLowpOutputStageInfo &info)
136{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100137 configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, info);
138}
139
140void CLGEMMLowpOutputStage::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *bias, ICLTensor *output, const GEMMLowpOutputStageInfo &info)
141{
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000142 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000143
Luca Foschiani689c9682020-02-26 14:30:14 +0000144 switch(info.type)
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000145 {
Luca Foschiani689c9682020-02-26 14:30:14 +0000146 case GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT:
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000147 {
Michele Di Giorgioba14c922020-10-12 13:27:57 +0100148 auto k = arm_compute::support::cpp14::make_unique<CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel>();
149 k->configure(compile_context, input, bias, output, &info);
150 _kernel = std::move(k);
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000151 break;
152 }
Luca Foschiani689c9682020-02-26 14:30:14 +0000153 case GEMMLowpOutputStageType::QUANTIZE_DOWN:
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000154 {
Luca Foschiani4b869532020-02-13 15:07:36 +0000155 auto k = arm_compute::support::cpp14::make_unique<CLGEMMLowpQuantizeDownInt32ScaleKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100156 k->configure(compile_context, input, bias, output, &info);
Luca Foschiani4b869532020-02-13 15:07:36 +0000157 _kernel = std::move(k);
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000158 break;
159 }
Sheri Zhang1b14c752020-03-09 14:29:52 +0000160 case GEMMLowpOutputStageType::QUANTIZE_DOWN_FLOAT:
161 {
162 auto k = arm_compute::support::cpp14::make_unique<CLGEMMLowpQuantizeDownInt32ScaleByFloatKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100163 k->configure(compile_context, input, bias, output, &info);
Sheri Zhang1b14c752020-03-09 14:29:52 +0000164 _kernel = std::move(k);
165 break;
166 }
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000167 default:
Luca Foschiani689c9682020-02-26 14:30:14 +0000168 ARM_COMPUTE_ERROR("Unsupported GEMMLowpOutputStage type.");
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000169 }
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000170}
171
172Status CLGEMMLowpOutputStage::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo &info)
173{
174 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
Sang-Hoon Parka7431ae2020-05-12 11:13:30 +0100175 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 +0000176
Luca Foschiani689c9682020-02-26 14:30:14 +0000177 switch(info.type)
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000178 {
Luca Foschiani689c9682020-02-26 14:30:14 +0000179 case GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT:
Michele Di Giorgioba14c922020-10-12 13:27:57 +0100180 return CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel::validate(input, bias, output, &info);
Luca Foschiani689c9682020-02-26 14:30:14 +0000181 case GEMMLowpOutputStageType::QUANTIZE_DOWN:
Luca Foschiani4b869532020-02-13 15:07:36 +0000182 return CLGEMMLowpQuantizeDownInt32ScaleKernel::validate(input, bias, output, &info);
Sheri Zhang1b14c752020-03-09 14:29:52 +0000183 case GEMMLowpOutputStageType::QUANTIZE_DOWN_FLOAT:
Sheri Zhang1b14c752020-03-09 14:29:52 +0000184 return CLGEMMLowpQuantizeDownInt32ScaleByFloatKernel::validate(input, bias, output, &info);
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000185 default:
Luca Foschiani689c9682020-02-26 14:30:14 +0000186 return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported GEMMLowpOutputStage type.");
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000187 }
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000188}
Sang-Hoon Parka45abfd2020-08-17 13:50:15 +0100189} // namespace arm_compute