Compute Library
 21.02
CpuPooling.cpp
Go to the documentation of this file.
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  */
25 
31 
32 namespace arm_compute
33 {
34 namespace cpu
35 {
36 CpuPooling::CpuPooling(std::shared_ptr<IMemoryManager> memory_manager)
37  : _memory_manager(std::move(memory_manager)), _pooling_layer_kernel(), _border_handler(), _asm_glue(), _is_global_pooling_layer(false), _data_layout(DataLayout::NCHW)
38 {
39 }
40 
41 CpuPooling::~CpuPooling() = default;
42 
44 {
45  // Check if we can run assembly kernels. Currently, indices are not supported by those kernels
46  const bool run_optimised = bool(CpuPoolingAssemblyDispatch::validate(input, output, pool_info)) && (indices == nullptr);
47 
48  if(run_optimised)
49  {
50  _asm_glue = std::make_unique<CpuPoolingAssemblyDispatch>(_memory_manager);
51  _asm_glue->configure(input, output, pool_info);
52  ARM_COMPUTE_ERROR_ON(!_asm_glue->is_configured());
53  }
54  else
55  {
56  // Check if we have Global Pooling Layer
57  _is_global_pooling_layer = (input->dimension(0) == pool_info.pool_size.width) && (input->dimension(1) == pool_info.pool_size.height);
58 
59  // Get data layout
60  _data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? input->data_layout() : pool_info.data_layout;
61 
62  // Configure pooling kernel
63  auto k = std::make_unique<kernels::CpuPoolingKernel>();
64  k->configure(input, output, pool_info, indices);
65  _pooling_layer_kernel = std::move(k);
66 
67  switch(_data_layout)
68  {
69  case DataLayout::NCHW:
70  {
71  // Configure border depending on operation required (quantize border in case of asymmetric data_type)
72  BorderMode border_mode = (!indices && pool_info.pool_type == PoolingType::MAX) ? BorderMode::REPLICATE : BorderMode::CONSTANT;
73  PixelValue zero_value((indices) ? std::numeric_limits<int>::min() : 0.f);
75  {
76  zero_value = PixelValue(0, input->data_type(), input->quantization_info());
77  }
78  auto b = std::make_unique<NEFillBorderKernel>();
79  b->configure(input, _pooling_layer_kernel->border_size(), border_mode, zero_value);
80  _border_handler = std::move(b);
81  break;
82  }
83  case DataLayout::NHWC:
84  break;
85  default:
86  ARM_COMPUTE_ERROR("Data layout not supported");
87  }
88  }
89 }
90 
91 Status CpuPooling::validate(const ITensorInfo *input, const ITensorInfo *output, const PoolingLayerInfo &pool_info, const ITensorInfo *indices)
92 {
93  const bool run_optimised = bool(CpuPoolingAssemblyDispatch::validate(input, output, pool_info)) && (indices == nullptr);
94 
95  if(run_optimised)
96  {
97  return Status{};
98  }
99 
100  return kernels::CpuPoolingKernel::validate(input, output, pool_info, indices);
101 }
102 
104 {
105  if(_asm_glue && _asm_glue->is_configured())
106  {
107  _asm_glue->run(tensors);
108  }
109  else
110  {
111  switch(_data_layout)
112  {
113  case DataLayout::NCHW:
114  // Fill border
115  NEScheduler::get().schedule_op(_border_handler.get(), Window::DimY, _border_handler->window(), tensors);
116 
117  // Run pooling layer
118  NEScheduler::get().schedule_op(_pooling_layer_kernel.get(), _is_global_pooling_layer ? Window::DimZ : Window::DimY, _pooling_layer_kernel->window(), tensors);
119  break;
120  case DataLayout::NHWC:
121  // Run pooling layer
122  NEScheduler::get().schedule_op(_pooling_layer_kernel.get(), Window::DimX, _pooling_layer_kernel->window(), tensors);
123  break;
124  default:
125  ARM_COMPUTE_ERROR("Data layout not supported");
126  }
127  }
128 }
129 } // namespace cpu
130 } // namespace arm_compute
BorderMode
Methods available to handle borders.
Definition: Types.h:265
Class describing the value of a pixel for any image format.
Definition: PixelValue.h:34
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
SimpleTensor< float > b
Definition: DFT.cpp:157
static Status validate(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &pool_info, const ITensorInfo *indices=nullptr)
Static function to check if given info will lead to a valid configuration of CpuPooling.
Definition: CpuPooling.cpp:91
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
virtual DataType data_type() const =0
Data type used for each element of the tensor.
virtual void schedule_op(ICPPKernel *kernel, const Hints &hints, const Window &window, ITensorPack &tensors)=0
Runs the kernel in the same thread as the caller synchronously.
static Status validate(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &info)
Indicates whether or not this function can be used to process the given parameters.
static Status validate(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &pool_info, const ITensorInfo *indices=nullptr)
Static function to check if given info will lead to a valid configuration of CpuPoolingKernel.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
Status class.
Definition: Error.h:52
Copyright (c) 2017-2021 Arm Limited.
size_t height
Height of the image region or rectangle.
Definition: Size2D.h:90
~CpuPooling()
Default destructor.
void configure(ITensorInfo *src, ITensorInfo *dst, const PoolingLayerInfo &pool_info, ITensorInfo *indices=nullptr)
Set the src and dst tensors.
Definition: CpuPooling.cpp:43
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
Pooling Layer Information struct.
Definition: Types.h:1214
CpuPooling(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Constructor.
Definition: CpuPooling.cpp:36
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
Num samples, channels, height, width.
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: Utils.h:1190
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
size_t width
Width of the image region or rectangle.
Definition: Size2D.h:89
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
Pixels outside the image are assumed to have the same value as the closest image pixel.
Num samples, height, width, channels.
Tensor packing service.
Definition: ITensorPack.h:37
DataLayout
[DataLayout enum definition]
Definition: Types.h:120
void run(ITensorPack &tensors) override
Run the kernels contained in the function.
Definition: CpuPooling.cpp:103
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:94