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

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "while_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 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. GELOGE(INTERNAL_ERROR, "Node: %s has invalid subgraph, cond branch: %s, body branch: %s.",
  43. while_desc->GetName().c_str(), cond_name.c_str(), body_name.c_str());
  44. return FAILED;
  45. }
  46. ComputeGraphPtr cond_graph = parent_graph_->GetSubgraph(cond_name);
  47. ComputeGraphPtr body_graph = parent_graph_->GetSubgraph(body_name);
  48. GE_CHECK_NOTNULL(cond_graph);
  49. GE_CHECK_NOTNULL(body_graph);
  50. const uint32_t cond_enter_index = label_index++;
  51. const uint32_t body_enter_index = label_index++;
  52. const uint32_t body_leave_index = label_index++;
  53. const std::string cond_enter_name = parent_node_->GetName() + "/CondLabelSet"; // rtLabelSet
  54. const std::string cond_active_name = parent_node_->GetName() + "/CondStreamActive"; // rtStreamActive
  55. const std::string cond_leave_name = parent_node_->GetName() + "/LabelSwitch"; // rtLabelSwitchByIndex
  56. const std::string body_enter_name = parent_node_->GetName() + "/EnterLabelSet"; // rtLabelSet
  57. const std::string body_active_name = parent_node_->GetName() + "/EnterStreamActive"; // rtStreamActive
  58. const std::string goto_leave_name = parent_node_->GetName() + "/LabelGoto"; // rtLabelGoto
  59. const std::string body_leave_name = parent_node_->GetName() + "/LeaveLabelSet"; // rtLabelSet
  60. NodePtr cond_stream_active = AddStreamActive(cond_graph, cond_active_name);
  61. if (cond_stream_active == nullptr) {
  62. GELOGE(INTERNAL_ERROR, "Subgraph: %s add stream active failed.", cond_graph->GetName().c_str());
  63. return FAILED;
  64. }
  65. if (AddLabelSetEnter(cond_graph, cond_enter_name, cond_enter_index, cond_stream_active) == nullptr) {
  66. GELOGE(INTERNAL_ERROR, "Subgraph: %s add label set failed.", cond_graph->GetName().c_str());
  67. return FAILED;
  68. }
  69. NodePtr body_stream_active = AddStreamActive(body_graph, body_active_name);
  70. if (body_stream_active == nullptr) {
  71. GELOGE(INTERNAL_ERROR, "Subgraph: %s add stream active failed.", body_graph->GetName().c_str());
  72. return FAILED;
  73. }
  74. if (AddLabelSetEnter(body_graph, body_enter_name, body_enter_index, body_stream_active) == nullptr) {
  75. GELOGE(INTERNAL_ERROR, "Subgraph: %s add label set failed.", body_graph->GetName().c_str());
  76. return FAILED;
  77. }
  78. if (AddLabelGotoLeave(body_graph, goto_leave_name, cond_enter_index) == nullptr) {
  79. GELOGE(INTERNAL_ERROR, "Subgraph: %s add label goto failed.", body_graph->GetName().c_str());
  80. return FAILED;
  81. }
  82. if (AddLabelSetLeave(body_graph, body_leave_name, body_leave_index) == nullptr) {
  83. GELOGE(INTERNAL_ERROR, "Subgraph: %s add label set failed.", body_graph->GetName().c_str());
  84. return FAILED;
  85. }
  86. NodePtr cond_out_node = cond_graph->FindFirstNodeMatchType(NETOUTPUT);
  87. GE_CHECK_NOTNULL(cond_out_node);
  88. OpDescPtr cond_out_desc = cond_out_node->GetOpDesc();
  89. GE_CHECK_NOTNULL(cond_out_desc);
  90. GeTensorDesc pred_desc = cond_out_desc->GetInputDesc(kCondOutputIndex);
  91. // false ==> 0 ==> switch_labels[0] ==> body_leave_index
  92. // true ==> 1 ==> switch_labels[1] ==> body_enter_name
  93. const std::vector<uint32_t> switch_labels = {body_leave_index, body_enter_index};
  94. NodePtr switch_node = AddLabelSwitchLeave(cond_graph, cond_leave_name, pred_desc, switch_labels);
  95. if (switch_node == nullptr) {
  96. GELOGE(INTERNAL_ERROR, "Subgraph: %s add label switch failed.", cond_graph->GetName().c_str());
  97. return FAILED;
  98. }
  99. // link Data input.
  100. const auto &all_in_data = cond_out_node->GetAllInDataAnchors();
  101. if (all_in_data.size() != kCondOutputNum) {
  102. GELOGE(FAILED, "Node: %s Cond sbugraph output size:%zu should equal size:%u.",
  103. switch_node->GetName().c_str(), all_in_data.size(), kCondOutputNum);
  104. return FAILED;
  105. }
  106. InDataAnchorPtr in_anchor = all_in_data.at(kCondOutputIndex);
  107. GE_CHECK_NOTNULL(in_anchor);
  108. if (GraphUtils::AddEdge(in_anchor->GetPeerOutAnchor(), switch_node->GetInDataAnchor(kCondOutputIndex)) != SUCCESS) {
  109. GELOGE(FAILED, "Node: %s Add pred data input failed.", switch_node->GetName().c_str());
  110. return FAILED;
  111. }
  112. GELOGI("Node: %s assign label success.", while_desc->GetName().c_str());
  113. return SUCCESS;
  114. }
  115. REGISTER_LABEL_MAKER(WHILE, WhileOpLabelMaker);
  116. REGISTER_LABEL_MAKER(_WHILE, WhileOpLabelMaker);
  117. REGISTER_LABEL_MAKER(STATELESSWHILE, WhileOpLabelMaker);
  118. } // namespace ge

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