Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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/NEON/functions/NEFullyConnectedLayer.h" |
| 25 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/Helpers.h" |
Gian Marco Iodice | 13edbff | 2017-06-26 17:20:16 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Size2D.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Validate.h" |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 31 | |
| 32 | #include <algorithm> |
| 33 | #include <cmath> |
| 34 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 35 | using namespace arm_compute; |
| 36 | using namespace arm_compute::misc::shape_calculator; |
| 37 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 38 | NEFullyConnectedLayerReshapeWeights::NEFullyConnectedLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager) |
| 39 | : _memory_group(std::move(memory_manager)), _transpose_kernel(), _transpose1xW_kernel(), _transpose_output(), _transpose_weights(false), _is_batched_fc_layer(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 40 | { |
| 41 | } |
| 42 | |
| 43 | void NEFullyConnectedLayerReshapeWeights::configure(const ITensor *input, ITensor *output, bool transpose_weights, bool is_batched_fc_layer) |
| 44 | { |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 45 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 47 | // Perform validate step |
| 48 | ARM_COMPUTE_ERROR_THROW_ON(NEFullyConnectedLayerReshapeWeights::validate(input->info(), output->info(), transpose_weights, is_batched_fc_layer)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | |
| 50 | _transpose_weights = transpose_weights; |
| 51 | _is_batched_fc_layer = is_batched_fc_layer; |
| 52 | |
| 53 | // Check if we need to transpose the weights |
| 54 | if(_transpose_weights) |
| 55 | { |
| 56 | if(_is_batched_fc_layer) |
| 57 | { |
| 58 | // Initialize the output tensor for transpose |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 59 | _transpose_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_transposed_shape(*input->info()))); |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 60 | _memory_group.manage(&_transpose_output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 61 | _transpose_kernel.configure(input, &_transpose_output); |
| 62 | |
| 63 | // Configure transpose 1xW kernel |
| 64 | _transpose1xW_kernel.configure(&_transpose_output, output); |
| 65 | |
| 66 | // Allocate temporary tensor used for transposing the weights |
| 67 | _transpose_output.allocator()->allocate(); |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | _transpose_kernel.configure(input, output); |
| 72 | } |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | if(_is_batched_fc_layer) |
| 77 | { |
| 78 | // Configure transpose 1xW kernel |
| 79 | _transpose1xW_kernel.configure(input, output); |
| 80 | } |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | Status NEFullyConnectedLayerReshapeWeights::validate(const ITensorInfo *input, const ITensorInfo *output, bool transpose_weights, bool is_batched_fc_layer) |
| 85 | { |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 86 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 87 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 2); |
| 88 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!transpose_weights && !is_batched_fc_layer, "Configuration transpose_weights=false & is_batched_fc_layer=false not supported"); |
| 89 | |
| 90 | if(transpose_weights) |
| 91 | { |
| 92 | if(is_batched_fc_layer) |
| 93 | { |
| 94 | std::unique_ptr<ITensorInfo> use_output = output->clone(); |
| 95 | use_output->set_is_resizable(true).reset_padding().set_tensor_shape(compute_transposed_shape(*input)); |
| 96 | |
| 97 | ARM_COMPUTE_RETURN_ON_ERROR(NETransposeKernel::validate(input, use_output.get())); |
| 98 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(use_output.get(), output)); |
| 99 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | else |
| 101 | { |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 102 | ARM_COMPUTE_RETURN_ON_ERROR(NETransposeKernel::validate(input, output)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | } |
| 104 | } |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 105 | else |
| 106 | { |
| 107 | if(is_batched_fc_layer) |
| 108 | { |
| 109 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMTranspose1xWKernel::validate(input, output)); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void NEFullyConnectedLayerReshapeWeights::run() |
| 117 | { |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 118 | _memory_group.acquire(); |
| 119 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 120 | if(_transpose_weights) |
| 121 | { |
| 122 | NEScheduler::get().schedule(&_transpose_kernel, Window::DimY); |
| 123 | } |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 124 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 125 | if(_is_batched_fc_layer) |
| 126 | { |
| 127 | NEScheduler::get().schedule(&_transpose1xW_kernel, Window::DimY); |
| 128 | } |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 129 | |
| 130 | _memory_group.release(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 131 | } |
| 132 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 133 | NEFullyConnectedLayer::NEFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 134 | : _memory_group(std::move(memory_manager)), _im2col_kernel(), _reshape_weights_function(), _interleave4x4_kernel(), _mm_kernel(), _accumulate_biases_kernel(), _im2col_output(), |
| 135 | _interleave4x4_output(), _reshape_weights_output(), _original_weights(nullptr), _is_batched_fc_layer(false), _linearize_input(false), _accumulate_biases(false), _is_prepared(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 136 | { |
| 137 | } |
| 138 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 139 | void NEFullyConnectedLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, bool transpose_weights, bool are_weights_reshaped) |
| 140 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | // With the Fully Connected layer we can have 4 different cases: |
| 142 | // 1) Convolution layer -> Fully Connected layer without batches |
| 143 | // 2) Fully Connected layer -> Fully Connected layer without batches |
| 144 | // 3) Convolution layer -> Fully Connected layer with batches |
| 145 | // 4) Fully Connected layer -> Fully Connected layer with batches |
| 146 | |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 147 | // Expected shape before transpose and reshaping |
| 148 | // Input: In x B (In and B can be multi-dimensional) |
| 149 | // Weights: flat(In) x Out |
| 150 | // Biases: Out |
| 151 | // Output: Out x B (B can be multi-dimensional) |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 152 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 153 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 154 | // Perform validate step |
| 155 | ARM_COMPUTE_ERROR_THROW_ON(NEFullyConnectedLayer::validate(input->info(), |
| 156 | weights->info(), |
| 157 | biases != nullptr ? biases->info() : nullptr, |
| 158 | output->info(), |
| 159 | transpose_weights, |
| 160 | are_weights_reshaped)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 161 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 162 | const int num_batch_dimensions = std::max(0, static_cast<int>(output->info()->tensor_shape().num_dimensions()) - 1); |
| 163 | const int num_input_dimensions = input->info()->tensor_shape().num_dimensions() - num_batch_dimensions; |
| 164 | const size_t linear_input_size = input->info()->tensor_shape().total_size_lower(num_input_dimensions); |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 165 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 166 | _original_weights = weights; |
| 167 | _linearize_input = (input->info()->tensor_shape().x() != linear_input_size) || (num_input_dimensions > 1 && linear_input_size == 1); |
| 168 | _accumulate_biases = biases != nullptr; |
| 169 | _is_batched_fc_layer = num_batch_dimensions > 0; |
| 170 | _is_prepared = are_weights_reshaped || (!transpose_weights && !_is_batched_fc_layer); |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 171 | |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 172 | const size_t interleave_width = 16 / input->info()->element_size(); |
| 173 | const ITensor *weights_to_use = weights; |
| 174 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 175 | if(!_is_prepared) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | { |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 177 | weights_to_use = &_reshape_weights_output; |
| 178 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 179 | _reshape_weights_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_fully_connected_reshaped_weights_shape(weights->info(), |
| 180 | transpose_weights, |
| 181 | _is_batched_fc_layer, interleave_width))); |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 182 | |
| 183 | // Reshape the weights |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 184 | _reshape_weights_function.configure(weights, &_reshape_weights_output, transpose_weights, _is_batched_fc_layer); |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 187 | const ITensor *multiply_input = input; |
| 188 | |
| 189 | if(_linearize_input) |
| 190 | { |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 191 | _im2col_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_im2col_fc_shape(input->info(), num_input_dimensions))); |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 192 | |
| 193 | // Configure im2col kernel |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 194 | _memory_group.manage(&_im2col_output); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 195 | _im2col_kernel.configure(input, &_im2col_output, Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false, true); |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 196 | |
| 197 | multiply_input = &_im2col_output; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 198 | } |
| 199 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 200 | int m = multiply_input->info()->dimension(1); |
| 201 | int k = multiply_input->info()->dimension(0); |
| 202 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 203 | if(_is_batched_fc_layer) |
| 204 | { |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 205 | _interleave4x4_output.allocator()->init(input->info()->clone()->set_is_resizable(true).reset_padding().set_tensor_shape(compute_interleaved_shape(*multiply_input->info()))); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 206 | |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 207 | // Configure interleave4x4 kernel |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 208 | _memory_group.manage(&_interleave4x4_output); |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 209 | _interleave4x4_kernel.configure(multiply_input, &_interleave4x4_output); |
| 210 | |
| 211 | multiply_input = &_interleave4x4_output; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 212 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 213 | |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 214 | // Configure matrix multiply kernel |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 215 | _mm_kernel.configure(multiply_input, weights_to_use, output, 1.0f, _is_batched_fc_layer, GEMMReshapeInfo(m, 0 /* no transpose */, k)); |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 216 | |
| 217 | if(_accumulate_biases) |
| 218 | { |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 219 | // Configure accumulate biases kernel |
| 220 | _accumulate_biases_kernel.configure(output, biases); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 223 | if(_linearize_input) |
| 224 | { |
| 225 | _im2col_output.allocator()->allocate(); |
| 226 | } |
| 227 | |
| 228 | if(_is_batched_fc_layer) |
| 229 | { |
| 230 | _interleave4x4_output.allocator()->allocate(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 234 | Status NEFullyConnectedLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, bool transpose_weights, bool are_weights_reshaped) |
| 235 | { |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 236 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 237 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, weights, output); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 238 | |
| 239 | const int num_batch_dimensions = std::max(0, static_cast<int>(output->tensor_shape().num_dimensions()) - 1); |
| 240 | const int num_input_dimensions = input->tensor_shape().num_dimensions() - num_batch_dimensions; |
| 241 | const size_t linear_input_size = input->tensor_shape().total_size_lower(num_input_dimensions); |
| 242 | |
| 243 | const bool linearize_input = (input->tensor_shape().x() != linear_input_size) || (num_input_dimensions > 1 && linear_input_size == 1); |
| 244 | const bool accumulate_biases = biases != nullptr; |
| 245 | const bool is_batched_fc_layer = num_batch_dimensions > 0; |
| 246 | |
| 247 | ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().total_size_upper(num_input_dimensions) != output->tensor_shape().total_size_upper(1)); |
| 248 | ARM_COMPUTE_RETURN_ERROR_ON(weights->num_dimensions() > 2); |
| 249 | |
| 250 | const size_t interleave_width = 16 / input->element_size(); |
| 251 | const ITensorInfo *weights_to_use = weights; |
| 252 | std::unique_ptr<ITensorInfo> reshape_weights_output = input->clone(); |
| 253 | |
| 254 | if(!are_weights_reshaped && (transpose_weights || is_batched_fc_layer)) |
| 255 | { |
| 256 | reshape_weights_output->set_tensor_shape(compute_fully_connected_reshaped_weights_shape(weights, transpose_weights, is_batched_fc_layer, interleave_width)); |
| 257 | |
| 258 | ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayerReshapeWeights::validate(weights, reshape_weights_output.get(), transpose_weights, is_batched_fc_layer)); |
| 259 | |
| 260 | weights_to_use = reshape_weights_output.get(); |
| 261 | } |
| 262 | |
| 263 | // Check correct shape of weights |
| 264 | if(is_batched_fc_layer) |
| 265 | { |
| 266 | // Transpose + Transpose1xW |
| 267 | ARM_COMPUTE_RETURN_ERROR_ON(weights_to_use->tensor_shape().x() != linear_input_size * interleave_width); |
| 268 | ARM_COMPUTE_RETURN_ERROR_ON(weights_to_use->tensor_shape().y() != static_cast<unsigned int>(std::ceil(static_cast<float>(output->tensor_shape().x()) / interleave_width))); |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | // Transpose |
| 273 | ARM_COMPUTE_RETURN_ERROR_ON(weights_to_use->tensor_shape().x() != output->tensor_shape().x()); |
| 274 | ARM_COMPUTE_RETURN_ERROR_ON(weights_to_use->tensor_shape().y() != linear_input_size); |
| 275 | } |
| 276 | |
| 277 | const ITensorInfo *multiply_input = input; |
| 278 | std::unique_ptr<ITensorInfo> im2col_output = input->clone(); |
| 279 | std::unique_ptr<ITensorInfo> interleave4x4_output = input->clone(); |
| 280 | |
| 281 | if(linearize_input) |
| 282 | { |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 283 | im2col_output->set_tensor_shape(compute_im2col_fc_shape(input, num_input_dimensions)); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 284 | |
| 285 | ARM_COMPUTE_RETURN_ON_ERROR(NEIm2ColKernel::validate(input, im2col_output.get(), Size2D(1, 1), PadStrideInfo(1, 1, 0, 0), false, true)); |
| 286 | |
| 287 | multiply_input = im2col_output.get(); |
| 288 | } |
| 289 | |
| 290 | int m = multiply_input->dimension(1); |
| 291 | int k = multiply_input->dimension(0); |
| 292 | |
| 293 | if(is_batched_fc_layer) |
| 294 | { |
| 295 | interleave4x4_output->set_tensor_shape(compute_interleaved_shape(*multiply_input)); |
| 296 | |
| 297 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMInterleave4x4Kernel::validate(multiply_input, interleave4x4_output.get())); |
| 298 | |
| 299 | multiply_input = interleave4x4_output.get(); |
| 300 | } |
| 301 | |
| 302 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixMultiplyKernel::validate(multiply_input, weights_to_use, output, 1.0f, is_batched_fc_layer, GEMMReshapeInfo(m, 0 /* no transpose */, k))); |
| 303 | |
| 304 | if(accumulate_biases) |
| 305 | { |
| 306 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 307 | ARM_COMPUTE_RETURN_ERROR_ON(biases->tensor_shape().x() != output->tensor_shape().x()); |
| 308 | |
| 309 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMMMatrixAccumulateBiasesKernel::validate(output, biases)); |
| 310 | } |
| 311 | |
| 312 | return Status{}; |
| 313 | } |
| 314 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 315 | void NEFullyConnectedLayer::run() |
| 316 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 317 | prepare(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 318 | |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 319 | _memory_group.acquire(); |
| 320 | |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 321 | // Linearize input if it comes from a convolutional layer |
| 322 | if(_linearize_input) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 323 | { |
| 324 | NEScheduler::get().schedule(&_im2col_kernel, Window::DimY); |
| 325 | } |
| 326 | |
| 327 | // Interleave input |
| 328 | if(_is_batched_fc_layer) |
| 329 | { |
| 330 | NEScheduler::get().schedule(&_interleave4x4_kernel, Window::DimY); |
| 331 | } |
| 332 | |
| 333 | // Run matrix multiply |
| 334 | NEScheduler::get().schedule(&_mm_kernel, _is_batched_fc_layer ? Window::DimY : Window::DimX); |
| 335 | |
| 336 | // Accumulate biases if provided |
| 337 | if(_accumulate_biases) |
| 338 | { |
| 339 | NEScheduler::get().schedule(&_accumulate_biases_kernel, Window::DimY); |
| 340 | } |
Georgios Pinitas | baf174e | 2017-09-08 19:47:30 +0100 | [diff] [blame] | 341 | |
| 342 | _memory_group.release(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 343 | } |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 344 | |
| 345 | void NEFullyConnectedLayer::prepare() |
| 346 | { |
| 347 | // Reshape of the weights (happens only once) |
| 348 | if(!_is_prepared) |
| 349 | { |
| 350 | ARM_COMPUTE_ERROR_ON(!_original_weights->is_used()); |
| 351 | |
| 352 | // Run weights reshape, clean internal tensors and mark original weights tensor as unused |
| 353 | _reshape_weights_output.allocator()->allocate(); |
| 354 | _reshape_weights_function.run(); |
| 355 | _reshape_weights_function = NEFullyConnectedLayerReshapeWeights(); |
| 356 | _original_weights->mark_as_unused(); |
| 357 | |
| 358 | _is_prepared = true; |
| 359 | } |
| 360 | } |