Anthony Barbier | 871448e | 2017-03-24 14:54:29 +0000 | [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 | |
| 26 | #include "arm_compute/core/Validate.h" |
| 27 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 28 | |
| 29 | using namespace arm_compute; |
| 30 | |
| 31 | CLFullyConnectedLayer::CLFullyConnectedLayer() |
| 32 | : _conv_function(), _gemm_function(), _transpose_kernel(), _acc_biases_kernel(), _run_func(), _weights_transpose(), _is_first_run(true), _run_acc_biases(false) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | void CLFullyConnectedLayer::configure(ICLTensor *input, ICLTensor *weights, const ICLTensor *biases, ICLTensor *output) |
| 37 | { |
| 38 | ARM_COMPUTE_ERROR_ON((weights->info()->num_dimensions() != 2) && (weights->info()->num_dimensions() != 4)); |
| 39 | |
| 40 | // Make sure that in the fully connected layer connected to fully connected layer case, the first dimension of the weights and input are same. |
| 41 | ARM_COMPUTE_ERROR_ON((weights->info()->num_dimensions() == 2) && (input->info()->dimension(0) != weights->info()->dimension(0))); |
| 42 | |
| 43 | if(weights->info()->num_dimensions() != 2) |
| 44 | { |
| 45 | _conv_function.configure(input, weights, biases, output, PadStrideInfo(1, 1, 0, 0, DimensionRoundingType::FLOOR)); |
| 46 | _run_func = &CLFullyConnectedLayer::run_conv; |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | TensorShape shape_trans(weights->info()->dimension(1), weights->info()->dimension(0)); |
| 51 | _weights_transpose.allocator()->init(TensorInfo(shape_trans, 1, weights->info()->data_type())); |
| 52 | |
| 53 | // Configure kernels |
| 54 | _transpose_kernel.configure(weights, &_weights_transpose); |
| 55 | _gemm_function.configure(input, &_weights_transpose, nullptr, output, 1.0f, 0.0f); |
| 56 | if(biases != nullptr) |
| 57 | { |
| 58 | _acc_biases_kernel.configure(output, biases); |
| 59 | _run_acc_biases = true; |
| 60 | } |
| 61 | |
| 62 | _run_func = &CLFullyConnectedLayer::run_fc; |
| 63 | |
| 64 | // Allocate intermediate buffers |
| 65 | _weights_transpose.allocator()->allocate(); |
| 66 | } |
| 67 | |
| 68 | void CLFullyConnectedLayer::run_conv() |
| 69 | { |
| 70 | _conv_function.run(); |
| 71 | } |
| 72 | |
| 73 | void CLFullyConnectedLayer::run_fc() |
| 74 | { |
| 75 | if(_is_first_run) |
| 76 | { |
| 77 | _is_first_run = false; |
| 78 | CLScheduler::get().enqueue(_transpose_kernel); |
| 79 | } |
| 80 | |
| 81 | _gemm_function.run(); |
| 82 | |
| 83 | if(_run_acc_biases) |
| 84 | { |
| 85 | CLScheduler::get().enqueue(_acc_biases_kernel); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void CLFullyConnectedLayer::run() |
| 90 | { |
| 91 | ARM_COMPUTE_ERROR_ON(_run_func == nullptr); |
| 92 | (this->*_run_func)(); |
| 93 | } |