Compute Library
 19.08
GCIm2ColKernel Class Reference

Interface for the im2col reshape kernel. More...

#include <GCIm2ColKernel.h>

Collaboration diagram for GCIm2ColKernel:
[legend]

Public Member Functions

 GCIm2ColKernel ()
 Default constructor. More...
 
 GCIm2ColKernel (const GCIm2ColKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
GCIm2ColKerneloperator= (const GCIm2ColKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 GCIm2ColKernel (GCIm2ColKernel &&)=default
 Allow instances of this class to be moved. More...
 
GCIm2ColKerneloperator= (GCIm2ColKernel &&)=default
 Allow instances of this class to be moved. More...
 
void configure (const IGCTensor *input, IGCTensor *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation=Size2D(1U, 1U))
 Set the input and output of the kernel. More...
 
void run (const Window &window) override
 Enqueue the OpenGL ES shader to process the given window. 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...
 
virtual BorderSize border_size () const
 The size of the border for that kernel. More...
 
const Windowwindow () const
 The maximum window the kernel can be executed on. More...
 

Static Public Member Functions

static Status validate (const ITensorInfo *input, const ITensorInfo *output, const Size2D &kernel_dims, const PadStrideInfo &conv_info, bool has_bias, const Size2D &dilation=Size2D(1U, 1U))
 Static function to check if given info will lead to a valid configuration of CLIm2ColKernel. More...
 

Detailed Description

Interface for the im2col reshape kernel.

Rearranges image blocks into columns. It is used to strip out each convolution block to a single column. It is used to transform a convolution to a plain matrix multiplication.

For example taking into account the image below and assuming 3x3 image blocks with stride of 1 we have:

\[ \left( \begin{array}{cccc} a00 & a01 & a02 & a03 \\ a10 & a11 & a12 & a13 \\ a20 & a21 & a22 & a23 \\ a30 & a31 & a32 & a33 \\ \end{array} \right) = \left( \begin{array}{ccccccccc} a00 & a01 & a02 & a10 & a11 & a12 & a20 & a21 & a22 \\ a01 & a02 & a03 & a11 & a12 & a13 & a21 & a22 & a23 \\ a10 & a11 & a12 & a20 & a21 & a22 & a30 & a31 & a32 \\ a11 & a12 & a13 & a21 & a22 & a23 & a31 & a32 & a33 \\ \end{array} \right) \]

Definition at line 57 of file GCIm2ColKernel.h.

Constructor & Destructor Documentation

◆ GCIm2ColKernel() [1/3]

Default constructor.

Definition at line 62 of file GCIm2ColKernel.cpp.

63  : _input(nullptr), _output(nullptr), _convolved_dims(), _kernel_dims(), _num_elems_processed_per_iteration(1), _run_func(nullptr)
64 {
65 }

◆ GCIm2ColKernel() [2/3]

GCIm2ColKernel ( const GCIm2ColKernel )
delete

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

◆ GCIm2ColKernel() [3/3]

GCIm2ColKernel ( GCIm2ColKernel &&  )
default

Allow instances of this class to be moved.

Member Function Documentation

◆ configure()

void configure ( const IGCTensor input,
IGCTensor output,
const Size2D kernel_dims,
const PadStrideInfo conv_info,
bool  has_bias,
const Size2D dilation = Size2D(1U, 1U) 
)

Set the input and output of the kernel.

Parameters
[in]inputThe input tensor to convert. 3 lower dimensions represent a single input [width, height, IFM], while every optional dimension from 4 and above represent a batch of inputs. Data types supported: F16/F32
[out]outputThe output tensor. First 2 lower dimensions represent a transform of each 3D input, while every dimension above represents a batch. Data types supported: Same as input
[in]kernel_dimsThe kernel dimensions (width and height).
[in]conv_infoContains padding and stride information described in PadStrideInfo.
[in]has_biasIn case biases are provided expands the matrix with 1.
[in]dilation(Optional) Dilation, in elements, across x and y. Defaults to (1, 1).

Definition at line 67 of file GCIm2ColKernel.cpp.

68 {
69  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
70 
71  // Perform validation step
72  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
73 
74  _input = input;
75  _output = output;
76 
77  // Create kernel
78  std::set<std::string> build_opts;
79  std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
80  build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
81  build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
82  build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
83  build_opts.insert("#define " + dt_name);
84 
85  if(has_bias)
86  {
87  build_opts.emplace("#define HAS_BIAS");
88  }
89 
90  int stride_x = 0;
91  int stride_y = 0;
92 
93  std::tie(stride_x, stride_y) = conv_info.stride();
94  _kernel_dims = std::make_pair(kernel_dims.width, kernel_dims.height);
95 
96  const bool run_img2col_reduced = (output->info()->dimension(0) == (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2))) && (TensorShape::num_max_dimensions >= 4)
97  && (std::equal(input->info()->tensor_shape().cbegin() + 3,
98  input->info()->tensor_shape().cend(),
99  output->info()->tensor_shape().cbegin() + 1))
100  && ((stride_x == 1) && (stride_y == 1) && !conv_info.has_padding())
101  && (dilation == Size2D(1U, 1U));
102 
103  std::string kernel_name = "im2col_generic";
104  if(!run_img2col_reduced)
105  {
106  if(input->info()->data_type() == DataType::F16 && _kernel_dims == std::pair<unsigned int, unsigned int>(1, 1))
107  {
108  build_opts.emplace("#define KERNEL_1x1");
109  }
110 
111  build_opts.emplace("#define IM2COL_GENERIC");
112  _convolved_dims = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1),
113  kernel_dims.width, kernel_dims.height,
115  _num_elems_processed_per_iteration = (input->info()->data_type() == DataType::F32) ? 1 : 2;
116 
117  build_opts.emplace("#define KERNEL_WIDTH " + support::cpp11::to_string(kernel_dims.width));
118  build_opts.emplace("#define KERNEL_HEIGHT " + support::cpp11::to_string(kernel_dims.height));
119  build_opts.emplace("#define KERNEL_DEPTH " + support::cpp11::to_string(input->info()->dimension(2)));
120  build_opts.emplace("#define CONVOLVED_WIDTH " + support::cpp11::to_string(_convolved_dims.first));
121  build_opts.emplace("#define CONVOLVED_HEIGHT " + support::cpp11::to_string(_convolved_dims.second));
122  build_opts.emplace("#define STRIDE_X " + support::cpp11::to_string(conv_info.stride().first));
123  build_opts.emplace("#define STRIDE_Y " + support::cpp11::to_string(conv_info.stride().second));
124  build_opts.emplace("#define PAD_LEFT " + support::cpp11::to_string(conv_info.pad_left()));
125  build_opts.emplace("#define PAD_TOP " + support::cpp11::to_string(conv_info.pad_top()));
126  build_opts.emplace("#define PAD_RIGHT " + support::cpp11::to_string(conv_info.pad_right()));
127  build_opts.emplace("#define PAD_BOTTOM " + support::cpp11::to_string(conv_info.pad_bottom()));
128  build_opts.emplace("#define SRC_WIDTH " + support::cpp11::to_string(input->info()->dimension(0)));
129  build_opts.emplace("#define SRC_HEIGHT " + support::cpp11::to_string(input->info()->dimension(1)));
130  build_opts.emplace("#define DILATION_X " + support::cpp11::to_string(dilation.x()));
131  build_opts.emplace("#define DILATION_Y " + support::cpp11::to_string(dilation.y()));
132 
133  _run_func = &GCIm2ColKernel::run_generic;
134  }
135  else
136  {
137  build_opts.emplace("#define IM2COL_REDUCED");
138  kernel_name = "im2col_reduced";
139 
140  if(input->info()->data_type() == DataType::F32)
141  {
142  _num_elems_processed_per_iteration = 4 / input->info()->element_size();
143  }
144  else if(input->info()->data_type() == DataType::F16)
145  {
146  int input_width = input->info()->dimension(0);
147  int input_height = input->info()->dimension(1);
148 
149  build_opts.emplace("#define IMAGE_SIZE " + support::cpp11::to_string(input_width * input_height));
150  if(input_width % 8 == 0)
151  {
152  _num_elems_processed_per_iteration = 8;
153  build_opts.emplace("#define IM2COL_REDUCED_8X");
154  }
155  else if(input_width % 4 == 0)
156  {
157  _num_elems_processed_per_iteration = 4;
158  build_opts.emplace("#define IM2COL_REDUCED_4X");
159  }
160  else if(input_width % 2 == 0)
161  {
162  _num_elems_processed_per_iteration = 2;
163  build_opts.emplace("#define IM2COL_REDUCED_2X");
164  }
165  else
166  {
167  _num_elems_processed_per_iteration = 2;
168  build_opts.emplace("#define IM2COL_REDUCED_GENERIC");
169  }
170  }
171 
172  _run_func = &GCIm2ColKernel::run_reduced;
173  }
174 
175  // Create kernel
176  _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel(kernel_name, build_opts));
177 
178  // Configure kernel window
179  Window win = calculate_max_window(*input->info(), Steps(_num_elems_processed_per_iteration));
180 
181  if(input->info()->data_type() == DataType::F16)
182  {
183  // Calculate input right and bottom border
184  const int input_width = input->info()->dimension(0);
185  const int input_height = input->info()->dimension(1);
186  int input_total_width = input->info()->padding().left + input_width + input->info()->padding().right;
187  int input_padding_right = ceil_to_multiple(input_total_width, _num_elems_processed_per_iteration) - input_total_width;
188  input_total_width = input_width + input_padding_right + input->info()->padding().right;
189  AccessWindowStatic input_access(input->info(), 0, 0, input_total_width, input_height);
190 
191  // Calculate output right and bottom border
192  const int output_width = output->info()->dimension(0);
193  const int output_height = output->info()->dimension(1);
194  const int output_padding_right = ceil_to_multiple(output_width, _num_elems_processed_per_iteration) - output_width;
195  AccessWindowStatic output_access(output->info(), 0, 0, output_width + output_padding_right, output_height);
196 
197  update_window_and_padding(win, input_access, output_access);
198  }
199 
200  output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
201 
202  if(!run_img2col_reduced)
203  {
204  // set the Z dimension's step same size as the whole dimension so that one can't split across the Z dimension
205  win.set_dimension_step(Window::DimZ, win[Window::DimZ].end() - win[Window::DimZ].start());
206  }
207 
208  IGCKernel::configure(win);
209 }
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
std::pair< unsigned int, unsigned int > scaled_dimensions(unsigned int width, unsigned int height, unsigned int kernel_width, unsigned int kernel_height, const PadStrideInfo &pad_stride_info, const Size2D &dilation=Size2D(1U, 1U))
Returns expected width and height of output scaled tensor depending on dimensions rounding mode.
Definition: Utils.cpp:387
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_THROW_ON(status)
Definition: Error.h:327
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps=Steps(), bool skip_border=false, BorderSize border_size=BorderSize())
Calculate the maximum window for a given tensor shape and border setting.
Definition: Helpers.cpp:28
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
size_t height
Height of the image region or rectangle.
Definition: Size2D.h:93
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: Helpers.h:402
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:66
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
virtual PaddingSize padding() const =0
Padding of tensor.
std::unique_ptr< Kernel > create_kernel()
Helper function to create and return a unique_ptr pointed to a CL/GLES kernel object.
Definition: Helpers.h:86
unsigned int left
left of the border
Definition: Types.h:342
unsigned int right
right of the border
Definition: Types.h:340
std::array< T, num_max_dimensions >::const_iterator cend() const
Returns a read-only (constant) iterator that points one past the last element in the dimension array.
Definition: Dimensions.h:234
static GCKernelLibrary & get()
Get the static instance of GCKernelLibrary.
void set_dimension_step(size_t dimension, int step)
Set the step of a given dimension.
Definition: Window.inl:153
std::array< T, num_max_dimensions >::const_iterator cbegin() const
Returns a read-only (constant) iterator that points to the first element in the dimension array.
Definition: Dimensions.h:210
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
size_t width
Width of the image region or rectangle.
Definition: Size2D.h:92
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
Container for valid region of a window.
Definition: Types.h:174
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:45
Describe a multidimensional execution window.
Definition: Window.h:39

