blob: 5f14d16ff4ca7567d5b728d2d2a34408470a4e4e [file] [log] [blame]
steniu0127b386c2017-07-18 17:37:43 +01001/*
2 * Copyright (c) 2017 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 "arm_compute/core/CL/kernels/CLDirectConvolutionLayerKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
29#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/Error.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/IAccessWindow.h"
33#include "arm_compute/core/ITensor.h"
34#include "arm_compute/core/Types.h"
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010035#include "arm_compute/core/Utils.h"
steniu0127b386c2017-07-18 17:37:43 +010036#include "arm_compute/core/Validate.h"
37#include "support/ToolchainSupport.h"
38
39using namespace arm_compute;
40
SiCong Lic51b72f2017-07-28 14:46:20 +010041CLDirectConvolutionLayerKernel::CLDirectConvolutionLayerKernel()
steniu0127b386c2017-07-18 17:37:43 +010042 : _input(nullptr), _biases(nullptr), _weights(nullptr), _output(nullptr), _border_size(0), _conv_pad_x(0), _conv_pad_y(0), _conv_stride_x(0), _conv_stride_y(0)
43{
44}
45
SiCong Lic51b72f2017-07-28 14:46:20 +010046BorderSize CLDirectConvolutionLayerKernel::border_size() const
steniu0127b386c2017-07-18 17:37:43 +010047{
48 return _border_size;
49}
50
SiCong Lic51b72f2017-07-28 14:46:20 +010051void CLDirectConvolutionLayerKernel::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info)
steniu0127b386c2017-07-18 17:37:43 +010052{
SiCong Lic51b72f2017-07-28 14:46:20 +010053 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010054 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
55 ARM_COMPUTE_ERROR_ON_MSG(weights->info()->dimension(0) != weights->info()->dimension(1),
56 "Only kernel sizes 1x1 and 3x3 are supported");
57 ARM_COMPUTE_ERROR_ON_MSG(weights->info()->dimension(0) != 1 && weights->info()->dimension(0) != 3,
58 "Only kernel sizes 1x1 and 3x3 are supported");
steniu0127b386c2017-07-18 17:37:43 +010059 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(2) != input->info()->dimension(2));
60 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(0) != weights->info()->dimension(1));
61 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4);
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010062 ARM_COMPUTE_ERROR_ON_MSG((weights->info()->dimension(0) == 1) && std::get<0>(conv_info.stride()) > 3, "Strides larger than 3 not supported for 1x1 convolution.");
63 ARM_COMPUTE_ERROR_ON_MSG((weights->info()->dimension(0) == 3) && std::get<0>(conv_info.stride()) > 2, "Strides larger than 2 not supported for 3x3 convolution.");
steniu0127b386c2017-07-18 17:37:43 +010064
steniu0127b386c2017-07-18 17:37:43 +010065 if(biases != nullptr)
66 {
67 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
68 ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(3));
69 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
70 }
71
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010072 const unsigned int kernel_size = weights->info()->dimension(0);
73
74 // Get convolved dimensions
75 unsigned int output_width = 0;
76 unsigned int output_height = 0;
77 std::tie(output_width, output_height) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_size, kernel_size, conv_info);
78
79 TensorShape output_shape = input->info()->tensor_shape();
80 output_shape.set(0, output_width);
81 output_shape.set(1, output_height);
82 output_shape.set(2, weights->info()->dimension(3));
83
84 // Output auto inizialitation if not yet initialized
85 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position());
86
87 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
88 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
89 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
90
steniu0127b386c2017-07-18 17:37:43 +010091 _conv_stride_x = std::get<0>(conv_info.stride());
92 _conv_stride_y = std::get<1>(conv_info.stride());
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010093 _conv_pad_x = std::min(std::get<0>(conv_info.pad()), kernel_size / 2);
94 _conv_pad_y = std::min(std::get<1>(conv_info.pad()), kernel_size / 2);
steniu0127b386c2017-07-18 17:37:43 +010095
96 _input = input;
97 _weights = weights;
98 _output = output;
99 _biases = biases;
100 _border_size = BorderSize(_conv_pad_y, _conv_pad_x);
101
102 std::stringstream kernel_name;
103 std::set<std::string> options;
104 kernel_name << "direct_convolution" << kernel_size << "x" << kernel_size;
105
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100106 options.emplace("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
107 options.emplace("-DDATA_SIZE=" + get_data_size_from_data_type(input->info()->data_type()));
108 options.emplace("-DWEIGHTS_DEPTH=" + support::cpp11::to_string(_weights->info()->dimension(2)));
steniu0127b386c2017-07-18 17:37:43 +0100109 options.emplace("-DSTRIDE_X=" + support::cpp11::to_string(_conv_stride_x));
110
111 if(_biases != nullptr)
112 {
113 options.emplace("-DHAS_BIAS");
114 }
115
116 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name.str(), options));
117
steniu0127b386c2017-07-18 17:37:43 +0100118 // Configure kernel window
119 Window win = calculate_max_window(*output->info());
120
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100121 bool is_kernel3x3_stride2 = ((kernel_size == 3) && (_conv_stride_x == 2));
122
123 const unsigned int num_elems_read_per_iteration_x = 8 + 2 * (kernel_size / 2) + (is_kernel3x3_stride2 ? 7 : 0);
124 const unsigned int num_elems_read_per_iteration_y = kernel_size;
125 const unsigned int num_elems_written_per_iteration_x = 8;
126 const unsigned int num_elems_written_per_iteration_y = 1;
steniu0127b386c2017-07-18 17:37:43 +0100127
128 // Calculate right and bottom border
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100129 const int input_width = input->info()->dimension(0) - kernel_size / 2 + _conv_pad_x;
130 const int input_height = input->info()->dimension(1) - kernel_size / 2 + _conv_pad_y;
steniu0127b386c2017-07-18 17:37:43 +0100131
132 // Create window and update padding
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100133 win = calculate_max_window(*output->info(), Steps(num_elems_written_per_iteration_x, num_elems_written_per_iteration_y));
steniu0127b386c2017-07-18 17:37:43 +0100134
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100135 AccessWindowStatic input_access(input->info(), -_conv_pad_x, -_conv_pad_y, input_width + num_elems_read_per_iteration_x, input_height + num_elems_read_per_iteration_y);
136 AccessWindowStatic weights_access(weights->info(), 0, 0, kernel_size, kernel_size);
137 AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_written_per_iteration_x, num_elems_written_per_iteration_y);
138
steniu0127b386c2017-07-18 17:37:43 +0100139 update_window_and_padding(win, input_access, weights_access, output_access);
140
141 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
142
143 ICLKernel::configure(win);
144}
145
SiCong Lic51b72f2017-07-28 14:46:20 +0100146void CLDirectConvolutionLayerKernel::run(const Window &window, cl::CommandQueue &queue)
steniu0127b386c2017-07-18 17:37:43 +0100147{
148 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
149 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
150
151 // Get initial windows
152 Window slice = window.first_slice_window_3D();
153 Window win_in = window;
154
155 win_in.adjust(Window::DimX, -_conv_pad_x, true);
156 win_in.adjust(Window::DimY, -_conv_pad_y, true);
157 win_in.set_dimension_step(Window::DimX, window.x().step() * _conv_stride_x);
158 win_in.set_dimension_step(Window::DimY, window.y().step() * _conv_stride_y);
159
160 Window slice_in = win_in.first_slice_window_3D();
161
162 unsigned int idx1 = 2 * num_arguments_per_3D_tensor();
163 add_3D_tensor_argument(idx1, _weights, slice);
164
165 if(_biases != nullptr)
166 {
167 Window slice_biases;
168 slice_biases.use_tensor_dimensions(_biases->info());
169 add_1D_tensor_argument(idx1, _biases, slice_biases);
170 }
171
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100172 _kernel.setArg(idx1++, static_cast<unsigned int>(_weights->info()->strides_in_bytes()[3]));
173
steniu0127b386c2017-07-18 17:37:43 +0100174 do
175 {
176 unsigned int idx = 0;
177 add_3D_tensor_argument(idx, _input, slice_in);
178 add_3D_tensor_argument(idx, _output, slice);
179
180 enqueue(queue, *this, slice, _lws_hint);
181 }
182 while(window.slide_window_slice_3D(slice) && win_in.slide_window_slice_3D(slice_in));
183}