Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 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/CL/functions/CLGEMMLowpOutputStage.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 27 | #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 Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 31 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 32 | #include <algorithm> |
| 33 | |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 34 | namespace arm_compute |
| 35 | { |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 36 | void 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 Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 38 | int min, int max) |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 39 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 40 | configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max); |
| 41 | } |
| 42 | |
| 43 | void 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 Giorgio | ba14c92 | 2020-10-12 13:27:57 +0100 | [diff] [blame] | 47 | 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 Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 54 | auto k = std::make_unique<CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel>(); |
Michele Di Giorgio | ba14c92 | 2020-10-12 13:27:57 +0100 | [diff] [blame] | 55 | k->configure(compile_context, input, bias, output, &info); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 56 | _kernel = std::move(k); |
| 57 | } |
| 58 | |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 59 | Status CLGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPoint::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 60 | int min, int max) |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 61 | { |
Michele Di Giorgio | ba14c92 | 2020-10-12 13:27:57 +0100 | [diff] [blame] | 62 | 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 Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 67 | } |
Georgios Pinitas | 51e53a3 | 2018-10-22 13:49:08 +0100 | [diff] [blame] | 68 | |
Manuel Bottini | 1f332d4 | 2019-11-29 17:25:25 +0000 | [diff] [blame] | 69 | void 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 Giorgio | ba14c92 | 2020-10-12 13:27:57 +0100 | [diff] [blame] | 73 | configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void 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 Giorgio | ba14c92 | 2020-10-12 13:27:57 +0100 | [diff] [blame] | 80 | 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 Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 87 | auto k = std::make_unique<CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel>(); |
Michele Di Giorgio | ba14c92 | 2020-10-12 13:27:57 +0100 | [diff] [blame] | 88 | k->configure(compile_context, input, bias, output, &info); |
Manuel Bottini | 1f332d4 | 2019-11-29 17:25:25 +0000 | [diff] [blame] | 89 | _kernel = std::move(k); |
| 90 | } |
| 91 | |
| 92 | Status CLGEMMLowpQuantizeDownInt32ToInt8ScaleByFixedPoint::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, |
| 93 | int min, int max) |
| 94 | { |
Michele Di Giorgio | ba14c92 | 2020-10-12 13:27:57 +0100 | [diff] [blame] | 95 | 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 Bottini | 1f332d4 | 2019-11-29 17:25:25 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Manuel Bottini | 9c9b70b | 2019-07-01 17:35:56 +0100 | [diff] [blame] | 102 | void 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 Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 106 | configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, result_fixedpoint_multiplier, result_shift, min, max); |
| 107 | } |
| 108 | |
| 109 | void 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 Giorgio | ba14c92 | 2020-10-12 13:27:57 +0100 | [diff] [blame] | 113 | 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 Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 119 | auto k = std::make_unique<CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel>(); |
Michele Di Giorgio | ba14c92 | 2020-10-12 13:27:57 +0100 | [diff] [blame] | 120 | k->configure(compile_context, input, bias, output, &info); |
Manuel Bottini | 9c9b70b | 2019-07-01 17:35:56 +0100 | [diff] [blame] | 121 | _kernel = std::move(k); |
| 122 | } |
| 123 | |
| 124 | Status CLGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPoint::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, |
| 125 | int min, int max) |
| 126 | { |
Michele Di Giorgio | ba14c92 | 2020-10-12 13:27:57 +0100 | [diff] [blame] | 127 | 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 Bottini | 9c9b70b | 2019-07-01 17:35:56 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 134 | void CLGEMMLowpOutputStage::configure(const ICLTensor *input, const ICLTensor *bias, ICLTensor *output, const GEMMLowpOutputStageInfo &info) |
| 135 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 136 | configure(CLKernelLibrary::get().get_compile_context(), input, bias, output, info); |
| 137 | } |
| 138 | |
| 139 | void CLGEMMLowpOutputStage::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *bias, ICLTensor *output, const GEMMLowpOutputStageInfo &info) |
| 140 | { |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 141 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 142 | |
Luca Foschiani | 689c968 | 2020-02-26 14:30:14 +0000 | [diff] [blame] | 143 | switch(info.type) |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 144 | { |
Luca Foschiani | 689c968 | 2020-02-26 14:30:14 +0000 | [diff] [blame] | 145 | case GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT: |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 146 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 147 | auto k = std::make_unique<CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel>(); |
Michele Di Giorgio | ba14c92 | 2020-10-12 13:27:57 +0100 | [diff] [blame] | 148 | k->configure(compile_context, input, bias, output, &info); |
| 149 | _kernel = std::move(k); |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 150 | break; |
| 151 | } |
Luca Foschiani | 689c968 | 2020-02-26 14:30:14 +0000 | [diff] [blame] | 152 | case GEMMLowpOutputStageType::QUANTIZE_DOWN: |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 153 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 154 | auto k = std::make_unique<CLGEMMLowpQuantizeDownInt32ScaleKernel>(); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 155 | k->configure(compile_context, input, bias, output, &info); |
Luca Foschiani | 4b86953 | 2020-02-13 15:07:36 +0000 | [diff] [blame] | 156 | _kernel = std::move(k); |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 157 | break; |
| 158 | } |
Sheri Zhang | 1b14c75 | 2020-03-09 14:29:52 +0000 | [diff] [blame] | 159 | case GEMMLowpOutputStageType::QUANTIZE_DOWN_FLOAT: |
| 160 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 161 | auto k = std::make_unique<CLGEMMLowpQuantizeDownInt32ScaleByFloatKernel>(); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 162 | k->configure(compile_context, input, bias, output, &info); |
Sheri Zhang | 1b14c75 | 2020-03-09 14:29:52 +0000 | [diff] [blame] | 163 | _kernel = std::move(k); |
| 164 | break; |
| 165 | } |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 166 | default: |
Luca Foschiani | 689c968 | 2020-02-26 14:30:14 +0000 | [diff] [blame] | 167 | ARM_COMPUTE_ERROR("Unsupported GEMMLowpOutputStage type."); |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 168 | } |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | Status 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 Park | a7431ae | 2020-05-12 11:13:30 +0100 | [diff] [blame] | 174 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM16); |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 175 | |
Luca Foschiani | 689c968 | 2020-02-26 14:30:14 +0000 | [diff] [blame] | 176 | switch(info.type) |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 177 | { |
Luca Foschiani | 689c968 | 2020-02-26 14:30:14 +0000 | [diff] [blame] | 178 | case GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT: |
Michele Di Giorgio | ba14c92 | 2020-10-12 13:27:57 +0100 | [diff] [blame] | 179 | return CLGEMMLowpQuantizeDownInt32ScaleByFixedPointKernel::validate(input, bias, output, &info); |
Luca Foschiani | 689c968 | 2020-02-26 14:30:14 +0000 | [diff] [blame] | 180 | case GEMMLowpOutputStageType::QUANTIZE_DOWN: |
Luca Foschiani | 4b86953 | 2020-02-13 15:07:36 +0000 | [diff] [blame] | 181 | return CLGEMMLowpQuantizeDownInt32ScaleKernel::validate(input, bias, output, &info); |
Sheri Zhang | 1b14c75 | 2020-03-09 14:29:52 +0000 | [diff] [blame] | 182 | case GEMMLowpOutputStageType::QUANTIZE_DOWN_FLOAT: |
Sheri Zhang | 1b14c75 | 2020-03-09 14:29:52 +0000 | [diff] [blame] | 183 | return CLGEMMLowpQuantizeDownInt32ScaleByFloatKernel::validate(input, bias, output, &info); |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 184 | default: |
Luca Foschiani | 689c968 | 2020-02-26 14:30:14 +0000 | [diff] [blame] | 185 | return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported GEMMLowpOutputStage type."); |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 186 | } |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 187 | } |
Sang-Hoon Park | a45abfd | 2020-08-17 13:50:15 +0100 | [diff] [blame] | 188 | } // namespace arm_compute |