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

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