blob: b225199c40e1ac4e60cfa0664aeccf1682269dd3 [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/runtime/cpu/operators/CpuPool2d.h"
Michele Di Giorgio19289042021-02-03 16:05:00 +000025
26#include "arm_compute/core/ITensor.h"
27#include "arm_compute/core/TensorInfo.h"
28#include "arm_compute/runtime/NEON/NEScheduler.h"
29#include "src/core/NEON/kernels/NEFillBorderKernel.h"
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010030#include "src/core/cpu/kernels/CpuPool2dKernel.h"
31#include "src/core/cpu/kernels/internal/CpuPool2dAssemblyWrapperKernel.h"
Michele Di Giorgio19289042021-02-03 16:05:00 +000032
33namespace arm_compute
34{
35namespace cpu
36{
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010037CpuPool2d::CpuPool2d()
Michele Di Giorgio0c19cbd2021-05-11 17:41:32 +010038 : _pooling_layer_kernel(),
39 _border_handler(),
40 _asm_glue(),
41 _is_global_pooling_layer(false),
42 _data_layout(DataLayout::NCHW),
43 _mem_req()
Michele Di Giorgio19289042021-02-03 16:05:00 +000044{
45}
46
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010047CpuPool2d::~CpuPool2d() = default;
Michele Di Giorgio19289042021-02-03 16:05:00 +000048
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010049void CpuPool2d::configure(ITensorInfo *src, ITensorInfo *dst, const PoolingLayerInfo &pool_info, ITensorInfo *indices)
Michele Di Giorgio19289042021-02-03 16:05:00 +000050{
51 // Check if we can run assembly kernels. Currently, indices are not supported by those kernels
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010052 const bool run_optimised = bool(kernels::CpuPool2dAssemblyWrapperKernel::validate(src, dst, pool_info)) && (indices == nullptr);
Michele Di Giorgio0c19cbd2021-05-11 17:41:32 +010053
54 // Get data layout
55 _data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
56
57 // Check if we have Global Pooling Layer
58 const unsigned int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
59 const unsigned int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
60 _is_global_pooling_layer = (src->dimension(idx_width) == pool_info.pool_size.width) && (src->dimension(idx_height) == pool_info.pool_size.height);
Michele Di Giorgio19289042021-02-03 16:05:00 +000061
62 if(run_optimised)
63 {
Michele Di Giorgio0c19cbd2021-05-11 17:41:32 +010064 const CPUInfo &ci = NEScheduler::get().cpu_info();
65 const unsigned int num_threads = NEScheduler::get().num_threads();
66
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010067 auto pooling_wrapper = std::make_unique<kernels::CpuPool2dAssemblyWrapperKernel>();
Michele Di Giorgio0c19cbd2021-05-11 17:41:32 +010068 ARM_COMPUTE_ERROR_ON(pooling_wrapper == nullptr);
69 pooling_wrapper->configure(src, dst, pool_info, ci);
70
71 // Get kernel's memory requirements
72 constexpr size_t alignment = 4096;
73 const size_t workspace_size = pooling_wrapper->get_working_size(num_threads);
74 _mem_req.push_back({ TensorType::ACL_INT_0, workspace_size, alignment });
75
76 _asm_glue = std::move(pooling_wrapper);
Michele Di Giorgio19289042021-02-03 16:05:00 +000077 }
78 else
79 {
Michele Di Giorgio19289042021-02-03 16:05:00 +000080 // Configure pooling kernel
Manuel Bottinib4bb6a02021-05-24 16:01:32 +010081 auto k = std::make_unique<kernels::CpuPool2dKernel>();
Michele Di Giorgio0c19cbd2021-05-11 17:41:32 +010082 k->configure(src, dst, pool_info, indices);
Michele Di Giorgio19289042021-02-03 16:05:00 +000083 _pooling_layer_kernel = std::move(k);
84
85 switch(_data_layout)
86 {
87 case DataLayout::NCHW:
88 {
89 // Configure border depending on operation required (quantize border in case of asymmetric data_type)
90 BorderMode border_mode = (!indices && pool_info.pool_type == PoolingType::MAX) ? BorderMode::REPLICATE : BorderMode::CONSTANT;
91 PixelValue zero_value((indices) ? std::numeric_limits<int>::min() : 0.f);
Michele Di Giorgio0c19cbd2021-05-11 17:41:32 +010092 if(is_data_type_quantized_asymmetric(src->data_type()) && !pool_info.exclude_padding)
Michele Di Giorgio19289042021-02-03 16:05:00 +000093 {
Michele Di Giorgio0c19cbd2021-05-11 17:41:32 +010094 zero_value = PixelValue(0, src->data_type(), src->quantization_info());
Michele Di Giorgio19289042021-02-03 16:05:00 +000095 }
96 auto b = std::make_unique<NEFillBorderKernel>();
Michele Di Giorgio0c19cbd2021-05-11 17:41:32 +010097 b->configure(src, _pooling_layer_kernel->border_size(), border_mode, zero_value);
Michele Di Giorgio19289042021-02-03 16:05:00 +000098 _border_handler = std::move(b);
99 break;
100 }
101 case DataLayout::NHWC:
102 break;
103 default:
104 ARM_COMPUTE_ERROR("Data layout not supported");
105 }
106 }
107}
108
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100109Status CpuPool2d::validate(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &pool_info, const ITensorInfo *indices)
Michele Di Giorgio19289042021-02-03 16:05:00 +0000110{
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100111 const bool run_optimised = bool(kernels::CpuPool2dAssemblyWrapperKernel::validate(src, dst, pool_info)) && (indices == nullptr);
Michele Di Giorgio19289042021-02-03 16:05:00 +0000112
113 if(run_optimised)
114 {
115 return Status{};
116 }
117
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100118 return kernels::CpuPool2dKernel::validate(src, dst, pool_info, indices);
Michele Di Giorgio19289042021-02-03 16:05:00 +0000119}
120
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100121void CpuPool2d::run(ITensorPack &tensors)
Michele Di Giorgio19289042021-02-03 16:05:00 +0000122{
Michele Di Giorgio0c19cbd2021-05-11 17:41:32 +0100123 ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No tensors provided");
124
125 if(_asm_glue)
Michele Di Giorgio19289042021-02-03 16:05:00 +0000126 {
Michele Di Giorgio0c19cbd2021-05-11 17:41:32 +0100127 const auto hints = (_is_global_pooling_layer) ? Window::DimX : Window::DimY;
128 NEScheduler::get().schedule_op(_asm_glue.get(), hints, _asm_glue->window(), tensors);
Michele Di Giorgio19289042021-02-03 16:05:00 +0000129 }
130 else
131 {
132 switch(_data_layout)
133 {
134 case DataLayout::NCHW:
135 // Fill border
136 NEScheduler::get().schedule_op(_border_handler.get(), Window::DimY, _border_handler->window(), tensors);
137
138 // Run pooling layer
139 NEScheduler::get().schedule_op(_pooling_layer_kernel.get(), _is_global_pooling_layer ? Window::DimZ : Window::DimY, _pooling_layer_kernel->window(), tensors);
140 break;
141 case DataLayout::NHWC:
142 // Run pooling layer
143 NEScheduler::get().schedule_op(_pooling_layer_kernel.get(), Window::DimX, _pooling_layer_kernel->window(), tensors);
144 break;
145 default:
146 ARM_COMPUTE_ERROR("Data layout not supported");
147 }
148 }
149}
Michele Di Giorgio0c19cbd2021-05-11 17:41:32 +0100150
Manuel Bottinib4bb6a02021-05-24 16:01:32 +0100151experimental::MemoryRequirements CpuPool2d::workspace() const
Michele Di Giorgio0c19cbd2021-05-11 17:41:32 +0100152{
153 return _mem_req;
154}
Michele Di Giorgio19289042021-02-03 16:05:00 +0000155} // namespace cpu
156} // namespace arm_compute