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.3 kB

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

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