Compute Library
 22.02
CpuPool2d.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 
29 #include "src/common/utils/Log.h"
33 
34 using namespace arm_compute::experimental;
35 
36 namespace arm_compute
37 {
38 namespace cpu
39 {
40 CpuPool2d::CpuPool2d()
41  : _pooling_layer_kernel(),
42  _asm_glue(),
43  _is_global_pooling_layer(false),
44  _data_layout(DataLayout::NCHW),
45  _aux_mem(1)
46 {
47 }
48 
49 CpuPool2d::~CpuPool2d() = default;
50 
52 {
53  ARM_COMPUTE_LOG_PARAMS(src, dst, pool_info, indices);
54 
55  // Check if we can run assembly kernels. Currently, indices are not supported by those kernels
56  const bool run_optimised = bool(kernels::CpuPool2dAssemblyWrapperKernel::validate(src, dst, pool_info)) && (indices == nullptr);
57 
58  // Get data layout
59  _data_layout = pool_info.data_layout == DataLayout::UNKNOWN ? src->data_layout() : pool_info.data_layout;
60 
61  // Check if we have Global Pooling Layer
62  const unsigned int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
64  _is_global_pooling_layer = (src->dimension(idx_width) == pool_info.pool_size.width) && (src->dimension(idx_height) == pool_info.pool_size.height);
65 
66  if(run_optimised)
67  {
68  const CPUInfo &ci = NEScheduler::get().cpu_info();
69  const unsigned int num_threads = NEScheduler::get().num_threads();
70 
71  auto pooling_wrapper = std::make_unique<kernels::CpuPool2dAssemblyWrapperKernel>();
72  ARM_COMPUTE_ERROR_ON(pooling_wrapper == nullptr);
73  pooling_wrapper->configure(src, dst, pool_info, ci);
74 
75  // Get kernel's memory requirements
76  constexpr size_t alignment = 4096;
77  const size_t workspace_size = pooling_wrapper->get_working_size(num_threads);
78  _aux_mem[0] = MemoryInfo(TensorType::ACL_INT_0, MemoryLifetime::Temporary, workspace_size, alignment);
79 
80  _asm_glue = std::move(pooling_wrapper);
81  }
82  else
83  {
84  // Configure pooling kernel
85  auto k = std::make_unique<kernels::CpuPool2dKernel>();
86  k->configure(src, dst, pool_info, indices);
87  _pooling_layer_kernel = std::move(k);
88  }
89 }
90 
91 Status CpuPool2d::validate(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &pool_info, const ITensorInfo *indices)
92 {
93  const bool run_optimised = bool(kernels::CpuPool2dAssemblyWrapperKernel::validate(src, dst, pool_info)) && (indices == nullptr);
94 
95  if(run_optimised)
96  {
97  return Status{};
98  }
99 
100  return kernels::CpuPool2dKernel::validate(src, dst, pool_info, indices);
101 }
102 
104 {
105  ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No tensors provided");
106 
107  if(_asm_glue)
108  {
109  const auto hints = (_is_global_pooling_layer) ? Window::DimX : Window::DimY;
110  NEScheduler::get().schedule_op(_asm_glue.get(), hints, _asm_glue->window(), tensors);
111  }
112  else
113  {
114  switch(_data_layout)
115  {
116  case DataLayout::NCHW:
117  NEScheduler::get().schedule_op(_pooling_layer_kernel.get(), _is_global_pooling_layer ? Window::DimZ : Window::DimY, _pooling_layer_kernel->window(), tensors);
118  break;
119  case DataLayout::NHWC:
120  NEScheduler::get().schedule_op(_pooling_layer_kernel.get(), Window::DimX, _pooling_layer_kernel->window(), tensors);
121  break;
122  default:
123  ARM_COMPUTE_ERROR("Data layout not supported");
124  }
125  }
126 }
127 
129 {
130  return _aux_mem;
131 }
132 } // namespace cpu
133 } // namespace arm_compute
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.
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
bool empty() const
Checks if pack is empty.
Definition: ITensorPack.cpp:80
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
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.
#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
CPUInfo & cpu_info()
Get CPU info.
Definition: IScheduler.cpp:41
Status class.
Definition: Error.h:52
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
size_t height
Height of the image region or rectangle.
Definition: Size2D.h:91
const CPUInfo & ci
std::vector< MemoryInfo > MemoryRequirements
Definition: Types.h:132
experimental::MemoryRequirements workspace() const override
Return the memory requirements required by the workspace.
Definition: CpuPool2d.cpp:128
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
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.
Definition: CpuPool2d.cpp:91
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
void run(ITensorPack &tensors) override
Run the kernels contained in the function.
Definition: CpuPool2d.cpp:103
Pooling Layer Information struct.
Definition: Types.h:1173
Num samples, channels, height, width.
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:90
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
size_t get_data_layout_dimension_index(const DataLayout &data_layout, const DataLayoutDimension &data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:193
Num samples, height, width, channels.
Tensor packing service.
Definition: ITensorPack.h:39
#define ARM_COMPUTE_LOG_PARAMS(...)
static Status validate(const ITensorInfo *src, const ITensorInfo *dst, const PoolingLayerInfo &info)
Static function to check if given info will lead to a valid configuration.
virtual unsigned int num_threads() const =0
Returns the number of threads that the SingleThreadScheduler has in its pool.
DataLayout
[DataLayout enum definition]
Definition: Types.h:113
void configure(ITensorInfo *src, ITensorInfo *dst, const PoolingLayerInfo &pool_info, ITensorInfo *indices=nullptr)
Set the src and dst tensors.
Definition: CpuPool2d.cpp:51
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:94