blob: d55c548b9928042ce1c084ec79da0a1f706a4c21 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyrou702dc0c2021-03-19 15:06:07 +00002 * Copyright (c) 2017-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLWeightsReshapeKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025#include "arm_compute/core/CL/ICLTensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026#include "arm_compute/core/Error.h"
Georgios Pinitas78c00902018-01-09 17:33:11 +000027#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010028#include "src/core/helpers/AutoConfiguration.h"
29#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000030#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000032namespace arm_compute
33{
Georgios Pinitas78c00902018-01-09 17:33:11 +000034using namespace arm_compute::misc::shape_calculator;
35
36namespace
37{
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010038Status validate_arguments(const ITensorInfo *input, const ITensorInfo *biases, const ITensorInfo *output, unsigned int num_groups)
Georgios Pinitas78c00902018-01-09 17:33:11 +000039{
40 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000041 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010042 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);
Georgios Pinitas78c00902018-01-09 17:33:11 +000046
47 if(biases != nullptr)
48 {
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000049 ARM_COMPUTE_RETURN_ERROR_ON(!is_data_type_float(input->data_type()));
Georgios Pinitas78c00902018-01-09 17:33:11 +000050 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
Georgios Pinitas78c00902018-01-09 17:33:11 +000051 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 {
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010060 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_weights_reshaped_shape(*input, biases != nullptr, num_groups));
Georgios Pinitas78c00902018-01-09 17:33:11 +000061 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitas78c00902018-01-09 17:33:11 +000062 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
63 }
64
65 return Status{};
66}
67} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068
Gian Marco Iodice5cb4c422017-06-23 10:38:25 +010069CLWeightsReshapeKernel::CLWeightsReshapeKernel()
70 : _input(nullptr), _biases(nullptr), _output(nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071{
72}
73
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010074void CLWeightsReshapeKernel::configure(const ICLTensor *input, const ICLTensor *biases, ICLTensor *output, unsigned int num_groups)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010076 configure(CLKernelLibrary::get().get_compile_context(), input, biases, output, num_groups);
77}
78
Manuel Bottini2803f702020-04-21 16:20:03 +010079void CLWeightsReshapeKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *biases, ICLTensor *output, unsigned int num_groups)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010080{
Georgios Pinitas78c00902018-01-09 17:33:11 +000081 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Gian Marco Iodice5cb4c422017-06-23 10:38:25 +010082
83 // Output tensor auto inizialitation if not yet initialized
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010084 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(compute_weights_reshaped_shape(*input->info(), (biases != nullptr), num_groups)));
Gian Marco Iodice5cb4c422017-06-23 10:38:25 +010085
Georgios Pinitas78c00902018-01-09 17:33:11 +000086 // Perform validation step
87 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(),
88 (biases != nullptr) ? biases->info() : nullptr,
Giorgio Arenac6aa49b2018-08-07 11:53:30 +010089 output->info(), num_groups));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090
Sheri Zhang11d73272020-10-28 14:01:55 +000091 auto padding_info = get_padding_info({ input, biases, output });
92
Gian Marco Iodice215b4ea2018-06-28 16:29:29 +010093 const DataType data_type = input->info()->data_type();
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010094
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095 _biases = biases;
96 _output = output;
97 _input = input;
98
99 // Create build options
Chunosov5124be52017-11-22 20:42:13 +0700100 CLBuildOptions build_opts;
Sheri Zhang55c0c0c2020-04-14 21:58:37 +0100101 build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(data_size_from_type(data_type)));
Giorgio Arenac6aa49b2018-08-07 11:53:30 +0100102 build_opts.add_option("-DNUM_GROUPS=" + support::cpp11::to_string(num_groups));
Chunosov5124be52017-11-22 20:42:13 +0700103 build_opts.add_option_if(biases != nullptr, "-DHAS_BIAS");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104
105 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100106 _kernel = create_kernel(compile_context, "reshape_to_columns", build_opts.options());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108 // Configure window
109 Window win = calculate_max_window(*input->info(), Steps());
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100110 ICLKernel::configure_internal(win);
Sheri Zhang11d73272020-10-28 14:01:55 +0000111
112 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113}
114
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100115Status CLWeightsReshapeKernel::validate(const ITensorInfo *input, const ITensorInfo *biases, const ITensorInfo *output, unsigned int num_groups)
Georgios Pinitas78c00902018-01-09 17:33:11 +0000116{
Giorgio Arenac6aa49b2018-08-07 11:53:30 +0100117 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, biases, output, num_groups));
Georgios Pinitas78c00902018-01-09 17:33:11 +0000118 return Status{};
119}
120
Gian Marco Iodice5cb4c422017-06-23 10:38:25 +0100121void CLWeightsReshapeKernel::run(const Window &window, cl::CommandQueue &queue)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122{
123 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
124 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
125
126 Window out_window;
SiCong Li86b53332017-08-23 11:02:43 +0100127 out_window.use_tensor_dimensions(_output->info()->tensor_shape());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128
129 Window in_slice = window.first_slice_window_3D();
130 Window out_slice = out_window.first_slice_window_2D();
131
132 Window biases_window;
133 Window biases_slice;
134
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +0100135 unsigned int idx = num_arguments_per_3D_tensor() + num_arguments_per_2D_tensor();
136 idx += (_biases != nullptr) ? num_arguments_per_1D_tensor() : 0;
137 _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(0));
138 _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(1));
139 _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(2));
140 _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(3));
141 _kernel.setArg<cl_uint>(idx++, _output->info()->strides_in_bytes().z());
142
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143 if(_biases != nullptr)
144 {
SiCong Li86b53332017-08-23 11:02:43 +0100145 biases_window.use_tensor_dimensions(_biases->info()->tensor_shape());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146 biases_slice = biases_window.first_slice_window_1D();
147 }
148
149 do
150 {
151 // Set arguments
152 unsigned idx = 0;
153 add_3D_tensor_argument(idx, _input, in_slice);
154 add_2D_tensor_argument(idx, _output, out_slice);
155 if(_biases != nullptr)
156 {
157 add_1D_tensor_argument(idx, _biases, biases_slice);
Michalis Spyrouebdde652019-07-08 11:52:46 +0100158 ARM_COMPUTE_UNUSED(biases_window.slide_window_slice_1D(biases_slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159 }
160
161 // Run kernel
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100162 enqueue(queue, *this, in_slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163 }
164 while(window.slide_window_slice_4D(in_slice) && out_window.slide_window_slice_2D(out_slice));
165}
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000166} // namespace arm_compute