blob: 89dd27a20a5161eb31c44dbcffe8de0ce4b85d54 [file] [log] [blame]
Michele Di Giorgio19289042021-02-03 16:05:00 +00001/*
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 */
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010024#include "src/core/cpu/kernels/internal/CpuPool2dAssemblyWrapperKernel.h"
Michele Di Giorgio19289042021-02-03 16:05:00 +000025#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/CPP/Validate.h"
30#include "src/core/NEON/INEKernel.h"
31#include "src/core/helpers/AutoConfiguration.h"
32#include "src/core/helpers/WindowHelpers.h"
33
34#include <arm_neon.h>
35
36namespace arm_compute
37{
38namespace cpu
39{
40namespace kernels
41{
42using namespace arm_compute::misc::shape_calculator;
43
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010044void CpuPool2dAssemblyWrapperKernel::configure(const ITensorInfo *src, ITensorInfo *dst, const PoolingLayerInfo &info, const CPUInfo &cpu_info)
Michele Di Giorgio19289042021-02-03 16:05:00 +000045{
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000046 ARM_COMPUTE_UNUSED(cpu_info);
Michele Di Giorgio19289042021-02-03 16:05:00 +000047 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
48
49 // dst initialization if not yet initialized
50 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(compute_pool_shape(*src, info)));
51
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000052#if defined(__aarch64__)
Michele Di Giorgio19289042021-02-03 16:05:00 +000053 const bool requantize = src->quantization_info() != dst->quantization_info();
54
55 switch(src->data_type())
56 {
57 case DataType::QASYMM8:
58 if(requantize)
59 {
60 create_arm_pooling_requant<uint8_t, uint8_t>(src, dst, info, cpu_info);
61 }
62 else
63 {
64 create_arm_pooling<uint8_t, uint8_t>(src, dst, info, cpu_info);
65 }
66 break;
67 case DataType::QASYMM8_SIGNED:
68 if(requantize)
69 {
70 create_arm_pooling_requant<int8_t, int8_t>(src, dst, info, cpu_info);
71 }
72 else
73 {
74 create_arm_pooling<int8_t, int8_t>(src, dst, info, cpu_info);
75 }
76 break;
77#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
78 case DataType::F16:
79 create_arm_pooling<float16_t, float16_t>(src, dst, info, cpu_info);
80 break;
81#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
82 case DataType::F32:
83 create_arm_pooling<float, float>(src, dst, info, cpu_info);
84 break;
85 default:
86 break;
87 }
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000088#endif // defined(__aarch64__)
Michele Di Giorgio19289042021-02-03 16:05:00 +000089
90 Window win = calculate_max_window(*dst, Steps());
91 INEKernel::configure(win);
92}
93
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010094Status CpuPool2dAssemblyWrapperKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &info)
Michele Di Giorgio19289042021-02-03 16:05:00 +000095{
96 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
97
98#ifndef __aarch64__
99 ARM_COMPUTE_RETURN_ERROR_MSG("32-bit is not supported by assembly kernels");
100#endif /* __aarch64__ */
101 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src);
102 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
103 ARM_COMPUTE_RETURN_ERROR_ON_MSG((src->data_layout() != DataLayout::NHWC) || (info.data_layout != DataLayout::NHWC), "Only NHWC is supported by assembly kernels");
104 ARM_COMPUTE_RETURN_ERROR_ON_MSG((info.pool_type != PoolingType::AVG) && (info.pool_type != PoolingType::MAX),
105 "Only AVG and MAX pooling are supported by assembly kernels");
106
107 if(dst->total_size() > 0)
108 {
109 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
110
111 const auto src_qinfo = src->quantization_info().uniform();
112 const auto dst_qinfo = dst->quantization_info().uniform();
113
114 if(src_qinfo != dst_qinfo)
115 {
116 const float multiplier = src_qinfo.scale / dst_qinfo.scale;
117 int32_t dst_multiplier{};
118 int32_t dst_shift{};
119 ARM_COMPUTE_RETURN_ERROR_ON(quantization::calculate_quantized_multiplier(multiplier, &dst_multiplier, &dst_shift));
120 }
121 else
122 {
123 if(src->data_type() == DataType::QASYMM8)
124 {
125 const bool has_padding = info.pad_stride_info.has_padding();
126 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!info.exclude_padding && has_padding, "Assembly kernels do not support padding for QASYMM8 with same src/dst quantization info");
127 }
128 }
129 }
130 else
131 {
132 if(src->data_type() == DataType::QASYMM8)
133 {
134 // If dst is not configured, the quantization info are the same
135 const bool has_padding = info.pad_stride_info.has_padding();
136 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!info.exclude_padding && has_padding, "Assembly kernels do not support padding for QASYMM8 with same src/dst quantization info");
137 }
138 }
139 return Status{};
140}
141
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100142void CpuPool2dAssemblyWrapperKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Michele Di Giorgio19289042021-02-03 16:05:00 +0000143{
144 ARM_COMPUTE_ERROR_ON_NULLPTR(_kernel_asm.get());
145 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
146 ARM_COMPUTE_UNUSED(window);
147 ARM_COMPUTE_UNUSED(info);
148
149 ARM_COMPUTE_ERROR_ON(tensors.empty());
150
151 const ITensor *src = tensors.get_const_tensor(TensorType::ACL_SRC);
Michele Di Giorgio0c19cbd2021-05-11 17:41:32 +0100152 ITensor *dst = tensors.get_tensor(TensorType::ACL_DST);
153 ITensor *workspace = tensors.get_tensor(TensorType::ACL_INT_0);
Michele Di Giorgio19289042021-02-03 16:05:00 +0000154
155 const auto in_ptr = src->buffer() + src->info()->offset_first_element_in_bytes();
156 auto out_ptr = dst->buffer() + dst->info()->offset_first_element_in_bytes();
157 auto working_space = workspace->buffer() + workspace->info()->offset_first_element_in_bytes();
158
159 const auto src_shape = src->info()->tensor_shape();
160 const auto dst_shape = dst->info()->tensor_shape();
161 const auto src_padding = src->info()->padding();
162 const auto dst_padding = dst->info()->padding();
163
164 const size_t ld_src_col = src_shape[0] + src_padding.left + src_padding.right;
165 const size_t ld_src_row = ld_src_col * (src_shape[1] + src_padding.top + src_padding.bottom);
166 const size_t ld_src_batch = ld_src_row * src_shape[2];
167 const size_t ld_dst_col = dst_shape[0] + dst_padding.left + dst_padding.right;
168 const size_t ld_dst_row = ld_dst_col * (dst_shape[1] + dst_padding.top + dst_padding.bottom);
169 const size_t ld_dst_batch = ld_dst_row * dst_shape[2];
170
171 _kernel_asm->execute(in_ptr, ld_src_col, ld_src_row, ld_src_batch,
172 out_ptr, ld_dst_col, ld_dst_row, ld_dst_batch,
173 working_space, info.thread_id, info.num_threads);
174}
175
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100176size_t CpuPool2dAssemblyWrapperKernel::get_working_size(unsigned int num_threads) const
Michele Di Giorgio19289042021-02-03 16:05:00 +0000177{
178 return _kernel_asm->get_working_size(num_threads);
179}
180
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100181bool CpuPool2dAssemblyWrapperKernel::is_configured() const
Michele Di Giorgio19289042021-02-03 16:05:00 +0000182{
183 return _kernel_asm != nullptr;
184}
185
186template <typename Typesrc, typename Typedst>
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100187void CpuPool2dAssemblyWrapperKernel::create_arm_pooling(const ITensorInfo *src, ITensorInfo *dst, const PoolingLayerInfo &info, const CPUInfo &cpu_info)
Michele Di Giorgio19289042021-02-03 16:05:00 +0000188{
189 const arm_conv::pooling::PoolingType pool_type = (info.pool_type == PoolingType::AVG) ? arm_conv::pooling::PoolingType::AVERAGE : arm_conv::pooling::PoolingType::MAX;
190
191 arm_conv::pooling::PoolingWindow window{};
192 window.cols = static_cast<unsigned int>(info.pool_size.x());
193 window.rows = static_cast<unsigned int>(info.pool_size.y());
194
195 arm_conv::pooling::PoolingStride stride{};
196 std::tie(stride.cols, stride.rows) = info.pad_stride_info.stride();
197
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000198 const arm_conv::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() };
Michele Di Giorgio19289042021-02-03 16:05:00 +0000199
200 constexpr unsigned int idx_width = 1;
201 constexpr unsigned int idx_height = 2;
202 constexpr unsigned int idx_channels = 0;
203 constexpr unsigned int idx_batches = 3;
204
205 const unsigned int n_batches = src->dimension(idx_batches);
206 const unsigned int src_rows = src->dimension(idx_height);
207 const unsigned int src_cols = src->dimension(idx_width);
208 const unsigned int n_channels = src->dimension(idx_channels);
209 const unsigned int dst_rows = dst->dimension(idx_height);
210 const unsigned int dst_cols = dst->dimension(idx_width);
211
212 arm_conv::pooling::PoolingArgs args(&cpu_info, pool_type, window, stride, info.exclude_padding, n_batches, src_rows, src_cols, n_channels, dst_rows, dst_cols, padding, nullptr);
213
214 // Configure assembly pooling kernel
215 auto pooling_kernel_asm = arm_conv::pooling::pooling<Typesrc, Typedst>(args);
216 if(pooling_kernel_asm == nullptr)
217 {
218 // Configuration not supported: Leave function unconfigured:
219 return;
220 }
221
222 _kernel_asm = std::move(pooling_kernel_asm);
223}
224
225template <typename Typesrc, typename Typedst>
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100226void CpuPool2dAssemblyWrapperKernel::create_arm_pooling_requant(const ITensorInfo *src, ITensorInfo *dst, const PoolingLayerInfo &info, const CPUInfo &cpu_info)
Michele Di Giorgio19289042021-02-03 16:05:00 +0000227{
228 const arm_conv::pooling::PoolingType pool_type = (info.pool_type == PoolingType::AVG) ? arm_conv::pooling::PoolingType::AVERAGE : arm_conv::pooling::PoolingType::MAX;
229
230 arm_conv::pooling::PoolingWindow window{};
231 window.cols = static_cast<unsigned int>(info.pool_size.x());
232 window.rows = static_cast<unsigned int>(info.pool_size.y());
233
234 arm_conv::pooling::PoolingStride stride{};
235 std::tie(stride.cols, stride.rows) = info.pad_stride_info.stride();
236
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000237 const arm_conv::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() };
Michele Di Giorgio19289042021-02-03 16:05:00 +0000238
239 constexpr unsigned int idx_width = 1;
240 constexpr unsigned int idx_height = 2;
241 constexpr unsigned int idx_channels = 0;
242 constexpr unsigned int idx_batches = 3;
243
244 const unsigned int n_batches = src->dimension(idx_batches);
245 const unsigned int src_rows = src->dimension(idx_height);
246 const unsigned int src_cols = src->dimension(idx_width);
247 const unsigned int n_channels = src->dimension(idx_channels);
248 const unsigned int dst_rows = dst->dimension(idx_height);
249 const unsigned int dst_cols = dst->dimension(idx_width);
250
251 arm_conv::pooling::PoolingArgs args(&cpu_info, pool_type, window, stride, info.exclude_padding, n_batches, src_rows, src_cols, n_channels, dst_rows, dst_cols, padding, nullptr);
252
253 const auto src_qinfo = src->quantization_info().uniform();
254 const auto dst_qinfo = dst->quantization_info().uniform();
255
256 const float multiplier = src_qinfo.scale / dst_qinfo.scale;
257 int32_t dst_multiplier{};
258 int32_t dst_shift{};
259 quantization::calculate_quantized_multiplier(multiplier, &dst_multiplier, &dst_shift);
260
261 const arm_conv::pooling::Requantize32 requant_args(src_qinfo.offset,
262 dst_qinfo.offset,
263 dst_shift, // left shift
264 0, // right shift
265 dst_multiplier);
266
267 // Configure assembly pooling kernel with requantization
268 auto pooling_kernel_asm = arm_conv::pooling::pooling<Typesrc, Typedst, arm_conv::pooling::Requantize32>(args, requant_args);
269 if(pooling_kernel_asm == nullptr)
270 {
271 // Configuration not supported: Leave function unconfigured:
272 return;
273 }
274
275 _kernel_asm = std::move(pooling_kernel_asm);
276}
277} // namespace kernels
278} // namespace cpu
279} // namespace arm_compute