1 /*
2 * Copyright (c) 2020-2022 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 "src/cpu/kernels/CpuMaxUnpoolingLayerKernel.h"
25
26 #include "arm_compute/core/TensorInfo.h"
27 #include "arm_compute/core/Validate.h"
28 #include "arm_compute/core/Window.h"
29 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
30 #include "src/core/CPP/Validate.h"
31 #include "src/core/common/Registrars.h"
32 #include "src/core/helpers/AutoConfiguration.h"
33 #include "src/core/helpers/WindowHelpers.h"
34 #include "src/cpu/kernels/maxunpool/list.h"
35 #include "support/ToolchainSupport.h"
36
37 namespace arm_compute
38 {
39 namespace cpu
40 {
41 namespace kernels
42 {
43 using namespace misc::shape_calculator;
44
45 namespace
46 {
47 static const std::vector<CpuMaxUnpoolingLayerKernel::MaxUnpoolingKernel> available_kernels =
48 {
49 {
50 "neon_fp32_maxunpooling",
__anon375319200202() 51 [](const DataTypeISASelectorData & data) { return data.dt == DataType::F32; },
52 REGISTER_FP32_NEON(neon_fp32_maxunpooling)
53 },
54 {
55 "neon_fp16_maxunpooling",
__anon375319200302() 56 [](const DataTypeISASelectorData & data) { return data.dt == DataType::F16 && data.isa.fp16; },
57 REGISTER_FP16_NEON(neon_fp16_maxunpooling)
58 },
59 {
60 "neon_qu8_maxunpooling",
__anon375319200402() 61 [](const DataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8; },
62 REGISTER_QASYMM8_NEON(neon_qs8_maxunpooling)
63 },
64 {
65 "neon_qs8_maxunpooling",
__anon375319200502() 66 [](const DataTypeISASelectorData & data) { return data.dt == DataType::QASYMM8_SIGNED; },
67 REGISTER_QASYMM8_SIGNED_NEON(neon_qu8_maxunpooling)
68 },
69 };
70
validate_arguments(const ITensorInfo * src,const ITensorInfo * indices,const ITensorInfo * dst,const PoolingLayerInfo & pool_info)71 Status validate_arguments(const ITensorInfo *src, const ITensorInfo *indices, const ITensorInfo *dst, const PoolingLayerInfo &pool_info)
72 {
73 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, indices, dst);
74 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src);
75 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
76 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32);
77 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, indices);
78
79 int pool_stride_x = 0;
80 int pool_stride_y = 0;
81 PoolingType pool_type = pool_info.pool_type;
82 const PadStrideInfo pad_stride_info = pool_info.pad_stride_info;
83 std::tie(pool_stride_x, pool_stride_y) = pad_stride_info.stride();
84 const int pool_size_x = pool_info.pool_size.width;
85 const int pool_size_y = pool_info.pool_size.height;
86 const Size2D pool_size(pool_size_x, pool_size_y);
87
88 ARM_COMPUTE_RETURN_ERROR_ON_MSG(pool_type != PoolingType::MAX, "Pooling indices only supported for MAX pooling method");
89 ARM_COMPUTE_RETURN_ERROR_ON_MSG((pool_size != Size2D(2, 2)), "Pooling indices only supported for pool size 2x2");
90 if(dst->total_size() != 0)
91 {
92 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
93 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, dst);
94 }
95
96 return Status{};
97 }
98 } // namespace
99
configure(const ITensorInfo * src,const ITensorInfo * indices,ITensorInfo * dst,const PoolingLayerInfo & pool_info)100 void CpuMaxUnpoolingLayerKernel::configure(const ITensorInfo *src, const ITensorInfo *indices, ITensorInfo *dst, const PoolingLayerInfo &pool_info)
101 {
102 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst, indices);
103 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, indices, dst, pool_info));
104 ARM_COMPUTE_UNUSED(indices);
105
106 const auto uk = CpuMaxUnpoolingLayerKernel::get_implementation(DataTypeISASelectorData{ src->data_type(), CPUInfo::get().get_isa() });
107 ARM_COMPUTE_ERROR_ON_NULLPTR(uk);
108 _run_method = uk->ukernel;
109
110 const TensorShape output_shape = compute_unpool_shape(*src, pool_info);
111 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(output_shape));
112
113 auto window = calculate_max_window(*src, Steps());
114 ICpuKernel::configure(window);
115 }
116
validate(const ITensorInfo * src,const ITensorInfo * indices,const ITensorInfo * dst,const PoolingLayerInfo & pool_info)117 Status CpuMaxUnpoolingLayerKernel::validate(const ITensorInfo *src, const ITensorInfo *indices, const ITensorInfo *dst, const PoolingLayerInfo &pool_info)
118 {
119 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, indices, dst);
120 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, indices, dst, pool_info));
121 return Status{};
122 }
123
run_op(ITensorPack & tensors,const Window & window,const ThreadInfo & info)124 void CpuMaxUnpoolingLayerKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
125 {
126 ARM_COMPUTE_UNUSED(info);
127 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
128 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
129
130 const auto src = tensors.get_const_tensor(TensorType::ACL_SRC_0);
131 const auto indices = tensors.get_const_tensor(TensorType::ACL_SRC_1);
132 const auto dst = tensors.get_tensor(TensorType::ACL_DST);
133
134 _run_method(src, indices, dst, window);
135 }
136
name() const137 const char *CpuMaxUnpoolingLayerKernel::name() const
138 {
139 return "CpuMaxUnpoolingLayerKernel";
140 }
141
get_available_kernels()142 const std::vector<CpuMaxUnpoolingLayerKernel::MaxUnpoolingKernel> &CpuMaxUnpoolingLayerKernel::get_available_kernels()
143 {
144 return available_kernels;
145 }
146 } // namespace kernels
147 } // namespace cpu
148 } // namespace arm_compute