Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +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 | */ |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/cpu/operators/CpuPool2d.h" |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/ITensor.h" |
| 27 | #include "arm_compute/core/TensorInfo.h" |
| 28 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
ramelg01 | 3ae3d88 | 2021-09-12 23:07:47 +0100 | [diff] [blame^] | 29 | #include "src/common/utils/Log.h" |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 30 | #include "src/core/NEON/kernels/NEFillBorderKernel.h" |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 31 | #include "src/cpu/kernels/CpuPool2dKernel.h" |
| 32 | #include "src/cpu/kernels/internal/CpuPool2dAssemblyWrapperKernel.h" |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 33 | |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 34 | using namespace arm_compute::experimental; |
| 35 | |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace cpu |
| 39 | { |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 40 | CpuPool2d::CpuPool2d() |
Michele Di Giorgio | 0c19cbd | 2021-05-11 17:41:32 +0100 | [diff] [blame] | 41 | : _pooling_layer_kernel(), |
| 42 | _border_handler(), |
| 43 | _asm_glue(), |
| 44 | _is_global_pooling_layer(false), |
| 45 | _data_layout(DataLayout::NCHW), |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 46 | _aux_mem(1) |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 47 | { |
| 48 | } |
| 49 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 50 | CpuPool2d::~CpuPool2d() = default; |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 51 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 52 | void CpuPool2d::configure(ITensorInfo *src, ITensorInfo *dst, const PoolingLayerInfo &pool_info, ITensorInfo *indices) |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 53 | { |
ramelg01 | 3ae3d88 | 2021-09-12 23:07:47 +0100 | [diff] [blame^] | 54 | ARM_COMPUTE_LOG_PARAMS(src, dst, pool_info, indices); |
| 55 | |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 56 | // Check if we can run assembly kernels. Currently, indices are not supported by those kernels |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 57 | const bool run_optimised = bool(kernels::CpuPool2dAssemblyWrapperKernel::validate(src, dst, pool_info)) && (indices == nullptr); |
Michele Di Giorgio | 0c19cbd | 2021-05-11 17:41:32 +0100 | [diff] [blame] | 58 | |
| 59 | // Get data layout |
| 60 | _data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout; |
| 61 | |
| 62 | // Check if we have Global Pooling Layer |
| 63 | const unsigned int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH); |
| 64 | const unsigned int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT); |
| 65 | _is_global_pooling_layer = (src->dimension(idx_width) == pool_info.pool_size.width) && (src->dimension(idx_height) == pool_info.pool_size.height); |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 66 | |
| 67 | if(run_optimised) |
| 68 | { |
Michele Di Giorgio | 0c19cbd | 2021-05-11 17:41:32 +0100 | [diff] [blame] | 69 | const CPUInfo &ci = NEScheduler::get().cpu_info(); |
| 70 | const unsigned int num_threads = NEScheduler::get().num_threads(); |
| 71 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 72 | auto pooling_wrapper = std::make_unique<kernels::CpuPool2dAssemblyWrapperKernel>(); |
Michele Di Giorgio | 0c19cbd | 2021-05-11 17:41:32 +0100 | [diff] [blame] | 73 | ARM_COMPUTE_ERROR_ON(pooling_wrapper == nullptr); |
| 74 | pooling_wrapper->configure(src, dst, pool_info, ci); |
| 75 | |
| 76 | // Get kernel's memory requirements |
| 77 | constexpr size_t alignment = 4096; |
| 78 | const size_t workspace_size = pooling_wrapper->get_working_size(num_threads); |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 79 | _aux_mem[0] = MemoryInfo(TensorType::ACL_INT_0, MemoryLifetime::Temporary, workspace_size, alignment); |
Michele Di Giorgio | 0c19cbd | 2021-05-11 17:41:32 +0100 | [diff] [blame] | 80 | |
| 81 | _asm_glue = std::move(pooling_wrapper); |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 82 | } |
| 83 | else |
| 84 | { |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 85 | // Configure pooling kernel |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 86 | auto k = std::make_unique<kernels::CpuPool2dKernel>(); |
Michele Di Giorgio | 0c19cbd | 2021-05-11 17:41:32 +0100 | [diff] [blame] | 87 | k->configure(src, dst, pool_info, indices); |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 88 | _pooling_layer_kernel = std::move(k); |
| 89 | |
| 90 | switch(_data_layout) |
| 91 | { |
| 92 | case DataLayout::NCHW: |
| 93 | { |
| 94 | // Configure border depending on operation required (quantize border in case of asymmetric data_type) |
| 95 | BorderMode border_mode = (!indices && pool_info.pool_type == PoolingType::MAX) ? BorderMode::REPLICATE : BorderMode::CONSTANT; |
| 96 | PixelValue zero_value((indices) ? std::numeric_limits<int>::min() : 0.f); |
Michele Di Giorgio | 0c19cbd | 2021-05-11 17:41:32 +0100 | [diff] [blame] | 97 | if(is_data_type_quantized_asymmetric(src->data_type()) && !pool_info.exclude_padding) |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 98 | { |
Michele Di Giorgio | 0c19cbd | 2021-05-11 17:41:32 +0100 | [diff] [blame] | 99 | zero_value = PixelValue(0, src->data_type(), src->quantization_info()); |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 100 | } |
| 101 | auto b = std::make_unique<NEFillBorderKernel>(); |
Michele Di Giorgio | 0c19cbd | 2021-05-11 17:41:32 +0100 | [diff] [blame] | 102 | b->configure(src, _pooling_layer_kernel->border_size(), border_mode, zero_value); |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 103 | _border_handler = std::move(b); |
| 104 | break; |
| 105 | } |
| 106 | case DataLayout::NHWC: |
| 107 | break; |
| 108 | default: |
| 109 | ARM_COMPUTE_ERROR("Data layout not supported"); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 114 | Status CpuPool2d::validate(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &pool_info, const ITensorInfo *indices) |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 115 | { |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 116 | const bool run_optimised = bool(kernels::CpuPool2dAssemblyWrapperKernel::validate(src, dst, pool_info)) && (indices == nullptr); |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 117 | |
| 118 | if(run_optimised) |
| 119 | { |
| 120 | return Status{}; |
| 121 | } |
| 122 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 123 | return kernels::CpuPool2dKernel::validate(src, dst, pool_info, indices); |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 126 | void CpuPool2d::run(ITensorPack &tensors) |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 127 | { |
Michele Di Giorgio | 0c19cbd | 2021-05-11 17:41:32 +0100 | [diff] [blame] | 128 | ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No tensors provided"); |
| 129 | |
| 130 | if(_asm_glue) |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 131 | { |
Michele Di Giorgio | 0c19cbd | 2021-05-11 17:41:32 +0100 | [diff] [blame] | 132 | const auto hints = (_is_global_pooling_layer) ? Window::DimX : Window::DimY; |
| 133 | NEScheduler::get().schedule_op(_asm_glue.get(), hints, _asm_glue->window(), tensors); |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 134 | } |
| 135 | else |
| 136 | { |
| 137 | switch(_data_layout) |
| 138 | { |
| 139 | case DataLayout::NCHW: |
| 140 | // Fill border |
| 141 | NEScheduler::get().schedule_op(_border_handler.get(), Window::DimY, _border_handler->window(), tensors); |
| 142 | |
| 143 | // Run pooling layer |
| 144 | NEScheduler::get().schedule_op(_pooling_layer_kernel.get(), _is_global_pooling_layer ? Window::DimZ : Window::DimY, _pooling_layer_kernel->window(), tensors); |
| 145 | break; |
| 146 | case DataLayout::NHWC: |
| 147 | // Run pooling layer |
| 148 | NEScheduler::get().schedule_op(_pooling_layer_kernel.get(), Window::DimX, _pooling_layer_kernel->window(), tensors); |
| 149 | break; |
| 150 | default: |
| 151 | ARM_COMPUTE_ERROR("Data layout not supported"); |
| 152 | } |
| 153 | } |
| 154 | } |
Michele Di Giorgio | 0c19cbd | 2021-05-11 17:41:32 +0100 | [diff] [blame] | 155 | |
Manuel Bottini | b4bb6a0 | 2021-05-24 16:01:32 +0100 | [diff] [blame] | 156 | experimental::MemoryRequirements CpuPool2d::workspace() const |
Michele Di Giorgio | 0c19cbd | 2021-05-11 17:41:32 +0100 | [diff] [blame] | 157 | { |
Manuel Bottini | 94f799e | 2021-06-09 16:37:32 +0100 | [diff] [blame] | 158 | return _aux_mem; |
Michele Di Giorgio | 0c19cbd | 2021-05-11 17:41:32 +0100 | [diff] [blame] | 159 | } |
Michele Di Giorgio | 1928904 | 2021-02-03 16:05:00 +0000 | [diff] [blame] | 160 | } // namespace cpu |
| 161 | } // namespace arm_compute |