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.

replace_with_empty_const_pass.cc 5.8 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/replace_with_empty_const_pass.h"
  17. #include <sstream>
  18. #include <string>
  19. #include "common/ge/ge_util.h"
  20. #include "framework/common/debug/ge_log.h"
  21. #include "framework/common/ge_inner_error_codes.h"
  22. #include "graph/utils/graph_utils.h"
  23. namespace ge {
  24. Status ReplaceWithEmptyConstPass::Run(NodePtr &node) {
  25. GELOGD("ReplaceWithEmptyConstPass in.");
  26. if (node == nullptr) {
  27. GELOGE(PARAM_INVALID, "Parameter is null.");
  28. return PARAM_INVALID;
  29. }
  30. if (node->GetOpDesc() == nullptr) {
  31. GELOGE(PARAM_INVALID, "Param [opDesc] must not be null.");
  32. return PARAM_INVALID;
  33. }
  34. if (node->GetType() == CONSTANT || node->GetType() == CONSTANTOP) {
  35. GELOGI("Node %s is const. Ignore current pass.", node->GetName().c_str());
  36. return SUCCESS;
  37. }
  38. // Node like no op, it has no output
  39. if (node->GetOpDesc()->GetAllOutputsDescPtr().empty()) {
  40. GELOGI("Node %s has no output desc. Ignore current pass.", node->GetName().c_str());
  41. return SUCCESS;
  42. }
  43. // If outputs of current node are all empty, replace it with empty const
  44. bool is_all_output_empty = true;
  45. for (const auto &output_desc_ptr : node->GetOpDesc()->GetAllOutputsDescPtr()) {
  46. if (output_desc_ptr == nullptr) {
  47. GELOGI("Node %s Got empty output_desc_ptr, ignore current pass.", node->GetName().c_str());
  48. return SUCCESS;
  49. }
  50. if (!IsEmptyTenor(output_desc_ptr->GetShape())) {
  51. is_all_output_empty = false;
  52. break;
  53. }
  54. }
  55. if (is_all_output_empty) {
  56. GELOGI("Node %s has empty tensor output. It will be replaced by empty const.", node->GetName().c_str());
  57. // Replace op which all output is empty with empty const
  58. Status ret = ReplaceWithEmptyConst(node);
  59. if (ret != SUCCESS) {
  60. // If replace failed, it should not break whole process, so still return success
  61. GELOGW("Failed to repalce node %s with empty const.", node->GetName().c_str());
  62. }
  63. }
  64. GELOGD("ReplaceWithEmptyConstPass end.");
  65. return SUCCESS;
  66. }
  67. Status ReplaceWithEmptyConstPass::ReplaceWithEmptyConst(NodePtr &node_to_replace) {
  68. std::map<string, vector<int>> shape_out_idx_map;
  69. auto op_desc = node_to_replace->GetOpDesc();
  70. // Collect out_idx follow different out shape
  71. for (const auto &out_anchor : node_to_replace->GetAllOutDataAnchors()) {
  72. auto out_desc = op_desc->GetOutputDesc(out_anchor->GetIdx());
  73. shape_out_idx_map[GetDimStr(out_desc.GetShape())].emplace_back(out_anchor->GetIdx());
  74. }
  75. for (const auto &shape_2_out_idx : shape_out_idx_map) {
  76. // Create empty const
  77. // The out_desc in one group should be same shape, so here only get first out_desc. its valid index.
  78. auto out_desc = op_desc->GetOutputDesc(shape_2_out_idx.second[0]);
  79. NodePtr const_node;
  80. auto graph = node_to_replace->GetOwnerComputeGraph();
  81. Status ret = InsertEmptyConst(out_desc, const_node, graph);
  82. if (ret != SUCCESS) {
  83. GELOGE(FAILED, "Failed insert const node.");
  84. return FAILED;
  85. }
  86. // Repalce data anchors
  87. for (const auto &anchor_idx: shape_2_out_idx.second) {
  88. if (GraphUtils::ReplaceNodeDataAnchors(const_node, node_to_replace, {}, {anchor_idx}) != GRAPH_SUCCESS) {
  89. GELOGE(FAILED, "[%s] ReplaceNodeAnchors failed.", node_to_replace->GetName().c_str());
  90. return FAILED;
  91. }
  92. }
  93. // Copy in control edge
  94. if (GraphUtils::CopyInCtrlEdges(node_to_replace, const_node) != GRAPH_SUCCESS) {
  95. GELOGE(FAILED, "CopyInCtrlEdges from %s to %s failed.", node_to_replace->GetName().c_str(),
  96. const_node->GetName().c_str());
  97. return FAILED;
  98. }
  99. // Copy out control edge
  100. if (GraphUtils::CopyOutCtrlEdges(node_to_replace, const_node) != GRAPH_SUCCESS) {
  101. GELOGE(FAILED, "CopyOutCtrlEdges from %s to %s failed.", node_to_replace->GetName().c_str(),
  102. const_node->GetName().c_str());
  103. return FAILED;
  104. }
  105. AddRePassNodesWithInOut(const_node);
  106. GELOGI("Node %s has been replaced by empty const %s.", node_to_replace->GetName().c_str(),
  107. const_node->GetName().c_str());
  108. }
  109. IsolateAndDeleteNode(node_to_replace, {});
  110. return SUCCESS;
  111. }
  112. Status ReplaceWithEmptyConstPass::InsertEmptyConst(const GeTensorDesc &out_desc, NodePtr &const_node,
  113. ComputeGraphPtr &graph) {
  114. GeTensorPtr empty_tensor = MakeShared<ge::GeTensor>(out_desc);
  115. if (empty_tensor == nullptr) {
  116. GELOGE(OUT_OF_MEMORY, "Failed create empty tensor.");
  117. return OUT_OF_MEMORY;
  118. }
  119. auto const_desc = OpDescUtils::CreateConstOp(empty_tensor);
  120. if (const_desc == nullptr) {
  121. GELOGE(OUT_OF_MEMORY, "Failed to get const desc from tensor");
  122. return OUT_OF_MEMORY;
  123. }
  124. const_node = graph->AddNode(const_desc);
  125. if (const_node == nullptr) {
  126. GELOGE(FAILED, "Failed insert const node.");
  127. return FAILED;
  128. }
  129. return SUCCESS;
  130. }
  131. bool ReplaceWithEmptyConstPass::IsEmptyTenor(const GeShape &shape) const {
  132. for (auto dim : shape.GetDims()) {
  133. if (dim == 0) {
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. string ReplaceWithEmptyConstPass::GetDimStr(const GeShape &shape) {
  140. std::stringstream dim_str;
  141. for (auto dim : shape.GetDims()) {
  142. dim_str << dim << '-';
  143. }
  144. return dim_str.str();
  145. }
  146. } // namespace ge

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