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_ref_delete_op_pass.cc 4.9 kB

5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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_ref_delete_op_pass.h"
  17. #include <string>
  18. namespace ge {
  19. Status VariableRefDeleteOpPass::Run(ge::ComputeGraphPtr graph) {
  20. GE_CHECK_NOTNULL(graph);
  21. std::set<std::string> all_var_names;
  22. auto root_graph = GraphUtils::FindRootGraph(graph);
  23. GE_CHECK_NOTNULL(root_graph);
  24. for (const auto &n : root_graph->GetAllNodes()) {
  25. all_var_names.insert(n->GetName());
  26. }
  27. for (auto &node : graph->GetDirectNode()) {
  28. GE_CHECK_NOTNULL(node->GetOpDesc());
  29. std::string ref_var_src_var_name;
  30. bool is_variable_ref = (node->GetOpDesc()->GetType() == VARIABLE) &&
  31. (ge::AttrUtils::GetStr(node->GetOpDesc(), REF_VAR_SRC_VAR_NAME, ref_var_src_var_name));
  32. if (!is_variable_ref) {
  33. continue;
  34. }
  35. if (all_var_names.count(ref_var_src_var_name) == 0) {
  36. REPORT_INNER_ERROR("E19999", "Can not find source variable[%s] of variable ref[%s], check invalid",
  37. ref_var_src_var_name.c_str(), node->GetName().c_str());
  38. GELOGE(FAILED, "[Check][Param] Can not find source variable[%s] of variable ref[%s]",
  39. ref_var_src_var_name.c_str(), node->GetName().c_str());
  40. return FAILED;
  41. }
  42. Status ret = DealVariableRef(graph, node, ref_var_src_var_name);
  43. if (ret != SUCCESS) {
  44. GELOGE(ret, "[Deal][VariableRef] [%s] in graph:%s failed", node->GetName().c_str(), graph->GetName().c_str());
  45. return FAILED;
  46. }
  47. }
  48. return SUCCESS;
  49. }
  50. Status VariableRefDeleteOpPass::DealVariableRef(ge::ComputeGraphPtr &graph, ge::NodePtr &variable_ref,
  51. const std::string &ref_var_src_var_name) {
  52. GE_CHECK_NOTNULL(variable_ref);
  53. auto inAnchor0 = variable_ref->GetInDataAnchor(0);
  54. if (inAnchor0 == nullptr) {
  55. REPORT_INNER_ERROR("E19999", "Node:%s(%s) has no input anchor, check invalid",
  56. variable_ref->GetName().c_str(), variable_ref->GetType().c_str());
  57. GELOGE(FAILED, "[Get][InDataAnchor] failed, variable_ref [%s] no input", variable_ref->GetName().c_str());
  58. return FAILED;
  59. }
  60. GE_CHECK_NOTNULL(inAnchor0->GetPeerOutAnchor());
  61. // get the output index of the previous node connected to the variable_ref
  62. // prepare for refreshing address in build phase
  63. int index = inAnchor0->GetPeerOutAnchor()->GetIdx();
  64. // get previous node of variable_ref
  65. NodePtr peer_node = inAnchor0->GetPeerOutAnchor()->GetOwnerNode();
  66. // add attr [REF_VAR_SRC_VAR_NAME] to the previous op output desc of the variable_ref
  67. auto op_desc = peer_node->GetOpDesc();
  68. GE_CHECK_NOTNULL(op_desc);
  69. auto out_desc = op_desc->MutableOutputDesc(static_cast<uint32_t>(index));
  70. bool is_set_str = ge::AttrUtils::SetStr(out_desc, REF_VAR_SRC_VAR_NAME, ref_var_src_var_name);
  71. if (is_set_str) {
  72. GELOGI("[%s-%d]: add attr [REF_VAR_SRC_VAR_NAME: %s ] ", peer_node->GetName().c_str(), index,
  73. ref_var_src_var_name.c_str());
  74. } else {
  75. REPORT_CALL_ERROR("E19999", "Set Attr:%s to output:%d desc of op:%s(%s) failed", REF_VAR_SRC_VAR_NAME.c_str(),
  76. index, op_desc->GetName().c_str(), op_desc->GetType().c_str());
  77. GELOGE(FAILED, "[Set][Attr] %s to output:%d desc of op:%s(%s) failed", REF_VAR_SRC_VAR_NAME.c_str(),
  78. index, op_desc->GetName().c_str(), op_desc->GetType().c_str());
  79. return FAILED;
  80. }
  81. // remove variable_ref
  82. if (GraphUtils::IsolateNode(variable_ref, {0}) != GRAPH_SUCCESS) {
  83. REPORT_CALL_ERROR("E19999", "Isolate node:%s(%s) failed",
  84. variable_ref->GetName().c_str(), variable_ref->GetType().c_str());
  85. GELOGE(INTERNAL_ERROR, "[Isolate][Node] name:%s, type:%s failed", variable_ref->GetName().c_str(),
  86. variable_ref->GetType().c_str());
  87. return FAILED;
  88. }
  89. if (GraphUtils::RemoveNodeWithoutRelink(graph, variable_ref) != GRAPH_SUCCESS) {
  90. REPORT_CALL_ERROR("E19999", "Remove node:%s(%s) without relink in graph:%s failed",
  91. variable_ref->GetName().c_str(), variable_ref->GetType().c_str(), graph->GetName().c_str());
  92. GELOGE(INTERNAL_ERROR, "[Remove][Node] %s(%s) without relink in graph:%s failed",
  93. variable_ref->GetName().c_str(), variable_ref->GetType().c_str(), graph->GetName().c_str());
  94. return FAILED;
  95. }
  96. return SUCCESS;
  97. }
  98. } // namespace ge

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