blob: 0bbec94e78d910a80f183b1e198d9c70261ad57a [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 Iodice7d323a62017-07-05 20:05:23 +010044 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010045 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);
Gian Marco Iodice368da832017-07-03 12:33:49 +010066 const DataType dt = weights->info()->data_type();
67 const int fixed_point_position = weights->info()->fixed_point_position();
68 TensorInfo info_wr(shape_wr, 1, dt, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069
70 _weights_reshaped.allocator()->init(info_wr);
71 _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped);
72 _weights_transposed_kernel.configure(&_weights_reshaped, output);
73 _weights_reshaped.allocator()->allocate();
74 }
75 else
76 {
77 _weights_reshape_kernel.configure(weights, biases, output);
78 }
79}
80
81void CLConvolutionLayerReshapeWeights::run()
82{
83 cl::CommandQueue q = CLScheduler::get().queue();
84 CLScheduler::get().enqueue(_weights_reshape_kernel);
85 if(_transpose1xW)
86 {
87 CLScheduler::get().enqueue(_weights_transposed_kernel);
88 }
89}
90
91CLConvolutionLayer::CLConvolutionLayer()
92 : _reshape_weights(), _input_im2col_kernel(), _input_interleave_kernel(), _mm_kernel(), _output_col2im_kernel(), _input_im2col_reshaped(), _input_interleaved_reshaped(), _weights_reshaped(),
93 _weights_transposed(), _gemm_output(), _has_bias(false), _is_fully_connected_convolution(false), _are_weights_reshaped(false)
94{
95}
96
97void CLConvolutionLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info)
98{
Gian Marco Iodice7d323a62017-07-05 20:05:23 +010099 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100100 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
101 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102 ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && weights->info()->dimension(2) != input->info()->dimension(2));
103 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4);
104
105 if(biases != nullptr)
106 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
Gian Marco Iodice368da832017-07-03 12:33:49 +0100108 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109 ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && biases->info()->dimension(0) != weights->info()->dimension(3));
110 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
111 }
112
Gian Marco Iodice368da832017-07-03 12:33:49 +0100113 const DataType dt = input->info()->data_type();
114 const int fixed_point_position = input->info()->fixed_point_position();
115
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100116 // Set the GPU target for matrix multiply
117 _mm_kernel.set_target(CLScheduler::get().target());
118
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119 _has_bias = (biases != nullptr);
120 _are_weights_reshaped = weights_info.are_reshaped();
121
Gian Marco Iodice368da832017-07-03 12:33:49 +0100122 // Get parameters from conv_info
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123 unsigned int stride_x = 0;
124 unsigned int stride_y = 0;
125 unsigned int pad_x = 0;
126 unsigned int pad_y = 0;
127 std::tie(stride_x, stride_y) = conv_info.stride();
128 std::tie(pad_x, pad_y) = conv_info.pad();
129
130 // Get convolved dimensions
131 unsigned int conv_w = 0;
132 unsigned int conv_h = 0;
133
Gian Marco Iodice368da832017-07-03 12:33:49 +0100134 const unsigned int kernel_width = (_are_weights_reshaped) ? weights_info.kernel_size().first : weights->info()->dimension(0);
135 const unsigned int kernel_height = (_are_weights_reshaped) ? weights_info.kernel_size().second : weights->info()->dimension(1);
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100136 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height,
137 conv_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138
139 // Check if its a "fully connected" convolution
140 _is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1));
141
Gian Marco Iodice368da832017-07-03 12:33:49 +0100142 unsigned int mat_weights_cols = weights->info()->dimension(3);
143 unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + (_has_bias ? 1 : 0);
144
145 // Reshape weights if needed
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146 if(_are_weights_reshaped)
147 {
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100148 mat_weights_cols = weights_info.num_kernels();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 const unsigned int quarter_reshaped_cols = weights->info()->dimension(0) / 4;
150 mat_weights_rows = (_has_bias ? 1 + quarter_reshaped_cols : quarter_reshaped_cols);
151 }
152 else
153 {
154 if(_is_fully_connected_convolution)
155 {
156 // Create tensor to store the reshaped weights
157 TensorShape shape_wr(mat_weights_cols, mat_weights_rows);
Gian Marco Iodice368da832017-07-03 12:33:49 +0100158 TensorInfo info_wr(shape_wr, 1, dt, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159 _weights_reshaped.allocator()->init(info_wr);
Gian Marco Iodice368da832017-07-03 12:33:49 +0100160 _reshape_weights.configure(weights, biases, &_weights_reshaped, false /* 1xW transpose */);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161 }
162 else
163 {
164 // Create tensor to store transposed weights
Gian Marco Iodice368da832017-07-03 12:33:49 +0100165 const float transpose_width = 16.0f / input->info()->element_size();
166 TensorShape shape_wt(mat_weights_rows * static_cast<unsigned int>(transpose_width), static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)));
167 TensorInfo info_wt(shape_wt, 1, dt, fixed_point_position);
168 _weights_reshaped.allocator()->init(info_wt);
169 _reshape_weights.configure(weights, biases, &_weights_reshaped, true /* 1xW transpose */);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170 }
Gian Marco Iodice368da832017-07-03 12:33:49 +0100171 weights = &_weights_reshaped;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172 }
Gian Marco Iodice368da832017-07-03 12:33:49 +0100173
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174 // Create tensor to store im2col reshaped inputs
Gian Marco Iodice368da832017-07-03 12:33:49 +0100175 const unsigned int mat_input_cols = mat_weights_rows;
176 const unsigned int mat_input_rows = conv_w * conv_h;
177 TensorShape shape_im2col = input->info()->tensor_shape();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178 shape_im2col.set(0, mat_input_cols);
179 shape_im2col.set(1, mat_input_rows);
180 shape_im2col.set(2, 1);
Gian Marco Iodice368da832017-07-03 12:33:49 +0100181 _input_im2col_reshaped.allocator()->init(TensorInfo(shape_im2col, 1, dt, fixed_point_position));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182
183 // Create tensor (interleave) to prepare input tensor for GEMM
184 if(!_is_fully_connected_convolution)
185 {
186 TensorShape shape_interleaved = shape_im2col;
187 shape_interleaved.set(0, shape_interleaved.x() * 4);
Gian Marco Iodice368da832017-07-03 12:33:49 +0100188 shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f));
189 _input_interleaved_reshaped.allocator()->init(TensorInfo(shape_interleaved, 1, dt, fixed_point_position));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190 }
191
192 // Create GEMM output tensor
193 TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape();
194 shape_gemm.set(0, mat_weights_cols);
195 shape_gemm.set(1, mat_input_rows);
Gian Marco Iodice368da832017-07-03 12:33:49 +0100196 _gemm_output.allocator()->init(TensorInfo(shape_gemm, 1, dt, fixed_point_position));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100197
198 // Configure kernels
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100199 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _has_bias);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100200
201 // Configure matrix multiply
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100202 if(_is_fully_connected_convolution)
203 {
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100204 // The matrix A and Matrix B have not been reshaped
205 _mm_kernel.configure(&_input_im2col_reshaped, weights, &_gemm_output, 1.0f, false);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100206 }
207 else
208 {
209 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
210 _mm_kernel.configure(&_input_interleaved_reshaped, weights, &_gemm_output, 1.0f);
211 }
Gian Marco Iodice368da832017-07-03 12:33:49 +0100212 _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100213
Gian Marco Iodice559d7712017-08-08 08:38:09 +0100214 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");
215
Gian Marco Iodice368da832017-07-03 12:33:49 +0100216 // Allocate intermediate tensor
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100217 if(!_are_weights_reshaped)
218 {
Gian Marco Iodice368da832017-07-03 12:33:49 +0100219 _weights_reshaped.allocator()->allocate();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100220 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221 _input_im2col_reshaped.allocator()->allocate();
222 if(!_is_fully_connected_convolution)
223 {
224 _input_interleaved_reshaped.allocator()->allocate();
225 }
226 _gemm_output.allocator()->allocate();
227}
228
229void CLConvolutionLayer::run()
230{
231 // Run weights reshaping (Runs once for every configure)
232 if(!_are_weights_reshaped)
233 {
234 _are_weights_reshaped = true;
235 _reshape_weights.run();
236 }
237
238 // Run input reshaping
239 CLScheduler::get().enqueue(_input_im2col_kernel);
240 if(!_is_fully_connected_convolution)
241 {
242 CLScheduler::get().enqueue(_input_interleave_kernel);
243 }
244
245 // Runs matrix multiply on reshaped matrices
246 CLScheduler::get().enqueue(_mm_kernel);
247
248 // Reshape output matrix
249 CLScheduler::get().enqueue(_output_col2im_kernel, false);
250}