blob: 4f9759c590a0f9686bf24003f23f3c38339c46df [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Teresa Charlin27886092021-02-25 20:15:01 +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/CL/functions/CLFullyConnectedLayer.h"
25
Georgios Pinitas529b5a22021-07-27 15:55:30 +010026#include "arm_compute/core/CL/CLKernelLibrary.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/runtime/CL/CLScheduler.h"
Georgios Pinitas529b5a22021-07-27 15:55:30 +010028#include "src/core/helpers/MemoryHelpers.h"
29#include "src/runtime/gpu/cl/operators/ClFullyConnected.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030
Michalis Spyroub27e13a2019-09-27 11:04:27 +010031namespace arm_compute
32{
Georgios Pinitas529b5a22021-07-27 15:55:30 +010033using namespace arm_compute::experimental;
Georgios Pinitas358ca202017-12-07 16:47:52 +000034
Georgios Pinitas529b5a22021-07-27 15:55:30 +010035struct CLFullyConnectedLayer::Impl
Georgios Pinitas358ca202017-12-07 16:47:52 +000036{
Georgios Pinitas529b5a22021-07-27 15:55:30 +010037 MemoryGroup memory_group{};
38 IWeightsManager *weights_manager{ nullptr };
Georgios Pinitas8b721992019-10-28 16:24:28 +000039
Georgios Pinitas529b5a22021-07-27 15:55:30 +010040 std::unique_ptr<opencl::ClFullyConnected> op{ nullptr };
Sang-Hoon Parkb66aa3b2020-01-10 14:44:13 +000041
Georgios Pinitas529b5a22021-07-27 15:55:30 +010042 const ITensor *original_weights{ nullptr };
Georgios Pinitas8b721992019-10-28 16:24:28 +000043
Georgios Pinitas529b5a22021-07-27 15:55:30 +010044 ITensorPack run_pack{};
45 WorkspaceData<CLTensor> workspace{};
46 experimental::MemoryRequirements aux_mem_req{};
Georgios Pinitas8b721992019-10-28 16:24:28 +000047
Georgios Pinitas529b5a22021-07-27 15:55:30 +010048 bool is_prepared{ false };
49};
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010050
Michalis Spyrou1a569a32019-09-10 17:20:34 +010051CLFullyConnectedLayer::CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
Georgios Pinitas529b5a22021-07-27 15:55:30 +010052 : _impl(std::make_unique<Impl>())
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053{
Georgios Pinitas529b5a22021-07-27 15:55:30 +010054 _impl->memory_group = MemoryGroup(std::move(memory_manager));
55 _impl->weights_manager = weights_manager;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056}
Teresa Charlin27886092021-02-25 20:15:01 +000057
Georgios Pinitas529b5a22021-07-27 15:55:30 +010058CLFullyConnectedLayer::~CLFullyConnectedLayer() = default;
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010059
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010060void CLFullyConnectedLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
61 FullyConnectedLayerInfo fc_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062{
Manuel Bottini2b84be52020-04-08 10:15:51 +010063 configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, fc_info);
64}
65
66void CLFullyConnectedLayer::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
67 FullyConnectedLayerInfo fc_info)
68{
Georgios Pinitas358ca202017-12-07 16:47:52 +000069 // Perform validate step
Georgios Pinitas529b5a22021-07-27 15:55:30 +010070 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
Georgios Pinitas358ca202017-12-07 16:47:52 +000071 ARM_COMPUTE_ERROR_THROW_ON(CLFullyConnectedLayer::validate(input->info(),
72 weights->info(),
73 biases != nullptr ? biases->info() : nullptr,
74 output->info(),
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +010075 fc_info));
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010076
Georgios Pinitas529b5a22021-07-27 15:55:30 +010077 _impl->op = std::make_unique<opencl::ClFullyConnected>();
78 _impl->original_weights = weights;
Georgios Pinitasf5d51f32021-08-17 16:09:10 +010079 _impl->is_prepared = fc_info.retain_internal_weights;
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010080
Georgios Pinitas529b5a22021-07-27 15:55:30 +010081 _impl->op->configure(compile_context, input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(), fc_info);
Michalis Spyroub27e13a2019-09-27 11:04:27 +010082
Georgios Pinitas45fbf9f2021-08-13 12:07:59 +010083 if(_impl->weights_manager != nullptr)
84 {
Giorgio Arena73df9312021-08-16 12:29:27 +010085 _impl->weights_manager->manage(_impl->original_weights);
Georgios Pinitas45fbf9f2021-08-13 12:07:59 +010086 }
87
Georgios Pinitasf5d51f32021-08-17 16:09:10 +010088 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 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099}
100
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100101Status CLFullyConnectedLayer::validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output,
102 FullyConnectedLayerInfo fc_info)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000103{
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100104 return opencl::ClFullyConnected::validate(input, weights, biases, output, fc_info);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000105}
106
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107void CLFullyConnectedLayer::run()
108{
Georgios Pinitase0437672018-05-02 14:07:55 +0100109 prepare();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100111 MemoryGroupResourceScope scope_mg(_impl->memory_group);
112 _impl->op->run(_impl->run_pack);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113}
Georgios Pinitase0437672018-05-02 14:07:55 +0100114
115void CLFullyConnectedLayer::prepare()
116{
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100117 if(!_impl->is_prepared)
Georgios Pinitase0437672018-05-02 14:07:55 +0100118 {
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100119 _impl->op->prepare(_impl->run_pack);
Georgios Pinitase0437672018-05-02 14:07:55 +0100120
Georgios Pinitas529b5a22021-07-27 15:55:30 +0100121 // 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;
Georgios Pinitas45fbf9f2021-08-13 12:07:59 +0100124
125 // Handle weights managed infrastructure
126 if(_impl->weights_manager != nullptr && _impl->weights_manager->are_weights_managed(_impl->original_weights))
127 {
Giorgio Arena73df9312021-08-16 12:29:27 +0100128 // 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
Georgios Pinitas45fbf9f2021-08-13 12:07:59 +0100131 const ITensor *original_b = _impl->original_weights;
132 if(!original_b->is_used())
133 {
Giorgio Arena73df9312021-08-16 12:29:27 +0100134 _impl->weights_manager->pre_mark_as_unused(original_b);
Georgios Pinitas45fbf9f2021-08-13 12:07:59 +0100135 }
136 _impl->original_weights->mark_as_used();
137 _impl->weights_manager->release(_impl->original_weights);
138 }
Georgios Pinitase0437672018-05-02 14:07:55 +0100139 }
140}
Michalis Spyroub27e13a2019-09-27 11:04:27 +0100141} // namespace arm_compute