blob: d501985aef07f315f18787c5f7a9493e36cf19a7 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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/CLLocallyConnectedLayer.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
Alex Gilday27c08ab2018-02-22 11:36:16 +000036namespace
37{
38void calculate_shapes(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info,
39 TensorShape &shape_wr, TensorShape &shape_im2col, TensorShape &shape_gemm)
40{
41 ARM_COMPUTE_UNUSED(output);
42
43 const unsigned int kernel_width = weights->dimension(0);
44 const unsigned int kernel_height = weights->dimension(1);
45
46 bool has_bias = (biases != nullptr);
47
48 // Get convolved dimensions
49 unsigned int conv_w = 0;
50 unsigned int conv_h = 0;
Georgios Pinitas19ea4192018-06-19 13:09:53 +010051 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(0),
52 input->dimension(1),
53 kernel_width,
54 kernel_height,
Alex Gilday27c08ab2018-02-22 11:36:16 +000055 conv_info);
56
57 const size_t mat_weights_cols = weights->dimension(3);
58 const size_t mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + ((has_bias) ? 1 : 0);
59 const size_t mat_weights_num = weights->dimension(4);
60
61 shape_wr = TensorShape(mat_weights_cols, mat_weights_rows, mat_weights_num);
62
63 const size_t mat_input_cols = mat_weights_rows;
64 const size_t mat_input_rows = conv_w * conv_h;
65
66 shape_im2col = input->tensor_shape();
Georgios Pinitas19ea4192018-06-19 13:09:53 +010067 if(shape_im2col.num_dimensions() >= 3)
68 {
69 shape_im2col.remove_dimension(2);
70 }
Alex Gilday27c08ab2018-02-22 11:36:16 +000071 shape_im2col.set(0, mat_input_cols);
72 shape_im2col.set(1, mat_input_rows);
Alex Gilday27c08ab2018-02-22 11:36:16 +000073
74 shape_gemm = shape_im2col;
75 shape_gemm.set(0, mat_weights_cols);
76 shape_gemm.set(1, mat_input_rows);
77}
78} // namespace
79
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010080CLLocallyConnectedLayer::CLLocallyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager)
81 : _memory_group(std::move(memory_manager)), _input_im2col_kernel(), _weights_reshape_kernel(), _mm_kernel(), _output_col2im_kernel(), _input_im2col_reshaped(), _weights_reshaped(), _gemm_output(),
Georgios Pinitas72219332018-06-05 14:56:06 +010082 _is_prepared(false), _original_weights(nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083{
84}
85
Alex Gilday27c08ab2018-02-22 11:36:16 +000086Status CLLocallyConnectedLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info)
87{
88 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
89 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(2) != input->dimension(2));
90 ARM_COMPUTE_RETURN_ERROR_ON(!conv_info.padding_is_symmetric());
91
92 bool has_bias = (biases != nullptr);
93
94 if(has_bias)
95 {
96 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3));
97 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 2);
98 }
99
100 const unsigned int kernel_width = weights->dimension(0);
101 const unsigned int kernel_height = weights->dimension(1);
102
103 // Get convolved dimensions
104 unsigned int conv_w = 0;
105 unsigned int conv_h = 0;
106 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(0), input->dimension(1), kernel_width, kernel_height,
107 conv_info);
108
109 ARM_COMPUTE_RETURN_ERROR_ON_MSG((output->dimension(0) != conv_w) || (output->dimension(1) != conv_h), "Output shape does not match the expected one");
110 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(4) != (conv_w * conv_h), "Weights shape does not match the expected one");
111
112 // Calculate intermediate buffer shapes
113 TensorShape shape_wr;
114 TensorShape shape_im2col;
115 TensorShape shape_gemm;
116 calculate_shapes(input, weights, biases, output, conv_info, shape_wr, shape_im2col, shape_gemm);
117
118 TensorInfo weights_reshaped_info(shape_wr, 1, weights->data_type());
119 TensorInfo input_im2col_reshaped_info(shape_im2col, 1, input->data_type());
120 TensorInfo gemm_output_info(shape_gemm, 1, input->data_type());
121
122 ARM_COMPUTE_RETURN_ON_ERROR(CLIm2ColKernel::validate(input, &input_im2col_reshaped_info, Size2D(kernel_width, kernel_height), conv_info, has_bias));
123 ARM_COMPUTE_RETURN_ON_ERROR(CLWeightsReshapeKernel::validate(weights, biases, &weights_reshaped_info));
124 ARM_COMPUTE_RETURN_ON_ERROR(CLLocallyConnectedMatrixMultiplyKernel::validate(&input_im2col_reshaped_info, &weights_reshaped_info, &gemm_output_info));
Giorgio Arena226e4b92018-08-23 12:00:02 +0100125 ARM_COMPUTE_RETURN_ON_ERROR(CLCol2ImKernel::validate(&gemm_output_info, output, Size2D(conv_w, conv_h)));
Alex Gilday27c08ab2018-02-22 11:36:16 +0000126
127 return Status{};
128}
129
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130void CLLocallyConnectedLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info)
131{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100132 configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info);
133}
134
135void CLLocallyConnectedLayer::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
136 const PadStrideInfo &conv_info)
137{
Alex Gilday27c08ab2018-02-22 11:36:16 +0000138 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
139 ARM_COMPUTE_ERROR_THROW_ON(CLLocallyConnectedLayer::validate(input->info(), weights->info(), biases == nullptr ? nullptr : biases->info(), output->info(), conv_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140
Georgios Pinitas1562be32018-03-08 19:09:19 +0000141 bool _has_bias = (biases != nullptr);
142 _original_weights = weights;
Georgios Pinitas72219332018-06-05 14:56:06 +0100143 _is_prepared = false;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144
Sanghoon Leef47bfb92018-01-23 15:16:47 +0000145 const unsigned int kernel_width = weights->info()->dimension(0);
146 const unsigned int kernel_height = weights->info()->dimension(1);
147
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148 // Get convolved dimensions
149 unsigned int conv_w = 0;
150 unsigned int conv_h = 0;
Sanghoon Leef47bfb92018-01-23 15:16:47 +0000151 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height,
Gian Marco Iodice4e288692017-06-27 11:41:59 +0100152 conv_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153
Alex Gilday27c08ab2018-02-22 11:36:16 +0000154 // Calculate intermediate buffer shapes
155 TensorShape shape_wr;
156 TensorShape shape_im2col;
157 TensorShape shape_gemm;
158 calculate_shapes(input->info(), weights->info(), biases == nullptr ? nullptr : biases->info(), output->info(), conv_info, shape_wr, shape_im2col, shape_gemm);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159
160 _weights_reshaped.allocator()->init(TensorInfo(shape_wr, 1, weights->info()->data_type()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161 _input_im2col_reshaped.allocator()->init(TensorInfo(shape_im2col, 1, input->info()->data_type()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162 _gemm_output.allocator()->init(TensorInfo(shape_gemm, 1, input->info()->data_type()));
163
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100164 // Manage intermediate buffers
165 _memory_group.manage(&_input_im2col_reshaped);
166 _memory_group.manage(&_gemm_output);
167
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168 // Configure kernels
Manuel Bottini2b84be52020-04-08 10:15:51 +0100169 _input_im2col_kernel.configure(compile_context, input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _has_bias);
170 _weights_reshape_kernel.configure(compile_context, weights, biases, &_weights_reshaped);
171 _mm_kernel.configure(compile_context, &_input_im2col_reshaped, &_weights_reshaped, &_gemm_output);
172 _output_col2im_kernel.configure(compile_context, &_gemm_output, output, Size2D(conv_w, conv_h));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173
174 // Allocate intermediate tensors
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100175 _input_im2col_reshaped.allocator()->allocate();
176 _gemm_output.allocator()->allocate();
Georgios Pinitas17812ba2018-06-04 19:27:13 +0100177
178 CLScheduler::get().tune_kernel_static(_input_im2col_kernel);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179}
180
181void CLLocallyConnectedLayer::run()
182{
Georgios Pinitas72219332018-06-05 14:56:06 +0100183 prepare();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184
Georgios Pinitasda953f22019-04-02 17:27:03 +0100185 MemoryGroupResourceScope scope_mg(_memory_group);
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100186
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187 // Run input reshaping
188 CLScheduler::get().enqueue(_input_im2col_kernel);
189
190 // Runs vector matrix multiply on reshaped matrices
191 CLScheduler::get().enqueue(_mm_kernel);
192
193 // Reshape output matrix
194 CLScheduler::get().enqueue(_output_col2im_kernel, false);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100195}
Georgios Pinitas72219332018-06-05 14:56:06 +0100196
197void CLLocallyConnectedLayer::prepare()
198{
199 if(!_is_prepared)
200 {
201 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
202
203 // Run weights reshaping and mark original weights tensor as unused
204 _weights_reshaped.allocator()->allocate();
205 CLScheduler::get().enqueue(_weights_reshape_kernel);
206 _original_weights->mark_as_unused();
207
208 CLScheduler::get().queue().finish();
209 _is_prepared = true;
210 }
211}