blob: 8002520a879fb1c277a0c6a4116c0a250a10da49 [file] [log] [blame]
Giorgio Arena945ae9e2021-10-13 11:13:04 +01001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2021-2023 Arm Limited.
Giorgio Arena945ae9e2021-10-13 11:13:04 +01003 *
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/ClDirectConv3dKernel.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000027#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
Giorgio Arena945ae9e2021-10-13 11:13:04 +010028#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Giorgio Arena51847d52021-10-19 15:45:57 +010029#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000030#include "arm_compute/core/utils/StringUtils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031
Giorgio Arena945ae9e2021-10-13 11:13:04 +010032#include "src/core/CL/CLValidate.h"
33#include "src/core/helpers/WindowHelpers.h"
34#include "support/Cast.h"
35
36namespace arm_compute
37{
38namespace opencl
39{
40namespace kernels
41{
42namespace
43{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010044Status validate_arguments(const ITensorInfo *src0,
45 const ITensorInfo *src1,
46 const ITensorInfo *src2,
47 const ITensorInfo *dst,
48 const Conv3dInfo &conv3d_info)
Giorgio Arena945ae9e2021-10-13 11:13:04 +010049{
Sheri Zhang5dda2172021-10-15 19:54:17 +010050 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_LAYOUT(src0, src1, dst);
51 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src0->data_layout() != DataLayout::NDHWC, "Only NDHWC layout supported");
Gunes Bayirfdb53422022-05-25 10:09:39 +010052
53 // When fusing activation, same workaround introduced for COMPMID-5324 may be necessary
Giorgio Arena945ae9e2021-10-13 11:13:04 +010054 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv3d_info.act_info.enabled(), "Fused activation not supported");
55
Sheri Zhang5dda2172021-10-15 19:54:17 +010056 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src0);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src0, 1, DataType::F16, DataType::F32, DataType::QASYMM8,
58 DataType::QASYMM8_SIGNED);
Sheri Zhang5dda2172021-10-15 19:54:17 +010059 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src0, src1);
Freddie Liardet69df64f2021-10-26 14:06:47 +010060 ARM_COMPUTE_RETURN_ERROR_ON(conv3d_info.dilation != Size3D(1U, 1U, 1U));
Giorgio Arena945ae9e2021-10-13 11:13:04 +010061
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010062 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src1->dimension(1) != src0->dimension(0),
63 "Weights feature map dimension should match the respective src's one");
Sheri Zhang5dda2172021-10-15 19:54:17 +010064 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src1->num_dimensions() > 5, "Weights can be at most 5 dimensional");
Giorgio Arena945ae9e2021-10-13 11:13:04 +010065
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010066 ARM_COMPUTE_RETURN_ERROR_ON(src1->dimension(2) >
67 (src0->dimension(1) + conv3d_info.padding.left + conv3d_info.padding.right));
68 ARM_COMPUTE_RETURN_ERROR_ON(src1->dimension(3) >
69 (src0->dimension(2) + conv3d_info.padding.top + conv3d_info.padding.bottom));
70 ARM_COMPUTE_RETURN_ERROR_ON(src1->dimension(4) >
71 (src0->dimension(3) + conv3d_info.padding.front + conv3d_info.padding.back));
Giorgio Arena945ae9e2021-10-13 11:13:04 +010072
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010073 if (src2 != nullptr)
Giorgio Arena945ae9e2021-10-13 11:13:04 +010074 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010075 if (is_data_type_quantized(src0->data_type()))
Giorgio Arena51847d52021-10-19 15:45:57 +010076 {
77 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src2, 1, DataType::S32);
78 }
79 else
80 {
81 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, src2);
82 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010083 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src2->dimension(0) != src1->dimension(0),
84 "Biases size and number of dst feature maps should match");
Sheri Zhang5dda2172021-10-15 19:54:17 +010085 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src2->num_dimensions() > 1, "Biases should be one dimensional");
Giorgio Arena945ae9e2021-10-13 11:13:04 +010086 }
87
88 // Checks performed when dst is configured
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010089 if (dst->total_size() != 0)
Giorgio Arena945ae9e2021-10-13 11:13:04 +010090 {
Sheri Zhang5dda2172021-10-15 19:54:17 +010091 ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->dimension(0) != src1->dimension(0), "Weights and dst OFMs should match");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010092 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(
93 dst->tensor_shape(),
94 misc::shape_calculator::compute_conv3d_shape(src0->tensor_shape(), src1->tensor_shape(), conv3d_info));
Sheri Zhang5dda2172021-10-15 19:54:17 +010095 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src0, dst);
Giorgio Arena945ae9e2021-10-13 11:13:04 +010096 }
97
98 return Status{};
99}
100} // namespace
101
102ClDirectConv3dKernel::ClDirectConv3dKernel()
103{
104 _type = CLKernelType::DIRECT;
105}
106
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100107void ClDirectConv3dKernel::configure(const CLCompileContext &compile_context,
108 const ITensorInfo *src0,
109 const ITensorInfo *src1,
110 const ITensorInfo *src2,
111 ITensorInfo *dst,
112 const Conv3dInfo &conv3d_info)
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100113{
Sheri Zhang5dda2172021-10-15 19:54:17 +0100114 ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100115
116 // Perform validation
Sheri Zhang5dda2172021-10-15 19:54:17 +0100117 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src0, src1, src2, dst, conv3d_info));
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100118
119 // Create window and update padding
Sheri Zhang5dda2172021-10-15 19:54:17 +0100120 const DataType data_type = src0->data_type();
121 const size_t src_width = src0->dimension(1);
122 const size_t src_height = src0->dimension(2);
123 const size_t src_depth = src0->dimension(3);
124 const size_t src_channels = src0->dimension(0);
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100125 const size_t dst_width = dst->dimension(1);
126 const size_t dst_height = dst->dimension(2);
127 const size_t dst_depth = dst->dimension(3);
128 const size_t dst_channels = dst->dimension(0);
Sheri Zhang5dda2172021-10-15 19:54:17 +0100129 const size_t weights_width = src1->dimension(2);
130 const size_t weights_height = src1->dimension(3);
131 const size_t weights_depth = src1->dimension(4);
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100132 const size_t pad_left = conv3d_info.padding.left;
133 const size_t pad_top = conv3d_info.padding.top;
134 const size_t pad_front = conv3d_info.padding.front;
135 const size_t conv_stride_x = conv3d_info.stride.x();
136 const size_t conv_stride_y = conv3d_info.stride.y();
137 const size_t conv_stride_z = conv3d_info.stride.z();
138
139 const size_t n0 = std::min(dst->dimension(0), static_cast<size_t>(4u));
140 const size_t m0 = (dst->tensor_shape()[0] > 16) ? ((data_type == DataType::F32) ? 2U : 4U) : 1U;
Sheri Zhang5dda2172021-10-15 19:54:17 +0100141 const size_t k0 = adjust_vec_size(8u, src0->dimension(0));
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100142 const size_t partial_store_n0 = dst->dimension(0) % n0;
143
144 CLBuildOptions build_options;
145 build_options.add_option("-cl-fast-relaxed-math");
146 build_options.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100147 build_options.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src_width));
148 build_options.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src_height));
149 build_options.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(src_depth));
150 build_options.add_option("-DSRC_CHANNELS=" + support::cpp11::to_string(src_channels));
151 build_options.add_option("-DDST_WIDTH=" + support::cpp11::to_string(dst_width));
152 build_options.add_option("-DDST_HEIGHT=" + support::cpp11::to_string(dst_height));
153 build_options.add_option("-DDST_DEPTH=" + support::cpp11::to_string(dst_depth));
154 build_options.add_option("-DDST_CHANNELS=" + support::cpp11::to_string(dst_channels));
155 build_options.add_option("-DWEI_WIDTH=" + support::cpp11::to_string(weights_width));
156 build_options.add_option("-DWEI_HEIGHT=" + support::cpp11::to_string(weights_height));
157 build_options.add_option("-DWEI_DEPTH=" + support::cpp11::to_string(weights_depth));
158 build_options.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_stride_x));
159 build_options.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_stride_y));
160 build_options.add_option("-DSTRIDE_Z=" + support::cpp11::to_string(conv_stride_z));
161 build_options.add_option("-DPAD_LEFT=" + support::cpp11::to_string(pad_left));
162 build_options.add_option("-DPAD_TOP=" + support::cpp11::to_string(pad_top));
163 build_options.add_option("-DPAD_FRONT=" + support::cpp11::to_string(pad_front));
164 build_options.add_option("-DN0=" + support::cpp11::to_string(n0));
165 build_options.add_option("-DM0=" + support::cpp11::to_string(m0));
166 build_options.add_option("-DK0=" + support::cpp11::to_string(k0));
167 build_options.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_store_n0));
Giorgio Arena51847d52021-10-19 15:45:57 +0100168
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100169 if (src2 != nullptr)
Giorgio Arena51847d52021-10-19 15:45:57 +0100170 {
171 build_options.add_option(std::string("-DHAS_BIAS"));
172 build_options.add_option(std::string("-DBIA_DATA_TYPE=" + get_cl_type_from_data_type(src2->data_type())));
173 }
174
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100175 if (is_data_type_quantized(data_type))
Giorgio Arena51847d52021-10-19 15:45:57 +0100176 {
177 const UniformQuantizationInfo iqinfo = src0->quantization_info().uniform();
178 const UniformQuantizationInfo wqinfo = src1->quantization_info().uniform();
179 const UniformQuantizationInfo oqinfo = dst->quantization_info().uniform();
180
181 PixelValue zero_value = PixelValue(0, src0->data_type(), src0->quantization_info());
182 int zero_value_s32;
183 zero_value.get(zero_value_s32);
184
185 float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale;
186 int output_multiplier = 0;
187 int output_shift = 0;
188 quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
189 build_options.add_option("-DIS_QUANTIZED");
190 build_options.add_option("-DDST_MULTIPLIER=" + support::cpp11::to_string(output_multiplier));
191 build_options.add_option("-DDST_SHIFT=" + support::cpp11::to_string(output_shift));
192 build_options.add_option("-DSRC_OFFSET=" + support::cpp11::to_string(-iqinfo.offset));
193 build_options.add_option("-DWEI_OFFSET=" + support::cpp11::to_string(-wqinfo.offset));
194 build_options.add_option("-DDST_OFFSET=" + support::cpp11::to_string(oqinfo.offset));
195 build_options.add_option("-DZERO_VALUE=" + support::cpp11::to_string(zero_value_s32));
196 build_options.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(DataType::S32));
197 }
198 else
199 {
200 build_options.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(DataType::F32));
201 build_options.add_option("-DZERO_VALUE=" + support::cpp11::to_string(0));
202 build_options.add_option("-DSRC_OFFSET=" + support::cpp11::to_string(0));
203 build_options.add_option("-DWEI_OFFSET=" + support::cpp11::to_string(0));
204 build_options.add_option("-DDST_OFFSET=" + support::cpp11::to_string(0));
205 }
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100206
207 std::string kernel_name = "direct_convolution3d_ndhwc";
208 _kernel = create_kernel(compile_context, kernel_name, build_options.options());
209
210 // Configure kernel window
211 Window win = calculate_max_window(*dst, Steps(n0, m0));
212 ICLKernel::configure_internal(win);
213
214 // Set config_id for enabling LWS tuning
215 _config_id = kernel_name;
216 _config_id += "_";
217 _config_id += lower_string(string_from_data_type(data_type));
218 _config_id += "_";
219 _config_id += support::cpp11::to_string(weights_width);
220 _config_id += "_";
221 _config_id += support::cpp11::to_string(weights_height);
222 _config_id += "_";
223 _config_id += support::cpp11::to_string(weights_depth);
224 _config_id += "_";
225 _config_id += support::cpp11::to_string(conv_stride_x);
226 _config_id += "_";
227 _config_id += support::cpp11::to_string(conv_stride_y);
228 _config_id += "_";
229 _config_id += support::cpp11::to_string(conv_stride_z);
230 _config_id += "_";
231 _config_id += support::cpp11::to_string(dst_width);
232 _config_id += "_";
233 _config_id += support::cpp11::to_string(dst_height);
234 _config_id += "_";
235 _config_id += support::cpp11::to_string(dst_channels);
236}
237
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100238Status ClDirectConv3dKernel::validate(const ITensorInfo *src0,
239 const ITensorInfo *src1,
240 const ITensorInfo *src2,
241 const ITensorInfo *dst,
242 const Conv3dInfo &conv3d_info)
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100243{
Sheri Zhang5dda2172021-10-15 19:54:17 +0100244 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src0, src1, src2, dst, conv3d_info));
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100245 return Status{};
246}
247
248void ClDirectConv3dKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
249{
250 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
251 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
252
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100253 const auto src =
254 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
255 const auto weights =
256 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
257 const auto biases =
258 utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2));
259 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100260
261 // Get initial windows
262 Window slice = window.first_slice_window_3D();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100263 slice.set(Window::DimY, Window::Dimension(0,
264 ceil_to_multiple(dst->info()->dimension(1) * dst->info()->dimension(2) *
265 dst->info()->dimension(3),
266 slice.y().step()),
267 slice.y().step()));
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100268 slice.set(Window::DimZ, Window::Dimension(0, dst->info()->dimension(4), 1));
269
270 unsigned int idx = 0;
271 add_4D_tensor_argument(idx, src, slice);
272 add_4D_tensor_argument(idx, dst, slice);
273 add_4D_tensor_argument(idx, weights, slice);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100274 if (biases != nullptr)
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100275 {
276 add_1D_tensor_argument(idx, biases, slice);
277 }
278 enqueue(queue, *this, slice, lws_hint());
279}
280} // namespace kernels
281} // namespace opencl
282} // namespace arm_compute