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.

stream_graph_optimizer.cc 6.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 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
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "stream_graph_optimizer.h"
  17. #include "common/util.h"
  18. #include "framework/common/debug/ge_log.h"
  19. #include "graph/utils/node_utils.h"
  20. #include "graph/utils/tensor_utils.h"
  21. #include "init/gelib.h"
  22. using std::vector;
  23. namespace {
  24. const int64_t kInvalidStream = -1;
  25. } // namespace
  26. namespace ge {
  27. StreamGraphOptimizer::~StreamGraphOptimizer() {}
  28. void StreamGraphOptimizer::RefreshNodeId(const ComputeGraphPtr &comp_graph, Graph2SubGraphInfoList &subgraph_map) {
  29. size_t node_size = comp_graph->GetAllNodesSize();
  30. GELOGD("Refresh placeholder and end nodeId start from node num: %zu", node_size);
  31. for (const auto &subgraph_pair : subgraph_map) {
  32. for (const auto &subgraph_info : subgraph_pair.second) {
  33. ComputeGraphPtr subgraph = subgraph_info->GetSubGraph();
  34. if (subgraph == nullptr) {
  35. continue;
  36. }
  37. for (ge::NodePtr &node : subgraph->GetDirectNode()) {
  38. GE_CHECK_NOTNULL_EXEC(node->GetOpDesc(), return);
  39. if ((node->GetType() == END) || (node->GetType() == PLACEHOLDER)) {
  40. node->GetOpDesc()->SetId(static_cast<int64_t>(node_size));
  41. node_size++;
  42. }
  43. }
  44. }
  45. }
  46. }
  47. bool StreamGraphOptimizer::IsSameStreamIdOrBatchLabel(const ComputeGraphPtr &comp_graph) {
  48. if (comp_graph == nullptr) {
  49. return false;
  50. }
  51. std::set<int64_t> stream_set;
  52. std::set<std::string> label_set;
  53. for (const ge::NodePtr &cur_node : comp_graph->GetDirectNode()) {
  54. GE_IF_BOOL_EXEC(cur_node->GetOpDesc() == nullptr, continue);
  55. int64_t stream_id = cur_node->GetOpDesc()->GetStreamId();
  56. if (stream_id == kInvalidStream) {
  57. continue;
  58. }
  59. stream_set.insert(stream_id);
  60. std::string batch_label;
  61. if (AttrUtils::GetStr(cur_node->GetOpDesc(), ATTR_NAME_BATCH_LABEL, batch_label)) {
  62. label_set.insert(batch_label);
  63. } else {
  64. GELOGD("Node %s[%s] has no batch_label, subgraph %s, stream id: %ld ", cur_node->GetName().c_str(),
  65. cur_node->GetType().c_str(), comp_graph->GetName().c_str(), stream_id);
  66. continue;
  67. }
  68. GELOGD("Node %s in subgraph %s stream id: %ld, batch_label: %s, node num: %zu", cur_node->GetName().c_str(),
  69. comp_graph->GetName().c_str(), stream_id, batch_label.c_str(), comp_graph->GetDirectNodesSize());
  70. }
  71. if (stream_set.size() > 1 || label_set.size() > 1) {
  72. GELOGI("Nodes of graph: %s have different stream id or batch_label, node num: %zu, different stream num: %zu.",
  73. comp_graph->GetName().c_str(), comp_graph->GetDirectNodesSize(), stream_set.size());
  74. return false;
  75. }
  76. if (!label_set.empty()) {
  77. (void)AttrUtils::SetStr(comp_graph, ATTR_NAME_BATCH_LABEL, *label_set.begin());
  78. }
  79. return true;
  80. }
  81. Status StreamGraphOptimizer::OptimizeStreamedSubGraph(const ComputeGraphPtr &comp_graph,
  82. Graph2SubGraphInfoList &subgraph_map,
  83. struct RunContext &run_context) {
  84. RefreshNodeId(comp_graph, subgraph_map);
  85. std::shared_ptr<GELib> instance = ge::GELib::GetInstance();
  86. GE_CHECK_NOTNULL(instance);
  87. for (const auto &subgraph_pair : subgraph_map) {
  88. for (const auto &subgraph_info : subgraph_pair.second) {
  89. ComputeGraphPtr subgraph = subgraph_info->GetSubGraph();
  90. GE_CHECK_NOTNULL(subgraph);
  91. GELOGD("Optimize subgraph %s", subgraph->GetName().c_str());
  92. std::string engine_name = subgraph_info->GetEngineName();
  93. vector<GraphOptimizerPtr> graph_optimizers;
  94. if (instance->DNNEngineManagerObj().IsEngineRegistered(engine_name)) {
  95. instance->OpsKernelManagerObj().GetGraphOptimizerByEngine(engine_name, graph_optimizers);
  96. GELOGI("Subgraph: %s start optimize streamed graph. engineName: %s, graph Optimizer num: %zu.",
  97. subgraph->GetName().c_str(), engine_name.c_str(), graph_optimizers.size());
  98. auto nodes = subgraph->GetDirectNode();
  99. if (nodes.empty()) {
  100. continue;
  101. }
  102. if (!IsSameStreamIdOrBatchLabel(subgraph)) {
  103. GELOGI("There are more than one stream or batch_label in subgraph %s", subgraph->GetName().c_str());
  104. continue;
  105. }
  106. OpDescPtr op_desc = nodes.at(0)->GetOpDesc();
  107. GE_CHECK_NOTNULL(op_desc);
  108. int64_t stream_id = op_desc->GetStreamId();
  109. if (static_cast<size_t>(stream_id) >= run_context.graphStreamList.size()) {
  110. GELOGE(FAILED, "stream_id %ld is bigger than run_context.graphStreamList.size() %zu", stream_id,
  111. run_context.graphStreamList.size());
  112. return FAILED;
  113. }
  114. run_context.stream = run_context.graphStreamList[stream_id];
  115. std::string batch_label;
  116. (void)AttrUtils::GetStr(subgraph, ATTR_NAME_BATCH_LABEL, batch_label);
  117. GELOGD("Subgraph has same stream id, subgraph: %s, engine_name: %s, stream_id: %ld, rtstream: %lu, "
  118. "batch_label: %s", subgraph->GetName().c_str(), engine_name.c_str(), stream_id,
  119. static_cast<uint64_t>(reinterpret_cast<uintptr_t>(run_context.stream)), batch_label.c_str());
  120. for (auto iter = graph_optimizers.begin(); iter != graph_optimizers.end(); ++iter) {
  121. GE_CHECK_NOTNULL(*iter);
  122. Status ret = (*iter)->OptimizeStreamGraph(*subgraph, run_context);
  123. if (ret != SUCCESS) {
  124. GELOGE(
  125. ret,
  126. "[optimizeStreamedSubGraph]: optimize streamed subgraph failed, subgraph: %s, engine_name: %s, graph "
  127. "Optimizer num: %zu, ret: %u",
  128. subgraph->GetName().c_str(), engine_name.c_str(), graph_optimizers.size(), ret);
  129. return ret;
  130. }
  131. GELOGD(
  132. "[optimizeStreamedSubGraph]: optimize streamed subgraph success, subgraph: %s, engine_name: %s, graph "
  133. "Optimizer num: %zu!",
  134. subgraph->GetName().c_str(), engine_name.c_str(), graph_optimizers.size());
  135. }
  136. }
  137. }
  138. }
  139. GELOGD("Optimize streamed subgraph success.");
  140. return SUCCESS;
  141. }
  142. } // namespace ge

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