Compute Library
 22.05
ClWeightsReshapeKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 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/Cast.h"
31 #include "support/StringSupport.h"
32 
33 namespace arm_compute
34 {
35 using namespace misc::shape_calculator;
36 namespace opencl
37 {
38 namespace kernels
39 {
40 namespace
41 {
42 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *biases, const ITensorInfo *output, unsigned int num_groups)
43 {
45  ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
46  ARM_COMPUTE_RETURN_ERROR_ON(num_groups == 0);
47  ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::NHWC && num_groups > 1);
48  ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4 && num_groups > 1);
49  ARM_COMPUTE_RETURN_ERROR_ON((input->dimension(3) % num_groups) != 0);
50 
51  if(biases != nullptr)
52  {
53  ARM_COMPUTE_RETURN_ERROR_ON(!is_data_type_float(input->data_type()));
55  ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 4) && (biases->num_dimensions() != 1));
56  ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 5) && (biases->num_dimensions() != 2));
57  ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 4) && (biases->dimension(0) != input->tensor_shape()[3]));
58  ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 5) && (biases->dimension(0) != input->tensor_shape()[3] || biases->dimension(1) != input->tensor_shape()[4]));
59  }
60 
61  // Checks performed when output is configured
62  if(output->total_size() != 0)
63  {
64  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_weights_reshaped_shape(*input, biases != nullptr, num_groups));
67  }
68 
69  return Status{};
70 }
71 } // namespace
72 
74 {
76 }
77 
78 void ClWeightsReshapeKernel::configure(const ClCompileContext &compile_context, const ITensorInfo *src, const ITensorInfo *biases, ITensorInfo *dst, unsigned int num_groups)
79 {
81 
82  // Output tensor auto inizialitation if not yet initialized
83  auto_init_if_empty(*dst, src->clone()->set_tensor_shape(compute_weights_reshaped_shape(*src, (biases != nullptr), num_groups)));
84 
85  // Perform validation step
86  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, biases, dst, num_groups));
87  auto padding_info = get_padding_info({ src, biases, dst });
88 
89  const DataType data_type = src->data_type();
90 
91  // Create build options
92  CLBuildOptions build_opts;
93  build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(data_size_from_type(data_type)));
94  build_opts.add_option("-DNUM_GROUPS=" + support::cpp11::to_string(num_groups));
95  build_opts.add_option_if(biases != nullptr, "-DHAS_BIAS");
96 
97  // Create kernel
98  _kernel = create_kernel(compile_context, "reshape_to_columns", build_opts.options());
99 
100  // Configure window
101  Window win = calculate_max_window(*src, Steps());
102  ICLKernel::configure_internal(win);
103 
105 }
106 
107 Status ClWeightsReshapeKernel::validate(const ITensorInfo *src, const ITensorInfo *biases, const ITensorInfo *dst, unsigned int num_groups)
108 {
109  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, biases, dst, num_groups));
110  return Status{};
111 }
112 
113 void ClWeightsReshapeKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
114 {
117 
118  auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
119  auto biases = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_BIAS));
120  auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
121 
122  Window out_window;
123  out_window.use_tensor_dimensions(dst->info()->tensor_shape());
124 
125  Window in_slice = window.first_slice_window_3D();
126  Window out_slice = out_window.first_slice_window_2D();
127 
128  Window biases_window;
129  Window biases_slice;
130 
131  unsigned int idx = num_arguments_per_3D_tensor() + num_arguments_per_2D_tensor();
132  idx += (biases != nullptr) ? num_arguments_per_1D_tensor() : 0;
133  _kernel.setArg<cl_uint>(idx++, src->info()->dimension(0));
134  _kernel.setArg<cl_uint>(idx++, src->info()->dimension(1));
135  _kernel.setArg<cl_uint>(idx++, src->info()->dimension(2));
136  _kernel.setArg<cl_uint>(idx++, src->info()->dimension(3));
137  _kernel.setArg<cl_uint>(idx++, dst->info()->strides_in_bytes().z());
138 
139  if(biases != nullptr)
140  {
141  biases_window.use_tensor_dimensions(biases->info()->tensor_shape());
142  biases_slice = biases_window.first_slice_window_1D();
143  }
144 
145  do
146  {
147  // Set arguments
148  unsigned idx = 0;
149  add_3D_tensor_argument(idx, src, in_slice);
150  add_2D_tensor_argument(idx, dst, out_slice);
151  if(biases != nullptr)
152  {
153  add_1D_tensor_argument(idx, biases, biases_slice);
154  ARM_COMPUTE_UNUSED(biases_window.slide_window_slice_1D(biases_slice));
155  }
156 
157  // Run kernel
158  enqueue(queue, *this, in_slice, lws_hint());
159  }
160  while(window.slide_window_slice_4D(in_slice) && out_window.slide_window_slice_2D(out_slice));
161 }
162 } // namespace kernels
163 } // namespace opencl
164 } // namespace arm_compute
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:296
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 configure(const ClCompileContext &compile_context, const ITensorInfo *src, const ITensorInfo *biases, ITensorInfo *dst, unsigned int num_groups=1)
Set the input and output of the kernel.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(...)
Definition: Validate.h:606
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:32
const StringSet & options() const
Gets the current options list set.
#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
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
Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const PadStrideInfo &conv_info)
#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:284
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
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2022 Arm Limited.
void run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
void add_option(std::string option)
Adds option to the existing build option list.
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:54
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:391
#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:179
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
const unsigned int num_groups
Definition: Im2Col.cpp:153
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.
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 Status validate(const ITensorInfo *src, const ITensorInfo *biases, const ITensorInfo *dst, unsigned int num_groups=1)
Static function to check if given info will lead to a valid configuration.
Elementeise CL kernel type.
Definition: CLTypes.h:84
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
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:601
CLCompileContext class.
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
Definition: ITensorPack.cpp:64
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:541
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:105
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:586
bool slide_window_slice_4D(Window &slice) const
Slide the passed 4D window slice.
Definition: Window.h:360
Tensor packing service.
Definition: ITensorPack.h:39
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
Window first_slice_window_3D() const
First 3D slice of the window.
Definition: Window.h:304
DataType
Available data types.
Definition: Types.h:79
bool slide_window_slice_1D(Window &slice) const
Slide the passed 1D window slice.
Definition: Window.h:324
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:1010
Window first_slice_window_1D() const
First 1D slice of the window.
Definition: Window.h:288