blob: 77028d96a2aed2301d51605c9c2fd51961daadda [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Teresa Charlind1dc09c2021-03-04 15:24:45 +00002 * Copyright (c) 2017-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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
Teresa Charlind1dc09c2021-03-04 15:24:45 +000026#include "arm_compute/core/ITensorPack.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Validate.h"
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010028#include "arm_compute/runtime/MemoryGroup.h"
29#include "arm_compute/runtime/NEON/functions/NEConvertFullyConnectedWeights.h"
ramelg01cbbb0382021-09-17 17:36:57 +010030#include "src/common/utils/Log.h"
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010031#include "src/core/helpers/MemoryHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010032#include "src/cpu/operators/CpuFullyConnected.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +000034namespace arm_compute
35{
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010036using namespace arm_compute::experimental;
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000037
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010038struct NEFullyConnectedLayer::Impl
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039{
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010040 MemoryGroup memory_group{};
41 IWeightsManager *weights_manager{ nullptr };
SiCongLi2e5fd632020-03-02 15:39:15 +000042
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010043 std::unique_ptr<cpu::CpuFullyConnected> op{ nullptr };
SiCongLi2e5fd632020-03-02 15:39:15 +000044
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010045 const ITensor *original_weights{ nullptr };
SiCongLi2e5fd632020-03-02 15:39:15 +000046
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010047 ITensorPack run_pack{};
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010048 WorkspaceData<Tensor> workspace{};
49 experimental::MemoryRequirements aux_mem_req{};
SiCongLi2e5fd632020-03-02 15:39:15 +000050
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010051 bool is_prepared{ false };
52};
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053
Michalis Spyrouebcebf12020-10-21 00:04:14 +010054NEFullyConnectedLayer::~NEFullyConnectedLayer() = default;
55
Michalis Spyrou1a569a32019-09-10 17:20:34 +010056NEFullyConnectedLayer::NEFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010057 : _impl(std::make_unique<Impl>())
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058{
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010059 _impl->memory_group = MemoryGroup(std::move(memory_manager));
60 _impl->weights_manager = weights_manager;
Giorgio Arenaa855af12018-07-16 17:20:38 +010061}
62
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010063void NEFullyConnectedLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output,
64 FullyConnectedLayerInfo fc_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010065{
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000066 // Perform validate step
Michele Di Giorgio9c700372020-01-08 11:33:44 +000067 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000068 ARM_COMPUTE_ERROR_THROW_ON(NEFullyConnectedLayer::validate(input->info(),
69 weights->info(),
70 biases != nullptr ? biases->info() : nullptr,
71 output->info(),
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010072 fc_info));
ramelg01cbbb0382021-09-17 17:36:57 +010073 ARM_COMPUTE_LOG_PARAMS(input, weights, biases, output, fc_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010075 _impl->op = std::make_unique<cpu::CpuFullyConnected>();
76 _impl->original_weights = weights;
77 _impl->is_prepared = false;
Moritz Pflanzer484e7b32017-08-09 11:43:18 +010078
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010079 _impl->op->configure(input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(), fc_info);
Michalis Spyrou1a569a32019-09-10 17:20:34 +010080
Georgios Pinitas45fbf9f2021-08-13 12:07:59 +010081 if(_impl->weights_manager != nullptr)
82 {
Giorgio Arena73df9312021-08-16 12:29:27 +010083 _impl->weights_manager->manage(_impl->original_weights);
Georgios Pinitas45fbf9f2021-08-13 12:07:59 +010084 }
85
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010086 _impl->aux_mem_req = _impl->op->workspace();
87 _impl->run_pack = { { ACL_SRC_0, input }, { ACL_SRC_1, weights }, { ACL_SRC_2, biases }, { ACL_DST, output } };
Georgios Pinitasfa1db172021-08-12 06:28:09 +010088 _impl->workspace = manage_workspace<Tensor>(_impl->aux_mem_req, _impl->memory_group, _impl->run_pack, _impl->run_pack);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089}
90
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010091Status NEFullyConnectedLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
92 FullyConnectedLayerInfo fc_info)
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000093{
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +010094 return cpu::CpuFullyConnected::validate(input, weights, biases, output, fc_info);
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000095}
96
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097void NEFullyConnectedLayer::run()
98{
Georgios Pinitas72219332018-06-05 14:56:06 +010099 prepare();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100101 MemoryGroupResourceScope scope_mg(_impl->memory_group);
102 _impl->op->run(_impl->run_pack);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103}
Georgios Pinitas72219332018-06-05 14:56:06 +0100104
105void NEFullyConnectedLayer::prepare()
106{
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100107 if(!_impl->is_prepared)
Georgios Pinitas72219332018-06-05 14:56:06 +0100108 {
Georgios Pinitasfa1db172021-08-12 06:28:09 +0100109 _impl->op->prepare(_impl->run_pack);
Georgios Pinitasef776a82018-07-25 17:57:49 +0100110
Michele Di Giorgiod9cdf142021-07-02 15:17:08 +0100111 // Release temporary tensors that are only used in prepare stage
112 release_temporaries<Tensor>(_impl->aux_mem_req, _impl->workspace);
113 _impl->is_prepared = true;
Georgios Pinitas45fbf9f2021-08-13 12:07:59 +0100114
115 // Handle weights managed infrastructure
116 if(_impl->weights_manager != nullptr && _impl->weights_manager->are_weights_managed(_impl->original_weights))
117 {
Giorgio Arena73df9312021-08-16 12:29:27 +0100118 // Ensure that b gets marked as unused (memory released) only after the last function which uses b also finishes its prepare
119 // This is for cases where multiple functions share the same b (weights)
120 // Therefore when a function marks original b as unused, we pre-mark it in weights manager, and mark it back to used so that it doesn't get released before its last reference
Georgios Pinitas45fbf9f2021-08-13 12:07:59 +0100121 const ITensor *original_b = _impl->original_weights;
122 if(!original_b->is_used())
123 {
Giorgio Arena73df9312021-08-16 12:29:27 +0100124 _impl->weights_manager->pre_mark_as_unused(original_b);
Georgios Pinitas45fbf9f2021-08-13 12:07:59 +0100125 }
126 _impl->original_weights->mark_as_used();
127 _impl->weights_manager->release(_impl->original_weights);
128 }
Georgios Pinitas72219332018-06-05 14:56:06 +0100129 }
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000130}
Georgios Pinitasc6aef872020-04-29 13:37:09 +0100131} // namespace arm_compute