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

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