References ARM_COMPUTE_ERROR_ON_NULLPTR, ARM_COMPUTE_ERROR_THROW_ON, arm_compute::calculate_max_window(), Dimensions< T >::cbegin(), arm_compute::ceil_to_multiple(), Dimensions< T >::cend(), arm_compute::test::validation::conv_info, arm_compute::create_kernel(), ITensorInfo::data_type(), arm_compute::test::validation::dilation, ITensorInfo::dimension(), Window::DimZ, ITensorInfo::element_size(), arm_compute::F16, arm_compute::F32, GCKernelLibrary::get(), arm_compute::test::validation::has_bias, Size2D::height, ITensor::info(), BorderSize::left, Dimensions< size_t >::num_max_dimensions, ITensorInfo::padding(), BorderSize::right, arm_compute::scaled_dimensions(), Window::set_dimension_step(), ITensorInfo::set_valid_region(), ITensorInfo::tensor_shape(), arm_compute::support::cpp11::to_string(), arm_compute::U, arm_compute::update_window_and_padding(), and Size2D::width.

Referenced by GCConvolutionLayer::configure().

◆ operator=() [1/2]

GCIm2ColKernel& operator= ( const GCIm2ColKernel )
delete

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

◆ operator=() [2/2]

GCIm2ColKernel& operator= ( GCIm2ColKernel &&  )
default

