blob: 89ec9009421532b0a8041219d54cdeafb8bbc166 [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/NEON/functions/NEConvolutionLayer.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
36NEConvolutionLayer::NEConvolutionLayer()
37 : _input_im2col_kernel(), _input_interleave_kernel(), _weights_reshape_kernel(), _weights_transposed_kernel(), _mm_kernel(), _output_col2im_kernel(), _input_im2col_reshaped(),
38 _input_interleaved_reshaped(), _weights_reshaped(), _weights_transposed(), _gemm_output(), _is_first_run(false), _has_bias(false), _is_fc(false)
39{
40}
41
42void NEConvolutionLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, const PadStrideInfo &conv_info)
43{
44 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
45 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::F32);
46 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F32);
47 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
48 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(2) != input->info()->dimension(2));
49 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4);
50
51 if(biases != nullptr)
52 {
53 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(biases, 1, DataType::F32);
54 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
55 ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(3));
56 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
57 }
58
59 _has_bias = (biases != nullptr);
60 _is_first_run = true;
61
62 // Get parameters for conv_info
63 unsigned int stride_x, stride_y, pad_x, pad_y = 0;
64 std::tie(stride_x, stride_y) = conv_info.stride();
65 std::tie(pad_x, pad_y) = conv_info.pad();
66
67 bool is_same_dimension = true;
68 // Make sure the input and weights have same low three dimensions
69 for(int i = 0; i < 3; i++)
70 {
71 is_same_dimension = (is_same_dimension) && (input->info()->dimension(i) == weights->info()->dimension(i));
72 }
73
74 // Run the fully connected path if is_same_dimension is true and conv_stride_x/conv_stride_y are 1, and conv_pad_x/conv_pad_y are 0 and skip col2im
75 _is_fc = (is_same_dimension) && ((stride_x & stride_y) == 1) && ((pad_x | pad_y) == 0);
76
77 // Get convolved dimensions
78 unsigned int conv_w = 0;
79 unsigned int conv_h = 0;
80
81 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), weights->info()->dimension(0),
82 stride_x, stride_y, pad_x, pad_y, conv_info.round());
83
84 // Create tensor to store the reshaped weights
85 const size_t mat_weights_cols = weights->info()->dimension(3);
86 const size_t mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + ((_has_bias) ? 1 : 0);
87 const TensorShape shape_wr(mat_weights_cols, mat_weights_rows);
88 TensorInfo info_wr(shape_wr, 1, weights->info()->data_type());
89 _weights_reshaped.allocator()->init(info_wr);
90
91 // Create tensor to store transposed weights
92 TensorShape shape_wt(mat_weights_rows * 4, static_cast<size_t>(std::ceil(mat_weights_cols / 4.f)));
93 TensorInfo info_wt(shape_wt, 1, weights->info()->data_type());
94 _weights_transposed.allocator()->init(info_wt);
95
96 // Create tensor to store im2col reshaped inputs
97 const size_t mat_input_cols = mat_weights_rows;
98 const size_t mat_input_rows = _is_fc ? (input->info()->dimension(3)) : (conv_w * conv_h);
99 TensorShape shape_im2col = input->info()->tensor_shape();
100 shape_im2col.set(0, mat_input_cols);
101 shape_im2col.set(1, mat_input_rows);
102 shape_im2col.set(2, 1);
103 if(_is_fc)
104 {
105 shape_im2col.set(3, 1);
106 }
107 TensorInfo info_im2col(shape_im2col, 1, input->info()->data_type());
108 _input_im2col_reshaped.allocator()->init(info_im2col);
109
110 // Create tensor to prepare input tensor for GEMM
111 TensorShape shape_interleaved = shape_im2col;
112 shape_interleaved.set(0, shape_interleaved.x() * 4);
113 shape_interleaved.set(1, std::ceil(static_cast<float>(shape_interleaved.y()) / 4));
114 TensorInfo info_interleaved(shape_interleaved, 1, input->info()->data_type());
115 _input_interleaved_reshaped.allocator()->init(info_interleaved);
116
117 // Create GEMM output tensor
118 TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape();
119 shape_gemm.set(0, mat_weights_cols);
120 shape_gemm.set(1, mat_input_rows);
121 TensorInfo info_gemm(shape_gemm, 1, input->info()->data_type());
122 _gemm_output.allocator()->init(info_gemm);
123
124 // Configure kernels
125 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, std::make_pair(conv_w, conv_h), conv_info, _has_bias);
126 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
127 _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped);
128 _weights_transposed_kernel.configure(&_weights_reshaped, &_weights_transposed);
129
130 if(_is_fc)
131 {
132 _mm_kernel.configure(&_input_interleaved_reshaped, &_weights_transposed, output, 1.0f);
133 }
134 else
135 {
136 _mm_kernel.configure(&_input_interleaved_reshaped, &_weights_transposed, &_gemm_output, 1.0f);
137 _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h));
138 }
139
140 // Allocate the tensors once the all configure methods have been called
141 _weights_reshaped.allocator()->allocate();
142 _weights_transposed.allocator()->allocate();
143 _input_im2col_reshaped.allocator()->allocate();
144 _input_interleaved_reshaped.allocator()->allocate();
145 _gemm_output.allocator()->allocate();
146}
147
148void NEConvolutionLayer::run()
149{
150 // Run weights reshaping (Runs once for every configure)
151 if(_is_first_run)
152 {
153 _is_first_run = false;
154 NEScheduler::get().multithread(&_weights_reshape_kernel, 3);
155 NEScheduler::get().multithread(&_weights_transposed_kernel);
156 }
157
158 // Run input reshaping
159 NEScheduler::get().multithread(&_input_im2col_kernel);
160
161 // Run interleave
162 NEScheduler::get().multithread(&_input_interleave_kernel);
163
164 // Runs GEMM on reshaped matrices
165 NEScheduler::get().multithread(&_mm_kernel);
166
167 // Reshape output matrix
168 if(!_is_fc)
169 {
170 NEScheduler::get().multithread(&_output_col2im_kernel);
171 }
172}