blob: 4b1bfd8b8fb9da9765d5360e0ac360aa3eaa6413 [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"
Kaizen8938bd32017-09-28 14:38:23 +010027#include "arm_compute/core/Size2D.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000028#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>
Kaizen8938bd32017-09-28 14:38:23 +010033#include <memory>
Anthony Barbier871448e2017-03-24 14:54:29 +000034#include <tuple>
35
36using namespace arm_compute;
37
Kaizen8938bd32017-09-28 14:38:23 +010038CLConvolutionLayerReshapeWeights::CLConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager)
39 : _memory_group(std::move(memory_manager)), _weights_reshape_kernel(), _weights_transposed_kernel(), _weights_reshaped(), _transpose1xW(false)
Anthony Barbier871448e2017-03-24 14:54:29 +000040{
41}
42
Anthony Barbierdbdab852017-06-23 15:42:00 +010043void CLConvolutionLayerReshapeWeights::configure(const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, bool transpose1xW)
44{
Kaizen8938bd32017-09-28 14:38:23 +010045 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
46 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, output);
47 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(weights, output);
Anthony Barbierdbdab852017-06-23 15:42:00 +010048 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4);
49
50 if(biases != nullptr)
51 {
Anthony Barbierdbdab852017-06-23 15:42:00 +010052 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
53 ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(3));
54 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
55 }
56
57 const bool _has_bias = (biases != nullptr);
58
59 _transpose1xW = transpose1xW;
60
61 if(transpose1xW)
62 {
63 // Create tensor to store the reshaped weights
64 const unsigned int mat_weights_cols = weights->info()->dimension(3);
65 const unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + (_has_bias ? 1 : 0);
66 TensorShape shape_wr(mat_weights_cols, mat_weights_rows);
Kaizen8938bd32017-09-28 14:38:23 +010067 const DataType dt = weights->info()->data_type();
68 const int fixed_point_position = weights->info()->fixed_point_position();
69 TensorInfo info_wr(shape_wr, 1, dt, fixed_point_position);
Anthony Barbierdbdab852017-06-23 15:42:00 +010070
71 _weights_reshaped.allocator()->init(info_wr);
Kaizen8938bd32017-09-28 14:38:23 +010072 _memory_group.manage(&_weights_reshaped);
Anthony Barbierdbdab852017-06-23 15:42:00 +010073 _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped);
74 _weights_transposed_kernel.configure(&_weights_reshaped, output);
75 _weights_reshaped.allocator()->allocate();
76 }
77 else
78 {
79 _weights_reshape_kernel.configure(weights, biases, output);
80 }
81}
82
83void CLConvolutionLayerReshapeWeights::run()
84{
Kaizen8938bd32017-09-28 14:38:23 +010085 _memory_group.acquire();
86
Anthony Barbierdbdab852017-06-23 15:42:00 +010087 cl::CommandQueue q = CLScheduler::get().queue();
88 CLScheduler::get().enqueue(_weights_reshape_kernel);
89 if(_transpose1xW)
90 {
91 CLScheduler::get().enqueue(_weights_transposed_kernel);
92 }
Kaizen8938bd32017-09-28 14:38:23 +010093
94 _memory_group.release();
Anthony Barbierdbdab852017-06-23 15:42:00 +010095}
96
Kaizen8938bd32017-09-28 14:38:23 +010097CLConvolutionLayer::CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
98 : _memory_group(std::move(memory_manager)), _reshape_weights(), _input_im2col_kernel(), _input_interleave_kernel(), _mm_kernel(), _output_col2im_kernel(), _input_im2col_reshaped(),
99 _input_interleaved_reshaped(), _weights_reshaped(), _weights_transposed(), _gemm_output(), _has_bias(false), _is_fully_connected_convolution(false), _are_weights_reshaped(false)
Anthony Barbierdbdab852017-06-23 15:42:00 +0100100{
101}
102
103void 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 +0000104{
Kaizen8938bd32017-09-28 14:38:23 +0100105 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
106 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
107 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, weights);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100108 ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && weights->info()->dimension(2) != input->info()->dimension(2));
Anthony Barbier871448e2017-03-24 14:54:29 +0000109 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4);
110
111 if(biases != nullptr)
112 {
Anthony Barbier871448e2017-03-24 14:54:29 +0000113 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
Kaizen8938bd32017-09-28 14:38:23 +0100114 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100115 ARM_COMPUTE_ERROR_ON(!weights_info.are_reshaped() && biases->info()->dimension(0) != weights->info()->dimension(3));
Anthony Barbier871448e2017-03-24 14:54:29 +0000116 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
117 }
118
Kaizen8938bd32017-09-28 14:38:23 +0100119 const DataType dt = input->info()->data_type();
120 const int fixed_point_position = input->info()->fixed_point_position();
121
122 // Set the GPU target for matrix multiply
123 _mm_kernel.set_target(CLScheduler::get().target());
124
Anthony Barbierdbdab852017-06-23 15:42:00 +0100125 _has_bias = (biases != nullptr);
126 _are_weights_reshaped = weights_info.are_reshaped();
Anthony Barbier871448e2017-03-24 14:54:29 +0000127
Kaizen8938bd32017-09-28 14:38:23 +0100128 // Get parameters from conv_info
Anthony Barbierdbdab852017-06-23 15:42:00 +0100129 unsigned int stride_x = 0;
130 unsigned int stride_y = 0;
131 unsigned int pad_x = 0;
132 unsigned int pad_y = 0;
Anthony Barbier871448e2017-03-24 14:54:29 +0000133 std::tie(stride_x, stride_y) = conv_info.stride();
134 std::tie(pad_x, pad_y) = conv_info.pad();
135
Anthony Barbier871448e2017-03-24 14:54:29 +0000136 // Get convolved dimensions
137 unsigned int conv_w = 0;
138 unsigned int conv_h = 0;
Anthony Barbierdbdab852017-06-23 15:42:00 +0100139
Kaizen8938bd32017-09-28 14:38:23 +0100140 const unsigned int kernel_width = (_are_weights_reshaped) ? weights_info.kernel_size().first : weights->info()->dimension(0);
141 const unsigned int kernel_height = (_are_weights_reshaped) ? weights_info.kernel_size().second : weights->info()->dimension(1);
142 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height,
143 conv_info);
Anthony Barbier871448e2017-03-24 14:54:29 +0000144
Anthony Barbierdbdab852017-06-23 15:42:00 +0100145 // Check if its a "fully connected" convolution
146 _is_fully_connected_convolution = ((conv_w == 1) && (conv_h == 1));
147
Kaizen8938bd32017-09-28 14:38:23 +0100148 unsigned int mat_weights_cols = weights->info()->dimension(3);
149 unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + (_has_bias ? 1 : 0);
150
151 // Reshape weights if needed
Anthony Barbierdbdab852017-06-23 15:42:00 +0100152 if(_are_weights_reshaped)
153 {
Kaizen8938bd32017-09-28 14:38:23 +0100154 mat_weights_cols = weights_info.num_kernels();
Anthony Barbierdbdab852017-06-23 15:42:00 +0100155 const unsigned int quarter_reshaped_cols = weights->info()->dimension(0) / 4;
156 mat_weights_rows = (_has_bias ? 1 + quarter_reshaped_cols : quarter_reshaped_cols);
157 }
158 else
159 {
160 if(_is_fully_connected_convolution)
161 {
162 // Create tensor to store the reshaped weights
163 TensorShape shape_wr(mat_weights_cols, mat_weights_rows);
Kaizen8938bd32017-09-28 14:38:23 +0100164 TensorInfo info_wr(shape_wr, 1, dt, fixed_point_position);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100165 _weights_reshaped.allocator()->init(info_wr);
Kaizen8938bd32017-09-28 14:38:23 +0100166 _reshape_weights.configure(weights, biases, &_weights_reshaped, false /* 1xW transpose */);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100167 }
168 else
169 {
170 // Create tensor to store transposed weights
Kaizen8938bd32017-09-28 14:38:23 +0100171 const float transpose_width = 16.0f / input->info()->element_size();
172 TensorShape shape_wt(mat_weights_rows * static_cast<unsigned int>(transpose_width), static_cast<unsigned int>(std::ceil(mat_weights_cols / transpose_width)));
173 TensorInfo info_wt(shape_wt, 1, dt, fixed_point_position);
174 _weights_reshaped.allocator()->init(info_wt);
175 _reshape_weights.configure(weights, biases, &_weights_reshaped, true /* 1xW transpose */);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100176 }
Kaizen8938bd32017-09-28 14:38:23 +0100177 weights = &_weights_reshaped;
Anthony Barbierdbdab852017-06-23 15:42:00 +0100178 }
Kaizen8938bd32017-09-28 14:38:23 +0100179
Anthony Barbier871448e2017-03-24 14:54:29 +0000180 // Create tensor to store im2col reshaped inputs
Kaizen8938bd32017-09-28 14:38:23 +0100181 const unsigned int mat_input_cols = mat_weights_rows;
182 const unsigned int mat_input_rows = conv_w * conv_h;
183 TensorShape shape_im2col = input->info()->tensor_shape();
Anthony Barbier871448e2017-03-24 14:54:29 +0000184 shape_im2col.set(0, mat_input_cols);
185 shape_im2col.set(1, mat_input_rows);
186 shape_im2col.set(2, 1);
Kaizen8938bd32017-09-28 14:38:23 +0100187 _input_im2col_reshaped.allocator()->init(TensorInfo(shape_im2col, 1, dt, fixed_point_position));
188 _memory_group.manage(&_input_im2col_reshaped);
Anthony Barbier871448e2017-03-24 14:54:29 +0000189
Anthony Barbierdbdab852017-06-23 15:42:00 +0100190 // Create tensor (interleave) to prepare input tensor for GEMM
191 if(!_is_fully_connected_convolution)
192 {
193 TensorShape shape_interleaved = shape_im2col;
194 shape_interleaved.set(0, shape_interleaved.x() * 4);
Kaizen8938bd32017-09-28 14:38:23 +0100195 shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f));
196 _input_interleaved_reshaped.allocator()->init(TensorInfo(shape_interleaved, 1, dt, fixed_point_position));
197 _memory_group.manage(&_input_interleaved_reshaped);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100198 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000199
200 // Create GEMM output tensor
201 TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape();
202 shape_gemm.set(0, mat_weights_cols);
203 shape_gemm.set(1, mat_input_rows);
Kaizen8938bd32017-09-28 14:38:23 +0100204 _gemm_output.allocator()->init(TensorInfo(shape_gemm, 1, dt, fixed_point_position));
205 _memory_group.manage(&_gemm_output);
Anthony Barbier871448e2017-03-24 14:54:29 +0000206
207 // Configure kernels
Kaizen8938bd32017-09-28 14:38:23 +0100208 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _has_bias);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100209
Kaizen8938bd32017-09-28 14:38:23 +0100210 // Configure matrix multiply
Anthony Barbierdbdab852017-06-23 15:42:00 +0100211 if(_is_fully_connected_convolution)
Anthony Barbier871448e2017-03-24 14:54:29 +0000212 {
Kaizen8938bd32017-09-28 14:38:23 +0100213 // The matrix A and Matrix B have not been reshaped
214 _mm_kernel.configure(&_input_im2col_reshaped, weights, &_gemm_output, 1.0f, false);
Anthony Barbier871448e2017-03-24 14:54:29 +0000215 }
216 else
217 {
Anthony Barbierdbdab852017-06-23 15:42:00 +0100218 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
219 _mm_kernel.configure(&_input_interleaved_reshaped, weights, &_gemm_output, 1.0f);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100220 _input_interleaved_reshaped.allocator()->allocate();
221 }
Kaizen8938bd32017-09-28 14:38:23 +0100222 _input_im2col_reshaped.allocator()->allocate();
223 _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h));
Anthony Barbier871448e2017-03-24 14:54:29 +0000224 _gemm_output.allocator()->allocate();
Kaizen8938bd32017-09-28 14:38:23 +0100225
226 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");
227
228 // Allocate intermediate tensor
229 if(!_are_weights_reshaped)
230 {
231 _weights_reshaped.allocator()->allocate();
232 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000233}
234
235void CLConvolutionLayer::run()
236{
237 // Run weights reshaping (Runs once for every configure)
Anthony Barbierdbdab852017-06-23 15:42:00 +0100238 if(!_are_weights_reshaped)
Anthony Barbier871448e2017-03-24 14:54:29 +0000239 {
Anthony Barbierdbdab852017-06-23 15:42:00 +0100240 _are_weights_reshaped = true;
241 _reshape_weights.run();
Anthony Barbier871448e2017-03-24 14:54:29 +0000242 }
243
Kaizen8938bd32017-09-28 14:38:23 +0100244 _memory_group.acquire();
245
Anthony Barbier871448e2017-03-24 14:54:29 +0000246 // Run input reshaping
247 CLScheduler::get().enqueue(_input_im2col_kernel);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100248 if(!_is_fully_connected_convolution)
249 {
250 CLScheduler::get().enqueue(_input_interleave_kernel);
251 }
Anthony Barbier871448e2017-03-24 14:54:29 +0000252
253 // Runs matrix multiply on reshaped matrices
254 CLScheduler::get().enqueue(_mm_kernel);
255
256 // Reshape output matrix
Anthony Barbierdbdab852017-06-23 15:42:00 +0100257 CLScheduler::get().enqueue(_output_col2im_kernel, false);
Kaizen8938bd32017-09-28 14:38:23 +0100258
259 _memory_group.release();
Anthony Barbier871448e2017-03-24 14:54:29 +0000260}