xref: /aosp_15_r20/external/ComputeLibrary/src/runtime/NEON/functions/NEFullyConnectedLayer.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2023 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/ITensorPack.h"
27 #include "arm_compute/core/Validate.h"
28 #include "arm_compute/runtime/MemoryGroup.h"
29 #include "arm_compute/runtime/NEON/functions/NEConvertFullyConnectedWeights.h"
30 #include "src/common/utils/Log.h"
31 #include "src/core/helpers/MemoryHelpers.h"
32 #include "src/cpu/operators/CpuFullyConnected.h"
33 
34 namespace arm_compute
35 {
36 using namespace arm_compute::experimental;
37 
38 struct NEFullyConnectedLayer::Impl
39 {
40     MemoryGroup      memory_group{};
41     IWeightsManager *weights_manager{ nullptr };
42 
43     std::unique_ptr<cpu::CpuFullyConnected> op{ nullptr };
44 
45     const ITensor *original_weights{ nullptr };
46 
47     ITensorPack                      run_pack{};
48     WorkspaceData<Tensor>            workspace{};
49     experimental::MemoryRequirements aux_mem_req{};
50 
51     bool is_prepared{ false };
52 };
53 
54 NEFullyConnectedLayer::~NEFullyConnectedLayer() = default;
55 
NEFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager,IWeightsManager * weights_manager)56 NEFullyConnectedLayer::NEFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
57     : _impl(std::make_unique<Impl>())
58 {
59     _impl->memory_group    = MemoryGroup(std::move(memory_manager));
60     _impl->weights_manager = weights_manager;
61 }
62 
configure(const ITensor * input,const ITensor * weights,const ITensor * biases,ITensor * output,FullyConnectedLayerInfo fc_info,const WeightsInfo & weights_info)63 void NEFullyConnectedLayer::configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output,
64                                       FullyConnectedLayerInfo fc_info, const WeightsInfo &weights_info)
65 {
66     // Perform validate step
67     ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
68     ARM_COMPUTE_ERROR_THROW_ON(NEFullyConnectedLayer::validate(input->info(),
69                                                                weights->info(),
70                                                                biases != nullptr ? biases->info() : nullptr,
71                                                                output->info(),
72                                                                fc_info,
73                                                                weights_info));
74     ARM_COMPUTE_LOG_PARAMS(input, weights, biases, output, fc_info);
75 
76     _impl->op               = std::make_unique<cpu::CpuFullyConnected>();
77     _impl->original_weights = weights;
78     _impl->is_prepared      = false;
79 
80     _impl->op->configure(input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(), fc_info, weights_info);
81 
82     if(_impl->weights_manager != nullptr)
83     {
84         _impl->weights_manager->manage(_impl->original_weights);
85     }
86 
87     _impl->aux_mem_req = _impl->op->workspace();
88     _impl->run_pack    = { { ACL_SRC_0, input }, { ACL_SRC_1, weights }, { ACL_SRC_2, biases }, { ACL_DST, output } };
89     _impl->workspace   = manage_workspace<Tensor>(_impl->aux_mem_req, _impl->memory_group, _impl->run_pack, _impl->run_pack);
90 }
91 
has_opt_impl(arm_compute::WeightFormat & expected_weight_format,const ITensorInfo * input,const ITensorInfo * weights,const ITensorInfo * biases,const ITensorInfo * output,const FullyConnectedLayerInfo & fc_info,const WeightsInfo & weights_info)92 Status NEFullyConnectedLayer::has_opt_impl(arm_compute::WeightFormat &expected_weight_format, const ITensorInfo *input, const ITensorInfo *weights,
93                                            const ITensorInfo *biases, const ITensorInfo *output, const FullyConnectedLayerInfo &fc_info,
94                                            const WeightsInfo &weights_info)
95 {
96     return cpu::CpuFullyConnected::has_opt_impl(expected_weight_format, input, weights, biases, output, fc_info, weights_info);
97 }
98 
validate(const ITensorInfo * input,const ITensorInfo * weights,const ITensorInfo * biases,const ITensorInfo * output,FullyConnectedLayerInfo fc_info,const WeightsInfo & weights_info)99 Status NEFullyConnectedLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
100                                        FullyConnectedLayerInfo fc_info, const WeightsInfo &weights_info)
101 {
102     return cpu::CpuFullyConnected::validate(input, weights, biases, output, fc_info, weights_info);
103 }
104 
run()105 void NEFullyConnectedLayer::run()
106 {
107     prepare();
108 
109     MemoryGroupResourceScope scope_mg(_impl->memory_group);
110     _impl->op->run(_impl->run_pack);
111 }
112 
prepare()113 void NEFullyConnectedLayer::prepare()
114 {
115     if(!_impl->is_prepared)
116     {
117         _impl->op->prepare(_impl->run_pack);
118 
119         // Release temporary tensors that are only used in prepare stage
120         release_temporaries<Tensor>(_impl->aux_mem_req, _impl->workspace);
121         _impl->is_prepared = true;
122 
123         // Handle weights managed infrastructure
124         if(_impl->weights_manager != nullptr && _impl->weights_manager->are_weights_managed(_impl->original_weights))
125         {
126             // Ensure that b gets marked as unused (memory released) only after the last function which uses b also finishes its prepare
127             // This is for cases where multiple functions share the same b (weights)
128             // 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
129             const ITensor *original_b = _impl->original_weights;
130             if(!original_b->is_used())
131             {
132                 _impl->weights_manager->pre_mark_as_unused(original_b);
133             }
134             _impl->original_weights->mark_as_used();
135             _impl->weights_manager->release(_impl->original_weights);
136         }
137     }
138 }
139 } // namespace arm_compute
140