Compute Library
 19.08
GCWeightsReshapeKernel Class Reference

GLES Compute kernel to perform reshaping on the weights used by convolution and locally connected layer. More...

#include <GCWeightsReshapeKernel.h>

Collaboration diagram for GCWeightsReshapeKernel:
[legend]

Public Member Functions

 GCWeightsReshapeKernel ()
 Constructor. More...
 
 GCWeightsReshapeKernel (const GCWeightsReshapeKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
GCWeightsReshapeKerneloperator= (const GCWeightsReshapeKernel &)=delete
 Prevent instances of this class from being copied (As this class contains pointers) More...
 
 GCWeightsReshapeKernel (GCWeightsReshapeKernel &&)=default
 Allow instances of this class to be moved. More...
 
GCWeightsReshapeKerneloperator= (GCWeightsReshapeKernel &&)=default
 Allow instances of this class to be moved. More...
 
 ~GCWeightsReshapeKernel ()=default
 Default destructor. More...
 
void configure (const IGCTensor *input, const IGCTensor *biases, IGCTensor *output)
 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...
 

Detailed Description

GLES Compute kernel to perform reshaping on the weights used by convolution and locally connected layer.

Rearranges each 3-dimensional kernel to a single row leading to a matrix with linearized kernels. In combination with the GCIm2ColKernel can transform a convolution to a matrix multiplication.

For example assuming a 3D weight kernel of 3x3 dimensions and depth of 2 we have:

\[ \left( \begin{array}{ccc} a000 & a001 & a002 \\ a010 & a011 & a012 \\ a020 & a021 & a022 \\ \end{array} \right) \left( \begin{array}{ccc} a100 & a101 & a102 \\ a110 & a111 & a112 \\ a120 & a121 & a122 \\ \end{array} \right) \rightarrow \left( \begin{array}{ccccccccc} a000 & a001 & a002 & a010 & a011 & a012 & a020 & a021 & a022 & a100 & a101 & a102 & a110 & a111 & a112 & a120 & a121 & a122 \\ \end{array} \right) \]

Definition at line 54 of file GCWeightsReshapeKernel.h.

Constructor & Destructor Documentation

◆ GCWeightsReshapeKernel() [1/3]

Constructor.

Definition at line 41 of file GCWeightsReshapeKernel.cpp.

42  : _input(nullptr), _biases(nullptr), _output(nullptr)
43 {
44 }

◆ GCWeightsReshapeKernel() [2/3]

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

◆ GCWeightsReshapeKernel() [3/3]

Allow instances of this class to be moved.

◆ ~GCWeightsReshapeKernel()

~GCWeightsReshapeKernel ( )
default

Default destructor.

Member Function Documentation

◆ configure()

void configure ( const IGCTensor input,
const IGCTensor biases,
IGCTensor output 
)

Set the input and output of the kernel.

Parameters
[in]inputThe input tensor to convert. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM] if shared, and 5D tensor with dimensions [kernel_x, kernel_y, IFM, OFM, batches] if unshared. Data types supported: F16, F32
[in]biasesThe shared biases tensor to append. Bias is 1D tensor with dimensions [OFM] if shared and 2D tensor with dimensions [OFM, batches] if unshared. Data types supported: Same as input
Warning
Appending biases to weights reshaped matrix is not supported for quantized asymmetric types.
Parameters
[out]outputThe output tensor. Should be a 2D Tensor. Data types supported: Same as input

Definition at line 46 of file GCWeightsReshapeKernel.cpp.

47 {
50 
51  // Output tensor auto inizialitation if not yet initialized
52  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(compute_weights_reshaped_shape(*input->info(), (biases != nullptr))));
53 
55 
56  if(biases != nullptr)
57  {
59  ARM_COMPUTE_ERROR_ON((input->info()->num_dimensions() == 4) && (biases->info()->num_dimensions() != 1));
60  ARM_COMPUTE_ERROR_ON((input->info()->num_dimensions() == 5) && (biases->info()->num_dimensions() != 2));
61  ARM_COMPUTE_ERROR_ON((input->info()->num_dimensions() == 4) && (biases->info()->dimension(0) != input->info()->tensor_shape()[3]));
62  ARM_COMPUTE_ERROR_ON((input->info()->num_dimensions() == 5) && (biases->info()->dimension(0) != input->info()->tensor_shape()[3] || biases->info()->dimension(1) != input->info()->tensor_shape()[4]));
63  }
64 
65  _biases = biases;
66  _output = output;
67  _input = input;
68 
69  // Create build options
70  std::set<std::string> build_opts;
71  std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
72  build_opts.emplace("#define " + dt_name);
73  build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
74  build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
75  build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
76  build_opts.emplace("#define RESHAPE_TO_COLUMNS");
77  if(biases != nullptr)
78  {
79  build_opts.emplace("#define HAS_BIAS");
80  }
81 
82  // Create kernel
83  _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("reshape_to_columns", build_opts));
84 
85  // Set static arguments
87  idx += (biases != nullptr) ? num_arguments_per_1D_tensor() : 0;
88  _kernel.set_argument(idx++, _input->info()->dimension(0));
89  _kernel.set_argument(idx++, _input->info()->dimension(1));
90  _kernel.set_argument(idx++, _input->info()->dimension(2));
91  _kernel.set_argument(idx++, _input->info()->dimension(3));
92 
93  // Configure window
94  Window win = calculate_max_window(*input->info(), Steps());
95 
96  // The GCWeightsReshapeKernel doesn't need padding so update_window_and_padding() can be skipped
97  output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
98  IGCKernel::configure(win);
99 }
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
unsigned int num_arguments_per_2D_tensor() const
Returns the number of arguments enqueued per 2D tensor object.
Definition: IGCKernel.cpp:142
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:543
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:337
unsigned int num_arguments_per_1D_tensor() const
Returns the number of arguments enqueued per 1D tensor object.
Definition: IGCKernel.cpp:137
unsigned int num_arguments_per_3D_tensor() const
Returns the number of arguments enqueued per 3D tensor object.
Definition: IGCKernel.cpp:147
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.
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...
Definition: Helpers.inl:201
1 channel, 1 F16 per channel
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
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
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:789
static GCKernelLibrary & get()
Get the static instance of GCKernelLibrary.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
TensorShape compute_weights_reshaped_shape(const ITensorInfo &weights, bool has_bias=false, unsigned int num_groups=1)
Calculate the reshaped shape of the weights.
Container for valid region of a window.
Definition: Types.h:174
Describe a multidimensional execution window.
Definition: Window.h:39

