blob: a35a18a3d4137db54e3aad9a396c567ca09a13b4 [file] [log] [blame]
Stephen Lie855c232018-01-04 14:13:22 +08001/*
Manuel Bottini55e16782019-01-15 13:21:57 +00002 * Copyright (c) 2017-2019 ARM Limited.
Stephen Lie855c232018-01-04 14:13:22 +08003 *
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
25#include "arm_compute/runtime/GLES_COMPUTE/functions/GCConvolutionLayer.h"
26
27#include "arm_compute/core/PixelValue.h"
28#include "arm_compute/core/Size2D.h"
29#include "arm_compute/core/Utils.h"
30#include "arm_compute/core/Validate.h"
31#include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h"
32
33#include <cmath>
34#include <memory>
35#include <tuple>
36
37using namespace arm_compute;
38
39GCConvolutionLayerReshapeWeights::GCConvolutionLayerReshapeWeights()
Georgios Pinitas72219332018-06-05 14:56:06 +010040 : _weights_reshape_kernel()
Stephen Lie855c232018-01-04 14:13:22 +080041{
42}
43
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010044void GCConvolutionLayerReshapeWeights::configure(const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output)
Stephen Lie855c232018-01-04 14:13:22 +080045{
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010046 ARM_COMPUTE_ERROR_ON_NULLPTR(weights, output);
Stephen Lie855c232018-01-04 14:13:22 +080047 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weights, 1, DataType::F16, DataType::F32);
Stephen Lie855c232018-01-04 14:13:22 +080048 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4);
49
50 if(biases != nullptr)
51 {
52 ARM_COMPUTE_ERROR_ON(is_data_type_quantized_asymmetric(weights->info()->data_type()));
53 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(weights, biases);
54 ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(3));
55 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
56 }
57
58 const bool append_biases = (biases != nullptr) && !is_data_type_quantized_asymmetric(weights->info()->data_type());
Stephen Lie855c232018-01-04 14:13:22 +080059 const IGCTensor *biases_to_use = (append_biases) ? biases : nullptr;
60
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010061 _weights_reshape_kernel.configure(weights, biases_to_use, output);
Stephen Lie855c232018-01-04 14:13:22 +080062}
63
64void GCConvolutionLayerReshapeWeights::run()
65{
66 GCScheduler::get().dispatch(_weights_reshape_kernel);
Stephen Lie855c232018-01-04 14:13:22 +080067}
68
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +000069GCConvolutionLayer::GCConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager)
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010070 : _memory_group(std::move(memory_manager)), _reshape_weights(), _input_im2col_kernel(), _mm_gemm(), _output_col2im_kernel(), _fill_border(), _activationlayer_function(), _original_weights(nullptr),
Georgios Pinitas72219332018-06-05 14:56:06 +010071 _input_im2col_reshaped(), _input_interleaved_reshaped(), _weights_reshaped(), _weights_transposed(), _gemm_output(), _tmp_output(), _is_activationlayer_enabled(false), _is_prepared(false)
Stephen Lie855c232018-01-04 14:13:22 +080072{
73}
74
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010075void GCConvolutionLayer::configure_mm(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output)
Stephen Lie855c232018-01-04 14:13:22 +080076{
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010077 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights);
78 ARM_COMPUTE_ERROR_THROW_ON(validate_mm(input->info(), weights->info(), output->info()));
79
80 _mm_gemm.configure(input, weights, nullptr, output, 1.f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run */));
81}
82
83Status GCConvolutionLayer::validate_mm(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *output)
84{
85 // Perform validation step on Matrix multiply function
86 GCGEMM::validate(input, weights, nullptr, output, 1.0f, 0.0f, GEMMInfo(false, false, true /* Reshape weights only for the first run */));
87 return Status{};
Stephen Lie855c232018-01-04 14:13:22 +080088}
89
Alex Gilday7da29b62018-03-23 14:16:00 +000090void GCConvolutionLayer::configure(const IGCTensor *input, const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info,
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010091 const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups)
Stephen Lie855c232018-01-04 14:13:22 +080092{
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010093 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights);
Stephen Lie855c232018-01-04 14:13:22 +080094 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
95 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights);
Michele Di Giorgio164b65d2018-04-13 14:28:08 +010096 ARM_COMPUTE_ERROR_ON_MSG(weights_info.are_reshaped(), "Weights already reshaped are not supported!");
97 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(2) != input->info()->dimension(2));
Stephen Lie855c232018-01-04 14:13:22 +080098 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 4);
Gian Marco Iodice916d1bc2018-08-13 11:20:41 +010099 ARM_COMPUTE_ERROR_ON(num_groups > 1);
100 ARM_COMPUTE_UNUSED(num_groups);
Stephen Lie855c232018-01-04 14:13:22 +0800101
Georgios Pinitas72219332018-06-05 14:56:06 +0100102 _is_prepared = false;
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100103 _original_weights = weights;
104
Stephen Lie855c232018-01-04 14:13:22 +0800105 if(biases != nullptr)
106 {
107 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100108 ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(3));
Stephen Lie855c232018-01-04 14:13:22 +0800109 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
110 }
111
112 const DataType dt = input->info()->data_type();
113
Michele Di Giorgiob8fc60f2018-04-25 11:58:07 +0100114 // Set the GPU target for im2col and col2im
115 _input_im2col_kernel.set_target(GCScheduler::get().get_target());
116 _output_col2im_kernel.set_target(GCScheduler::get().get_target());
117
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100118 const bool append_bias = (biases != nullptr);
119 const unsigned bias_element = (append_bias) ? 1 : 0;
120 const IGCTensor *biases_to_use = (append_bias) ? biases : nullptr;
Stephen Lie855c232018-01-04 14:13:22 +0800121
122 // Get parameters from conv_info
123 unsigned int stride_x = 0;
124 unsigned int stride_y = 0;
125 std::tie(stride_x, stride_y) = conv_info.stride();
126
127 // Get convolved dimensions
128 unsigned int conv_w = 0;
129 unsigned int conv_h = 0;
130
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100131 const unsigned int kernel_width = weights->info()->dimension(0);
132 const unsigned int kernel_height = weights->info()->dimension(1);
Stephen Lie855c232018-01-04 14:13:22 +0800133 std::tie(conv_w, conv_h) = scaled_dimensions(input->info()->dimension(0), input->info()->dimension(1), kernel_width, kernel_height,
Alex Gilday7da29b62018-03-23 14:16:00 +0000134 conv_info, dilation);
Stephen Lie855c232018-01-04 14:13:22 +0800135
Stephen Lie855c232018-01-04 14:13:22 +0800136 unsigned int mat_weights_cols = weights->info()->dimension(3);
137 unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + bias_element;
138
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100139 // _weights_reshaped will be auto configured in the kernel.
140 // Just append biases and do not transpose 1xW as it will be reshaped in GCGEMM
141 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped);
142
143 weights = &_weights_reshaped;
Stephen Lie855c232018-01-04 14:13:22 +0800144
145 // Create tensor to store im2col reshaped inputs
146 const unsigned int mat_input_cols = mat_weights_rows;
147 const unsigned int mat_input_rows = conv_w * conv_h;
148 TensorShape shape_im2col = input->info()->tensor_shape();
149 shape_im2col.set(0, mat_input_cols);
150 shape_im2col.set(1, mat_input_rows);
151 shape_im2col.set(2, 1);
152
153 // FIXME: input->clone() doesn't work with subtensors for grouped convolutions.
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100154 TensorInfo im2col_reshaped_info(shape_im2col, 1, dt);
Stephen Lie855c232018-01-04 14:13:22 +0800155 _input_im2col_reshaped.allocator()->init(im2col_reshaped_info);
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +0000156 _memory_group.manage(&_input_im2col_reshaped);
Stephen Lie855c232018-01-04 14:13:22 +0800157
Stephen Lie855c232018-01-04 14:13:22 +0800158 // Create GEMM output tensor
159 TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape();
160 shape_gemm.set(0, mat_weights_cols);
161 shape_gemm.set(1, mat_input_rows);
162 const DataType gemm_data_type = dt;
163
164 // FIXME: input->clone() doesn't work with subtensors for grouped convolutions.
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100165 TensorInfo info_gemm(shape_gemm, 1, gemm_data_type);
Stephen Lie855c232018-01-04 14:13:22 +0800166 _gemm_output.allocator()->init(info_gemm);
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +0000167 _memory_group.manage(&_gemm_output);
Stephen Lie855c232018-01-04 14:13:22 +0800168
Stephen Lie855c232018-01-04 14:13:22 +0800169 if(dt == DataType::F16)
170 {
171 BorderSize border_size = BorderSize(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left());
172 input->info()->extend_padding(border_size);
Manuel Bottini55e16782019-01-15 13:21:57 +0000173 _fill_border.configure(input, border_size, BorderMode::CONSTANT, PixelValue()); // for PAD of im2col fp16: consider it as border
Stephen Lie855c232018-01-04 14:13:22 +0800174 }
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100175 // Configure im2col
176 _input_im2col_kernel.configure(input, &_input_im2col_reshaped, Size2D(kernel_width, kernel_height), conv_info, append_bias, dilation);
Stephen Lie855c232018-01-04 14:13:22 +0800177
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100178 // Configure GEMM
179 configure_mm(&_input_im2col_reshaped, weights, &_gemm_output);
180
Stephen Lie855c232018-01-04 14:13:22 +0800181 _input_im2col_reshaped.allocator()->allocate();
182
183 // Configure Col2Im
184 _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h));
185 _gemm_output.allocator()->allocate();
186
187 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");
188
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000189 //Configure Activation Layer
190 _is_activationlayer_enabled = act_info.enabled();
191
192 if(_is_activationlayer_enabled)
193 {
194 _activationlayer_function.configure(output, nullptr, act_info);
195 }
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100196
197 ARM_COMPUTE_UNUSED(weights_info);
Stephen Lie855c232018-01-04 14:13:22 +0800198}
199
200void GCConvolutionLayer::run()
201{
Georgios Pinitas72219332018-06-05 14:56:06 +0100202 prepare();
Stephen Lie855c232018-01-04 14:13:22 +0800203
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +0000204 _memory_group.acquire();
205
Stephen Lie855c232018-01-04 14:13:22 +0800206 // Run im2col
207 GCScheduler::get().dispatch(_fill_border);
208 GCScheduler::get().memory_barrier();
209 GCScheduler::get().dispatch(_input_im2col_kernel);
210
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100211 // Run gemm on reshaped matrices
212 _mm_gemm.run();
Stephen Lie855c232018-01-04 14:13:22 +0800213 GCScheduler::get().memory_barrier();
Georgios Pinitas72219332018-06-05 14:56:06 +0100214
Stephen Lie855c232018-01-04 14:13:22 +0800215 // Reshape output matrix
216 GCScheduler::get().dispatch(_output_col2im_kernel, false);
Georgios Pinitas72219332018-06-05 14:56:06 +0100217 GCScheduler::get().memory_barrier();
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +0000218
219 _memory_group.release();
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000220
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000221 // Run Activation Layer
222 if(_is_activationlayer_enabled)
223 {
224 _activationlayer_function.run();
225 }
Stephen Lie855c232018-01-04 14:13:22 +0800226}
Georgios Pinitas72219332018-06-05 14:56:06 +0100227
228void GCConvolutionLayer::prepare()
229{
230 if(!_is_prepared)
231 {
232 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
233
234 // Run weights reshaping and mark as unused
235 _weights_reshaped.allocator()->allocate();
236 _reshape_weights.run();
237
238 // Mark original weights tensor as unused
239 _original_weights->mark_as_unused();
240
241 _is_prepared = true;
242 }
243}