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.

variable_prepare_op_pass.cc 23 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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/variable_prepare_op_pass.h"
  17. #include <map>
  18. #include <memory>
  19. #include <string>
  20. #include "common/ge/ge_util.h"
  21. #include "external/graph/graph.h"
  22. #include "framework/common/debug/ge_log.h"
  23. #include "common/omg_util.h"
  24. #include "graph/debug/ge_attr_define.h"
  25. #include "graph/node.h"
  26. #include "graph/utils/tensor_utils.h"
  27. namespace ge {
  28. std::map<std::string, std::map<int, std::vector<int>>> VariablePrepareOpPass::ref_node_without_prototype_map_ = {
  29. {REFSWITCH, {{0, {0, 1}}}}};
  30. Status VariablePrepareOpPass::Run(ComputeGraphPtr graph) {
  31. GE_CHECK_NOTNULL(graph);
  32. for (const auto &node : graph->GetDirectNode()) {
  33. auto iter = ref_input_output_map_.find(node->GetType());
  34. if (iter == ref_input_output_map_.end()) {
  35. GenerateRefTypeAndInputOutputMap(node);
  36. }
  37. }
  38. if (ref_input_output_map_.empty()) {
  39. GELOGI("No need to add variable_ref.");
  40. return SUCCESS;
  41. }
  42. for (auto &node : graph->GetDirectNode()) {
  43. GE_IF_BOOL_EXEC(node->GetOpDesc() == nullptr, continue);
  44. if (node->GetOpDesc()->GetType() == VARIABLE) {
  45. Status ret = DealVariableNode(node);
  46. if (ret != SUCCESS) {
  47. GELOGE(ret, "[Deal][VariableNode] failed, node:%s(%s)", node->GetName().c_str(), node->GetType().c_str());
  48. return FAILED;
  49. }
  50. }
  51. }
  52. return SUCCESS;
  53. }
  54. Status VariablePrepareOpPass::DealVariableNode(NodePtr &var_node) {
  55. GE_CHECK_NOTNULL(var_node);
  56. for (auto &dst_node_and_inanchor : var_node->GetOutDataNodesAndAnchors()) {
  57. NodePtr dst_node = dst_node_and_inanchor.first;
  58. GE_CHECK_NOTNULL(dst_node);
  59. InDataAnchorPtr dst_in_data_anchor = dst_node_and_inanchor.second;
  60. GE_CHECK_NOTNULL(dst_in_data_anchor);
  61. auto input_index = dst_in_data_anchor->GetIdx();
  62. vector<int> ref_output_indexes;
  63. GetWritableNodeOutIndex(dst_node, input_index, ref_output_indexes);
  64. if (!ref_output_indexes.empty()) {
  65. for (auto output_index : ref_output_indexes) {
  66. Status ret = DealWritableNode(dst_node, input_index, output_index, var_node);
  67. if (ret != SUCCESS) {
  68. GELOGE(FAILED, "[Deal][WritableNode] [%s] failed, input index:%d, output index:%d var:%s.",
  69. dst_node->GetName().c_str(), output_index, input_index, var_node->GetName().c_str());
  70. return FAILED;
  71. }
  72. }
  73. }
  74. }
  75. return SUCCESS;
  76. }
  77. Status VariablePrepareOpPass::DealWritableNode(const ge::NodePtr &writable_node, int input_index, int output_index,
  78. const ge::NodePtr &var_node) {
  79. // Find the last ref node:
  80. // If the ref input has corresponding output, add variable ref after it.
  81. // If the ref input has no corresponding output, insert RefIdentity and variable ref before it.
  82. // If ref node with control output was found while finding the last ref node, add variable ref after it.
  83. std::stack<pair<NodePtr, pair<int, int>>> nodes_to_check;
  84. nodes_to_check.push({writable_node, {input_index, output_index}});
  85. while (!nodes_to_check.empty()) {
  86. auto node_index = nodes_to_check.top();
  87. nodes_to_check.pop();
  88. auto cur_node = node_index.first;
  89. int cur_input_index = node_index.second.first;
  90. int cur_output_index = node_index.second.second;
  91. // Collect ref node after cur node
  92. const auto nodes_size = nodes_to_check.size();
  93. // Add peer ref output node of current node to stack
  94. CHECK_FALSE_EXEC(GetPeerNodeOfRefOutput(cur_node, cur_output_index, nodes_to_check) == SUCCESS,
  95. GELOGE(FAILED, "[Get][PeerNode] Of Ref Output for node[%s] failed, output index:%d.",
  96. cur_node->GetName().c_str(), cur_output_index);
  97. return FAILED);
  98. if (nodes_size == nodes_to_check.size()) {
  99. const auto &op_desc = cur_node->GetOpDesc();
  100. GE_CHECK_NOTNULL(op_desc);
  101. // No need to add variable_ref for framework op
  102. if (op_desc->GetType() == FRAMEWORKOP) {
  103. GELOGD("No need to add variable_ref for frameworkop");
  104. continue;
  105. }
  106. if (static_cast<uint32_t>(cur_output_index) < op_desc->GetOutputsSize()) {
  107. // Add variable ref node after ref output for final ref node
  108. CHECK_FALSE_EXEC(AddVariableRef(cur_node, var_node, cur_output_index) == SUCCESS,
  109. GELOGE(FAILED, "[Add][VariableRef] for node:%s failed, output index:%d var:%s",
  110. cur_node->GetName().c_str(), cur_output_index, var_node->GetName().c_str());
  111. return FAILED);
  112. } else {
  113. // Insert variable ref node before ref input without corresponding ref output
  114. CHECK_FALSE_EXEC(InsertVariableRef(cur_node, cur_input_index, var_node) == SUCCESS,
  115. GELOGE(FAILED, "[Insert][VariableRef] and ref identity failed, node:%s, in index:%d, var:%s",
  116. cur_node->GetName().c_str(), cur_input_index, var_node->GetName().c_str());
  117. return FAILED);
  118. }
  119. continue;
  120. }
  121. if (HasControlOut(cur_node)) {
  122. // Add variable ref node after ref output for ref node has control output.
  123. CHECK_FALSE_EXEC(AddVariableRef(cur_node, var_node, cur_output_index) == SUCCESS,
  124. GELOGE(FAILED, "[Add][VariableRef] for node:%s failed, var:%s output index:%d",
  125. cur_node->GetName().c_str(), var_node->GetName().c_str(), cur_output_index);
  126. return FAILED);
  127. }
  128. }
  129. return SUCCESS;
  130. }
  131. Status VariablePrepareOpPass::GetPeerNodeOfRefOutput(const ge::NodePtr &node, int output_index,
  132. std::stack<pair<NodePtr, pair<int, int>>> &nodes) {
  133. if (output_index < 0) {
  134. REPORT_INNER_ERROR("E19999", "Param output_index:%d < 0, check invalid", output_index);
  135. GELOGE(PARAM_INVALID, "[Check][Param] Invalid ref output index: %s-%d.", node->GetName().c_str(), output_index);
  136. return PARAM_INVALID;
  137. }
  138. const auto &op_desc = node->GetOpDesc();
  139. GE_CHECK_NOTNULL(op_desc);
  140. if (static_cast<uint32_t>(output_index) == op_desc->GetOutputsSize()) {
  141. return SUCCESS;
  142. }
  143. if (output_index >= static_cast<int>(node->GetAllOutDataAnchorsSize())) {
  144. GELOGW("Can not get %d th output anchor of %s", output_index, node->GetName().c_str());
  145. return SUCCESS;
  146. }
  147. const auto &out_anchor = node->GetOutDataAnchor(output_index);
  148. GE_CHECK_NOTNULL(out_anchor);
  149. for (const auto &peer_in_anchor : out_anchor->GetPeerInDataAnchors()) {
  150. auto peer_node = peer_in_anchor->GetOwnerNode();
  151. if (peer_node == nullptr) {
  152. continue;
  153. }
  154. const int peer_in_index = peer_in_anchor->GetIdx();
  155. vector<int> ref_output_indexes;
  156. GetWritableNodeOutIndex(peer_node, peer_in_index, ref_output_indexes);
  157. for (auto ref_output_index : ref_output_indexes) {
  158. nodes.push({peer_node, {peer_in_index, ref_output_index}});
  159. }
  160. }
  161. return SUCCESS;
  162. }
  163. Status VariablePrepareOpPass::AddVariableRef(ge::NodePtr &final_writable_node, const ge::NodePtr &var_node, int index) {
  164. GE_CHECK_NOTNULL(final_writable_node);
  165. GE_CHECK_NOTNULL(var_node);
  166. if (index >= static_cast<int>(final_writable_node->GetAllOutDataAnchorsSize())) {
  167. GELOGW("Can not get %d th output anchor of %s", index, final_writable_node->GetName().c_str());
  168. return SUCCESS;
  169. }
  170. // Check for duplicate creation
  171. OutDataAnchorPtr out_anchor = final_writable_node->GetOutDataAnchor(index);
  172. GE_CHECK_NOTNULL(out_anchor);
  173. for (const auto &peer_anchor : out_anchor->GetPeerAnchors()) {
  174. NodePtr peer_node = peer_anchor->GetOwnerNode();
  175. OpDescPtr peer_opdesc = peer_node->GetOpDesc();
  176. GE_CHECK_NOTNULL(peer_opdesc);
  177. string src_var_name;
  178. (void)ge::AttrUtils::GetStr(peer_opdesc, REF_VAR_SRC_VAR_NAME, src_var_name);
  179. if (peer_node->GetType() == VARIABLE && var_node->GetName() == src_var_name) {
  180. GELOGI("The corresponding variable_ref has been added to this connection.");
  181. return SUCCESS;
  182. }
  183. }
  184. // creat variable_ref
  185. std::stringstream variable_ref_name;
  186. variable_ref_name << "_TO_" << final_writable_node->GetName() << "_REF_" << index;
  187. NodePtr variable_ref_node = CreateVariableRef(var_node->GetName() + variable_ref_name.str(), var_node);
  188. GE_CHECK_NOTNULL(variable_ref_node);
  189. Status ret_check = CheckStreamLabel(variable_ref_node, final_writable_node);
  190. if (ret_check != SUCCESS) {
  191. GELOGE(FAILED, "[Check][StreamLabel] failed, var ref node:%s, writable node:%s",
  192. variable_ref_node->GetName().c_str(), final_writable_node->GetName().c_str());
  193. return FAILED;
  194. }
  195. GELOGI("Add variable_ref between [%s] and [%s]", var_node->GetName().c_str(), variable_ref_node->GetName().c_str());
  196. // add control anchor between variable_ref and final peer node
  197. // variable_ref_node need to execute before other nodes
  198. CHECK_FALSE_EXEC(AddControlEdge(final_writable_node, variable_ref_node) == SUCCESS,
  199. GELOGE(FAILED, "[Add][ControlEdge] between variable ref node:%s and final peer node:%s failed",
  200. var_node->GetName().c_str(), variable_ref_node->GetName().c_str());
  201. return FAILED);
  202. graphStatus ret = ge::GraphUtils::AddEdge(out_anchor, variable_ref_node->GetInDataAnchor(0));
  203. if (ret != GRAPH_SUCCESS) {
  204. REPORT_CALL_ERROR("E19999",
  205. "add edge between variable_ref:%s(index:0) and final_writable peer node:%s(index:%d) failed",
  206. variable_ref_node->GetName().c_str(), final_writable_node->GetName().c_str(), index);
  207. GELOGE(FAILED, "[Add][Edge] between variable_ref:%s(index:0) and final_writable peer node:%s(index:%d) failed",
  208. variable_ref_node->GetName().c_str(), final_writable_node->GetName().c_str(), index);
  209. return FAILED;
  210. }
  211. return SUCCESS;
  212. }
  213. Status VariablePrepareOpPass::InsertVariableRef(ge::NodePtr &node, int in_index, const ge::NodePtr &var_node) {
  214. GE_CHECK_NOTNULL(node);
  215. GE_CHECK_NOTNULL(var_node);
  216. // Check connection between two nodes
  217. const auto in_anchor = node->GetInDataAnchor(in_index);
  218. GE_CHECK_NOTNULL(in_anchor);
  219. auto peer_out_anchor = in_anchor->GetPeerOutAnchor();
  220. GE_CHECK_NOTNULL(peer_out_anchor);
  221. auto peer_in_node = peer_out_anchor->GetOwnerNode();
  222. GE_CHECK_NOTNULL(peer_in_node);
  223. // Create ref_identity
  224. std::stringstream ref_identity_name;
  225. ref_identity_name << "RefIdentity_" << peer_in_node->GetName() << "_" << peer_out_anchor->GetIdx() << "_TO_"
  226. << node->GetName() << "_" << in_index;
  227. NodePtr ref_identity_node = CreateRefIdentity(ref_identity_name.str(), node, static_cast<uint32_t>(in_index));
  228. GE_CHECK_NOTNULL(ref_identity_node);
  229. // Create variable_ref
  230. std::stringstream variable_ref_name;
  231. variable_ref_name << "_TO_" << node->GetName() << "_REF_" << in_index;
  232. NodePtr variable_ref_node = CreateVariableRef(var_node->GetName() + variable_ref_name.str(), var_node);
  233. GE_CHECK_NOTNULL(variable_ref_node);
  234. Status ret_check = CheckStreamLabel(variable_ref_node, node);
  235. if (ret_check != SUCCESS) {
  236. GELOGE(FAILED, "[Check][StreamLabel] failed, ref node:%s, writable node:%s",
  237. variable_ref_node->GetName().c_str(), node->GetName().c_str());
  238. return FAILED;
  239. }
  240. GELOGI("Insert variable_ref of [%s] between [%s] and [%s]", var_node->GetName().c_str(),
  241. peer_in_node->GetName().c_str(), node->GetName().c_str());
  242. // add control anchor between variable_ref and node
  243. // variable_ref_node need to execute before other nodes
  244. CHECK_FALSE_EXEC(AddControlEdge(node, variable_ref_node) == SUCCESS,
  245. GELOGE(FAILED, "[Add][ControlEdge] between variable ref node:%s and ref node:%s failed",
  246. variable_ref_node->GetName().c_str(), node->GetName().c_str());
  247. return FAILED);
  248. // Insert variable ref node between two nodes and remove the original edge.
  249. CHECK_FALSE_EXEC(ge::GraphUtils::RemoveEdge(peer_out_anchor, in_anchor) == SUCCESS,
  250. REPORT_CALL_ERROR("E19999", "remove edge between ref node:%s and its peer node:%s failed",
  251. node->GetName().c_str(), peer_in_node->GetName().c_str());
  252. GELOGE(FAILED, "[Remove][Edge] between ref node:%s and its peer node:%s failed",
  253. node->GetName().c_str(), peer_in_node->GetName().c_str());
  254. return FAILED);
  255. CHECK_FALSE_EXEC(ge::GraphUtils::AddEdge(peer_out_anchor, ref_identity_node->GetInDataAnchor(0)) == SUCCESS,
  256. REPORT_CALL_ERROR("E19999", "Add edge between pre node:%s and ref_identity:%s failed",
  257. peer_in_node->GetName().c_str(), ref_identity_node->GetName().c_str());
  258. GELOGE(FAILED, "[Add][Edge] between pre node:%s and ref_identity:%s failed",
  259. peer_in_node->GetName().c_str(), ref_identity_node->GetName().c_str());
  260. return FAILED);
  261. CHECK_FALSE_EXEC(ge::GraphUtils::AddEdge(ref_identity_node->GetOutDataAnchor(0), in_anchor) == SUCCESS,
  262. REPORT_CALL_ERROR("E19999", "Add edge between ref_identity:%s and ref node:%s failed",
  263. ref_identity_node->GetName().c_str(), node->GetName().c_str());
  264. GELOGE(FAILED, "[Add][Edge] between ref_identity:%s and ref node:%s failed",
  265. ref_identity_node->GetName().c_str(), node->GetName().c_str());
  266. return FAILED);
  267. // Add edge from ref identity node to variable ref node.
  268. CHECK_FALSE_EXEC(ge::GraphUtils::AddEdge(ref_identity_node->GetOutDataAnchor(0),
  269. variable_ref_node->GetInDataAnchor(0)) == SUCCESS,
  270. REPORT_CALL_ERROR("E19999", "Add edge between ref_identity:%s and variable_ref:%s failed",
  271. ref_identity_node->GetName().c_str(), variable_ref_node->GetName().c_str());
  272. GELOGE(FAILED, "[Add][Edge] between ref_identity:%s and variable_ref:%s failed",
  273. ref_identity_node->GetName().c_str(), variable_ref_node->GetName().c_str());
  274. return FAILED);
  275. CHECK_FALSE_EXEC(ge::GraphUtils::AddEdge(node->GetOutControlAnchor(),
  276. variable_ref_node->GetInControlAnchor()) == SUCCESS,
  277. REPORT_CALL_ERROR("E19999", "Add control edge between ref node:%s and variable_ref:%s failed",
  278. node->GetName().c_str(), variable_ref_node->GetName().c_str());
  279. GELOGE(FAILED, "[Add][ControlEdge] between ref node:%s and variable_ref:%s failed",
  280. node->GetName().c_str(), variable_ref_node->GetName().c_str());
  281. return FAILED);
  282. return SUCCESS;
  283. }
  284. Status VariablePrepareOpPass::AddControlEdge(const ge::NodePtr &node, const ge::NodePtr &variable_ref_node) {
  285. auto out_anchors = node->GetAllOutAnchors();
  286. for (auto &out_anchor : out_anchors) {
  287. GE_CHECK_NOTNULL(out_anchor);
  288. for (auto &peer_in_anchor : out_anchor->GetPeerAnchors()) {
  289. GE_CHECK_NOTNULL(peer_in_anchor);
  290. NodePtr peer_node = peer_in_anchor->GetOwnerNode();
  291. GE_CHECK_NOTNULL(peer_node);
  292. CHECK_FALSE_EXEC(ge::GraphUtils::AddEdge(variable_ref_node->GetOutControlAnchor(),
  293. peer_node->GetInControlAnchor()) == SUCCESS,
  294. REPORT_CALL_ERROR("E19999",
  295. "Add control edge between variable_ref:%s and ref node's peer node:%s failed",
  296. variable_ref_node->GetName().c_str(), peer_node->GetName().c_str());
  297. GELOGE(FAILED, "[Add][ControlEdge] between variable_ref:%s and ref node's peer node:%s failed",
  298. variable_ref_node->GetName().c_str(), peer_node->GetName().c_str());
  299. return FAILED);
  300. }
  301. }
  302. return SUCCESS;
  303. }
  304. ge::NodePtr VariablePrepareOpPass::CreateRefIdentity(const std::string &ref_identity_name, const ge::NodePtr &node,
  305. uint32_t input_index) {
  306. OpDescPtr op_desc = node->GetOpDesc();
  307. if (op_desc == nullptr) {
  308. REPORT_INNER_ERROR("E19999", "opdesc of param node is nullptr, check invalid");
  309. GELOGE(FAILED, "[Get][OpDesc] failed, opdesc of param node is nullptr");
  310. return nullptr;
  311. }
  312. OpDescPtr ref_identity_op_desc = MakeShared<OpDesc>(ref_identity_name.c_str(), REFIDENTITY);
  313. if (ref_identity_op_desc == nullptr) {
  314. REPORT_CALL_ERROR("E19999", "New OpDesc failed.");
  315. GELOGE(FAILED, "[New][OpDesc] failed");
  316. return nullptr;
  317. }
  318. GE_IF_BOOL_EXEC(ref_identity_op_desc->AddOutputDesc(op_desc->GetInputDesc(input_index)) != SUCCESS,
  319. GELOGW("add output desc edge failed");
  320. return nullptr);
  321. GE_IF_BOOL_EXEC(ref_identity_op_desc->AddInputDesc(op_desc->GetInputDesc(input_index)) != SUCCESS,
  322. GELOGW("add input desc edge failed");
  323. return nullptr);
  324. NodePtr ref_identity_node = node->GetOwnerComputeGraph()->AddNode(ref_identity_op_desc);
  325. GE_IF_BOOL_EXEC(ref_identity_node == nullptr, GELOGW("ref_identity_node is null"); return nullptr);
  326. return ref_identity_node;
  327. }
  328. ge::NodePtr VariablePrepareOpPass::CreateVariableRef(const std::string &variable_ref_name,
  329. const ge::NodePtr &var_node) {
  330. OpDescPtr var_op_desc = var_node->GetOpDesc();
  331. if (var_op_desc == nullptr) {
  332. REPORT_INNER_ERROR("E19999", "param var_node's opdesc is nullptr, check invalid");
  333. GELOGE(FAILED, "[Get][OpDesc] failed, var opdesc is nullptr");
  334. return nullptr;
  335. }
  336. OpDescPtr var_ref_op_desc = MakeShared<OpDesc>(variable_ref_name.c_str(), var_op_desc->GetType());
  337. if (var_ref_op_desc == nullptr) {
  338. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  339. GELOGE(FAILED, "New OpDesc failed");
  340. return nullptr;
  341. }
  342. GE_IF_BOOL_EXEC(var_ref_op_desc->AddOutputDesc(var_op_desc->GetOutputDesc(0)) != SUCCESS,
  343. GELOGW("add output desc edge failed");
  344. return nullptr);
  345. GE_IF_BOOL_EXEC(var_ref_op_desc->AddInputDesc(var_op_desc->GetOutputDesc(0)) != SUCCESS,
  346. GELOGW("add input desc edge failed");
  347. return nullptr);
  348. NodePtr variable_ref_node = var_node->GetOwnerComputeGraph()->AddNode(var_ref_op_desc);
  349. GE_IF_BOOL_EXEC(variable_ref_node == nullptr, GELOGW("variable_ref_node is null"); return nullptr);
  350. bool is_set_str = ge::AttrUtils::SetStr(var_ref_op_desc, REF_VAR_SRC_VAR_NAME, var_op_desc->GetName());
  351. if (is_set_str) {
  352. GELOGD("Set node [%s] REF_VAR_SRC_VAR_NAME [%s]", variable_ref_node->GetName().c_str(),
  353. var_op_desc->GetName().c_str());
  354. }
  355. return variable_ref_node;
  356. }
  357. void VariablePrepareOpPass::GetWritableNodeOutIndex(const NodePtr &node, int input_index,
  358. std::vector<int> &output_indexes) {
  359. if (node == nullptr) {
  360. return;
  361. }
  362. GELOGD("get writable node and input index %s:%d", node->GetName().c_str(), input_index);
  363. auto node_type = node->GetType();
  364. if (node_type == FRAMEWORKOP) {
  365. std::string original_type;
  366. GE_IF_BOOL_EXEC(GetOriginalType(node, original_type) != SUCCESS, GELOGW("Get node original type fail"));
  367. GELOGD("find frameworkop: [%s], original type is %s", node->GetName().c_str(), original_type.c_str());
  368. FindRefOutIndex(original_type, input_index, ref_node_without_prototype_map_, output_indexes);
  369. return;
  370. }
  371. FindRefOutIndex(node_type, input_index, ref_input_output_map_, output_indexes);
  372. return;
  373. }
  374. void VariablePrepareOpPass::GenerateRefTypeAndInputOutputMap(const NodePtr &node) {
  375. auto op_desc = node->GetOpDesc();
  376. if (op_desc == nullptr) {
  377. GELOGW("op_desc in null, please check node:[%s]", node->GetName().c_str());
  378. return;
  379. }
  380. for (const auto &name_index : op_desc->GetAllInputName()) {
  381. // Record the index of output with the same name as input, thinking of them as a pair of ref input and output.
  382. const int out_index = op_desc->GetOutputIndexByName(name_index.first);
  383. if (out_index != -1) {
  384. ref_input_output_map_[node->GetType()][name_index.second] = {out_index};
  385. continue;
  386. }
  387. // Record the ref input without corresponding output.
  388. const auto &input_desc = op_desc->GetInputDesc(name_index.second);
  389. if (!input_desc.GetRefPortIndex().empty()) {
  390. ref_input_output_map_[node->GetType()][name_index.second] = {static_cast<int>(op_desc->GetOutputsSize())};
  391. }
  392. }
  393. }
  394. void VariablePrepareOpPass::FindRefOutIndex(const std::string &node_type, int input_index,
  395. const std::map<std::string, std::map<int, vector<int>>> &ref_map,
  396. std::vector<int> &output_indexes) {
  397. auto node_iter = ref_map.find(node_type);
  398. if (node_iter == ref_map.end()) {
  399. return;
  400. }
  401. auto index_iter = node_iter->second.find(input_index);
  402. if (index_iter == node_iter->second.end()) {
  403. return;
  404. }
  405. for (const auto &out_index : index_iter->second) {
  406. output_indexes.emplace_back(out_index);
  407. }
  408. }
  409. Status VariablePrepareOpPass::CheckStreamLabel(const ge::NodePtr &var_ref_node,
  410. const ge::NodePtr &final_writable_node) {
  411. // Solve the problem that the writable node is not in the same stream as the subsequent node.
  412. // Causes the stream to not trigger properly.
  413. // The label of node should be handled uniformly.
  414. OpDescPtr writable_desc = final_writable_node->GetOpDesc();
  415. GE_CHECK_NOTNULL(writable_desc);
  416. std::string stream_label;
  417. (void)AttrUtils::GetStr(writable_desc, ATTR_NAME_STREAM_LABEL, stream_label);
  418. if (!stream_label.empty()) {
  419. GE_CHK_STATUS_RET(SetStreamLabel(var_ref_node, stream_label),
  420. "[Set][StreamLabel] %s failed", stream_label.c_str());
  421. }
  422. return SUCCESS;
  423. }
  424. bool VariablePrepareOpPass::HasControlOut(const ge::NodePtr &node) {
  425. const auto &out_control_anchor = node->GetOutControlAnchor();
  426. for (const auto &peer_in_control_anchor : out_control_anchor->GetPeerInControlAnchors()) {
  427. if (peer_in_control_anchor == nullptr || peer_in_control_anchor->GetOwnerNode() == nullptr) {
  428. continue;
  429. }
  430. return true;
  431. }
  432. return false;
  433. }
  434. } // namespace ge

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