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.

mem_rw_conflict_optimize.cc 32 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. * Unless required by applicable law or agreed to in writing, software
  8. * distributed under the License is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. * See the License for the specific language governing permissions and
  11. * limitations under the License.
  12. */
  13. #include <string>
  14. #include <vector>
  15. #include "common/ge/ge_util.h"
  16. #include "graph/common/omg_util.h"
  17. #include "graph/debug/ge_attr_define.h"
  18. #include "graph/optimize/graph_optimize.h"
  19. #include "graph/utils/graph_utils.h"
  20. #include "graph/utils/node_utils.h"
  21. namespace {
  22. using namespace ge;
  23. const int kIdentityAnchorIndex = 0;
  24. // rw type of input.
  25. enum class InputRWType {
  26. kReadOnly, // Normal op input only read
  27. kWriteable, // Op like Assign/ApplyMomentum
  28. kScopeWriteable, // Op like hcom_allreduce, it will modify input ,but not expect take effect on pre ouput
  29. kInvalidRWType
  30. };
  31. // rw type of output
  32. enum class OutputRWType {
  33. kReadOnly, // 1.const output 2.not ref output but has several peer output
  34. kSoftRead, // not ref output but only has one output node
  35. kWriteable, // ref output. Like Assign/ApplyMomentum
  36. kInvalidRWType
  37. };
  38. // input and output rw_type of one node. key is anchor_idx, value is rw_type
  39. struct NodeInputOutputRWType {
  40. map<uint32_t, InputRWType> input_rw_type_map;
  41. map<uint32_t, OutputRWType> output_rw_type_map;
  42. };
  43. // input and output rw_type of node in current graph
  44. thread_local map<string, NodeInputOutputRWType> node_rwtype_map_;
  45. ///
  46. /// @brief Convert input rw_type enum to string. For log print.
  47. /// @param rw_type
  48. /// @return rw_type_name
  49. ///
  50. static std::string InputRWTypeToSerialString(InputRWType rw_type) {
  51. const static char *names[4] = {"ReadOnly", "Writeable", "ScopeWriteable", "InvalidRWType"};
  52. return names[static_cast<int>(rw_type)];
  53. }
  54. ///
  55. /// @brief Convert output rw_type enum to string. For log print.
  56. /// @param rw_type
  57. /// @return rw_type_name
  58. ///
  59. static std::string OutputRWTypeToSerialString(OutputRWType rw_type) {
  60. const static char *names[4] = {"ReadOnly", "SoftRead", "Writeable", "InvalidRWType"};
  61. return names[static_cast<int>(rw_type)];
  62. }
  63. OutputRWType GetSingleNodeOutputRWTypeByIndex(const Node &node, uint32_t index) {
  64. auto op_desc = node.GetOpDesc();
  65. if (op_desc == nullptr) {
  66. return OutputRWType::kInvalidRWType;
  67. }
  68. if (op_desc->GetType() == VARIABLE) {
  69. return OutputRWType::kWriteable;
  70. }
  71. // check if it is ref output
  72. auto input_names = op_desc->GetAllInputName();
  73. for (auto &input_name_2_idx : input_names) {
  74. if (op_desc->GetOutputNameByIndex(index) == input_name_2_idx.first) {
  75. return OutputRWType::kWriteable;
  76. }
  77. }
  78. // check if it is ref switch
  79. std::string type;
  80. if ((node.GetType() == FRAMEWORK_OP_TYPE) && AttrUtils::GetStr(op_desc, ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE, type)
  81. && (type == REFSWITCH)) {
  82. return OutputRWType::kWriteable;
  83. }
  84. if (op_desc->GetType() == CONSTANT || op_desc->GetType() == CONSTANTOP) {
  85. return OutputRWType::kReadOnly;
  86. }
  87. auto out_data_anchor = node.GetOutDataAnchor(index);
  88. if (out_data_anchor == nullptr) {
  89. return OutputRWType::kInvalidRWType;
  90. }
  91. if (out_data_anchor->GetPeerInDataNodesSize() > 1) {
  92. return OutputRWType::kReadOnly;
  93. } else {
  94. return OutputRWType::kSoftRead;
  95. }
  96. }
  97. ///
  98. /// @brief Get input rw_type of one node with sub graph. It will return rw_type after solve conflict scene.
  99. /// @param rw_type_set
  100. /// @return
  101. ///
  102. InputRWType GetInputRwTypeInConflict(const std::set<int> &rw_type_set) {
  103. // for input rw type calc
  104. int total_rw_type = 0;
  105. for (const auto rw : rw_type_set) {
  106. total_rw_type += rw;
  107. }
  108. switch (total_rw_type) {
  109. case 0:
  110. return InputRWType::kReadOnly; // all input rw type is readonly
  111. case 2:
  112. return InputRWType::kScopeWriteable; // readonly 2 scope_writeable
  113. case 3:
  114. return InputRWType::kWriteable; // all input rw type is writeable or readonly 2 writeable
  115. case 5:
  116. return InputRWType::kInvalidRWType; // writeable 2 scope_writeable
  117. default:
  118. return InputRWType::kInvalidRWType;
  119. }
  120. }
  121. bool IsSubgraphInputNode(const NodePtr &node) {
  122. if ((node == nullptr) || (node->GetOpDesc() == nullptr) || (node->GetType() != DATA) ||
  123. (node->GetOwnerComputeGraph()->GetParentNode() == nullptr)) {
  124. return false;
  125. }
  126. return true;
  127. }
  128. bool IsSubgraphOutputNode(const NodePtr &node) {
  129. if ((node == nullptr) || (node->GetOpDesc() == nullptr) || (node->GetType() != NETOUTPUT) ||
  130. (node->GetOwnerComputeGraph()->GetParentNode() == nullptr)) {
  131. return false;
  132. }
  133. return true;
  134. }
  135. NodePtr CreateIdentityAfterSrcNode(const Node &src_node, int out_anchor_idx) {
  136. if (src_node.GetOpDesc() == nullptr) {
  137. return nullptr;
  138. }
  139. static std::atomic_long identity_num(0);
  140. auto next_num = identity_num.fetch_add(1);
  141. // 1. create new identity op desc
  142. string identity_name = src_node.GetName() + "_" + IDENTITY + std::to_string(next_num);
  143. auto identity_opdesc = MakeShared<OpDesc>(identity_name, IDENTITY);
  144. if (identity_opdesc == nullptr) {
  145. GELOGE(OUT_OF_MEMORY, "Failed to insert identity node, name %s", identity_name.c_str());
  146. return nullptr;
  147. }
  148. auto data_desc = src_node.GetOpDesc()->GetOutputDesc(out_anchor_idx);
  149. // 2. add input_desc & output_desc for new identity
  150. Status ret = identity_opdesc->AddInputDesc("x", data_desc);
  151. if (ret != SUCCESS) {
  152. GELOGE(ret, "Add Input desc failed for new identity %s.", identity_name.c_str());
  153. return nullptr;
  154. }
  155. ret = identity_opdesc->AddOutputDesc("y", data_desc);
  156. if (ret != SUCCESS) {
  157. GELOGE(ret, "Add Output desc failed for new Identity %s.", identity_name.c_str());
  158. return nullptr;
  159. }
  160. GELOGI("Insert new Identity node %s.", identity_name.c_str());
  161. auto graph = src_node.GetOwnerComputeGraph();
  162. if (graph == nullptr) {
  163. GELOGE(GRAPH_PARAM_INVALID, "Node %s owner compute graph is null.", src_node.GetName().c_str());
  164. return nullptr;
  165. }
  166. return graph->AddNode(identity_opdesc);
  167. }
  168. OutputRWType GetOutputRWTypeByIndex(const Node &node, uint32_t index) {
  169. auto op_desc = node.GetOpDesc();
  170. if (op_desc == nullptr) {
  171. return OutputRWType::kInvalidRWType;
  172. }
  173. if (op_desc->GetType() == WHILE) {
  174. return OutputRWType::kSoftRead;
  175. }
  176. vector<string> subgraph_names = op_desc->GetSubgraphInstanceNames();
  177. if (subgraph_names.empty()) {
  178. // single node without sub graph
  179. return GetSingleNodeOutputRWTypeByIndex(node, index);
  180. } else {
  181. // node with sub graph
  182. auto output_node_vec = NodeUtils::GetSubgraphOutputNodes(node);
  183. auto output_rw_type = OutputRWType::kInvalidRWType;
  184. if (output_node_vec.size() == 1) {
  185. // find rw type from map.
  186. auto iter = node_rwtype_map_.find(output_node_vec.at(0)->GetName());
  187. if (iter == node_rwtype_map_.end()) {
  188. GELOGW("Can not find rw type of node %s from map.It could take some effect on following preprocess.",
  189. output_node_vec.at(0)->GetName().c_str());
  190. return OutputRWType::kInvalidRWType;
  191. }
  192. auto index_2_output_rw_type = iter->second.output_rw_type_map.find(index);
  193. if (index_2_output_rw_type == iter->second.output_rw_type_map.end()) {
  194. GELOGW("Can not find rw type of node %s from map.It could take some effect on following preprocess.",
  195. output_node_vec.at(0)->GetName().c_str());
  196. return OutputRWType::kInvalidRWType;
  197. }
  198. output_rw_type = index_2_output_rw_type->second;
  199. } else {
  200. output_rw_type = OutputRWType::kSoftRead;
  201. }
  202. // check peer input
  203. auto out_data_anchor = node.GetOutDataAnchor(index);
  204. if (out_data_anchor == nullptr) {
  205. return OutputRWType::kInvalidRWType;
  206. }
  207. if (out_data_anchor->GetPeerInDataNodesSize() > 1) {
  208. return OutputRWType::kReadOnly;
  209. } else {
  210. return output_rw_type;
  211. }
  212. }
  213. }
  214. InputRWType GetSingleNodeInputRWTypeByIndex(const Node &node, uint32_t index) {
  215. auto op_desc = node.GetOpDesc();
  216. if (op_desc == nullptr) {
  217. return InputRWType::kInvalidRWType;
  218. }
  219. if (op_desc->GetType() == HCOMALLREDUCE || op_desc->GetType() == HCOMALLGATHER
  220. || op_desc->GetType() == HCOMREDUCESCATTER || op_desc->GetType() == HCOMREDUCE) {
  221. return InputRWType::kScopeWriteable;
  222. }
  223. // check if it is ref input
  224. auto output_names = op_desc->GetAllOutputName();
  225. for (auto &output_name_2_idx : output_names) {
  226. if (op_desc->GetInputNameByIndex(index) == output_name_2_idx.first) {
  227. return InputRWType::kWriteable;
  228. }
  229. }
  230. // check if it is ref switch
  231. std::string type;
  232. if ((node.GetType() == FRAMEWORK_OP_TYPE) && (AttrUtils::GetStr(op_desc, ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE, type))
  233. && (type == REFSWITCH) && (index == 0)) {
  234. return InputRWType::kWriteable;
  235. }
  236. return InputRWType::kReadOnly;
  237. }
  238. InputRWType GetInputRWTypeByIndex(const Node &node, uint32_t index) {
  239. auto op_desc = node.GetOpDesc();
  240. if (op_desc == nullptr) {
  241. return InputRWType::kInvalidRWType;
  242. }
  243. if (op_desc->GetType() == WHILE) {
  244. return InputRWType::kScopeWriteable;
  245. }
  246. vector<string> subgraph_names = op_desc->GetSubgraphInstanceNames();
  247. if (subgraph_names.empty()) {
  248. // single node without sub graph
  249. return GetSingleNodeInputRWTypeByIndex(node, index);
  250. } else {
  251. // node with sub graph
  252. std::set<int> node_rw_type_set;
  253. auto data_node_vec = NodeUtils::GetSubgraphDataNodesByIndex(node, index);
  254. // get all input data node in subgraph
  255. std::set<int> anchor_rw_type_set;
  256. for (const auto &data_node : data_node_vec) {
  257. // Data only has 1 out data anchor. Here just take first out data anchor. And index 0 is valid.
  258. auto out_data_anchor = data_node->GetOutDataAnchor(0);
  259. if (out_data_anchor == nullptr) {
  260. continue;
  261. }
  262. auto data_op_desc = data_node->GetOpDesc();
  263. if (data_op_desc == nullptr) {
  264. continue;
  265. }
  266. // find rw type from map.
  267. auto iter = node_rwtype_map_.find(data_op_desc->GetName());
  268. if (iter == node_rwtype_map_.end()) {
  269. GELOGW("Can not find rw type of node %s from map.It could take some effect on following preprocess.",
  270. data_op_desc->GetName().c_str());
  271. return InputRWType::kInvalidRWType;
  272. }
  273. auto input_rw_type = iter->second.input_rw_type_map.find(out_data_anchor->GetIdx());
  274. if (input_rw_type == iter->second.input_rw_type_map.end()) {
  275. GELOGW("Can not find rw type of node %s from map.It could take some effect on following preprocess.",
  276. data_op_desc->GetName().c_str());
  277. return InputRWType::kInvalidRWType;
  278. }
  279. anchor_rw_type_set.emplace(static_cast<int>(input_rw_type->second));
  280. }
  281. return GetInputRwTypeInConflict(anchor_rw_type_set);
  282. }
  283. }
  284. Status MarkRWTypeForSubgraph(const ComputeGraphPtr &sub_graph) {
  285. for (const auto &node : sub_graph->GetDirectNode()) {
  286. GE_CHECK_NOTNULL(node);
  287. GE_CHECK_NOTNULL(node->GetOpDesc());
  288. std::set<int> anchor_rw_type_set;
  289. if (node->GetType() == DATA) {
  290. // calc all input_rw_type of peer output , as input_rw_type of DATA. Index 0 is valid.
  291. auto anchor_2_node_vec = NodeUtils::GetOutDataNodesWithAnchorByIndex(*node, 0);
  292. for (const auto anchor_2_node_pair : anchor_2_node_vec) {
  293. auto input_rw_type = GetInputRWTypeByIndex(*anchor_2_node_pair.second, anchor_2_node_pair.first->GetIdx());
  294. GELOGD("Input rw type of Node %s %dth input anchor is %s", anchor_2_node_pair.second->GetName().c_str(),
  295. anchor_2_node_pair.first->GetIdx(), InputRWTypeToSerialString(input_rw_type).c_str());
  296. anchor_rw_type_set.emplace(static_cast<int>(input_rw_type));
  297. }
  298. auto anchor_rw_type = GetInputRwTypeInConflict(anchor_rw_type_set);
  299. GELOGD("Input rw type of Node %s is %s", node->GetName().c_str(),
  300. InputRWTypeToSerialString(anchor_rw_type).c_str());
  301. map<uint32_t, InputRWType> input_rw_type_map{std::make_pair(0, anchor_rw_type)};
  302. NodeInputOutputRWType data_rw_type{input_rw_type_map};
  303. node_rwtype_map_.emplace(std::make_pair(node->GetName(), data_rw_type));
  304. }
  305. if (node->GetType() == NETOUTPUT) {
  306. // calc all output_rw_type of peer input , as output_rw_type of DATA
  307. map<uint32_t, OutputRWType> output_rw_type_map;
  308. for (const auto &in_data_anchor : node->GetAllInDataAnchors()) {
  309. GE_CHECK_NOTNULL(in_data_anchor);
  310. auto pre_out_anchor = in_data_anchor->GetPeerOutAnchor();
  311. GE_CHECK_NOTNULL(pre_out_anchor);
  312. auto pre_node = pre_out_anchor->GetOwnerNode();
  313. GE_CHECK_NOTNULL(pre_node);
  314. auto pre_output_rw_type = GetOutputRWTypeByIndex(*pre_node, pre_out_anchor->GetIdx());
  315. GELOGD("Output rw type of Node %s %dth output anchor is %s", pre_node->GetName().c_str(),
  316. pre_out_anchor->GetIdx(), OutputRWTypeToSerialString(pre_output_rw_type).c_str());
  317. auto parent_node = sub_graph->GetParentNode();
  318. if (pre_output_rw_type == OutputRWType::kWriteable && parent_node->GetType() != PARTITIONEDCALL) {
  319. // insert identity
  320. auto identity_node = CreateIdentityAfterSrcNode(*pre_node, pre_out_anchor->GetIdx());
  321. GE_CHECK_NOTNULL(identity_node);
  322. auto ret = GraphUtils::InsertNodeBetweenDataAnchors(pre_out_anchor, in_data_anchor, identity_node);
  323. if (ret != SUCCESS) {
  324. GELOGE(ret, "Fail to insert identity");
  325. return ret;
  326. }
  327. GELOGI("InsertNode %s between %s and %s successfully.", identity_node->GetName().c_str(),
  328. pre_node->GetName().c_str(), node->GetName().c_str());
  329. pre_output_rw_type = OutputRWType::kSoftRead;
  330. }
  331. output_rw_type_map.emplace(std::make_pair(in_data_anchor->GetIdx(), pre_output_rw_type));
  332. }
  333. NodeInputOutputRWType output_rw_type{{}, output_rw_type_map};
  334. node_rwtype_map_.emplace(std::make_pair(node->GetName(), output_rw_type));
  335. }
  336. }
  337. return SUCCESS;
  338. }
  339. ///
  340. /// @brief Reverse traversal all subgraph and mark rw_type for Data/Netoutput.
  341. /// @param sub_graph_vecgs
  342. ///
  343. Status MarkRWTypeForAllSubgraph(const vector<ComputeGraphPtr> &sub_graph_vec) {
  344. for (auto iter = sub_graph_vec.rbegin(); iter != sub_graph_vec.rend(); ++iter) {
  345. auto parent_node = (*iter)->GetParentNode();
  346. if (parent_node == nullptr) {
  347. GELOGD("Current sub graph has no parent node. Ignore it.");
  348. continue;
  349. }
  350. if (parent_node->GetType() == WHILE) {
  351. continue;
  352. }
  353. auto ret = MarkRWTypeForSubgraph(*iter);
  354. if (ret != SUCCESS) {
  355. return ret;
  356. }
  357. }
  358. return SUCCESS;
  359. }
  360. ///
  361. /// @brief Check identity is near subgraph.
  362. /// Eg. As output of Data node in subgraph
  363. /// or as input of Netoutput of subgraph
  364. /// or as input of one node with subgraph
  365. /// or as output of one node with subgraph
  366. /// @param node
  367. /// @return is_near_subgraph
  368. ///
  369. bool CheckIdentityIsNearSubgraph(const Node &node) {
  370. for (const auto &in_node : node.GetInDataNodes()) {
  371. auto in_node_opdesc = in_node->GetOpDesc();
  372. if (in_node_opdesc == nullptr) {
  373. continue;
  374. }
  375. // near entrance of subgraph
  376. if (IsSubgraphInputNode(in_node)) {
  377. return true;
  378. }
  379. // near subgraph
  380. if (!in_node_opdesc->GetSubgraphInstanceNames().empty()) {
  381. return true;
  382. }
  383. }
  384. for (const auto &out_node : node.GetOutDataNodes()) {
  385. auto out_node_opdesc = out_node->GetOpDesc();
  386. if (out_node_opdesc == nullptr) {
  387. continue;
  388. }
  389. // near output of subgraph
  390. if (IsSubgraphOutputNode(out_node)) {
  391. return true;
  392. }
  393. // near subgraph
  394. if (!out_node_opdesc->GetSubgraphInstanceNames().empty()) {
  395. return true;
  396. }
  397. }
  398. return false;
  399. }
  400. enum ConflictResult { DO_NOTHING, WRONG_GRAPH, INSERT_IDENTITY };
  401. vector<vector<ConflictResult>> output_2_input_rwtype = {{DO_NOTHING, WRONG_GRAPH, INSERT_IDENTITY},
  402. {DO_NOTHING, WRONG_GRAPH, DO_NOTHING},
  403. {DO_NOTHING, DO_NOTHING, INSERT_IDENTITY}};
  404. ConflictResult GetConflictResultBetweenNode(const OutputRWType output_rw_type, const InputRWType input_rw_type) {
  405. if (output_rw_type == OutputRWType::kInvalidRWType || input_rw_type == InputRWType::kInvalidRWType) {
  406. return WRONG_GRAPH;
  407. }
  408. auto n = static_cast<int>(output_rw_type);
  409. auto m = static_cast<int>(input_rw_type);
  410. // no need to check index or container, because container and index is all defined.
  411. return output_2_input_rwtype[n][m];
  412. }
  413. ///
  414. /// @brief Keep identity_node which near subgraph or has multi output
  415. /// @param node
  416. /// @return
  417. ///
  418. Status RemoveNoUseIdentity(const NodePtr &node) {
  419. if (node->GetInDataNodes().empty() || node->GetOutDataNodesSize() > 1) {
  420. return SUCCESS;
  421. }
  422. if (node->GetOutDataNodesSize() == 1 && node->GetOutDataNodes().at(0)->GetType() == STREAMMERGE) {
  423. return SUCCESS;
  424. }
  425. if (CheckIdentityIsNearSubgraph(*node)) {
  426. return SUCCESS;
  427. }
  428. GE_CHECK_NOTNULL(node->GetInDataAnchor(kIdentityAnchorIndex));
  429. auto pre_out_anchor = node->GetInDataAnchor(kIdentityAnchorIndex)->GetPeerOutAnchor();
  430. GE_CHECK_NOTNULL(pre_out_anchor);
  431. auto pre_node = pre_out_anchor->GetOwnerNode();
  432. auto pre_output_rw_type = GetOutputRWTypeByIndex(*pre_node, pre_out_anchor->GetIdx());
  433. auto anchor_2_outnode_vec = NodeUtils::GetOutDataNodesWithAnchorByIndex(*node, kIdentityAnchorIndex);
  434. ConflictResult conflict_result = WRONG_GRAPH;
  435. if (!anchor_2_outnode_vec.empty()) {
  436. auto anchor_2_outnode = anchor_2_outnode_vec.at(0);
  437. auto peer_input_rw_type = GetInputRWTypeByIndex(*anchor_2_outnode.second, anchor_2_outnode.first->GetIdx());
  438. GELOGD("Pre Node %s %dth output rw type is %s, peer node %s %dth input rw type is %s.", pre_node->GetName().c_str(),
  439. pre_out_anchor->GetIdx(), OutputRWTypeToSerialString(pre_output_rw_type).c_str(),
  440. anchor_2_outnode.second->GetName().c_str(), anchor_2_outnode.first->GetIdx(),
  441. InputRWTypeToSerialString(peer_input_rw_type).c_str());
  442. conflict_result = GetConflictResultBetweenNode(pre_output_rw_type, peer_input_rw_type);
  443. } else {
  444. // identity node has no out data node, it can be removed
  445. conflict_result = DO_NOTHING;
  446. }
  447. if (conflict_result != DO_NOTHING) {
  448. return SUCCESS;
  449. }
  450. GELOGI("No need insert Identity. Node %s need to remove.", node->GetName().c_str());
  451. auto ret = GraphUtils::IsolateNode(node, {0});
  452. if (ret != SUCCESS) {
  453. GELOGE(ret, "Fail to isolate node %s.", node->GetName().c_str());
  454. return ret;
  455. }
  456. ret = GraphUtils::RemoveNodeWithoutRelink(node->GetOwnerComputeGraph(), node);
  457. if (ret != SUCCESS) {
  458. GELOGE(ret, "Fail to isolate node %s.", node->GetName().c_str());
  459. return ret;
  460. }
  461. GELOGI("Pre node is %s and %dth output rw type is %s. Isolate and remove Identity node %s.",
  462. pre_node->GetName().c_str(), pre_out_anchor->GetIdx(), OutputRWTypeToSerialString(pre_output_rw_type).c_str(),
  463. node->GetName().c_str());
  464. return SUCCESS;
  465. }
  466. Status SplitIdentityAlongAnchor(const OutDataAnchorPtr &out_data_anchor, const InDataAnchorPtr &peer_in_data_anchor,
  467. const OutDataAnchorPtr &pre_out_data_anchor, NodePtr &pre_node) {
  468. // 1.check peer in node RW type.
  469. GE_CHECK_NOTNULL(peer_in_data_anchor);
  470. auto peer_in_data_node = peer_in_data_anchor->GetOwnerNode();
  471. GE_CHECK_NOTNULL(peer_in_data_node);
  472. auto input_rw_type = GetInputRWTypeByIndex(*peer_in_data_node, peer_in_data_anchor->GetIdx());
  473. auto ret = out_data_anchor->Unlink(peer_in_data_anchor);
  474. auto old_identity = out_data_anchor->GetOwnerNode();
  475. if (ret != SUCCESS) {
  476. GELOGE(ret, "Failed to unlink from %s %dth out to %s.", old_identity->GetName().c_str(), out_data_anchor->GetIdx(),
  477. peer_in_data_anchor->GetOwnerNode()->GetName().c_str());
  478. return ret;
  479. }
  480. if (input_rw_type == InputRWType::kScopeWriteable || input_rw_type == InputRWType::kWriteable) {
  481. auto new_identity = CreateIdentityAfterSrcNode(*pre_node, pre_out_data_anchor->GetIdx());
  482. GE_CHECK_NOTNULL(new_identity);
  483. if (GraphUtils::AddEdge(pre_out_data_anchor, new_identity->GetInDataAnchor(kIdentityAnchorIndex)) != SUCCESS
  484. || GraphUtils::AddEdge(new_identity->GetOutDataAnchor(kIdentityAnchorIndex), peer_in_data_anchor) != SUCCESS) {
  485. GELOGE(INTERNAL_ERROR, "Failed to insert Identity between node %s and %s",
  486. pre_out_data_anchor->GetOwnerNode()->GetName().c_str(),
  487. peer_in_data_anchor->GetOwnerNode()->GetName().c_str());
  488. return INTERNAL_ERROR;
  489. }
  490. // 2. copy in-control-edge from dst to Identity
  491. if (GraphUtils::CopyInCtrlEdges(peer_in_data_node, new_identity) != SUCCESS) {
  492. GELOGE(INTERNAL_ERROR, "Failed to copy in_control edges from node %s to %s", peer_in_data_node->GetName().c_str(),
  493. new_identity->GetName().c_str());
  494. return INTERNAL_ERROR;
  495. }
  496. GELOGI("Node %s intput rw type is %s. Insert Identity between %s and %s.", peer_in_data_node->GetName().c_str(),
  497. InputRWTypeToSerialString(input_rw_type).c_str(), pre_out_data_anchor->GetOwnerNode()->GetName().c_str(),
  498. peer_in_data_anchor->GetOwnerNode()->GetName().c_str());
  499. } else {
  500. // copy control edge to pre and peer node
  501. if (GraphUtils::CopyInCtrlEdges(old_identity, peer_in_data_node) != SUCCESS
  502. || GraphUtils::CopyOutCtrlEdges(old_identity, pre_node) != SUCCESS) {
  503. GELOGW("Fail to copy control edge from node %s.", old_identity->GetName().c_str());
  504. return FAILED;
  505. }
  506. // link identity pre node to next node directly
  507. if (GraphUtils::AddEdge(pre_out_data_anchor, peer_in_data_anchor) != SUCCESS) {
  508. GELOGW("Fail to link data edge from node %s to %s.", pre_out_data_anchor->GetOwnerNode()->GetName().c_str(),
  509. peer_in_data_anchor->GetOwnerNode()->GetName().c_str());
  510. return FAILED;
  511. }
  512. GELOGI("Node %s input rw type is %s, link data edge from Identity input node %s to out node %s directly.",
  513. peer_in_data_node->GetName().c_str(), InputRWTypeToSerialString(input_rw_type).c_str(),
  514. pre_node->GetName().c_str(), peer_in_data_node->GetName().c_str());
  515. }
  516. return SUCCESS;
  517. }
  518. Status SplitIdentity(const NodePtr &node) {
  519. GE_CHECK_NOTNULL(node);
  520. auto out_data_anchor = node->GetOutDataAnchor(kIdentityAnchorIndex);
  521. GE_CHECK_NOTNULL(out_data_anchor);
  522. if (out_data_anchor->GetPeerInDataNodesSize() <= 1) {
  523. return SUCCESS;
  524. }
  525. // get pre node and next node of identity
  526. GE_CHECK_NOTNULL(node->GetInDataAnchor(kIdentityAnchorIndex));
  527. auto pre_out_data_anchor = node->GetInDataAnchor(kIdentityAnchorIndex)->GetPeerOutAnchor();
  528. GE_CHECK_NOTNULL(pre_out_data_anchor);
  529. auto pre_node = pre_out_data_anchor->GetOwnerNode();
  530. GE_CHECK_NOTNULL(pre_node);
  531. for (const auto &peer_in_data_anchor : out_data_anchor->GetPeerInDataAnchors()) {
  532. Status ret = SplitIdentityAlongAnchor(out_data_anchor, peer_in_data_anchor, pre_out_data_anchor, pre_node);
  533. if (ret != SUCCESS) {
  534. GELOGE(ret, "Split identity node along anchor failed.");
  535. return ret;
  536. }
  537. }
  538. // 2.isolate Identity node with no data output
  539. if (node->GetOutDataNodesSize() == 0) {
  540. Status ret = GraphUtils::IsolateNode(node, {});
  541. if (ret != SUCCESS) {
  542. GELOGE(FAILED, "IsolateAndDelete identity node %s.", node->GetName().c_str());
  543. return FAILED;
  544. }
  545. ret = GraphUtils::RemoveNodeWithoutRelink(node->GetOwnerComputeGraph(), node);
  546. if (ret != SUCCESS) {
  547. GELOGE(FAILED, "IsolateAndDelete identity node %s.", node->GetName().c_str());
  548. return FAILED;
  549. }
  550. GELOGI("IsolateAndDelete identity node %s.", node->GetName().c_str());
  551. }
  552. return SUCCESS;
  553. }
  554. Status InsertIdentityAsNeeded(const NodePtr &node) {
  555. auto op_desc = node->GetOpDesc();
  556. GE_CHECK_NOTNULL(op_desc);
  557. if (node->GetOutDataNodesSize() == 0) {
  558. return SUCCESS;
  559. }
  560. for (const auto &out_data_anchor : node->GetAllOutDataAnchors()) {
  561. GE_CHECK_NOTNULL(out_data_anchor);
  562. auto output_rw_type = GetOutputRWTypeByIndex(*node, out_data_anchor->GetIdx());
  563. for (const auto &peer_in_data_anchor : out_data_anchor->GetPeerInDataAnchors()) {
  564. GE_CHECK_NOTNULL(peer_in_data_anchor);
  565. auto peer_in_node = peer_in_data_anchor->GetOwnerNode();
  566. GE_CHECK_NOTNULL(peer_in_node);
  567. auto input_rw_type = GetInputRWTypeByIndex(*peer_in_node, peer_in_data_anchor->GetIdx());
  568. GELOGD("Node %s output rw type is %s, Node %s input rw type is %s", node->GetName().c_str(),
  569. OutputRWTypeToSerialString(output_rw_type).c_str(), peer_in_node->GetName().c_str(),
  570. InputRWTypeToSerialString(input_rw_type).c_str());
  571. auto conflict_result = GetConflictResultBetweenNode(output_rw_type, input_rw_type);
  572. switch (conflict_result) {
  573. case DO_NOTHING:
  574. case WRONG_GRAPH:
  575. GELOGD("No need insert Identity.");
  576. continue;
  577. case INSERT_IDENTITY:
  578. auto identity_node = CreateIdentityAfterSrcNode(*node, out_data_anchor->GetIdx());
  579. if (identity_node == nullptr) {
  580. GELOGE(FAILED, "Create identity node failed.");
  581. return FAILED;
  582. }
  583. auto ret = GraphUtils::InsertNodeBetweenDataAnchors(out_data_anchor, peer_in_data_anchor, identity_node);
  584. if (ret != GRAPH_SUCCESS) {
  585. GELOGE(INTERNAL_ERROR, "Failed to insert reshape between node %s and %s", node->GetName().c_str(),
  586. peer_in_node->GetName().c_str());
  587. return INTERNAL_ERROR;
  588. }
  589. GELOGI("Insert Identity between %s and %s to handle memory conflict.", node->GetName().c_str(),
  590. peer_in_node->GetName().c_str());
  591. continue;
  592. }
  593. }
  594. }
  595. return SUCCESS;
  596. }
  597. Status HandleAllreduceDuplicateInput(ComputeGraphPtr &compute_graph) {
  598. for (const auto &node : compute_graph->GetDirectNode()) {
  599. if (node->GetType() == HCOMALLREDUCE) {
  600. std::set<OutDataAnchorPtr> pre_out_anchor_set;
  601. for (const auto &in_data_anchor : node->GetAllInDataAnchors()) {
  602. auto pre_out_anchor = in_data_anchor->GetPeerOutAnchor();
  603. GE_CHECK_NOTNULL(pre_out_anchor);
  604. if (pre_out_anchor_set.find(pre_out_anchor) == pre_out_anchor_set.end()) {
  605. pre_out_anchor_set.emplace(pre_out_anchor);
  606. continue;
  607. }
  608. // need insert identity
  609. auto pre_node = pre_out_anchor->GetOwnerNode();
  610. auto identity_node = CreateIdentityAfterSrcNode(*pre_node, pre_out_anchor->GetIdx());
  611. GE_CHECK_NOTNULL(identity_node);
  612. auto ret = GraphUtils::InsertNodeBetweenDataAnchors(pre_out_anchor, in_data_anchor, identity_node);
  613. GE_CHK_STATUS_RET(ret, "Fail to insert identity.");
  614. GELOGI("InsertNode %s between %s and %s successfully.", identity_node->GetName().c_str(),
  615. pre_node->GetName().c_str(), node->GetName().c_str());
  616. }
  617. }
  618. }
  619. return SUCCESS;
  620. }
  621. } // namespace
  622. namespace ge {
  623. Status GraphOptimize::CheckRWConflict(ComputeGraphPtr &compute_graph, bool &has_conflict) {
  624. node_rwtype_map_.clear();
  625. auto sub_graph_vec = compute_graph->GetAllSubgraphs();
  626. if (sub_graph_vec.empty()) {
  627. GELOGD("No sub graph here. Ignore memory conflict handle.");
  628. return SUCCESS;
  629. }
  630. // 1.loop all subgraph, mark rw type from inside to outside
  631. Status ret = MarkRWTypeForAllSubgraph(sub_graph_vec);
  632. if (ret != SUCCESS) {
  633. GELOGE(ret, "Fail to mark rw type for subgraph.");
  634. return ret;
  635. }
  636. has_conflict = false;
  637. for (const auto &node : compute_graph->GetAllNodes()) {
  638. auto op_desc = node->GetOpDesc();
  639. GE_CHECK_NOTNULL(op_desc);
  640. if (node->GetOutDataNodesSize() == 0) {
  641. return SUCCESS;
  642. }
  643. if (node->GetType() == WHILE) {
  644. return SUCCESS;
  645. }
  646. for (const auto &out_data_anchor : node->GetAllOutDataAnchors()) {
  647. GE_CHECK_NOTNULL(out_data_anchor);
  648. auto output_rw_type = GetOutputRWTypeByIndex(*node, out_data_anchor->GetIdx());
  649. for (const auto &peer_in_data_anchor : out_data_anchor->GetPeerInDataAnchors()) {
  650. GE_CHECK_NOTNULL(peer_in_data_anchor);
  651. auto peer_in_node = peer_in_data_anchor->GetOwnerNode();
  652. GE_CHECK_NOTNULL(peer_in_node);
  653. if (peer_in_node->GetType() == WHILE) {
  654. return SUCCESS;
  655. }
  656. auto input_rw_type = GetInputRWTypeByIndex(*peer_in_node, peer_in_data_anchor->GetIdx());
  657. auto conflict_result = GetConflictResultBetweenNode(output_rw_type, input_rw_type);
  658. switch (conflict_result) {
  659. case DO_NOTHING:
  660. GELOGD("No rw conflict.");
  661. continue;
  662. case WRONG_GRAPH:
  663. has_conflict = true;
  664. GELOGI("Node %s output rw type is %s, next node %s input_rw_type is %s.It is wrong graph.",
  665. node->GetName().c_str(), OutputRWTypeToSerialString(output_rw_type).c_str(),
  666. peer_in_node->GetName().c_str(), InputRWTypeToSerialString(input_rw_type).c_str());
  667. return SUCCESS;
  668. case INSERT_IDENTITY:
  669. GELOGD("There is rw conflict. It will handle later.");
  670. continue;
  671. }
  672. }
  673. }
  674. }
  675. return SUCCESS;
  676. }
  677. Status GraphOptimize::HandleMemoryRWConflict(ComputeGraphPtr &compute_graph) {
  678. GE_DUMP(compute_graph, "BeforeHandleMemConflict");
  679. node_rwtype_map_.clear();
  680. auto sub_graph_vec = compute_graph->GetAllSubgraphs();
  681. if (sub_graph_vec.empty()) {
  682. // only root graph, to handle allreduce servral input from one output anchor
  683. return HandleAllreduceDuplicateInput(compute_graph);
  684. }
  685. // 1.loop all subgraph, mark rw type from inside to outside
  686. Status ret = MarkRWTypeForAllSubgraph(sub_graph_vec);
  687. if (ret != SUCCESS) {
  688. GELOGE(ret, "Fail to mark rw type for subgraph.");
  689. return ret;
  690. }
  691. // 2.loop all node, including node in subgraph and handle memory rw conflict
  692. for (auto &node : compute_graph->GetAllNodes()) {
  693. // ignore while subgraph node
  694. const auto parent_node = node->GetOwnerComputeGraph()->GetParentNode();
  695. if ((parent_node != nullptr) && (kWhileOpTypes.count(parent_node->GetType()) > 0)) {
  696. continue;
  697. }
  698. // ignore data / netoutput of subgraph
  699. if (node->GetType() == DATA && AttrUtils::HasAttr(node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX)) {
  700. continue;
  701. }
  702. if (node->GetType() == NETOUTPUT && AttrUtils::HasAttr(node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX)) {
  703. continue;
  704. }
  705. if (node->GetType() == IDENTITY || node->GetType() == READVARIABLEOP) {
  706. // split identity
  707. ret = SplitIdentity(node);
  708. if (ret != SUCCESS) {
  709. GELOGE(ret, "Fail to split identity node %s.", node->GetName().c_str());
  710. return ret;
  711. }
  712. // remove no use identity
  713. ret = RemoveNoUseIdentity(node);
  714. if (ret != SUCCESS) {
  715. GELOGE(ret, "Fail to remove useless identity node %s.", node->GetName().c_str());
  716. return ret;
  717. }
  718. }
  719. // insert Identity
  720. ret = InsertIdentityAsNeeded(node);
  721. if (ret != SUCCESS) {
  722. GELOGE(ret, "Fail to insert Identity node.");
  723. return ret;
  724. }
  725. }
  726. GE_DUMP(compute_graph, "AfterHandleMemConflict");
  727. return SUCCESS;
  728. }
  729. } // namespace ge

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