blob: 5cfd72f724a058d2c95f3409d8c01a865bbca2da [file] [log] [blame]
Stephen Lie855c232018-01-04 14:13:22 +08001/*
2 * Copyright (c) 2017-2018 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
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,
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000091 const Size2D &dilation, const ActivationLayerInfo &act_info)
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);
99
Georgios Pinitas72219332018-06-05 14:56:06 +0100100 _is_prepared = false;
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100101 _original_weights = weights;
102
Stephen Lie855c232018-01-04 14:13:22 +0800103 if(biases != nullptr)
104 {
105 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100106 ARM_COMPUTE_ERROR_ON(biases->info()->dimension(0) != weights->info()->dimension(3));
Stephen Lie855c232018-01-04 14:13:22 +0800107 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1);
108 }
109
110 const DataType dt = input->info()->data_type();
111
Michele Di Giorgiob8fc60f2018-04-25 11:58:07 +0100112 // Set the GPU target for im2col and col2im
113 _input_im2col_kernel.set_target(GCScheduler::get().get_target());
114 _output_col2im_kernel.set_target(GCScheduler::get().get_target());
115
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100116 const bool append_bias = (biases != nullptr);
117 const unsigned bias_element = (append_bias) ? 1 : 0;
118 const IGCTensor *biases_to_use = (append_bias) ? biases : nullptr;
Stephen Lie855c232018-01-04 14:13:22 +0800119
120 // Get parameters from conv_info
121 unsigned int stride_x = 0;
122 unsigned int stride_y = 0;
123 std::tie(stride_x, stride_y) = conv_info.stride();
124
125 // Get convolved dimensions
126 unsigned int conv_w = 0;
127 unsigned int conv_h = 0;
128
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100129 const unsigned int kernel_width = weights->info()->dimension(0);
130 const unsigned int kernel_height = weights->info()->dimension(1);
Stephen Lie855c232018-01-04 14:13:22 +0800131 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 +0000132 conv_info, dilation);
Stephen Lie855c232018-01-04 14:13:22 +0800133
Stephen Lie855c232018-01-04 14:13:22 +0800134 unsigned int mat_weights_cols = weights->info()->dimension(3);
135 unsigned int mat_weights_rows = weights->info()->dimension(0) * weights->info()->dimension(1) * weights->info()->dimension(2) + bias_element;
136
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100137 // _weights_reshaped will be auto configured in the kernel.
138 // Just append biases and do not transpose 1xW as it will be reshaped in GCGEMM
139 _reshape_weights.configure(weights, biases_to_use, &_weights_reshaped);
140
141 weights = &_weights_reshaped;
Stephen Lie855c232018-01-04 14:13:22 +0800142
143 // Create tensor to store im2col reshaped inputs
144 const unsigned int mat_input_cols = mat_weights_rows;
145 const unsigned int mat_input_rows = conv_w * conv_h;
146 TensorShape shape_im2col = input->info()->tensor_shape();
147 shape_im2col.set(0, mat_input_cols);
148 shape_im2col.set(1, mat_input_rows);
149 shape_im2col.set(2, 1);
150
151 // FIXME: input->clone() doesn't work with subtensors for grouped convolutions.
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100152 TensorInfo im2col_reshaped_info(shape_im2col, 1, dt);
Stephen Lie855c232018-01-04 14:13:22 +0800153 _input_im2col_reshaped.allocator()->init(im2col_reshaped_info);
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +0000154 _memory_group.manage(&_input_im2col_reshaped);
Stephen Lie855c232018-01-04 14:13:22 +0800155
Stephen Lie855c232018-01-04 14:13:22 +0800156 // Create GEMM output tensor
157 TensorShape shape_gemm = _input_im2col_reshaped.info()->tensor_shape();
158 shape_gemm.set(0, mat_weights_cols);
159 shape_gemm.set(1, mat_input_rows);
160 const DataType gemm_data_type = dt;
161
162 // FIXME: input->clone() doesn't work with subtensors for grouped convolutions.
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100163 TensorInfo info_gemm(shape_gemm, 1, gemm_data_type);
Stephen Lie855c232018-01-04 14:13:22 +0800164 _gemm_output.allocator()->init(info_gemm);
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +0000165 _memory_group.manage(&_gemm_output);
Stephen Lie855c232018-01-04 14:13:22 +0800166
Stephen Lie855c232018-01-04 14:13:22 +0800167 if(dt == DataType::F16)
168 {
169 BorderSize border_size = BorderSize(conv_info.pad_top(), conv_info.pad_right(), conv_info.pad_bottom(), conv_info.pad_left());
170 input->info()->extend_padding(border_size);
171 _fill_border.configure(input, border_size, BorderMode::CONSTANT, PixelValue(0)); // for PAD of im2col fp16: consider it as border
172 }
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100173 // Configure im2col
174 _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 +0800175
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100176 // Configure GEMM
177 configure_mm(&_input_im2col_reshaped, weights, &_gemm_output);
178
Stephen Lie855c232018-01-04 14:13:22 +0800179 _input_im2col_reshaped.allocator()->allocate();
180
181 // Configure Col2Im
182 _output_col2im_kernel.configure(&_gemm_output, output, std::make_pair(conv_w, conv_h));
183 _gemm_output.allocator()->allocate();
184
185 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");
186
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000187 //Configure Activation Layer
188 _is_activationlayer_enabled = act_info.enabled();
189
190 if(_is_activationlayer_enabled)
191 {
192 _activationlayer_function.configure(output, nullptr, act_info);
193 }
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100194
195 ARM_COMPUTE_UNUSED(weights_info);
Stephen Lie855c232018-01-04 14:13:22 +0800196}
197
198void GCConvolutionLayer::run()
199{
Georgios Pinitas72219332018-06-05 14:56:06 +0100200 prepare();
Stephen Lie855c232018-01-04 14:13:22 +0800201
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +0000202 _memory_group.acquire();
203
Stephen Lie855c232018-01-04 14:13:22 +0800204 // Run im2col
205 GCScheduler::get().dispatch(_fill_border);
206 GCScheduler::get().memory_barrier();
207 GCScheduler::get().dispatch(_input_im2col_kernel);
208
Michele Di Giorgio164b65d2018-04-13 14:28:08 +0100209 // Run gemm on reshaped matrices
210 _mm_gemm.run();
Stephen Lie855c232018-01-04 14:13:22 +0800211 GCScheduler::get().memory_barrier();
Georgios Pinitas72219332018-06-05 14:56:06 +0100212
Stephen Lie855c232018-01-04 14:13:22 +0800213 // Reshape output matrix
214 GCScheduler::get().dispatch(_output_col2im_kernel, false);
Georgios Pinitas72219332018-06-05 14:56:06 +0100215 GCScheduler::get().memory_barrier();
Michalis Spyrou9e9cbaf2018-03-15 14:41:34 +0000216
217 _memory_group.release();
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000218
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000219 // Run Activation Layer
220 if(_is_activationlayer_enabled)
221 {
222 _activationlayer_function.run();
223 }
Stephen Lie855c232018-01-04 14:13:22 +0800224}
Georgios Pinitas72219332018-06-05 14:56:06 +0100225
226void GCConvolutionLayer::prepare()
227{
228 if(!_is_prepared)
229 {
230 ARM_COMPUTE_ERROR_ON(!_original_weights->is_used());
231
232 // Run weights reshaping and mark as unused
233 _weights_reshaped.allocator()->allocate();
234 _reshape_weights.run();
235
236 // Mark original weights tensor as unused
237 _original_weights->mark_as_unused();
238
239 _is_prepared = true;
240 }
241}