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