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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. GELOGE(FAILED, "Can not find source variable[%s] of variable ref[%s]", ref_var_src_var_name.c_str(),
  37. node->GetName().c_str());
  38. return FAILED;
  39. }
  40. Status ret = DealVariableRef(graph, node, ref_var_src_var_name);
  41. if (ret != SUCCESS) {
  42. GELOGE(ret, "variable ref [%s] delete failed", node->GetName().c_str());
  43. return FAILED;
  44. }
  45. }
  46. return SUCCESS;
  47. }
  48. Status VariableRefDeleteOpPass::DealVariableRef(ge::ComputeGraphPtr &graph, ge::NodePtr &variable_ref,
  49. const std::string &ref_var_src_var_name) {
  50. GE_CHECK_NOTNULL(variable_ref);
  51. auto inAnchor0 = variable_ref->GetInDataAnchor(0);
  52. if (inAnchor0 == nullptr) {
  53. GELOGE(FAILED, "variable_ref [%s] no input", variable_ref->GetName().c_str());
  54. return FAILED;
  55. }
  56. GE_CHECK_NOTNULL(inAnchor0->GetPeerOutAnchor());
  57. // get the output index of the previous node connected to the variable_ref
  58. // prepare for refreshing address in build phase
  59. int index = inAnchor0->GetPeerOutAnchor()->GetIdx();
  60. // get previous node of variable_ref
  61. NodePtr peer_node = inAnchor0->GetPeerOutAnchor()->GetOwnerNode();
  62. // add attr [REF_VAR_SRC_VAR_NAME] to the previous op output desc of the variable_ref
  63. auto op_desc = peer_node->GetOpDesc();
  64. GE_CHECK_NOTNULL(op_desc);
  65. auto out_desc = op_desc->MutableOutputDesc(static_cast<uint32_t>(index));
  66. bool is_set_str = ge::AttrUtils::SetStr(out_desc, REF_VAR_SRC_VAR_NAME, ref_var_src_var_name);
  67. if (is_set_str) {
  68. GELOGI("[%s-%d]: add attr [REF_VAR_SRC_VAR_NAME: %s ] ", peer_node->GetName().c_str(), index,
  69. ref_var_src_var_name.c_str());
  70. } else {
  71. GELOGE(FAILED, "[%s-%d]: add attr [REF_VAR_SRC_VAR_NAME: %s ] failed", peer_node->GetName().c_str(), index,
  72. ref_var_src_var_name.c_str());
  73. return FAILED;
  74. }
  75. // remove variable_ref
  76. if (GraphUtils::IsolateNode(variable_ref, {0}) != GRAPH_SUCCESS) {
  77. GELOGE(INTERNAL_ERROR, "Isolate removed node: %s, type: %s failed", variable_ref->GetName().c_str(),
  78. variable_ref->GetType().c_str());
  79. return FAILED;
  80. }
  81. if (GraphUtils::RemoveNodeWithoutRelink(graph, variable_ref) != GRAPH_SUCCESS) {
  82. GELOGE(INTERNAL_ERROR, "Remove node: %s, type: %s without relink failed", variable_ref->GetName().c_str(),
  83. variable_ref->GetType().c_str());
  84. return FAILED;
  85. }
  86. return SUCCESS;
  87. }
  88. } // namespace ge

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