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

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

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