blob: 933d92bef7fcd31fa2ecf50a162efacb589244f1 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +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/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
36CLConvolutionLayerReshapeWeights::CLConvolutionLayerReshapeWeights()
37 : _weights_reshape_kernel(), _weights_transposed_kernel(), _weights_reshaped(), _transpose1xW(false)
38{
39}
40
41void 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)
99{
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);
104 ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && weights->info()->dimension(2) != input->info()->dimension(2));
105 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);
111 ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && biases->info()->dimension(0) != weights->info()->dimension(3));
112 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
113 }
114
115 _has_bias = (biases != nullptr);
116 _are_weights_reshaped = weights_info.are_reshaped();
117
118 // Get parameters for conv_info
119 unsigned int stride_x = 0;
120 unsigned int stride_y = 0;
121 unsigned int pad_x = 0;
122 unsigned int pad_y = 0;
123 std::tie(stride_x, stride_y) = conv_info.stride();
124 std::tie(pad_x, pad_y) = conv_info.pad();
125
126 // Get convolved dimensions
127 unsigned int conv_w = 0;
128 unsigned int conv_h = 0;
129
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100130 const unsigned int kernel_width = _are_weights_reshaped ? weights_info.kernel_size().first : weights->info()->dimension(0);
131 const unsigned int kernel_height = _are_weights_reshaped ? weights_info.kernel_size().second : weights->info()->dimension(1);
132 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height,
133 conv_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134 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");
135
136 // Check if its a "fully connected" convolution
137 _is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1));
138
139 // Create tensor to store the reshaped weights
140 size_t mat_weights_cols = weights->info()->dimension(3);
141 size_t mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + ((_has_bias) ? 1 : 0);
142 if(_are_weights_reshaped)
143 {
144 mat_weights_cols = output->info()->dimension(2);
145 const unsigned int quarter_reshaped_cols = weights->info()->dimension(0) / 4;
146 mat_weights_rows = (_has_bias ? 1 + quarter_reshaped_cols : quarter_reshaped_cols);
147 }
148 else
149 {
150 if(_is_fully_connected_convolution)
151 {
152 // Create tensor to store the reshaped weights
153 TensorShape shape_wr(mat_weights_cols, mat_weights_rows);
154 TensorInfo info_wr(shape_wr, 1, weights->info()->data_type());
155 _weights_reshaped.allocator()->init(info_wr);
156 _reshape_weights.configure(weights, biases, &_weights_reshaped, false);
157 weights = &_weights_reshaped;
158 }
159 else
160 {
161 // Create tensor to store transposed weights
162 TensorShape shape_wt(mat_weights_rows * 4, static_cast<size_t>(std::ceil(mat_weights_cols / 4.f)));
163 TensorInfo info_wt(shape_wt, 1, weights->info()->data_type());
164 _weights_transposed.allocator()->init(info_wt);
165 _reshape_weights.configure(weights, biases, &_weights_transposed, true);
166 weights = &_weights_transposed;
167 }
168 }
169 // Create tensor to store im2col reshaped inputs
170 const size_t mat_input_cols = mat_weights_rows;
171 const size_t mat_input_rows = conv_w * conv_h;
172 TensorShape shape_im2col = input->info()->tensor_shape();
173 shape_im2col.set(0, mat_input_cols);
174 shape_im2col.set(1, mat_input_rows);
175 shape_im2col.set(2, 1);
176 _input_im2col_reshaped.allocator()->init(TensorInfo(shape_im2col, 1, input->info()->data_type()));
177
178 // Create tensor (interleave) to prepare input tensor for GEMM
179 if(!_is_fully_connected_convolution)
180 {
181 TensorShape shape_interleaved = shape_im2col;
182 shape_interleaved.set(0, shape_interleaved.x() * 4);
183 shape_interleaved.set(1, std::ceil(static_cast<float>(shape_interleaved.y()) / 4.f));
184 _input_interleaved_reshaped.allocator()->init(TensorInfo(shape_interleaved, 1, input->info()->data_type()));
185 }
186
187 // Create GEMM output tensor
188 TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape();
189 shape_gemm.set(0, mat_weights_cols);
190 shape_gemm.set(1, mat_input_rows);
191 _gemm_output.allocator()->init(TensorInfo(shape_gemm, 1, input->info()->data_type()));
192
193 // Configure kernels
194 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, std::make_pair(conv_w, conv_h), conv_info, _has_bias);
195 _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h));
196
197 if(_is_fully_connected_convolution)
198 {
199 _mm_kernel.configure(&_input_im2col_reshaped, weights, &_gemm_output, 1.0f);
200 }
201 else
202 {
203 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
204 _mm_kernel.configure(&_input_interleaved_reshaped, weights, &_gemm_output, 1.0f);
205 }
206
207 if(!_are_weights_reshaped)
208 {
209 if(!_is_fully_connected_convolution)
210 {
211 _weights_transposed.allocator()->allocate();
212 }
213 else
214 {
215 _weights_reshaped.allocator()->allocate();
216 }
217 }
218
219 _input_im2col_reshaped.allocator()->allocate();
220 if(!_is_fully_connected_convolution)
221 {
222 _input_interleaved_reshaped.allocator()->allocate();
223 }
224 _gemm_output.allocator()->allocate();
225}
226
227void CLConvolutionLayer::run()
228{
229 // Run weights reshaping (Runs once for every configure)
230 if(!_are_weights_reshaped)
231 {
232 _are_weights_reshaped = true;
233 _reshape_weights.run();
234 }
235
236 // Run input reshaping
237 CLScheduler::get().enqueue(_input_im2col_kernel);
238 if(!_is_fully_connected_convolution)
239 {
240 CLScheduler::get().enqueue(_input_interleave_kernel);
241 }
242
243 // Runs matrix multiply on reshaped matrices
244 CLScheduler::get().enqueue(_mm_kernel);
245
246 // Reshape output matrix
247 CLScheduler::get().enqueue(_output_col2im_kernel, false);
248}