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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. GELOGE(PARAM_INVALID, "NoUseReshapeRemovePass enter. OpDesc is null.");
  37. return PARAM_INVALID;
  38. }
  39. if (op_desc_ptr->GetType() != RESHAPE) {
  40. return SUCCESS;
  41. }
  42. GELOGI("NoUseReshapeRemovePass enter.");
  43. bool to_be_deleted = true;
  44. // compare input and output dims
  45. if (op_desc_ptr->GetAllInputsDesc().empty() || op_desc_ptr->GetAllOutputsDesc().empty()) {
  46. GELOGE(INTERNAL_ERROR, "Input or output num is zero. node name:%s, input size:%zu, output size:%zu",
  47. op_desc_ptr->GetName().c_str(), op_desc_ptr->GetAllInputsDesc().size(),
  48. op_desc_ptr->GetAllOutputsDesc().size());
  49. return INTERNAL_ERROR;
  50. }
  51. const auto &input_desc = op_desc_ptr->MutableInputDesc(0);
  52. const auto &output_desc = op_desc_ptr->MutableOutputDesc(0);
  53. GE_CHECK_NOTNULL(input_desc);
  54. GE_CHECK_NOTNULL(output_desc);
  55. std::vector<int64_t> input_4dims = input_desc->GetShape().GetDims();
  56. std::vector<int64_t> output_4dims = output_desc->GetShape().GetDims();
  57. if (input_desc->GetShape().IsUnknownShape() || output_desc->GetShape().IsUnknownShape()) {
  58. GELOGI("Current Reshape %s is unknown shape which should be kept.", op_desc_ptr->GetName().c_str());
  59. return SUCCESS;
  60. }
  61. if (input_4dims.size() != output_4dims.size()) {
  62. GELOGI("Input and output dim size is not equal.Keep this reshape op.");
  63. return SUCCESS;
  64. }
  65. size_t vec_size = input_4dims.size();
  66. for (size_t i = 0; i < vec_size; i++) {
  67. if (input_4dims[i] < 0) {
  68. GELOGI("Input shape is unknown.Keep this reshape op.");
  69. return SUCCESS;
  70. }
  71. if (input_4dims[i] != output_4dims[i]) {
  72. to_be_deleted = false;
  73. break;
  74. }
  75. }
  76. if (to_be_deleted) {
  77. auto ret = TryRemoveConstShapeInput(node);
  78. GE_CHK_STATUS_RET_NOLOG(ret);
  79. GELOGI("NoUseReshapeRemovePass remove useless reshape node:%s", node->GetName().c_str());
  80. return IsolateAndDeleteNode(node, {kReshapeDataIndex});
  81. }
  82. return SUCCESS;
  83. }
  84. Status NoUseReshapeRemovePass::TryRemoveConstShapeInput(ge::NodePtr &reshape_node) {
  85. auto shape_input_anchor = reshape_node->GetInDataAnchor(kReshapeShapeIndex);
  86. if (shape_input_anchor == nullptr) {
  87. return SUCCESS;
  88. }
  89. GE_CHECK_NOTNULL(shape_input_anchor->GetPeerOutAnchor());
  90. auto shape_input = shape_input_anchor->GetPeerOutAnchor()->GetOwnerNode();
  91. GE_CHECK_NOTNULL(shape_input);
  92. if (shape_input->GetType() != CONSTANT && shape_input->GetType() != CONSTANTOP) {
  93. return SUCCESS;
  94. }
  95. // op(x) const(shape)
  96. // \ /
  97. // reshape
  98. // const input can unlink but should copy control_dependency
  99. auto ret = PassUtils::UnlinkNodeWithControlCopy(reshape_node, kReshapeShapeIndex);
  100. if (ret != SUCCESS) {
  101. GELOGE(ret, "Unlink node %s with control copy failed.", shape_input->GetName().c_str());
  102. return ret;
  103. }
  104. // remove const without any data_output
  105. if (shape_input->GetOutDataNodesSize() == 0) {
  106. auto ret = IsolateAndDeleteNode(shape_input, {});
  107. GE_CHK_GRAPH_STATUS_RET(ret, "Fail to remove node %s", shape_input->GetName().c_str());
  108. GELOGI("Remove useless shape input const %s.", shape_input->GetName().c_str());
  109. }
  110. return SUCCESS;
  111. }
  112. } // namespace ge

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