1 /*
2 * Copyright (c) 2017-2020 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 "arm_compute/runtime/NEON/functions/NEPoolingLayer.h"
25
26 #include "arm_compute/core/ITensor.h"
27 #include "arm_compute/runtime/NEON/NEScheduler.h"
28 #include "src/core/NEON/kernels/NEFillBorderKernel.h"
29 #include "src/core/NEON/kernels/NEPoolingLayerKernel.h"
30 #include "support/MemorySupport.h"
31
32 namespace arm_compute
33 {
34 NEPoolingLayer::~NEPoolingLayer() = default;
35
NEPoolingLayer()36 NEPoolingLayer::NEPoolingLayer()
37 : _pooling_layer_kernel(), _border_handler(), _is_global_pooling_layer(false), _data_layout(DataLayout::NCHW)
38 {
39 }
40
configure(ITensor * input,ITensor * output,const PoolingLayerInfo & pool_info,ITensor * indices)41 void NEPoolingLayer::configure(ITensor *input, ITensor *output, const PoolingLayerInfo &pool_info, ITensor *indices)
42 {
43 // Check if we have Global Pooling Layer
44 _is_global_pooling_layer = (input->info()->dimension(0) == pool_info.pool_size.width) && (input->info()->dimension(1) == pool_info.pool_size.height);
45
46 // Get data layout
47 _data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? input->info()->data_layout() : pool_info.data_layout;
48
49 // Configure pooling kernel
50 _pooling_layer_kernel = arm_compute::support::cpp14::make_unique<NEPoolingLayerKernel>();
51 _pooling_layer_kernel->configure(input, output, pool_info, indices);
52
53 switch(_data_layout)
54 {
55 case DataLayout::NCHW:
56 {
57 // Configure border depending on operation required (quantize border in case of asymmetric data_type)
58 BorderMode border_mode = (!indices && pool_info.pool_type == PoolingType::MAX) ? BorderMode::REPLICATE : BorderMode::CONSTANT;
59 PixelValue zero_value((indices) ? std::numeric_limits<int>::min() : 0.f);
60 if(is_data_type_quantized_asymmetric(input->info()->data_type()) && !pool_info.exclude_padding)
61 {
62 zero_value = PixelValue(0, input->info()->data_type(), input->info()->quantization_info());
63 }
64 _border_handler = arm_compute::support::cpp14::make_unique<NEFillBorderKernel>();
65 _border_handler->configure(input, _pooling_layer_kernel->border_size(), border_mode, zero_value);
66 break;
67 }
68 case DataLayout::NHWC:
69 break;
70 default:
71 ARM_COMPUTE_ERROR("Data layout not supported");
72 }
73 }
74
validate(const ITensorInfo * input,const ITensorInfo * output,const PoolingLayerInfo & pool_info,const ITensorInfo * indices)75 Status NEPoolingLayer::validate(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info, const ITensorInfo *indices)
76 {
77 return NEPoolingLayerKernel::validate(input, output, pool_info, indices);
78 }
79
run()80 void NEPoolingLayer::run()
81 {
82 switch(_data_layout)
83 {
84 case DataLayout::NCHW:
85 // Fill border
86 NEScheduler::get().schedule(_border_handler.get(), Window::DimY);
87
88 // Run pooling layer
89 NEScheduler::get().schedule(_pooling_layer_kernel.get(), _is_global_pooling_layer ? Window::DimZ : Window::DimY);
90 break;
91 case DataLayout::NHWC:
92 // Run pooling layer
93 NEScheduler::get().schedule(_pooling_layer_kernel.get(), Window::DimX);
94 break;
95 default:
96 ARM_COMPUTE_ERROR("Data layout not supported");
97 }
98 }
99
100 } // namespace arm_compute
101