• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019, 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/IWeightsManager.h"
25 
26 namespace arm_compute
27 {
IWeightsManager()28 IWeightsManager::IWeightsManager()
29     : _managed_weights(), _managed_counter(), _managed_weights_parents()
30 {
31 }
32 
manage(const ITensor * weights,ITransformWeights * parent)33 void IWeightsManager::manage(const ITensor *weights, ITransformWeights *parent)
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 }
55 
run(const ITensor * weights,ITransformWeights * weights_transform)56 ITensor *IWeightsManager::run(const ITensor *weights, ITransformWeights *weights_transform)
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 }
116 
are_weights_managed(const ITensor * weights)117 bool IWeightsManager::are_weights_managed(const ITensor *weights)
118 {
119     return (_managed_weights.find(weights) != _managed_weights.end());
120 }
121 
acquire(const ITensor * weights,ITransformWeights * weights_transform)122 ITensor *IWeightsManager::acquire(const ITensor *weights, ITransformWeights *weights_transform)
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 }
154 
release(const ITensor * weights)155 void IWeightsManager::release(const ITensor *weights)
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 }
168 
pre_mark_as_unused(const ITensor * weights)169 void IWeightsManager::pre_mark_as_unused(const ITensor *weights)
170 {
171     if(weights == nullptr || !are_weights_managed(weights))
172     {
173         return;
174     }
175 
176     _managed_counter[weights].is_unused = true;
177 }
178 } // namespace arm_compute
179