References ARM_COMPUTE_ERROR_ON, ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN, ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES, ARM_COMPUTE_ERROR_ON_NULLPTR, arm_compute::auto_init_if_empty(), arm_compute::calculate_max_window(), ICloneable< T >::clone(), arm_compute::misc::shape_calculator::compute_weights_reshaped_shape(), arm_compute::create_kernel(), ITensorInfo::data_type(), ITensorInfo::dimension(), arm_compute::F16, arm_compute::F32, GCKernelLibrary::get(), ITensor::info(), IGCKernel::num_arguments_per_1D_tensor(), IGCKernel::num_arguments_per_2D_tensor(), IGCKernel::num_arguments_per_3D_tensor(), ITensorInfo::num_dimensions(), ITensorInfo::set_valid_region(), ITensorInfo::tensor_shape(), and arm_compute::support::cpp11::to_string().

Referenced by GCConvolutionLayerReshapeWeights::configure().

◆ operator=() [1/2]

GCWeightsReshapeKernel& operator= ( const GCWeightsReshapeKernel )
delete

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

◆ operator=() [2/2]

GCWeightsReshapeKernel& operator= ( GCWeightsReshapeKernel &&  )
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 101 of file GCWeightsReshapeKernel.cpp.

102 {
105 
106  Window out_window;
107  out_window.use_tensor_dimensions(_output->info()->tensor_shape());
108 
109  Window in_slice = window.first_slice_window_3D();
110  Window out_slice = out_window.first_slice_window_2D();
111 
112  Window biases_window;
113  Window biases_slice;
114 
115  if(_biases != nullptr)
116  {
117  biases_window.use_tensor_dimensions(_biases->info()->tensor_shape());
118  biases_slice = biases_window.first_slice_window_1D();
119  }
120 
121  _kernel.use();
122 
123  do
124  {
125  // Set arguments
126  unsigned idx = 0;
127  add_3D_tensor_argument(idx, _input, 1, in_slice);
128  add_2D_tensor_argument(idx, _output, 2, out_slice);
129  if(_biases != nullptr)
130  {
131  add_1D_tensor_argument(idx, _biases, 3, biases_slice);
132  biases_window.slide_window_slice_1D(biases_slice);
133  }
134 
135  _kernel.update_shader_params();
136  // Run kernel
137  enqueue(*this, in_slice);
138  }
139  while(window.slide_window_slice_4D(in_slice) && out_window.slide_window_slice_2D(out_slice));
140 }
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:267
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's parameters to the object's kernel's arguments starting from the index idx.
Definition: IGCKernel.cpp:132
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:39
void use_tensor_dimensions(const TensorShape &shape, size_t first_dimension=Window::DimX)
Use the tensor's dimensions to fill the window dimensions.
Definition: Window.inl:250
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Definition: Window.h:307
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(f, w)
Definition: Validate.h:183
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.
Definition: IGCKernel.cpp:127
bool slide_window_slice_4D(Window &slice) const
Slide the passed 4D window slice.
Definition: Window.h:331
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:275
bool slide_window_slice_1D(Window &slice) const
Slide the passed 1D window slice.
Definition: Window.h:295
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:940
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.
Definition: IGCKernel.cpp:122
Window first_slice_window_1D() const
First 1D slice of the window.
Definition: Window.h:259

References IGCKernel::add_1D_tensor_argument(), IGCKernel::add_2D_tensor_argument(), IGCKernel::add_3D_tensor_argument(), ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS, ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL, arm_compute::enqueue(), Window::first_slice_window_1D(), Window::first_slice_window_2D(), Window::first_slice_window_3D(), ITensor::info(), Window::slide_window_slice_1D(), Window::slide_window_slice_2D(), Window::slide_window_slice_4D(), ITensorInfo::tensor_shape(), Window::use_tensor_dimensions(), and IKernel::window().


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