Manuel Bottini | ae58bdf | 2021-06-17 17:18:45 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 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 | */ |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/cpu/operators/CpuGemmLowpOutputStage.h" |
Manuel Bottini | ae58bdf | 2021-06-17 17:18:45 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/ITensor.h" |
| 27 | #include "arm_compute/core/Validate.h" |
| 28 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 29 | #include "src/cpu/kernels/CpuGemmLowpQuantizeDownInt32ScaleKernel.h" |
| 30 | #include "src/cpu/kernels/CpuGemmLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel.h" |
| 31 | #include "src/cpu/kernels/CpuGemmLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel.h" |
| 32 | #include "src/cpu/kernels/CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel.h" |
Manuel Bottini | ae58bdf | 2021-06-17 17:18:45 +0100 | [diff] [blame] | 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace cpu |
| 37 | { |
| 38 | void CpuGemmLowpOutputStage::configure(ITensorInfo *src, ITensorInfo *bias, ITensorInfo *dst, const GEMMLowpOutputStageInfo &info) |
| 39 | { |
| 40 | // Perform validate step |
| 41 | ARM_COMPUTE_ERROR_THROW_ON(CpuGemmLowpOutputStage::validate(src, bias, dst, info)); |
| 42 | |
| 43 | switch(info.type) |
| 44 | { |
| 45 | case GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT: |
| 46 | { |
| 47 | switch(info.output_data_type) |
| 48 | { |
| 49 | case DataType::QASYMM8: |
| 50 | { |
| 51 | auto k = std::make_unique<kernels::CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel>(); |
| 52 | k->configure(src, bias, dst, info.gemmlowp_multiplier, info.gemmlowp_shift, info.gemmlowp_offset, info.gemmlowp_min_bound, info.gemmlowp_max_bound); |
| 53 | _kernel = std::move(k); |
| 54 | break; |
| 55 | } |
| 56 | case DataType::QASYMM8_SIGNED: |
| 57 | { |
| 58 | auto k = std::make_unique<kernels::CpuGemmLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel>(); |
| 59 | k->configure(src, bias, dst, info.gemmlowp_multiplier, info.gemmlowp_shift, info.gemmlowp_offset, info.gemmlowp_min_bound, info.gemmlowp_max_bound); |
| 60 | _kernel = std::move(k); |
| 61 | break; |
| 62 | } |
| 63 | case DataType::QSYMM16: |
| 64 | { |
| 65 | auto k = std::make_unique<kernels::CpuGemmLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel>(); |
| 66 | k->configure(src, bias, dst, info.gemmlowp_multiplier, info.gemmlowp_shift, info.gemmlowp_min_bound, info.gemmlowp_max_bound); |
| 67 | _kernel = std::move(k); |
| 68 | break; |
| 69 | } |
| 70 | default: |
| 71 | { |
| 72 | ARM_COMPUTE_ERROR("Unsupported output data type."); |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | break; |
| 77 | } |
| 78 | case GEMMLowpOutputStageType::QUANTIZE_DOWN: |
| 79 | { |
| 80 | switch(info.output_data_type) |
| 81 | { |
| 82 | case DataType::QASYMM8: |
| 83 | case DataType::QASYMM8_SIGNED: |
| 84 | { |
| 85 | auto k = std::make_unique<kernels::CpuGemmLowpQuantizeDownInt32ScaleKernel>(); |
| 86 | k->configure(src, bias, dst, &info); |
| 87 | _kernel = std::move(k); |
| 88 | break; |
| 89 | } |
| 90 | default: |
| 91 | { |
| 92 | ARM_COMPUTE_ERROR("Unsupported output data type."); |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | break; |
| 97 | } |
| 98 | default: |
| 99 | ARM_COMPUTE_ERROR("Unsupported GEMMLowpOutputStage type."); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | Status CpuGemmLowpOutputStage::validate(const ITensorInfo *src, const ITensorInfo *bias, const ITensorInfo *dst, const GEMMLowpOutputStageInfo &info) |
| 104 | { |
| 105 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 106 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->data_type() == DataType::UNKNOWN, "CpuGemmLowpOutputStage cannot be used with UNKNOWN output data type."); |
| 107 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM16); |
| 108 | ARM_COMPUTE_RETURN_ERROR_ON((info.type != GEMMLowpOutputStageType::QUANTIZE_DOWN) && (info.type != GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT)); |
| 109 | |
| 110 | switch(info.type) |
| 111 | { |
| 112 | case GEMMLowpOutputStageType::QUANTIZE_DOWN_FIXEDPOINT: |
| 113 | { |
| 114 | switch(dst->data_type()) |
| 115 | { |
| 116 | case DataType::QASYMM8: |
| 117 | return kernels::CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::validate(src, bias, dst, info.gemmlowp_min_bound, info.gemmlowp_max_bound); |
| 118 | case DataType::QASYMM8_SIGNED: |
| 119 | return kernels::CpuGemmLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::validate(src, bias, dst, info.gemmlowp_min_bound, info.gemmlowp_max_bound); |
| 120 | case DataType::QSYMM16: |
| 121 | return kernels::CpuGemmLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::validate(src, bias, dst, info.gemmlowp_min_bound, info.gemmlowp_max_bound); |
| 122 | default: |
| 123 | return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported output data type."); |
| 124 | } |
| 125 | } |
| 126 | case GEMMLowpOutputStageType::QUANTIZE_DOWN: |
| 127 | { |
| 128 | switch(dst->data_type()) |
| 129 | { |
| 130 | case DataType::QASYMM8: |
| 131 | case DataType::QASYMM8_SIGNED: |
| 132 | return kernels::CpuGemmLowpQuantizeDownInt32ScaleKernel::validate(src, bias, dst, &info); |
| 133 | default: |
| 134 | return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported output data type."); |
| 135 | } |
| 136 | } |
| 137 | default: |
| 138 | return ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Unsupported GEMMLowpOutputStage type."); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void CpuGemmLowpOutputStage::run(ITensorPack &tensors) |
| 143 | { |
| 144 | NEScheduler::get().schedule_op(_kernel.get(), Window::DimY, _kernel->window(), tensors); |
| 145 | } |
| 146 | } // namespace cpu |
| 147 | } // namespace arm_compute |