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

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

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