• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "extendrt/delegate/graph_executor/litert/func_graph_reuse_manager.h"
18 #include <utility>
19 #include "src/common/common.h"
20 namespace mindspore {
21 std::mutex mtx_manager_;
22 
GetInstance()23 FuncGraphReuseManager *FuncGraphReuseManager::GetInstance() {
24   std::unique_lock<std::mutex> l(mtx_manager_);
25   static FuncGraphReuseManager instance;
26   return &instance;
27 }
28 
GetSharedFuncGraph(std::map<std::string,std::map<std::string,std::string>> config_info)29 FuncGraphPtr FuncGraphReuseManager::GetSharedFuncGraph(
30   std::map<std::string, std::map<std::string, std::string>> config_info) {
31   std::unique_lock<std::mutex> l(mtx_manager_);
32   auto id = config_info.find(mindspore::lite::kInnerModelParallelRunnerSection);
33   if (id != config_info.end()) {
34     if (id->second.find(lite::kInnerRunnerIDKey) != id->second.end()) {
35       auto runner_id = id->second[lite::kInnerRunnerIDKey];
36       if (all_func_graphs_.find(runner_id) != all_func_graphs_.end()) {
37         auto func_graph = all_func_graphs_[runner_id];
38         return func_graph;
39       }
40     } else {
41       MS_LOG(ERROR) << "config info not find runner id.";
42       return nullptr;
43     }
44   }
45   MS_LOG(INFO) << "can not find model buf in all store function graphs";
46   return nullptr;
47 }
48 
StoreFuncGraph(FuncGraphPtr func_graph,std::map<std::string,std::map<std::string,std::string>> config_info)49 Status FuncGraphReuseManager::StoreFuncGraph(FuncGraphPtr func_graph,
50                                              std::map<std::string, std::map<std::string, std::string>> config_info) {
51   std::unique_lock<std::mutex> l(mtx_manager_);
52   auto id = config_info.find(lite::kInnerModelParallelRunnerSection);
53   if (id != config_info.end()) {
54     if (id->second.find(lite::kInnerRunnerIDKey) != id->second.end()) {
55       auto runner_id = id->second[lite::kInnerRunnerIDKey];
56       all_func_graphs_[runner_id] = func_graph;
57       return kSuccess;
58     }
59   }
60   return kSuccess;
61 }
62 
GetFbModelBuf(size_t * data_size,bool * is_shared_fb_buf,std::map<std::string,std::map<std::string,std::string>> config_info)63 std::pair<void *, std::shared_ptr<mindspore::infer::helper::InferHelpers>> FuncGraphReuseManager::GetFbModelBuf(
64   size_t *data_size, bool *is_shared_fb_buf, std::map<std::string, std::map<std::string, std::string>> config_info) {
65   std::unique_lock<std::mutex> l(mtx_manager_);
66   auto id = config_info.find(lite::kInnerModelParallelRunnerSection);
67   if (id != config_info.end()) {
68     auto item_runner_id = id->second.find(lite::kInnerRunnerIDKey);
69     if (item_runner_id != id->second.end()) {
70       // get runner id
71       auto runner_id = id->second[lite::kInnerRunnerIDKey];
72       *is_shared_fb_buf = true;
73       if (all_fb_model_buf_.find(runner_id) != all_fb_model_buf_.end() &&
74           all_infer_helpers_.find(runner_id) != all_infer_helpers_.end()) {
75         *data_size = all_fb_model_buf_[runner_id].buf_size;
76         return std::make_pair(all_fb_model_buf_[runner_id].buf, all_infer_helpers_[runner_id]);
77       }
78     } else {
79       MS_LOG(ERROR) << "config info not find runner id, numa id or worker id.";
80       return std::make_pair(nullptr, nullptr);
81     }
82   }
83   MS_LOG(INFO) << "can not find model buf in all store Pb model buf";
84   return std::make_pair(nullptr, nullptr);
85 }
86 
StoreFbModelBuf(void * model_buf,size_t data_size,std::shared_ptr<mindspore::infer::helper::InferHelpers> helper,std::map<std::string,std::map<std::string,std::string>> config_info)87 Status FuncGraphReuseManager::StoreFbModelBuf(void *model_buf, size_t data_size,
88                                               std::shared_ptr<mindspore::infer::helper::InferHelpers> helper,
89                                               std::map<std::string, std::map<std::string, std::string>> config_info) {
90   std::unique_lock<std::mutex> l(mtx_manager_);
91   auto id = config_info.find(lite::kInnerModelParallelRunnerSection);
92   if (id != config_info.end()) {
93     auto item_runner_id = id->second.find(lite::kInnerRunnerIDKey);
94     if (item_runner_id != id->second.end()) {
95       auto runner_id = id->second[lite::kInnerRunnerIDKey];
96       ModelBufPair buf = {model_buf, data_size};
97       all_fb_model_buf_[runner_id] = buf;
98       all_infer_helpers_[runner_id] = helper;
99       return kSuccess;
100     }
101   }
102   return kLiteError;
103 }
104 
GetInOut(std::map<std::string,std::map<std::string,std::string>> config_info,std::vector<tensor::TensorPtr> * in_tensor,std::vector<tensor::TensorPtr> * out_tensor,std::vector<std::string> * in_name,std::vector<std::string> * out_name)105 Status FuncGraphReuseManager::GetInOut(std::map<std::string, std::map<std::string, std::string>> config_info,
106                                        std::vector<tensor::TensorPtr> *in_tensor,
107                                        std::vector<tensor::TensorPtr> *out_tensor, std::vector<std::string> *in_name,
108                                        std::vector<std::string> *out_name) {
109   std::unique_lock<std::mutex> l(mtx_manager_);
110   auto id = config_info.find(mindspore::lite::kInnerModelParallelRunnerSection);
111   if (id != config_info.end()) {
112     if (id->second.find(lite::kInnerRunnerIDKey) != id->second.end()) {
113       auto runner_id = id->second[lite::kInnerRunnerIDKey];
114       if (all_in_tensors_.find(runner_id) != all_in_tensors_.end() &&
115           all_out_tensors_.find(runner_id) != all_out_tensors_.end() &&
116           all_in_names_.find(runner_id) != all_in_names_.end() &&
117           all_out_names_.find(runner_id) != all_out_names_.end()) {
118         *in_tensor = all_in_tensors_[runner_id];
119         *out_tensor = all_out_tensors_[runner_id];
120         *in_name = all_in_names_[runner_id];
121         *out_name = all_out_names_[runner_id];
122         return kSuccess;
123       }
124     } else {
125       MS_LOG(ERROR) << "config info not find runner id.";
126       return kLiteError;
127     }
128   }
129   MS_LOG(INFO) << "can not find model buf in all store function graphs";
130   return kLiteError;
131 }
132 
StoreInOut(std::map<std::string,std::map<std::string,std::string>> config_info,std::vector<tensor::TensorPtr> in_tensor,std::vector<tensor::TensorPtr> out_tensor,std::vector<std::string> in_name,std::vector<std::string> out_name)133 Status FuncGraphReuseManager::StoreInOut(std::map<std::string, std::map<std::string, std::string>> config_info,
134                                          std::vector<tensor::TensorPtr> in_tensor,
135                                          std::vector<tensor::TensorPtr> out_tensor, std::vector<std::string> in_name,
136                                          std::vector<std::string> out_name) {
137   std::unique_lock<std::mutex> l(mtx_manager_);
138   auto id = config_info.find(lite::kInnerModelParallelRunnerSection);
139   if (id != config_info.end()) {
140     if (id->second.find(lite::kInnerRunnerIDKey) != id->second.end()) {
141       auto runner_id = id->second[lite::kInnerRunnerIDKey];
142       all_in_tensors_[runner_id] = in_tensor;
143       all_out_tensors_[runner_id] = out_tensor;
144       all_in_names_[runner_id] = in_name;
145       all_out_names_[runner_id] = out_name;
146       return kSuccess;
147     }
148   }
149   return kSuccess;
150 }
151 
ReleaseSharedFuncGraph(std::map<std::string,std::map<std::string,std::string>> config_info)152 void FuncGraphReuseManager::ReleaseSharedFuncGraph(
153   std::map<std::string, std::map<std::string, std::string>> config_info) {
154   std::unique_lock<std::mutex> l(mtx_manager_);
155   MS_LOG(INFO) << "ReleaseSharedFuncGraph begin.";
156   std::string runner_id = "";
157   auto id = config_info.find(lite::kInnerModelParallelRunnerSection);
158   if (id != config_info.end()) {
159     runner_id = id->second[lite::kInnerRunnerIDKey];
160   }
161   if (all_func_graphs_.find(runner_id) != all_func_graphs_.end()) {
162     MS_LOG(INFO) << "release shared function graph of runner id: " << runner_id;
163     all_func_graphs_.erase(runner_id);
164   }
165   if (all_infer_helpers_.find(runner_id) != all_infer_helpers_.end()) {
166     MS_LOG(INFO) << "release shared infer helpers of runner id: " << runner_id;
167     all_infer_helpers_.erase(runner_id);
168   }
169   if (all_in_names_.find(runner_id) != all_in_names_.end() && all_out_names_.find(runner_id) != all_out_names_.end() &&
170       all_in_tensors_.find(runner_id) != all_in_tensors_.end() &&
171       all_out_tensors_.find(runner_id) != all_out_tensors_.end()) {
172     MS_LOG(INFO) << "release shared input/output of runner id: " << runner_id;
173     all_in_names_.erase(runner_id);
174     all_out_names_.erase(runner_id);
175     all_in_tensors_.erase(runner_id);
176     all_out_tensors_.erase(runner_id);
177   }
178   if (all_fb_model_buf_.find(runner_id) != all_fb_model_buf_.end()) {
179     void *fb_model_buf = all_fb_model_buf_[runner_id].buf;
180     if (fb_model_buf != nullptr) {
181       free(fb_model_buf);
182       fb_model_buf = nullptr;
183     }
184     all_fb_model_buf_.erase(runner_id);
185   }
186   MS_LOG(INFO) << "ReleaseSharedFuncGraph end.";
187 }
188 
~FuncGraphReuseManager()189 FuncGraphReuseManager::~FuncGraphReuseManager() {
190   std::unique_lock<std::mutex> l(mtx_manager_);
191   MS_LOG(INFO) << "~FuncGraphReuseManager() begin.";
192   all_func_graphs_.clear();
193   all_infer_helpers_.clear();
194   all_in_tensors_.clear();
195   all_out_tensors_.clear();
196   all_in_names_.clear();
197   all_out_names_.clear();
198   for (auto &item : all_fb_model_buf_) {
199     auto &buf = item.second.buf;
200     free(buf);
201     buf = nullptr;
202   }
203   all_fb_model_buf_.clear();
204   MS_LOG(INFO) << "~FuncGraphReuseManager() end.";
205 }
206 }  // namespace mindspore
207