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.

same_transdata_breadth_fusion_pass.cc 45 kB

5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 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
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. /**
  2. * Copyright 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 "graph/passes/same_transdata_breadth_fusion_pass.h"
  17. #include <memory>
  18. #include <sstream>
  19. #include <string>
  20. #include <utility>
  21. #include <vector>
  22. #include "common/ge_inner_error_codes.h"
  23. #include "common/types.h"
  24. #include "graph/debug/ge_attr_define.h"
  25. #include "graph/utils/graph_utils.h"
  26. #include "graph/utils/op_desc_utils.h"
  27. #include "init/gelib.h"
  28. namespace {
  29. const int kNoTransOp = 1;
  30. } // namespace
  31. namespace ge {
  32. void SameTransdataBreadthFusionPass::GetSubGraphNodesInfo() {
  33. vector<vector<NodePtr>> before_transdata_nodes(sub_graph_anchors_.size());
  34. vector<pair<int, InDataAnchorPtr>> all_transdata_nodes;
  35. for (size_t i = 0; i < sub_graph_anchors_.size(); ++i) {
  36. auto nodes_anchor = sub_graph_anchors_[i];
  37. auto iter = nodes_anchor.begin();
  38. auto first_out_anchor = iter->first;
  39. GE_CHECK_NOTNULL_JUST_RETURN(first_out_anchor);
  40. before_transdata_nodes[i].push_back(first_out_anchor->GetOwnerNode());
  41. GELOGD("index:%zu, node:%s, type:%s", i, first_out_anchor->GetOwnerNode()->GetName().c_str(),
  42. first_out_anchor->GetOwnerNode()->GetType().c_str());
  43. while (iter != nodes_anchor.end()) {
  44. auto in_anchor = iter->second;
  45. GE_CHECK_NOTNULL_JUST_RETURN(in_anchor);
  46. auto in_node = in_anchor->GetOwnerNode();
  47. GELOGD("index:%zu, node:%s, type:%s", i, first_out_anchor->GetOwnerNode()->GetName().c_str(),
  48. first_out_anchor->GetOwnerNode()->GetType().c_str());
  49. if (in_node->GetType() == TRANSDATA) {
  50. all_transdata_nodes.emplace_back(i, in_anchor);
  51. } else {
  52. before_transdata_nodes[i].push_back(in_node);
  53. }
  54. ++iter;
  55. }
  56. GELOGD("index:%zu, before trandata node size:%zu", i, before_transdata_nodes[i].size());
  57. }
  58. before_transdata_nodes_.swap(before_transdata_nodes);
  59. all_transdata_nodes_.swap(all_transdata_nodes);
  60. }
  61. OpDescPtr SameTransdataBreadthFusionPass::GetCastOp(const GeTensorDesc &in_desc, const GeTensorDesc &out_desc) {
  62. static std::atomic_long atomic_fusion_cast_op_count(1);
  63. auto fusion_cast_op_count = atomic_fusion_cast_op_count.fetch_add(1);
  64. std::stringstream cast_op_name;
  65. cast_op_name << "fusion_cast_" << fusion_cast_op_count;
  66. auto node_op = ge::OperatorFactory::CreateOperator(cast_op_name.str().c_str(), CAST);
  67. auto cast_op = ge::OpDescUtils::GetOpDescFromOperator(node_op);
  68. node_op.BreakConnect();
  69. if (cast_op == nullptr) {
  70. REPORT_INNER_ERROR("E19999", "Create Operator:%s(%s) failed", cast_op_name.str().c_str(), CAST);
  71. GELOGE(INTERNAL_ERROR, "new fusion cast op failed!");
  72. return nullptr;
  73. }
  74. const int default_output_index = 0;
  75. const int default_input_index = 0;
  76. if (cast_op->GetInputsSize() == 0) {
  77. if (cast_op->AddInputDesc(in_desc) != GRAPH_SUCCESS) {
  78. GELOGW("AddInputDesc fail.");
  79. }
  80. } else {
  81. if (cast_op->UpdateInputDesc(default_input_index, in_desc) != GRAPH_SUCCESS) {
  82. GELOGW("UpdateInputDesc fail");
  83. }
  84. }
  85. if (cast_op->GetOutputsSize() == 0) {
  86. if (cast_op->AddOutputDesc(out_desc) != GRAPH_SUCCESS) {
  87. GELOGW("AddOutputDesc fail.");
  88. }
  89. } else {
  90. if (cast_op->UpdateOutputDesc(default_output_index, out_desc) != GRAPH_SUCCESS) {
  91. GELOGW("UpdateOutputDesc fail");
  92. }
  93. }
  94. if (!AttrUtils::SetInt(cast_op, CAST_ATTR_DST_TYPE, static_cast<int64_t>(out_desc.GetDataType()))) {
  95. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", CAST_ATTR_DST_TYPE.c_str(),
  96. cast_op->GetName().c_str(), cast_op->GetType().c_str());
  97. GELOGE(INTERNAL_ERROR, "set dst_type attr failed");
  98. return nullptr;
  99. }
  100. return cast_op;
  101. }
  102. void SameTransdataBreadthFusionPass::InsertSameTransdataNodeIndex(int anchors_index,
  103. vector<int> &same_transdata_nodes) {
  104. auto same_iter = same_transdata_nodes.begin();
  105. while (same_iter != same_transdata_nodes.end()) {
  106. if (before_transdata_nodes_[anchors_index].size() <= before_transdata_nodes_[*same_iter].size()) {
  107. same_transdata_nodes.insert(same_iter, anchors_index);
  108. return;
  109. }
  110. ++same_iter;
  111. }
  112. same_transdata_nodes.push_back(anchors_index);
  113. }
  114. std::set<std::string> SameTransdataBreadthFusionPass::GetInControlIdentityNodes(const NodePtr &node,
  115. int subgraph_index) {
  116. std::set<std::string> in_node_names;
  117. for (const auto &in_node : node->GetInControlNodes()) {
  118. if (in_node->GetType() == IDENTITY) {
  119. in_node_names.insert(in_node->GetName());
  120. }
  121. }
  122. for (const auto &subgraph_node : before_transdata_nodes_[subgraph_index]) {
  123. for (const auto &in_node : subgraph_node->GetInControlNodes()) {
  124. if (in_node->GetType() == IDENTITY) {
  125. in_node_names.insert(in_node->GetName());
  126. }
  127. }
  128. }
  129. GELOGD("control in nodes for %s(%d): %zu", node->GetName().c_str(), subgraph_index, in_node_names.size());
  130. return in_node_names;
  131. }
  132. void SameTransdataBreadthFusionPass::GetSameTransdataNode(vector<int> &same_transdata_nodes) {
  133. auto iter = all_transdata_nodes_.begin();
  134. same_transdata_nodes.push_back(iter->first);
  135. auto node_for_compare_in_anchor = iter->second;
  136. GE_CHECK_NOTNULL_JUST_RETURN(node_for_compare_in_anchor);
  137. auto node_for_compare = node_for_compare_in_anchor->GetOwnerNode();
  138. // Get op-desc, input/output desc, in-control-edges-from-identity, as the compare-key
  139. auto op_desc_for_compare = node_for_compare->GetOpDesc();
  140. GE_CHECK_NOTNULL_JUST_RETURN(op_desc_for_compare);
  141. string op_compare_stream_label;
  142. (void)AttrUtils::GetStr(op_desc_for_compare, ATTR_NAME_STREAM_LABEL, op_compare_stream_label);
  143. auto op_compare_in_ctrl_nodes = GetInControlIdentityNodes(node_for_compare, iter->first);
  144. auto input_desc_for_compare = op_desc_for_compare->GetInputDescPtr(node_for_compare_in_anchor->GetIdx());
  145. GE_CHECK_NOTNULL_JUST_RETURN(input_desc_for_compare);
  146. auto output_desc_for_compare = op_desc_for_compare->GetOutputDescPtr(0);
  147. GE_CHECK_NOTNULL_JUST_RETURN(output_desc_for_compare);
  148. iter = all_transdata_nodes_.erase(iter);
  149. while (iter != all_transdata_nodes_.end()) {
  150. auto in_anchor = iter->second;
  151. if (in_anchor == nullptr) {
  152. continue;
  153. }
  154. auto node_tmp = in_anchor->GetOwnerNode();
  155. if (node_tmp == node_for_compare) {
  156. ++iter;
  157. continue;
  158. }
  159. GE_CHECK_NOTNULL_JUST_RETURN(node_tmp);
  160. auto op_desc_tmp = node_tmp->GetOpDesc();
  161. GE_CHECK_NOTNULL_JUST_RETURN(op_desc_tmp);
  162. auto input_desc_tmp = op_desc_tmp->GetInputDescPtr(in_anchor->GetIdx());
  163. auto output_desc_tmp = op_desc_tmp->GetOutputDescPtr(0);
  164. string op_tmp_stream_label;
  165. (void)AttrUtils::GetStr(op_desc_tmp, ATTR_NAME_STREAM_LABEL, op_tmp_stream_label);
  166. auto op_tmp_in_ctrl_nodes = GetInControlIdentityNodes(node_tmp, iter->first);
  167. GE_CHECK_NOTNULL_JUST_RETURN(input_desc_tmp);
  168. GE_CHECK_NOTNULL_JUST_RETURN(output_desc_tmp);
  169. if ((op_compare_stream_label == op_tmp_stream_label) &&
  170. (input_desc_tmp->GetFormat() == input_desc_for_compare->GetFormat()) &&
  171. (output_desc_tmp->GetFormat() == output_desc_for_compare->GetFormat()) &&
  172. (op_compare_in_ctrl_nodes == op_tmp_in_ctrl_nodes)) {
  173. GELOGD("same transdata node:%s, src node:%s", node_tmp->GetName().c_str(), node_for_compare->GetName().c_str());
  174. InsertSameTransdataNodeIndex(iter->first, same_transdata_nodes);
  175. iter = all_transdata_nodes_.erase(iter);
  176. } else {
  177. ++iter;
  178. }
  179. }
  180. }
  181. graphStatus SameTransdataBreadthFusionPass::ReLinkDataOutput2PreNode(const NodePtr &transdata_node,
  182. const OutDataAnchorPtr &pre_out_anchor,
  183. const NodePtr &relink_node) {
  184. GE_CHECK_NOTNULL(pre_out_anchor);
  185. GE_CHECK_NOTNULL(transdata_node);
  186. auto transdata_peer_out_control_anchor = pre_out_anchor->GetOwnerNode()->GetOutControlAnchor();
  187. for (auto &out_anchor : transdata_node->GetAllOutDataAnchors()) {
  188. // relink data edge
  189. for (auto &transdata_peer_in_anchor : out_anchor->GetPeerInDataAnchors()) {
  190. if (transdata_peer_in_anchor->GetOwnerNode() == relink_node) {
  191. continue;
  192. }
  193. GELOGI("remove edge.src:%s, dst:%s", out_anchor->GetOwnerNode()->GetName().c_str(),
  194. transdata_peer_in_anchor->GetOwnerNode()->GetName().c_str());
  195. if (GraphUtils::RemoveEdge(out_anchor, transdata_peer_in_anchor) != GRAPH_SUCCESS) {
  196. REPORT_CALL_ERROR("E19999", "Remove edge between op:%s(%s)(index:%d) and op:%s(%s)(index:%d) failed",
  197. out_anchor->GetOwnerNode()->GetName().c_str(),
  198. out_anchor->GetOwnerNode()->GetType().c_str(), out_anchor->GetIdx(),
  199. transdata_peer_in_anchor->GetOwnerNode()->GetName().c_str(),
  200. transdata_peer_in_anchor->GetOwnerNode()->GetType().c_str(),
  201. transdata_peer_in_anchor->GetIdx());
  202. GELOGE(GRAPH_FAILED, "remove edge failed!src node:%s, dst node:%s", transdata_node->GetName().c_str(),
  203. transdata_peer_in_anchor->GetOwnerNode()->GetName().c_str());
  204. return GRAPH_FAILED;
  205. }
  206. GELOGI("add edge.src:%s, dst:%s", pre_out_anchor->GetOwnerNode()->GetName().c_str(),
  207. transdata_peer_in_anchor->GetOwnerNode()->GetName().c_str());
  208. if (GraphUtils::AddEdge(pre_out_anchor, transdata_peer_in_anchor) != GRAPH_SUCCESS) {
  209. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:%d) and op:%s(%s)(index:%d) failed",
  210. pre_out_anchor->GetOwnerNode()->GetName().c_str(),
  211. pre_out_anchor->GetOwnerNode()->GetType().c_str(), pre_out_anchor->GetIdx(),
  212. transdata_peer_in_anchor->GetOwnerNode()->GetName().c_str(),
  213. transdata_peer_in_anchor->GetOwnerNode()->GetType().c_str(),
  214. transdata_peer_in_anchor->GetIdx());
  215. GELOGE(GRAPH_FAILED, "add edge failed!src node:%s, dst node:%s",
  216. pre_out_anchor->GetOwnerNode()->GetName().c_str(),
  217. transdata_peer_in_anchor->GetOwnerNode()->GetName().c_str());
  218. return GRAPH_FAILED;
  219. }
  220. }
  221. }
  222. return GRAPH_SUCCESS;
  223. }
  224. graphStatus SameTransdataBreadthFusionPass::ReLinkOutDataPeerInControlNodes2PreNode(
  225. const NodePtr &transdata_node, const OutDataAnchorPtr &pre_out_anchor) {
  226. GE_CHECK_NOTNULL(pre_out_anchor);
  227. GE_CHECK_NOTNULL(transdata_node);
  228. auto transdata_peer_out_control_anchor = pre_out_anchor->GetOwnerNode()->GetOutControlAnchor();
  229. for (auto &out_anchor : transdata_node->GetAllOutDataAnchors()) {
  230. for (auto &transdata_peer_in_control_anchor : out_anchor->GetPeerInControlAnchors()) {
  231. GELOGD("remove edge.src:%s, dst:%s", out_anchor->GetOwnerNode()->GetName().c_str(),
  232. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str());
  233. if (GraphUtils::RemoveEdge(out_anchor, transdata_peer_in_control_anchor) != GRAPH_SUCCESS) {
  234. REPORT_CALL_ERROR("E19999", "Remove control edge between op:%s(%s) and op:%s(%s) failed",
  235. out_anchor->GetOwnerNode()->GetName().c_str(),
  236. out_anchor->GetOwnerNode()->GetType().c_str(),
  237. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str(),
  238. transdata_peer_in_control_anchor->GetOwnerNode()->GetType().c_str());
  239. GELOGE(GRAPH_FAILED, "remove edge failed!src node:%s, dst node:%s", transdata_node->GetName().c_str(),
  240. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str());
  241. return GRAPH_FAILED;
  242. }
  243. if (transdata_peer_out_control_anchor == nullptr) {
  244. GELOGD("add edge.src:%s, dst:%s", pre_out_anchor->GetOwnerNode()->GetName().c_str(),
  245. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str());
  246. if (GraphUtils::AddEdge(pre_out_anchor, transdata_peer_in_control_anchor) != GRAPH_SUCCESS) {
  247. REPORT_CALL_ERROR("E19999", "Add control edge between op:%s(%s) and op:%s(%s) failed",
  248. pre_out_anchor->GetOwnerNode()->GetName().c_str(),
  249. pre_out_anchor->GetOwnerNode()->GetType().c_str(),
  250. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str(),
  251. transdata_peer_in_control_anchor->GetOwnerNode()->GetType().c_str());
  252. GELOGE(GRAPH_FAILED, "add edge failed!src node:%s, dst node:%s",
  253. pre_out_anchor->GetOwnerNode()->GetName().c_str(),
  254. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str());
  255. return GRAPH_FAILED;
  256. }
  257. } else {
  258. GELOGD("add edge.src node:%s, dst node:%s", pre_out_anchor->GetOwnerNode()->GetName().c_str(),
  259. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str());
  260. if (GraphUtils::AddEdge(transdata_peer_out_control_anchor, transdata_peer_in_control_anchor) != GRAPH_SUCCESS) {
  261. REPORT_CALL_ERROR("E19999", "Add control edge between op:%s(%s) and op:%s(%s) failed",
  262. transdata_peer_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  263. transdata_peer_out_control_anchor->GetOwnerNode()->GetType().c_str(),
  264. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str(),
  265. transdata_peer_in_control_anchor->GetOwnerNode()->GetType().c_str());
  266. GELOGE(GRAPH_FAILED, "add edge failed!src node:%s, dst node:%s",
  267. pre_out_anchor->GetOwnerNode()->GetName().c_str(),
  268. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str());
  269. return GRAPH_FAILED;
  270. }
  271. }
  272. }
  273. }
  274. return GRAPH_SUCCESS;
  275. }
  276. graphStatus SameTransdataBreadthFusionPass::ReLinkTransdataOutput2PreNode(const NodePtr &transdata_node,
  277. const OutDataAnchorPtr &pre_out_anchor,
  278. const NodePtr &relink_node) {
  279. GE_CHECK_NOTNULL(pre_out_anchor);
  280. if (ReLinkDataOutput2PreNode(transdata_node, pre_out_anchor, relink_node) != GRAPH_SUCCESS) {
  281. return GRAPH_FAILED;
  282. }
  283. if (ReLinkOutDataPeerInControlNodes2PreNode(transdata_node, pre_out_anchor) != GRAPH_SUCCESS) {
  284. return GRAPH_FAILED;
  285. }
  286. auto transdata_peer_out_control_anchor = pre_out_anchor->GetOwnerNode()->GetOutControlAnchor();
  287. return ReLinkTransdataControlOutput2PreNode(transdata_node, pre_out_anchor, transdata_peer_out_control_anchor);
  288. }
  289. graphStatus SameTransdataBreadthFusionPass::ReLinkOutControlPeerInControlAnchors(
  290. const NodePtr &transdata_node_keep, const OutDataAnchorPtr &pre_out_anchor,
  291. const OutControlAnchorPtr &transdata_peer_out_control_anchor) {
  292. GE_CHECK_NOTNULL(transdata_node_keep);
  293. GE_CHECK_NOTNULL(pre_out_anchor);
  294. auto out_control_anchor = transdata_node_keep->GetOutControlAnchor();
  295. if (out_control_anchor == nullptr) {
  296. return GRAPH_SUCCESS;
  297. }
  298. for (auto &transdata_peer_in_control_anchor : out_control_anchor->GetPeerInControlAnchors()) {
  299. GELOGD("remove edge.src:%s, dst:%s", transdata_node_keep->GetName().c_str(),
  300. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str());
  301. if (GraphUtils::RemoveEdge(out_control_anchor, transdata_peer_in_control_anchor) != GRAPH_SUCCESS) {
  302. REPORT_CALL_ERROR("E19999", "Remove control edge between op:%s(%s) and op:%s(%s) failed",
  303. out_control_anchor->GetOwnerNode()->GetName().c_str(),
  304. out_control_anchor->GetOwnerNode()->GetType().c_str(),
  305. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str(),
  306. transdata_peer_in_control_anchor->GetOwnerNode()->GetType().c_str());
  307. GELOGE(GRAPH_FAILED, "remove transdata control edge failed!");
  308. return GRAPH_FAILED;
  309. }
  310. if (transdata_peer_out_control_anchor == nullptr) {
  311. GELOGD("add edge.src:%s, dst:%s", pre_out_anchor->GetOwnerNode()->GetName().c_str(),
  312. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str());
  313. if (GraphUtils::AddEdge(pre_out_anchor, transdata_peer_in_control_anchor) != GRAPH_SUCCESS) {
  314. REPORT_CALL_ERROR("E19999", "Add control edge between op:%s(%s) and op:%s(%s) failed",
  315. pre_out_anchor->GetOwnerNode()->GetName().c_str(),
  316. pre_out_anchor->GetOwnerNode()->GetType().c_str(),
  317. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str(),
  318. transdata_peer_in_control_anchor->GetOwnerNode()->GetType().c_str());
  319. GELOGE(GRAPH_FAILED, "add control edge failed!");
  320. return GRAPH_FAILED;
  321. }
  322. } else {
  323. GELOGD("add edge.src:%s, dst:%s", transdata_peer_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  324. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str());
  325. if (GraphUtils::AddEdge(transdata_peer_out_control_anchor, transdata_peer_in_control_anchor) != GRAPH_SUCCESS) {
  326. REPORT_CALL_ERROR("E19999", "Add control edge between op:%s(%s) and op:%s(%s) failed",
  327. transdata_peer_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  328. transdata_peer_out_control_anchor->GetOwnerNode()->GetType().c_str(),
  329. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str(),
  330. transdata_peer_in_control_anchor->GetOwnerNode()->GetType().c_str());
  331. GELOGE(GRAPH_FAILED, "add control edge failed!");
  332. return GRAPH_FAILED;
  333. }
  334. }
  335. }
  336. return GRAPH_SUCCESS;
  337. }
  338. graphStatus SameTransdataBreadthFusionPass::ReLinkOutControlPeerInDataAnchors(
  339. const NodePtr &transdata_node_keep, const OutDataAnchorPtr &pre_out_anchor,
  340. const OutControlAnchorPtr &transdata_peer_out_control_anchor) {
  341. GE_CHECK_NOTNULL(transdata_node_keep);
  342. GE_CHECK_NOTNULL(pre_out_anchor);
  343. auto out_control_anchor = transdata_node_keep->GetOutControlAnchor();
  344. if (out_control_anchor == nullptr) {
  345. return GRAPH_SUCCESS;
  346. }
  347. for (auto &transdata_peer_in_data_anchor : out_control_anchor->GetPeerInDataAnchors()) {
  348. if (transdata_peer_in_data_anchor == nullptr || transdata_peer_in_data_anchor->GetOwnerNode() == nullptr) {
  349. continue;
  350. }
  351. GELOGD("remove edge.src:%s, dst:%s", transdata_node_keep->GetName().c_str(),
  352. transdata_peer_in_data_anchor->GetOwnerNode()->GetName().c_str());
  353. if (GraphUtils::RemoveEdge(out_control_anchor, transdata_peer_in_data_anchor) != GRAPH_SUCCESS) {
  354. REPORT_CALL_ERROR("E19999", "Remove control edge between op:%s(%s) and op:%s(%s) failed",
  355. out_control_anchor->GetOwnerNode()->GetName().c_str(),
  356. out_control_anchor->GetOwnerNode()->GetType().c_str(),
  357. transdata_peer_in_data_anchor->GetOwnerNode()->GetName().c_str(),
  358. transdata_peer_in_data_anchor->GetOwnerNode()->GetType().c_str());
  359. GELOGE(GRAPH_FAILED, "remove transdata control edge failed!");
  360. return GRAPH_FAILED;
  361. }
  362. if (transdata_peer_out_control_anchor == nullptr) {
  363. GELOGD("add edge.src:%s, dst:%s", pre_out_anchor->GetOwnerNode()->GetName().c_str(),
  364. transdata_peer_in_data_anchor->GetOwnerNode()->GetName().c_str());
  365. if (GraphUtils::AddEdge(pre_out_anchor, transdata_peer_in_data_anchor) != GRAPH_SUCCESS) {
  366. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:%d) and op:%s(%s)(index:%d) failed",
  367. pre_out_anchor->GetOwnerNode()->GetName().c_str(),
  368. pre_out_anchor->GetOwnerNode()->GetType().c_str(), pre_out_anchor->GetIdx(),
  369. transdata_peer_in_data_anchor->GetOwnerNode()->GetName().c_str(),
  370. transdata_peer_in_data_anchor->GetOwnerNode()->GetType().c_str(),
  371. transdata_peer_in_data_anchor->GetIdx());
  372. GELOGE(GRAPH_FAILED, "add control edge failed!");
  373. return GRAPH_FAILED;
  374. }
  375. } else {
  376. GELOGD("add edge.src:%s, dst:%s", transdata_peer_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  377. transdata_peer_in_data_anchor->GetOwnerNode()->GetName().c_str());
  378. if (GraphUtils::AddEdge(transdata_peer_out_control_anchor, transdata_peer_in_data_anchor) != GRAPH_SUCCESS) {
  379. REPORT_CALL_ERROR("E19999", "Add control edge between op:%s(%s) and op:%s(%s) failed",
  380. transdata_peer_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  381. transdata_peer_out_control_anchor->GetOwnerNode()->GetType().c_str(),
  382. transdata_peer_in_data_anchor->GetOwnerNode()->GetName().c_str(),
  383. transdata_peer_in_data_anchor->GetOwnerNode()->GetType().c_str());
  384. GELOGE(GRAPH_FAILED, "add control edge failed!");
  385. return GRAPH_FAILED;
  386. }
  387. }
  388. }
  389. return GRAPH_SUCCESS;
  390. }
  391. graphStatus SameTransdataBreadthFusionPass::ReLinkTransdataControlOutput2PreNode(
  392. const NodePtr &transdata_node_keep, const OutDataAnchorPtr &pre_out_anchor,
  393. const OutControlAnchorPtr &transdata_peer_out_control_anchor) {
  394. if (ReLinkOutControlPeerInControlAnchors(transdata_node_keep, pre_out_anchor, transdata_peer_out_control_anchor) !=
  395. GRAPH_SUCCESS) {
  396. return GRAPH_FAILED;
  397. }
  398. return ReLinkOutControlPeerInDataAnchors(transdata_node_keep, pre_out_anchor, transdata_peer_out_control_anchor);
  399. }
  400. graphStatus SameTransdataBreadthFusionPass::Run(ComputeGraphPtr graph) {
  401. GELOGI("[SameTransdataBreadthFusionPass]: optimize begin.");
  402. if (graph == nullptr) {
  403. return GRAPH_SUCCESS;
  404. }
  405. for (auto &node : graph->GetDirectNode()) {
  406. if (IsTransOp(node) || node->GetOutDataNodesSize() <= 1) {
  407. continue;
  408. }
  409. GELOGD("Current normal node name: %s, type: %s.", node->GetName().c_str(), node->GetType().c_str());
  410. for (auto &out_anchor : node->GetAllOutDataAnchors()) {
  411. vector<std::vector<pair<OutDataAnchorPtr, InDataAnchorPtr>>> sub_graph_anchors;
  412. std::vector<pair<OutDataAnchorPtr, InDataAnchorPtr>> nodes_list;
  413. if (GetSubGraphsBetweenNormalAndTransdataNode(out_anchor, sub_graph_anchors, nodes_list) != GRAPH_SUCCESS) {
  414. GELOGW("get transop failed!");
  415. continue;
  416. }
  417. if (sub_graph_anchors.size() <= 1) {
  418. continue;
  419. }
  420. sub_graph_anchors_.swap(sub_graph_anchors);
  421. // check reshape node
  422. GetSubGraphNodesInfo();
  423. GELOGD("all trandata node size:%zu", all_transdata_nodes_.size());
  424. if (ExtractTransNode(graph) != GRAPH_SUCCESS) {
  425. return GRAPH_FAILED;
  426. }
  427. }
  428. }
  429. GELOGI("[SameTransdataBreadthFusionPass]: Optimize success.");
  430. return GRAPH_SUCCESS;
  431. }
  432. graphStatus SameTransdataBreadthFusionPass::ExtractTransNode(const ComputeGraphPtr &graph) {
  433. while (all_transdata_nodes_.size() > 1) {
  434. vector<int> same_transdata_nodes;
  435. GetSameTransdataNode(same_transdata_nodes);
  436. GELOGD("same transdata node size:%zu", same_transdata_nodes.size());
  437. // reuse transdata ,new cast
  438. if (same_transdata_nodes.size() <= 1) {
  439. continue;
  440. }
  441. int anchors_index = same_transdata_nodes[0];
  442. auto transdata_in_anchor = sub_graph_anchors_[anchors_index].back().second;
  443. GE_CHECK_NOTNULL(transdata_in_anchor);
  444. auto transdata_node_keep = transdata_in_anchor->GetOwnerNode();
  445. auto transdata_out_anchor = transdata_node_keep->GetOutDataAnchor(0);
  446. GELOGD("anchor index %d, before transdata node size:%zu", anchors_index,
  447. before_transdata_nodes_[anchors_index].size());
  448. if (before_transdata_nodes_[anchors_index].size() > 1) {
  449. if (RelinkRemainTransdata(graph, same_transdata_nodes) != GRAPH_SUCCESS) {
  450. return GRAPH_FAILED;
  451. }
  452. }
  453. if (LinkNewCastNode2RemainTransdata(graph, same_transdata_nodes, transdata_out_anchor, transdata_node_keep) !=
  454. GRAPH_SUCCESS) {
  455. return GRAPH_FAILED;
  456. }
  457. }
  458. return GRAPH_SUCCESS;
  459. }
  460. graphStatus SameTransdataBreadthFusionPass::RelinkRemainTransdata(const ComputeGraphPtr &graph,
  461. const vector<int> &same_transdata_nodes) {
  462. int anchors_index = same_transdata_nodes[0];
  463. auto head_node_anchor = sub_graph_anchors_[anchors_index][0].first;
  464. GE_CHECK_NOTNULL(head_node_anchor);
  465. auto head_node = head_node_anchor->GetOwnerNode();
  466. GE_CHECK_NOTNULL(head_node->GetOpDesc());
  467. auto head_output_desc = head_node->GetOpDesc()->GetOutputDescPtr(head_node_anchor->GetIdx());
  468. auto transdata_in_anchor = sub_graph_anchors_[anchors_index].back().second;
  469. GE_CHECK_NOTNULL(transdata_in_anchor);
  470. auto transdata_node_keep = transdata_in_anchor->GetOwnerNode();
  471. GE_CHECK_NOTNULL(transdata_node_keep->GetOpDesc());
  472. auto transdata_out_anchor = transdata_node_keep->GetOutDataAnchor(0);
  473. GELOGD("head node:%s, transdata node keep:%s", head_node->GetName().c_str(), transdata_node_keep->GetName().c_str());
  474. bool reuse_nodes = AllNodeBeforeTransdataHasOneDataOut(anchors_index);
  475. UpdateTransdataDesc(transdata_in_anchor, transdata_node_keep->GetOpDesc(), head_output_desc);
  476. auto transdata_peer_out_anchor = sub_graph_anchors_[anchors_index].back().first;
  477. GE_CHECK_NOTNULL(transdata_peer_out_anchor);
  478. auto transdata_peer_out_node = transdata_peer_out_anchor->GetOwnerNode();
  479. GELOGI("remove edge.src:%s, dst:%s", transdata_peer_out_node->GetName().c_str(),
  480. transdata_node_keep->GetName().c_str());
  481. if (GraphUtils::RemoveEdge(transdata_peer_out_anchor, transdata_in_anchor) != GRAPH_SUCCESS) {
  482. GELOGW("remove edge failed!out node %s, in node %s", transdata_peer_out_node->GetName().c_str(),
  483. transdata_node_keep->GetName().c_str());
  484. }
  485. GELOGI("add edge.out node %s, in node %s", head_node->GetName().c_str(), transdata_node_keep->GetName().c_str());
  486. if (GraphUtils::AddEdge(head_node_anchor, transdata_in_anchor) != GRAPH_SUCCESS) {
  487. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:%d) and op:%s(%s)(index:%d) failed",
  488. head_node_anchor->GetOwnerNode()->GetName().c_str(),
  489. head_node_anchor->GetOwnerNode()->GetType().c_str(), head_node_anchor->GetIdx(),
  490. transdata_in_anchor->GetOwnerNode()->GetName().c_str(),
  491. transdata_in_anchor->GetOwnerNode()->GetType().c_str(),
  492. transdata_in_anchor->GetIdx());
  493. GELOGE(GRAPH_FAILED, "add edge failed!out node %s, in node %s", head_node->GetName().c_str(),
  494. transdata_node_keep->GetName().c_str());
  495. return GRAPH_FAILED;
  496. }
  497. NodePtr relink_node;
  498. // relink to transdata output nodes
  499. if (reuse_nodes) {
  500. if (ReuseNodesBeforeTransdata(anchors_index, transdata_out_anchor, relink_node) != GRAPH_SUCCESS) {
  501. return GRAPH_FAILED;
  502. }
  503. if (ReLinkTransdataOutput2PreNode(transdata_node_keep, transdata_peer_out_anchor, relink_node) != GRAPH_SUCCESS) {
  504. return GRAPH_FAILED;
  505. }
  506. } else {
  507. OutDataAnchorPtr pre_out_anchor = transdata_out_anchor;
  508. if (AddCastNode(graph, same_transdata_nodes[0], pre_out_anchor, relink_node) != GRAPH_SUCCESS) {
  509. return GRAPH_FAILED;
  510. }
  511. if (ReLinkTransdataOutput2PreNode(transdata_node_keep, pre_out_anchor, relink_node) != GRAPH_SUCCESS) {
  512. return GRAPH_FAILED;
  513. }
  514. }
  515. return GRAPH_SUCCESS;
  516. }
  517. void SameTransdataBreadthFusionPass::UpdateTransdataDesc(const InDataAnchorPtr &transdata_in_anchor,
  518. const OpDescPtr &transdata_op_desc,
  519. const ConstGeTensorDescPtr &head_output_desc) {
  520. if (transdata_op_desc == nullptr || transdata_in_anchor == nullptr || head_output_desc == nullptr) {
  521. return;
  522. }
  523. auto mutable_input_desc = transdata_op_desc->MutableInputDesc(transdata_in_anchor->GetIdx());
  524. GE_CHECK_NOTNULL_JUST_RETURN(mutable_input_desc);
  525. mutable_input_desc->SetDataType(head_output_desc->GetDataType());
  526. mutable_input_desc->SetOriginDataType(head_output_desc->GetOriginDataType());
  527. auto mutable_output_desc = transdata_op_desc->MutableOutputDesc(0);
  528. GE_CHECK_NOTNULL_JUST_RETURN(mutable_output_desc);
  529. mutable_output_desc->SetDataType(head_output_desc->GetDataType());
  530. mutable_output_desc->SetOriginDataType(head_output_desc->GetOriginDataType());
  531. // maybe need to check support
  532. }
  533. bool SameTransdataBreadthFusionPass::AllNodeBeforeTransdataHasOneDataOut(int anchors_index) {
  534. for (size_t i = 1; i < before_transdata_nodes_[anchors_index].size(); ++i) {
  535. auto node = before_transdata_nodes_[anchors_index][i];
  536. if (node == nullptr) {
  537. return false;
  538. }
  539. if (node->GetOutDataNodes().size() > 1 || node->GetInDataNodes().size() > 1) {
  540. return false;
  541. }
  542. }
  543. return true;
  544. }
  545. graphStatus SameTransdataBreadthFusionPass::ReuseNodesBeforeTransdata(int anchors_index,
  546. const OutDataAnchorPtr &transdata_out_anchor,
  547. NodePtr &relink_node) {
  548. auto head_node_anchor = sub_graph_anchors_[anchors_index][0].first;
  549. auto head_node_peer_anchor = sub_graph_anchors_[anchors_index][0].second;
  550. GE_CHECK_NOTNULL(head_node_anchor);
  551. GE_CHECK_NOTNULL(head_node_peer_anchor);
  552. GE_CHECK_NOTNULL(transdata_out_anchor);
  553. GELOGI("remove edge.src:%s, dst:%s", head_node_anchor->GetOwnerNode()->GetName().c_str(),
  554. head_node_peer_anchor->GetOwnerNode()->GetName().c_str());
  555. if (head_node_anchor->IsLinkedWith(head_node_peer_anchor)) {
  556. if (GraphUtils::RemoveEdge(head_node_anchor, head_node_peer_anchor) != GRAPH_SUCCESS) {
  557. GELOGW("remove edge failed!src:%s, dst:%s", head_node_anchor->GetOwnerNode()->GetName().c_str(),
  558. head_node_peer_anchor->GetOwnerNode()->GetName().c_str());
  559. }
  560. } else {
  561. GELOGW("edge not link now. src:%s, dst:%s", head_node_anchor->GetOwnerNode()->GetName().c_str(),
  562. head_node_peer_anchor->GetOwnerNode()->GetName().c_str());
  563. }
  564. NodePtr transdata_node_keep = transdata_out_anchor->GetOwnerNode();
  565. if (before_transdata_nodes_[anchors_index].size() == kNoTransOp) {
  566. return GRAPH_SUCCESS;
  567. }
  568. GELOGI("add edge.src:%s, dst:%s", transdata_node_keep->GetName().c_str(),
  569. head_node_peer_anchor->GetOwnerNode()->GetName().c_str());
  570. if (GraphUtils::AddEdge(transdata_out_anchor, head_node_peer_anchor) != GRAPH_SUCCESS) {
  571. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:%d) and op:%s(%s)(index:%d) failed",
  572. transdata_out_anchor->GetOwnerNode()->GetName().c_str(),
  573. transdata_out_anchor->GetOwnerNode()->GetType().c_str(), transdata_out_anchor->GetIdx(),
  574. head_node_peer_anchor->GetOwnerNode()->GetName().c_str(),
  575. head_node_peer_anchor->GetOwnerNode()->GetType().c_str(),
  576. head_node_peer_anchor->GetIdx());
  577. GELOGE(GRAPH_FAILED, "add edge.src:%s, dst:%s", transdata_node_keep->GetName().c_str(),
  578. head_node_peer_anchor->GetOwnerNode()->GetName().c_str());
  579. return GRAPH_FAILED;
  580. }
  581. relink_node = head_node_peer_anchor->GetOwnerNode();
  582. GE_CHECK_NOTNULL(transdata_node_keep->GetOpDesc());
  583. auto transdata_output_desc = transdata_node_keep->GetOpDesc()->GetOutputDescPtr(0);
  584. GE_CHECK_NOTNULL(transdata_output_desc);
  585. for (size_t i = 0; i < sub_graph_anchors_[anchors_index].size() - 1; ++i) {
  586. auto in_data_anchor = sub_graph_anchors_[anchors_index][i].second;
  587. GE_CHECK_NOTNULL(in_data_anchor);
  588. auto in_owner_node = in_data_anchor->GetOwnerNode();
  589. auto in_op_desc = in_owner_node->GetOpDesc();
  590. GE_CHECK_NOTNULL(in_op_desc);
  591. auto input_desc = in_op_desc->GetInputDesc(in_data_anchor->GetIdx());
  592. CopyTensorDesc(transdata_output_desc, input_desc);
  593. if (in_op_desc->UpdateInputDesc(in_data_anchor->GetIdx(), input_desc) != GRAPH_SUCCESS) {
  594. REPORT_CALL_ERROR("E19999", "Update input:%d desc in op:%s(%s) failed", in_data_anchor->GetIdx(),
  595. in_op_desc->GetName().c_str(), in_op_desc->GetType().c_str());
  596. GELOGE(FAILED, "UpdateInputDesc fail.");
  597. return FAILED;
  598. }
  599. int output_idx = sub_graph_anchors_[anchors_index][i + 1].first->GetIdx();
  600. auto output_desc = in_op_desc->GetOutputDesc(output_idx);
  601. CopyTensorDesc(transdata_output_desc, output_desc);
  602. GE_IF_BOOL_EXEC(in_op_desc->UpdateOutputDesc(output_idx, output_desc) != GRAPH_SUCCESS,
  603. REPORT_CALL_ERROR("E19999", "Update output:%d desc in op:%s(%s) failed", output_idx,
  604. in_op_desc->GetName().c_str(), in_op_desc->GetType().c_str());
  605. GELOGE(GRAPH_FAILED, "update input desc failed");
  606. return GRAPH_FAILED);
  607. // relink control edge
  608. if (RelinkInControlEdge(in_owner_node, transdata_node_keep) != GRAPH_SUCCESS) {
  609. return GRAPH_FAILED;
  610. }
  611. }
  612. return GRAPH_SUCCESS;
  613. }
  614. void SameTransdataBreadthFusionPass::CopyTensorDesc(const ConstGeTensorDescPtr &src_desc, GeTensorDesc &dst_desc) {
  615. if (src_desc == nullptr) {
  616. return;
  617. }
  618. dst_desc.SetFormat(src_desc->GetFormat());
  619. dst_desc.SetOriginFormat(src_desc->GetOriginFormat());
  620. dst_desc.SetShape(src_desc->GetShape());
  621. dst_desc.SetOriginShape(src_desc->GetOriginShape());
  622. uint32_t real_dim = 0;
  623. if (TensorUtils::GetRealDimCnt(*src_desc, real_dim) == GRAPH_SUCCESS) {
  624. TensorUtils::SetRealDimCnt(dst_desc, real_dim);
  625. }
  626. }
  627. graphStatus SameTransdataBreadthFusionPass::LinkNewCastNode2RemainTransdata(
  628. const ComputeGraphPtr &graph, const vector<int> &same_transdata_nodes, const OutDataAnchorPtr &transdata_out_anchor,
  629. const NodePtr &transdata_node_keep) {
  630. for (size_t i = 1; i < same_transdata_nodes.size(); ++i) {
  631. int anchors_index = same_transdata_nodes[i];
  632. bool reuse_nodes = AllNodeBeforeTransdataHasOneDataOut(anchors_index);
  633. auto transdata_peer_out_anchor = sub_graph_anchors_[anchors_index].back().first;
  634. GE_CHECK_NOTNULL(transdata_peer_out_anchor);
  635. auto transdata_remove_in_anchor = sub_graph_anchors_[anchors_index].back().second;
  636. GE_CHECK_NOTNULL(transdata_remove_in_anchor);
  637. auto transdata_node_remove = transdata_remove_in_anchor->GetOwnerNode();
  638. if (transdata_node_remove->GetInDataNodes().size() > 1) {
  639. continue;
  640. }
  641. GELOGI("remove edge.src:%s, dst:%s", transdata_peer_out_anchor->GetOwnerNode()->GetName().c_str(),
  642. transdata_remove_in_anchor->GetOwnerNode()->GetName().c_str());
  643. if (GraphUtils::RemoveEdge(transdata_peer_out_anchor, transdata_remove_in_anchor) != GRAPH_SUCCESS) {
  644. REPORT_CALL_ERROR("E19999", "Remove edge between op:%s(%s)(index:%d) and op:%s(%s)(index:%d) failed",
  645. transdata_peer_out_anchor->GetOwnerNode()->GetName().c_str(),
  646. transdata_peer_out_anchor->GetOwnerNode()->GetType().c_str(),
  647. transdata_peer_out_anchor->GetIdx(),
  648. transdata_remove_in_anchor->GetOwnerNode()->GetName().c_str(),
  649. transdata_remove_in_anchor->GetOwnerNode()->GetType().c_str(),
  650. transdata_remove_in_anchor->GetIdx());
  651. return GRAPH_FAILED;
  652. }
  653. OutDataAnchorPtr pre_out_anchor = nullptr;
  654. NodePtr relink_node = nullptr;
  655. if (reuse_nodes) {
  656. // reuse nodes before transdata
  657. if (ReuseNodesBeforeTransdata(anchors_index, transdata_out_anchor, relink_node) != GRAPH_SUCCESS) {
  658. return GRAPH_FAILED;
  659. }
  660. if (before_transdata_nodes_[anchors_index].size() > kNoTransOp) {
  661. pre_out_anchor = transdata_peer_out_anchor;
  662. } else {
  663. pre_out_anchor = transdata_out_anchor;
  664. }
  665. } else {
  666. // miss cast control edge
  667. pre_out_anchor = transdata_out_anchor;
  668. if (AddCastNode(graph, same_transdata_nodes[i], pre_out_anchor, relink_node) != GRAPH_SUCCESS) {
  669. return GRAPH_FAILED;
  670. }
  671. }
  672. if (ReLinkTransdataOutput2PreNode(transdata_node_remove, pre_out_anchor, relink_node) != GRAPH_SUCCESS) {
  673. return GRAPH_FAILED;
  674. }
  675. if (RelinkInControlEdge(transdata_node_remove, transdata_node_keep) != GRAPH_SUCCESS) {
  676. return GRAPH_FAILED;
  677. }
  678. if (graph->RemoveNode(transdata_node_remove) != GRAPH_SUCCESS) {
  679. REPORT_CALL_ERROR("E19999", "Remove node:%s(%s) from graph:%s failed",
  680. transdata_node_remove->GetName().c_str(), transdata_node_remove->GetType().c_str(),
  681. graph->GetName().c_str());
  682. GELOGE(GRAPH_FAILED, "remove node %s failed!", transdata_node_remove->GetName().c_str());
  683. return GRAPH_FAILED;
  684. }
  685. }
  686. return GRAPH_SUCCESS;
  687. }
  688. graphStatus SameTransdataBreadthFusionPass::RelinkInControlEdge(const NodePtr &node_src, const NodePtr &node_dst) {
  689. GE_CHECK_NOTNULL(node_dst);
  690. GE_CHECK_NOTNULL(node_src);
  691. if (node_src->GetInControlNodes().empty()) {
  692. return GRAPH_SUCCESS;
  693. }
  694. GE_CHECK_NOTNULL(node_src->GetInControlAnchor());
  695. for (auto &peer_out_control_anchor : node_src->GetInControlAnchor()->GetPeerOutControlAnchors()) {
  696. GELOGD("remove edge.src:%s, dst:%s", peer_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  697. node_src->GetName().c_str());
  698. if (GraphUtils::RemoveEdge(peer_out_control_anchor, node_src->GetInControlAnchor()) != GRAPH_SUCCESS) {
  699. REPORT_CALL_ERROR("E19999", "Remove control edge between op:%s(%s) and op:%s(%s) failed",
  700. peer_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  701. peer_out_control_anchor->GetOwnerNode()->GetType().c_str(),
  702. node_src->GetName().c_str(), node_src->GetType().c_str());
  703. GELOGE(GRAPH_FAILED, "remove edge faliled!src:%s, dst:%s",
  704. peer_out_control_anchor->GetOwnerNode()->GetName().c_str(), node_src->GetName().c_str());
  705. return GRAPH_FAILED;
  706. }
  707. GELOGD("add edge.src:%s, dst:%s", peer_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  708. node_dst->GetName().c_str());
  709. if (GraphUtils::AddEdge(peer_out_control_anchor, node_dst->GetInControlAnchor()) != GRAPH_SUCCESS) {
  710. REPORT_CALL_ERROR("E19999", "Add control edge between op:%s(%s) and op:%s(%s) failed",
  711. peer_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  712. peer_out_control_anchor->GetOwnerNode()->GetType().c_str(),
  713. node_dst->GetName().c_str(), node_dst->GetType().c_str());
  714. GELOGE(GRAPH_FAILED, "add edge failed!src:%s, dst:%s", peer_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  715. node_dst->GetName().c_str());
  716. return GRAPH_FAILED;
  717. }
  718. }
  719. return GRAPH_SUCCESS;
  720. }
  721. graphStatus SameTransdataBreadthFusionPass::AddCastNode(const ComputeGraphPtr &graph, int anchors_index,
  722. OutDataAnchorPtr &pre_out_anchor, NodePtr &first_link_node) {
  723. GE_CHECK_NOTNULL(pre_out_anchor);
  724. GE_CHECK_NOTNULL(graph);
  725. auto pre_node = pre_out_anchor->GetOwnerNode();
  726. GE_CHECK_NOTNULL(pre_node->GetOpDesc());
  727. auto pre_output_desc = pre_node->GetOpDesc()->GetOutputDescPtr(pre_out_anchor->GetIdx());
  728. GE_CHECK_NOTNULL(pre_output_desc);
  729. for (size_t i = 0; i < sub_graph_anchors_[anchors_index].size() - 1; ++i) {
  730. auto in_data_anchor = sub_graph_anchors_[anchors_index][i].second;
  731. GE_CHECK_NOTNULL(in_data_anchor);
  732. auto in_owner_node = in_data_anchor->GetOwnerNode();
  733. auto in_op_desc = in_owner_node->GetOpDesc();
  734. GE_CHECK_NOTNULL(in_op_desc);
  735. auto input_desc = in_op_desc->GetInputDesc(in_data_anchor->GetIdx());
  736. input_desc.SetFormat(pre_output_desc->GetFormat());
  737. input_desc.SetOriginFormat(pre_output_desc->GetOriginFormat());
  738. input_desc.SetShape(pre_output_desc->GetShape());
  739. input_desc.SetOriginShape(pre_output_desc->GetOriginShape());
  740. uint32_t real_dim = 0;
  741. if (TensorUtils::GetRealDimCnt(*pre_output_desc, real_dim) != GRAPH_SUCCESS) {
  742. GELOGW("get %s real dim cnt failed!", pre_node->GetName().c_str());
  743. }
  744. TensorUtils::SetRealDimCnt(input_desc, real_dim);
  745. auto output_desc = in_op_desc->GetOutputDesc(sub_graph_anchors_[anchors_index][i + 1].first->GetIdx());
  746. output_desc.SetFormat(pre_output_desc->GetFormat());
  747. output_desc.SetOriginFormat(pre_output_desc->GetOriginFormat());
  748. output_desc.SetShape(pre_output_desc->GetShape());
  749. output_desc.SetOriginShape(pre_output_desc->GetOriginShape());
  750. TensorUtils::SetRealDimCnt(output_desc, real_dim);
  751. auto cast_op_desc = GetCastOp(input_desc, output_desc);
  752. if (cast_op_desc == nullptr) {
  753. return GRAPH_FAILED;
  754. }
  755. auto cast_node = graph->AddNode(cast_op_desc);
  756. if (cast_node == nullptr) {
  757. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  758. cast_op_desc->GetName().c_str(), cast_op_desc->GetType().c_str(), graph->GetName().c_str());
  759. return GRAPH_FAILED;
  760. }
  761. GELOGD("add edge.src:%s, dst:%s", pre_out_anchor->GetOwnerNode()->GetName().c_str(), cast_node->GetName().c_str());
  762. if (GraphUtils::AddEdge(pre_out_anchor, cast_node->GetInDataAnchor(0)) != GRAPH_SUCCESS) {
  763. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:%d) and op:%s(%s)(index:0) failed",
  764. pre_out_anchor->GetOwnerNode()->GetName().c_str(),
  765. pre_out_anchor->GetOwnerNode()->GetType().c_str(), pre_out_anchor->GetIdx(),
  766. cast_node->GetName().c_str(), cast_node->GetType().c_str());
  767. return GRAPH_FAILED;
  768. }
  769. if (i == 0) {
  770. first_link_node = cast_node;
  771. }
  772. if (!AttrUtils::SetBool(cast_op_desc, ATTR_NEED_COMPILE, true)) {
  773. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_NEED_COMPILE.c_str(),
  774. cast_op_desc->GetName().c_str(), cast_op_desc->GetType().c_str());
  775. GELOGE(FAILED, "SetExtAttr fail.");
  776. return FAILED;
  777. }
  778. pre_out_anchor = cast_node->GetOutDataAnchor(0);
  779. }
  780. return GRAPH_SUCCESS;
  781. }
  782. graphStatus SameTransdataBreadthFusionPass::GetSubGraphsBetweenNormalAndTransdataNode(
  783. OutDataAnchorPtr &out_anchor,
  784. std::vector<std::vector<std::pair<OutDataAnchorPtr, InDataAnchorPtr>>> &sub_graphs_out,
  785. std::vector<std::pair<OutDataAnchorPtr, InDataAnchorPtr>> &nodes_list) {
  786. graphStatus ret = GRAPH_SUCCESS;
  787. if (out_anchor == nullptr) {
  788. REPORT_INNER_ERROR("E19999", "Param out_anchor is nullptr, check invalid");
  789. GELOGE(GRAPH_FAILED, "out data anchor is null!This should not happen!");
  790. return GRAPH_FAILED;
  791. }
  792. for (auto &peer_in_anchor : out_anchor->GetPeerInDataAnchors()) {
  793. if (peer_in_anchor == nullptr || peer_in_anchor->GetOwnerNode() == nullptr ||
  794. peer_in_anchor->GetOwnerNode()->GetOpDesc() == nullptr) {
  795. continue;
  796. }
  797. nodes_list.push_back(make_pair(out_anchor, peer_in_anchor));
  798. auto peer_in_node = peer_in_anchor->GetOwnerNode();
  799. if ((peer_in_node->GetType() == TRANSDATA && peer_in_node->GetOutDataNodes().size() > 0) ||
  800. !IsHandleOp(peer_in_node)) {
  801. sub_graphs_out.push_back(nodes_list);
  802. nodes_list.pop_back();
  803. } else {
  804. if (peer_in_node->GetType() == TRANSDATA) {
  805. if (peer_in_node->GetOutDataNodes().size() == 0) {
  806. nodes_list.pop_back();
  807. continue;
  808. }
  809. }
  810. for (auto &peer_out_anchor : peer_in_node->GetAllOutDataAnchors()) {
  811. ret = GetSubGraphsBetweenNormalAndTransdataNode(peer_out_anchor, sub_graphs_out, nodes_list);
  812. if (ret != GRAPH_SUCCESS) {
  813. GELOGE(GRAPH_FAILED, "get all transop between normal node failed!node:%s", peer_in_node->GetName().c_str());
  814. return GRAPH_FAILED;
  815. }
  816. }
  817. nodes_list.pop_back();
  818. }
  819. }
  820. return GRAPH_SUCCESS;
  821. }
  822. bool SameTransdataBreadthFusionPass::IsTransOp(const NodePtr &node) {
  823. if (node == nullptr) {
  824. return false;
  825. }
  826. return node->GetType() == CAST || node->GetType() == TRANSPOSE || node->GetType() == TRANSPOSED ||
  827. node->GetType() == RESHAPE || node->GetType() == TRANSDATA;
  828. }
  829. bool SameTransdataBreadthFusionPass::IsHandleOp(const NodePtr &node) {
  830. if (node == nullptr) {
  831. return false;
  832. }
  833. return node->GetType() == CAST || node->GetType() == TRANSDATA;
  834. }
  835. } // namespace ge

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