blob: f0bbc3514f3f315b00214b32ac100513792d7a33 [file] [log] [blame]
Anthony Barbier871448e2017-03-24 14:54:29 +00001/*
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/runtime/CL/functions/CLConvolutionLayer.h"
25
26#include "arm_compute/core/PixelValue.h"
27#include "arm_compute/core/Utils.h"
28#include "arm_compute/core/Validate.h"
29#include "arm_compute/runtime/CL/CLScheduler.h"
30
31#include <cmath>
32#include <tuple>
33
34using namespace arm_compute;
35
Anthony Barbierdbdab852017-06-23 15:42:00 +010036CLConvolutionLayerReshapeWeights::CLConvolutionLayerReshapeWeights()
37 : _weights_reshape_kernel(), _weights_transposed_kernel(), _weights_reshaped(), _transpose1xW(false)
Anthony Barbier871448e2017-03-24 14:54:29 +000038{
39}
40
Anthony Barbierdbdab852017-06-23 15:42:00 +010041void CLConvolutionLayerReshapeWeights::configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, bool transpose1xW)
42{
43 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::F32);
44 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::F32);
45 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F32);
46 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases, output);
47 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(weights, biases, output);
48 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4);
49
50 if(biases != nullptr)
51 {
52 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::F32);
53 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
54 ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(3));
55 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
56 }
57
58 const bool _has_bias = (biases != nullptr);
59
60 _transpose1xW = transpose1xW;
61
62 if(transpose1xW)
63 {
64 // Create tensor to store the reshaped weights
65 const unsigned int mat_weights_cols = weights->info()->dimension(3);
66 const unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + (_has_bias ? 1 : 0);
67 TensorShape shape_wr(mat_weights_cols, mat_weights_rows);
68 const DataType dt = weights->info()->data_type();
69 TensorInfo info_wr(shape_wr, 1, dt);
70
71 _weights_reshaped.allocator()->init(info_wr);
72 _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped);
73 _weights_transposed_kernel.configure(&_weights_reshaped, output);
74 _weights_reshaped.allocator()->allocate();
75 }
76 else
77 {
78 _weights_reshape_kernel.configure(weights, biases, output);
79 }
80}
81
82void CLConvolutionLayerReshapeWeights::run()
83{
84 cl::CommandQueue q = CLScheduler::get().queue();
85 CLScheduler::get().enqueue(_weights_reshape_kernel);
86 if(_transpose1xW)
87 {
88 CLScheduler::get().enqueue(_weights_transposed_kernel);
89 }
90}
91
92CLConvolutionLayer::CLConvolutionLayer()
93 : _reshape_weights(), _input_im2col_kernel(), _input_interleave_kernel(), _mm_kernel(), _output_col2im_kernel(), _input_im2col_reshaped(), _input_interleaved_reshaped(), _weights_reshaped(),
94 _weights_transposed(), _gemm_output(), _has_bias(false), _is_fully_connected_convolution(false), _are_weights_reshaped(false)
95{
96}
97
98void CLConvolutionLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info)
Anthony Barbier871448e2017-03-24 14:54:29 +000099{
100 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
101 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::F16, DataType::F32);
102 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F16, DataType::F32);
103 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100104 ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && weights->info()->dimension(2) != input->info()->dimension(2));
Anthony Barbier871448e2017-03-24 14:54:29 +0000105 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4);
106
107 if(biases != nullptr)
108 {
109 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::F16, DataType::F32);
110 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100111 ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && biases->info()->dimension(0) != weights->info()->dimension(3));
Anthony Barbier871448e2017-03-24 14:54:29 +0000112 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
113 }
114
Anthony Barbierdbdab852017-06-23 15:42:00 +0100115 _has_bias = (biases != nullptr);
116 _are_weights_reshaped = weights_info.are_reshaped();
Anthony Barbier871448e2017-03-24 14:54:29 +0000117
118 // Get parameters for conv_info
Anthony Barbierdbdab852017-06-23 15:42:00 +0100119 unsigned int stride_x = 0;
120 unsigned int stride_y = 0;
121 unsigned int pad_x = 0;
122 unsigned int pad_y = 0;
Anthony Barbier871448e2017-03-24 14:54:29 +0000123 std::tie(stride_x, stride_y) = conv_info.stride();
124 std::tie(pad_x, pad_y) = conv_info.pad();
125
Anthony Barbier871448e2017-03-24 14:54:29 +0000126 // Get convolved dimensions
127 unsigned int conv_w = 0;
128 unsigned int conv_h = 0;
Anthony Barbierdbdab852017-06-23 15:42:00 +0100129
130 const unsigned int kernel_width = _are_weights_reshaped ? weights_info.kernel_size() : weights->info()->dimension(0);
131 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width,
Anthony Barbier871448e2017-03-24 14:54:29 +0000132 stride_x, stride_y, pad_x, pad_y, conv_info.round());
Anthony Barbiera4376382017-04-12 15:12:46 +0100133 ARM_COMPUTE_ERROR_ON_MSG((output->info()->dimension(0) != conv_w) || (output->info()->dimension(1) != conv_h), "Output shape does not match the expected one");
Anthony Barbier871448e2017-03-24 14:54:29 +0000134
Anthony Barbierdbdab852017-06-23 15:42:00 +0100135 // Check if its a "fully connected" convolution
136 _is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1));
137
Anthony Barbier871448e2017-03-24 14:54:29 +0000138 // Create tensor to store the reshaped weights
Anthony Barbierdbdab852017-06-23 15:42:00 +0100139 size_t mat_weights_cols = weights->info()->dimension(3);
140 size_t mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + ((_has_bias) ? 1 : 0);
141 if(_are_weights_reshaped)
142 {
143 mat_weights_cols = output->info()->dimension(2);
144 const unsigned int quarter_reshaped_cols = weights->info()->dimension(0) / 4;
145 mat_weights_rows = (_has_bias ? 1 + quarter_reshaped_cols : quarter_reshaped_cols);
146 }
147 else
148 {
149 if(_is_fully_connected_convolution)
150 {
151 // Create tensor to store the reshaped weights
152 TensorShape shape_wr(mat_weights_cols, mat_weights_rows);
153 TensorInfo info_wr(shape_wr, 1, weights->info()->data_type());
154 _weights_reshaped.allocator()->init(info_wr);
155 _reshape_weights.configure(weights, biases, &_weights_reshaped, false);
156 weights = &_weights_reshaped;
157 }
158 else
159 {
160 // Create tensor to store transposed weights
161 TensorShape shape_wt(mat_weights_rows * 4, static_cast<size_t>(std::ceil(mat_weights_cols / 4.f)));
162 TensorInfo info_wt(shape_wt, 1, weights->info()->data_type());
163 _weights_transposed.allocator()->init(info_wt);
164 _reshape_weights.configure(weights, biases, &_weights_transposed, true);
165 weights = &_weights_transposed;
166 }
167 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000168 // Create tensor to store im2col reshaped inputs
169 const size_t mat_input_cols = mat_weights_rows;
Anthony Barbierdbdab852017-06-23 15:42:00 +0100170 const size_t mat_input_rows = conv_w * conv_h;
Anthony Barbier871448e2017-03-24 14:54:29 +0000171 TensorShape shape_im2col = input->info()->tensor_shape();
172 shape_im2col.set(0, mat_input_cols);
173 shape_im2col.set(1, mat_input_rows);
174 shape_im2col.set(2, 1);
Anthony Barbier871448e2017-03-24 14:54:29 +0000175 _input_im2col_reshaped.allocator()->init(TensorInfo(shape_im2col, 1, input->info()->data_type()));
176
Anthony Barbierdbdab852017-06-23 15:42:00 +0100177 // Create tensor (interleave) to prepare input tensor for GEMM
178 if(!_is_fully_connected_convolution)
179 {
180 TensorShape shape_interleaved = shape_im2col;
181 shape_interleaved.set(0, shape_interleaved.x() * 4);
182 shape_interleaved.set(1, std::ceil(static_cast<float>(shape_interleaved.y()) / 4.f));
183 _input_interleaved_reshaped.allocator()->init(TensorInfo(shape_interleaved, 1, input->info()->data_type()));
184 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000185
186 // Create GEMM output tensor
187 TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape();
188 shape_gemm.set(0, mat_weights_cols);
189 shape_gemm.set(1, mat_input_rows);
190 _gemm_output.allocator()->init(TensorInfo(shape_gemm, 1, input->info()->data_type()));
191
192 // Configure kernels
193 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, std::make_pair(conv_w, conv_h), conv_info, _has_bias);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100194 _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h));
195
196 if(_is_fully_connected_convolution)
Anthony Barbier871448e2017-03-24 14:54:29 +0000197 {
Anthony Barbierdbdab852017-06-23 15:42:00 +0100198 _mm_kernel.configure(&_input_im2col_reshaped, weights, &_gemm_output, 1.0f);
Anthony Barbier871448e2017-03-24 14:54:29 +0000199 }
200 else
201 {
Anthony Barbierdbdab852017-06-23 15:42:00 +0100202 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
203 _mm_kernel.configure(&_input_interleaved_reshaped, weights, &_gemm_output, 1.0f);
Anthony Barbier871448e2017-03-24 14:54:29 +0000204 }
205
Anthony Barbierdbdab852017-06-23 15:42:00 +0100206 if(!_are_weights_reshaped)
207 {
208 if(!_is_fully_connected_convolution)
209 {
210 _weights_transposed.allocator()->allocate();
211 }
212 else
213 {
214 _weights_reshaped.allocator()->allocate();
215 }
216 }
217
Anthony Barbier871448e2017-03-24 14:54:29 +0000218 _input_im2col_reshaped.allocator()->allocate();
Anthony Barbierdbdab852017-06-23 15:42:00 +0100219 if(!_is_fully_connected_convolution)
220 {
221 _input_interleaved_reshaped.allocator()->allocate();
222 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000223 _gemm_output.allocator()->allocate();
224}
225
226void CLConvolutionLayer::run()
227{
228 // Run weights reshaping (Runs once for every configure)
Anthony Barbierdbdab852017-06-23 15:42:00 +0100229 if(!_are_weights_reshaped)
Anthony Barbier871448e2017-03-24 14:54:29 +0000230 {
Anthony Barbierdbdab852017-06-23 15:42:00 +0100231 _are_weights_reshaped = true;
232 _reshape_weights.run();
Anthony Barbier871448e2017-03-24 14:54:29 +0000233 }
234
235 // Run input reshaping
236 CLScheduler::get().enqueue(_input_im2col_kernel);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100237 if(!_is_fully_connected_convolution)
238 {
239 CLScheduler::get().enqueue(_input_interleave_kernel);
240 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000241
242 // Runs matrix multiply on reshaped matrices
243 CLScheduler::get().enqueue(_mm_kernel);
244
245 // Reshape output matrix
Anthony Barbierdbdab852017-06-23 15:42:00 +0100246 CLScheduler::get().enqueue(_output_col2im_kernel, false);
Anthony Barbier871448e2017-03-24 14:54:29 +0000247}