Compute Library
 21.02
CLWeightsReshapeKernel.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  */
26 #include "arm_compute/core/Error.h"
30 #include "support/StringSupport.h"
31 
32 namespace arm_compute
33 {
35 
36 namespace
37 {
38 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *biases, const ITensorInfo *output, unsigned int num_groups)
39 {
41  ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
42  ARM_COMPUTE_RETURN_ERROR_ON(num_groups == 0);
43  ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::NHWC && num_groups > 1);
44  ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4 && num_groups > 1);
45  ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(3) % num_groups) != 0);
46 
47  if(biases != nullptr)
48  {
49  ARM_COMPUTE_RETURN_ERROR_ON(!is_data_type_float(input->data_type()));
51  ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 4) && (biases->num_dimensions() != 1));
52  ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 5) && (biases->num_dimensions() != 2));
53  ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 4) && (biases->dimension(0) != input->tensor_shape()[3]));
54  ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 5) && (biases->dimension(0) != input->tensor_shape()[3] || biases->dimension(1) != input->tensor_shape()[4]));
55  }
56 
57  // Checks performed when output is configured
58  if(output->total_size() != 0)
59  {
60  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_weights_reshaped_shape(*input, biases != nullptr, num_groups));
63  }
64 
65  return Status{};
66 }
67 } // namespace
68 
70  : _input(nullptr), _biases(nullptr), _output(nullptr)
71 {
72 }
73 
74 void CLWeightsReshapeKernel::configure(const ICLTensor *input, const ICLTensor *biases, ICLTensor *output, unsigned int num_groups)
75 {
76  configure(CLKernelLibrary::get().get_compile_context(), input, biases, output, num_groups);
77 }
78 
79 void CLWeightsReshapeKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *biases, ICLTensor *output, unsigned int num_groups)
80 {
81  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
82 
83  // Output tensor auto inizialitation if not yet initialized
84  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(compute_weights_reshaped_shape(*input->info(), (biases != nullptr), num_groups)));
85 
86  // Perform validation step
88  (biases != nullptr) ? biases->info() : nullptr,
89  output->info(), num_groups));
90 
91  auto padding_info = get_padding_info({ input, biases, output });
92 
93  const DataType data_type = input->info()->data_type();
94 
95  _biases = biases;
96  _output = output;
97  _input = input;
98 
99  // Create build options
100  CLBuildOptions build_opts;
101  build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(data_size_from_type(data_type)));
102  build_opts.add_option("-DNUM_GROUPS=" + support::cpp11::to_string(num_groups));
103  build_opts.add_option_if(biases != nullptr, "-DHAS_BIAS");
104 
105  // Create kernel
106  _kernel = create_kernel(compile_context, "reshape_to_columns", build_opts.options());
107 
108  // Configure window
109  Window win = calculate_max_window(*input->info(), Steps());
110  // The CLWeightsReshapeKernel doesn't need padding so update_window_and_padding() can be skipped
111  output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
112  ICLKernel::configure_internal(win);
113 
115 }
116 
117 Status CLWeightsReshapeKernel::validate(const ITensorInfo *input, const ITensorInfo *biases, const ITensorInfo *output, unsigned int num_groups)
118 {
119  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, biases, output, num_groups));
120  return Status{};
121 }
122 
123 void CLWeightsReshapeKernel::run(const Window &window, cl::CommandQueue &queue)
124 {
127 
128  Window out_window;
129  out_window.use_tensor_dimensions(_output->info()->tensor_shape());
130 
131  Window in_slice = window.first_slice_window_3D();
132  Window out_slice = out_window.first_slice_window_2D();
133 
134  Window biases_window;
135  Window biases_slice;
136 
138  idx += (_biases != nullptr) ? num_arguments_per_1D_tensor() : 0;
139  _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(0));
140  _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(1));
141  _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(2));
142  _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(3));
143  _kernel.setArg<cl_uint>(idx++, _output->info()->strides_in_bytes().z());
144 
145  if(_biases != nullptr)
146  {
147  biases_window.use_tensor_dimensions(_biases->info()->tensor_shape());
148  biases_slice = biases_window.first_slice_window_1D();
149  }
150 
151  do
152  {
153  // Set arguments
154  unsigned idx = 0;
155  add_3D_tensor_argument(idx, _input, in_slice);
156  add_2D_tensor_argument(idx, _output, out_slice);
157  if(_biases != nullptr)
158  {
159  add_1D_tensor_argument(idx, _biases, biases_slice);
160  ARM_COMPUTE_UNUSED(biases_window.slide_window_slice_1D(biases_slice));
161  }
162 
163  // Run kernel
164  enqueue(queue, *this, in_slice, lws_hint());
165  }
166  while(window.slide_window_slice_4D(in_slice) && out_window.slide_window_slice_2D(out_slice));
167 }
168 } // namespace arm_compute
static constexpr unsigned int num_arguments_per_1D_tensor()
Returns the number of arguments enqueued per 1D tensor object.
Definition: ICLKernel.h:198
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:283
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
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
static Status validate(const ITensorInfo *input, const ITensorInfo *biases, const ITensorInfo *output, unsigned int num_groups=1)
Static function to check if given info will lead to a valid configuration of CLWeightsReshapeKernel.
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(...)
Definition: Validate.h:610
const StringSet & options() const
Gets the current options list set.
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:276
#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.
#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
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:288
void add_3D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, 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: ICLKernel.h:172
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.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:163
void add_option(std::string option)
Adds option to the existing build option list.
const DataType data_type
Definition: Im2Col.cpp:150
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:403
static constexpr unsigned int num_arguments_per_3D_tensor()
Returns the number of arguments enqueued per 3D tensor object.
Definition: ICLKernel.h:214
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(f, w)
Definition: Validate.h:183
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
void configure(const ICLTensor *input, const ICLTensor *biases, ICLTensor *output, unsigned int num_groups=1)
Set the input and output of the kernel.
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
T z() const
Alias to access the size of the third dimension.
Definition: Dimensions.h:97
const unsigned int num_groups
Definition: Im2Col.cpp:153
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.
size_t data_size_from_type(DataType data_type)
The size in bytes of the data type.
Definition: Utils.h:106
void add_option_if(bool cond, std::string option)
Adds option if a given condition is true;.
static constexpr unsigned int num_arguments_per_2D_tensor()
Returns the number of arguments enqueued per 2D tensor object.
Definition: ICLKernel.h:206
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
bool has_padding_changed(const std::unordered_map< const ITensorInfo *, PaddingSize > &padding_map)
Check if the previously stored padding info has changed after configuring a kernel.
Definition: Utils.cpp:528
CLCompileContext class.
void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, 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: ICLKernel.h:148
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
TensorShape compute_weights_reshaped_shape(const ITensorInfo &weights, bool has_bias=false, unsigned int num_groups=1)
Calculate the reshaped shape of the weights.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
Num samples, height, width, channels.
std::string get_cl_unsigned_type_from_element_size(size_t element_size)
Translates the element size to an unsigned integer data type.
Definition: CLHelpers.cpp:103
std::unordered_map< const ITensorInfo *, PaddingSize > get_padding_info(std::initializer_list< const ITensorInfo *> infos)
Stores padding information before configuring a kernel.
Definition: Utils.cpp:513
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
bool slide_window_slice_4D(Window &slice) const
Slide the passed 4D window slice.
Definition: Window.h:347
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
virtual const Strides & strides_in_bytes() const =0
The strides in bytes for accessing each dimension of the tensor.
Container for valid region of a window.
Definition: Types.h:188
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:124
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:291
DataType
Available data types.
Definition: Types.h:77
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
bool is_data_type_float(DataType dt)
Check if a given data type is of floating point type.
Definition: Utils.h:1148
Window first_slice_window_1D() const
First 1D slice of the window.
Definition: Window.h:275