blob: a0735b1112802f4c3c9ca44c0735e3ad555b35d2 [file] [log] [blame]
Giorgio Arena945ae9e2021-10-13 11:13:04 +01001/*
2 * Copyright (c) 2021 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/ClDirectConv3dKernel.h"
25
26#include "arm_compute/core/CL/ICLTensor.h"
27#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Giorgio Arena51847d52021-10-19 15:45:57 +010028#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Giorgio Arena945ae9e2021-10-13 11:13:04 +010029#include "src/core/CL/CLValidate.h"
30#include "src/core/helpers/WindowHelpers.h"
31#include "support/Cast.h"
32
33namespace arm_compute
34{
35namespace opencl
36{
37namespace kernels
38{
39namespace
40{
Sheri Zhang5dda2172021-10-15 19:54:17 +010041Status validate_arguments(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const Conv3dInfo &conv3d_info)
Giorgio Arena945ae9e2021-10-13 11:13:04 +010042{
Sheri Zhang5dda2172021-10-15 19:54:17 +010043 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_LAYOUT(src0, src1, dst);
44 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src0->data_layout() != DataLayout::NDHWC, "Only NDHWC layout supported");
Giorgio Arena945ae9e2021-10-13 11:13:04 +010045 ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv3d_info.act_info.enabled(), "Fused activation not supported");
46
Sheri Zhang5dda2172021-10-15 19:54:17 +010047 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src0);
Giorgio Arena51847d52021-10-19 15:45:57 +010048 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src0, 1, DataType::F16, DataType::F32, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
Sheri Zhang5dda2172021-10-15 19:54:17 +010049 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src0, src1);
Freddie Liardet69df64f2021-10-26 14:06:47 +010050 ARM_COMPUTE_RETURN_ERROR_ON(conv3d_info.dilation != Size3D(1U, 1U, 1U));
Giorgio Arena945ae9e2021-10-13 11:13:04 +010051
Sheri Zhang5dda2172021-10-15 19:54:17 +010052 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src1->dimension(1) != src0->dimension(0), "Weights feature map dimension should match the respective src's one");
53 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src1->num_dimensions() > 5, "Weights can be at most 5 dimensional");
Giorgio Arena945ae9e2021-10-13 11:13:04 +010054
Sheri Zhang5dda2172021-10-15 19:54:17 +010055 ARM_COMPUTE_RETURN_ERROR_ON(src1->dimension(2) > (src0->dimension(1) + conv3d_info.padding.left + conv3d_info.padding.right));
56 ARM_COMPUTE_RETURN_ERROR_ON(src1->dimension(3) > (src0->dimension(2) + conv3d_info.padding.top + conv3d_info.padding.bottom));
57 ARM_COMPUTE_RETURN_ERROR_ON(src1->dimension(4) > (src0->dimension(3) + conv3d_info.padding.front + conv3d_info.padding.back));
Giorgio Arena945ae9e2021-10-13 11:13:04 +010058
Sheri Zhang5dda2172021-10-15 19:54:17 +010059 if(src2 != nullptr)
Giorgio Arena945ae9e2021-10-13 11:13:04 +010060 {
Giorgio Arena51847d52021-10-19 15:45:57 +010061 if(is_data_type_quantized(src0->data_type()))
62 {
63 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src2, 1, DataType::S32);
64 }
65 else
66 {
67 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, src2);
68 }
Sheri Zhang5dda2172021-10-15 19:54:17 +010069 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src2->dimension(0) != src1->dimension(0), "Biases size and number of dst feature maps should match");
70 ARM_COMPUTE_RETURN_ERROR_ON_MSG(src2->num_dimensions() > 1, "Biases should be one dimensional");
Giorgio Arena945ae9e2021-10-13 11:13:04 +010071 }
72
73 // Checks performed when dst is configured
74 if(dst->total_size() != 0)
75 {
Sheri Zhang5dda2172021-10-15 19:54:17 +010076 ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->dimension(0) != src1->dimension(0), "Weights and dst OFMs should match");
77 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(), misc::shape_calculator::compute_conv3d_shape(src0->tensor_shape(), src1->tensor_shape(), conv3d_info));
78 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src0, dst);
Giorgio Arena945ae9e2021-10-13 11:13:04 +010079 }
80
81 return Status{};
82}
83} // namespace
84
85ClDirectConv3dKernel::ClDirectConv3dKernel()
86{
87 _type = CLKernelType::DIRECT;
88}
89
Sheri Zhang5dda2172021-10-15 19:54:17 +010090void ClDirectConv3dKernel::configure(const CLCompileContext &compile_context, const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *src2, ITensorInfo *dst,
Giorgio Arena945ae9e2021-10-13 11:13:04 +010091 const Conv3dInfo &conv3d_info)
92{
Sheri Zhang5dda2172021-10-15 19:54:17 +010093 ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
Giorgio Arena945ae9e2021-10-13 11:13:04 +010094
95 // Perform validation
Sheri Zhang5dda2172021-10-15 19:54:17 +010096 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src0, src1, src2, dst, conv3d_info));
Giorgio Arena945ae9e2021-10-13 11:13:04 +010097
98 // Create window and update padding
Sheri Zhang5dda2172021-10-15 19:54:17 +010099 const DataType data_type = src0->data_type();
100 const size_t src_width = src0->dimension(1);
101 const size_t src_height = src0->dimension(2);
102 const size_t src_depth = src0->dimension(3);
103 const size_t src_channels = src0->dimension(0);
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100104 const size_t dst_width = dst->dimension(1);
105 const size_t dst_height = dst->dimension(2);
106 const size_t dst_depth = dst->dimension(3);
107 const size_t dst_channels = dst->dimension(0);
Sheri Zhang5dda2172021-10-15 19:54:17 +0100108 const size_t weights_width = src1->dimension(2);
109 const size_t weights_height = src1->dimension(3);
110 const size_t weights_depth = src1->dimension(4);
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100111 const size_t pad_left = conv3d_info.padding.left;
112 const size_t pad_top = conv3d_info.padding.top;
113 const size_t pad_front = conv3d_info.padding.front;
114 const size_t conv_stride_x = conv3d_info.stride.x();
115 const size_t conv_stride_y = conv3d_info.stride.y();
116 const size_t conv_stride_z = conv3d_info.stride.z();
117
118 const size_t n0 = std::min(dst->dimension(0), static_cast<size_t>(4u));
119 const size_t m0 = (dst->tensor_shape()[0] > 16) ? ((data_type == DataType::F32) ? 2U : 4U) : 1U;
Sheri Zhang5dda2172021-10-15 19:54:17 +0100120 const size_t k0 = adjust_vec_size(8u, src0->dimension(0));
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100121 const size_t partial_store_n0 = dst->dimension(0) % n0;
122
123 CLBuildOptions build_options;
124 build_options.add_option("-cl-fast-relaxed-math");
125 build_options.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100126 build_options.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src_width));
127 build_options.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src_height));
128 build_options.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(src_depth));
129 build_options.add_option("-DSRC_CHANNELS=" + support::cpp11::to_string(src_channels));
130 build_options.add_option("-DDST_WIDTH=" + support::cpp11::to_string(dst_width));
131 build_options.add_option("-DDST_HEIGHT=" + support::cpp11::to_string(dst_height));
132 build_options.add_option("-DDST_DEPTH=" + support::cpp11::to_string(dst_depth));
133 build_options.add_option("-DDST_CHANNELS=" + support::cpp11::to_string(dst_channels));
134 build_options.add_option("-DWEI_WIDTH=" + support::cpp11::to_string(weights_width));
135 build_options.add_option("-DWEI_HEIGHT=" + support::cpp11::to_string(weights_height));
136 build_options.add_option("-DWEI_DEPTH=" + support::cpp11::to_string(weights_depth));
137 build_options.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_stride_x));
138 build_options.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_stride_y));
139 build_options.add_option("-DSTRIDE_Z=" + support::cpp11::to_string(conv_stride_z));
140 build_options.add_option("-DPAD_LEFT=" + support::cpp11::to_string(pad_left));
141 build_options.add_option("-DPAD_TOP=" + support::cpp11::to_string(pad_top));
142 build_options.add_option("-DPAD_FRONT=" + support::cpp11::to_string(pad_front));
143 build_options.add_option("-DN0=" + support::cpp11::to_string(n0));
144 build_options.add_option("-DM0=" + support::cpp11::to_string(m0));
145 build_options.add_option("-DK0=" + support::cpp11::to_string(k0));
146 build_options.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_store_n0));
Giorgio Arena51847d52021-10-19 15:45:57 +0100147
148 if(src2 != nullptr)
149 {
150 build_options.add_option(std::string("-DHAS_BIAS"));
151 build_options.add_option(std::string("-DBIA_DATA_TYPE=" + get_cl_type_from_data_type(src2->data_type())));
152 }
153
154 if(is_data_type_quantized(data_type))
155 {
156 const UniformQuantizationInfo iqinfo = src0->quantization_info().uniform();
157 const UniformQuantizationInfo wqinfo = src1->quantization_info().uniform();
158 const UniformQuantizationInfo oqinfo = dst->quantization_info().uniform();
159
160 PixelValue zero_value = PixelValue(0, src0->data_type(), src0->quantization_info());
161 int zero_value_s32;
162 zero_value.get(zero_value_s32);
163
164 float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale;
165 int output_multiplier = 0;
166 int output_shift = 0;
167 quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
168 build_options.add_option("-DIS_QUANTIZED");
169 build_options.add_option("-DDST_MULTIPLIER=" + support::cpp11::to_string(output_multiplier));
170 build_options.add_option("-DDST_SHIFT=" + support::cpp11::to_string(output_shift));
171 build_options.add_option("-DSRC_OFFSET=" + support::cpp11::to_string(-iqinfo.offset));
172 build_options.add_option("-DWEI_OFFSET=" + support::cpp11::to_string(-wqinfo.offset));
173 build_options.add_option("-DDST_OFFSET=" + support::cpp11::to_string(oqinfo.offset));
174 build_options.add_option("-DZERO_VALUE=" + support::cpp11::to_string(zero_value_s32));
175 build_options.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(DataType::S32));
176 }
177 else
178 {
179 build_options.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(DataType::F32));
180 build_options.add_option("-DZERO_VALUE=" + support::cpp11::to_string(0));
181 build_options.add_option("-DSRC_OFFSET=" + support::cpp11::to_string(0));
182 build_options.add_option("-DWEI_OFFSET=" + support::cpp11::to_string(0));
183 build_options.add_option("-DDST_OFFSET=" + support::cpp11::to_string(0));
184 }
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100185
186 std::string kernel_name = "direct_convolution3d_ndhwc";
187 _kernel = create_kernel(compile_context, kernel_name, build_options.options());
188
189 // Configure kernel window
190 Window win = calculate_max_window(*dst, Steps(n0, m0));
191 ICLKernel::configure_internal(win);
192
193 // Set config_id for enabling LWS tuning
194 _config_id = kernel_name;
195 _config_id += "_";
196 _config_id += lower_string(string_from_data_type(data_type));
197 _config_id += "_";
198 _config_id += support::cpp11::to_string(weights_width);
199 _config_id += "_";
200 _config_id += support::cpp11::to_string(weights_height);
201 _config_id += "_";
202 _config_id += support::cpp11::to_string(weights_depth);
203 _config_id += "_";
204 _config_id += support::cpp11::to_string(conv_stride_x);
205 _config_id += "_";
206 _config_id += support::cpp11::to_string(conv_stride_y);
207 _config_id += "_";
208 _config_id += support::cpp11::to_string(conv_stride_z);
209 _config_id += "_";
210 _config_id += support::cpp11::to_string(dst_width);
211 _config_id += "_";
212 _config_id += support::cpp11::to_string(dst_height);
213 _config_id += "_";
214 _config_id += support::cpp11::to_string(dst_channels);
215}
216
Sheri Zhang5dda2172021-10-15 19:54:17 +0100217Status ClDirectConv3dKernel::validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const Conv3dInfo &conv3d_info)
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100218{
Sheri Zhang5dda2172021-10-15 19:54:17 +0100219 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src0, src1, src2, dst, conv3d_info));
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100220 return Status{};
221}
222
223void ClDirectConv3dKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
224{
225 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
226 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
227
228 const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
229 const auto weights = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
230 const auto biases = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2));
231 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
232
233 // Get initial windows
234 Window slice = window.first_slice_window_3D();
235 slice.set(Window::DimY, Window::Dimension(0, ceil_to_multiple(dst->info()->dimension(1) * dst->info()->dimension(2) * dst->info()->dimension(3), slice.y().step()), slice.y().step()));
236 slice.set(Window::DimZ, Window::Dimension(0, dst->info()->dimension(4), 1));
237
238 unsigned int idx = 0;
239 add_4D_tensor_argument(idx, src, slice);
240 add_4D_tensor_argument(idx, dst, slice);
241 add_4D_tensor_argument(idx, weights, slice);
242 if(biases != nullptr)
243 {
244 add_1D_tensor_argument(idx, biases, slice);
245 }
246 enqueue(queue, *this, slice, lws_hint());
247}
248} // namespace kernels
249} // namespace opencl
250} // namespace arm_compute