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.

dynamic_single_op_reset_shape_pass.cc 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/dynamic_single_op_reset_shape_pass.h"
  17. #include "common/ge_inner_error_codes.h"
  18. #include "graph/utils/node_utils.h"
  19. #include "graph/utils/graph_utils.h"
  20. #include "graph/utils/tensor_utils.h"
  21. #include "graph/utils/op_desc_utils.h"
  22. #include "graph/utils/type_utils.h"
  23. #include "graph/debug/ge_attr_define.h"
  24. namespace ge {
  25. namespace {
  26. const int64_t kDynamicShapeDim = -2;
  27. const char *const kEngineNameAiCpu = "DNN_VM_AICPU_ASCEND";
  28. const char *const kEngineNameAiCpuTf = "DNN_VM_AICPU";
  29. } // namespace
  30. Status DynamicSingleOpResetShapePass::Run(ComputeGraphPtr graph) {
  31. GE_CHECK_NOTNULL(graph);
  32. std::shared_ptr<GELib> instance = ge::GELib::GetInstance();
  33. if (instance == nullptr || !instance->InitFlag()) {
  34. GELOGE(ge::GE_CLI_GE_NOT_INITIALIZED, "Run CompileNodesPass failed.");
  35. return ge::GE_CLI_GE_NOT_INITIALIZED;
  36. }
  37. // pass if graph has not aicpu node.
  38. bool is_not_aicpu = false;
  39. if (CheckAllAicpuNodes(graph, is_not_aicpu) != SUCCESS) {
  40. GELOGE(ge::GE_CLI_GE_NOT_INITIALIZED, "Check if graph has not aicpu node failed.");
  41. return ge::GE_CLI_GE_NOT_INITIALIZED;
  42. }
  43. if (is_not_aicpu) {
  44. GELOGI("The graph [%s] has not aicpu node, whose aicpu nodes would not be reset dynamic shape",
  45. graph->GetName().c_str());
  46. return SUCCESS;
  47. }
  48. for (const auto &node : graph->GetDirectNode()) {
  49. GE_CHECK_NOTNULL(node->GetOpDesc());
  50. // pass input and output node
  51. if (node->GetType() == DATA || node->GetType() == CONSTANT || node->GetType() == CONSTANTOP ||
  52. node->GetType() == NETOUTPUT) {
  53. continue;
  54. }
  55. // pass node without attr: ATTR_DYNAMIC_SHAPE_SINGLE_AICPU
  56. bool single_aicpu_unknown = false;
  57. if (!AttrUtils::GetBool(node->GetOpDesc(), ATTR_DYNAMIC_SHAPE_SINGLE_AICPU, single_aicpu_unknown) ||
  58. !single_aicpu_unknown) {
  59. continue;
  60. }
  61. // reset aicpu shape to unknown shape
  62. auto op_desc = node->GetOpDesc();
  63. if (ResetOpShape(op_desc) != SUCCESS) {
  64. GELOGE(ge::GE_CLI_GE_NOT_INITIALIZED, "Reset node[%s] dynamic shapr failed.", node->GetName().c_str());
  65. return ge::GE_CLI_GE_NOT_INITIALIZED;
  66. }
  67. GELOGD("Reset dynamic aicpu node [%s] shape success!", node->GetName().c_str());
  68. }
  69. GELOGD("Reset dynamic aicpu nodes shape of graph [%s] success!", graph->GetName().c_str());
  70. return SUCCESS;
  71. }
  72. Status DynamicSingleOpResetShapePass::CheckAllAicpuNodes(const ComputeGraphPtr &graph, bool &is_not_aicpu) {
  73. is_not_aicpu = false;
  74. for (const auto &node : graph->GetDirectNode()) {
  75. GE_CHECK_NOTNULL(node->GetOpDesc());
  76. // pass input and output node
  77. if (node->GetType() == DATA || node->GetType() == CONSTANT || node->GetType() == CONSTANTOP ||
  78. node->GetType() == NETOUTPUT) {
  79. continue;
  80. }
  81. // find if there are aicpu nodes.
  82. auto op_desc = node->GetOpDesc();
  83. string engine_name = op_desc->GetOpEngineName();
  84. if (engine_name.empty()) {
  85. GELOGE(GRAPH_FAILED, "Get engine failed of node[%s].", node->GetName().c_str());
  86. return GRAPH_FAILED;
  87. }
  88. if (engine_name != kEngineNameAiCpu && engine_name != kEngineNameAiCpuTf) {
  89. is_not_aicpu = true;
  90. return SUCCESS;
  91. }
  92. }
  93. return SUCCESS;
  94. }
  95. bool DynamicSingleOpResetShapePass::CheckIfConstInput(const GeTensorDescPtr &input_tensor_desc) {
  96. bool is_const = false;
  97. (void)AttrUtils::GetBool(input_tensor_desc, CONST_ATTR_NAME_INPUT, is_const);
  98. return is_const;
  99. }
  100. Status DynamicSingleOpResetShapePass::ResetOpShape(OpDescPtr &op_desc) {
  101. GE_CHECK_NOTNULL(op_desc);
  102. std::vector<int64_t> dynamic_shape_dims = {kDynamicShapeDim};
  103. GeShape dynamic_shape(dynamic_shape_dims);
  104. (void)ResetInputTensorShape(op_desc, dynamic_shape);
  105. (void)ResetOutputTensorShape(op_desc, dynamic_shape);
  106. return SUCCESS;
  107. }
  108. Status DynamicSingleOpResetShapePass::ResetInputTensorShape(OpDescPtr &op_desc,
  109. const GeShape &dynamic_shape) {
  110. GE_CHECK_NOTNULL(op_desc);
  111. for (size_t i = 0; i < op_desc->GetAllInputsDesc().size(); i++) {
  112. auto input_desc = op_desc->MutableInputDesc(static_cast<uint32_t>(i));
  113. GE_CHECK_NOTNULL(input_desc);
  114. // pass scalar input desc
  115. auto dims_ori = input_desc->GetShape().GetDims();
  116. if (dims_ori.size() == 0) {
  117. continue;
  118. }
  119. // pass const input
  120. if (CheckIfConstInput(input_desc)) {
  121. continue;
  122. }
  123. input_desc->SetShape(dynamic_shape);
  124. }
  125. return SUCCESS;
  126. }
  127. Status DynamicSingleOpResetShapePass::ResetOutputTensorShape(OpDescPtr &op_desc, const GeShape &dynamic_shape) {
  128. GE_CHECK_NOTNULL(op_desc);
  129. for (size_t i = 0; i < op_desc->GetAllOutputsDesc().size(); i++) {
  130. auto output_desc = op_desc->MutableOutputDesc(static_cast<uint32_t>(i));
  131. GE_CHECK_NOTNULL(output_desc);
  132. // pass scalar input desc
  133. auto output_dims_ori = output_desc->GetShape().GetDims();
  134. if (output_dims_ori.size() == 0) {
  135. continue;
  136. }
  137. output_desc->SetShape(dynamic_shape);
  138. }
  139. return SUCCESS;
  140. }
  141. } // namespace ge

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