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.

reshape_recovery_pass.cc 4.9 kB

4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/reshape_recovery_pass.h"
  17. #include "common/ge/ge_util.h"
  18. namespace ge {
  19. namespace {
  20. NodePtr CreateReshape(const ConstGeTensorDescPtr &src, const ConstGeTensorDescPtr &dst, const ComputeGraphPtr &graph) {
  21. static std::atomic_long reshape_num(0);
  22. auto next_num = reshape_num.fetch_add(1);
  23. auto reshape = MakeShared<OpDesc>("Reshape_ReshapeRecoveryPass_" + std::to_string(next_num), RESHAPE);
  24. if (reshape == nullptr) {
  25. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  26. return nullptr;
  27. }
  28. auto ret = reshape->AddInputDesc("x", *src);
  29. if (ret != GRAPH_SUCCESS) {
  30. REPORT_CALL_ERROR("E19999", "Add input desc to op:%s(%s) failed, name:x",
  31. reshape->GetName().c_str(), reshape->GetType().c_str());
  32. return nullptr;
  33. }
  34. ret = reshape->AddInputDesc("shape", GeTensorDesc(GeShape(), Format(), DT_INT32));
  35. if (ret != GRAPH_SUCCESS) {
  36. REPORT_CALL_ERROR("E19999", "Add input desc to op:%s(%s) failed, name:shape",
  37. reshape->GetName().c_str(), reshape->GetType().c_str());
  38. return nullptr;
  39. }
  40. ret = reshape->AddOutputDesc("y", *dst);
  41. if (ret != GRAPH_SUCCESS) {
  42. REPORT_CALL_ERROR("E19999", "Add input desc to op:%s(%s) failed, name:y",
  43. reshape->GetName().c_str(), reshape->GetType().c_str());
  44. return nullptr;
  45. }
  46. return graph->AddNode(reshape);
  47. }
  48. Status InsertReshapeIfNeed(const NodePtr &node) {
  49. GE_CHECK_NOTNULL(node);
  50. GE_CHECK_NOTNULL(node->GetOpDesc());
  51. for (auto src_anchor : node->GetAllOutDataAnchors()) {
  52. auto src_tensor = node->GetOpDesc()->GetOutputDescPtr(src_anchor->GetIdx());
  53. GE_CHECK_NOTNULL(src_tensor);
  54. for (auto dst_anchor : src_anchor->GetPeerInDataAnchors()) {
  55. auto dst_node = dst_anchor->GetOwnerNode();
  56. GELOGD("Try insert reshape between %s[%d] and %s[%d] to keep the shape continues",
  57. node->GetName().c_str(), src_anchor->GetIdx(), dst_node->GetName().c_str(), dst_anchor->GetIdx());
  58. GE_CHECK_NOTNULL(dst_node);
  59. GE_CHECK_NOTNULL(dst_node->GetOpDesc());
  60. auto dst_tensor = dst_node->GetOpDesc()->GetInputDescPtr(dst_anchor->GetIdx());
  61. GE_CHECK_NOTNULL(dst_tensor);
  62. bool is_dynamic = false;
  63. const auto &src_tensor_dims = src_tensor->GetShape().GetDims();
  64. const auto &dst_tensor_dims = dst_tensor->GetShape().GetDims();
  65. if ((std::any_of(src_tensor_dims.begin(), src_tensor_dims.end(), [](int64_t val) { return val < 0 ; }))
  66. || (std::any_of(dst_tensor_dims.begin(), dst_tensor_dims.end(), [](int64_t val) { return val < 0; }))) {
  67. GELOGD("No need to insert reshape node between %s nad %s.", node->GetName().c_str(),
  68. dst_node->GetName().c_str());
  69. is_dynamic = true;
  70. }
  71. bool is_need_insert_reshape = src_tensor_dims != dst_tensor_dims &&
  72. !is_dynamic;
  73. if (is_need_insert_reshape) {
  74. auto reshape = CreateReshape(src_tensor, dst_tensor, node->GetOwnerComputeGraph());
  75. GE_CHECK_NOTNULL(reshape);
  76. auto ret = GraphUtils::InsertNodeBetweenDataAnchors(src_anchor, dst_anchor, reshape);
  77. if (ret != GRAPH_SUCCESS) {
  78. REPORT_CALL_ERROR("E19999",
  79. "Insert node:%s(%s) between node:%s(%s)(out_index:%d) and node:%s(%s)(out_index:%d) failed",
  80. reshape->GetName().c_str(), reshape->GetType().c_str(),
  81. node->GetName().c_str(), node->GetType().c_str(), src_anchor->GetIdx(),
  82. dst_node->GetName().c_str(), dst_node->GetType().c_str(), dst_anchor->GetIdx());
  83. GELOGE(INTERNAL_ERROR, "Failed to insert reshape between node %s and %s",
  84. node->GetName().c_str(), dst_node->GetName().c_str());
  85. return INTERNAL_ERROR;
  86. }
  87. GELOGI("Insert reshape between %s and %s to keep the shape continues",
  88. node->GetName().c_str(), dst_node->GetName().c_str());
  89. }
  90. }
  91. }
  92. return SUCCESS;
  93. }
  94. } // namespace
  95. Status ReshapeRecoveryPass::Run(ComputeGraphPtr graph) {
  96. for (const auto &node : graph->GetDirectNode()) {
  97. auto ret = InsertReshapeIfNeed(node);
  98. if (ret != SUCCESS) {
  99. return ret;
  100. }
  101. }
  102. return SUCCESS;
  103. }
  104. } // namespace ge

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