1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2021-2022 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "src/gpu/cl/kernels/ClDirectConv3dKernel.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/core/CL/ICLTensor.h"
27*c217d954SCole Faust #include "arm_compute/core/utils/misc/ShapeCalculator.h"
28*c217d954SCole Faust #include "arm_compute/core/utils/quantization/AsymmHelpers.h"
29*c217d954SCole Faust #include "src/core/CL/CLValidate.h"
30*c217d954SCole Faust #include "src/core/helpers/WindowHelpers.h"
31*c217d954SCole Faust #include "support/Cast.h"
32*c217d954SCole Faust
33*c217d954SCole Faust namespace arm_compute
34*c217d954SCole Faust {
35*c217d954SCole Faust namespace opencl
36*c217d954SCole Faust {
37*c217d954SCole Faust namespace kernels
38*c217d954SCole Faust {
39*c217d954SCole Faust namespace
40*c217d954SCole Faust {
validate_arguments(const ITensorInfo * src0,const ITensorInfo * src1,const ITensorInfo * src2,const ITensorInfo * dst,const Conv3dInfo & conv3d_info)41*c217d954SCole Faust Status validate_arguments(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const Conv3dInfo &conv3d_info)
42*c217d954SCole Faust {
43*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_LAYOUT(src0, src1, dst);
44*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(src0->data_layout() != DataLayout::NDHWC, "Only NDHWC layout supported");
45*c217d954SCole Faust
46*c217d954SCole Faust // When fusing activation, same workaround introduced for COMPMID-5324 may be necessary
47*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv3d_info.act_info.enabled(), "Fused activation not supported");
48*c217d954SCole Faust
49*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(src0);
50*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src0, 1, DataType::F16, DataType::F32, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
51*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src0, src1);
52*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(conv3d_info.dilation != Size3D(1U, 1U, 1U));
53*c217d954SCole Faust
54*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(src1->dimension(1) != src0->dimension(0), "Weights feature map dimension should match the respective src's one");
55*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(src1->num_dimensions() > 5, "Weights can be at most 5 dimensional");
56*c217d954SCole Faust
57*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(src1->dimension(2) > (src0->dimension(1) + conv3d_info.padding.left + conv3d_info.padding.right));
58*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(src1->dimension(3) > (src0->dimension(2) + conv3d_info.padding.top + conv3d_info.padding.bottom));
59*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON(src1->dimension(4) > (src0->dimension(3) + conv3d_info.padding.front + conv3d_info.padding.back));
60*c217d954SCole Faust
61*c217d954SCole Faust if(src2 != nullptr)
62*c217d954SCole Faust {
63*c217d954SCole Faust if(is_data_type_quantized(src0->data_type()))
64*c217d954SCole Faust {
65*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src2, 1, DataType::S32);
66*c217d954SCole Faust }
67*c217d954SCole Faust else
68*c217d954SCole Faust {
69*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, src2);
70*c217d954SCole Faust }
71*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(src2->dimension(0) != src1->dimension(0), "Biases size and number of dst feature maps should match");
72*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(src2->num_dimensions() > 1, "Biases should be one dimensional");
73*c217d954SCole Faust }
74*c217d954SCole Faust
75*c217d954SCole Faust // Checks performed when dst is configured
76*c217d954SCole Faust if(dst->total_size() != 0)
77*c217d954SCole Faust {
78*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->dimension(0) != src1->dimension(0), "Weights and dst OFMs should match");
79*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(), misc::shape_calculator::compute_conv3d_shape(src0->tensor_shape(), src1->tensor_shape(), conv3d_info));
80*c217d954SCole Faust ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src0, dst);
81*c217d954SCole Faust }
82*c217d954SCole Faust
83*c217d954SCole Faust return Status{};
84*c217d954SCole Faust }
85*c217d954SCole Faust } // namespace
86*c217d954SCole Faust
ClDirectConv3dKernel()87*c217d954SCole Faust ClDirectConv3dKernel::ClDirectConv3dKernel()
88*c217d954SCole Faust {
89*c217d954SCole Faust _type = CLKernelType::DIRECT;
90*c217d954SCole Faust }
91*c217d954SCole Faust
configure(const CLCompileContext & compile_context,const ITensorInfo * src0,const ITensorInfo * src1,const ITensorInfo * src2,ITensorInfo * dst,const Conv3dInfo & conv3d_info)92*c217d954SCole Faust void ClDirectConv3dKernel::configure(const CLCompileContext &compile_context, const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *src2, ITensorInfo *dst,
93*c217d954SCole Faust const Conv3dInfo &conv3d_info)
94*c217d954SCole Faust {
95*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
96*c217d954SCole Faust
97*c217d954SCole Faust // Perform validation
98*c217d954SCole Faust ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src0, src1, src2, dst, conv3d_info));
99*c217d954SCole Faust
100*c217d954SCole Faust // Create window and update padding
101*c217d954SCole Faust const DataType data_type = src0->data_type();
102*c217d954SCole Faust const size_t src_width = src0->dimension(1);
103*c217d954SCole Faust const size_t src_height = src0->dimension(2);
104*c217d954SCole Faust const size_t src_depth = src0->dimension(3);
105*c217d954SCole Faust const size_t src_channels = src0->dimension(0);
106*c217d954SCole Faust const size_t dst_width = dst->dimension(1);
107*c217d954SCole Faust const size_t dst_height = dst->dimension(2);
108*c217d954SCole Faust const size_t dst_depth = dst->dimension(3);
109*c217d954SCole Faust const size_t dst_channels = dst->dimension(0);
110*c217d954SCole Faust const size_t weights_width = src1->dimension(2);
111*c217d954SCole Faust const size_t weights_height = src1->dimension(3);
112*c217d954SCole Faust const size_t weights_depth = src1->dimension(4);
113*c217d954SCole Faust const size_t pad_left = conv3d_info.padding.left;
114*c217d954SCole Faust const size_t pad_top = conv3d_info.padding.top;
115*c217d954SCole Faust const size_t pad_front = conv3d_info.padding.front;
116*c217d954SCole Faust const size_t conv_stride_x = conv3d_info.stride.x();
117*c217d954SCole Faust const size_t conv_stride_y = conv3d_info.stride.y();
118*c217d954SCole Faust const size_t conv_stride_z = conv3d_info.stride.z();
119*c217d954SCole Faust
120*c217d954SCole Faust const size_t n0 = std::min(dst->dimension(0), static_cast<size_t>(4u));
121*c217d954SCole Faust const size_t m0 = (dst->tensor_shape()[0] > 16) ? ((data_type == DataType::F32) ? 2U : 4U) : 1U;
122*c217d954SCole Faust const size_t k0 = adjust_vec_size(8u, src0->dimension(0));
123*c217d954SCole Faust const size_t partial_store_n0 = dst->dimension(0) % n0;
124*c217d954SCole Faust
125*c217d954SCole Faust CLBuildOptions build_options;
126*c217d954SCole Faust build_options.add_option("-cl-fast-relaxed-math");
127*c217d954SCole Faust build_options.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
128*c217d954SCole Faust build_options.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(src_width));
129*c217d954SCole Faust build_options.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(src_height));
130*c217d954SCole Faust build_options.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(src_depth));
131*c217d954SCole Faust build_options.add_option("-DSRC_CHANNELS=" + support::cpp11::to_string(src_channels));
132*c217d954SCole Faust build_options.add_option("-DDST_WIDTH=" + support::cpp11::to_string(dst_width));
133*c217d954SCole Faust build_options.add_option("-DDST_HEIGHT=" + support::cpp11::to_string(dst_height));
134*c217d954SCole Faust build_options.add_option("-DDST_DEPTH=" + support::cpp11::to_string(dst_depth));
135*c217d954SCole Faust build_options.add_option("-DDST_CHANNELS=" + support::cpp11::to_string(dst_channels));
136*c217d954SCole Faust build_options.add_option("-DWEI_WIDTH=" + support::cpp11::to_string(weights_width));
137*c217d954SCole Faust build_options.add_option("-DWEI_HEIGHT=" + support::cpp11::to_string(weights_height));
138*c217d954SCole Faust build_options.add_option("-DWEI_DEPTH=" + support::cpp11::to_string(weights_depth));
139*c217d954SCole Faust build_options.add_option("-DSTRIDE_X=" + support::cpp11::to_string(conv_stride_x));
140*c217d954SCole Faust build_options.add_option("-DSTRIDE_Y=" + support::cpp11::to_string(conv_stride_y));
141*c217d954SCole Faust build_options.add_option("-DSTRIDE_Z=" + support::cpp11::to_string(conv_stride_z));
142*c217d954SCole Faust build_options.add_option("-DPAD_LEFT=" + support::cpp11::to_string(pad_left));
143*c217d954SCole Faust build_options.add_option("-DPAD_TOP=" + support::cpp11::to_string(pad_top));
144*c217d954SCole Faust build_options.add_option("-DPAD_FRONT=" + support::cpp11::to_string(pad_front));
145*c217d954SCole Faust build_options.add_option("-DN0=" + support::cpp11::to_string(n0));
146*c217d954SCole Faust build_options.add_option("-DM0=" + support::cpp11::to_string(m0));
147*c217d954SCole Faust build_options.add_option("-DK0=" + support::cpp11::to_string(k0));
148*c217d954SCole Faust build_options.add_option("-DPARTIAL_N0=" + support::cpp11::to_string(partial_store_n0));
149*c217d954SCole Faust
150*c217d954SCole Faust if(src2 != nullptr)
151*c217d954SCole Faust {
152*c217d954SCole Faust build_options.add_option(std::string("-DHAS_BIAS"));
153*c217d954SCole Faust build_options.add_option(std::string("-DBIA_DATA_TYPE=" + get_cl_type_from_data_type(src2->data_type())));
154*c217d954SCole Faust }
155*c217d954SCole Faust
156*c217d954SCole Faust if(is_data_type_quantized(data_type))
157*c217d954SCole Faust {
158*c217d954SCole Faust const UniformQuantizationInfo iqinfo = src0->quantization_info().uniform();
159*c217d954SCole Faust const UniformQuantizationInfo wqinfo = src1->quantization_info().uniform();
160*c217d954SCole Faust const UniformQuantizationInfo oqinfo = dst->quantization_info().uniform();
161*c217d954SCole Faust
162*c217d954SCole Faust PixelValue zero_value = PixelValue(0, src0->data_type(), src0->quantization_info());
163*c217d954SCole Faust int zero_value_s32;
164*c217d954SCole Faust zero_value.get(zero_value_s32);
165*c217d954SCole Faust
166*c217d954SCole Faust float multiplier = iqinfo.scale * wqinfo.scale / oqinfo.scale;
167*c217d954SCole Faust int output_multiplier = 0;
168*c217d954SCole Faust int output_shift = 0;
169*c217d954SCole Faust quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
170*c217d954SCole Faust build_options.add_option("-DIS_QUANTIZED");
171*c217d954SCole Faust build_options.add_option("-DDST_MULTIPLIER=" + support::cpp11::to_string(output_multiplier));
172*c217d954SCole Faust build_options.add_option("-DDST_SHIFT=" + support::cpp11::to_string(output_shift));
173*c217d954SCole Faust build_options.add_option("-DSRC_OFFSET=" + support::cpp11::to_string(-iqinfo.offset));
174*c217d954SCole Faust build_options.add_option("-DWEI_OFFSET=" + support::cpp11::to_string(-wqinfo.offset));
175*c217d954SCole Faust build_options.add_option("-DDST_OFFSET=" + support::cpp11::to_string(oqinfo.offset));
176*c217d954SCole Faust build_options.add_option("-DZERO_VALUE=" + support::cpp11::to_string(zero_value_s32));
177*c217d954SCole Faust build_options.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(DataType::S32));
178*c217d954SCole Faust }
179*c217d954SCole Faust else
180*c217d954SCole Faust {
181*c217d954SCole Faust build_options.add_option("-DACC_DATA_TYPE=" + get_cl_type_from_data_type(DataType::F32));
182*c217d954SCole Faust build_options.add_option("-DZERO_VALUE=" + support::cpp11::to_string(0));
183*c217d954SCole Faust build_options.add_option("-DSRC_OFFSET=" + support::cpp11::to_string(0));
184*c217d954SCole Faust build_options.add_option("-DWEI_OFFSET=" + support::cpp11::to_string(0));
185*c217d954SCole Faust build_options.add_option("-DDST_OFFSET=" + support::cpp11::to_string(0));
186*c217d954SCole Faust }
187*c217d954SCole Faust
188*c217d954SCole Faust std::string kernel_name = "direct_convolution3d_ndhwc";
189*c217d954SCole Faust _kernel = create_kernel(compile_context, kernel_name, build_options.options());
190*c217d954SCole Faust
191*c217d954SCole Faust // Configure kernel window
192*c217d954SCole Faust Window win = calculate_max_window(*dst, Steps(n0, m0));
193*c217d954SCole Faust ICLKernel::configure_internal(win);
194*c217d954SCole Faust
195*c217d954SCole Faust // Set config_id for enabling LWS tuning
196*c217d954SCole Faust _config_id = kernel_name;
197*c217d954SCole Faust _config_id += "_";
198*c217d954SCole Faust _config_id += lower_string(string_from_data_type(data_type));
199*c217d954SCole Faust _config_id += "_";
200*c217d954SCole Faust _config_id += support::cpp11::to_string(weights_width);
201*c217d954SCole Faust _config_id += "_";
202*c217d954SCole Faust _config_id += support::cpp11::to_string(weights_height);
203*c217d954SCole Faust _config_id += "_";
204*c217d954SCole Faust _config_id += support::cpp11::to_string(weights_depth);
205*c217d954SCole Faust _config_id += "_";
206*c217d954SCole Faust _config_id += support::cpp11::to_string(conv_stride_x);
207*c217d954SCole Faust _config_id += "_";
208*c217d954SCole Faust _config_id += support::cpp11::to_string(conv_stride_y);
209*c217d954SCole Faust _config_id += "_";
210*c217d954SCole Faust _config_id += support::cpp11::to_string(conv_stride_z);
211*c217d954SCole Faust _config_id += "_";
212*c217d954SCole Faust _config_id += support::cpp11::to_string(dst_width);
213*c217d954SCole Faust _config_id += "_";
214*c217d954SCole Faust _config_id += support::cpp11::to_string(dst_height);
215*c217d954SCole Faust _config_id += "_";
216*c217d954SCole Faust _config_id += support::cpp11::to_string(dst_channels);
217*c217d954SCole Faust }
218*c217d954SCole Faust
validate(const ITensorInfo * src0,const ITensorInfo * src1,const ITensorInfo * src2,const ITensorInfo * dst,const Conv3dInfo & conv3d_info)219*c217d954SCole Faust Status ClDirectConv3dKernel::validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const Conv3dInfo &conv3d_info)
220*c217d954SCole Faust {
221*c217d954SCole Faust ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src0, src1, src2, dst, conv3d_info));
222*c217d954SCole Faust return Status{};
223*c217d954SCole Faust }
224*c217d954SCole Faust
run_op(ITensorPack & tensors,const Window & window,cl::CommandQueue & queue)225*c217d954SCole Faust void ClDirectConv3dKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
226*c217d954SCole Faust {
227*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
228*c217d954SCole Faust ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
229*c217d954SCole Faust
230*c217d954SCole Faust const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_0));
231*c217d954SCole Faust const auto weights = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_1));
232*c217d954SCole Faust const auto biases = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC_2));
233*c217d954SCole Faust auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
234*c217d954SCole Faust
235*c217d954SCole Faust // Get initial windows
236*c217d954SCole Faust Window slice = window.first_slice_window_3D();
237*c217d954SCole Faust 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()));
238*c217d954SCole Faust slice.set(Window::DimZ, Window::Dimension(0, dst->info()->dimension(4), 1));
239*c217d954SCole Faust
240*c217d954SCole Faust unsigned int idx = 0;
241*c217d954SCole Faust add_4D_tensor_argument(idx, src, slice);
242*c217d954SCole Faust add_4D_tensor_argument(idx, dst, slice);
243*c217d954SCole Faust add_4D_tensor_argument(idx, weights, slice);
244*c217d954SCole Faust if(biases != nullptr)
245*c217d954SCole Faust {
246*c217d954SCole Faust add_1D_tensor_argument(idx, biases, slice);
247*c217d954SCole Faust }
248*c217d954SCole Faust enqueue(queue, *this, slice, lws_hint());
249*c217d954SCole Faust }
250*c217d954SCole Faust } // namespace kernels
251*c217d954SCole Faust } // namespace opencl
252*c217d954SCole Faust } // namespace arm_compute
253