Compute Library
 21.02
GCWeightsReshapeKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 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 
26 #include "arm_compute/core/Error.h"
32 #include "arm_compute/core/Types.h"
37 #include "support/StringSupport.h"
38 
40 
41 using namespace arm_compute;
43 
45  : _input(nullptr), _biases(nullptr), _output(nullptr)
46 {
47 }
48 
50 {
53 
54  // Output tensor auto inizialitation if not yet initialized
55  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(compute_weights_reshaped_shape(*input->info(), (biases != nullptr))));
56 
58 
59  if(biases != nullptr)
60  {
62  ARM_COMPUTE_ERROR_ON((input->info()->num_dimensions() == 4) && (biases->info()->num_dimensions() != 1));
63  ARM_COMPUTE_ERROR_ON((input->info()->num_dimensions() == 5) && (biases->info()->num_dimensions() != 2));
64  ARM_COMPUTE_ERROR_ON((input->info()->num_dimensions() == 4) && (biases->info()->dimension(0) != input->info()->tensor_shape()[3]));
65  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]));
66  }
67 
68  _biases = biases;
69  _output = output;
70  _input = input;
71 
72  // Create build options
73  std::set<std::string> build_opts;
74  std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
75  build_opts.emplace("#define " + dt_name);
76  build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
77  build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
78  build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
79  build_opts.emplace("#define RESHAPE_TO_COLUMNS");
80  if(biases != nullptr)
81  {
82  build_opts.emplace("#define HAS_BIAS");
83  }
84 
85  // Create kernel
86  _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("reshape_to_columns", build_opts));
87 
88  // Set static arguments
90  idx += (biases != nullptr) ? num_arguments_per_1D_tensor() : 0;
91  _kernel.set_argument(idx++, _input->info()->dimension(0));
92  _kernel.set_argument(idx++, _input->info()->dimension(1));
93  _kernel.set_argument(idx++, _input->info()->dimension(2));
94  _kernel.set_argument(idx++, _input->info()->dimension(3));
95 
96  // Configure window
97  Window win = calculate_max_window(*input->info(), Steps());
98 
99  // The GCWeightsReshapeKernel doesn't need padding so update_window_and_padding() can be skipped
100  output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
101  IGCKernel::configure(win);
102 }
103 
105 {
108 
109  Window out_window;
110  out_window.use_tensor_dimensions(_output->info()->tensor_shape());
111 
112  Window in_slice = window.first_slice_window_3D();
113  Window out_slice = out_window.first_slice_window_2D();
114 
115  Window biases_window;
116  Window biases_slice;
117 
118  if(_biases != nullptr)
119  {
120  biases_window.use_tensor_dimensions(_biases->info()->tensor_shape());
121  biases_slice = biases_window.first_slice_window_1D();
122  }
123 
124  _kernel.use();
125 
126  do
127  {
128  // Set arguments
129  unsigned idx = 0;
130  add_3D_tensor_argument(idx, _input, 1, in_slice);
131  add_2D_tensor_argument(idx, _output, 2, out_slice);
132  if(_biases != nullptr)
133  {
134  add_1D_tensor_argument(idx, _biases, 3, biases_slice);
135  biases_window.slide_window_slice_1D(biases_slice);
136  }
137 
138  _kernel.update_shader_params();
139  // Run kernel
140  enqueue(*this, in_slice);
141  }
142  while(window.slide_window_slice_4D(in_slice) && out_window.slide_window_slice_2D(out_slice));
143 }
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:283
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
unsigned int num_arguments_per_2D_tensor() const
Returns the number of arguments enqueued per 2D tensor object.
Definition: IGCKernel.cpp:142
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
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
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
void configure(const IGCTensor *input, const IGCTensor *biases, IGCTensor *output)
Set the input and output of the kernel.
Interface for GLES Compute tensor.
Definition: IGCTensor.h:35
GCKernel class.
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
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
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Definition: Window.h:323
Copyright (c) 2017-2021 Arm Limited.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
1 channel, 1 F16 per channel
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(f, w)
Definition: Validate.h:183
void run(const Window &window) override
Enqueue the OpenGL ES shader to process the given window.
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
Manages all the GLES kernels compilation and caching, provides accessors for the GLES Context...
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
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 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&#39;s metadata.
void add_2D_tensor_argument(unsigned int &idx, const IGCTensor *tensor, const unsigned int binding_point, const Window &window)
Add the passed 2D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: IGCKernel.cpp:127
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
Wrapper to configure the Khronos EGL and OpenGL ES C header.
static GCKernelLibrary & get()
Get the static instance of GCKernelLibrary.
TensorShape compute_weights_reshaped_shape(const ITensorInfo &weights, bool has_bias=false, unsigned int num_groups=1)
Calculate the reshaped shape of the weights.
bool slide_window_slice_4D(Window &slice) const
Slide the passed 4D window slice.
Definition: Window.h:347
GCKernel create_kernel(const std::string &shader_name, const StringSet &build_options_set={}) const
Creates a kernel from the kernel library.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
Container for valid region of a window.
Definition: Types.h:188
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:291
bool slide_window_slice_1D(Window &slice) const
Slide the passed 1D window slice.
Definition: Window.h:311
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
Window first_slice_window_1D() const
First 1D slice of the window.
Definition: Window.h:275