blob: c6ef6c643e11af65e10af3460cace76ff098ae2b [file] [log] [blame]
Anthony Barbier871448e2017-03-24 14:54:29 +00001/*
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/NEON/functions/NEFullyConnectedLayer.h"
25
26#include "arm_compute/core/Validate.h"
27#include "arm_compute/runtime/NEON/NEScheduler.h"
28
29using namespace arm_compute;
30
31NEFullyConnectedLayer::NEFullyConnectedLayer()
32 : _conv_function(), _gemm_function(), _transpose_kernel(), _acc_biases_kernel(), _run_func(), _weights_transposed(), _is_first_run(true), _run_acc_biases(false)
33{
34}
35
36void NEFullyConnectedLayer::configure(ITensor *input, ITensor *weights, const ITensor *biases, ITensor *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 = &NEFullyConnectedLayer::run_conv;
47 return;
48 }
49
50 TensorShape shape_trans(weights->info()->dimension(1), weights->info()->dimension(0));
51 TensorInfo tensor_info(shape_trans, 1, weights->info()->data_type());
52 _weights_transposed.allocator()->init(tensor_info);
53
54 _transpose_kernel.configure(weights, &_weights_transposed);
55 _gemm_function.configure(input, &_weights_transposed, nullptr, output, 1.0f, 0.0f);
56
57 if(biases != nullptr)
58 {
59 _acc_biases_kernel.configure(output, biases);
60 _run_acc_biases = true;
61 }
62
63 _run_func = &NEFullyConnectedLayer::run_fc;
64
65 // Allocate once all the configure methods have been called
66 _weights_transposed.allocator()->allocate();
67}
68
69void NEFullyConnectedLayer::run_conv()
70{
71 _conv_function.run();
72}
73
74void NEFullyConnectedLayer::run_fc()
75{
76 if(_is_first_run)
77 {
78 _is_first_run = false;
79 NEScheduler::get().multithread(&_transpose_kernel);
80 }
81
82 _gemm_function.run();
83
84 if(_run_acc_biases)
85 {
86 NEScheduler::get().multithread(&_acc_biases_kernel);
87 }
88}
89
90void NEFullyConnectedLayer::run()
91{
92 ARM_COMPUTE_ERROR_ON(_run_func == nullptr);
93 (this->*_run_func)();
94}