blob: b29bf8f1365c0b8f6c54867f958b821c71eb84d0 [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"
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010027#include "arm_compute/core/Size2D.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/Utils.h"
29#include "arm_compute/core/Validate.h"
30#include "arm_compute/runtime/CL/CLScheduler.h"
31
32#include <cmath>
33#include <tuple>
34
35using namespace arm_compute;
36
37CLConvolutionLayerReshapeWeights::CLConvolutionLayerReshapeWeights()
38 : _weights_reshape_kernel(), _weights_transposed_kernel(), _weights_reshaped(), _transpose1xW(false)
39{
40}
41
42void CLConvolutionLayerReshapeWeights::configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, bool transpose1xW)
43{
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010044 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::F16, DataType::F32);
45 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output);
46 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(weights, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4);
48
49 if(biases != nullptr)
50 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
52 ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(3));
53 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
54 }
55
56 const bool _has_bias = (biases != nullptr);
57
58 _transpose1xW = transpose1xW;
59
60 if(transpose1xW)
61 {
62 // Create tensor to store the reshaped weights
63 const unsigned int mat_weights_cols = weights->info()->dimension(3);
64 const unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + (_has_bias ? 1 : 0);
65 TensorShape shape_wr(mat_weights_cols, mat_weights_rows);
66 const DataType dt = weights->info()->data_type();
67 TensorInfo info_wr(shape_wr, 1, dt);
68
69 _weights_reshaped.allocator()->init(info_wr);
70 _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped);
71 _weights_transposed_kernel.configure(&_weights_reshaped, output);
72 _weights_reshaped.allocator()->allocate();
73 }
74 else
75 {
76 _weights_reshape_kernel.configure(weights, biases, output);
77 }
78}
79
80void CLConvolutionLayerReshapeWeights::run()
81{
82 cl::CommandQueue q = CLScheduler::get().queue();
83 CLScheduler::get().enqueue(_weights_reshape_kernel);
84 if(_transpose1xW)
85 {
86 CLScheduler::get().enqueue(_weights_transposed_kernel);
87 }
88}
89
90CLConvolutionLayer::CLConvolutionLayer()
91 : _reshape_weights(), _input_im2col_kernel(), _input_interleave_kernel(), _mm_kernel(), _output_col2im_kernel(), _input_im2col_reshaped(), _input_interleaved_reshaped(), _weights_reshaped(),
92 _weights_transposed(), _gemm_output(), _has_bias(false), _is_fully_connected_convolution(false), _are_weights_reshaped(false)
93{
94}
95
96void CLConvolutionLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info)
97{
98 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
100 ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && weights->info()->dimension(2) != input->info()->dimension(2));
101 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4);
102
103 if(biases != nullptr)
104 {
105 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::F16, DataType::F32);
106 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
107 ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && biases->info()->dimension(0) != weights->info()->dimension(3));
108 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
109 }
110
111 _has_bias = (biases != nullptr);
112 _are_weights_reshaped = weights_info.are_reshaped();
113
114 // Get parameters for conv_info
115 unsigned int stride_x = 0;
116 unsigned int stride_y = 0;
117 unsigned int pad_x = 0;
118 unsigned int pad_y = 0;
119 std::tie(stride_x, stride_y) = conv_info.stride();
120 std::tie(pad_x, pad_y) = conv_info.pad();
121
122 // Get convolved dimensions
123 unsigned int conv_w = 0;
124 unsigned int conv_h = 0;
125
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100126 const unsigned int kernel_width = _are_weights_reshaped ? weights_info.kernel_size().first : weights->info()->dimension(0);
127 const unsigned int kernel_height = _are_weights_reshaped ? weights_info.kernel_size().second : weights->info()->dimension(1);
128 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height,
129 conv_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130 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");
131
132 // Check if its a "fully connected" convolution
133 _is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1));
134
135 // Create tensor to store the reshaped weights
136 size_t mat_weights_cols = weights->info()->dimension(3);
137 size_t mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + ((_has_bias) ? 1 : 0);
138 if(_are_weights_reshaped)
139 {
140 mat_weights_cols = output->info()->dimension(2);
141 const unsigned int quarter_reshaped_cols = weights->info()->dimension(0) / 4;
142 mat_weights_rows = (_has_bias ? 1 + quarter_reshaped_cols : quarter_reshaped_cols);
143 }
144 else
145 {
146 if(_is_fully_connected_convolution)
147 {
148 // Create tensor to store the reshaped weights
149 TensorShape shape_wr(mat_weights_cols, mat_weights_rows);
150 TensorInfo info_wr(shape_wr, 1, weights->info()->data_type());
151 _weights_reshaped.allocator()->init(info_wr);
152 _reshape_weights.configure(weights, biases, &_weights_reshaped, false);
153 weights = &_weights_reshaped;
154 }
155 else
156 {
157 // Create tensor to store transposed weights
158 TensorShape shape_wt(mat_weights_rows * 4, static_cast<size_t>(std::ceil(mat_weights_cols / 4.f)));
159 TensorInfo info_wt(shape_wt, 1, weights->info()->data_type());
160 _weights_transposed.allocator()->init(info_wt);
161 _reshape_weights.configure(weights, biases, &_weights_transposed, true);
162 weights = &_weights_transposed;
163 }
164 }
165 // Create tensor to store im2col reshaped inputs
166 const size_t mat_input_cols = mat_weights_rows;
167 const size_t mat_input_rows = conv_w * conv_h;
168 TensorShape shape_im2col = input->info()->tensor_shape();
169 shape_im2col.set(0, mat_input_cols);
170 shape_im2col.set(1, mat_input_rows);
171 shape_im2col.set(2, 1);
172 _input_im2col_reshaped.allocator()->init(TensorInfo(shape_im2col, 1, input->info()->data_type()));
173
174 // Create tensor (interleave) to prepare input tensor for GEMM
175 if(!_is_fully_connected_convolution)
176 {
177 TensorShape shape_interleaved = shape_im2col;
178 shape_interleaved.set(0, shape_interleaved.x() * 4);
179 shape_interleaved.set(1, std::ceil(static_cast<float>(shape_interleaved.y()) / 4.f));
180 _input_interleaved_reshaped.allocator()->init(TensorInfo(shape_interleaved, 1, input->info()->data_type()));
181 }
182
183 // Create GEMM output tensor
184 TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape();
185 shape_gemm.set(0, mat_weights_cols);
186 shape_gemm.set(1, mat_input_rows);
187 _gemm_output.allocator()->init(TensorInfo(shape_gemm, 1, input->info()->data_type()));
188
189 // Configure kernels
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100190 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _has_bias);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191 _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h));
192
193 if(_is_fully_connected_convolution)
194 {
195 _mm_kernel.configure(&_input_im2col_reshaped, weights, &_gemm_output, 1.0f);
196 }
197 else
198 {
199 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
200 _mm_kernel.configure(&_input_interleaved_reshaped, weights, &_gemm_output, 1.0f);
201 }
202
203 if(!_are_weights_reshaped)
204 {
205 if(!_is_fully_connected_convolution)
206 {
207 _weights_transposed.allocator()->allocate();
208 }
209 else
210 {
211 _weights_reshaped.allocator()->allocate();
212 }
213 }
214
215 _input_im2col_reshaped.allocator()->allocate();
216 if(!_is_fully_connected_convolution)
217 {
218 _input_interleaved_reshaped.allocator()->allocate();
219 }
220 _gemm_output.allocator()->allocate();
221}
222
223void CLConvolutionLayer::run()
224{
225 // Run weights reshaping (Runs once for every configure)
226 if(!_are_weights_reshaped)
227 {
228 _are_weights_reshaped = true;
229 _reshape_weights.run();
230 }
231
232 // Run input reshaping
233 CLScheduler::get().enqueue(_input_im2col_kernel);
234 if(!_is_fully_connected_convolution)
235 {
236 CLScheduler::get().enqueue(_input_interleave_kernel);
237 }
238
239 // Runs matrix multiply on reshaped matrices
240 CLScheduler::get().enqueue(_mm_kernel);
241
242 // Reshape output matrix
243 CLScheduler::get().enqueue(_output_col2im_kernel, false);
244}