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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /**
  2. * Copyright 2019-2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "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 "framework/common/debug/ge_log.h"
  25. #include "graph/debug/ge_attr_define.h"
  26. #include "graph/utils/graph_utils.h"
  27. #include "graph/utils/op_desc_utils.h"
  28. #include "init/gelib.h"
  29. namespace {
  30. const char *const kRemainNode = "node_remain";
  31. const int kNoTransOp = 1;
  32. } // namespace
  33. namespace ge {
  34. void SameTransdataBreadthFusionPass::GetSubGraphNodesInfo() {
  35. vector<vector<NodePtr>> before_transdata_nodes(sub_graph_anchors_.size());
  36. vector<pair<int, InDataAnchorPtr>> all_transdata_nodes;
  37. for (size_t i = 0; i < sub_graph_anchors_.size(); ++i) {
  38. auto nodes_anchor = sub_graph_anchors_[i];
  39. auto iter = nodes_anchor.begin();
  40. auto first_out_anchor = iter->first;
  41. GE_CHECK_NOTNULL_JUST_RETURN(first_out_anchor);
  42. before_transdata_nodes[i].push_back(first_out_anchor->GetOwnerNode());
  43. GELOGD("index:%zu, node:%s, type:%s", i, first_out_anchor->GetOwnerNode()->GetName().c_str(),
  44. first_out_anchor->GetOwnerNode()->GetType().c_str());
  45. while (iter != nodes_anchor.end()) {
  46. auto in_anchor = iter->second;
  47. GE_CHECK_NOTNULL_JUST_RETURN(in_anchor);
  48. auto in_node = in_anchor->GetOwnerNode();
  49. GELOGD("index:%zu, node:%s, type:%s", i, first_out_anchor->GetOwnerNode()->GetName().c_str(),
  50. first_out_anchor->GetOwnerNode()->GetType().c_str());
  51. if (in_node->GetType() == TRANSDATA) {
  52. all_transdata_nodes.emplace_back(i, in_anchor);
  53. } else {
  54. before_transdata_nodes[i].push_back(in_node);
  55. }
  56. ++iter;
  57. }
  58. GELOGD("index:%zu, before trandata node size:%zu", i, before_transdata_nodes[i].size());
  59. }
  60. before_transdata_nodes_.swap(before_transdata_nodes);
  61. all_transdata_nodes_.swap(all_transdata_nodes);
  62. }
  63. OpDescPtr SameTransdataBreadthFusionPass::GetCastOp(const GeTensorDesc &in_desc, const GeTensorDesc &out_desc) {
  64. static uint32_t fusion_cast_op_count = 1;
  65. std::stringstream cast_op_name;
  66. cast_op_name << "fusion_cast_" << fusion_cast_op_count++;
  67. auto node_op = ge::OperatorFactory::CreateOperator(cast_op_name.str(), CAST);
  68. auto cast_op = ge::OpDescUtils::GetOpDescFromOperator(node_op);
  69. node_op.BreakConnect();
  70. if (cast_op == nullptr) {
  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. GELOGE(INTERNAL_ERROR, "set dst_type attr failed");
  96. return nullptr;
  97. }
  98. return cast_op;
  99. }
  100. void SameTransdataBreadthFusionPass::InsertSameTransdataNodeIndex(int anchors_index,
  101. vector<int> &same_transdata_nodes) {
  102. auto same_iter = same_transdata_nodes.begin();
  103. while (same_iter != same_transdata_nodes.end()) {
  104. if (before_transdata_nodes_[anchors_index].size() <= before_transdata_nodes_[*same_iter].size()) {
  105. same_transdata_nodes.insert(same_iter, anchors_index);
  106. return;
  107. }
  108. ++same_iter;
  109. }
  110. same_transdata_nodes.push_back(anchors_index);
  111. }
  112. void SameTransdataBreadthFusionPass::GetSameTransdataNode(vector<int> &same_transdata_nodes) {
  113. auto iter = all_transdata_nodes_.begin();
  114. same_transdata_nodes.push_back(iter->first);
  115. auto node_for_compare_in_anchor = iter->second;
  116. GE_CHECK_NOTNULL_JUST_RETURN(node_for_compare_in_anchor);
  117. auto node_for_compare = node_for_compare_in_anchor->GetOwnerNode();
  118. auto op_desc_for_compare = node_for_compare->GetOpDesc();
  119. GE_CHECK_NOTNULL_JUST_RETURN(op_desc_for_compare);
  120. string op_compare_stream_label;
  121. (void)AttrUtils::GetStr(op_desc_for_compare, ATTR_NAME_STREAM_LABEL, op_compare_stream_label);
  122. auto input_desc_for_compare = op_desc_for_compare->GetInputDescPtr(node_for_compare_in_anchor->GetIdx());
  123. GE_CHECK_NOTNULL_JUST_RETURN(input_desc_for_compare);
  124. auto output_desc_for_compare = op_desc_for_compare->GetOutputDescPtr(0);
  125. GE_CHECK_NOTNULL_JUST_RETURN(output_desc_for_compare);
  126. iter = all_transdata_nodes_.erase(iter);
  127. while (iter != all_transdata_nodes_.end()) {
  128. auto in_anchor = iter->second;
  129. if (in_anchor == nullptr) {
  130. continue;
  131. }
  132. auto node_tmp = in_anchor->GetOwnerNode();
  133. if (node_tmp == node_for_compare) {
  134. ++iter;
  135. continue;
  136. }
  137. GE_CHECK_NOTNULL_JUST_RETURN(node_tmp);
  138. auto op_desc_tmp = node_tmp->GetOpDesc();
  139. GE_CHECK_NOTNULL_JUST_RETURN(op_desc_tmp);
  140. auto input_desc_tmp = op_desc_tmp->GetInputDescPtr(in_anchor->GetIdx());
  141. auto output_desc_tmp = op_desc_tmp->GetOutputDescPtr(0);
  142. string op_tmp_stream_label;
  143. (void)AttrUtils::GetStr(op_desc_tmp, ATTR_NAME_STREAM_LABEL, op_tmp_stream_label);
  144. GE_CHECK_NOTNULL_JUST_RETURN(input_desc_tmp);
  145. GE_CHECK_NOTNULL_JUST_RETURN(output_desc_tmp);
  146. if ((op_compare_stream_label == op_tmp_stream_label) &&
  147. (input_desc_tmp->GetFormat() == input_desc_for_compare->GetFormat()) &&
  148. (output_desc_tmp->GetFormat() == output_desc_for_compare->GetFormat())) {
  149. GELOGD("same transdata node:%s, src node:%s", node_tmp->GetName().c_str(), node_for_compare->GetName().c_str());
  150. InsertSameTransdataNodeIndex(iter->first, same_transdata_nodes);
  151. iter = all_transdata_nodes_.erase(iter);
  152. } else {
  153. ++iter;
  154. }
  155. }
  156. }
  157. graphStatus SameTransdataBreadthFusionPass::ReLinkDataOutput2PreNode(const NodePtr &transdata_node,
  158. const OutDataAnchorPtr &pre_out_anchor,
  159. const NodePtr &relink_node) {
  160. GE_CHECK_NOTNULL(pre_out_anchor);
  161. GE_CHECK_NOTNULL(transdata_node);
  162. auto transdata_peer_out_control_anchor = pre_out_anchor->GetOwnerNode()->GetOutControlAnchor();
  163. for (auto &out_anchor : transdata_node->GetAllOutDataAnchors()) {
  164. // relink data edge
  165. for (auto &transdata_peer_in_anchor : out_anchor->GetPeerInDataAnchors()) {
  166. if (transdata_peer_in_anchor->GetOwnerNode() == relink_node) {
  167. continue;
  168. }
  169. GELOGI("remove edge.src:%s, dst:%s", out_anchor->GetOwnerNode()->GetName().c_str(),
  170. transdata_peer_in_anchor->GetOwnerNode()->GetName().c_str());
  171. if (GraphUtils::RemoveEdge(out_anchor, transdata_peer_in_anchor) != GRAPH_SUCCESS) {
  172. GELOGE(GRAPH_FAILED, "remove edge failed!src node:%s, dst node:%s", transdata_node->GetName().c_str(),
  173. transdata_peer_in_anchor->GetOwnerNode()->GetName().c_str());
  174. return GRAPH_FAILED;
  175. }
  176. GELOGI("add edge.src:%s, dst:%s", pre_out_anchor->GetOwnerNode()->GetName().c_str(),
  177. transdata_peer_in_anchor->GetOwnerNode()->GetName().c_str());
  178. if (GraphUtils::AddEdge(pre_out_anchor, transdata_peer_in_anchor) != GRAPH_SUCCESS) {
  179. GELOGE(GRAPH_FAILED, "add edge failed!src node:%s, dst node:%s",
  180. pre_out_anchor->GetOwnerNode()->GetName().c_str(),
  181. transdata_peer_in_anchor->GetOwnerNode()->GetName().c_str());
  182. return GRAPH_FAILED;
  183. }
  184. }
  185. }
  186. return GRAPH_SUCCESS;
  187. }
  188. graphStatus SameTransdataBreadthFusionPass::ReLinkOutDataPeerInControlNodes2PreNode(
  189. const NodePtr &transdata_node, const OutDataAnchorPtr &pre_out_anchor) {
  190. GE_CHECK_NOTNULL(pre_out_anchor);
  191. GE_CHECK_NOTNULL(transdata_node);
  192. auto transdata_peer_out_control_anchor = pre_out_anchor->GetOwnerNode()->GetOutControlAnchor();
  193. for (auto &out_anchor : transdata_node->GetAllOutDataAnchors()) {
  194. for (auto &transdata_peer_in_control_anchor : out_anchor->GetPeerInControlAnchors()) {
  195. GELOGD("remove edge.src:%s, dst:%s", out_anchor->GetOwnerNode()->GetName().c_str(),
  196. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str());
  197. if (GraphUtils::RemoveEdge(out_anchor, transdata_peer_in_control_anchor) != GRAPH_SUCCESS) {
  198. GELOGE(GRAPH_FAILED, "remove edge failed!src node:%s, dst node:%s", transdata_node->GetName().c_str(),
  199. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str());
  200. return GRAPH_FAILED;
  201. }
  202. if (transdata_peer_out_control_anchor == nullptr) {
  203. GELOGD("add edge.src:%s, dst:%s", pre_out_anchor->GetOwnerNode()->GetName().c_str(),
  204. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str());
  205. if (GraphUtils::AddEdge(pre_out_anchor, transdata_peer_in_control_anchor) != GRAPH_SUCCESS) {
  206. GELOGE(GRAPH_FAILED, "add edge failed!src node:%s, dst node:%s",
  207. pre_out_anchor->GetOwnerNode()->GetName().c_str(),
  208. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str());
  209. return GRAPH_FAILED;
  210. }
  211. } else {
  212. GELOGD("add edge.src node:%s, dst node:%s", pre_out_anchor->GetOwnerNode()->GetName().c_str(),
  213. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str());
  214. if (GraphUtils::AddEdge(transdata_peer_out_control_anchor, transdata_peer_in_control_anchor) != GRAPH_SUCCESS) {
  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_control_anchor->GetOwnerNode()->GetName().c_str());
  218. return GRAPH_FAILED;
  219. }
  220. }
  221. }
  222. }
  223. return GRAPH_SUCCESS;
  224. }
  225. graphStatus SameTransdataBreadthFusionPass::ReLinkTransdataOutput2PreNode(const NodePtr &transdata_node,
  226. const OutDataAnchorPtr &pre_out_anchor,
  227. const NodePtr &relink_node) {
  228. GE_CHECK_NOTNULL(pre_out_anchor);
  229. if (ReLinkDataOutput2PreNode(transdata_node, pre_out_anchor, relink_node) != GRAPH_SUCCESS) {
  230. return GRAPH_FAILED;
  231. }
  232. if (ReLinkOutDataPeerInControlNodes2PreNode(transdata_node, pre_out_anchor) != GRAPH_SUCCESS) {
  233. return GRAPH_FAILED;
  234. }
  235. auto transdata_peer_out_control_anchor = pre_out_anchor->GetOwnerNode()->GetOutControlAnchor();
  236. return ReLinkTransdataControlOutput2PreNode(transdata_node, pre_out_anchor, transdata_peer_out_control_anchor);
  237. }
  238. graphStatus SameTransdataBreadthFusionPass::ReLinkOutControlPeerInControlAnchors(
  239. const NodePtr &transdata_node_keep, const OutDataAnchorPtr &pre_out_anchor,
  240. const OutControlAnchorPtr &transdata_peer_out_control_anchor) {
  241. GE_CHECK_NOTNULL(transdata_node_keep);
  242. GE_CHECK_NOTNULL(pre_out_anchor);
  243. auto out_control_anchor = transdata_node_keep->GetOutControlAnchor();
  244. if (out_control_anchor == nullptr) {
  245. return GRAPH_SUCCESS;
  246. }
  247. for (auto &transdata_peer_in_control_anchor : out_control_anchor->GetPeerInControlAnchors()) {
  248. GELOGD("remove edge.src:%s, dst:%s", transdata_node_keep->GetName().c_str(),
  249. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str());
  250. if (GraphUtils::RemoveEdge(out_control_anchor, transdata_peer_in_control_anchor) != GRAPH_SUCCESS) {
  251. GELOGE(GRAPH_FAILED, "remove transdata control edge failed!");
  252. return GRAPH_FAILED;
  253. }
  254. if (transdata_peer_out_control_anchor == nullptr) {
  255. GELOGD("add edge.src:%s, dst:%s", pre_out_anchor->GetOwnerNode()->GetName().c_str(),
  256. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str());
  257. if (GraphUtils::AddEdge(pre_out_anchor, transdata_peer_in_control_anchor) != GRAPH_SUCCESS) {
  258. GELOGE(GRAPH_FAILED, "add control edge failed!");
  259. return GRAPH_FAILED;
  260. }
  261. } else {
  262. GELOGD("add edge.src:%s, dst:%s", transdata_peer_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  263. transdata_peer_in_control_anchor->GetOwnerNode()->GetName().c_str());
  264. if (GraphUtils::AddEdge(transdata_peer_out_control_anchor, transdata_peer_in_control_anchor) != GRAPH_SUCCESS) {
  265. GELOGE(GRAPH_FAILED, "add control edge failed!");
  266. return GRAPH_FAILED;
  267. }
  268. }
  269. }
  270. return GRAPH_SUCCESS;
  271. }
  272. graphStatus SameTransdataBreadthFusionPass::ReLinkOutControlPeerInDataAnchors(
  273. const NodePtr &transdata_node_keep, const OutDataAnchorPtr &pre_out_anchor,
  274. const OutControlAnchorPtr &transdata_peer_out_control_anchor) {
  275. GE_CHECK_NOTNULL(transdata_node_keep);
  276. GE_CHECK_NOTNULL(pre_out_anchor);
  277. auto out_control_anchor = transdata_node_keep->GetOutControlAnchor();
  278. if (out_control_anchor == nullptr) {
  279. return GRAPH_SUCCESS;
  280. }
  281. for (auto &transdata_peer_in_data_anchor : out_control_anchor->GetPeerInDataAnchors()) {
  282. if (transdata_peer_in_data_anchor == nullptr || transdata_peer_in_data_anchor->GetOwnerNode() == nullptr) {
  283. continue;
  284. }
  285. GELOGD("remove edge.src:%s, dst:%s", transdata_node_keep->GetName().c_str(),
  286. transdata_peer_in_data_anchor->GetOwnerNode()->GetName().c_str());
  287. if (GraphUtils::RemoveEdge(out_control_anchor, transdata_peer_in_data_anchor) != GRAPH_SUCCESS) {
  288. GELOGE(GRAPH_FAILED, "remove transdata control edge failed!");
  289. return GRAPH_FAILED;
  290. }
  291. if (transdata_peer_out_control_anchor == nullptr) {
  292. GELOGD("add edge.src:%s, dst:%s", pre_out_anchor->GetOwnerNode()->GetName().c_str(),
  293. transdata_peer_in_data_anchor->GetOwnerNode()->GetName().c_str());
  294. if (GraphUtils::AddEdge(pre_out_anchor, transdata_peer_in_data_anchor) != GRAPH_SUCCESS) {
  295. GELOGE(GRAPH_FAILED, "add control edge failed!");
  296. return GRAPH_FAILED;
  297. }
  298. } else {
  299. GELOGD("add edge.src:%s, dst:%s", transdata_peer_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  300. transdata_peer_in_data_anchor->GetOwnerNode()->GetName().c_str());
  301. if (GraphUtils::AddEdge(transdata_peer_out_control_anchor, transdata_peer_in_data_anchor) != GRAPH_SUCCESS) {
  302. GELOGE(GRAPH_FAILED, "add control edge failed!");
  303. return GRAPH_FAILED;
  304. }
  305. }
  306. }
  307. return GRAPH_SUCCESS;
  308. }
  309. graphStatus SameTransdataBreadthFusionPass::ReLinkTransdataControlOutput2PreNode(
  310. const NodePtr &transdata_node_keep, const OutDataAnchorPtr &pre_out_anchor,
  311. const OutControlAnchorPtr &transdata_peer_out_control_anchor) {
  312. if (ReLinkOutControlPeerInControlAnchors(transdata_node_keep, pre_out_anchor, transdata_peer_out_control_anchor) !=
  313. GRAPH_SUCCESS) {
  314. return GRAPH_FAILED;
  315. }
  316. return ReLinkOutControlPeerInDataAnchors(transdata_node_keep, pre_out_anchor, transdata_peer_out_control_anchor);
  317. }
  318. graphStatus SameTransdataBreadthFusionPass::Run(ComputeGraphPtr graph) {
  319. GE_TIMESTAMP_START(SameTransdataBreadthFusionPass);
  320. GELOGI("[SameTransdataBreadthFusionPass]: optimize begin.");
  321. if (graph == nullptr) {
  322. return GRAPH_SUCCESS;
  323. }
  324. for (auto &node : graph->GetDirectNode()) {
  325. if (IsTransOp(node) || node->GetOutDataNodes().size() <= 1) {
  326. continue;
  327. }
  328. GELOGD("Current normal node name: %s, type: %s.", node->GetName().c_str(), node->GetType().c_str());
  329. for (auto &out_anchor : node->GetAllOutDataAnchors()) {
  330. vector<std::vector<pair<OutDataAnchorPtr, InDataAnchorPtr>>> sub_graph_anchors;
  331. std::vector<pair<OutDataAnchorPtr, InDataAnchorPtr>> nodes_list;
  332. if (GetSubGraphsBetweenNormalAndTransdataNode(out_anchor, sub_graph_anchors, nodes_list) != GRAPH_SUCCESS) {
  333. GELOGW("get transop failed!");
  334. continue;
  335. }
  336. if (sub_graph_anchors.size() <= 1) {
  337. continue;
  338. }
  339. sub_graph_anchors_.swap(sub_graph_anchors);
  340. // check reshape node
  341. GetSubGraphNodesInfo();
  342. GELOGD("all trandata node size:%zu", all_transdata_nodes_.size());
  343. if (ExtractTransNode(graph) != GRAPH_SUCCESS) {
  344. return GRAPH_FAILED;
  345. }
  346. }
  347. }
  348. GELOGI("[SameTransdataBreadthFusionPass]: Optimize success.");
  349. GE_TIMESTAMP_END(SameTransdataBreadthFusionPass, "GraphManager::SameTransdataBreadthFusionPass");
  350. return GRAPH_SUCCESS;
  351. }
  352. graphStatus SameTransdataBreadthFusionPass::ExtractTransNode(const ComputeGraphPtr &graph) {
  353. while (all_transdata_nodes_.size() > 1) {
  354. vector<int> same_transdata_nodes;
  355. GetSameTransdataNode(same_transdata_nodes);
  356. GELOGD("same transdata node size:%zu", same_transdata_nodes.size());
  357. // reuse transdata ,new cast
  358. if (same_transdata_nodes.size() <= 1) {
  359. continue;
  360. }
  361. int anchors_index = same_transdata_nodes[0];
  362. auto transdata_in_anchor = sub_graph_anchors_[anchors_index].back().second;
  363. GE_CHECK_NOTNULL(transdata_in_anchor);
  364. auto transdata_node_keep = transdata_in_anchor->GetOwnerNode();
  365. auto transdata_out_anchor = transdata_node_keep->GetOutDataAnchor(0);
  366. GELOGD("anchor index %d, before transdata node size:%zu", anchors_index,
  367. before_transdata_nodes_[anchors_index].size());
  368. if (before_transdata_nodes_[anchors_index].size() > 1) {
  369. if (RelinkRemainTransdata(graph, same_transdata_nodes) != GRAPH_SUCCESS) {
  370. return GRAPH_FAILED;
  371. }
  372. }
  373. if (LinkNewCastNode2RemainTransdata(graph, same_transdata_nodes, transdata_out_anchor, transdata_node_keep) !=
  374. GRAPH_SUCCESS) {
  375. return GRAPH_FAILED;
  376. }
  377. }
  378. return GRAPH_SUCCESS;
  379. }
  380. graphStatus SameTransdataBreadthFusionPass::RelinkRemainTransdata(const ComputeGraphPtr &graph,
  381. const vector<int> &same_transdata_nodes) {
  382. int anchors_index = same_transdata_nodes[0];
  383. auto head_node_anchor = sub_graph_anchors_[anchors_index][0].first;
  384. GE_CHECK_NOTNULL(head_node_anchor);
  385. auto head_node = head_node_anchor->GetOwnerNode();
  386. GE_CHECK_NOTNULL(head_node->GetOpDesc());
  387. auto head_output_desc = head_node->GetOpDesc()->GetOutputDescPtr(head_node_anchor->GetIdx());
  388. auto transdata_in_anchor = sub_graph_anchors_[anchors_index].back().second;
  389. GE_CHECK_NOTNULL(transdata_in_anchor);
  390. auto transdata_node_keep = transdata_in_anchor->GetOwnerNode();
  391. GE_CHECK_NOTNULL(transdata_node_keep->GetOpDesc());
  392. auto transdata_out_anchor = transdata_node_keep->GetOutDataAnchor(0);
  393. GELOGD("head node:%s, transdata node keep:%s", head_node->GetName().c_str(), transdata_node_keep->GetName().c_str());
  394. bool reuse_nodes = AllNodeBeforeTransdataHasOneDataOut(anchors_index);
  395. UpdateTransdataDesc(transdata_in_anchor, transdata_node_keep->GetOpDesc(), head_output_desc);
  396. auto transdata_peer_out_anchor = sub_graph_anchors_[anchors_index].back().first;
  397. GE_CHECK_NOTNULL(transdata_peer_out_anchor);
  398. auto transdata_peer_out_node = transdata_peer_out_anchor->GetOwnerNode();
  399. GELOGI("remove edge.src:%s, dst:%s", transdata_peer_out_node->GetName().c_str(),
  400. transdata_node_keep->GetName().c_str());
  401. if (GraphUtils::RemoveEdge(transdata_peer_out_anchor, transdata_in_anchor) != GRAPH_SUCCESS) {
  402. GELOGW("remove edge failed!out node %s, in node %s", transdata_peer_out_node->GetName().c_str(),
  403. transdata_node_keep->GetName().c_str());
  404. }
  405. GELOGI("add edge.out node %s, in node %s", head_node->GetName().c_str(), transdata_node_keep->GetName().c_str());
  406. if (GraphUtils::AddEdge(head_node_anchor, transdata_in_anchor) != GRAPH_SUCCESS) {
  407. GELOGE(GRAPH_FAILED, "add edge failed!out node %s, in node %s", head_node->GetName().c_str(),
  408. transdata_node_keep->GetName().c_str());
  409. return GRAPH_FAILED;
  410. }
  411. NodePtr relink_node;
  412. // relink to transdata output nodes
  413. if (reuse_nodes) {
  414. if (ReuseNodesBeforeTransdata(anchors_index, transdata_out_anchor, relink_node) != GRAPH_SUCCESS) {
  415. return GRAPH_FAILED;
  416. }
  417. if (ReLinkTransdataOutput2PreNode(transdata_node_keep, transdata_peer_out_anchor, relink_node) != GRAPH_SUCCESS) {
  418. return GRAPH_FAILED;
  419. }
  420. } else {
  421. OutDataAnchorPtr pre_out_anchor = transdata_out_anchor;
  422. if (AddCastNode(graph, same_transdata_nodes[0], pre_out_anchor, relink_node) != GRAPH_SUCCESS) {
  423. return GRAPH_FAILED;
  424. }
  425. if (ReLinkTransdataOutput2PreNode(transdata_node_keep, pre_out_anchor, relink_node) != GRAPH_SUCCESS) {
  426. return GRAPH_FAILED;
  427. }
  428. }
  429. return GRAPH_SUCCESS;
  430. }
  431. void SameTransdataBreadthFusionPass::UpdateTransdataDesc(const InDataAnchorPtr &transdata_in_anchor,
  432. const OpDescPtr &transdata_op_desc,
  433. const ConstGeTensorDescPtr &head_output_desc) {
  434. if (transdata_op_desc == nullptr || transdata_in_anchor == nullptr || head_output_desc == nullptr) {
  435. return;
  436. }
  437. auto mutable_input_desc = transdata_op_desc->MutableInputDesc(transdata_in_anchor->GetIdx());
  438. GE_CHECK_NOTNULL_JUST_RETURN(mutable_input_desc);
  439. mutable_input_desc->SetDataType(head_output_desc->GetDataType());
  440. mutable_input_desc->SetOriginDataType(head_output_desc->GetOriginDataType());
  441. auto mutable_output_desc = transdata_op_desc->MutableOutputDesc(0);
  442. GE_CHECK_NOTNULL_JUST_RETURN(mutable_output_desc);
  443. mutable_output_desc->SetDataType(head_output_desc->GetDataType());
  444. mutable_output_desc->SetOriginDataType(head_output_desc->GetOriginDataType());
  445. // maybe need to check support
  446. }
  447. bool SameTransdataBreadthFusionPass::AllNodeBeforeTransdataHasOneDataOut(int anchors_index) {
  448. for (size_t i = 1; i < before_transdata_nodes_[anchors_index].size(); ++i) {
  449. auto node = before_transdata_nodes_[anchors_index][i];
  450. if (node == nullptr) {
  451. return false;
  452. }
  453. if (node->GetOutDataNodes().size() > 1 || node->GetInDataNodes().size() > 1) {
  454. return false;
  455. }
  456. }
  457. return true;
  458. }
  459. graphStatus SameTransdataBreadthFusionPass::ReuseNodesBeforeTransdata(int anchors_index,
  460. const OutDataAnchorPtr &transdata_out_anchor,
  461. NodePtr &relink_node) {
  462. auto head_node_anchor = sub_graph_anchors_[anchors_index][0].first;
  463. auto head_node_peer_anchor = sub_graph_anchors_[anchors_index][0].second;
  464. GE_CHECK_NOTNULL(head_node_anchor);
  465. GE_CHECK_NOTNULL(head_node_peer_anchor);
  466. GE_CHECK_NOTNULL(transdata_out_anchor);
  467. GELOGI("remove edge.src:%s, dst:%s", head_node_anchor->GetOwnerNode()->GetName().c_str(),
  468. head_node_peer_anchor->GetOwnerNode()->GetName().c_str());
  469. if (head_node_anchor->IsLinkedWith(head_node_peer_anchor)) {
  470. if (GraphUtils::RemoveEdge(head_node_anchor, head_node_peer_anchor) != GRAPH_SUCCESS) {
  471. GELOGW("remove edge failed!src:%s, dst:%s", head_node_anchor->GetOwnerNode()->GetName().c_str(),
  472. head_node_peer_anchor->GetOwnerNode()->GetName().c_str());
  473. }
  474. } else {
  475. GELOGW("edge not link now. src:%s, dst:%s", head_node_anchor->GetOwnerNode()->GetName().c_str(),
  476. head_node_peer_anchor->GetOwnerNode()->GetName().c_str());
  477. }
  478. NodePtr transdata_node_keep = transdata_out_anchor->GetOwnerNode();
  479. if (before_transdata_nodes_[anchors_index].size() == kNoTransOp) {
  480. return GRAPH_SUCCESS;
  481. }
  482. GELOGI("add edge.src:%s, dst:%s", transdata_node_keep->GetName().c_str(),
  483. head_node_peer_anchor->GetOwnerNode()->GetName().c_str());
  484. if (GraphUtils::AddEdge(transdata_out_anchor, head_node_peer_anchor) != GRAPH_SUCCESS) {
  485. GELOGE(GRAPH_FAILED, "add edge.src:%s, dst:%s", transdata_node_keep->GetName().c_str(),
  486. head_node_peer_anchor->GetOwnerNode()->GetName().c_str());
  487. return GRAPH_FAILED;
  488. }
  489. relink_node = head_node_peer_anchor->GetOwnerNode();
  490. GE_CHECK_NOTNULL(transdata_node_keep->GetOpDesc());
  491. auto transdata_output_desc = transdata_node_keep->GetOpDesc()->GetOutputDescPtr(0);
  492. GE_CHECK_NOTNULL(transdata_output_desc);
  493. for (size_t i = 0; i < sub_graph_anchors_[anchors_index].size() - 1; ++i) {
  494. auto in_data_anchor = sub_graph_anchors_[anchors_index][i].second;
  495. GE_CHECK_NOTNULL(in_data_anchor);
  496. auto in_owner_node = in_data_anchor->GetOwnerNode();
  497. auto in_op_desc = in_owner_node->GetOpDesc();
  498. GE_CHECK_NOTNULL(in_op_desc);
  499. auto input_desc = in_op_desc->GetInputDesc(in_data_anchor->GetIdx());
  500. CopyTensorDesc(transdata_output_desc, input_desc);
  501. if (in_op_desc->UpdateInputDesc(in_data_anchor->GetIdx(), input_desc) != GRAPH_SUCCESS) {
  502. GELOGE(FAILED, "UpdateInputDesc fail.");
  503. return FAILED;
  504. }
  505. int output_idx = sub_graph_anchors_[anchors_index][i + 1].first->GetIdx();
  506. auto output_desc = in_op_desc->GetOutputDesc(output_idx);
  507. CopyTensorDesc(transdata_output_desc, output_desc);
  508. GE_IF_BOOL_EXEC(in_op_desc->UpdateOutputDesc(output_idx, output_desc) != GRAPH_SUCCESS,
  509. GELOGE(GRAPH_FAILED, "update input desc failed");
  510. return GRAPH_FAILED);
  511. // relink control edge
  512. if (RelinkInControlEdge(in_owner_node, transdata_node_keep) != GRAPH_SUCCESS) {
  513. return GRAPH_FAILED;
  514. }
  515. }
  516. return GRAPH_SUCCESS;
  517. }
  518. void SameTransdataBreadthFusionPass::CopyTensorDesc(const ConstGeTensorDescPtr &src_desc, GeTensorDesc &dst_desc) {
  519. if (src_desc == nullptr) {
  520. return;
  521. }
  522. dst_desc.SetFormat(src_desc->GetFormat());
  523. dst_desc.SetOriginFormat(src_desc->GetOriginFormat());
  524. dst_desc.SetShape(src_desc->GetShape());
  525. dst_desc.SetOriginShape(src_desc->GetOriginShape());
  526. uint32_t real_dim = 0;
  527. if (TensorUtils::GetRealDimCnt(*src_desc, real_dim) == GRAPH_SUCCESS) {
  528. TensorUtils::SetRealDimCnt(dst_desc, real_dim);
  529. }
  530. }
  531. graphStatus SameTransdataBreadthFusionPass::LinkNewCastNode2RemainTransdata(
  532. const ComputeGraphPtr &graph, const vector<int> &same_transdata_nodes, const OutDataAnchorPtr &transdata_out_anchor,
  533. const NodePtr &transdata_node_keep) {
  534. for (size_t i = 1; i < same_transdata_nodes.size(); ++i) {
  535. int anchors_index = same_transdata_nodes[i];
  536. bool reuse_nodes = AllNodeBeforeTransdataHasOneDataOut(anchors_index);
  537. auto transdata_peer_out_anchor = sub_graph_anchors_[anchors_index].back().first;
  538. GE_CHECK_NOTNULL(transdata_peer_out_anchor);
  539. auto transdata_remove_in_anchor = sub_graph_anchors_[anchors_index].back().second;
  540. GE_CHECK_NOTNULL(transdata_remove_in_anchor);
  541. auto transdata_node_remove = transdata_remove_in_anchor->GetOwnerNode();
  542. if (transdata_node_remove->GetInDataNodes().size() > 1) {
  543. continue;
  544. }
  545. GELOGI("remove edge.src:%s, dst:%s", transdata_peer_out_anchor->GetOwnerNode()->GetName().c_str(),
  546. transdata_remove_in_anchor->GetOwnerNode()->GetName().c_str());
  547. if (GraphUtils::RemoveEdge(transdata_peer_out_anchor, transdata_remove_in_anchor) != GRAPH_SUCCESS) {
  548. return GRAPH_FAILED;
  549. }
  550. OutDataAnchorPtr pre_out_anchor = nullptr;
  551. NodePtr relink_node = nullptr;
  552. if (reuse_nodes) {
  553. // reuse nodes before transdata
  554. if (ReuseNodesBeforeTransdata(anchors_index, transdata_out_anchor, relink_node) != GRAPH_SUCCESS) {
  555. return GRAPH_FAILED;
  556. }
  557. if (before_transdata_nodes_[anchors_index].size() > kNoTransOp) {
  558. pre_out_anchor = transdata_peer_out_anchor;
  559. } else {
  560. pre_out_anchor = transdata_out_anchor;
  561. }
  562. } else {
  563. // miss cast control edge
  564. pre_out_anchor = transdata_out_anchor;
  565. if (AddCastNode(graph, same_transdata_nodes[i], pre_out_anchor, relink_node) != GRAPH_SUCCESS) {
  566. return GRAPH_FAILED;
  567. }
  568. }
  569. if (ReLinkTransdataOutput2PreNode(transdata_node_remove, pre_out_anchor, relink_node) != GRAPH_SUCCESS) {
  570. return GRAPH_FAILED;
  571. }
  572. if (RelinkInControlEdge(transdata_node_remove, transdata_node_keep) != GRAPH_SUCCESS) {
  573. return GRAPH_FAILED;
  574. }
  575. if (graph->RemoveNode(transdata_node_remove) != GRAPH_SUCCESS) {
  576. GELOGE(GRAPH_FAILED, "remove node %s failed!", transdata_node_remove->GetName().c_str());
  577. return GRAPH_FAILED;
  578. }
  579. }
  580. return GRAPH_SUCCESS;
  581. }
  582. graphStatus SameTransdataBreadthFusionPass::RelinkInControlEdge(const NodePtr &node_src, const NodePtr &node_dst) {
  583. GE_CHECK_NOTNULL(node_dst);
  584. GE_CHECK_NOTNULL(node_src);
  585. if (node_src->GetInControlNodes().empty()) {
  586. return GRAPH_SUCCESS;
  587. }
  588. GE_CHECK_NOTNULL(node_src->GetInControlAnchor());
  589. for (auto &peer_out_control_anchor : node_src->GetInControlAnchor()->GetPeerOutControlAnchors()) {
  590. GELOGD("remove edge.src:%s, dst:%s", peer_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  591. node_src->GetName().c_str());
  592. if (GraphUtils::RemoveEdge(peer_out_control_anchor, node_src->GetInControlAnchor()) != GRAPH_SUCCESS) {
  593. GELOGE(GRAPH_FAILED, "remove edge faliled!src:%s, dst:%s",
  594. peer_out_control_anchor->GetOwnerNode()->GetName().c_str(), node_src->GetName().c_str());
  595. return GRAPH_FAILED;
  596. }
  597. GELOGD("add edge.src:%s, dst:%s", peer_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  598. node_dst->GetName().c_str());
  599. if (GraphUtils::AddEdge(peer_out_control_anchor, node_dst->GetInControlAnchor()) != GRAPH_SUCCESS) {
  600. GELOGE(GRAPH_FAILED, "add edge failed!src:%s, dst:%s", peer_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  601. node_dst->GetName().c_str());
  602. return GRAPH_FAILED;
  603. }
  604. }
  605. return GRAPH_SUCCESS;
  606. }
  607. graphStatus SameTransdataBreadthFusionPass::AddCastNode(const ComputeGraphPtr &graph, int anchors_index,
  608. OutDataAnchorPtr &pre_out_anchor, NodePtr &first_link_node) {
  609. GE_CHECK_NOTNULL(pre_out_anchor);
  610. GE_CHECK_NOTNULL(graph);
  611. auto pre_node = pre_out_anchor->GetOwnerNode();
  612. GE_CHECK_NOTNULL(pre_node->GetOpDesc());
  613. auto pre_output_desc = pre_node->GetOpDesc()->GetOutputDescPtr(pre_out_anchor->GetIdx());
  614. GE_CHECK_NOTNULL(pre_output_desc);
  615. for (size_t i = 0; i < sub_graph_anchors_[anchors_index].size() - 1; ++i) {
  616. auto in_data_anchor = sub_graph_anchors_[anchors_index][i].second;
  617. GE_CHECK_NOTNULL(in_data_anchor);
  618. auto in_owner_node = in_data_anchor->GetOwnerNode();
  619. auto in_op_desc = in_owner_node->GetOpDesc();
  620. GE_CHECK_NOTNULL(in_op_desc);
  621. auto input_desc = in_op_desc->GetInputDesc(in_data_anchor->GetIdx());
  622. input_desc.SetFormat(pre_output_desc->GetFormat());
  623. input_desc.SetOriginFormat(pre_output_desc->GetOriginFormat());
  624. input_desc.SetShape(pre_output_desc->GetShape());
  625. input_desc.SetOriginShape(pre_output_desc->GetOriginShape());
  626. uint32_t real_dim = 0;
  627. if (TensorUtils::GetRealDimCnt(*pre_output_desc, real_dim) != GRAPH_SUCCESS) {
  628. GELOGW("get %s real dim cnt failed!", pre_node->GetName().c_str());
  629. }
  630. TensorUtils::SetRealDimCnt(input_desc, real_dim);
  631. auto output_desc = in_op_desc->GetOutputDesc(sub_graph_anchors_[anchors_index][i + 1].first->GetIdx());
  632. output_desc.SetFormat(pre_output_desc->GetFormat());
  633. output_desc.SetOriginFormat(pre_output_desc->GetOriginFormat());
  634. output_desc.SetShape(pre_output_desc->GetShape());
  635. output_desc.SetOriginShape(pre_output_desc->GetOriginShape());
  636. TensorUtils::SetRealDimCnt(output_desc, real_dim);
  637. auto cast_op_desc = GetCastOp(input_desc, output_desc);
  638. if (cast_op_desc == nullptr) {
  639. return GRAPH_FAILED;
  640. }
  641. auto cast_node = graph->AddNode(cast_op_desc);
  642. if (cast_node == nullptr) {
  643. return GRAPH_FAILED;
  644. }
  645. GELOGD("add edge.src:%s, dst:%s", pre_out_anchor->GetOwnerNode()->GetName().c_str(), cast_node->GetName().c_str());
  646. if (GraphUtils::AddEdge(pre_out_anchor, cast_node->GetInDataAnchor(0)) != GRAPH_SUCCESS) {
  647. return GRAPH_FAILED;
  648. }
  649. if (i == 0) {
  650. first_link_node = cast_node;
  651. }
  652. if (!AttrUtils::SetBool(cast_op_desc, ATTR_NEED_COMPILE, true)) {
  653. GELOGE(FAILED, "SetExtAttr fail.");
  654. return FAILED;
  655. }
  656. pre_out_anchor = cast_node->GetOutDataAnchor(0);
  657. }
  658. return GRAPH_SUCCESS;
  659. }
  660. graphStatus SameTransdataBreadthFusionPass::GetSubGraphsBetweenNormalAndTransdataNode(
  661. OutDataAnchorPtr &out_anchor, std::vector<std::vector<std::pair<OutDataAnchorPtr, InDataAnchorPtr>>> &sub_graphs_out,
  662. std::vector<std::pair<OutDataAnchorPtr, InDataAnchorPtr>> &nodes_list) {
  663. graphStatus ret = GRAPH_SUCCESS;
  664. if (out_anchor == nullptr) {
  665. GELOGE(GRAPH_FAILED, "out data anchor is null!This should not happen!");
  666. return GRAPH_FAILED;
  667. }
  668. for (auto &peer_in_anchor : out_anchor->GetPeerInDataAnchors()) {
  669. if (peer_in_anchor == nullptr || peer_in_anchor->GetOwnerNode() == nullptr ||
  670. peer_in_anchor->GetOwnerNode()->GetOpDesc() == nullptr) {
  671. continue;
  672. }
  673. nodes_list.push_back(make_pair(out_anchor, peer_in_anchor));
  674. auto peer_in_node = peer_in_anchor->GetOwnerNode();
  675. if ((peer_in_node->GetType() == TRANSDATA && peer_in_node->GetOutDataNodes().size() > 0) ||
  676. !IsHandleOp(peer_in_node)) {
  677. sub_graphs_out.push_back(nodes_list);
  678. nodes_list.pop_back();
  679. } else {
  680. if (peer_in_node->GetType() == TRANSDATA) {
  681. if (peer_in_node->GetOutDataNodes().size() == 0) {
  682. nodes_list.pop_back();
  683. continue;
  684. }
  685. }
  686. for (auto &peer_out_anchor : peer_in_node->GetAllOutDataAnchors()) {
  687. ret = GetSubGraphsBetweenNormalAndTransdataNode(peer_out_anchor, sub_graphs_out, nodes_list);
  688. if (ret != GRAPH_SUCCESS) {
  689. GELOGE(GRAPH_FAILED, "get all transop between normal node failed!node:%s", peer_in_node->GetName().c_str());
  690. return GRAPH_FAILED;
  691. }
  692. }
  693. nodes_list.pop_back();
  694. }
  695. }
  696. return GRAPH_SUCCESS;
  697. }
  698. bool SameTransdataBreadthFusionPass::IsTransOp(const NodePtr &node) {
  699. if (node == nullptr) {
  700. return false;
  701. }
  702. return node->GetType() == CAST || node->GetType() == TRANSPOSE || node->GetType() == TRANSPOSED ||
  703. node->GetType() == RESHAPE || node->GetType() == TRANSDATA;
  704. }
  705. bool SameTransdataBreadthFusionPass::IsHandleOp(const NodePtr &node) {
  706. if (node == nullptr) {
  707. return false;
  708. }
  709. return node->GetType() == CAST || node->GetType() == TRANSDATA;
  710. }
  711. } // namespace ge

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