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