Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +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/graph/nodes/FullyConnectedLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/Helpers.h" |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Logger.h" |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 28 | #include "arm_compute/runtime/CL/functions/CLFullyConnectedLayer.h" |
| 29 | #include "arm_compute/runtime/NEON/functions/NEFullyConnectedLayer.h" |
| 30 | #include "support/ToolchainSupport.h" |
| 31 | #include "utils/TypePrinter.h" |
| 32 | |
| 33 | using namespace arm_compute::graph; |
| 34 | |
| 35 | namespace |
| 36 | { |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 37 | TensorShape calculate_fullyconnected_layer_output_shape(const TensorShape &input_shape, unsigned int output_neurons) |
| 38 | { |
| 39 | // Note: Only 1D batch space is supported at the moment |
| 40 | unsigned int batches = input_shape[1]; |
| 41 | if(input_shape.num_dimensions() > 2) |
| 42 | { |
| 43 | batches = input_shape[3]; |
| 44 | } |
| 45 | return TensorShape(output_neurons, batches); |
| 46 | } |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 47 | template <typename FullyConnectedType, typename TensorType, TargetHint target_hint> |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 48 | std::unique_ptr<arm_compute::IFunction> instantiate_function(ITensor *input, Tensor &weights, Tensor &biases, ITensor *output) |
| 49 | { |
| 50 | bool weights_are_loaded = weights.tensor() != nullptr; |
| 51 | bool biases_are_loaded = biases.tensor() != nullptr; |
| 52 | |
| 53 | auto conv = arm_compute::support::cpp14::make_unique<FullyConnectedType>(); |
| 54 | conv->configure( |
| 55 | dynamic_cast<TensorType *>(input), |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 56 | dynamic_cast<TensorType *>(weights.set_target(target_hint)), |
| 57 | dynamic_cast<TensorType *>(biases.set_target(target_hint)), |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 58 | dynamic_cast<TensorType *>(output)); |
| 59 | if(!weights_are_loaded) |
| 60 | { |
| 61 | weights.allocate_and_fill_if_needed(); |
| 62 | } |
| 63 | if(!biases_are_loaded) |
| 64 | { |
| 65 | biases.allocate_and_fill_if_needed(); |
| 66 | } |
| 67 | |
| 68 | return std::move(conv); |
| 69 | } |
| 70 | |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 71 | template <TargetHint target_hint> |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 72 | std::unique_ptr<arm_compute::IFunction> instantiate(ITensor *input, Tensor &weights, Tensor &biases, ITensor *output); |
| 73 | |
| 74 | template <> |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 75 | std::unique_ptr<arm_compute::IFunction> instantiate<TargetHint::OPENCL>(ITensor *input, Tensor &weights, Tensor &biases, ITensor *output) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 76 | { |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 77 | return instantiate_function<arm_compute::CLFullyConnectedLayer, arm_compute::CLTensor, TargetHint::OPENCL>(input, weights, biases, output); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | template <> |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 81 | std::unique_ptr<arm_compute::IFunction> instantiate<TargetHint::NEON>(ITensor *input, Tensor &weights, Tensor &biases, ITensor *output) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 82 | { |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 83 | return instantiate_function<arm_compute::NEFullyConnectedLayer, arm_compute::Tensor, TargetHint::NEON>(input, weights, biases, output); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 84 | } |
| 85 | } // namespace |
| 86 | |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 87 | std::unique_ptr<arm_compute::IFunction> FullyConnectedLayer::instantiate_node(GraphContext &ctx, ITensor *input, ITensor *output) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 88 | { |
| 89 | if(_weights.tensor() == nullptr) |
| 90 | { |
| 91 | unsigned int num_weights = 1; |
| 92 | unsigned int num_dimensions = input->info()->num_dimensions(); |
| 93 | // Ignore the batch dimension if there is one: |
| 94 | if(num_dimensions == 2 || num_dimensions == 4) |
| 95 | { |
| 96 | num_dimensions--; |
| 97 | } |
| 98 | for(unsigned int i = 0; i < num_dimensions; i++) |
| 99 | { |
| 100 | num_weights *= input->info()->dimension(i); |
| 101 | } |
| 102 | _weights.set_info(TensorInfo(TensorShape(num_weights, _num_neurons), input->info()->num_channels(), input->info()->data_type(), input->info()->fixed_point_position())); |
| 103 | } |
| 104 | if(_biases.tensor() == nullptr) |
| 105 | { |
| 106 | _biases.set_info(TensorInfo(TensorShape(_num_neurons), input->info()->num_channels(), input->info()->data_type(), input->info()->fixed_point_position())); |
| 107 | } |
| 108 | |
Georgios Pinitas | 6f669f0 | 2017-09-26 12:32:57 +0100 | [diff] [blame] | 109 | // Auto configure output |
| 110 | arm_compute::auto_init_if_empty(*output->info(), |
| 111 | calculate_fullyconnected_layer_output_shape(input->info()->tensor_shape(), _num_neurons), |
| 112 | input->info()->num_channels(), input->info()->data_type(), input->info()->fixed_point_position()); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 113 | |
| 114 | std::unique_ptr<arm_compute::IFunction> func; |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 115 | _target_hint = ctx.hints().target_hint(); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 116 | |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 117 | if(_target_hint == TargetHint::OPENCL) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 118 | { |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 119 | func = instantiate<TargetHint::OPENCL>(input, _weights, _biases, output); |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 120 | ARM_COMPUTE_LOG("Instantiating CLFullyConnectedLayer"); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 121 | } |
| 122 | else |
| 123 | { |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 124 | func = instantiate<TargetHint::NEON>(input, _weights, _biases, output); |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 125 | ARM_COMPUTE_LOG("Instantiating NEFullyConnectedLayer"); |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Michalis Spyrou | e472082 | 2017-10-02 17:44:52 +0100 | [diff] [blame] | 128 | ARM_COMPUTE_LOG(" Type: " << input->info()->data_type() |
| 129 | << " Input Shape: " << input->info()->tensor_shape() |
| 130 | << " Weights shape: " << _weights.info().tensor_shape() |
| 131 | << " Biases Shape: " << _biases.info().tensor_shape() |
| 132 | << " Output Shape: " << output->info()->tensor_shape() |
| 133 | << std::endl); |
| 134 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 135 | return func; |
| 136 | } |