Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 9204646 | 2020-03-07 22:15:55 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 ARM Limited. |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 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/GLES_COMPUTE/functions/GCFullyConnectedLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/Validate.h" |
| 27 | #include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h" |
Matthew Bentham | 9204646 | 2020-03-07 22:15:55 +0000 | [diff] [blame] | 28 | #include "support/MemorySupport.h" |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 29 | |
| 30 | #include <algorithm> |
| 31 | |
| 32 | using namespace arm_compute; |
| 33 | |
| 34 | void GCFullyConnectedLayerReshapeWeights::configure(const IGCTensor *input, IGCTensor *output) |
| 35 | { |
| 36 | auto k = arm_compute::support::cpp14::make_unique<GCTransposeKernel>(); |
| 37 | k->configure(input, output); |
| 38 | _kernel = std::move(k); |
| 39 | } |
| 40 | |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 41 | GCFullyConnectedLayer::GCFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager) |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 42 | : _memory_group(std::move(memory_manager)), _weights_manager(std::move(weights_manager)), _im2col_kernel(), _reshape_weights_kernel(), _mm_kernel(), _accumulate_biases_kernel(), _im2col_output(), |
| 43 | _reshape_weights_output(), _original_weights(nullptr), _are_weights_reshaped(true), _is_fc_after_conv(true), _accumulate_biases(false) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
| 47 | void GCFullyConnectedLayer::configure_conv_fc(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output) |
| 48 | { |
| 49 | ARM_COMPUTE_ERROR_ON((weights->info()->dimension(1) != (input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2)))); |
| 50 | |
| 51 | const DataType dt = input->info()->data_type(); |
| 52 | |
| 53 | // If the fully connected layer is called after a convolution layer, the input tensor must be linearized |
| 54 | |
| 55 | // Initialize output tensor for im2col |
| 56 | TensorShape shape_im2col; |
| 57 | shape_im2col.set(0, input->info()->dimension(0) * input->info()->dimension(1) * input->info()->dimension(2)); |
| 58 | shape_im2col.set(1, input->info()->dimension(3)); |
| 59 | shape_im2col.set(2, input->info()->dimension(4)); |
| 60 | shape_im2col.set(3, input->info()->dimension(5)); |
| 61 | _im2col_output.allocator()->init(TensorInfo(shape_im2col, 1, dt)); |
| 62 | |
| 63 | // Configure im2col kernel |
Michalis Spyrou | 9e9cbaf | 2018-03-15 14:41:34 +0000 | [diff] [blame] | 64 | _memory_group.manage(&_im2col_output); |
Stephen Li | e855c23 | 2018-01-04 14:13:22 +0800 | [diff] [blame] | 65 | _im2col_kernel.configure(input, &_im2col_output, Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 66 | |
| 67 | // Configure matrix multiply kernel |
| 68 | _mm_kernel.configure(&_im2col_output, weights, output, 1.0f, false); |
| 69 | |
| 70 | // Allocate the output tensor for im2col once all the configure methods have been called |
| 71 | _im2col_output.allocator()->allocate(); |
| 72 | } |
| 73 | |
| 74 | void GCFullyConnectedLayer::configure_fc_fc(const IGCTensor *input, const IGCTensor *weights, IGCTensor *output) |
| 75 | { |
| 76 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != weights->info()->dimension(1)); |
| 77 | |
| 78 | // Configure matrix multiply kernel |
| 79 | _mm_kernel.configure(input, weights, output, 1.0f, false); |
| 80 | } |
| 81 | |
Georgios Pinitas | ceff0f9 | 2018-03-19 19:57:01 +0000 | [diff] [blame] | 82 | void GCFullyConnectedLayer::configure(const IGCTensor *input, const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output, |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 83 | FullyConnectedLayerInfo fc_info) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 84 | { |
| 85 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32, DataType::F16); |
| 86 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output); |
| 87 | ARM_COMPUTE_ERROR_ON(weights->info()->num_dimensions() > 2); |
| 88 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 89 | _original_weights = weights; |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 90 | _are_weights_reshaped = fc_info.transpose_weights ? fc_info.are_weights_reshaped : true; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 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 | |
| 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 | |
| 110 | const IGCTensor *weights_to_use = weights; |
| 111 | |
| 112 | if(!_are_weights_reshaped) |
| 113 | { |
| 114 | weights_to_use = &_reshape_weights_output; |
| 115 | |
| 116 | // Reshape the weights |
| 117 | _reshape_weights_kernel.configure(weights, &_reshape_weights_output); |
| 118 | } |
| 119 | |
| 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) |
| 124 | { |
| 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)); |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | _is_fc_after_conv = input->info()->num_dimensions() > 1; |
| 132 | } |
| 133 | |
| 134 | if(_is_fc_after_conv) |
| 135 | { |
| 136 | // Fully Connected layer after a Convolution Layer without batches |
| 137 | configure_conv_fc(input, weights_to_use, output); |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | // Fully Connected layer after a Fully Connected Layer without batches |
| 142 | configure_fc_fc(input, weights_to_use, output); |
| 143 | } |
| 144 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 145 | ARM_COMPUTE_ERROR_ON(fc_info.retain_internal_weights && _reshape_weights_output.gc_buffer() == 0); |
| 146 | _are_weights_reshaped = _are_weights_reshaped || fc_info.retain_internal_weights; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void GCFullyConnectedLayer::run() |
| 150 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 151 | prepare(); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 152 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 153 | MemoryGroupResourceScope scope_mg(_memory_group); |
Georgios Pinitas | ceff0f9 | 2018-03-19 19:57:01 +0000 | [diff] [blame] | 154 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 155 | // Linearize input if it comes from a convolutional layer |
| 156 | if(_is_fc_after_conv) |
| 157 | { |
Joel Liang | eb8f71e | 2017-12-27 13:16:00 +0800 | [diff] [blame] | 158 | GCScheduler::get().dispatch(_im2col_kernel, false); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Joel Liang | eb8f71e | 2017-12-27 13:16:00 +0800 | [diff] [blame] | 161 | if(!_are_weights_reshaped || _is_fc_after_conv) |
| 162 | { |
| 163 | GCScheduler::get().memory_barrier(); |
| 164 | } |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 165 | |
| 166 | // Run matrix multiply |
Joel Liang | eb8f71e | 2017-12-27 13:16:00 +0800 | [diff] [blame] | 167 | GCScheduler::get().dispatch(_mm_kernel, !_accumulate_biases); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 168 | |
| 169 | // Accumulate biases if provided |
| 170 | if(_accumulate_biases) |
| 171 | { |
Joel Liang | eb8f71e | 2017-12-27 13:16:00 +0800 | [diff] [blame] | 172 | GCScheduler::get().memory_barrier(); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 173 | |
Joel Liang | eb8f71e | 2017-12-27 13:16:00 +0800 | [diff] [blame] | 174 | GCScheduler::get().dispatch(_accumulate_biases_kernel); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 175 | } |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 176 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 177 | |
| 178 | void GCFullyConnectedLayer::prepare() |
| 179 | { |
| 180 | // Reshape of the weights (happens only once) |
| 181 | if(!_are_weights_reshaped) |
| 182 | { |
| 183 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
| 184 | |
| 185 | // Run reshape weights kernel and mark weights as unused |
| 186 | _reshape_weights_output.allocator()->allocate(); |
| 187 | _reshape_weights_kernel.run(); |
| 188 | |
| 189 | // Mark original weights tensor as unused |
| 190 | _original_weights->mark_as_unused(); |
| 191 | |
| 192 | _are_weights_reshaped = true; |
| 193 | } |
Matthew Bentham | 9204646 | 2020-03-07 22:15:55 +0000 | [diff] [blame] | 194 | } |