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.

graph.cc 13 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /**
  2. * Copyright 2019-2020 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 "external/graph/graph.h"
  17. #include "debug/ge_util.h"
  18. #include "framework/common/debug/ge_log.h"
  19. #include "graph/debug/ge_attr_define.h"
  20. #include "graph/debug/ge_op_types.h"
  21. #include "graph/model.h"
  22. #include "graph/utils/graph_utils.h"
  23. #include "graph/utils/op_desc_utils.h"
  24. using std::map;
  25. using std::pair;
  26. using std::string;
  27. using std::vector;
  28. namespace ge {
  29. class GraphImpl {
  30. public:
  31. friend class GraphUtils;
  32. GraphImpl(const GraphImpl &) = delete;
  33. GraphImpl &operator=(const GraphImpl &) = delete;
  34. explicit GraphImpl(const std::string &name) : name_(name) {}
  35. ~GraphImpl() {
  36. if (IsValid()) {
  37. if (compute_graph_ != nullptr) {
  38. GraphUtils::BreakConnect(compute_graph_->GetAllNodesInfo());
  39. }
  40. }
  41. for (const auto &it : op_list_) {
  42. Operator op = it.second;
  43. op.BreakConnect();
  44. }
  45. }
  46. graphStatus SetInputs(const std::vector<Operator> &inputs) {
  47. compute_graph_ = GraphUtils::CreateGraphFromOperator(name_, inputs);
  48. GE_CHK_BOOL_RET_STATUS(compute_graph_ != nullptr, GRAPH_FAILED, "Build Graph failed.");
  49. GE_CHK_BOOL_RET_STATUS(inputs.size() != 0, GRAPH_FAILED, "set input NULL.");
  50. compute_graph_->SetInputSize(static_cast<uint32_t>(inputs.size()));
  51. return GRAPH_SUCCESS;
  52. }
  53. graphStatus SetOutputs(const std::vector<Operator> &outputs) {
  54. if (compute_graph_ == nullptr) {
  55. GELOGE(GRAPH_FAILED, "set ComputeGraph failed.");
  56. return GRAPH_FAILED;
  57. }
  58. if (outputs.empty()) {
  59. GELOGW("set outputs size is 0.");
  60. return GRAPH_SUCCESS;
  61. }
  62. // Construct special output node
  63. std::vector<std::pair<Operator, std::vector<size_t>>> output_indexs;
  64. for (size_t i = 0; i < outputs.size(); ++i) {
  65. output_indexs.emplace_back(outputs[i], std::vector<size_t>{});
  66. }
  67. graphStatus ret = SetOutputs(output_indexs);
  68. return ret;
  69. }
  70. graphStatus SetOutputs(const std::vector<std::pair<Operator, std::vector<size_t>>> &output_indexs) {
  71. if (compute_graph_ == nullptr) {
  72. GELOGE(GRAPH_FAILED, "set ComputeGraph failed.");
  73. return GRAPH_FAILED;
  74. }
  75. if (output_indexs.empty()) {
  76. GELOGW("set outputs size is 0.");
  77. return GRAPH_SUCCESS;
  78. }
  79. // Construct special output node
  80. std::vector<std::pair<ge::NodePtr, int32_t>> output_nodes;
  81. for (const auto &item : output_indexs) {
  82. const Operator &output = item.first;
  83. const vector<size_t> &indexs = item.second;
  84. ge::NodePtr node = compute_graph_->FindNode(output.GetName());
  85. if (node == nullptr) {
  86. GELOGW("user designated out_node [%s] not exist in graph, will ignored!", output.GetName().c_str());
  87. continue;
  88. }
  89. ge::OpDescPtr tmp_op_ptr = node->GetOpDesc();
  90. GE_CHECK_NOTNULL_EXEC(tmp_op_ptr, continue);
  91. size_t out_size = tmp_op_ptr->GetOutputsSize();
  92. if (indexs.empty()) {
  93. for (size_t i = 0; i < out_size; ++i) {
  94. output_name_ += output.GetName() + ":" + std::to_string(i) + ";";
  95. output_nodes.emplace_back(node, i);
  96. }
  97. } else {
  98. for (size_t i = 0; i < indexs.size(); ++i) {
  99. if (indexs[i] >= out_size) {
  100. GELOGW("index[%zu] is not belong to out_node[%s]", indexs[i], output.GetName().c_str());
  101. } else {
  102. output_name_ += output.GetName() + ":" + std::to_string(i) + ";";
  103. output_nodes.emplace_back(node, indexs[i]);
  104. }
  105. }
  106. }
  107. }
  108. // Del last ";"
  109. if (!output_name_.empty()) {
  110. output_name_ = output_name_.substr(0, output_name_.length() - 1);
  111. }
  112. compute_graph_->SetUserDefOutput(output_name_);
  113. compute_graph_->SetOutputSize(static_cast<uint32_t>(output_indexs.size()));
  114. compute_graph_->SetGraphOutNodesInfo(output_nodes);
  115. return GRAPH_SUCCESS;
  116. }
  117. graphStatus SetOutputs(const std::vector<pair<Operator, string>> &outputs) {
  118. GE_CHK_BOOL_RET_STATUS(compute_graph_ != nullptr, GRAPH_FAILED, "set ComputeGraph faild.");
  119. GE_CHK_BOOL_EXEC_INFO(outputs.size() != 0, return GRAPH_SUCCESS, "set outputs size is 0.");
  120. // Construct specified output
  121. std::vector<std::pair<ge::NodePtr, int32_t>> output_nodes;
  122. for (auto item : outputs) {
  123. ge::NodePtr node = compute_graph_->FindNode(item.first.GetName());
  124. if (node == nullptr) {
  125. GELOGE(GRAPH_FAILED, " Warning, user designated out_node (%s) not exist in graph, this out_node ignored!",
  126. item.first.GetName().c_str());
  127. return GRAPH_FAILED;
  128. }
  129. ge::OpDescPtr tmp_op_ptr = node->GetOpDesc();
  130. GE_CHECK_NOTNULL_EXEC(tmp_op_ptr, continue);
  131. size_t out_size = tmp_op_ptr->GetOutputsSize();
  132. if (item.second.empty()) {
  133. for (size_t i = 0; i < out_size; ++i) {
  134. output_name_ += item.first.GetName() + ":" + std::to_string(i) + ";";
  135. output_nodes.push_back(std::make_pair(node, i));
  136. }
  137. } else {
  138. int32_t index = tmp_op_ptr->GetOutputIndexByName(item.second);
  139. if (index < 0) {
  140. GELOGE(GRAPH_FAILED,
  141. " Warning, user designated out_node (%s):(%s) not exist in graph, this out_node ignored!",
  142. item.first.GetName().c_str(), item.second.c_str());
  143. return GRAPH_FAILED;
  144. }
  145. output_name_ += item.first.GetName() + ":" + std::to_string(index) + ";";
  146. output_nodes.push_back(std::make_pair(node, index));
  147. }
  148. }
  149. // Del last ";"
  150. if (!output_name_.empty()) {
  151. output_name_ = output_name_.substr(0, output_name_.length() - 1);
  152. }
  153. compute_graph_->SetOutputSize(static_cast<uint32_t>(outputs.size()));
  154. compute_graph_->SetGraphOutNodesInfo(output_nodes);
  155. GELOGI("********************SetOutputs Success***********************");
  156. GE_IF_BOOL_EXEC(!output_name_.empty(), GELOGI(" NetOutputs: (%s)", output_name_.c_str()));
  157. return GRAPH_SUCCESS;
  158. }
  159. graphStatus SetTargets(const std::vector<Operator> &targets) {
  160. GE_CHK_BOOL_RET_STATUS(compute_graph_ != nullptr, GRAPH_FAILED, "set ComputeGraph faild.");
  161. GE_CHK_BOOL_EXEC_INFO(targets.size() != 0, return GRAPH_SUCCESS, "set targets size is 0.");
  162. std::vector<ge::NodePtr> target_nodes;
  163. for (auto item : targets) {
  164. ge::NodePtr node = compute_graph_->FindNode(item.GetName());
  165. if (node == nullptr) {
  166. GELOGW(" Warning, user designated target_node (%s) not exist in graph, this target_node ignored!",
  167. item.GetName().c_str());
  168. continue;
  169. }
  170. target_nodes.push_back(node);
  171. }
  172. compute_graph_->SetGraphTargetNodesInfo(target_nodes);
  173. return GRAPH_SUCCESS;
  174. }
  175. bool IsValid() const { return (compute_graph_ != nullptr); }
  176. graphStatus AddOp(const ge::Operator &op) {
  177. std::pair<std::map<string, ge::Operator>::iterator, bool> ret;
  178. ret = op_list_.emplace(std::pair<string, ge::Operator>(op.GetName(), op));
  179. GE_CHK_BOOL_RET_STATUS(ret.second != false, GRAPH_FAILED, "the op have added before, op name:%s.",
  180. op.GetName().c_str());
  181. return GRAPH_SUCCESS;
  182. }
  183. graphStatus GetAllOpName(std::vector<string> &op_name) const {
  184. for (const auto &it : op_list_) {
  185. op_name.push_back(it.second.GetName());
  186. }
  187. return GRAPH_SUCCESS;
  188. }
  189. graphStatus FindOpByName(const string &name, ge::Operator &op) const {
  190. auto it = op_list_.find(name);
  191. GE_CHK_BOOL_EXEC(it != op_list_.end(), return GRAPH_FAILED, "there is no op: %s.", name.c_str());
  192. op = it->second;
  193. return GRAPH_SUCCESS;
  194. }
  195. graphStatus FindOpByType(const string &type, std::vector<ge::Operator> &ops) const {
  196. for (auto &op : op_list_) {
  197. auto op_type = op.second.GetOpType();
  198. if (op_type == type) {
  199. ops.push_back(op.second);
  200. continue;
  201. }
  202. if (op_type == ge::FRAMEWORKOP) {
  203. op.second.GetAttr(ge::ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE, op_type);
  204. if (op_type == type) {
  205. ops.push_back(op.second);
  206. }
  207. }
  208. }
  209. return GRAPH_SUCCESS;
  210. }
  211. void SetNeedIteration(bool need_iteration) {
  212. if (compute_graph_ == nullptr) {
  213. GELOGE(GRAPH_FAILED, "Set need iteration failed, as compute graph is null.");
  214. return;
  215. }
  216. compute_graph_->SetNeedIteration(need_iteration);
  217. }
  218. const std::string &GetName() const { return name_; }
  219. private:
  220. std::string name_;
  221. std::string output_name_;
  222. std::map<string, ge::Operator> op_list_;
  223. ComputeGraphPtr compute_graph_{nullptr};
  224. };
  225. Graph::Graph(const std::string &name) {
  226. impl_ = ComGraphMakeShared<GraphImpl>(name);
  227. if (impl_ == nullptr) {
  228. GELOGW("GraphImpl make shared failed, impl_ is nullptr");
  229. }
  230. }
  231. graphStatus Graph::AddOp(const ge::Operator &op) {
  232. GE_CHK_BOOL_EXEC(impl_ != nullptr, return GRAPH_FAILED, "AddOp failed: graph can not be used, impl is nullptr.");
  233. return impl_->AddOp(op);
  234. }
  235. graphStatus Graph::GetAllOpName(std::vector<string> &op_name) const {
  236. GE_CHK_BOOL_EXEC(impl_ != nullptr, return GRAPH_FAILED,
  237. "GetAllOpName failed: graph can not be used, impl is nullptr.");
  238. return impl_->GetAllOpName(op_name);
  239. }
  240. graphStatus Graph::FindOpByName(const std::string &name, Operator &op) const {
  241. Operator op_find_op_def("NULL");
  242. op = op_find_op_def;
  243. GE_CHK_BOOL_EXEC(impl_ != nullptr, return GRAPH_FAILED,
  244. "FindOpByName failed: graph can not be used, impl is nullptr.");
  245. return impl_->FindOpByName(name, op);
  246. }
  247. graphStatus Graph::FindOpByType(const string &type, std::vector<ge::Operator> &ops) const {
  248. GE_CHECK_NOTNULL(impl_);
  249. return impl_->FindOpByType(type, ops);
  250. }
  251. Graph &Graph::SetInputs(const vector<ge::Operator> &inputs) {
  252. GE_CHK_BOOL_EXEC(impl_ != nullptr, return *this, "SetInputs failed: graph can not be used, impl is nullptr.")
  253. GE_CHK_BOOL_EXEC(inputs.size() > 0, return *this, "SetInputs failed: input operator size can not be 0.");
  254. (void)impl_->SetInputs(inputs);
  255. return *this;
  256. }
  257. Graph &Graph::SetOutputs(const vector<ge::Operator> &outputs) {
  258. if (impl_ == nullptr) {
  259. GELOGE(GRAPH_FAILED, "SetOutputs failed: graph can not be used, impl is nullptr.");
  260. return *this;
  261. }
  262. (void)impl_->SetOutputs(outputs);
  263. return *this;
  264. }
  265. Graph &Graph::SetOutputs(const std::vector<std::pair<Operator, std::vector<size_t>>> &output_indexs) {
  266. if (impl_ == nullptr) {
  267. GELOGE(GRAPH_FAILED, "SetOutputs failed: graph can not be used, impl is nullptr.");
  268. return *this;
  269. }
  270. (void)impl_->SetOutputs(output_indexs);
  271. return *this;
  272. }
  273. Graph &Graph::SetOutputs(const std::vector<pair<Operator, string>> &outputs) {
  274. GE_CHK_BOOL_EXEC(impl_ != nullptr, return *this, "SetOutputs failed: graph can not be used, impl is nullptr.")
  275. (void)impl_->SetOutputs(outputs);
  276. return *this;
  277. }
  278. Graph &Graph::SetTargets(const vector<ge::Operator> &targets) {
  279. if (impl_ == nullptr) {
  280. GELOGE(GRAPH_FAILED, "SetTargets failed: graph can not be used, impl is nullptr.");
  281. return *this;
  282. }
  283. (void)impl_->SetTargets(targets);
  284. return *this;
  285. }
  286. bool Graph::IsValid() const {
  287. if (impl_ == nullptr) {
  288. return false;
  289. }
  290. return impl_->IsValid();
  291. }
  292. void Graph::SetNeedIteration(bool need_iteration) {
  293. if (impl_ == nullptr) {
  294. GELOGE(GRAPH_FAILED, "Set need iteration failed, as impl is null.");
  295. return;
  296. }
  297. impl_->SetNeedIteration(need_iteration);
  298. }
  299. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY ComputeGraphPtr GraphUtils::GetComputeGraph(const ge::Graph &graph) {
  300. GE_CHK_BOOL_EXEC_NOLOG(graph.IsValid(), return nullptr);
  301. return graph.impl_->compute_graph_;
  302. }
  303. graphStatus Graph::SaveToFile(const string &file_name) const {
  304. Model model = Model();
  305. model.SetGraph(*this);
  306. return model.SaveToFile(file_name);
  307. }
  308. graphStatus Graph::LoadFromFile(const string &file_name) {
  309. Model model = Model();
  310. graphStatus ret = model.LoadFromFile(file_name);
  311. if (ret != GRAPH_SUCCESS) {
  312. return ret;
  313. }
  314. *this = model.GetGraph();
  315. return GRAPH_SUCCESS;
  316. }
  317. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY const std::string &Graph::GetName() const { return impl_->GetName(); }
  318. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Graph
  319. GraphUtils::CreateGraphFromComputeGraph(const ge::ComputeGraphPtr compute_graph) {
  320. GE_CHK_BOOL_EXEC_NOLOG(compute_graph != nullptr, return Graph(""));
  321. auto name = compute_graph->GetName();
  322. auto graph = Graph(name);
  323. GE_CHK_BOOL_EXEC_NOLOG(graph.impl_ != nullptr, return graph);
  324. graph.impl_->compute_graph_ = compute_graph;
  325. return graph;
  326. }
  327. GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY graphStatus GraphUtils::RecoverGraphOperators(const Graph &graph) {
  328. GE_CHECK_NOTNULL(graph.impl_);
  329. GE_CHECK_NOTNULL(graph.impl_->compute_graph_);
  330. graph.impl_->op_list_.clear();
  331. for (const auto &node : graph.impl_->compute_graph_->GetDirectNode()) {
  332. graph.impl_->op_list_[node->GetName()] = OpDescUtils::CreateOperatorFromNode(node);
  333. }
  334. return SUCCESS;
  335. }
  336. } // namespace ge

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示