blob: f5eaa5afb2bfa369b521deb79226f849b7b88e35 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas78c00902018-01-09 17:33:11 +00002 * Copyright (c) 2017-2018 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 */
24#include "arm_compute/core/CL/kernels/CLWeightsReshapeKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
30#include "arm_compute/core/Error.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/Types.h"
33#include "arm_compute/core/Validate.h"
Georgios Pinitas78c00902018-01-09 17:33:11 +000034#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
36using namespace arm_compute;
Georgios Pinitas78c00902018-01-09 17:33:11 +000037using namespace arm_compute::misc::shape_calculator;
38
39namespace
40{
41Status validate_arguments(const ITensorInfo *input, const ITensorInfo *biases, const ITensorInfo *output)
42{
43 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
44 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32);
45
46 if(biases != nullptr)
47 {
48 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(input->data_type()));
49 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
50 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(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));
61 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
62 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
63 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
64 }
65
66 return Status{};
67}
68} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069
Gian Marco Iodice5cb4c422017-06-23 10:38:25 +010070CLWeightsReshapeKernel::CLWeightsReshapeKernel()
71 : _input(nullptr), _biases(nullptr), _output(nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072{
73}
74
75void CLWeightsReshapeKernel::configure(const ICLTensor *input, const ICLTensor *biases, ICLTensor *output)
76{
Georgios Pinitas78c00902018-01-09 17:33:11 +000077 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Gian Marco Iodice5cb4c422017-06-23 10:38:25 +010078
79 // Output tensor auto inizialitation if not yet initialized
Georgios Pinitas78c00902018-01-09 17:33:11 +000080 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(compute_weights_reshaped_shape(*input->info(), (biases != nullptr))));
Gian Marco Iodice5cb4c422017-06-23 10:38:25 +010081
Georgios Pinitas78c00902018-01-09 17:33:11 +000082 // Perform validation step
83 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(),
84 (biases != nullptr) ? biases->info() : nullptr,
85 output->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086
Georgios Pinitas78c00902018-01-09 17:33:11 +000087 const DataType data_type = input->info()->data_type();
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010088
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089 _biases = biases;
90 _output = output;
91 _input = input;
92
93 // Create build options
Chunosov5124be52017-11-22 20:42:13 +070094 CLBuildOptions build_opts;
95 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
96 build_opts.add_option_if(biases != nullptr, "-DHAS_BIAS");
97 build_opts.add_option_if(is_data_type_fixed_point(data_type), "-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098
99 // Create kernel
Chunosov5124be52017-11-22 20:42:13 +0700100 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("reshape_to_columns", build_opts.options()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101
102 // Set static arguments
103 unsigned int idx = num_arguments_per_3D_tensor() + num_arguments_per_2D_tensor();
104 idx += (biases != nullptr) ? num_arguments_per_1D_tensor() : 0;
105 _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(0));
106 _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(1));
107 _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(2));
108 _kernel.setArg<cl_uint>(idx++, _input->info()->dimension(3));
109
110 // Configure window
111 Window win = calculate_max_window(*input->info(), Steps());
112 // The CLWeightsReshapeKernel doesn't need padding so update_window_and_padding() can be skipped
113 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
114 ICLKernel::configure(win);
115}
116
Georgios Pinitas78c00902018-01-09 17:33:11 +0000117Status CLWeightsReshapeKernel::validate(const ITensorInfo *input, const ITensorInfo *biases, const ITensorInfo *output)
118{
119 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, biases, output));
120 return Status{};
121}
122
Gian Marco Iodice5cb4c422017-06-23 10:38:25 +0100123void CLWeightsReshapeKernel::run(const Window &window, cl::CommandQueue &queue)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124{
125 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
126 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
127
128 Window out_window;
SiCong Li86b53332017-08-23 11:02:43 +0100129 out_window.use_tensor_dimensions(_output->info()->tensor_shape());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130
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 if(_biases != nullptr)
138 {
SiCong Li86b53332017-08-23 11:02:43 +0100139 biases_window.use_tensor_dimensions(_biases->info()->tensor_shape());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140 biases_slice = biases_window.first_slice_window_1D();
141 }
142
143 do
144 {
145 // Set arguments
146 unsigned idx = 0;
147 add_3D_tensor_argument(idx, _input, in_slice);
148 add_2D_tensor_argument(idx, _output, out_slice);
149 if(_biases != nullptr)
150 {
151 add_1D_tensor_argument(idx, _biases, biases_slice);
152 biases_window.slide_window_slice_1D(biases_slice);
153 }
154
155 // Run kernel
156 enqueue(queue, *this, in_slice);
157 }
158 while(window.slide_window_slice_4D(in_slice) && out_window.slide_window_slice_2D(out_slice));
159}