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

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include "graph/utils/node_utils.h"
  24. namespace {
  25. const std::unordered_set<std::string> kControlFlowOps = {
  26. ge::SWITCH,
  27. ge::REFSWITCH,
  28. ge::MERGE,
  29. ge::REFMERGE,
  30. ge::ENTER,
  31. ge::REFENTER,
  32. ge::NEXTITERATION,
  33. ge::REFNEXTITERATION,
  34. ge::EXIT,
  35. ge::REFEXIT,
  36. ge::LOOPCOND
  37. };
  38. }
  39. namespace ge {
  40. Status ReplaceWithEmptyConstPass::Run(NodePtr &node) {
  41. GELOGD("ReplaceWithEmptyConstPass in.");
  42. if (node == nullptr) {
  43. REPORT_INNER_ERROR("E19999", "Param node is nullptr, check invalid");
  44. GELOGE(PARAM_INVALID, "[Check][Param] Parameter node is nullptr.");
  45. return PARAM_INVALID;
  46. }
  47. if (node->GetOpDesc() == nullptr) {
  48. REPORT_INNER_ERROR("E19999", "Param node's op_desc is nullptr, check invalid");
  49. GELOGE(PARAM_INVALID, "[Get][OpDesc] failed, Param [opDesc] must not be null.");
  50. return PARAM_INVALID;
  51. }
  52. if (node->GetType() == CONSTANT || node->GetType() == CONSTANTOP || node->GetType() == DATA) {
  53. GELOGI("Node %s is const. Ignore current pass.", node->GetName().c_str());
  54. return SUCCESS;
  55. }
  56. if (kControlFlowOps.count(NodeUtils::GetNodeType(node)) != 0) {
  57. GELOGI("Node %s is control flow op. Ignore current pass.", node->GetName().c_str());
  58. return SUCCESS;
  59. }
  60. // Node like no op, it has no output
  61. if (node->GetOpDesc()->GetAllOutputsDescPtr().empty()) {
  62. GELOGI("Node %s has no output desc. Ignore current pass.", node->GetName().c_str());
  63. return SUCCESS;
  64. }
  65. // If outputs of current node are all empty, replace it with empty const
  66. bool is_all_output_empty = true;
  67. for (const auto &output_desc_ptr : node->GetOpDesc()->GetAllOutputsDescPtr()) {
  68. if (output_desc_ptr == nullptr) {
  69. GELOGI("Node %s Got empty output_desc_ptr, ignore current pass.", node->GetName().c_str());
  70. return SUCCESS;
  71. }
  72. if (!IsEmptyTenor(output_desc_ptr->GetShape())) {
  73. is_all_output_empty = false;
  74. break;
  75. }
  76. }
  77. if (is_all_output_empty) {
  78. GELOGI("Node %s has empty tensor output. It will be replaced by empty const.", node->GetName().c_str());
  79. // Replace op which all output is empty with empty const
  80. vector<GeTensorPtr> outputs;
  81. Status ret = GetOutputsOfCurrNode(node, outputs);
  82. if (ret != SUCCESS) {
  83. // If replace failed, it should not break whole process, so still return success
  84. GELOGW("Failed to get outputs of node %s.", node->GetName().c_str());
  85. }
  86. else {
  87. ret = Folding(node, outputs);
  88. if (ret != SUCCESS) {
  89. // If replace failed, it should not break whole process, so still return success
  90. GELOGW("Failed to repalce node %s with empty const.", node->GetName().c_str());
  91. }
  92. }
  93. }
  94. GELOGD("ReplaceWithEmptyConstPass end.");
  95. return SUCCESS;
  96. }
  97. Status ReplaceWithEmptyConstPass::GetOutputsOfCurrNode(const NodePtr &node_to_replace, vector<GeTensorPtr> &outputs) {
  98. for (const auto &out_anchor : node_to_replace->GetAllOutDataAnchors()) {
  99. GE_CHECK_NOTNULL(node_to_replace->GetOpDesc());
  100. auto out_desc = node_to_replace->GetOpDesc()->GetOutputDesc(out_anchor->GetIdx());
  101. GeTensorPtr empty_tensor = MakeShared<ge::GeTensor>(out_desc);
  102. GE_CHECK_NOTNULL(empty_tensor);
  103. outputs.emplace_back(empty_tensor);
  104. }
  105. return SUCCESS;
  106. }
  107. bool ReplaceWithEmptyConstPass::IsEmptyTenor(const GeShape &shape) const {
  108. for (auto dim : shape.GetDims()) {
  109. if (dim == 0) {
  110. return true;
  111. }
  112. }
  113. return false;
  114. }
  115. } // namespace ge

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