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

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

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