Compute Library
 21.08
IWeightsManager Class Reference

Weights manager interface to handle weights transformations. More...

#include <IWeightsManager.h>

Public Member Functions

 IWeightsManager ()
 Constructor. More...
 
virtual ~IWeightsManager ()=default
 Default Destructor. More...
 
 IWeightsManager (const IWeightsManager &)=delete
 Prevent instances of this class to be copy constructed. More...
 
IWeightsManageroperator= (const IWeightsManager &)=delete
 Prevent instances of this class to be copied. More...
 
 IWeightsManager (IWeightsManager &&)=default
 Allow instances of this class to be move constructed. More...
 
IWeightsManageroperator= (IWeightsManager &&)=default
 Allow instances of this class to be moved. More...
 
void manage (const ITensor *weights, ITransformWeights *parent=nullptr)
 Start managing a weights tensor. More...
 
ITensorrun (const ITensor *weights, ITransformWeights *weights_transform)
 Run the reshape function. More...
 
ITensoracquire (const ITensor *weights, ITransformWeights *weights_transform)
 Acquire the requested reshape tensor of the selected weights. More...
 
bool are_weights_managed (const ITensor *weights)
 Check if the weights are managed. More...
 
void release (const ITensor *weights)
 Release weights refcount and mark as unused if reaches 0. More...
 
void mark_as_unused (const ITensor *weights)
 Marks weights as unused. More...
 

Detailed Description

Weights manager interface to handle weights transformations.

Definition at line 36 of file IWeightsManager.h.

Constructor & Destructor Documentation

◆ IWeightsManager() [1/3]

Constructor.

Definition at line 28 of file IWeightsManager.cpp.

29  : _managed_weights(), _managed_counter(), _managed_weights_parents()
30 {
31 }

◆ ~IWeightsManager()

virtual ~IWeightsManager ( )
virtualdefault

Default Destructor.

◆ IWeightsManager() [2/3]

IWeightsManager ( const IWeightsManager )
delete

Prevent instances of this class to be copy constructed.

◆ IWeightsManager() [3/3]

IWeightsManager ( IWeightsManager &&  )
default

Allow instances of this class to be move constructed.

Member Function Documentation

◆ acquire()

ITensor * acquire ( const ITensor weights,
ITransformWeights weights_transform 
)

Acquire the requested reshape tensor of the selected weights.

Parameters
[in]weightsPointer to the weights tensor to be managed
[in]weights_transformWeights transformation object

Definition at line 122 of file IWeightsManager.cpp.

References IWeightsManager::are_weights_managed(), ARM_COMPUTE_ERROR_ON_MSG, ITransformWeights::get_weights(), ITransformWeights::increase_refcount(), IWeightsManager::manage(), and ITransformWeights::uid().

123 {
124  ARM_COMPUTE_ERROR_ON_MSG(!are_weights_managed(weights), "Cannot acquire weights. Weights are not managed");
125 
126  ITensor *transformed_weights{ nullptr };
127  auto item = _managed_weights.find(weights);
128 
129  // Check if I already have the requested transform. If I do,
130  // increase the refcount of the transformed weights object and
131  // reuse the tensor
132  for(auto it : item->second)
133  {
134  if(it->uid() == weights_transform->uid())
135  {
136  transformed_weights = it->get_weights();
137  it->increase_refcount();
138  break;
139  }
140  }
141 
142  if(transformed_weights == nullptr)
143  {
144  transformed_weights = weights_transform->get_weights();
145  weights_transform->increase_refcount();
146  item->second.emplace_back(weights_transform);
147  }
148 
149  // Manage the weights and store link to the parent node
150  manage(transformed_weights, weights_transform);
151 
152  return transformed_weights;
153 }
void manage(const ITensor *weights, ITransformWeights *parent=nullptr)
Start managing a weights tensor.
bool are_weights_managed(const ITensor *weights)
Check if the weights are managed.
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456

◆ are_weights_managed()

bool are_weights_managed ( const ITensor weights)

Check if the weights are managed.

Parameters
[in]weightsPointer to the weights tensor we want to check if managed
Returns
True if the weights tensor is managed else false

Definition at line 117 of file IWeightsManager.cpp.

Referenced by IWeightsManager::acquire(), IWeightsManager::manage(), IWeightsManager::mark_as_unused(), IWeightsManager::release(), and IWeightsManager::run().

118 {
119  return (_managed_weights.find(weights) != _managed_weights.end());
120 }

◆ manage()

void manage ( const ITensor weights,
ITransformWeights parent = nullptr 
)

Start managing a weights tensor.

Parameters
[in]weightsPointer to the weights tensor to be managed
[in]parentParent node in case where the weights are coming from a previous reshape function

