Compute Library
 21.08
CLFullyConnectedLayer.cpp
Go to the documentation of this file.
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  */
25 
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{};
47 
48  bool is_prepared{ false };
49 };
50 
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 
59 
60 void CLFullyConnectedLayer::configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
62 {
63  configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, fc_info);
64 }
65 
66 void CLFullyConnectedLayer::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
68 {
69  // Perform validate step
70  ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
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(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 
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 
108 {
109  prepare();
110 
111  MemoryGroupResourceScope scope_mg(_impl->memory_group);
112  _impl->op->run(_impl->run_pack);
113 }
114 
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  // If function marks b as unused ensure that all prepare stages are done before releasing
129  const ITensor *original_b = _impl->original_weights;
130  if(!original_b->is_used())
131  {
132  _impl->weights_manager->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
CLFullyConnectedLayer(std::shared_ptr< IMemoryManager > memory_manager=nullptr, IWeightsManager *weights_manager=nullptr)
Constructor.
bool retain_internal_weights
Retain internal reshaped weights.
Definition: Types.h:1546
bool is_used() const
Flags if the tensor is used or not.
Definition: ITensor.cpp:163
Fully connected layer info.
Definition: Types.h:1538
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
Status class.
Definition: Error.h:52
Interface for CPU tensor.
Definition: ITensor.h:36
void prepare() override
Prepare the function for executing.
Copyright (c) 2017-2021 Arm Limited.
std::vector< MemoryInfo > MemoryRequirements
Definition: Types.h:113
~CLFullyConnectedLayer()
Default destructor.
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, FullyConnectedLayerInfo fc_info=FullyConnectedLayerInfo())
Static function to check if given info will lead to a valid configuration of CLFullyConnectedLayer.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
void run() override
Run the kernels contained in the function.
Weights manager interface to handle weights transformations.
static Status validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, FullyConnectedLayerInfo fc_info=FullyConnectedLayerInfo())
Static function to check if given info will lead to a valid configuration.
CLCompileContext class.
void configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, FullyConnectedLayerInfo fc_info=FullyConnectedLayerInfo())
Set the input and output tensors.
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157