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.

if_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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/if_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 kIfPredIndex = 0;
  25. constexpr uint8_t kThenBranchIndex = 0;
  26. constexpr uint8_t kElseBranchIndex = 1;
  27. /**
  28. * @ingroup ge
  29. * @brief Make label node to functional call.
  30. * @param [in/out] label_index: serial id for whole graph.
  31. * @return: 0 for success / others for fail
  32. */
  33. Status IfOpLabelMaker::Run(uint32_t &label_index) {
  34. GE_CHECK_NOTNULL(parent_node_);
  35. GE_CHECK_NOTNULL(parent_graph_);
  36. OpDescPtr if_desc = parent_node_->GetOpDesc();
  37. GE_CHECK_NOTNULL(if_desc);
  38. const std::string then_branch_name = if_desc->GetSubgraphInstanceName(kThenBranchIndex);
  39. const std::string else_branch_name = if_desc->GetSubgraphInstanceName(kElseBranchIndex);
  40. if (then_branch_name.empty() || else_branch_name.empty()) {
  41. REPORT_INNER_ERROR("E19999", "Node:%s(%s), check subgraph invalid, "
  42. "then branch graph: %s, else branch graph: %s",
  43. if_desc->GetName().c_str(), if_desc->GetType().c_str(),
  44. then_branch_name.c_str(), else_branch_name.c_str());
  45. GELOGE(INTERNAL_ERROR, "[Check][Param] Node: %s has invalid subgraph, then branch: %s, else branch: %s.",
  46. if_desc->GetName().c_str(), then_branch_name.c_str(), else_branch_name.c_str());
  47. return FAILED;
  48. }
  49. ComputeGraphPtr then_sub_graph = parent_graph_->GetSubgraph(then_branch_name);
  50. ComputeGraphPtr else_sub_graph = parent_graph_->GetSubgraph(else_branch_name);
  51. GE_CHECK_NOTNULL(then_sub_graph);
  52. GE_CHECK_NOTNULL(else_sub_graph);
  53. const uint32_t then_enter_index = label_index++;
  54. const uint32_t else_enter_index = label_index++;
  55. const uint32_t else_leave_index = label_index++;
  56. const std::string then_enter_name = parent_node_->GetName() + "/LabelSwitch"; // rtLabelSwitchByIndex
  57. const std::string then_label_name = parent_node_->GetName() + "/ThenLabelSet"; // rtLabelSet(0)
  58. const std::string then_active_name = parent_node_->GetName() + "/ThenStreamActive"; // rtStreamActive
  59. const std::string then_leave_name = parent_node_->GetName() + "/LabelGoto"; // rtLabelGoto
  60. const std::string else_enter_name = parent_node_->GetName() + "/ElseLabelSet"; // rtLabelSet(1)
  61. const std::string else_active_name = parent_node_->GetName() + "/ElseStreamActive"; // rtStreamActive
  62. const std::string else_leave_name = parent_node_->GetName() + "/LeaveLabelSet"; // rtLabelSet
  63. NodePtr then_stream_active = AddStreamActive(then_sub_graph, then_active_name);
  64. if (then_stream_active == nullptr) {
  65. REPORT_CALL_ERROR("E19999", "Add StreamActive node in graph:%s fail",
  66. then_sub_graph->GetName().c_str());
  67. GELOGE(INTERNAL_ERROR, "[Add][StreamActive] in Subgraph:%s failed.", then_sub_graph->GetName().c_str());
  68. return FAILED;
  69. }
  70. NodePtr then_enter_label = AddLabelSetEnter(then_sub_graph, then_label_name, then_enter_index, then_stream_active);
  71. if (then_enter_label == nullptr) {
  72. REPORT_CALL_ERROR("E19999", "Add LabelSetEnter node in graph:%s fail",
  73. then_sub_graph->GetName().c_str());
  74. GELOGE(INTERNAL_ERROR, "[Add][LabelSetEnter] in Subgraph:%s failed.", then_sub_graph->GetName().c_str());
  75. return FAILED;
  76. }
  77. if (AddLabelGotoLeave(then_sub_graph, then_leave_name, else_leave_index) == nullptr) {
  78. REPORT_CALL_ERROR("E19999", "Add LabelGotoLeave node in graph:%s fail",
  79. then_sub_graph->GetName().c_str());
  80. GELOGE(INTERNAL_ERROR, "[Add][LabelGotoLeave] in Subgraph:%s failed.", then_sub_graph->GetName().c_str());
  81. return FAILED;
  82. }
  83. NodePtr else_stream_active = AddStreamActive(else_sub_graph, else_active_name);
  84. if (else_stream_active == nullptr) {
  85. REPORT_CALL_ERROR("E19999", "Add StreamActive node in graph:%s fail",
  86. else_stream_active->GetName().c_str());
  87. GELOGE(INTERNAL_ERROR, "[Add][StreamActive] in Subgraph:%s failed.", else_sub_graph->GetName().c_str());
  88. return FAILED;
  89. }
  90. if (AddLabelSetEnter(else_sub_graph, else_enter_name, else_enter_index, else_stream_active) == nullptr) {
  91. REPORT_CALL_ERROR("E19999", "Add LabelSetEnter node in graph:%s fail",
  92. else_sub_graph->GetName().c_str());
  93. GELOGE(INTERNAL_ERROR, "[Add][LabelSetEnter] in Subgraph:%s failed.", else_sub_graph->GetName().c_str());
  94. return FAILED;
  95. }
  96. if (AddLabelSetLeave(else_sub_graph, else_leave_name, else_leave_index) == nullptr) {
  97. REPORT_CALL_ERROR("E19999", "Add LabelSetLeave node in graph:%s fail",
  98. else_sub_graph->GetName().c_str());
  99. GELOGE(INTERNAL_ERROR, "[Add][LabelSetLeave] in Subgraph:%s failed.", else_sub_graph->GetName().c_str());
  100. return FAILED;
  101. }
  102. // false ==> 0 ==> switch_labels[0] ==> else_enter_index
  103. // true ==> 1 ==> switch_labels[1] ==> then_enter_index
  104. const std::vector<uint32_t> switch_labels = {else_enter_index, then_enter_index};
  105. const GeTensorDesc &pred_desc = if_desc->GetInputDesc(kIfPredIndex);
  106. NodePtr switch_node = AddLabelSwitchEnter(then_sub_graph, then_enter_name, pred_desc, switch_labels);
  107. if (switch_node == nullptr) {
  108. REPORT_CALL_ERROR("E19999", "Add LabelSwitchEnter node in graph:%s fail",
  109. then_sub_graph->GetName().c_str());
  110. GELOGE(INTERNAL_ERROR, "[Add][LabelSwitchEnter] in Subgraph:%s failed.", then_sub_graph->GetName().c_str());
  111. return FAILED;
  112. }
  113. // Link control edge to then branch head.
  114. if (GraphUtils::AddEdge(switch_node->GetOutControlAnchor(), then_enter_label->GetInControlAnchor()) != SUCCESS) {
  115. REPORT_CALL_ERROR("E19999", "Add ctrl edge from %s to %s in graph:%s fail", switch_node->GetName().c_str(),
  116. then_enter_label->GetName().c_str(), then_sub_graph->GetName().c_str());
  117. GELOGE(INTERNAL_ERROR, "[Add][CtrlEdge] to %s failed.", then_enter_label->GetName().c_str());
  118. return FAILED;
  119. }
  120. uint32_t parent_index = 0; // If cond input is first.
  121. const std::string data_name = parent_node_->GetName() + "/SwitchIndexData";
  122. if (AddLabelSwitchIndex(then_sub_graph, data_name, pred_desc, switch_node, parent_index) == nullptr) {
  123. REPORT_CALL_ERROR("E19999", "Add LabelSwitchIndex node in graph:%s fail",
  124. then_sub_graph->GetName().c_str());
  125. GELOGE(INTERNAL_ERROR, "[Add][LabelSwitchIndex] in Subgraph:%s failed.", then_sub_graph->GetName().c_str());
  126. return FAILED;
  127. }
  128. GELOGI("Node: %s assign label success.", if_desc->GetName().c_str());
  129. return SUCCESS;
  130. }
  131. REGISTER_LABEL_MAKER(IF, IfOpLabelMaker);
  132. REGISTER_LABEL_MAKER(_IF, IfOpLabelMaker);
  133. REGISTER_LABEL_MAKER(STATELESSIF, IfOpLabelMaker);
  134. } // namespace ge

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