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