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.

node_item.cc 6.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * Copyright 2019-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 "node_item.h"
  17. #include <sstream>
  18. #include "common/debug/log.h"
  19. #include "graph/common/omg_util.h"
  20. #include "graph/compute_graph.h"
  21. #include "graph/debug/ge_attr_define.h"
  22. #include "graph/utils/node_utils.h"
  23. #include "hybrid/node_executor/node_executor.h"
  24. namespace ge {
  25. namespace hybrid {
  26. namespace {
  27. const char *const kAttrNameOriginalFusionGraph = "_original_fusion_graph";
  28. const char *const kNodeTypeRetVal = "_RetVal";
  29. Status ParseInputMapping(Node &node, OpDesc &op_desc, FusedSubgraph &fused_subgraph) {
  30. uint32_t parent_index = 0;
  31. if (!AttrUtils::GetInt(op_desc, ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  32. GELOGE(FAILED, "[%s] Failed to get attr [%s]", op_desc.GetName().c_str(), ATTR_NAME_PARENT_NODE_INDEX.c_str());
  33. return FAILED;
  34. }
  35. for (auto &node_and_anchor : node.GetOutDataNodesAndAnchors()) {
  36. auto dst_op_desc = node_and_anchor.first->GetOpDesc();
  37. GE_CHECK_NOTNULL(dst_op_desc);
  38. auto in_idx = node_and_anchor.second->GetIdx();
  39. auto tensor_desc = dst_op_desc->MutableInputDesc(in_idx);
  40. fused_subgraph.input_mapping[parent_index].emplace_back(tensor_desc);
  41. GELOGD("Input[%u] mapped to [%s:%u]", parent_index, dst_op_desc->GetName().c_str(), in_idx);
  42. }
  43. return SUCCESS;
  44. }
  45. Status ParseOutputMapping(OpDescPtr op_desc, FusedSubgraph &fused_subgraph) {
  46. uint32_t parent_index = 0;
  47. if (!AttrUtils::GetInt(op_desc, ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  48. GELOGE(FAILED, "[%s] Failed to get attr [%s]", op_desc->GetName().c_str(), ATTR_NAME_PARENT_NODE_INDEX.c_str());
  49. return FAILED;
  50. }
  51. fused_subgraph.output_mapping.emplace(parent_index, op_desc);
  52. return SUCCESS;
  53. }
  54. Status ParseFusedSubgraph(NodeItem &node_item) {
  55. if (!node_item.op_desc->HasAttr(kAttrNameOriginalFusionGraph)) {
  56. return SUCCESS;
  57. }
  58. GELOGI("[%s] Start to parse fused subgraph.", node_item.node_name.c_str());
  59. auto fused_subgraph = std::unique_ptr<FusedSubgraph>(new (std::nothrow) FusedSubgraph());
  60. GE_CHECK_NOTNULL(fused_subgraph);
  61. ComputeGraphPtr fused_graph;
  62. (void)AttrUtils::GetGraph(*node_item.op_desc, kAttrNameOriginalFusionGraph, fused_graph);
  63. GE_CHECK_NOTNULL(fused_graph);
  64. fused_graph->SetGraphUnknownFlag(true);
  65. fused_subgraph->graph = fused_graph;
  66. GE_CHK_GRAPH_STATUS_RET(fused_graph->TopologicalSorting());
  67. for (auto &node : fused_graph->GetAllNodes()) {
  68. GE_CHECK_NOTNULL(node);
  69. auto op_desc = node->GetOpDesc();
  70. GE_CHECK_NOTNULL(op_desc);
  71. std::string node_type;
  72. GE_CHK_STATUS_RET(GetOriginalType(node, node_type));
  73. if (node_type == DATA) {
  74. GE_CHK_GRAPH_STATUS_RET(ParseInputMapping(*node, *op_desc, *fused_subgraph));
  75. } else if (node_type == kNodeTypeRetVal) {
  76. GE_CHK_GRAPH_STATUS_RET(ParseOutputMapping(op_desc, *fused_subgraph));
  77. } else {
  78. fused_subgraph->nodes.emplace_back(node);
  79. }
  80. }
  81. node_item.fused_subgraph = std::move(fused_subgraph);
  82. GELOGI("[%s] Done parsing fused subgraph successfully.", node_item.NodeName().c_str());
  83. return SUCCESS;
  84. }
  85. } // namespace
  86. NodeItem::NodeItem(NodePtr node) : node(std::move(node)) {
  87. this->op_desc = this->node->GetOpDesc().get();
  88. this->node_id = this->op_desc->GetId();
  89. this->num_inputs = this->op_desc->GetInputsSize();
  90. this->num_outputs = this->op_desc->GetOutputsSize();
  91. this->node_name = this->node->GetName();
  92. this->node_type = this->node->GetType();
  93. }
  94. Status NodeItem::Init() {
  95. int32_t unknown_shape_type_val = 0;
  96. (void)AttrUtils::GetInt(op_desc, ::ge::ATTR_NAME_UNKNOWN_SHAPE_TYPE, unknown_shape_type_val);
  97. shape_inference_type = static_cast<UnknowShapeOpType>(unknown_shape_type_val);
  98. GE_CHK_STATUS_RET(NodeUtils::GetNodeUnknownShapeStatus(*node, is_dynamic), "[%s] Failed to get shape status.",
  99. node->GetName().c_str());
  100. GE_CHK_STATUS_RET(ParseFusedSubgraph(*this), "[%s] Failed to parse fused subgraph", node_name.c_str());
  101. if (is_dynamic) {
  102. for (int i = 0; i < num_inputs; ++i) {
  103. const auto &input_desc = op_desc->MutableInputDesc(i);
  104. GE_CHECK_NOTNULL(input_desc);
  105. if (input_desc->MutableShape().IsUnknownShape()) {
  106. is_input_shape_static.push_back(false);
  107. } else {
  108. num_static_input_shapes++;
  109. is_input_shape_static.push_back(true);
  110. GELOGD("[%s] The shape of input[%d] is static. shape = [%s]", NodeName().c_str(), i,
  111. input_desc->MutableShape().ToString().c_str());
  112. }
  113. }
  114. for (int i = 0; i < num_outputs; ++i) {
  115. const auto &output_desc = op_desc->MutableOutputDesc(i);
  116. GE_CHECK_NOTNULL(output_desc);
  117. if (output_desc->MutableShape().IsUnknownShape()) {
  118. is_output_shape_static = false;
  119. break;
  120. }
  121. }
  122. }
  123. return SUCCESS;
  124. }
  125. bool NodeItem::IsControlOp() const {
  126. auto op_type = op_desc->GetType();
  127. return op_type == IF || op_type == CASE || op_type == WHILE || op_type == FOR;
  128. }
  129. std::string NodeItem::DebugString() const {
  130. std::stringstream ss;
  131. ss << "Node: ";
  132. ss << "id = " << node_id;
  133. ss << ", name = [" << node->GetName();
  134. ss << "], type = " << node->GetType();
  135. ss << ", is_dynamic = " << (is_dynamic ? "True" : "False");
  136. ss << ", is_output_static = " << (is_output_shape_static ? "True" : "False");
  137. ss << ", unknown_shape_op_type = " << shape_inference_type;
  138. ss << ", input_start = " << input_start;
  139. ss << ", num_inputs = " << num_inputs;
  140. ss << ", output_start = " << output_start;
  141. ss << ", num_outputs = " << num_outputs;
  142. ss << ", dependent_nodes = [";
  143. for (const auto &dep_node : dependents_for_shape_inference) {
  144. ss << dep_node->GetName() << ", ";
  145. }
  146. ss << "]";
  147. int index = 0;
  148. for (auto &items : outputs) {
  149. ss << ", output[" << index++ << "]: ";
  150. for (auto &item : items) {
  151. ss << "(" << item.second->NodeName() << ":" << item.first << "), ";
  152. }
  153. }
  154. return ss.str();
  155. }
  156. void NodeItem::SetToDynamic() {
  157. num_static_input_shapes = 0;
  158. is_dynamic = true;
  159. for (size_t i = 0; i < is_input_shape_static.size(); ++i) {
  160. is_input_shape_static[i] = false;
  161. }
  162. if (kernel_task != nullptr && !kernel_task->IsSupportDynamicShape()) {
  163. GELOGD("[%s] Dynamic shape is not supported, clear node task.", node_name.c_str());
  164. kernel_task = nullptr;
  165. }
  166. }
  167. } // namespace hybrid
  168. } // namespace ge

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