blob: 3321967d2fae4ba0264103b053cd7c5101115765 [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"
28#include "src/core/CPP/Validate.h"
29#include "src/core/common/Registrars.h"
30#include "src/core/helpers/AutoConfiguration.h"
31#include "src/core/helpers/WindowHelpers.h"
32#include "src/cpu/kernels/pool3d/list.h"
33
34namespace arm_compute
35{
36namespace cpu
37{
38namespace kernels
39{
40namespace
41{
42using namespace misc::shape_calculator;
43
44static const std::vector<CpuPool3dKernel::Pooling3dKernel> available_kernels =
45{
46 {
47 "neon_fp16_ndhwc_poolMxNxD",
48 [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F16 && data.isa.fp16); },
49 REGISTER_FP16_NEON(arm_compute::cpu::neon_fp16_pool3d)
50 },
51
52 {
53 "neon_fp32_ndhwc_poolMxNxD",
54 [](const DataTypeISASelectorData & data) { return (data.dt == DataType::F32); },
55 REGISTER_FP32_NEON(arm_compute::cpu::neon_fp32_pool3d)
56 }
57};
58
59Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const Pooling3dLayerInfo &pool_info)
60{
61 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
62 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->data_layout() != DataLayout::NDHWC, "Only NDHWC layout supported");
63 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src);
64 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
65
66 const auto data_layout = src->data_layout();
67 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
68 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
69 const int idx_depth = get_data_layout_dimension_index(data_layout, DataLayoutDimension::DEPTH);
70
71 const bool is_global_pooling = pool_info.is_global_pooling;
72 const unsigned int pool_size_x = is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
73 const unsigned int pool_size_y = is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
74 const unsigned int pool_size_z = is_global_pooling ? src->dimension(idx_depth) : pool_info.pool_size.depth;
75
76 const unsigned int stride_x = pool_info.stride.x();
77 const unsigned int stride_y = pool_info.stride.y();
78 const unsigned int stride_z = pool_info.stride.z();
79
80 ARM_COMPUTE_RETURN_ERROR_ON((pool_size_x == 0) || (pool_size_y == 0) || (pool_size_z == 0));
81 ARM_COMPUTE_RETURN_ERROR_ON((stride_x == 0) || (stride_y == 0) || (stride_z == 0));
82
83 int output_width = 0;
84 int output_height = 0;
85 int output_depth = 0;
86
87 std::tie(output_width, output_height, output_depth) = scaled_3d_dimensions_signed(src->tensor_shape()[idx_width], src->tensor_shape()[idx_height], src->tensor_shape()[idx_depth],
88 pool_size_x, pool_size_y, pool_size_z, pool_info);
89 ARM_COMPUTE_RETURN_ERROR_ON_MSG((output_width < 1 || output_height < 1 || output_depth < 1), "Calculated output dimension size is invalid");
90
91 if(dst->total_size() != 0)
92 {
93 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
94 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, dst);
95 TensorInfo out_info(TensorInfo(compute_pool3d_shape(src->tensor_shape(), pool_info), 1, dst->data_type(), DataLayout::NDHWC));
96 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &out_info);
97 }
98
99 const auto *uk = CpuPool3dKernel::get_implementation(DataTypeISASelectorData{ src->data_type(), CPUInfo::get().get_isa() });
100 ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
101
102 return Status{};
103}
104} //namespace
105
106void CpuPool3dKernel::configure(const ITensorInfo *src, ITensorInfo *dst, const Pooling3dLayerInfo &pool_info)
107{
108 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
109
110 // Perform validation step
111 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, pool_info));
112
113 // dst auto inizialitation if not yet initialized
114 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(compute_pool3d_shape(src->tensor_shape(), pool_info)));
115
116 // Get data layout
117 const auto data_layout = src->data_layout();
118 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
119 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
120 const int idx_depth = get_data_layout_dimension_index(data_layout, DataLayoutDimension::DEPTH);
121
122 // Update pool size in case of global pooling
123 const bool is_global_pooling = pool_info.is_global_pooling;
124 const Size3D pool_size(
125 is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width,
126 is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height,
127 is_global_pooling ? src->dimension(idx_depth) : pool_info.pool_size.depth);
128
129 const auto *uk = CpuPool3dKernel::get_implementation(DataTypeISASelectorData{ src->data_type(), CPUInfo::get().get_isa() });
130 ARM_COMPUTE_ERROR_ON(uk == nullptr);
131
132 // Set instance variables
133 _pool_info = pool_info;
134 _run_method = uk->ukernel;
135 _name = std::string("CpuPool3dKernel").append("/").append(uk->name);
136
137 // Configure kernel window
138 Window win = calculate_max_window(*dst, Steps());
139 ICpuKernel::configure(win);
140}
141
142Status CpuPool3dKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const Pooling3dLayerInfo &pool_info)
143{
144 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
145
146 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, pool_info));
147
148 return Status{};
149}
150
151void CpuPool3dKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
152{
153 ARM_COMPUTE_UNUSED(info);
154 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
155 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
156 ARM_COMPUTE_ERROR_ON(_run_method == nullptr);
157
158 const ITensor *src = tensors.get_const_tensor(TensorType::ACL_SRC_0);
159 ITensor *dst = tensors.get_tensor(TensorType::ACL_DST_0);
160
161 _run_method(src, dst, _pool_info, window);
162}
163
164const char *CpuPool3dKernel::name() const
165{
166 return _name.c_str();
167}
168
169const std::vector<CpuPool3dKernel::Pooling3dKernel> &CpuPool3dKernel::get_available_kernels()
170{
171 return available_kernels;
172}
173
174} // namespace kernels
175} // namespace cpu
176} // namespace arm_compute