blob: 8b484d4e0b81013b15642c64d9ef2bb4a97dd57f [file] [log] [blame]
Adnan AlSinan171fc3d2022-03-15 18:46:42 +00001/*
2 * Copyright (c) 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/CpuPool3dKernel.h"
25
26#include "arm_compute/core/TensorInfo.h"
27#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028
Adnan AlSinan171fc3d2022-03-15 18:46:42 +000029#include "src/core/common/Registrars.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030#include "src/core/CPP/Validate.h"
Adnan AlSinan171fc3d2022-03-15 18:46:42 +000031#include "src/core/helpers/AutoConfiguration.h"
32#include "src/core/helpers/WindowHelpers.h"
33#include "src/cpu/kernels/pool3d/list.h"
34
35namespace arm_compute
36{
37namespace cpu
38{
39namespace kernels
40{
41namespace
42{
43using namespace misc::shape_calculator;
44
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010045static const std::vector<CpuPool3dKernel::Pooling3dKernel> available_kernels = {
46 {"neon_qu8_ndhwc_poolMxNxD", [](const DataTypeISASelectorData &data) { return (data.dt == DataType::QASYMM8); },
47 REGISTER_QASYMM8_NEON(arm_compute::cpu::neon_q8_pool3d)},
48 {"neon_qs8_ndhwc_poolMxNxD",
49 [](const DataTypeISASelectorData &data) { return (data.dt == DataType::QASYMM8_SIGNED); },
50 REGISTER_QASYMM8_SIGNED_NEON(arm_compute::cpu::neon_q8_signed_pool3d)},
51 {"neon_fp16_ndhwc_poolMxNxD",
52 [](const DataTypeISASelectorData &data) { return (data.dt == DataType::F16 && data.isa.fp16); },
53 REGISTER_FP16_NEON(arm_compute::cpu::neon_fp16_pool3d)},
54 {"neon_fp32_ndhwc_poolMxNxD", [](const DataTypeISASelectorData &data) { return (data.dt == DataType::F32); },
55 REGISTER_FP32_NEON(arm_compute::cpu::neon_fp32_pool3d)}};
Adnan AlSinan171fc3d2022-03-15 18:46:42 +000056
57Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const Pooling3dLayerInfo &pool_info)
58{
59 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
60 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->data_layout() != DataLayout::NDHWC, "Only NDHWC layout supported");
61 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010062 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32, DataType::QASYMM8,
63 DataType::QASYMM8_SIGNED);
Adnan AlSinan9104cd52022-04-06 16:19:31 +010064
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010065 ARM_COMPUTE_RETURN_ERROR_ON_MSG((!is_data_type_float(src->data_type())) &&
66 (!pool_info.exclude_padding && (pool_info.pool_type == PoolingType::AVG)),
Adnan AlSinan9104cd52022-04-06 16:19:31 +010067 "Exclude padding is unsupported for non-float types for Avg op");
Adnan AlSinan171fc3d2022-03-15 18:46:42 +000068
69 const auto data_layout = src->data_layout();
70 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
71 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
72 const int idx_depth = get_data_layout_dimension_index(data_layout, DataLayoutDimension::DEPTH);
73
74 const bool is_global_pooling = pool_info.is_global_pooling;
75 const unsigned int pool_size_x = is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
76 const unsigned int pool_size_y = is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
77 const unsigned int pool_size_z = is_global_pooling ? src->dimension(idx_depth) : pool_info.pool_size.depth;
78
79 const unsigned int stride_x = pool_info.stride.x();
80 const unsigned int stride_y = pool_info.stride.y();
81 const unsigned int stride_z = pool_info.stride.z();
82
83 ARM_COMPUTE_RETURN_ERROR_ON((pool_size_x == 0) || (pool_size_y == 0) || (pool_size_z == 0));
84 ARM_COMPUTE_RETURN_ERROR_ON((stride_x == 0) || (stride_y == 0) || (stride_z == 0));
85
86 int output_width = 0;
87 int output_height = 0;
88 int output_depth = 0;
89
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010090 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_pool_3d_region_entirely_outside_input(pool_info),
91 "Pooling region that is entirely outside input tensor is unsupported");
Adnan AlSinanfacd9dd2022-05-04 11:31:45 +010092
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010093 std::tie(output_width, output_height, output_depth) =
94 scaled_3d_dimensions_signed(src->tensor_shape()[idx_width], src->tensor_shape()[idx_height],
95 src->tensor_shape()[idx_depth], pool_size_x, pool_size_y, pool_size_z, pool_info);
96 ARM_COMPUTE_RETURN_ERROR_ON_MSG((output_width < 1 || output_height < 1 || output_depth < 1),
97 "Calculated output dimension size is invalid");
Adnan AlSinan171fc3d2022-03-15 18:46:42 +000098
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010099 if (dst->total_size() != 0)
Adnan AlSinan171fc3d2022-03-15 18:46:42 +0000100 {
101 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
102 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, dst);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103 TensorInfo out_info(
104 TensorInfo(compute_pool3d_shape(src->tensor_shape(), pool_info), 1, dst->data_type(), DataLayout::NDHWC));
Adnan AlSinan171fc3d2022-03-15 18:46:42 +0000105 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &out_info);
106 }
107
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100108 const auto *uk =
109 CpuPool3dKernel::get_implementation(DataTypeISASelectorData{src->data_type(), CPUInfo::get().get_isa()});
Adnan AlSinan171fc3d2022-03-15 18:46:42 +0000110 ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
111
112 return Status{};
113}
114} //namespace
115
116void CpuPool3dKernel::configure(const ITensorInfo *src, ITensorInfo *dst, const Pooling3dLayerInfo &pool_info)
117{
118 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
119
120 // Perform validation step
121 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, pool_info));
122
123 // dst auto inizialitation if not yet initialized
124 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(compute_pool3d_shape(src->tensor_shape(), pool_info)));
125
126 // Get data layout
127 const auto data_layout = src->data_layout();
128 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
129 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
130 const int idx_depth = get_data_layout_dimension_index(data_layout, DataLayoutDimension::DEPTH);
131
132 // Update pool size in case of global pooling
133 const bool is_global_pooling = pool_info.is_global_pooling;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100134 const Size3D pool_size(is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width,
135 is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height,
136 is_global_pooling ? src->dimension(idx_depth) : pool_info.pool_size.depth);
Adnan AlSinan171fc3d2022-03-15 18:46:42 +0000137
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100138 const auto *uk =
139 CpuPool3dKernel::get_implementation(DataTypeISASelectorData{src->data_type(), CPUInfo::get().get_isa()});
Adnan AlSinan171fc3d2022-03-15 18:46:42 +0000140 ARM_COMPUTE_ERROR_ON(uk == nullptr);
141
142 // Set instance variables
143 _pool_info = pool_info;
144 _run_method = uk->ukernel;
145 _name = std::string("CpuPool3dKernel").append("/").append(uk->name);
146
147 // Configure kernel window
148 Window win = calculate_max_window(*dst, Steps());
149 ICpuKernel::configure(win);
150}
151
152Status CpuPool3dKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const Pooling3dLayerInfo &pool_info)
153{
154 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
155
156 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, pool_info));
157
158 return Status{};
159}
160
161void CpuPool3dKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
162{
163 ARM_COMPUTE_UNUSED(info);
164 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
165 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
166 ARM_COMPUTE_ERROR_ON(_run_method == nullptr);
167
168 const ITensor *src = tensors.get_const_tensor(TensorType::ACL_SRC_0);
169 ITensor *dst = tensors.get_tensor(TensorType::ACL_DST_0);
170
171 _run_method(src, dst, _pool_info, window);
172}
173
174const char *CpuPool3dKernel::name() const
175{
176 return _name.c_str();
177}
178
179const std::vector<CpuPool3dKernel::Pooling3dKernel> &CpuPool3dKernel::get_available_kernels()
180{
181 return available_kernels;
182}
183
184} // namespace kernels
185} // namespace cpu
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100186} // namespace arm_compute