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.

end_of_sequence_add_control_pass.cc 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/end_of_sequence_add_control_pass.h"
  17. #include <string>
  18. #include <vector>
  19. #include "init/gelib.h"
  20. #include "graph/node.h"
  21. namespace ge {
  22. Status EndOfSequenceAddControlPass::Run(ComputeGraphPtr graph) {
  23. if (graph == nullptr) {
  24. GELOGE(PARAM_INVALID, "param [graph] must not be null.");
  25. return PARAM_INVALID;
  26. }
  27. if (graph->GetParentGraph() != nullptr) {
  28. return SUCCESS;
  29. }
  30. NodePtr end_of_sequence = GetEndOfSequence(graph);
  31. if (end_of_sequence == nullptr) {
  32. return SUCCESS;
  33. }
  34. GELOGI("EndOfSequenceAddControlPass begin.");
  35. std::vector<NodePtr> target_nodes;
  36. for (NodePtr &node : graph->GetDirectNode()) {
  37. if (node == nullptr) {
  38. GELOGW("node is nullptr.");
  39. continue;
  40. }
  41. string stream_label;
  42. (void)AttrUtils::GetStr(node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, stream_label);
  43. if (!stream_label.empty() || IsDataLikeNode(node)) {
  44. continue;
  45. }
  46. // Save the nodes whose pre-nodes are all data-like node
  47. auto in_data_nodes = node->GetInDataNodes();
  48. bool flag = false;
  49. for (auto in_node : in_data_nodes) {
  50. if (!IsDataLikeNode(in_node)) {
  51. flag = true;
  52. break;
  53. }
  54. }
  55. if (flag) {
  56. continue;
  57. }
  58. target_nodes.push_back(node);
  59. }
  60. // Insert control edge
  61. Status status = AddControlEdge(end_of_sequence, target_nodes);
  62. if (status != SUCCESS) {
  63. GELOGE(FAILED, "Graph add EndOfSequence op out ctrl edge fail.");
  64. return FAILED;
  65. }
  66. GELOGI("EndOfSequenceAddControlPass end.");
  67. return SUCCESS;
  68. }
  69. Status EndOfSequenceAddControlPass::AddControlEdge(NodePtr &end_of_sequence, std::vector<NodePtr> &target_nodes) {
  70. auto out_ctrl_anchor = end_of_sequence->GetOutControlAnchor();
  71. for (NodePtr &node : target_nodes) {
  72. auto in_ctrl_anchor = node->GetInControlAnchor();
  73. if (in_ctrl_anchor == nullptr) {
  74. continue;
  75. }
  76. Status status = GraphUtils::AddEdge(out_ctrl_anchor, in_ctrl_anchor);
  77. if (status != GRAPH_SUCCESS) {
  78. GELOGE(FAILED, "Graph add EndOfSequence op out ctrl edge fail, dst node: %s.", node->GetName().c_str());
  79. return FAILED;
  80. }
  81. GELOGI("Graph add EndOfSequence op out ctrl edge, dst node: %s.", node->GetName().c_str());
  82. }
  83. return SUCCESS;
  84. }
  85. inline NodePtr EndOfSequenceAddControlPass::GetEndOfSequence(const ComputeGraphPtr &graph) const {
  86. // Internal function, guaranteeing graph non-null
  87. for (NodePtr &node : graph->GetDirectNode()) {
  88. if (node->GetType() == ENDOFSEQUENCE) {
  89. return node;
  90. }
  91. }
  92. return nullptr;
  93. }
  94. bool EndOfSequenceAddControlPass::IsDataLikeNode(const NodePtr &node) {
  95. std::shared_ptr<GELib> instance_ptr = GELib::GetInstance();
  96. if ((instance_ptr == nullptr) || (!instance_ptr->InitFlag())) {
  97. GELOGW("GELib not initialized");
  98. return false;
  99. }
  100. OpDescPtr op_desc = node->GetOpDesc();
  101. if (op_desc == nullptr) {
  102. return false;
  103. }
  104. string engine_name = op_desc->GetOpEngineName();
  105. if (engine_name.empty()) {
  106. engine_name = instance_ptr->DNNEngineManagerObj().GetDNNEngineName(node);
  107. }
  108. const map<string, SchedulerConf> schedulers = instance_ptr->DNNEngineManagerObj().GetSchedulers();
  109. // Only one scheduler has been supported by now
  110. for (auto schedulers_iter = schedulers.begin(); schedulers_iter != schedulers.end(); ++schedulers_iter) {
  111. const map<string, EngineConfPtr> cal_engines = schedulers_iter->second.cal_engines;
  112. auto cal_engines_iter = cal_engines.find(engine_name);
  113. if (cal_engines_iter == cal_engines.end()) {
  114. GELOGW("No cal_engines found within engine %s, node name %s", engine_name.c_str(), node->GetName().c_str());
  115. continue;
  116. }
  117. EngineConfPtr engine_conf_ptr = cal_engines_iter->second;
  118. if (engine_conf_ptr == nullptr) {
  119. GELOGW("engine_conf_ptr within engine %s, node name %s is null", engine_name.c_str(), node->GetName().c_str());
  120. continue;
  121. }
  122. bool skip_assign_stream = engine_conf_ptr->skip_assign_stream;
  123. if (skip_assign_stream) {
  124. return true;
  125. }
  126. return false;
  127. }
  128. return false;
  129. }
  130. } // namespace ge

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