Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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/CL/functions/CLFullyConnectedLayer.h" |
| 25 | |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Size2D.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Validate.h" |
| 28 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 29 | #include "support/ToolchainSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | |
| 31 | #include <algorithm> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 33 | using namespace arm_compute; |
| 34 | |
| 35 | void CLFullyConnectedLayerReshapeWeights::configure(const ICLTensor *input, ICLTensor *output) |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 36 | { |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 37 | auto k = arm_compute::support::cpp14::make_unique<CLTransposeKernel>(); |
| 38 | k->configure(input, output); |
| 39 | _kernel = std::move(k); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 40 | } |
| 41 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 42 | CLFullyConnectedLayer::CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager) |
| 43 | : _memory_group(std::move(memory_manager)), _im2col_kernel(), _reshape_weights_kernel(), _mm_kernel(), _accumulate_biases_kernel(), _im2col_output(), _reshape_weights_output(), |
| 44 | _are_weights_reshaped(true), _is_fc_after_conv(true), _accumulate_biases(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 45 | { |
| 46 | } |
| 47 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 48 | void CLFullyConnectedLayer::configure_conv_fc(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output) |
| 49 | { |
| 50 | ARM_COMPUTE_ERROR_ON((weights->info()->dimension(1) != (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2)))); |
| 51 | |
| 52 | const DataType dt = input->info()->data_type(); |
| 53 | const int fixed_point_position = input->info()->fixed_point_position(); |
| 54 | |
| 55 | // If the fully connected layer is called after a convolution layer, the input tensor must be linearized |
| 56 | |
| 57 | // Initialize output tensor for im2col |
| 58 | TensorShape shape_im2col; |
| 59 | shape_im2col.set(0, input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2)); |
| 60 | shape_im2col.set(1, input->info()->dimension(3)); |
| 61 | shape_im2col.set(2, input->info()->dimension(4)); |
| 62 | shape_im2col.set(3, input->info()->dimension(5)); |
| 63 | _im2col_output.allocator()->init(TensorInfo(shape_im2col, 1, dt, fixed_point_position)); |
| 64 | |
| 65 | // Configure im2col kernel |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 66 | _memory_group.manage(&_im2col_output); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 67 | _im2col_kernel.configure(input, &_im2col_output, Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false); |
| 68 | |
| 69 | // Configure matrix multiply kernel |
| 70 | _mm_kernel.configure(&_im2col_output, weights, output, 1.0f, false); |
| 71 | |
| 72 | // Allocate the output tensor for im2col once all the configure methods have been called |
| 73 | _im2col_output.allocator()->allocate(); |
| 74 | } |
| 75 | |
| 76 | void CLFullyConnectedLayer::configure_fc_fc(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output) |
| 77 | { |
| 78 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != weights->info()->dimension(1)); |
| 79 | |
| 80 | // Configure matrix multiply kernel |
| 81 | _mm_kernel.configure(input, weights, output, 1.0f, false); |
| 82 | } |
| 83 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 84 | void CLFullyConnectedLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, bool transpose_weights, bool are_weights_reshaped) |
| 85 | { |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 86 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
| 87 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output); |
Georgios Pinitas | 96880cf | 2017-10-20 18:52:20 +0100 | [diff] [blame] | 88 | ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 2); |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 89 | |
| 90 | _are_weights_reshaped = transpose_weights ? are_weights_reshaped : true; |
| 91 | _is_fc_after_conv = true; |
| 92 | _accumulate_biases = false; |
| 93 | |
| 94 | if(biases != nullptr) |
| 95 | { |
| 96 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 97 | |
| 98 | _accumulate_biases = true; |
| 99 | |
| 100 | // Configure accumulate biases kernel |
| 101 | _accumulate_biases_kernel.configure(output, biases); |
| 102 | } |
| 103 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | // With the Fully Connected layer we can have 4 different cases: |
| 105 | // 1) Convolution layer -> Fully Connected layer without batches |
| 106 | // 2) Fully Connected layer -> Fully Connected layer without batches |
| 107 | // 3) Convolution layer -> Fully Connected layer with batches |
| 108 | // 4) Fully Connected layer -> Fully Connected layer with batches |
| 109 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 110 | const ICLTensor *weights_to_use = weights; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 112 | if(!_are_weights_reshaped) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | { |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 114 | weights_to_use = &_reshape_weights_output; |
| 115 | |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 116 | // Reshape the weights |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 117 | _reshape_weights_kernel.configure(weights, &_reshape_weights_output); |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 120 | // Check if we have a fully connected layer with batches |
| 121 | const bool is_batched_fc_layer = output->info()->dimension(1) > 1; |
| 122 | |
| 123 | if(is_batched_fc_layer) |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 124 | { |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 125 | _is_fc_after_conv = (TensorShape::num_max_dimensions >= 4) && (std::equal(input->info()->tensor_shape().cbegin() + 3, |
| 126 | input->info()->tensor_shape().cend(), |
| 127 | output->info()->tensor_shape().cbegin() + 1)); |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 128 | } |
| 129 | else |
| 130 | { |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 131 | _is_fc_after_conv = input->info()->num_dimensions() > 1; |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 134 | if(_is_fc_after_conv) |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 135 | { |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 136 | // Fully Connected layer after a Convolution Layer without batches |
| 137 | configure_conv_fc(input, weights_to_use, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 138 | } |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 139 | else |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 140 | { |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 141 | // Fully Connected layer after a Fully Connected Layer without batches |
| 142 | configure_fc_fc(input, weights_to_use, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // Allocate the transpose tensor if the are_weights_reshaped flag is false and once all the configure methods have been called |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 146 | if(!_are_weights_reshaped) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 147 | { |
Moritz Pflanzer | 768e9f1 | 2017-08-11 15:33:30 +0100 | [diff] [blame] | 148 | // Allocate the tensor for the weights reshaped |
| 149 | _reshape_weights_output.allocator()->allocate(); |
| 150 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void CLFullyConnectedLayer::run() |
| 154 | { |
| 155 | // Reshape of the weights (happens only once) |
| 156 | if(!_are_weights_reshaped) |
| 157 | { |
| 158 | _are_weights_reshaped = true; |
| 159 | _reshape_weights_kernel.run(); |
| 160 | } |
| 161 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 162 | _memory_group.acquire(); |
| 163 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 164 | // Linearize input if it comes from a convolutional layer |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 165 | if(_is_fc_after_conv) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 166 | { |
| 167 | CLScheduler::get().enqueue(_im2col_kernel, false); |
| 168 | } |
| 169 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 170 | // Run matrix multiply |
| 171 | CLScheduler::get().enqueue(_mm_kernel, !_accumulate_biases); |
| 172 | |
| 173 | // Accumulate biases if provided |
| 174 | if(_accumulate_biases) |
| 175 | { |
| 176 | CLScheduler::get().enqueue(_accumulate_biases_kernel); |
| 177 | } |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 178 | |
| 179 | _memory_group.release(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 180 | } |