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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 "if_label_maker.h"
  17. #include "common/util.h"
  18. #include "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. GELOGE(INTERNAL_ERROR, "Node: %s has invalid subgraph, then branch: %s, else branch: %s.",
  42. if_desc->GetName().c_str(), then_branch_name.c_str(), else_branch_name.c_str());
  43. return FAILED;
  44. }
  45. ComputeGraphPtr then_sub_graph = parent_graph_->GetSubgraph(then_branch_name);
  46. ComputeGraphPtr else_sub_graph = parent_graph_->GetSubgraph(else_branch_name);
  47. GE_CHECK_NOTNULL(then_sub_graph);
  48. GE_CHECK_NOTNULL(else_sub_graph);
  49. const uint32_t then_enter_index = label_index++;
  50. const uint32_t else_enter_index = label_index++;
  51. const uint32_t else_leave_index = label_index++;
  52. const std::string then_enter_name = parent_node_->GetName() + "/LabelSwitch"; // rtLabelSwitchByIndex
  53. const std::string then_label_name = parent_node_->GetName() + "/ThenLabelSet"; // rtLabelSet(0)
  54. const std::string then_active_name = parent_node_->GetName() + "/ThenStreamActive"; // rtStreamActive
  55. const std::string then_leave_name = parent_node_->GetName() + "/LabelGoto"; // rtLabelGoto
  56. const std::string else_enter_name = parent_node_->GetName() + "/ElseLabelSet"; // rtLabelSet(1)
  57. const std::string else_active_name = parent_node_->GetName() + "/ElseStreamActive"; // rtStreamActive
  58. const std::string else_leave_name = parent_node_->GetName() + "/LeaveLabelSet"; // rtLabelSet
  59. NodePtr then_stream_active = AddStreamActive(then_sub_graph, then_active_name);
  60. if (then_stream_active == nullptr) {
  61. GELOGE(INTERNAL_ERROR, "Subgraph: %s add stream active failed.", then_sub_graph->GetName().c_str());
  62. return FAILED;
  63. }
  64. NodePtr then_enter_label = AddLabelSetEnter(then_sub_graph, then_label_name, then_enter_index, then_stream_active);
  65. if (then_enter_label == nullptr) {
  66. GELOGE(INTERNAL_ERROR, "Subgraph: %s add label set failed.", then_sub_graph->GetName().c_str());
  67. return FAILED;
  68. }
  69. if (AddLabelGotoLeave(then_sub_graph, then_leave_name, else_leave_index) == nullptr) {
  70. GELOGE(INTERNAL_ERROR, "Subgraph: %s add label goto failed.", then_sub_graph->GetName().c_str());
  71. return FAILED;
  72. }
  73. NodePtr else_stream_active = AddStreamActive(else_sub_graph, else_active_name);
  74. if (else_stream_active == nullptr) {
  75. GELOGE(INTERNAL_ERROR, "Subgraph: %s add stream active failed.", else_sub_graph->GetName().c_str());
  76. return FAILED;
  77. }
  78. if (AddLabelSetEnter(else_sub_graph, else_enter_name, else_enter_index, else_stream_active) == nullptr) {
  79. GELOGE(INTERNAL_ERROR, "Subgraph: %s add label set failed.", else_sub_graph->GetName().c_str());
  80. return FAILED;
  81. }
  82. if (AddLabelSetLeave(else_sub_graph, else_leave_name, else_leave_index) == nullptr) {
  83. GELOGE(INTERNAL_ERROR, "Subgraph: %s add label set failed.", else_sub_graph->GetName().c_str());
  84. return FAILED;
  85. }
  86. // false ==> 0 ==> switch_labels[0] ==> else_enter_index
  87. // true ==> 1 ==> switch_labels[1] ==> then_enter_index
  88. const std::vector<uint32_t> switch_labels = {else_enter_index, then_enter_index};
  89. const GeTensorDesc &pred_desc = if_desc->GetInputDesc(kIfPredIndex);
  90. NodePtr switch_node = AddLabelSwitchEnter(then_sub_graph, then_enter_name, pred_desc, switch_labels);
  91. if (switch_node == nullptr) {
  92. GELOGE(INTERNAL_ERROR, "Subgraph: %s add label switch failed.", then_sub_graph->GetName().c_str());
  93. return FAILED;
  94. }
  95. // Link control edge to then branch head.
  96. if (GraphUtils::AddEdge(switch_node->GetOutControlAnchor(), then_enter_label->GetInControlAnchor()) != SUCCESS) {
  97. GELOGE(INTERNAL_ERROR, "LabelSwitchByIndex: Add ctrl edge to %s failed.", then_enter_label->GetName().c_str());
  98. return FAILED;
  99. }
  100. uint32_t parent_index = 0; // If cond input is first.
  101. const std::string data_name = parent_node_->GetName() + "/SwitchIndexData";
  102. if (AddLabelSwitchIndex(then_sub_graph, data_name, pred_desc, switch_node, parent_index) == nullptr) {
  103. GELOGE(INTERNAL_ERROR, "Subgraph: %s add switch input failed.", then_sub_graph->GetName().c_str());
  104. return FAILED;
  105. }
  106. GELOGI("Node: %s assign label success.", if_desc->GetName().c_str());
  107. return SUCCESS;
  108. }
  109. REGISTER_LABEL_MAKER(IF, IfOpLabelMaker);
  110. REGISTER_LABEL_MAKER(_IF, IfOpLabelMaker);
  111. REGISTER_LABEL_MAKER(STATELESSIF, IfOpLabelMaker);
  112. } // namespace ge

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