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.

global_step_insert_pass.cc 5.7 kB

5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/global_step_insert_pass.h"
  17. #include <memory>
  18. #include <string>
  19. #include <vector>
  20. #include "framework/common/debug/ge_log.h"
  21. #include "framework/common/util.h"
  22. #include "graph/debug/ge_attr_define.h"
  23. #include "common/ge/ge_util.h"
  24. #include "graph/manager/graph_var_manager.h"
  25. #include "graph/passes/pass_utils.h"
  26. #include "graph/ge_context.h"
  27. namespace {
  28. const char *const kFlagOff = "0";
  29. } // namespace
  30. namespace ge {
  31. NodePtr GlobalStepInsertPass::InsertOp(ComputeGraphPtr &compute_graph,
  32. const string &node_type,
  33. const string &node_name,
  34. const std::vector<GeTensorDesc> &input_list,
  35. const std::vector<GeTensorDesc> &output_list) {
  36. OpDescPtr op_desc = MakeShared<OpDesc>(node_name, node_type);
  37. GE_IF_BOOL_EXEC(op_desc == nullptr,
  38. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  39. GELOGE(FAILED,"[New][OpDesc] failed");
  40. return nullptr);
  41. for (auto &input_desc : input_list) {
  42. graphStatus graph_status = op_desc->AddInputDesc(input_desc);
  43. if (graph_status != GRAPH_SUCCESS) {
  44. REPORT_CALL_ERROR("E19999", "Add input desc to op:%s(%s) failed",
  45. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  46. GELOGE(FAILED, "[Add][InputDesc] to op:%s(%s) failed",
  47. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  48. return nullptr;
  49. }
  50. }
  51. for (auto &output_desc : output_list) {
  52. graphStatus graph_status = op_desc->AddOutputDesc(output_desc);
  53. if (graph_status != GRAPH_SUCCESS) {
  54. REPORT_CALL_ERROR("E19999", "Add output desc to op:%s(%s) failed",
  55. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  56. GELOGE(FAILED, "[Add][OutputDesc] to op:%s(%s) failed, ret:%u.",
  57. op_desc->GetName().c_str(), op_desc->GetType().c_str(), graph_status);
  58. return nullptr;
  59. }
  60. }
  61. GE_IF_BOOL_EXEC(compute_graph == nullptr, GELOGE(FAILED,"[Check][Param] compute_graph is nullptr"); return nullptr);
  62. NodePtr node = compute_graph->AddNode(op_desc);
  63. GE_IF_BOOL_EXEC(node == nullptr,
  64. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  65. op_desc->GetName().c_str(), op_desc->GetType().c_str(), compute_graph->GetName().c_str());
  66. GELOGE(FAILED, "[Add][Node] %s(%s) to graph:%s failed",
  67. op_desc->GetName().c_str(), op_desc->GetType().c_str(), compute_graph->GetName().c_str());
  68. return nullptr);
  69. GELOGI("Insert op success, name:%s, type:%s.", node_name.c_str(), node_type.c_str());
  70. return node;
  71. }
  72. Status GlobalStepInsertPass::Run(ComputeGraphPtr compute_graph) {
  73. // run_flag off means offline, no need insert global step node which type is variable
  74. std::string run_flag;
  75. if (ge::GetContext().GetOption(ge::RUN_FLAG, run_flag) == GRAPH_SUCCESS && run_flag == kFlagOff) {
  76. GELOGI("compute_graph [%u] [%s] skip insert global step", compute_graph->GetGraphID(),
  77. compute_graph->GetName().c_str());
  78. return SUCCESS;
  79. }
  80. NodePtr output_node = compute_graph->FindFirstNodeMatchType(NETOUTPUT);
  81. if (output_node == nullptr) {
  82. GELOGD("Node type %s can't be found in graph %u", NETOUTPUT, compute_graph->GetGraphID());
  83. return SUCCESS;
  84. }
  85. if (compute_graph->GetParentGraph() != nullptr) {
  86. GELOGD("Subgraph %s no need global step variable.", compute_graph->GetName().c_str());
  87. return SUCCESS;
  88. }
  89. NodePtr exist_node = compute_graph->FindNode(NODE_NAME_GLOBAL_STEP);
  90. if (exist_node != nullptr) {
  91. GELOGD("Node %s already exist, no need add.", NODE_NAME_GLOBAL_STEP.c_str());
  92. return SUCCESS;
  93. }
  94. // set global step tensor desc
  95. GeTensorDesc tensor_desc(GeShape({1}), FORMAT_ND, DT_UINT64);
  96. std::vector<GeTensorDesc> input_desc_list = {};
  97. std::vector<GeTensorDesc> output_desc_list = {tensor_desc};
  98. NodePtr global_step = InsertOp(compute_graph, VARIABLE, NODE_NAME_GLOBAL_STEP,
  99. input_desc_list, output_desc_list);
  100. if (global_step == nullptr) {
  101. GELOGE(FAILED, "[Insert][Op] to graph:%s failed.", compute_graph->GetName().c_str());
  102. return FAILED;
  103. }
  104. // add ctrl edges
  105. graphStatus add_ret = GraphUtils::AddEdge(global_step->GetOutControlAnchor(), output_node->GetInControlAnchor());
  106. if (add_ret != GRAPH_SUCCESS) {
  107. REPORT_CALL_ERROR("E19999", "Add control edge between op:%s(%s) and op:%s(%s) failed",
  108. global_step->GetName().c_str(), global_step->GetType().c_str(),
  109. output_node->GetName().c_str(), output_node->GetType().c_str());
  110. GELOGE(FAILED, "[Add][ControlEdge] between op:%s(%s) and op:%s(%s) failed",
  111. global_step->GetName().c_str(), global_step->GetType().c_str(),
  112. output_node->GetName().c_str(), output_node->GetType().c_str());
  113. return FAILED;
  114. }
  115. GELOGD("Add global_step to netoutput edge in graph %u success", compute_graph->GetGraphID());
  116. return SUCCESS;
  117. }
  118. } // namespace ge

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