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.

while_label_maker.cc 7.5 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/label/while_label_maker.h"
  17. #include "framework/common/util.h"
  18. #include "framework/common/ge_inner_error_codes.h"
  19. #include "framework/common/types.h"
  20. #include "framework/common/op/ge_op_utils.h"
  21. #include "graph/debug/ge_attr_define.h"
  22. #include "graph/utils/graph_utils.h"
  23. namespace ge {
  24. constexpr uint8_t kCondOutputNum = 1;
  25. constexpr uint8_t kCondOutputIndex = 0;
  26. constexpr uint8_t kCondBranchIndex = 0;
  27. constexpr uint8_t kBodyBranchIndex = 1;
  28. /**
  29. * @ingroup ge
  30. * @brief Make label node to functional call.
  31. * @param [in/out] label_index: serial id for whole graph.
  32. * @return: 0 for success / others for fail
  33. */
  34. Status WhileOpLabelMaker::Run(uint32_t &label_index) {
  35. GE_CHECK_NOTNULL(parent_node_);
  36. GE_CHECK_NOTNULL(parent_graph_);
  37. OpDescPtr while_desc = parent_node_->GetOpDesc();
  38. GE_CHECK_NOTNULL(while_desc);
  39. std::string cond_name = while_desc->GetSubgraphInstanceName(kCondBranchIndex);
  40. std::string body_name = while_desc->GetSubgraphInstanceName(kBodyBranchIndex);
  41. if (cond_name.empty() || body_name.empty()) {
  42. REPORT_INNER_ERROR("E19999", "Node:%s(%s) cond subgraph index:%d or body subgraph index:%d name is empty, "
  43. "check invalid", while_desc->GetName().c_str(), while_desc->GetType().c_str(),
  44. kCondBranchIndex, kBodyBranchIndex);
  45. GELOGE(INTERNAL_ERROR, "[Check][Param] Node: %s has invalid subgraph, cond branch: %s, body branch: %s.",
  46. while_desc->GetName().c_str(), cond_name.c_str(), body_name.c_str());
  47. return FAILED;
  48. }
  49. ComputeGraphPtr cond_graph = parent_graph_->GetSubgraph(cond_name);
  50. ComputeGraphPtr body_graph = parent_graph_->GetSubgraph(body_name);
  51. GE_CHECK_NOTNULL(cond_graph);
  52. GE_CHECK_NOTNULL(body_graph);
  53. const uint32_t cond_enter_index = label_index++;
  54. const uint32_t body_enter_index = label_index++;
  55. const uint32_t body_leave_index = label_index++;
  56. const std::string cond_enter_name = parent_node_->GetName() + "/CondLabelSet"; // rtLabelSet
  57. const std::string cond_active_name = parent_node_->GetName() + "/CondStreamActive"; // rtStreamActive
  58. const std::string cond_leave_name = parent_node_->GetName() + "/LabelSwitch"; // rtLabelSwitchByIndex
  59. const std::string body_enter_name = parent_node_->GetName() + "/EnterLabelSet"; // rtLabelSet
  60. const std::string body_active_name = parent_node_->GetName() + "/EnterStreamActive"; // rtStreamActive
  61. const std::string goto_leave_name = parent_node_->GetName() + "/LabelGoto"; // rtLabelGoto
  62. const std::string body_leave_name = parent_node_->GetName() + "/LeaveLabelSet"; // rtLabelSet
  63. NodePtr cond_stream_active = AddStreamActive(cond_graph, cond_active_name);
  64. if (cond_stream_active == nullptr) {
  65. REPORT_CALL_ERROR("E19999", "Add StreamActive node in graph:%s fail",
  66. cond_graph->GetName().c_str());
  67. GELOGE(INTERNAL_ERROR, "[Add][StreamActive] in Subgraph:%s failed.", cond_graph->GetName().c_str());
  68. return FAILED;
  69. }
  70. if (AddLabelSetEnter(cond_graph, cond_enter_name, cond_enter_index, cond_stream_active) == nullptr) {
  71. REPORT_CALL_ERROR("E19999", "Add LabelSetEnter node in graph:%s fail",
  72. cond_graph->GetName().c_str());
  73. GELOGE(INTERNAL_ERROR, "[Add][LabelSetEnter] in Subgraph:%s failed.", cond_graph->GetName().c_str());
  74. return FAILED;
  75. }
  76. NodePtr body_stream_active = AddStreamActive(body_graph, body_active_name);
  77. if (body_stream_active == nullptr) {
  78. REPORT_CALL_ERROR("E19999", "Add StreamActive node in graph:%s fail",
  79. body_graph->GetName().c_str());
  80. GELOGE(INTERNAL_ERROR, "[Add][StreamActive] in Subgraph:%s failed.", body_graph->GetName().c_str());
  81. return FAILED;
  82. }
  83. if (AddLabelSetEnter(body_graph, body_enter_name, body_enter_index, body_stream_active) == nullptr) {
  84. REPORT_CALL_ERROR("E19999", "Add LabelSetEnter node in graph:%s fail",
  85. body_graph->GetName().c_str());
  86. GELOGE(INTERNAL_ERROR, "[Add][LabelSetEnter] in Subgraph:%s failed.", body_graph->GetName().c_str());
  87. return FAILED;
  88. }
  89. if (AddLabelGotoLeave(body_graph, goto_leave_name, cond_enter_index) == nullptr) {
  90. REPORT_CALL_ERROR("E19999", "Add LabelGotoLeave node in graph:%s fail",
  91. body_graph->GetName().c_str());
  92. GELOGE(INTERNAL_ERROR, "[Add][LabelGotoLeave] in Subgraph:%s failed.", body_graph->GetName().c_str());
  93. return FAILED;
  94. }
  95. if (AddLabelSetLeave(body_graph, body_leave_name, body_leave_index) == nullptr) {
  96. REPORT_CALL_ERROR("E19999", "Add LabelSetLeave node in graph:%s fail",
  97. body_graph->GetName().c_str());
  98. GELOGE(INTERNAL_ERROR, "[Add][LabelSetLeave] in Subgraph:%s failed.", body_graph->GetName().c_str());
  99. return FAILED;
  100. }
  101. NodePtr cond_out_node = cond_graph->FindFirstNodeMatchType(NETOUTPUT);
  102. GE_CHECK_NOTNULL(cond_out_node);
  103. OpDescPtr cond_out_desc = cond_out_node->GetOpDesc();
  104. GE_CHECK_NOTNULL(cond_out_desc);
  105. GeTensorDesc pred_desc = cond_out_desc->GetInputDesc(kCondOutputIndex);
  106. // false ==> 0 ==> switch_labels[0] ==> body_leave_index
  107. // true ==> 1 ==> switch_labels[1] ==> body_enter_name
  108. const std::vector<uint32_t> switch_labels = {body_leave_index, body_enter_index};
  109. NodePtr switch_node = AddLabelSwitchLeave(cond_graph, cond_leave_name, pred_desc, switch_labels);
  110. if (switch_node == nullptr) {
  111. REPORT_CALL_ERROR("E19999", "Add LabelSwitchLeave node in graph:%s fail",
  112. cond_graph->GetName().c_str());
  113. GELOGE(INTERNAL_ERROR, "[Add][LabelSwitchLeave] in Subgraph:%s failed.", cond_graph->GetName().c_str());
  114. return FAILED;
  115. }
  116. // link Data input.
  117. const auto &all_in_data = cond_out_node->GetAllInDataAnchors();
  118. if (all_in_data.size() != kCondOutputNum) {
  119. GELOGE(FAILED, "[Check][Param] Node: %s Cond sbugraph output size:%zu should equal size:%u.",
  120. switch_node->GetName().c_str(), all_in_data.size(), kCondOutputNum);
  121. return FAILED;
  122. }
  123. InDataAnchorPtr in_anchor = all_in_data.at(kCondOutputIndex);
  124. GE_CHECK_NOTNULL(in_anchor);
  125. if (GraphUtils::AddEdge(in_anchor->GetPeerOutAnchor(), switch_node->GetInDataAnchor(kCondOutputIndex)) != SUCCESS) {
  126. REPORT_CALL_ERROR("E19999", "Add ctrl edge from %s to %s in graph:%s fail",
  127. in_anchor->GetPeerOutAnchor()->GetOwnerNode()->GetName().c_str(),
  128. switch_node->GetName().c_str(), cond_graph->GetName().c_str());
  129. GELOGE(FAILED, "[Add][PredDataInput] to Node:%s failed.", switch_node->GetName().c_str());
  130. return FAILED;
  131. }
  132. GELOGI("Node: %s assign label success.", while_desc->GetName().c_str());
  133. return SUCCESS;
  134. }
  135. REGISTER_LABEL_MAKER(WHILE, WhileOpLabelMaker);
  136. REGISTER_LABEL_MAKER(_WHILE, WhileOpLabelMaker);
  137. REGISTER_LABEL_MAKER(STATELESSWHILE, WhileOpLabelMaker);
  138. } // namespace ge

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