Definition at line 33 of file IWeightsManager.cpp.

References IWeightsManager::are_weights_managed().

Referenced by IWeightsManager::acquire().

34 {
35  if(!are_weights_managed(weights))
36  {
37  _managed_weights[weights];
38  _managed_counter[weights];
39  }
40  else
41  {
42  _managed_counter[weights].counter++;
43  }
44 
45  // In case the weights are an output of a previous reshape function
46  // store the parent's link
47  if(parent != nullptr)
48  {
49  if(_managed_weights_parents.find(weights) == _managed_weights_parents.end())
50  {
51  _managed_weights_parents[weights] = parent;
52  }
53  }
54 }
bool are_weights_managed(const ITensor *weights)
Check if the weights are managed.

◆ mark_as_unused()

void mark_as_unused ( const ITensor weights)

Marks weights as unused.

Parameters
weightsWeights to mark unused

Definition at line 169 of file IWeightsManager.cpp.

References IWeightsManager::are_weights_managed().

Referenced by IWeightsManager::run().

170 {
171  if(weights == nullptr || !are_weights_managed(weights))
172  {
173  return;
174  }
175 
176  _managed_counter[weights].is_unused = true;
177 }
bool are_weights_managed(const ITensor *weights)
Check if the weights are managed.

◆ operator=() [1/2]

IWeightsManager& operator= ( const IWeightsManager )
delete

Prevent instances of this class to be copied.

◆ operator=() [2/2]

IWeightsManager& operator= ( IWeightsManager &&  )
default

Allow instances of this class to be moved.

◆ release()

void release ( const ITensor weights)

Release weights refcount and mark as unused if reaches 0.

Parameters
[in]weightsWeights to release

Definition at line 155 of file IWeightsManager.cpp.

References IWeightsManager::are_weights_managed(), and ITensor::mark_as_unused().

156 {
157  if(weights == nullptr || !are_weights_managed(weights))
158  {
159  return;
160  }
161 
162  _managed_counter[weights].counter--;
163  if(_managed_counter[weights].counter == 0 && _managed_counter[weights].is_unused)
164  {
165  weights->mark_as_unused();
166  }
167 }
bool are_weights_managed(const ITensor *weights)
Check if the weights are managed.

◆ run()

ITensor * run ( const ITensor weights,
ITransformWeights weights_transform 
)

Run the reshape function.

Parameters
[in]weightsPointer to the weights tensor we want to reshape
[in]weights_transformWeights transformation object
Returns
The reshaped tensor

Definition at line 56 of file IWeightsManager.cpp.

References IWeightsManager::are_weights_managed(), ARM_COMPUTE_ERROR_ON_MSG, ITransformWeights::get_weights(), IWeightsManager::mark_as_unused(), ITensor::mark_as_unused(), ITransformWeights::run(), and ITransformWeights::uid().

57 {
58  ARM_COMPUTE_ERROR_ON_MSG(!are_weights_managed(weights), "Cannot run function. Weights are not managed");
59 
60  // Find if I have the same weights with weights transform. If I do, don't run the reshape
61  auto item = _managed_weights.find(weights);
62  bool perform_run{ true };
63  ITensor *weights_tensor{ nullptr };
64 
65  // Check if I already have the requested transform and I have run the reshape function
66  for(auto it : item->second)
67  {
68  if(it->is_reshape_run() && (it->uid() == weights_transform->uid()))
69  {
70  weights_tensor = it->get_weights();
71  perform_run = false;
72  break;
73  }
74  }
75 
76  if(perform_run)
77  {
78  weights_transform->run();
79  weights_tensor = weights_transform->get_weights();
80  }
81 
82  // Check if we can release memory from parent
83  auto parent_item = _managed_weights_parents.find(weights);
84  if(parent_item != _managed_weights_parents.end())
85  {
86  int32_t refcount = parent_item->second->decrease_refcount();
87  if(refcount == 0)
88  {
89  parent_item->second->release();
90  }
91  }
92 
93  // Check top level weights. If all the transformations are done
94  // mark the weights as unused
95  if(_managed_weights_parents.find(weights) == _managed_weights_parents.end())
96  {
97  auto item = _managed_weights.find(weights);
98  bool mark_as_unused = true;
99  for(auto it : item->second)
100  {
101  if(!it->is_reshape_run())
102  {
103  mark_as_unused = false;
104  break;
105  }
106  }
107 
108  if(mark_as_unused)
109  {
110  weights->mark_as_unused();
111  }
112  }
113 
114  return weights_tensor;
115 }
bool are_weights_managed(const ITensor *weights)
Check if the weights are managed.
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
void mark_as_unused(const ITensor *weights)
Marks weights as unused.

The documentation for this class was generated from the following files: