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 */
24 #include "src/core/CL/kernels/CLWeightsReshapeKernel.h"
25 #include "arm_compute/core/CL/ICLTensor.h"
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
28 #include "src/core/helpers/AutoConfiguration.h"
29 #include "src/core/helpers/WindowHelpers.h"
30 #include "support/StringSupport.h"
31
32 namespace arm_compute
33 {
34 using namespace arm_compute::misc::shape_calculator;
35
36 namespace
37 {
validate_arguments(const ITensorInfo * input,const ITensorInfo * biases,const ITensorInfo * output,unsigned int num_groups)38 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *biases, const ITensorInfo *output, unsigned int num_groups)
39 {
40 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
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()));
50 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
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));
61 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
62 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
63 }
64
65 return Status{};
66 }
67 } // namespace
68
CLWeightsReshapeKernel()69 CLWeightsReshapeKernel::CLWeightsReshapeKernel()
70 : _input(nullptr), _biases(nullptr), _output(nullptr)
71 {
72 }
73
configure(const ICLTensor * input,const ICLTensor * biases,ICLTensor * output,unsigned int num_groups)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
configure(const CLCompileContext & compile_context,const ICLTensor * input,const ICLTensor * biases,ICLTensor * output,unsigned int num_groups)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
87 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(),
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
114 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
115 }
116
validate(const ITensorInfo * input,const ITensorInfo * biases,const ITensorInfo * output,unsigned int num_groups)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
run(const Window & window,cl::CommandQueue & queue)123 void CLWeightsReshapeKernel::run(const Window &window, cl::CommandQueue &queue)
124 {
125 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
126 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
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
137 unsigned int idx = num_arguments_per_3D_tensor() + num_arguments_per_2D_tensor();
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
169