blob: 973559441f5d86e1e293aa4673ff29404afe974c [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Anthony Barbier21f67d62018-02-16 15:17:48 +00002 * Copyright (c) 2017-2018 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/NEON/functions/NELocallyConnectedLayer.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/NEON/NEScheduler.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;
51 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(0), input->dimension(1), kernel_width, kernel_height,
52 conv_info);
53
54 const size_t mat_weights_cols = weights->dimension(3);
55 const size_t mat_weights_rows = weights->dimension(0) * weights->dimension(1) * weights->dimension(2) + ((has_bias) ? 1 : 0);
56 const size_t mat_weights_num = weights->dimension(4);
57
58 shape_wr = TensorShape(mat_weights_cols, mat_weights_rows, mat_weights_num);
59
60 const size_t mat_input_cols = mat_weights_rows;
61 const size_t mat_input_rows = conv_w * conv_h;
62
63 shape_im2col = input->tensor_shape();
64 shape_im2col.set(0, mat_input_cols);
65 shape_im2col.set(1, mat_input_rows);
66 shape_im2col.set(2, 1);
67
68 shape_gemm = shape_im2col;
69 shape_gemm.set(0, mat_weights_cols);
70 shape_gemm.set(1, mat_input_rows);
71}
72} // namespace
73
Georgios Pinitas658039b2017-09-15 16:30:50 +010074NELocallyConnectedLayer::NELocallyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager)
75 : _memory_group(std::move(memory_manager)), _input_im2col_kernel(), _weights_reshape_kernel(), _mm_kernel(), _output_col2im_kernel(), _input_im2col_reshaped(), _weights_reshaped(), _gemm_output(),
76 _is_first_run(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077{
78}
79
Alex Gilday27c08ab2018-02-22 11:36:16 +000080Status NELocallyConnectedLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info)
81{
82 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, weights, output);
83 ARM_COMPUTE_RETURN_ERROR_ON(weights->dimension(2) != input->dimension(2));
84 ARM_COMPUTE_RETURN_ERROR_ON(!conv_info.padding_is_symmetric());
85
86 bool has_bias = (biases != nullptr);
87
88 if(has_bias)
89 {
90 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != weights->dimension(3));
91 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 2);
92 }
93
94 const unsigned int kernel_width = weights->dimension(0);
95 const unsigned int kernel_height = weights->dimension(1);
96
97 // Get convolved dimensions
98 unsigned int conv_w = 0;
99 unsigned int conv_h = 0;
100 std::tie(conv_w, conv_h) = scaled_dimensions(input->dimension(0), input->dimension(1), kernel_width, kernel_height,
101 conv_info);
102
103 ARM_COMPUTE_RETURN_ERROR_ON_MSG((output->dimension(0) != conv_w) || (output->dimension(1) != conv_h), "Output shape does not match the expected one");
104 ARM_COMPUTE_RETURN_ERROR_ON_MSG(weights->dimension(4) != (conv_w * conv_h), "Weights shape does not match the expected one");
105
106 // Calculate intermediate buffer shapes
107 TensorShape shape_wr;
108 TensorShape shape_im2col;
109 TensorShape shape_gemm;
110 calculate_shapes(input, weights, biases, output, conv_info, shape_wr, shape_im2col, shape_gemm);
111
112 TensorInfo weights_reshaped_info(shape_wr, 1, weights->data_type());
113 TensorInfo input_im2col_reshaped_info(shape_im2col, 1, input->data_type());
114 TensorInfo gemm_output_info(shape_gemm, 1, input->data_type());
115
116 ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, &input_im2col_reshaped_info, Size2D(kernel_width, kernel_height), conv_info, has_bias, false));
117 ARM_COMPUTE_RETURN_ON_ERROR(NEWeightsReshapeKernel::validate(weights, biases, &weights_reshaped_info));
118 ARM_COMPUTE_RETURN_ON_ERROR(NELocallyConnectedMatrixMultiplyKernel::validate(&input_im2col_reshaped_info, &weights_reshaped_info, &gemm_output_info));
119 ARM_COMPUTE_RETURN_ON_ERROR(NECol2ImKernel::validate(&gemm_output_info, output, Size2D(conv_w, conv_h)));
120
121 return Status{};
122}
123
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124void NELocallyConnectedLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info)
125{
Alex Gilday27c08ab2018-02-22 11:36:16 +0000126 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
127 ARM_COMPUTE_ERROR_THROW_ON(NELocallyConnectedLayer::validate(input->info(), weights->info(), biases == nullptr ? nullptr : biases->info(), output->info(), conv_info));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128
129 bool _has_bias = (biases != nullptr);
130 _is_first_run = true;
131
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100132 const unsigned int kernel_width = weights->info()->dimension(0);
133 const unsigned int kernel_height = weights->info()->dimension(1);
134
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135 // Get convolved dimensions
136 unsigned int conv_w = 0;
137 unsigned int conv_h = 0;
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100138 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 +0100139 conv_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140
Alex Gilday27c08ab2018-02-22 11:36:16 +0000141 // Calculate intermediate buffer shapes
142 TensorShape shape_wr;
143 TensorShape shape_im2col;
144 TensorShape shape_gemm;
145 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 +0100146
147 _weights_reshaped.allocator()->init(TensorInfo(shape_wr, 1, weights->info()->data_type()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148 _input_im2col_reshaped.allocator()->init(TensorInfo(shape_im2col, 1, input->info()->data_type()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 _gemm_output.allocator()->init(TensorInfo(shape_gemm, 1, input->info()->data_type()));
150
Georgios Pinitas658039b2017-09-15 16:30:50 +0100151 // Manage intermediate buffers
152 _memory_group.manage(&_input_im2col_reshaped);
153 _memory_group.manage(&_gemm_output);
154
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 // Configure kernels
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100156 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, _has_bias);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157 _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped);
158 _mm_kernel.configure(&_input_im2col_reshaped, &_weights_reshaped, &_gemm_output);
Georgios Pinitasd912fd82017-11-27 21:00:13 +0000159 _output_col2im_kernel.configure(&_gemm_output, output, Size2D(conv_w, conv_h));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160
161 // Allocate intermediate tensors
162 _weights_reshaped.allocator()->allocate();
163 _input_im2col_reshaped.allocator()->allocate();
164 _gemm_output.allocator()->allocate();
165}
166
167void NELocallyConnectedLayer::run()
168{
169 // Run weights reshaping (Runs once for every configure)
170 if(_is_first_run)
171 {
172 _is_first_run = false;
173 NEScheduler::get().schedule(&_weights_reshape_kernel, 3);
174 }
175
Georgios Pinitas658039b2017-09-15 16:30:50 +0100176 _memory_group.acquire();
177
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178 // Run input reshaping
179 NEScheduler::get().schedule(&_input_im2col_kernel, Window::DimY);
180
181 // Runs GEMM on reshaped matrices
182 NEScheduler::get().schedule(&_mm_kernel, Window::DimX);
183
184 // Reshape output matrix
185 NEScheduler::get().schedule(&_output_col2im_kernel, Window::DimY);
Georgios Pinitas658039b2017-09-15 16:30:50 +0100186
187 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188}