Compute Library
 23.11
NEFullyConnectedLayer.cpp
Go to the documentation of this file.
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  */
25 
30 
31 #include "src/common/utils/Log.h"
34 
35 namespace arm_compute
36 {
37 using namespace arm_compute::experimental;
38 
39 struct NEFullyConnectedLayer::Impl
40 {
41  MemoryGroup memory_group{};
42  IWeightsManager *weights_manager{nullptr};
43 
44  std::unique_ptr<cpu::CpuFullyConnected> op{nullptr};
45 
46  const ITensor *original_weights{nullptr};
47 
48  ITensorPack run_pack{};
49  WorkspaceData<Tensor> workspace{};
51 
52  bool is_prepared{false};
53  bool dynamic_weights{false};
54 };
55 
57 
58 NEFullyConnectedLayer::NEFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager,
59  IWeightsManager *weights_manager)
60  : _impl(std::make_unique<Impl>())
61 {
62  _impl->memory_group = MemoryGroup(std::move(memory_manager));
63  _impl->weights_manager = weights_manager;
64 }
65 
67  const ITensor *weights,
68  const ITensor *biases,
69  ITensor *output,
72 {
73  // Perform validate step
74  ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
76  biases != nullptr ? biases->info() : nullptr,
77  output->info(), fc_info, weights_info));
78  ARM_COMPUTE_LOG_PARAMS(input, weights, biases, output, fc_info);
79 
80  _impl->op = std::make_unique<cpu::CpuFullyConnected>();
81  _impl->original_weights = weights;
82  _impl->is_prepared = false;
83 
84  _impl->op->configure(input->info(), weights->info(), (biases != nullptr) ? biases->info() : nullptr, output->info(),
85  fc_info, weights_info);
86 
87  if (_impl->weights_manager != nullptr)
88  {
89  _impl->weights_manager->manage(_impl->original_weights);
90  }
91 
92  _impl->aux_mem_req = _impl->op->workspace();
93  _impl->run_pack = {{ACL_SRC_0, input}, {ACL_SRC_1, weights}, {ACL_SRC_2, biases}, {ACL_DST, output}};
94  _impl->workspace =
95  manage_workspace<Tensor>(_impl->aux_mem_req, _impl->memory_group, _impl->run_pack, _impl->run_pack);
96 
97  _impl->dynamic_weights = !weights->info()->are_values_constant() && fc_info.transpose_weights &&
98  !fc_info.are_weights_reshaped && !fc_info.retain_internal_weights;
99 }
100 
102  const ITensorInfo *input,
103  const ITensorInfo *weights,
104  const ITensorInfo *biases,
105  const ITensorInfo *output,
106  const FullyConnectedLayerInfo &fc_info,
107  const WeightsInfo &weights_info)
108 {
109  return cpu::CpuFullyConnected::has_opt_impl(expected_weight_format, input, weights, biases, output, fc_info,
110  weights_info);
111 }
112 
114  const ITensorInfo *weights,
115  const ITensorInfo *biases,
116  const ITensorInfo *output,
117  FullyConnectedLayerInfo fc_info,
118  const WeightsInfo &weights_info)
119 {
120  return cpu::CpuFullyConnected::validate(input, weights, biases, output, fc_info, weights_info);
121 }
122 
124 {
125  if (!_impl->dynamic_weights)
126  {
127  prepare();
128  }
129 
130  MemoryGroupResourceScope scope_mg(_impl->memory_group);
131  _impl->op->run(_impl->run_pack);
132 }
133 
135 {
136  if (!_impl->is_prepared)
137  {
138  _impl->op->prepare(_impl->run_pack);
139 
140  // Release temporary tensors that are only used in prepare stage
141  release_temporaries<Tensor>(_impl->aux_mem_req, _impl->workspace);
142  _impl->is_prepared = true;
143 
144  // Handle weights managed infrastructure
145  if (_impl->weights_manager != nullptr && _impl->weights_manager->are_weights_managed(_impl->original_weights))
146  {
147  // Ensure that b gets marked as unused (memory released) only after the last function which uses b also finishes its prepare
148  // This is for cases where multiple functions share the same b (weights)
149  // 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
150  const ITensor *original_b = _impl->original_weights;
151  if (!original_b->is_used())
152  {
153  _impl->weights_manager->pre_mark_as_unused(original_b);
154  }
155  _impl->original_weights->mark_as_used();
156  _impl->weights_manager->release(_impl->original_weights);
157  }
158  }
159 }
160 } // namespace arm_compute
ITensorPack.h
arm_compute::experimental::MemoryRequirements
std::vector< MemoryInfo > MemoryRequirements
Definition: Types.h:123
arm_compute::WeightsInfo
Convolution Layer Weights Information class.
Definition: Types.h:1670
arm_compute::test::validation::weights_info
weights_info
Definition: BatchNormalizationLayer.cpp:165
arm_compute::cpu::CpuFullyConnected::has_opt_impl
static Status has_opt_impl(arm_compute::WeightFormat &expected_weight_format, const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, FullyConnectedLayerInfo fc_info, WeightsInfo weights_info)
Static function that queries whether there exists fixed-format kernel and if it exists it will return...
Definition: CpuFullyConnected.cpp:359
CpuFullyConnected.h
arm_compute::ITensor
Interface for CPU tensor.
Definition: ITensor.h:36
arm_compute::ACL_SRC_0
@ ACL_SRC_0
Definition: Types.h:45
arm_compute::ACL_SRC_1
@ ACL_SRC_1
Definition: Types.h:46
arm_compute::ACL_SRC_2
@ ACL_SRC_2
Definition: Types.h:47
MemoryGroup.h
arm_compute::FullyConnectedLayerInfo
Fully connected layer info.
Definition: FullyConnectedLayerInfo.h:33
ARM_COMPUTE_ERROR_ON_NULLPTR
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
arm_compute::ITensor::info
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
arm_compute::NEFullyConnectedLayer::prepare
void prepare() override
Prepare the function for executing.
Definition: NEFullyConnectedLayer.cpp:134
arm_compute::WeightFormat
WeightFormat
Memory layouts for the weights tensor.
Definition: CoreTypes.h:311
ARM_COMPUTE_ERROR_THROW_ON
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
arm_compute::FullyConnectedLayerInfo::are_weights_reshaped
bool are_weights_reshaped
Definition: FullyConnectedLayerInfo.h:40
arm_compute::NEFullyConnectedLayer::configure
void configure(const ITensor *input, const ITensor *weights, const ITensor *biases, ITensor *output, FullyConnectedLayerInfo fc_info=FullyConnectedLayerInfo(), const WeightsInfo &weights_info=WeightsInfo())
Set the input and output tensors.
Definition: NEFullyConnectedLayer.cpp:66
arm_compute::ACL_DST
@ ACL_DST
Definition: Types.h:55
arm_compute::NEFullyConnectedLayer::NEFullyConnectedLayer
NEFullyConnectedLayer(std::shared_ptr< IMemoryManager > memory_manager=nullptr, IWeightsManager *weights_manager=nullptr)
Constructor.
Definition: NEFullyConnectedLayer.cpp:58
arm_compute::NEFullyConnectedLayer::~NEFullyConnectedLayer
~NEFullyConnectedLayer()
Default destructor.
arm_compute::Status
Status class.
Definition: Error.h:52
arm_compute::NEFullyConnectedLayer::run
void run() override
Run the kernels contained in the function.
Definition: NEFullyConnectedLayer.cpp:123
MemoryHelpers.h
arm_compute::experimental
Definition: Types.h:83
arm_compute::cpu::CpuFullyConnected::validate
static Status validate(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *dst, FullyConnectedLayerInfo fc_info=FullyConnectedLayerInfo(), const WeightsInfo &weights_info=WeightsInfo())
Static function to check if given info will lead to a valid configuration of CpuFullyConnected.
Definition: CpuFullyConnected.cpp:376
arm_compute::NEFullyConnectedLayer::has_opt_impl
static Status 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)
Static function that queries whether fixed-format kernel exists for a given problem description.
Definition: NEFullyConnectedLayer.cpp:101
arm_compute::ITensor::is_used
bool is_used() const
Flags if the tensor is used or not.
Definition: ITensor.cpp:162
arm_compute::MemoryGroupResourceScope
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
NEFullyConnectedLayer.h
Log.h
arm_compute::FullyConnectedLayerInfo::retain_internal_weights
bool retain_internal_weights
Retain internal reshaped weights.
Definition: FullyConnectedLayerInfo.h:41
arm_compute::ITensorInfo
Store the tensor's metadata.
Definition: ITensorInfo.h:44
NEConvertFullyConnectedWeights.h
arm_compute::FullyConnectedLayerInfo::transpose_weights
bool transpose_weights
Transpose weights if true.
Definition: FullyConnectedLayerInfo.h:39
arm_compute::ITensorInfo::are_values_constant
virtual bool are_values_constant() const =0
Flag indicating whether the values of the tensor are constant, meaning that they can change on kernel...
arm_compute::NEFullyConnectedLayer::validate
static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, FullyConnectedLayerInfo fc_info=FullyConnectedLayerInfo(), const WeightsInfo &weights_info=WeightsInfo())
Static function to check if given info will lead to a valid configuration of NEFullyConnectedLayer.
Definition: NEFullyConnectedLayer.cpp:113
arm_compute::IWeightsManager
Weights manager interface to handle weights transformations.
Definition: IWeightsManager.h:36
ARM_COMPUTE_LOG_PARAMS
#define ARM_COMPUTE_LOG_PARAMS(...)
Definition: Log.h:35
Validate.h
arm_compute::MemoryGroup
Memory group.
Definition: MemoryGroup.h:42
arm_compute::test::validation::input
auto input
Definition: LSTMLayerQuantized.cpp:486