blob: 7fd81cdb943cc8be495b77677fb2a9e81d7c0cd6 [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/CL/functions/CLFullyConnectedLayer.h"
25
Kaizen8938bd32017-09-28 14:38:23 +010026#include "arm_compute/core/Size2D.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000027#include "arm_compute/core/Validate.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000028#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000029#include "arm_compute/runtime/CL/CLScheduler.h"
Kaizen8938bd32017-09-28 14:38:23 +010030#include "support/ToolchainSupport.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000031
Anthony Barbiera4376382017-04-12 15:12:46 +010032#include <algorithm>
Anthony Barbiera4376382017-04-12 15:12:46 +010033
Anthony Barbier871448e2017-03-24 14:54:29 +000034using namespace arm_compute;
35
Kaizen8938bd32017-09-28 14:38:23 +010036void CLFullyConnectedLayerReshapeWeights::configure(const ICLTensor *input, ICLTensor *output)
37{
38 auto k = arm_compute::support::cpp14::make_unique<CLTransposeKernel>();
39 k->configure(input, output);
40 _kernel = std::move(k);
41}
42
43CLFullyConnectedLayer::CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000044 : _memory_group(memory_manager), _im2col_kernel(), _reshape_weights_kernel(), _mm_kernel(), _mm_gemmlowp(memory_manager), _gemmlowp_output_stage(), _accumulate_biases_kernel(), _im2col_output(),
45 _gemmlowp_output(), _reshape_weights_output(), _are_weights_reshaped(true), _is_fc_after_conv(true), _accumulate_biases(false), _is_quantized(false)
Anthony Barbierdbdab852017-06-23 15:42:00 +010046{
47}
48
Anthony Barbier8140e1e2017-12-14 23:48:46 +000049void CLFullyConnectedLayer::configure_mm(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output, bool is_interleaved_transposed)
50{
51 if(_is_quantized)
52 {
53 // Since we need negative offsets for computing convolution, we need to change QuantizationInfo()
54 // Extract and negate input and weights offset
55 const QuantizationInfo input_quantization_info = input->info()->quantization_info();
56 const QuantizationInfo weights_quantization_info = weights->info()->quantization_info();
57
58 input->info()->set_quantization_info(QuantizationInfo(input_quantization_info.scale, -input_quantization_info.offset));
59 weights->info()->set_quantization_info(QuantizationInfo(weights_quantization_info.scale, -weights_quantization_info.offset));
60
61 // Configure gemmlowp function
62 _mm_gemmlowp.configure(input, weights, output);
63
64 // Revert back QuantizatioInfo as input and weights could be used in other fully connected layers
65 input->info()->set_quantization_info(input_quantization_info);
66 weights->info()->set_quantization_info(weights_quantization_info);
67 }
68 else
69 {
70 // Configure matrix multiply kernel
71 _mm_kernel.set_target(CLScheduler::get().target());
72 _mm_kernel.configure(input, weights, output, 1.f, is_interleaved_transposed);
73 }
74}
75
Kaizen8938bd32017-09-28 14:38:23 +010076void CLFullyConnectedLayer::configure_conv_fc(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output)
Anthony Barbierdbdab852017-06-23 15:42:00 +010077{
Kaizen8938bd32017-09-28 14:38:23 +010078 ARM_COMPUTE_ERROR_ON((weights->info()->dimension(1) != (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2))));
Anthony Barbierdbdab852017-06-23 15:42:00 +010079
Anthony Barbiera4376382017-04-12 15:12:46 +010080 // If the fully connected layer is called after a convolution layer, the input tensor must be linearized
Anthony Barbier871448e2017-03-24 14:54:29 +000081
Anthony Barbiera4376382017-04-12 15:12:46 +010082 // Initialize output tensor for im2col
Anthony Barbier8140e1e2017-12-14 23:48:46 +000083 TensorShape shape_im2col = input->info()->tensor_shape();
84 shape_im2col.collapse(3);
85 _im2col_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(shape_im2col));
Anthony Barbier871448e2017-03-24 14:54:29 +000086
Anthony Barbiera4376382017-04-12 15:12:46 +010087 // Configure im2col kernel
Kaizen8938bd32017-09-28 14:38:23 +010088 _memory_group.manage(&_im2col_output);
89 _im2col_kernel.configure(input, &_im2col_output, Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false);
Anthony Barbiera4376382017-04-12 15:12:46 +010090
Anthony Barbiera4376382017-04-12 15:12:46 +010091 // Configure matrix multiply kernel
Anthony Barbier8140e1e2017-12-14 23:48:46 +000092 configure_mm(&_im2col_output, weights, output, false);
Anthony Barbiera4376382017-04-12 15:12:46 +010093
94 // Allocate the output tensor for im2col once all the configure methods have been called
95 _im2col_output.allocator()->allocate();
96}
97
Kaizen8938bd32017-09-28 14:38:23 +010098void CLFullyConnectedLayer::configure_fc_fc(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output)
Anthony Barbiera4376382017-04-12 15:12:46 +010099{
100 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != weights->info()->dimension(1));
101
102 // Configure matrix multiply kernel
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000103 configure_mm(input, weights, output, false);
Anthony Barbiera4376382017-04-12 15:12:46 +0100104}
105
Anthony Barbierdbdab852017-06-23 15:42:00 +0100106void CLFullyConnectedLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, bool transpose_weights, bool are_weights_reshaped)
Anthony Barbiera4376382017-04-12 15:12:46 +0100107{
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000108 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32);
Anthony Barbiera4376382017-04-12 15:12:46 +0100109 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000110 ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 2);
Anthony Barbiera4376382017-04-12 15:12:46 +0100111
Kaizen8938bd32017-09-28 14:38:23 +0100112 _are_weights_reshaped = transpose_weights ? are_weights_reshaped : true;
Anthony Barbierdbdab852017-06-23 15:42:00 +0100113 _is_fc_after_conv = true;
Anthony Barbierdbdab852017-06-23 15:42:00 +0100114 _accumulate_biases = false;
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000115 _is_quantized = is_data_type_quantized_asymmetric(input->info()->data_type());
Anthony Barbiera4376382017-04-12 15:12:46 +0100116
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000117 // Configure gemmlowp output
118 if(_is_quantized)
119 {
120 _gemmlowp_output.allocator()->init(output->info()->clone()->set_is_resizable(true).reset_padding().set_data_type(DataType::S32));
121 }
122
123 // Configure accumulate biases kernel for non quantized asymmetric types
124 if(biases != nullptr && !_is_quantized)
Anthony Barbier871448e2017-03-24 14:54:29 +0000125 {
Anthony Barbiera4376382017-04-12 15:12:46 +0100126 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
127
128 _accumulate_biases = true;
129
130 // Configure accumulate biases kernel
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000131 _accumulate_biases_kernel.set_target(CLScheduler::get().target());
Anthony Barbiera4376382017-04-12 15:12:46 +0100132 _accumulate_biases_kernel.configure(output, biases);
Anthony Barbier871448e2017-03-24 14:54:29 +0000133 }
134
Anthony Barbiera4376382017-04-12 15:12:46 +0100135 // With the Fully Connected layer we can have 4 different cases:
136 // 1) Convolution layer -> Fully Connected layer without batches
137 // 2) Fully Connected layer -> Fully Connected layer without batches
138 // 3) Convolution layer -> Fully Connected layer with batches
139 // 4) Fully Connected layer -> Fully Connected layer with batches
Anthony Barbier871448e2017-03-24 14:54:29 +0000140
Anthony Barbierdbdab852017-06-23 15:42:00 +0100141 const ICLTensor *weights_to_use = weights;
142
Kaizen8938bd32017-09-28 14:38:23 +0100143 if(!_are_weights_reshaped)
Anthony Barbier871448e2017-03-24 14:54:29 +0000144 {
Kaizen8938bd32017-09-28 14:38:23 +0100145 weights_to_use = &_reshape_weights_output;
Anthony Barbiera4376382017-04-12 15:12:46 +0100146
Kaizen8938bd32017-09-28 14:38:23 +0100147 // Reshape the weights
148 _reshape_weights_kernel.configure(weights, &_reshape_weights_output);
Anthony Barbierdbdab852017-06-23 15:42:00 +0100149 }
150
Kaizen8938bd32017-09-28 14:38:23 +0100151 // Check if we have a fully connected layer with batches
152 const bool is_batched_fc_layer = output->info()->dimension(1) > 1;
153
154 if(is_batched_fc_layer)
Anthony Barbierdbdab852017-06-23 15:42:00 +0100155 {
156 _is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->info()->tensor_shape().cbegin() + 3,
157 input->info()->tensor_shape().cend(),
158 output->info()->tensor_shape().cbegin() + 1));
Anthony Barbiera4376382017-04-12 15:12:46 +0100159 }
160 else
161 {
Kaizen8938bd32017-09-28 14:38:23 +0100162 _is_fc_after_conv = input->info()->num_dimensions() > 1;
163 }
Anthony Barbiera4376382017-04-12 15:12:46 +0100164
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000165 ICLTensor *tmp_output = (_is_quantized) ? &_gemmlowp_output : output;
Kaizen8938bd32017-09-28 14:38:23 +0100166 if(_is_fc_after_conv)
167 {
168 // Fully Connected layer after a Convolution Layer without batches
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000169 configure_conv_fc(input, weights_to_use, tmp_output);
Kaizen8938bd32017-09-28 14:38:23 +0100170 }
171 else
172 {
173 // Fully Connected layer after a Fully Connected Layer without batches
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000174 configure_fc_fc(input, weights_to_use, tmp_output);
175 }
176
177 // Configure output stage for asymmetric quantized types
178 if(_is_quantized)
179 {
180 float multiplier = input->info()->quantization_info().scale * weights->info()->quantization_info().scale / output->info()->quantization_info().scale;
181 int output_multiplier, output_shift;
182 quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
183 _gemmlowp_output_stage.configure(&_gemmlowp_output, biases, output, output_multiplier, output_shift, output->info()->quantization_info().offset);
184 _gemmlowp_output.allocator()->allocate();
Anthony Barbiera4376382017-04-12 15:12:46 +0100185 }
186
Anthony Barbierdbdab852017-06-23 15:42:00 +0100187 // Allocate the transpose tensor if the are_weights_reshaped flag is false and once all the configure methods have been called
Kaizen8938bd32017-09-28 14:38:23 +0100188 if(!_are_weights_reshaped)
Anthony Barbiera4376382017-04-12 15:12:46 +0100189 {
Kaizen8938bd32017-09-28 14:38:23 +0100190 // Allocate the tensor for the weights reshaped
191 _reshape_weights_output.allocator()->allocate();
Anthony Barbier871448e2017-03-24 14:54:29 +0000192 }
193}
194
195void CLFullyConnectedLayer::run()
196{
Anthony Barbierdbdab852017-06-23 15:42:00 +0100197 // Reshape of the weights (happens only once)
198 if(!_are_weights_reshaped)
Anthony Barbiera4376382017-04-12 15:12:46 +0100199 {
Anthony Barbierdbdab852017-06-23 15:42:00 +0100200 _are_weights_reshaped = true;
201 _reshape_weights_kernel.run();
Anthony Barbiera4376382017-04-12 15:12:46 +0100202 }
203
Kaizen8938bd32017-09-28 14:38:23 +0100204 _memory_group.acquire();
205
Anthony Barbiera4376382017-04-12 15:12:46 +0100206 // Linearize input if it comes from a convolutional layer
Anthony Barbierdbdab852017-06-23 15:42:00 +0100207 if(_is_fc_after_conv)
Anthony Barbiera4376382017-04-12 15:12:46 +0100208 {
209 CLScheduler::get().enqueue(_im2col_kernel, false);
210 }
211
Anthony Barbiera4376382017-04-12 15:12:46 +0100212 // Run matrix multiply
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000213 if(_is_quantized)
214 {
215 _mm_gemmlowp.run();
216 }
217 else
218 {
219 CLScheduler::get().enqueue(_mm_kernel, !_accumulate_biases);
220 }
Anthony Barbiera4376382017-04-12 15:12:46 +0100221
222 // Accumulate biases if provided
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000223 if(_is_quantized)
Anthony Barbiera4376382017-04-12 15:12:46 +0100224 {
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000225 _gemmlowp_output_stage.run();
226 }
227 else
228 {
229 if(_accumulate_biases)
230 {
231 CLScheduler::get().enqueue(_accumulate_biases_kernel);
232 }
Anthony Barbiera4376382017-04-12 15:12:46 +0100233 }
Kaizen8938bd32017-09-28 14:38:23 +0100234
235 _memory_group.release();
Anthony Barbier871448e2017-03-24 14:54:29 +0000236}