Michele Di Giorgio | d556d7b | 2020-10-27 10:56:31 +0000 | [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 | */ |
| 24 | #include "src/core/NEON/kernels/assembly/NEPoolingAssemblyWrapperKernel.h" |
| 25 | #include "arm_compute/core/Utils.h" |
| 26 | #include "arm_compute/core/Validate.h" |
| 27 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 28 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
| 29 | #include "src/core/helpers/AutoConfiguration.h" |
| 30 | #include "src/core/helpers/WindowHelpers.h" |
| 31 | |
| 32 | #include <arm_neon.h> |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | using namespace arm_compute::misc::shape_calculator; |
| 37 | |
| 38 | void NEPoolingAssemblyWrapperKernel::configure(const ITensorInfo *input, ITensorInfo *output, const PoolingLayerInfo &info, const CPUInfo &cpu_info) |
| 39 | { |
| 40 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 41 | |
| 42 | // Output initialization if not yet initialized |
| 43 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_pool_shape(*input, info))); |
| 44 | |
| 45 | const bool requantize = input->quantization_info() != output->quantization_info(); |
| 46 | |
| 47 | switch(input->data_type()) |
| 48 | { |
| 49 | case DataType::QASYMM8: |
| 50 | if(requantize) |
| 51 | { |
| 52 | create_arm_pooling_requant<uint8_t, uint8_t>(input, output, info, cpu_info); |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | create_arm_pooling<uint8_t, uint8_t>(input, output, info, cpu_info); |
| 57 | } |
| 58 | break; |
| 59 | case DataType::QASYMM8_SIGNED: |
| 60 | if(requantize) |
| 61 | { |
| 62 | create_arm_pooling_requant<int8_t, int8_t>(input, output, info, cpu_info); |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | create_arm_pooling<int8_t, int8_t>(input, output, info, cpu_info); |
| 67 | } |
| 68 | break; |
| 69 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 70 | case DataType::F16: |
| 71 | create_arm_pooling<float16_t, float16_t>(input, output, info, cpu_info); |
| 72 | break; |
| 73 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 74 | case DataType::F32: |
| 75 | create_arm_pooling<float, float>(input, output, info, cpu_info); |
| 76 | break; |
| 77 | default: |
| 78 | break; |
| 79 | } |
| 80 | |
| 81 | Window win = calculate_max_window(*output, Steps()); |
| 82 | INEKernel::configure(win); |
| 83 | } |
| 84 | |
| 85 | Status NEPoolingAssemblyWrapperKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &info) |
| 86 | { |
| 87 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 88 | |
| 89 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32); |
| 90 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((input->data_layout() != DataLayout::NHWC) || (info.data_layout != DataLayout::NHWC), "Only NHWC is supported by assembly kernels"); |
| 91 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((info.pool_type != PoolingType::AVG) && (info.pool_type != PoolingType::MAX), |
| 92 | "Only AVG and MAX pooling are supported by assembly kernels"); |
| 93 | |
| 94 | if(output->total_size() > 0) |
| 95 | { |
| 96 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 97 | |
| 98 | const auto input_qinfo = input->quantization_info().uniform(); |
| 99 | const auto output_qinfo = output->quantization_info().uniform(); |
| 100 | |
| 101 | if(input_qinfo != output_qinfo) |
| 102 | { |
| 103 | const float multiplier = input_qinfo.scale / output_qinfo.scale; |
| 104 | int32_t output_multiplier{}; |
| 105 | int32_t output_shift{}; |
| 106 | ARM_COMPUTE_RETURN_ERROR_ON(quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift)); |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | if(input->data_type() == DataType::QASYMM8) |
| 111 | { |
| 112 | const bool has_padding = info.pad_stride_info.has_padding(); |
| 113 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!info.exclude_padding && has_padding, "Assembly kernels do not support padding for QASYMM8 with same input/output quantization info"); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | if(input->data_type() == DataType::QASYMM8) |
| 120 | { |
| 121 | // If output is not configured, the quantization info are the same |
| 122 | const bool has_padding = info.pad_stride_info.has_padding(); |
| 123 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!info.exclude_padding && has_padding, "Assembly kernels do not support padding for QASYMM8 with same input/output quantization info"); |
| 124 | } |
| 125 | } |
| 126 | return Status{}; |
| 127 | } |
| 128 | |
| 129 | void NEPoolingAssemblyWrapperKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
| 130 | { |
| 131 | ARM_COMPUTE_ERROR_ON_NULLPTR(_kernel_asm.get()); |
| 132 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 133 | ARM_COMPUTE_UNUSED(window); |
| 134 | ARM_COMPUTE_UNUSED(info); |
| 135 | |
| 136 | ARM_COMPUTE_ERROR_ON(tensors.empty()); |
| 137 | |
| 138 | const ITensor *input = tensors.get_const_tensor(TensorType::ACL_SRC); |
| 139 | ITensor *output = tensors.get_tensor(TensorType::ACL_DST_0); |
| 140 | ITensor *workspace = tensors.get_tensor(TensorType::ACL_DST_1); |
| 141 | |
| 142 | const auto in_ptr = input->buffer() + input->info()->offset_first_element_in_bytes(); |
| 143 | auto out_ptr = output->buffer() + output->info()->offset_first_element_in_bytes(); |
| 144 | auto working_space = workspace->buffer() + workspace->info()->offset_first_element_in_bytes(); |
| 145 | |
| 146 | _kernel_asm->execute(in_ptr, out_ptr, working_space, info.thread_id, info.num_threads); |
| 147 | } |
| 148 | |
| 149 | size_t NEPoolingAssemblyWrapperKernel::get_working_size(unsigned int num_threads) const |
| 150 | { |
| 151 | return _kernel_asm->get_working_size(num_threads); |
| 152 | } |
| 153 | |
| 154 | bool NEPoolingAssemblyWrapperKernel::is_configured() const |
| 155 | { |
| 156 | return _kernel_asm != nullptr; |
| 157 | } |
| 158 | |
| 159 | template <typename TypeInput, typename TypeOutput> |
| 160 | void NEPoolingAssemblyWrapperKernel::create_arm_pooling(const ITensorInfo *input, ITensorInfo *output, const PoolingLayerInfo &info, const CPUInfo &cpu_info) |
| 161 | { |
| 162 | const arm_conv::pooling::PoolingType pool_type = (info.pool_type == PoolingType::AVG) ? arm_conv::pooling::PoolingType::AVERAGE : arm_conv::pooling::PoolingType::MAX; |
| 163 | |
| 164 | arm_conv::pooling::PoolingWindow window{}; |
| 165 | window.cols = static_cast<unsigned int>(info.pool_size.x()); |
| 166 | window.rows = static_cast<unsigned int>(info.pool_size.y()); |
| 167 | |
| 168 | arm_conv::pooling::PoolingStride stride{}; |
| 169 | std::tie(stride.cols, stride.rows) = info.pad_stride_info.stride(); |
| 170 | |
| 171 | const arm_conv::pooling::PaddingValues padding{ info.pad_stride_info.pad_left(), info.pad_stride_info.pad_top(), info.pad_stride_info.pad_right(), info.pad_stride_info.pad_bottom() }; |
| 172 | |
| 173 | constexpr unsigned int idx_width = 1; |
| 174 | constexpr unsigned int idx_height = 2; |
| 175 | constexpr unsigned int idx_channels = 0; |
| 176 | constexpr unsigned int idx_batches = 3; |
| 177 | |
| 178 | const unsigned int n_batches = input->dimension(idx_batches); |
| 179 | const unsigned int input_rows = input->dimension(idx_height); |
| 180 | const unsigned int input_cols = input->dimension(idx_width); |
| 181 | const unsigned int n_channels = input->dimension(idx_channels); |
| 182 | const unsigned int output_rows = output->dimension(idx_height); |
| 183 | const unsigned int output_cols = output->dimension(idx_width); |
| 184 | |
| 185 | arm_conv::pooling::PoolingArgs args(&cpu_info, pool_type, window, stride, info.exclude_padding, n_batches, input_rows, input_cols, n_channels, output_rows, output_cols, padding, nullptr); |
| 186 | |
| 187 | // Configure assembly pooling kernel |
| 188 | auto pooling_kernel_asm = arm_conv::pooling::pooling<TypeInput, TypeOutput>(args); |
| 189 | if(pooling_kernel_asm == nullptr) |
| 190 | { |
| 191 | // Configuration not supported: Leave function unconfigured: |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | _kernel_asm = std::move(pooling_kernel_asm); |
| 196 | } |
| 197 | |
| 198 | template <typename TypeInput, typename TypeOutput> |
| 199 | void NEPoolingAssemblyWrapperKernel::create_arm_pooling_requant(const ITensorInfo *input, ITensorInfo *output, const PoolingLayerInfo &info, const CPUInfo &cpu_info) |
| 200 | { |
| 201 | const arm_conv::pooling::PoolingType pool_type = (info.pool_type == PoolingType::AVG) ? arm_conv::pooling::PoolingType::AVERAGE : arm_conv::pooling::PoolingType::MAX; |
| 202 | |
| 203 | arm_conv::pooling::PoolingWindow window{}; |
| 204 | window.cols = static_cast<unsigned int>(info.pool_size.x()); |
| 205 | window.rows = static_cast<unsigned int>(info.pool_size.y()); |
| 206 | |
| 207 | arm_conv::pooling::PoolingStride stride{}; |
| 208 | std::tie(stride.cols, stride.rows) = info.pad_stride_info.stride(); |
| 209 | |
| 210 | const arm_conv::pooling::PaddingValues padding{ info.pad_stride_info.pad_left(), info.pad_stride_info.pad_top(), info.pad_stride_info.pad_right(), info.pad_stride_info.pad_bottom() }; |
| 211 | |
| 212 | constexpr unsigned int idx_width = 1; |
| 213 | constexpr unsigned int idx_height = 2; |
| 214 | constexpr unsigned int idx_channels = 0; |
| 215 | constexpr unsigned int idx_batches = 3; |
| 216 | |
| 217 | const unsigned int n_batches = input->dimension(idx_batches); |
| 218 | const unsigned int input_rows = input->dimension(idx_height); |
| 219 | const unsigned int input_cols = input->dimension(idx_width); |
| 220 | const unsigned int n_channels = input->dimension(idx_channels); |
| 221 | const unsigned int output_rows = output->dimension(idx_height); |
| 222 | const unsigned int output_cols = output->dimension(idx_width); |
| 223 | |
| 224 | arm_conv::pooling::PoolingArgs args(&cpu_info, pool_type, window, stride, info.exclude_padding, n_batches, input_rows, input_cols, n_channels, output_rows, output_cols, padding, nullptr); |
| 225 | |
| 226 | const auto input_qinfo = input->quantization_info().uniform(); |
| 227 | const auto output_qinfo = output->quantization_info().uniform(); |
| 228 | |
| 229 | const float multiplier = input_qinfo.scale / output_qinfo.scale; |
| 230 | int32_t output_multiplier{}; |
| 231 | int32_t output_shift{}; |
| 232 | quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift); |
| 233 | |
| 234 | const arm_conv::pooling::Requantize32 requant_args(input_qinfo.offset, |
| 235 | output_qinfo.offset, |
| 236 | output_shift, // left shift |
| 237 | 0, // right shift |
| 238 | output_multiplier); |
| 239 | |
| 240 | // Configure assembly pooling kernel with requantization |
| 241 | auto pooling_kernel_asm = arm_conv::pooling::pooling<TypeInput, TypeOutput, arm_conv::pooling::Requantize32>(args, requant_args); |
| 242 | if(pooling_kernel_asm == nullptr) |
| 243 | { |
| 244 | // Configuration not supported: Leave function unconfigured: |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | _kernel_asm = std::move(pooling_kernel_asm); |
| 249 | } |
| 250 | } // namespace arm_compute |