Compute Library
 21.02
GCDepthwiseConvolutionLayer3x3Kernel Class Reference

Interface for the kernel to run a 3x3 depthwise convolution on a tensor. More...

#include <GCDepthwiseConvolutionLayer3x3Kernel.h>

Collaboration diagram for GCDepthwiseConvolutionLayer3x3Kernel:
[legend]

Public Member Functions

 GCDepthwiseConvolutionLayer3x3Kernel ()
 Default constructor. More...
 
 GCDepthwiseConvolutionLayer3x3Kernel (const GCDepthwiseConvolutionLayer3x3Kernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
GCDepthwiseConvolutionLayer3x3Kerneloperator= (const GCDepthwiseConvolutionLayer3x3Kernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 GCDepthwiseConvolutionLayer3x3Kernel (GCDepthwiseConvolutionLayer3x3Kernel &&)=default
 Default Move Constructor. More...
 
GCDepthwiseConvolutionLayer3x3Kerneloperator= (GCDepthwiseConvolutionLayer3x3Kernel &&)=default
 Default move assignment operator. More...
 
void configure (const IGCTensor *input, const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output, const PadStrideInfo &conv_info, unsigned int depth_multiplier=1)
 Initialize the function's source, destination, conv and border_size. More...
 
void run (const Window &window) override
 Enqueue the OpenGL ES shader to process the given window. More...
 
BorderSize border_size () const override
 The size of the border for that kernel. More...
 
- Public Member Functions inherited from IGCKernel
 IGCKernel ()
 Constructor. More...
 
GCKernelkernel ()
 Returns a reference to the GLES kernel of this object. More...
 
void add_1D_tensor_argument (unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
 Add the passed 1D tensor's parameters to the object's kernel's arguments starting from the index idx. More...
 
void add_2D_tensor_argument (unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
 Add the passed 2D tensor's parameters to the object's kernel's arguments starting from the index idx. More...
 
void add_3D_tensor_argument (unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
 Add the passed 3D tensor's parameters to the object's kernel's arguments starting from the index idx. More...
 
unsigned int num_arguments_per_1D_tensor () const
 Returns the number of arguments enqueued per 1D tensor object. More...
 
unsigned int num_arguments_per_2D_tensor () const
 Returns the number of arguments enqueued per 2D tensor object. More...
 
unsigned int num_arguments_per_3D_tensor () const
 Returns the number of arguments enqueued per 3D tensor object. More...
 
void set_lws_hint (gles::NDRange &lws_hint)
 Set the Local-Workgroup-Size hint. More...
 
void set_target (GPUTarget target)
 Set the targeted GPU architecture. More...
 
GPUTarget get_target () const
 Get the targeted GPU architecture. More...
 
- Public Member Functions inherited from IKernel
 IKernel ()
 Constructor. More...
 
virtual ~IKernel ()=default
 Destructor. More...
 
virtual bool is_parallelisable () const
 Indicates whether or not the kernel is parallelisable. More...
 
const Windowwindow () const
 The maximum window the kernel can be executed on. More...
 

Detailed Description

Interface for the kernel to run a 3x3 depthwise convolution on a tensor.

Definition at line 35 of file GCDepthwiseConvolutionLayer3x3Kernel.h.

Constructor & Destructor Documentation

◆ GCDepthwiseConvolutionLayer3x3Kernel() [1/3]

Default constructor.

Definition at line 44 of file GCDepthwiseConvolutionLayer3x3Kernel.cpp.

45  : _border_size(0), _input(), _output(), _weights(), _biases(), _conv_stride_x(0), _conv_stride_y(0), _conv_pad_left(0), _conv_pad_top(0), _lws(gles::NDRange(1U, 1U, 1U))
46 {
47 }
Class interface for specifying NDRange values.
Definition: OpenGLES.h:53

◆ GCDepthwiseConvolutionLayer3x3Kernel() [2/3]

Prevent instances of this class from being copied (As this class contains pointers)

◆ GCDepthwiseConvolutionLayer3x3Kernel() [3/3]

Member Function Documentation

◆ border_size()

BorderSize border_size ( ) const
overridevirtual

The size of the border for that kernel.

Returns
The width in number of elements of the border.

Reimplemented from IKernel.

Definition at line 49 of file GCDepthwiseConvolutionLayer3x3Kernel.cpp.

50 {
51  return _border_size;
52 }

◆ configure()

void configure ( const IGCTensor input,
const IGCTensor weights,
const IGCTensor biases,
IGCTensor output,
const PadStrideInfo conv_info,
unsigned int  depth_multiplier = 1 
)

Initialize the function's source, destination, conv and border_size.

Parameters
[in]inputSource tensor. DataType supported: F16.
[in]weightsWeights tensor. A 3D tensor with dimensions [3, 3, IFM]. Data type supported: Same as input.
[in]biases(Optional) Biases tensor. A 1D tensor with dimensions [IFM]. Must be nullptr if not needed. Data type supported: Same as input.
[out]outputDestination tensor. Data type supported: Same as input.
[in]conv_infoPadding and stride information to use for the convolution.
[in]depth_multiplier(Optional) Multiplier to apply to the input's depth in order to retrieve the output's depth. Defaults to 1.

Definition at line 54 of file GCDepthwiseConvolutionLayer3x3Kernel.cpp.

References ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN, ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES, ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS, arm_compute::auto_init_if_empty(), BorderSize::bottom, arm_compute::calculate_max_enlarged_window(), arm_compute::ceil_to_multiple(), arm_compute::misc::shape_calculator::compute_depthwise_convolution_shape(), arm_compute::test::validation::conv_info, GCKernelLibrary::create_kernel(), ITensorInfo::data_type(), ITensorInfo::dimension(), arm_compute::F16, arm_compute::F32, GCKernelLibrary::get(), ITensor::info(), arm_compute::test::validation::input, input_height, input_width, kernel_name, BorderSize::left, ITensorInfo::num_dimensions(), arm_compute::test::validation::output_shape, PadStrideInfo::pad_bottom(), PadStrideInfo::pad_left(), PadStrideInfo::pad_right(), PadStrideInfo::pad_top(), ITensorInfo::padding(), BorderSize::right, PadStrideInfo::stride(), ITensorInfo::tensor_shape(), arm_compute::support::cpp11::to_string(), BorderSize::top, and arm_compute::update_window_and_padding().

56 {
59  ARM_COMPUTE_ERROR_ON(weights->info()->dimension(0) != 3 || weights->info()->dimension(1) != 3);
60 
61  if(biases != nullptr)
62  {
64  ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(2));
65  ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
66  }
67 
68  // Get convolved dimensions
69  const TensorShape output_shape = compute_depthwise_convolution_shape(*input->info(), *weights->info(), conv_info, depth_multiplier);
70 
71  // Output auto inizialitation if not yet initialized
72  auto_init_if_empty(*output->info(),
74  1,
75  input->info()->data_type());
76 
78  ARM_COMPUTE_ERROR_ON(output->info()->dimension(2) != weights->info()->dimension(2));
79 
80  _input = input;
81  _output = output;
82  _weights = weights;
83  _biases = biases;
84  _conv_stride_x = conv_info.stride().first;
85  _conv_stride_y = conv_info.stride().second;
86  _conv_pad_left = conv_info.pad_left();
87  _conv_pad_top = conv_info.pad_top();
88  _border_size = BorderSize(_conv_pad_top, conv_info.pad_right(), conv_info.pad_bottom(), _conv_pad_left);
89 
90  // Set build options
91  ARM_COMPUTE_ERROR_ON(_conv_stride_x < 1 || _conv_stride_x > 3);
92  std::set<std::string> options;
93 
94  options.emplace("#define DEPTH_MULTIPLIER " + support::cpp11::to_string(depth_multiplier));
95  options.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(_lws[0]));
96  options.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(_lws[1]));
97  options.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(_lws[2]));
98  options.emplace("#define STRIDE_X " + support::cpp11::to_string(_conv_stride_x));
99  options.emplace("#define STRIDE_Y " + support::cpp11::to_string(_conv_stride_y));
100 
101  std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
102  options.emplace(("#define " + dt_name));
103 
104  unsigned int num_elems_read_per_iteration_x = 8;
105  unsigned int num_elems_read_per_iteration_y = 1;
106  unsigned int num_elems_written_per_iteration_x = 4;
107  unsigned int num_elems_written_per_iteration_y = 1;
108  unsigned int num_elems_written_per_iteration_z = 1;
109 
110  if((_conv_stride_x == 1) && (_conv_stride_y == 1))
111  {
112  switch(input->info()->data_type())
113  {
114 #define PROCESS_4X_3Y_1Z
115 
116  case DataType::F16:
117 #if defined(PROCESS_4X_3Y_1Z)
118  options.emplace("#define PROCESS_4X_3Y_1Z");
119  num_elems_read_per_iteration_y = 5;
120  num_elems_written_per_iteration_y = 3;
121 #endif /* PROCESS_4X_3Y_1Z */
122 #undef PROCESS_4X_3Y_1Z
123  break;
124 
125  default:
126  ARM_COMPUTE_ERROR("Current data type is not supported");
127  break;
128  }
129  }
130  else
131  {
132  switch(input->info()->data_type())
133  {
134  case DataType::F16:
135  options.emplace("#define PROCESS_4X_1Y_1Z");
136  break;
137 
138  default:
139  ARM_COMPUTE_ERROR("Current data type is not supported");
140  break;
141  }
142  }
143 
144  if(_biases != nullptr)
145  {
146  options.emplace("#define BIAS");
147  }
148 
149  // Create kernel
150  std::string kernel_name = "depthwise_convolution_3x3";
151  _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel(kernel_name, options));
152 
153  // Calculate output right and bottom border
154  const int output_width = output->info()->dimension(0);
155  const int output_height = output->info()->dimension(1);
156  const int output_padding_right = ceil_to_multiple(output_width, num_elems_written_per_iteration_x * _lws[0]) - output_width;
157  const int output_padding_bottom = ceil_to_multiple(output_height, num_elems_written_per_iteration_y * _lws[1]) - output_height;
158 
159  // Calculate input right and bottom border
160  const int input_width = input->info()->dimension(0);
161  const int input_height = input->info()->dimension(1);
162 
163  const int input_total_width = std::max(int(input->info()->padding().left), int(_conv_pad_left)) + input_width + std::max(int(input->info()->padding().right), int(_conv_pad_left));
164  const int input_total_height = std::max(int(input->info()->padding().top), int(_conv_pad_top)) + input_height + std::max(int(input->info()->padding().bottom), int(_conv_pad_top));
165 
166  const int input_padding_right = ceil_to_multiple(input_total_width, num_elems_read_per_iteration_x * _lws[0]) - input_width - _conv_pad_left;
167  const int input_padding_bottom = ceil_to_multiple(input_total_height, num_elems_read_per_iteration_y * _lws[1]) - input_height - _conv_pad_top;
168 
169  BorderSize border = BorderSize(0, output_padding_right, output_padding_bottom, 0);
170 
171  Window win = calculate_max_enlarged_window(*output->info(), Steps(num_elems_written_per_iteration_x, num_elems_written_per_iteration_y, num_elems_written_per_iteration_z), border);
172 
173  AccessWindowStatic input_access(input->info(), -_conv_pad_left, -_conv_pad_top, input_width + input_padding_right, input_height + input_padding_bottom);
174  AccessWindowStatic weights_access = AccessWindowStatic(nullptr, 0, 0, 0, 0);
175  AccessWindowStatic bias_access = AccessWindowStatic(nullptr, 0, 0, 0, 1);
176 
177  switch(weights->info()->data_type())
178  {
179  case DataType::F16:
180  weights_access = AccessWindowStatic(weights->info(), 0, 0, 4, 3);
181  if(_biases != nullptr)
182  {
183  bias_access = AccessWindowStatic(_biases->info(), 0, 0, _biases->info()->dimension(0) + 1, 1);
184  }
185  break;
186 
187  default:
188  ARM_COMPUTE_ERROR("Current data type is not supported");
189  break;
190  }
191 
192  AccessWindowStatic output_access(output->info(), 0, 0, output_width + output_padding_right, output_height + output_padding_bottom);
193 
194  if(_biases != nullptr)
195  {
196  update_window_and_padding(win, input_access, weights_access, bias_access, output_access);
197  }
198  else
199  {
200  update_window_and_padding(win, input_access, weights_access, output_access);
201  }
202 
203  output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
204 
205  IGCKernel::configure(win);
206 }
unsigned int top
top of the border
Definition: Types.h:375
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
Shape of a tensor.
Definition: TensorShape.h:39
TensorShape compute_depthwise_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, PadStrideInfo conv_info, unsigned int depth_multiplier, const Size2D &dilation=Size2D(1U, 1U))
Calculate the depthwise convolution output shape of a tensor.
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
Container for 2D border size.
Definition: Types.h:273
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
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(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
unsigned int bottom
bottom of the border
Definition: Types.h:377
GCKernel class.
unsigned int pad_top() const
Get the top padding.
Definition: Types.h:806
1 channel, 1 F16 per channel
Implementation of a static rectangular access pattern.
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: WindowHelpers.h:46
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:543
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
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
std::pair< unsigned int, unsigned int > stride() const
Get the stride.
Definition: Types.h:770
std::string kernel_name
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
unsigned int pad_right() const
Get the right padding.
Definition: Types.h:801
virtual PaddingSize padding() const =0
Padding of tensor.
unsigned int left
left of the border
Definition: Types.h:378
unsigned int right
right of the border
Definition: Types.h:376
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
static GCKernelLibrary & get()
Get the static instance of GCKernelLibrary.
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:286
Window calculate_max_enlarged_window(const ValidRegion &valid_region, const Steps &steps, BorderSize border_size)
GCKernel create_kernel(const std::string &shader_name, const StringSet &build_options_set={}) const
Creates a kernel from the kernel library.
Container for valid region of a window.
Definition: Types.h:188
unsigned int pad_bottom() const
Get the bottom padding.
Definition: Types.h:811
unsigned int pad_left() const
Get the left padding.
Definition: Types.h:796
Describe a multidimensional execution window.
Definition: Window.h:39

◆ operator=() [1/2]

Prevent instances of this class from being copied (As this class contains pointers)

◆ operator=() [2/2]

Default move assignment operator.

◆ run()

void run ( const Window window)
overridevirtual

Enqueue the OpenGL ES shader to process the given window.

Parameters
[in]windowRegion on which to execute the kernel. (Must be a valid region of the window returned by window()).

Implements IGCKernel.

Definition at line 208 of file GCDepthwiseConvolutionLayer3x3Kernel.cpp.

References IGCKernel::add_1D_tensor_argument(), IGCKernel::add_3D_tensor_argument(), Window::adjust(), ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, Window::DimX, Window::DimY, arm_compute::enqueue(), Window::first_slice_window_3D(), ITensor::info(), IGCKernel::num_arguments_per_3D_tensor(), ITensorInfo::padding(), Window::set_dimension_step(), IGCTensor::set_needs_shifting(), Window::shift(), Window::slide_window_slice_3D(), Window::Dimension::step(), ITensorInfo::tensor_shape(), Window::use_tensor_dimensions(), IKernel::window(), Window::x(), and Window::y().

209 {
212 
213  _kernel.use();
214 
215  _output->set_needs_shifting(true);
216 
217  // Create input window and adjust
218  Window win_in = window;
219  win_in.adjust(Window::DimX, -_conv_pad_left, true);
220  win_in.adjust(Window::DimY, -_conv_pad_top, true);
221  win_in.set_dimension_step(Window::DimX, window.x().step() * _conv_stride_x);
222  win_in.set_dimension_step(Window::DimY, window.y().step() * _conv_stride_y);
223 
224  Window slice_in = win_in.first_slice_window_3D();
225  Window slice_out = window.first_slice_window_3D();
226  Window slice_weights = window.first_slice_window_3D();
227  slice_weights.set_dimension_step(Window::DimX, 0);
228  slice_weights.set_dimension_step(Window::DimY, 0);
229 
230  // Set biases
231  if(_biases != nullptr)
232  {
233  unsigned int idx = 3 * num_arguments_per_3D_tensor();
234  Window slice_biases;
235  slice_biases.use_tensor_dimensions(_biases->info()->tensor_shape());
236  add_1D_tensor_argument(idx, _biases, 4, slice_biases);
237  }
238 
239  slice_out.shift(Window::DimX, -(_output->info()->padding()).left);
240 
241  do
242  {
243  unsigned int idx = 0;
244  add_3D_tensor_argument(idx, _input, 1, slice_in);
245  add_3D_tensor_argument(idx, _output, 2, slice_out);
246  add_3D_tensor_argument(idx, _weights, 3, slice_weights);
247 
248  _kernel.update_shader_params();
249  enqueue(*this, slice_out, _lws);
250  }
251  while(window.slide_window_slice_3D(slice_out) && win_in.slide_window_slice_3D(slice_in));
252 }
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
void add_3D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
Add the passed 3D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: IGCKernel.cpp:132
void enqueue(IGCKernel &kernel, const Window &window, const gles::NDRange &lws=gles::NDRange(1U, 1U, 1U))
Add the kernel to the command queue with the given window.
Definition: IGCKernel.cpp:41
void shift(size_t dimension, int shift_value)
Shift the values of a given dimension by the given shift_value.
Definition: Window.inl:133
constexpr int step() const
Return the step of the dimension.
Definition: Window.h:104
unsigned int num_arguments_per_3D_tensor() const
Returns the number of arguments enqueued per 3D tensor object.
Definition: IGCKernel.cpp:147
void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension=Window::DimX)
Use the tensor&#39;s dimensions to fill the window dimensions.
Definition: Window.inl:276
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
void set_needs_shifting(bool needs_shifting)
Set the flag indicating whether or not a tensor needs shifting.
Definition: IGCTensor.cpp:61
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
virtual PaddingSize padding() const =0
Padding of tensor.
bool slide_window_slice_3D(Window &slice) const
Slide the passed 3D window slice.
Definition: Window.h:335
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
void set_dimension_step(size_t dimension, int step)
Set the step of a given dimension.
Definition: Window.inl:167
constexpr const Dimension & y() const
Alias to access the second dimension of the window.
Definition: Window.h:154
void adjust(size_t dimension, int adjust_value, bool is_at_start)
Adjust the start or end of a given dimension by the given value.
Definition: Window.inl:140
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:291
Describe a multidimensional execution window.
Definition: Window.h:39
void add_1D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, 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: IGCKernel.cpp:122
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:145

The documentation for this class was generated from the following files: