blob: 7f9e9d20e190a4695ad501309c2d15aa2780b908 [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"
35#include "arm_compute/core/Validate.h"
36#include "support/ToolchainSupport.h"
37
38using namespace arm_compute;
39
40template <unsigned int kernel_size>
41CLDirectConvolutionLayerKernel<kernel_size>::CLDirectConvolutionLayerKernel()
42 : _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
46template <unsigned int kernel_size>
47BorderSize CLDirectConvolutionLayerKernel<kernel_size>::border_size() const
48{
49 return _border_size;
50}
51
52template <unsigned int kernel_size>
53void CLDirectConvolutionLayerKernel<kernel_size>::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info)
54{
55 static_assert(kernel_size == 3, "Currently only 3x3 direct convolution is supported!");
56
57 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
58 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
59 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);
62 ARM_COMPUTE_ERROR_ON_MSG((kernel_size == 3 && std::get<0>(conv_info.stride()) > 2), "Strides larger than 2 not supported in 3x3 direct convolution!");
63
64 ARM_COMPUTE_ERROR_ON(kernel_size != weights->info()->dimension(0));
65
66 if(biases != nullptr)
67 {
68 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
69 ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(3));
70 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
71 }
72
73 _conv_stride_x = std::get<0>(conv_info.stride());
74 _conv_stride_y = std::get<1>(conv_info.stride());
75 _conv_pad_x = std::get<0>(conv_info.pad());
76 _conv_pad_y = std::get<1>(conv_info.pad());
77
78 _input = input;
79 _weights = weights;
80 _output = output;
81 _biases = biases;
82 _border_size = BorderSize(_conv_pad_y, _conv_pad_x);
83
84 std::stringstream kernel_name;
85 std::set<std::string> options;
86 kernel_name << "direct_convolution" << kernel_size << "x" << kernel_size;
87
88 options.insert("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
89
90 options.emplace("-DSTRIDE_X=" + support::cpp11::to_string(_conv_stride_x));
91
92 if(_biases != nullptr)
93 {
94 options.emplace("-DHAS_BIAS");
95 }
96
97 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name.str(), options));
98
99 unsigned int idx = (_biases == nullptr) ? 3 * num_arguments_per_3D_tensor() : (num_arguments_per_1D_tensor() + 3 * num_arguments_per_3D_tensor());
100 _kernel.setArg<cl_uint>(idx++, _weights->info()->strides_in_bytes()[3]); // weights_stride_w
101 _kernel.setArg<cl_uint>(idx++, _weights->info()->dimension(2)); // filter depth
102
103 // Using this local workgroup size gives better performance over others that have been tried.
104 _lws_hint = cl::NDRange(4, 1, 8);
105
106 // Configure kernel window
107 Window win = calculate_max_window(*output->info());
108
109 unsigned int num_elems_read_per_iteration = 16 * _conv_stride_x;
110 unsigned int num_elems_written_per_iteration = 8;
111
112 // Calculate right and bottom border
113 const int input_width = input->info()->dimension(0);
114 const int input_height = input->info()->dimension(1);
115 const int upper_bound_w = ceil_to_multiple(((output->info()->dimension(0) - 1) * _conv_stride_x + kernel_size), num_elems_read_per_iteration) - _conv_pad_x - input_width;
116 const int upper_bound_h = ((output->info()->dimension(1) - 1) * _conv_stride_y - _conv_pad_y + kernel_size) - input_height;
117 const int padding_right = std::max(upper_bound_w, static_cast<int>(kernel_size));
118 const int padding_bottom = std::max(upper_bound_h, static_cast<int>(kernel_size));
119
120 // Create window and update padding
121 win = calculate_max_window(*output->info(), Steps(num_elems_written_per_iteration));
122 AccessWindowStatic input_access(input->info(), -_conv_pad_x, -_conv_pad_y, input_width + padding_right, input_height + padding_bottom);
123
124 AccessWindowStatic weights_access(weights->info(), 0, 0, kernel_size, kernel_size);
125 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
126 update_window_and_padding(win, input_access, weights_access, output_access);
127
128 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
129
130 ICLKernel::configure(win);
131}
132
133template <unsigned int kernel_size>
134void CLDirectConvolutionLayerKernel<kernel_size>::run(const Window &window, cl::CommandQueue &queue)
135{
136 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
137 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
138
139 // Get initial windows
140 Window slice = window.first_slice_window_3D();
141 Window win_in = window;
142
143 win_in.adjust(Window::DimX, -_conv_pad_x, true);
144 win_in.adjust(Window::DimY, -_conv_pad_y, true);
145 win_in.set_dimension_step(Window::DimX, window.x().step() * _conv_stride_x);
146 win_in.set_dimension_step(Window::DimY, window.y().step() * _conv_stride_y);
147
148 Window slice_in = win_in.first_slice_window_3D();
149
150 unsigned int idx1 = 2 * num_arguments_per_3D_tensor();
151 add_3D_tensor_argument(idx1, _weights, slice);
152
153 if(_biases != nullptr)
154 {
155 Window slice_biases;
156 slice_biases.use_tensor_dimensions(_biases->info());
157 add_1D_tensor_argument(idx1, _biases, slice_biases);
158 }
159
160 do
161 {
162 unsigned int idx = 0;
163 add_3D_tensor_argument(idx, _input, slice_in);
164 add_3D_tensor_argument(idx, _output, slice);
165
166 enqueue(queue, *this, slice, _lws_hint);
167 }
168 while(window.slide_window_slice_3D(slice) && win_in.slide_window_slice_3D(slice_in));
169}
170
171template class arm_compute::CLDirectConvolutionLayerKernel<3>;