Compute Library
 21.02
CpuPoolingAssemblyDispatch.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 
28 #include "src/core/CPP/Validate.h"
30 
31 namespace arm_compute
32 {
33 namespace cpu
34 {
35 CpuPoolingAssemblyDispatch::CpuPoolingAssemblyDispatch(std::shared_ptr<IMemoryManager> memory_manager)
36  : _memory_group(std::move(memory_manager)),
37  _workspace(),
38  _is_global_pooling_layer(false)
39 {
40 }
41 
43 
45 {
46  const CPUInfo &ci = NEScheduler::get().cpu_info();
47  const unsigned int num_threads = NEScheduler::get().num_threads();
48 
49  // If we don't support a combination of data types, silently return: it is the caller's responsibility to check if configure() was successful via is_configured()
50  if(!CpuPoolingAssemblyDispatch::validate(src, dst, info))
51  {
52  return;
53  }
54 
55  auto pooling_wrapper = std::make_unique<kernels::CpuPoolingAssemblyWrapperKernel>();
56  ARM_COMPUTE_ERROR_ON(pooling_wrapper == nullptr);
57  pooling_wrapper->configure(src, dst, info, ci);
58 
59  // Check if we have Global Pooling Layer
60  _is_global_pooling_layer = (src->dimension(2) == info.pool_size.width) && (src->dimension(1) == info.pool_size.height);
61 
62  // Allocate workspace based on kernel's memory requirements
63  constexpr size_t alignment = 4096;
64  const size_t workspace_size = pooling_wrapper->get_working_size(num_threads);
65  _workspace.allocator()->init(TensorInfo(TensorShape{ (workspace_size + alignment /* FIXME: remove alignment after COMPMID-1088 */) }, 1, DataType::S8), alignment);
66  _memory_group.manage(&_workspace);
67  _workspace.allocator()->allocate();
68 
69  _kernel = std::move(pooling_wrapper);
70 }
71 
73 {
75 }
76 
78 {
79  return _kernel != nullptr;
80 }
81 
83 {
84  ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No srcs provided");
85 
86  tensors.add_tensor(TensorType::ACL_DST_1, &_workspace);
87 
88  if(_is_global_pooling_layer)
89  {
90  NEScheduler::get().schedule_op(_kernel.get(), Window::DimX, _kernel->window(), tensors);
91  }
92  else
93  {
94  NEScheduler::get().schedule_op(_kernel.get(), Window::DimY, _kernel->window(), tensors);
95  }
96 }
97 } // namespace cpu
98 } // namespace arm_compute
void run(ITensorPack &tensors) override
Run the kernels contained in the function.
Shape of a tensor.
Definition: TensorShape.h:39
void init(const TensorAllocator &allocator, const Coordinates &coords, TensorInfo &sub_info)
Shares the same backing memory with another tensor allocator, while the tensor info might be differen...
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:61
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.
#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:42
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:90
TensorAllocator * allocator()
Return a pointer to the tensor&#39;s allocator.
Definition: Tensor.cpp:48
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.
void manage(IMemoryManageable *obj) override
Sets a object to be managed by the given memory group.
Definition: MemoryGroup.h:79
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
void allocate() override
Allocate size specified by TensorInfo of CPU memory.
Pooling Layer Information struct.
Definition: Types.h:1214
bool is_configured() const
Was the function successfully configured ?
CpuPoolingAssemblyDispatch(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Constructor.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
void configure(const ITensorInfo *src, ITensorInfo *dst, const PoolingLayerInfo &info)
If supported create an assembly routine, else fallback to Compute Library function.
size_t width
Width of the image region or rectangle.
Definition: Size2D.h:89
Tensor packing service.
Definition: ITensorPack.h:37
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:45
virtual unsigned int num_threads() const =0
Returns the number of threads that the SingleThreadScheduler has in his pool.
signed 8-bit number
void add_tensor(int id, ITensor *tensor)
Add tensor to the pack.
Definition: ITensorPack.cpp:30
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:94