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 51 kB

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

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