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.

infershape_pass.cc 7.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/infershape_pass.h"
  17. #include "common/util/error_manager/error_manager.h"
  18. #include "framework/common/debug/ge_log.h"
  19. #include "analyzer/analyzer.h"
  20. #include "framework/common/util.h"
  21. #include "graph/shape_refiner.h"
  22. #include "graph/utils/graph_utils.h"
  23. #include "graph/utils/node_utils.h"
  24. #include "common/omg_util.h"
  25. #include "graph/debug/ge_attr_define.h"
  26. #include "graph/utils/tensor_utils.h"
  27. #include "graph/utils/type_utils.h"
  28. namespace ge {
  29. void SerialShapeRange(const GeTensorDescPtr &desc, std::string &desc_str) {
  30. desc_str += "[";
  31. std::vector<std::pair<int64_t, int64_t>> shape_range;
  32. (void)desc->GetShapeRange(shape_range);
  33. for (const auto &pair : shape_range) {
  34. desc_str += "{";
  35. desc_str += std::to_string(pair.first) + "," + std::to_string(pair.second);
  36. desc_str += "},";
  37. }
  38. desc_str += "]";
  39. shape_range.clear();
  40. (void)desc->GetOriginShapeRange(shape_range);
  41. for (const auto &pair : shape_range) {
  42. desc_str += ",{";
  43. desc_str += std::to_string(pair.first) + "," + std::to_string(pair.second);
  44. desc_str += "},";
  45. }
  46. }
  47. std::string GetInTensorInfoWithString(const ge::NodePtr &node) {
  48. ge::OpDescPtr op_desc = node->GetOpDesc();
  49. std::stringstream ss;
  50. ss << "{";
  51. int32_t in_idx = 0;
  52. for (const auto &input_desc : op_desc->GetAllInputsDescPtr()) {
  53. if (input_desc == nullptr) {
  54. in_idx++;
  55. continue;
  56. }
  57. if (in_idx > 0) {
  58. ss << " ";
  59. }
  60. ss << "input_" << in_idx << " " << "tensor: [";
  61. ss << "(shape:[" << input_desc->MutableShape().ToString() << "]),";
  62. ss << "(format:" << TypeUtils::FormatToSerialString(input_desc->GetFormat()) << "),";
  63. ss << "(dtype:" << TypeUtils::DataTypeToSerialString(input_desc->GetDataType()) << "),";
  64. ss << "(origin_shape:" << input_desc->GetOriginShape().ToString() << "),";
  65. ss << "(origin_format:" << TypeUtils::FormatToSerialString(input_desc->GetOriginFormat()) << "),";
  66. ss << "(origin_dtype:" << TypeUtils::DataTypeToSerialString(input_desc->GetOriginDataType()) << "),";
  67. string range_str;
  68. SerialShapeRange(input_desc, range_str);
  69. ss << "(shape_range:" << range_str << ")]";
  70. in_idx++;
  71. }
  72. return ss.str();
  73. }
  74. Status InferShapePass::Run(NodePtr &node) {
  75. // kOptimizeAfterSubGraph exist means after subgraph
  76. auto ret = ShapeRefiner::InferShapeAndType(node, !OptionExists(kOptimizeAfterSubGraph));
  77. if (ret != GRAPH_SUCCESS) {
  78. // select INFERSHAPE failed info
  79. auto graph = node->GetOwnerComputeGraph();
  80. GE_CHECK_NOTNULL(graph);
  81. auto root_graph = ge::GraphUtils::FindRootGraph(graph);
  82. GE_CHECK_NOTNULL(root_graph);
  83. analyzer::DataInfo analyze_info{root_graph->GetSessionID(), root_graph->GetGraphID(),
  84. analyzer::INFER_SHAPE, node, "InferShapeFailed!"};
  85. (void)Analyzer::GetInstance()->DoAnalyze(analyze_info);
  86. (void)Analyzer::GetInstance()->SaveAnalyzerDataToFile(root_graph->GetSessionID(),
  87. root_graph->GetGraphID());
  88. REPORT_CALL_ERROR("E19999", "Call InferShapeAndType for node:%s(%s) failed, input_tensor:%s",
  89. node->GetName().c_str(), node->GetType().c_str(), GetInTensorInfoWithString(node).c_str());
  90. GELOGE(GE_GRAPH_INFERSHAPE_FAILED, "[Call][InferShapeAndType] for node:%s(%s) failed, input_tensor:%s",
  91. node->GetName().c_str(), node->GetType().c_str(), GetInTensorInfoWithString(node).c_str());
  92. return GE_GRAPH_INFERSHAPE_FAILED;
  93. }
  94. GE_CHK_STATUS_RET_NOLOG(RePassLoopNode(node));
  95. bool need_repass = false;
  96. auto has_attr = AttrUtils::GetBool(node->GetOpDesc(), ATTR_NAME_NEED_INFER_AGAIN, need_repass);
  97. if (has_attr) {
  98. if (!OptionExists(kOptimizeAfterSubGraph)) {
  99. return SUCCESS;
  100. }
  101. if (need_repass) {
  102. AddImmediateRePassNode(node);
  103. GELOGD("Node %s need repass immediately.", node->GetName().c_str());
  104. } else {
  105. // clear attr on while
  106. node->GetOpDesc()->DelAttr(ATTR_NAME_NEED_INFER_AGAIN);
  107. }
  108. }
  109. return SUCCESS;
  110. }
  111. Status InferShapePass::RePassLoopNode(const NodePtr &node) {
  112. const auto RePassNode = [&](const std::set<std::string> &re_pass_types) {
  113. for (auto &n : node->GetOutDataNodes()) {
  114. GE_CHECK_NOTNULL(n);
  115. std::string node_type;
  116. GE_CHK_STATUS_RET(GetOriginalType(n, node_type), "[Get][OriginalType] of node:%s failed.", n->GetName().c_str());
  117. if (re_pass_types.count(node_type) > 0) {
  118. AddImmediateRePassNode(n);
  119. (void)AttrUtils::SetBool(n->GetOpDesc(), ATTR_NAME_NEED_INFER_AGAIN, false);
  120. GELOGD("Node %s need repass immediately after %s.", n->GetName().c_str(), node->GetName().c_str());
  121. }
  122. }
  123. return SUCCESS;
  124. };
  125. const auto ExProcNode = [&](const std::set<std::string> &proc_types,
  126. const std::function<void(InferShapePass *, NodePtr)> &proc_func,
  127. const std::string &info) {
  128. for (auto &n : node->GetOutDataNodes()) {
  129. GE_CHECK_NOTNULL(n);
  130. std::string node_type;
  131. GE_CHK_STATUS_RET(GetOriginalType(n, node_type), "[Get][OriginalType] of node:%s failed.", n->GetName().c_str());
  132. if (proc_types.count(node_type) > 0) {
  133. proc_func(this, n);
  134. GELOGD("Node %s %s after %s.", n->GetName().c_str(), info.c_str(), node->GetName().c_str());
  135. }
  136. }
  137. return SUCCESS;
  138. };
  139. std::string node_type;
  140. GE_CHK_STATUS_RET(GetOriginalType(node, node_type),
  141. "[Get][OriginalType] of node:%s failed.", node->GetName().c_str());
  142. if (kNextIterationOpTypes.count(node_type) > 0) {
  143. return RePassNode(kMergeOpTypes); // Re-Pass Merge
  144. }
  145. if (kMergeOpTypes.count(node_type) > 0) {
  146. if (node->GetOpDesc()->HasAttr(ATTR_NAME_NEED_INFER_AGAIN)) {
  147. node->GetOpDesc()->DelAttr(ATTR_NAME_NEED_INFER_AGAIN);
  148. return RePassNode(kSwitchOpTypes); // Re-Pass Switch
  149. }
  150. return SUCCESS;
  151. }
  152. if (kSwitchOpTypes.count(node_type) > 0) {
  153. if (node->GetOpDesc()->HasAttr(ATTR_NAME_NEED_INFER_AGAIN)) {
  154. node->GetOpDesc()->DelAttr(ATTR_NAME_NEED_INFER_AGAIN);
  155. return ExProcNode(kExitOpTypes, &InferShapePass::AddNodeResume, "need resume"); // Resume Exit
  156. } else {
  157. return ExProcNode(kExitOpTypes, &InferShapePass::AddNodeSuspend, "need suspend"); // Suspend Exit
  158. }
  159. }
  160. return SUCCESS;
  161. }
  162. } // namespace ge

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