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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. GELOGI("NoUseReshapeRemovePass remove useless node:%s", node->GetName().c_str());
  78. auto ret = PassUtils::UnlinkNodeWithControlCopy(node, kReshapeShapeIndex);
  79. if (ret != SUCCESS) {
  80. GELOGE(ret, "DimensionAdjustPass unlink node with control copy fail.");
  81. return ret;
  82. }
  83. return IsolateAndDeleteNode(node, {kReshapeDataIndex});
  84. }
  85. return SUCCESS;
  86. }
  87. } // namespace ge

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