• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2019 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 "transform/graph_ir/df_graph_manager.h"
18 
19 #include <sstream>
20 
21 #ifndef ENABLE_LITE_ACL
22 #include "pipeline/jit/parse/python_adapter.h"
23 #include "pipeline/jit/pipeline.h"
24 #endif
25 #ifndef NO_DLIB
26 #include "tdt/tsd_client.h"
27 #endif
28 
29 namespace mindspore {
30 namespace transform {
DfGraphWrapper(const std::string & name,const int & id,const DfGraphPtr & graph_ptr,const OptionMap & options)31 DfGraphWrapper::DfGraphWrapper(const std::string &name, const int &id, const DfGraphPtr &graph_ptr,
32                                const OptionMap &options)
33     : name_(name), id_(id), graph_ptr_(graph_ptr), options_(options) {}
34 
DfGraphManager()35 DfGraphManager::DfGraphManager() {
36   graph_id_ = 0;
37   graph_runner_ptr_ = nullptr;
38   sess_ptr_ = nullptr;
39 }
40 
~DfGraphManager()41 DfGraphManager::~DfGraphManager() {
42   // in python first destroy after atexit but in c++ destoy before atexit
43   DeleteGraphRunner();
44   DeleteGeSession();
45   ClearGraph();
46 #ifndef ENABLE_LITE_ACL
47   parse::python_adapter::set_python_env_flag(false);
48 #endif
49 }
50 
GetInstance()51 DfGraphManager &DfGraphManager::GetInstance() {
52   static DfGraphManager instance;
53   return instance;
54 }
55 
GenerateId()56 int DfGraphManager::GenerateId() {
57   graph_id_++;
58   if (graph_id_ <= 0) {
59     graph_id_ = 1;
60   }
61   MS_LOG(INFO) << "Generate graph Id : " << graph_id_;
62   return graph_id_;
63 }
64 
AddGraph(const std::string & name,const DfGraphPtr & graph_ptr,const OptionMap & options)65 Status DfGraphManager::AddGraph(const std::string &name, const DfGraphPtr &graph_ptr, const OptionMap &options) {
66   std::lock_guard<std::mutex> lg(lock_);
67   if (name.empty()) {
68     MS_LOG(ERROR) << "The graph name is null, add graph failed";
69     return Status::INVALID_ARGUMENT;
70   }
71 
72   if (graph_ptr == nullptr) {
73     MS_LOG(INFO) << "The new graph {" << name << "}'s pointer is null, add graph failed";
74     return Status::INVALID_ARGUMENT;
75   }
76 
77   int id = GenerateId();
78   DfGraphWrapperPtr wrap_ptr = std::make_shared<DfGraphWrapper>(name, id, graph_ptr, options);
79   auto ret = graphs_.emplace(name, wrap_ptr);
80   if (ret.second == false) {
81     MS_LOG(WARNING) << "The graph name:{ " << name << " }is already exists! The old graph will be overwritten!!";
82     ret.first->second = wrap_ptr;
83   }
84   MS_LOG(INFO) << "Add graph " << name << " to GraphManager success!";
85   return Status::SUCCESS;
86 }
87 
GetAllGraphs()88 std::vector<DfGraphWrapperPtr> DfGraphManager::GetAllGraphs() {
89   std::lock_guard<std::mutex> lg(lock_);
90   std::vector<DfGraphWrapperPtr> ret;
91   std::stringstream ss;
92   ss << "{ ";
93   for (auto it = graphs_.begin(); it != graphs_.end(); ++it) {
94     ss << it->first << ", ";
95     ret.emplace_back(it->second);
96   }
97   ss << "}";
98   MS_LOG(INFO) << "Return graphs: " << ss.str();
99   return ret;
100 }
GetSavedGraphs()101 std::set<string> DfGraphManager::GetSavedGraphs() { return saved_graphs_; }
102 
AddSavedGraphs(const std::string & id)103 void DfGraphManager::AddSavedGraphs(const std::string &id) { saved_graphs_.insert(id); }
104 
GetGraphByName(const std::string & name)105 DfGraphWrapperPtr DfGraphManager::GetGraphByName(const std::string &name) {
106   std::lock_guard<std::mutex> lg(lock_);
107   if (name.empty()) {
108     MS_LOG(ERROR) << "The graph name is null";
109     return nullptr;
110   }
111 
112   auto it = graphs_.find(name);
113   if (it == graphs_.end()) {
114     MS_LOG(INFO) << "Can't found graph name: " << name;
115     return nullptr;
116   }
117   MS_LOG(INFO) << "Return graph: " << name;
118   return it->second;
119 }
120 
ClearGraph()121 void DfGraphManager::ClearGraph() noexcept {
122   std::lock_guard<std::mutex> lg(lock_);
123   graphs_.clear();
124   anf_graphs_.clear();
125   MS_LOG(INFO) << "Remove all graphs in GraphManager";
126 }
127 
SetAnfGraph(const std::string & name,const AnfGraphPtr & anf_graph_ptr)128 void DfGraphManager::SetAnfGraph(const std::string &name, const AnfGraphPtr &anf_graph_ptr) {
129   DfGraphWrapperPtr df_graph = GetGraphByName(name);
130   if (df_graph == nullptr) {
131     MS_LOG(ERROR) << "Can't found graph name: " << name;
132     return;
133   }
134   std::lock_guard<std::mutex> lg(lock_);
135   anf_graphs_[df_graph->id_] = anf_graph_ptr;
136 }
137 
GetAnfGraph(uint32_t graph_id)138 AnfGraphPtr DfGraphManager::GetAnfGraph(uint32_t graph_id) {
139   std::lock_guard<std::mutex> lg(lock_);
140   auto iter = anf_graphs_.find(graph_id);
141   if (iter == anf_graphs_.end()) {
142     MS_LOG(ERROR) << "Can't found anf graph, graph_id = " << graph_id;
143     return nullptr;
144   }
145 
146   return iter->second;
147 }
148 
EraseAnfGraph()149 void DfGraphManager::EraseAnfGraph() {
150   std::lock_guard<std::mutex> lg(lock_);
151   anf_graphs_.clear();
152 }
153 
SetGeSession(const std::shared_ptr<ge::Session> & sess_ptr)154 void DfGraphManager::SetGeSession(const std::shared_ptr<ge::Session> &sess_ptr) {
155   std::lock_guard<std::mutex> lg(lock_);
156   if (sess_ptr == nullptr) {
157     MS_LOG(WARNING) << "You are adding a empty Ge Session";
158   }
159 
160   if (sess_ptr_ == nullptr) {
161     MS_LOG(INFO) << "Add a new Ge Session success";
162   } else {
163     MS_LOG(INFO) << "Add a new Ge Session success, the old Ge Session will be overwritten!!";
164   }
165   sess_ptr_ = sess_ptr;
166 }
167 
GetGeSession()168 std::shared_ptr<ge::Session> DfGraphManager::GetGeSession() {
169   std::lock_guard<std::mutex> lg(lock_);
170   return sess_ptr_;
171 }
172 
DeleteGeSession()173 void DfGraphManager::DeleteGeSession() noexcept {
174   std::lock_guard<std::mutex> lg(lock_);
175   if (sess_ptr_ == nullptr) {
176     MS_LOG(INFO) << "Ge Session is not exist";
177   } else {
178     sess_ptr_ = nullptr;
179     saved_graphs_.clear();
180     MS_LOG(INFO) << "Delete Ge Session success";
181   }
182 }
183 
SetGraphRunner(const std::shared_ptr<transform::GraphRunner> & graph_runner_ptr)184 void DfGraphManager::SetGraphRunner(const std::shared_ptr<transform::GraphRunner> &graph_runner_ptr) noexcept {
185   std::lock_guard<std::mutex> lg(lock_);
186   if (graph_runner_ptr == nullptr) {
187     MS_LOG(WARNING) << "You are adding a empty GraphRunner";
188   }
189 
190   if (graph_runner_ptr_ == nullptr) {
191     MS_LOG(INFO) << "Add a new GraphRunner success";
192   } else {
193     MS_LOG(INFO) << "Add a new GraphRunner success, the old GraphRunner will be overwritten!!";
194   }
195   graph_runner_ptr_ = graph_runner_ptr;
196 }
197 
GetGraphRunner()198 std::shared_ptr<transform::GraphRunner> DfGraphManager::GetGraphRunner() {
199   std::lock_guard<std::mutex> lg(lock_);
200   return graph_runner_ptr_;
201 }
202 
DeleteGraphRunner()203 void DfGraphManager::DeleteGraphRunner() noexcept {
204   std::lock_guard<std::mutex> lg(lock_);
205   if (graph_runner_ptr_ == nullptr) {
206     MS_LOG(INFO) << "GraphRunner is not exist";
207   } else {
208     graph_runner_ptr_ = nullptr;
209     MS_LOG(INFO) << "Delete GraphRunner success";
210   }
211 }
212 }  // namespace transform
213 }  // namespace mindspore
214