blob: 929ccf7cb678fc9ee7b465df83c331dd8deb1865 [file] [log] [blame]
ramelg0137515692022-02-26 22:06:20 +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/gpu/cl/kernels/ClPool3dKernel.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
27#include "arm_compute/core/TensorInfo.h"
28#include "arm_compute/core/utils/misc/ShapeCalculator.h"
29#include "src/core/CL/CLValidate.h"
30#include "src/core/helpers/AutoConfiguration.h"
31#include "src/core/helpers/WindowHelpers.h"
32#include "support/Cast.h"
33#include "utils/TypePrinter.h"
34
35namespace arm_compute
36{
37namespace opencl
38{
39namespace kernels
40{
41using namespace arm_compute::misc::shape_calculator;
42
43namespace
44{
45Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const Pooling3dLayerInfo &pool_info)
46{
47 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
48 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->data_layout() != DataLayout::NDHWC, "Only NDHWC layout supported");
49
50 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src);
51 ARM_COMPUTE_RETURN_ERROR_ON_MSG((pool_info.stride.x() == 0 || pool_info.stride.y() == 0 || pool_info.stride.z() == 0), "Strides cannot be zero.");
52 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::F16, DataType::F32);
53
54 const auto data_layout = src->data_layout();
55 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
56 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
57 const int idx_depth = get_data_layout_dimension_index(data_layout, DataLayoutDimension::DEPTH);
58 const bool is_global_pooling = pool_info.is_global_pooling;
59 const unsigned int pool_size_x = is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
60 const unsigned int pool_size_y = is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
61 const unsigned int pool_size_z = is_global_pooling ? src->dimension(idx_depth) : pool_info.pool_size.depth;
62 int output_width = 0;
63 int output_height = 0;
64 int output_depth = 0;
65
66 bool round_type_ceil_with_asymm_padding = (pool_info.round_type == DimensionRoundingType::CEIL) && (!is_symmetric(pool_info.padding));
67 ARM_COMPUTE_RETURN_ERROR_ON_MSG(round_type_ceil_with_asymm_padding, "Cannot use dimension round type CEIL when padding is asymmetric.");
68
69 ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_pool_3d_region_entirely_outside_input(pool_info), "Pooling region that is entirely outside input tensor is unsupported");
70 std::tie(output_width, output_height, output_depth) = scaled_3d_dimensions_signed(src->tensor_shape()[idx_width], src->tensor_shape()[idx_height],
71 src->tensor_shape()[idx_depth], pool_size_x, pool_size_y,
72 pool_size_z, pool_info);
73
74 ARM_COMPUTE_RETURN_ERROR_ON_MSG((output_width < 1 || output_height < 1 || output_depth < 1), "Calculated output dimension size is invalid");
75 // Checks performed when dst is configured
76 if(dst->total_size() != 0)
77 {
78 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
79 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(src, dst);
80 TensorInfo out_info(TensorInfo(compute_pool3d_shape(src->tensor_shape(), pool_info), 1, dst->data_type()));
81 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, &out_info);
82 }
83
84 return Status{};
85}
86} // namespace
87
88ClPool3dKernel::ClPool3dKernel()
89{
90 _type = CLKernelType::POOL;
91}
92
93void ClPool3dKernel::configure(const ClCompileContext &compile_context, const ITensorInfo *src, ITensorInfo *dst, const Pooling3dLayerInfo &pool_info)
94{
95 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
96 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, pool_info));
97 auto padding_info = get_padding_info({ src, dst });
98
99 // Auto init if empty
100 TensorShape out_shape = compute_pool3d_shape(src->tensor_shape(), pool_info);
101 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(out_shape));
102
103 // Set instance variables
104 _pool_info = pool_info;
105 _data_layout = src->data_layout();
106
107 _num_elems_processed_per_iteration = (dst->data_type() == DataType::F32) ? 2 : 4;
108 _num_elems_processed_per_iteration = adjust_vec_size(_num_elems_processed_per_iteration, dst->dimension(0));
109
110 const PoolingType pool_type = pool_info.pool_type;
111 const int idx_width = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
112 const int idx_height = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
113 const int idx_depth = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::DEPTH);
114 const int idx_channel = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
115 const int idx_batch_size = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::BATCHES);
116 const int pool_size_x = pool_info.is_global_pooling ? src->dimension(idx_width) : pool_info.pool_size.width;
117 const int pool_size_y = pool_info.is_global_pooling ? src->dimension(idx_height) : pool_info.pool_size.height;
118 const int pool_size_z = pool_info.is_global_pooling ? src->dimension(idx_depth) : pool_info.pool_size.depth;
119 const bool exclude_padding = pool_info.exclude_padding;
120 const int pool_stride_x = pool_info.stride.x();
121 const int pool_stride_y = pool_info.stride.y();
122 const int pool_stride_z = pool_info.stride.z();
123 const int pool_pad_top = pool_info.padding.top;
124 const int pool_pad_left = pool_info.padding.left;
125 const int pool_pad_front = pool_info.padding.front;
126 const DataType data_type = src->data_type();
127
128 // Set build options
129 CLBuildOptions build_opts;
130 build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(_num_elems_processed_per_iteration));
131 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
132 build_opts.add_option("-DPOOL_" + string_from_pooling_type(pool_type));
133 build_opts.add_option("-DSTRIDE_X=" + support::cpp11::to_string(pool_stride_x));
134 build_opts.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(pool_stride_y));
135 build_opts.add_option("-DSTRIDE_Z=" + support::cpp11::to_string(pool_stride_z));
136 build_opts.add_option("-DPAD_X=" + support::cpp11::to_string(pool_pad_left));
137 build_opts.add_option("-DPAD_Y=" + support::cpp11::to_string(pool_pad_top));
138 build_opts.add_option("-DPAD_Z=" + support::cpp11::to_string(pool_pad_front));
139 build_opts.add_option("-DPOOL_SIZE_X=" + support::cpp11::to_string(pool_size_x));
140 build_opts.add_option("-DPOOL_SIZE_Y=" + support::cpp11::to_string(pool_size_y));
141 build_opts.add_option("-DPOOL_SIZE_Z=" + support::cpp11::to_string(pool_size_z));
142 build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src->dimension(idx_width)));
143 build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src->dimension(idx_height)));
144 build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(src->dimension(idx_depth)));
145
146 // Set the initial value for the pooling operation accordingly with the data type
147 if(pool_type == PoolingType::MAX)
148 {
149 build_opts.add_option("-DINITIAL_VALUE=" + float_to_string_with_full_precision(std::numeric_limits<float>::lowest()));
150 }
151 else
152 {
153 // Pool AVG and Pool L2 initial value
154 build_opts.add_option("-DINITIAL_VALUE=0");
155 }
156 // Create kernel
157 // Floating point mixed precision is support on F16 only
158 const auto use_fp_mixed_precision = (data_type == DataType::F16) && pool_info.fp_mixed_precision && pool_type != PoolingType::MAX;
159
160 // Wider accumulation is required to avoid accuracy loss
161 // Case 1: Floating point mixed precision (fp16 src data and fp32 accumulation)
162 DataType acc_data_type = data_type;
163 if(use_fp_mixed_precision)
164 {
165 acc_data_type = DataType::F32;
166 }
167 build_opts.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(acc_data_type));
168 build_opts.add_option_if(use_fp_mixed_precision, "-DFP_MIXED_PRECISION");
169 build_opts.add_option_if(exclude_padding, "-DEXCLUDE_PADDING");
170 build_opts.add_option("-DDST_HEIGHT=" + support::cpp11::to_string(dst->dimension(idx_height)));
171 build_opts.add_option("-DDST_DEPTH=" + support::cpp11::to_string(dst->dimension(idx_depth)));
172 build_opts.add_option("-DDST_CHANNELS=" + support::cpp11::to_string(dst->dimension(idx_channel)));
173 build_opts.add_option("-DDST_BATCH_SIZE=" + support::cpp11::to_string(dst->dimension(idx_batch_size)));
174 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(src->dimension(0) % _num_elems_processed_per_iteration));
175 std::string kernel_name = "pooling_3d_layer_MxN_ndhwc";
176 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
177
178 // Configure kernel window
179 Window win = calculate_max_window(*dst, Steps(_num_elems_processed_per_iteration));
180 ICLKernel::configure_internal(win);
181
182 // Set config_id for enabling LWS tuning
183 _config_id = "pooling_layer_3d";
184 _config_id += lower_string(string_from_data_type(data_type));
185 _config_id += "_";
186 _config_id += lower_string(string_from_data_layout(_data_layout));
187 _config_id += "_";
188 _config_id += support::cpp11::to_string(dst->dimension(idx_width));
189 _config_id += "_";
190 _config_id += support::cpp11::to_string(dst->dimension(idx_height));
191 _config_id += "_";
192 _config_id += support::cpp11::to_string(dst->dimension(idx_channel));
193 _config_id += "_";
194 _config_id += lower_string(string_from_data_layout(src->data_layout()));
195
196 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
197}
198
199Status ClPool3dKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const Pooling3dLayerInfo &pool_info)
200{
201 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, pool_info));
202 return Status{};
203}
204
205void ClPool3dKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
206{
207 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
208 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
209
210 const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
211 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST_0));
212
213 // Collapse 3D window
214 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
215
216 // Set CL kernel arguments
217 unsigned int idx = 0;
218 // Passing of the window not needed, as the steps are not used for the pool3d kernel
219 add_5D_tensor_argument(idx, src, window);
220 add_5D_tensor_argument(idx, dst, window);
221 enqueue(queue, *this, window_collapsed, lws_hint());
222}
223} // namespace kernels
224} // namespace opencl
225} // namespace arm_compute