You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

df_graph_manager.cc 6.4 kB

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