arm_compute v17.04
[platform/upstream/armcl.git] / src / runtime / NEON / functions / NEConvolutionLayer.cpp
1 /*
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
34 using namespace arm_compute;
35
36 NEConvolutionLayer::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)
39 {
40 }
41
42 void 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     // Get convolved dimensions
68     unsigned int conv_w = 0;
69     unsigned int conv_h = 0;
70     std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), weights->info()->dimension(0),
71                                                  stride_x, stride_y, pad_x, pad_y, conv_info.round());
72     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");
73
74     // Create tensor to store the reshaped weights
75     const size_t      mat_weights_cols = weights->info()->dimension(3);
76     const size_t      mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + ((_has_bias) ? 1 : 0);
77     const TensorShape shape_wr(mat_weights_cols, mat_weights_rows);
78     TensorInfo        info_wr(shape_wr, 1, weights->info()->data_type());
79     _weights_reshaped.allocator()->init(info_wr);
80
81     // Create tensor to store transposed weights
82     TensorShape shape_wt(mat_weights_rows * 4, static_cast<size_t>(std::ceil(mat_weights_cols / 4.f)));
83     TensorInfo  info_wt(shape_wt, 1, weights->info()->data_type());
84     _weights_transposed.allocator()->init(info_wt);
85
86     // Create tensor to store im2col reshaped inputs
87     const size_t mat_input_cols = mat_weights_rows;
88     const size_t mat_input_rows = conv_w * conv_h;
89     TensorShape  shape_im2col   = input->info()->tensor_shape();
90     shape_im2col.set(0, mat_input_cols);
91     shape_im2col.set(1, mat_input_rows);
92     shape_im2col.set(2, 1);
93     TensorInfo info_im2col(shape_im2col, 1, input->info()->data_type());
94     _input_im2col_reshaped.allocator()->init(info_im2col);
95
96     // Create tensor to prepare input tensor for GEMM
97     TensorShape shape_interleaved = shape_im2col;
98     shape_interleaved.set(0, shape_interleaved.x() * 4);
99     shape_interleaved.set(1, std::ceil(static_cast<float>(shape_interleaved.y()) / 4));
100     TensorInfo info_interleaved(shape_interleaved, 1, input->info()->data_type());
101     _input_interleaved_reshaped.allocator()->init(info_interleaved);
102
103     // Create GEMM output tensor
104     TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape();
105     shape_gemm.set(0, mat_weights_cols);
106     shape_gemm.set(1, mat_input_rows);
107     TensorInfo info_gemm(shape_gemm, 1, input->info()->data_type());
108     _gemm_output.allocator()->init(info_gemm);
109
110     // Configure kernels
111     _input_im2col_kernel.configure(input, &_input_im2col_reshaped, std::make_pair(conv_w, conv_h), conv_info, _has_bias);
112     _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
113     _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped);
114     _weights_transposed_kernel.configure(&_weights_reshaped, &_weights_transposed);
115     _mm_kernel.configure(&_input_interleaved_reshaped, &_weights_transposed, &_gemm_output, 1.0f);
116     _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h));
117
118     // Allocate the tensors once the all configure methods have been called
119     _weights_reshaped.allocator()->allocate();
120     _weights_transposed.allocator()->allocate();
121     _input_im2col_reshaped.allocator()->allocate();
122     _input_interleaved_reshaped.allocator()->allocate();
123     _gemm_output.allocator()->allocate();
124 }
125
126 void NEConvolutionLayer::run()
127 {
128     // Run weights reshaping (Runs once for every configure)
129     if(_is_first_run)
130     {
131         _is_first_run = false;
132         NEScheduler::get().multithread(&_weights_reshape_kernel, 3);
133         NEScheduler::get().multithread(&_weights_transposed_kernel);
134     }
135
136     // Run input reshaping
137     NEScheduler::get().multithread(&_input_im2col_kernel);
138
139     // Run interleave
140     NEScheduler::get().multithread(&_input_interleave_kernel);
141
142     // Runs GEMM on reshaped matrices
143     NEScheduler::get().multithread(&_mm_kernel);
144
145     // Reshape output matrix
146     NEScheduler::get().multithread(&_output_col2im_kernel);
147 }