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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/debug/ge_attr_define.h"
  24. #include "utils/tensor_utils.h"
  25. #include "utils/type_utils.h"
  26. namespace ge {
  27. void SerialShapeRange(const GeTensorDescPtr &desc, std::string &desc_str) {
  28. desc_str += "[";
  29. std::vector<std::pair<int64_t, int64_t>> shape_range;
  30. (void)desc->GetShapeRange(shape_range);
  31. for (const auto &pair : shape_range) {
  32. desc_str += "{";
  33. desc_str += std::to_string(pair.first) + "," + std::to_string(pair.second);
  34. desc_str += "},";
  35. }
  36. desc_str += "]";
  37. shape_range.clear();
  38. (void)desc->GetOriginShapeRange(shape_range);
  39. for (const auto &pair : shape_range) {
  40. desc_str += ",{";
  41. desc_str += std::to_string(pair.first) + "," + std::to_string(pair.second);
  42. desc_str += "},";
  43. }
  44. }
  45. std::string GetInTensorInfoWithString(const ge::NodePtr &node) {
  46. ge::OpDescPtr op_desc = node->GetOpDesc();
  47. std::stringstream ss;
  48. ss << "{";
  49. int32_t in_idx = 0;
  50. for (const auto &input_desc : op_desc->GetAllInputsDescPtr()) {
  51. if (input_desc == nullptr) {
  52. in_idx++;
  53. continue;
  54. }
  55. if (in_idx > 0) {
  56. ss << " ";
  57. }
  58. ss << "input_" << in_idx << " " << "tensor: [";
  59. ss << "(shape:[" << input_desc->MutableShape().ToString() << "]),";
  60. ss << "(format:" << TypeUtils::FormatToSerialString(input_desc->GetFormat()) << "),";
  61. ss << "(dtype:" << TypeUtils::DataTypeToSerialString(input_desc->GetDataType()) << "),";
  62. ss << "(origin_shape:" << input_desc->GetOriginShape().ToString() << "),";
  63. ss << "(origin_format:" << TypeUtils::FormatToSerialString(input_desc->GetOriginFormat()) << "),";
  64. ss << "(origin_dtype:" << TypeUtils::DataTypeToSerialString(input_desc->GetOriginDataType()) << "),";
  65. string range_str;
  66. SerialShapeRange(input_desc, range_str);
  67. ss << "(shape_range:" << range_str << ")]";
  68. in_idx++;
  69. }
  70. return ss.str();
  71. }
  72. Status InferShapePass::Run(NodePtr &node) {
  73. // kOptimizeAfterSubGraph exist means after subgraph
  74. auto ret = ShapeRefiner::InferShapeAndType(node, !OptionExists(kOptimizeAfterSubGraph));
  75. if (ret != GRAPH_SUCCESS) {
  76. // select INFERSHAPE failed info
  77. auto graph = node->GetOwnerComputeGraph();
  78. GE_CHECK_NOTNULL(graph);
  79. auto root_graph = ge::GraphUtils::FindRootGraph(graph);
  80. GE_CHECK_NOTNULL(root_graph);
  81. analyzer::DataInfo analyze_info{root_graph->GetSessionID(), root_graph->GetGraphID(),
  82. analyzer::INFER_SHAPE, node, "InferShapeFailed!"};
  83. (void)Analyzer::GetInstance()->DoAnalyze(analyze_info);
  84. (void)Analyzer::GetInstance()->SaveAnalyzerDataToFile(root_graph->GetSessionID(),
  85. root_graph->GetGraphID());
  86. REPORT_CALL_ERROR("E19999", "Call InferShapeAndType for node:%s(%s) failed, input_tensor:%s",
  87. node->GetName().c_str(), node->GetType().c_str(), GetInTensorInfoWithString(node).c_str());
  88. GELOGE(GE_GRAPH_INFERSHAPE_FAILED, "infershape failed. node: %s", node->GetName().c_str());
  89. return GE_GRAPH_INFERSHAPE_FAILED;
  90. }
  91. GE_CHK_STATUS_RET_NOLOG(RePassLoopNode(node));
  92. bool need_repass = false;
  93. auto has_attr = AttrUtils::GetBool(node->GetOpDesc(), ATTR_NAME_NEED_INFER_AGAIN, need_repass);
  94. if (has_attr) {
  95. if (!OptionExists(kOptimizeAfterSubGraph)) {
  96. return SUCCESS;
  97. }
  98. if (need_repass) {
  99. AddImmediateRePassNode(node);
  100. GELOGD("Node %s need repass immediately.", node->GetName().c_str());
  101. } else {
  102. // clear attr on while
  103. node->GetOpDesc()->DelAttr(ATTR_NAME_NEED_INFER_AGAIN);
  104. }
  105. }
  106. return SUCCESS;
  107. }
  108. Status InferShapePass::RePassLoopNode(const NodePtr &node) {
  109. const auto RePassNode = [&](const std::set<std::string> &re_pass_types) {
  110. for (auto &n : node->GetOutDataNodes()) {
  111. GE_CHECK_NOTNULL(n);
  112. if (re_pass_types.count(n->GetType()) > 0) {
  113. AddImmediateRePassNode(n);
  114. (void)AttrUtils::SetBool(n->GetOpDesc(), ATTR_NAME_NEED_INFER_AGAIN, false);
  115. GELOGD("Node %s need repass immediately after %s.", n->GetName().c_str(), node->GetName().c_str());
  116. }
  117. }
  118. return SUCCESS;
  119. };
  120. if (node->GetType() == NEXTITERATION || node->GetType() == REFNEXTITERATION) {
  121. return RePassNode({MERGE, REFMERGE}); // Re-Pass Merge
  122. }
  123. if (node->GetType() == MERGE || node->GetType() == REFMERGE) {
  124. if (node->GetOpDesc()->HasAttr(ATTR_NAME_NEED_INFER_AGAIN)) {
  125. node->GetOpDesc()->DelAttr(ATTR_NAME_NEED_INFER_AGAIN);
  126. }
  127. return SUCCESS;
  128. }
  129. return SUCCESS;
  130. }
  131. } // namespace ge

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