Allow instances of this class to be moved.

◆ 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 221 of file GCIm2ColKernel.cpp.

222 {
223  ARM_COMPUTE_ERROR_ON(_run_func == nullptr);
224  (this->*_run_func)(window);
225 }
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:337

References ARM_COMPUTE_ERROR_ON, and IKernel::window().

◆ validate()

Status validate ( const ITensorInfo input,
const ITensorInfo output,
const Size2D kernel_dims,
const PadStrideInfo conv_info,
bool  has_bias,
const Size2D dilation = Size2D(1U, 1U) 
)
static

Static function to check if given info will lead to a valid configuration of CLIm2ColKernel.

Parameters
[in]inputThe input tensor to convert. 3 lower dimensions represent a single input [width, height, IFM], while every optional dimension from 4 and above represent a batch of inputs. Data types supported: F16/F32
[in]outputThe output tensor. First 2 lower dimensions represent a transform of each 3D input, while every dimension above represents a batch. Data types supported: Same as input
[in]kernel_dimsThe kernel dimensions (width and height).
[in]conv_infoContains padding and stride information described in PadStrideInfo.
[in]has_biasIn case biases are provided expands the matrix with 1.
[in]dilation(Optional) Dilation, in elements, across x and y. Defaults to (1, 1).
Returns
a status

Definition at line 211 of file GCIm2ColKernel.cpp.

212 {
213  ARM_COMPUTE_UNUSED(kernel_dims);
217  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
218  return Status{};
219 }
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:193
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:160

References ARM_COMPUTE_RETURN_ON_ERROR, ARM_COMPUTE_UNUSED, arm_compute::test::validation::conv_info, arm_compute::test::validation::dilation, and arm_compute::test::validation::has_bias.


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