blob: aae4a67eef748f62e033fbc0d13e78e117a87524 [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(),
Anthony Barbiera4376382017-04-12 15:12:46 +010038 _input_interleaved_reshaped(), _weights_reshaped(), _weights_transposed(), _gemm_output(), _is_first_run(false), _has_bias(false)
Anthony Barbier871448e2017-03-24 14:54:29 +000039{
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
Anthony Barbier46d59272017-05-04 09:15:15 +010063 unsigned int stride_x = 0;
64 unsigned int stride_y = 0;
65 unsigned int pad_x = 0;
66 unsigned int pad_y = 0;
Anthony Barbier871448e2017-03-24 14:54:29 +000067 std::tie(stride_x, stride_y) = conv_info.stride();
68 std::tie(pad_x, pad_y) = conv_info.pad();
69
Anthony Barbier871448e2017-03-24 14:54:29 +000070 // Get convolved dimensions
71 unsigned int conv_w = 0;
72 unsigned int conv_h = 0;
Anthony Barbier871448e2017-03-24 14:54:29 +000073 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), weights->info()->dimension(0),
74 stride_x, stride_y, pad_x, pad_y, conv_info.round());
Anthony Barbiera4376382017-04-12 15:12:46 +010075 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");
Anthony Barbier871448e2017-03-24 14:54:29 +000076
77 // Create tensor to store the reshaped weights
Anthony Barbier46d59272017-05-04 09:15:15 +010078 const unsigned int mat_weights_cols = weights->info()->dimension(3);
79 const unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + (_has_bias ? 1 : 0);
80 TensorShape shape_wr(mat_weights_cols, mat_weights_rows);
81 TensorInfo info_wr(shape_wr, 1, weights->info()->data_type());
Anthony Barbier871448e2017-03-24 14:54:29 +000082 _weights_reshaped.allocator()->init(info_wr);
83
84 // Create tensor to store transposed weights
Anthony Barbier46d59272017-05-04 09:15:15 +010085 TensorShape shape_wt(mat_weights_rows * 4, static_cast<unsigned int>(std::ceil(mat_weights_cols / 4.f)));
Anthony Barbier871448e2017-03-24 14:54:29 +000086 TensorInfo info_wt(shape_wt, 1, weights->info()->data_type());
87 _weights_transposed.allocator()->init(info_wt);
88
89 // Create tensor to store im2col reshaped inputs
Anthony Barbier46d59272017-05-04 09:15:15 +010090 const unsigned int mat_input_cols = mat_weights_rows;
91 const unsigned int mat_input_rows = conv_w * conv_h;
92 TensorShape shape_im2col = input->info()->tensor_shape();
Anthony Barbier871448e2017-03-24 14:54:29 +000093 shape_im2col.set(0, mat_input_cols);
94 shape_im2col.set(1, mat_input_rows);
95 shape_im2col.set(2, 1);
Anthony Barbier871448e2017-03-24 14:54:29 +000096 TensorInfo info_im2col(shape_im2col, 1, input->info()->data_type());
97 _input_im2col_reshaped.allocator()->init(info_im2col);
98
99 // Create tensor to prepare input tensor for GEMM
100 TensorShape shape_interleaved = shape_im2col;
101 shape_interleaved.set(0, shape_interleaved.x() * 4);
Anthony Barbier46d59272017-05-04 09:15:15 +0100102 shape_interleaved.set(1, std::ceil(shape_interleaved.y() / 4.f));
Anthony Barbier871448e2017-03-24 14:54:29 +0000103 TensorInfo info_interleaved(shape_interleaved, 1, input->info()->data_type());
104 _input_interleaved_reshaped.allocator()->init(info_interleaved);
105
106 // Create GEMM output tensor
107 TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape();
108 shape_gemm.set(0, mat_weights_cols);
109 shape_gemm.set(1, mat_input_rows);
110 TensorInfo info_gemm(shape_gemm, 1, input->info()->data_type());
111 _gemm_output.allocator()->init(info_gemm);
112
113 // Configure kernels
114 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, std::make_pair(conv_w, conv_h), conv_info, _has_bias);
115 _input_interleave_kernel.configure(&_input_im2col_reshaped, &_input_interleaved_reshaped);
116 _weights_reshape_kernel.configure(weights, biases, &_weights_reshaped);
117 _weights_transposed_kernel.configure(&_weights_reshaped, &_weights_transposed);
Anthony Barbiera4376382017-04-12 15:12:46 +0100118 _mm_kernel.configure(&_input_interleaved_reshaped, &_weights_transposed, &_gemm_output, 1.0f);
119 _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h));
Anthony Barbier871448e2017-03-24 14:54:29 +0000120
121 // Allocate the tensors once the all configure methods have been called
122 _weights_reshaped.allocator()->allocate();
123 _weights_transposed.allocator()->allocate();
124 _input_im2col_reshaped.allocator()->allocate();
125 _input_interleaved_reshaped.allocator()->allocate();
126 _gemm_output.allocator()->allocate();
127}
128
129void NEConvolutionLayer::run()
130{
131 // Run weights reshaping (Runs once for every configure)
132 if(_is_first_run)
133 {
134 _is_first_run = false;
135 NEScheduler::get().multithread(&_weights_reshape_kernel, 3);
136 NEScheduler::get().multithread(&_weights_transposed_kernel);
137 }
138
139 // Run input reshaping
140 NEScheduler::get().multithread(&_input_im2col_kernel);
141
142 // Run interleave
143 NEScheduler::get().multithread(&_input_interleave_kernel);
144
145 // Runs GEMM on reshaped matrices
146 NEScheduler::get().multithread(&_mm_kernel);
147
148 // Reshape output matrix
Anthony Barbiera4376382017-04-12 15:12:46 +0100149 NEScheduler::get().multithread(&_output_col2im_kernel);
Anthony Barbier871448e2017-03-24 14:54:29 +0000150}