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.

no_use_reshape_remove_pass.cc 5.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/no_use_reshape_remove_pass.h"
  17. #include <string>
  18. #include <vector>
  19. #include "common/op/ge_op_utils.h"
  20. #include "external/graph/types.h"
  21. #include "framework/common/debug/ge_log.h"
  22. #include "framework/common/ge_inner_error_codes.h"
  23. #include "graph/passes/pass_utils.h"
  24. #include "graph/utils/graph_utils.h"
  25. #include "graph/utils/op_desc_utils.h"
  26. #include "graph/utils/tensor_utils.h"
  27. namespace ge {
  28. namespace {
  29. const int kReshapeDataIndex = 0;
  30. const int kReshapeShapeIndex = 1;
  31. } // namespace
  32. Status NoUseReshapeRemovePass::Run(ge::NodePtr &node) {
  33. GE_CHECK_NOTNULL(node);
  34. OpDescPtr op_desc_ptr = node->GetOpDesc();
  35. if (op_desc_ptr == nullptr) {
  36. REPORT_INNER_ERROR("E19999", "Param node's op_desc is nullptr, check invalid");
  37. GELOGE(PARAM_INVALID, "[Check][Param] NoUseReshapeRemovePass enter. OpDesc is null.");
  38. return PARAM_INVALID;
  39. }
  40. if (op_desc_ptr->GetType() != RESHAPE) {
  41. return SUCCESS;
  42. }
  43. GELOGI("NoUseReshapeRemovePass enter.");
  44. bool to_be_deleted = true;
  45. // compare input and output dims
  46. if (op_desc_ptr->GetAllInputsDesc().empty() || op_desc_ptr->GetAllOutputsDesc().empty()) {
  47. REPORT_INNER_ERROR("E19999", "Input or Output desc num is zero in node:%s(%s), check invalid",
  48. op_desc_ptr->GetName().c_str(), op_desc_ptr->GetType().c_str());
  49. GELOGE(INTERNAL_ERROR, "[Check][Param] Input or output num is zero. node name:%s, input size:%zu, output size:%zu",
  50. op_desc_ptr->GetName().c_str(), op_desc_ptr->GetAllInputsDesc().size(),
  51. op_desc_ptr->GetAllOutputsDesc().size());
  52. return INTERNAL_ERROR;
  53. }
  54. const auto &input_desc = op_desc_ptr->MutableInputDesc(0);
  55. const auto &output_desc = op_desc_ptr->MutableOutputDesc(0);
  56. GE_CHECK_NOTNULL(input_desc);
  57. GE_CHECK_NOTNULL(output_desc);
  58. std::vector<int64_t> input_4dims = input_desc->GetShape().GetDims();
  59. std::vector<int64_t> output_4dims = output_desc->GetShape().GetDims();
  60. if (input_desc->GetShape().IsUnknownShape() || output_desc->GetShape().IsUnknownShape()) {
  61. GELOGI("Current Reshape %s is unknown shape which should be kept.", op_desc_ptr->GetName().c_str());
  62. return SUCCESS;
  63. }
  64. if (input_4dims.size() != output_4dims.size()) {
  65. GELOGI("Input and output dim size is not equal.Keep this reshape op.");
  66. return SUCCESS;
  67. }
  68. size_t vec_size = input_4dims.size();
  69. for (size_t i = 0; i < vec_size; i++) {
  70. if (input_4dims[i] < 0) {
  71. GELOGI("Input shape is unknown.Keep this reshape op.");
  72. return SUCCESS;
  73. }
  74. if (input_4dims[i] != output_4dims[i]) {
  75. to_be_deleted = false;
  76. break;
  77. }
  78. }
  79. if (to_be_deleted) {
  80. auto ret = TryRemoveConstShapeInput(node);
  81. GE_CHK_STATUS_RET_NOLOG(ret);
  82. GELOGI("NoUseReshapeRemovePass remove useless reshape node:%s", node->GetName().c_str());
  83. return IsolateAndDeleteNode(node, {kReshapeDataIndex});
  84. }
  85. return SUCCESS;
  86. }
  87. Status NoUseReshapeRemovePass::TryRemoveConstShapeInput(ge::NodePtr &reshape_node) {
  88. auto shape_input_anchor = reshape_node->GetInDataAnchor(kReshapeShapeIndex);
  89. if (shape_input_anchor == nullptr) {
  90. return SUCCESS;
  91. }
  92. GE_CHECK_NOTNULL(shape_input_anchor->GetPeerOutAnchor());
  93. auto shape_input = shape_input_anchor->GetPeerOutAnchor()->GetOwnerNode();
  94. GE_CHECK_NOTNULL(shape_input);
  95. if (shape_input->GetType() != CONSTANT && shape_input->GetType() != CONSTANTOP) {
  96. return SUCCESS;
  97. }
  98. // op(x) const(shape)
  99. // \ /
  100. // reshape
  101. // const input can unlink but should copy control_dependency
  102. auto ret = PassUtils::UnlinkNodeWithControlCopy(reshape_node, kReshapeShapeIndex);
  103. if (ret != SUCCESS) {
  104. REPORT_CALL_ERROR("E19999", "Unlink op:%s(%s) data input:%u with control edge copy failed",
  105. reshape_node->GetName().c_str(), reshape_node->GetType().c_str(), kReshapeShapeIndex);
  106. GELOGE(ret, "[Unlink][Node] %s(%s) data input:%u with control edge copy failed",
  107. reshape_node->GetName().c_str(), reshape_node->GetType().c_str(), kReshapeShapeIndex);
  108. return ret;
  109. }
  110. // remove const without any data_output
  111. if (shape_input->GetOutDataNodesSize() == 0) {
  112. auto ret = IsolateAndDeleteNode(shape_input, {});
  113. GE_CHK_GRAPH_STATUS_RET(ret, "[Remove][Node] %s failed", shape_input->GetName().c_str());
  114. GELOGI("Remove useless shape input const %s.", shape_input->GetName().c_str());
  115. }
  116. return SUCCESS;
  117. }
  118. } // namespace ge

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