Compute Library
 22.05
ClDirectConv3dKernel.cpp
Go to the documentation of this file.
1 /*
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  */
25 
29 #include "src/core/CL/CLValidate.h"
31 #include "support/Cast.h"
32 
33 namespace arm_compute
34 {
35 namespace opencl
36 {
37 namespace kernels
38 {
39 namespace
40 {
41 Status validate_arguments(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const Conv3dInfo &conv3d_info)
42 {
44  ARM_COMPUTE_RETURN_ERROR_ON_MSG(src0->data_layout() != DataLayout::NDHWC, "Only NDHWC layout supported");
45  ARM_COMPUTE_RETURN_ERROR_ON_MSG(conv3d_info.act_info.enabled(), "Fused activation not supported");
46 
50  ARM_COMPUTE_RETURN_ERROR_ON(conv3d_info.dilation != Size3D(1U, 1U, 1U));
51 
52  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");
54 
55  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));
58 
59  if(src2 != nullptr)
60  {
61  if(is_data_type_quantized(src0->data_type()))
62  {
64  }
65  else
66  {
68  }
69  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");
71  }
72 
73  // Checks performed when dst is configured
74  if(dst->total_size() != 0)
75  {
76  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));
79  }
80 
81  return Status{};
82 }
83 } // namespace
84 
86 {
87  _type = CLKernelType::DIRECT;
88 }
89 
90 void ClDirectConv3dKernel::configure(const CLCompileContext &compile_context, const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *src2, ITensorInfo *dst,
91  const Conv3dInfo &conv3d_info)
92 {
93  ARM_COMPUTE_ERROR_ON_NULLPTR(src0, src1, dst);
94 
95  // Perform validation
96  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src0, src1, src2, dst, conv3d_info));
97 
98  // Create window and update padding
99  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);
104  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);
108  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);
111  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;
120  const size_t k0 = adjust_vec_size(8u, src0->dimension(0));
121  const size_t partial_store_n0 = dst->dimension(0) % n0;
122 
124  build_options.add_option("-cl-fast-relaxed-math");
125  build_options.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
126  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));
147 
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  }
185 
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 
217 Status ClDirectConv3dKernel::validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const Conv3dInfo &conv3d_info)
218 {
219  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src0, src1, src2, dst, conv3d_info));
220  return Status{};
221 }
222 
223 void ClDirectConv3dKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
224 {
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
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
bool is_data_type_quantized(DataType dt)
Check if a given data type is of quantized type.
Definition: Utils.h:1030
Class describing the value of a pixel for any image format.
Definition: PixelValue.h:34
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
#define ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(tensor)
Definition: CLValidate.h:35
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
Descriptor used by the 3d Convolution function.
void run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
void enqueue(cl::CommandQueue &queue, ICLKernel &kernel, const Window &window, const cl::NDRange &lws_hint=CLKernelLibrary::get().default_ndrange(), bool use_dummy_work_items=false)
Add the kernel to the command queue with the given window.
Definition: ICLKernel.cpp:32
const StringSet & options() const
Gets the current options list set.
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:384
void get(uint8_t &v) const
Interpret the pixel value as a U8.
Definition: PixelValue.h:244
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
std::string to_string(T &&value)
Convert integer and float values to string.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
Definition: Validate.h:488
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Quantization info when assuming per layer quantization.
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:79
Status calculate_quantized_multiplier(float multiplier, int32_t *quant_multiplier, int32_t *shift, bool ignore_epsilon=false)
Calculate quantized representation of multiplier.
Status class.
Definition: Error.h:52
std::string lower_string(const std::string &val)
Lower a given string.
Definition: Utils.cpp:351
Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const PadStrideInfo &conv_info)
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
std::set< std::string > build_options
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:284
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2022 Arm Limited.
1 channel, 1 F16 per channel
1 channel, 1 S32 per channel
void add_option(std::string option)
Adds option to the existing build option list.
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:54
const size_t conv_stride_y
Definition: impl.cpp:58
cl::Kernel create_kernel(const CLCompileContext &ctx, const std::string &kernel_name, const std::set< std::string > &build_opts=std::set< std::string >())
Creates an opencl kernel using a compile context.
Definition: CLHelpers.cpp:391
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
Definition: Utils.cpp:135
size_t front
Padding across the depth dimenstion on the front, in elements.
Definition: Types.h:806
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
auto ceil_to_multiple(S value, T divisor) -> decltype(((value+divisor - 1)/divisor) *divisor)
Computes the smallest number larger or equal to value that is a multiple of divisor.
Definition: Utils.h:71
quantized, asymmetric fixed-point 8-bit number unsigned
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
UniformQuantizationInfo uniform() const
Return per layer quantization info.
std::string get_cl_type_from_data_type(const DataType &dt)
Translates a tensor data type to the appropriate OpenCL type.
Definition: CLHelpers.cpp:39
size_t top
Padding across the height dimenstion on the top, in elements.
Definition: Types.h:804
size_t left
Padding across the width dimenstion on the left, in elements.
Definition: Types.h:802
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
static Status validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst, const Conv3dInfo &conv3d_info)
Static function to check if given info will lead to a valid configuration.
Num samples, depth, height, width, channels.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
CLCompileContext class.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
Definition: ITensorPack.cpp:64
TensorShape compute_conv3d_shape(const TensorShape &src, const TensorShape &weights, const Conv3dInfo &conv3d_info)
Calculate the output shape of 3d Convolution.
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:541
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:788
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244
Tensor packing service.
Definition: ITensorPack.h:39
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
const size_t weights_width
Definition: impl.cpp:53
const size_t weights_height
Definition: impl.cpp:54
size_t z() const
Semantic accessor for depth as z.
Definition: Size3D.h:76
unsigned int adjust_vec_size(unsigned int vec_size, size_t dim0)
Returns the adjusted vector size in case it is less than the input&#39;s first dimension, getting rounded down to its closest valid vector size.
Definition: Utils.h:1222
quantized, asymmetric fixed-point 8-bit number signed
void configure(const CLCompileContext &compile_context, const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *src2, ITensorInfo *dst, const Conv3dInfo &conv3d_info)
Set the src, weights, biases and dst tensors info.
void add_1D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 1D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: ICLKernel.h:179
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:304
std::string kernel_name
DataType
Available data types.
Definition: Types.h:79
size_t x() const
Semantic accessor for width as x.
Definition: Size3D.h:58
void add_4D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 4D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: ICLKernel.h:237
const size_t conv_stride_x
Definition: impl.cpp:57
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)
size_t y() const
Semantic accessor for height as y.
Definition: Size3D.h:67