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.

cast_remove_pass.cc 5.7 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/cast_remove_pass.h"
  17. #include <string>
  18. #include <vector>
  19. #include "framework/common/debug/ge_log.h"
  20. #include "common/transop_util.h"
  21. #include "graph/debug/ge_attr_define.h"
  22. #include "graph/utils/type_utils.h"
  23. namespace ge {
  24. Status CastRemovePass::Run(NodePtr &node) {
  25. if (node == nullptr) {
  26. REPORT_INNER_ERROR("E19999", "Param node is nullptr, check invalid");
  27. GELOGE(PARAM_INVALID, "[Check][Param] Param [node] must not be null.");
  28. return PARAM_INVALID;
  29. }
  30. OpDescPtr op_desc = node->GetOpDesc();
  31. if (op_desc == nullptr) {
  32. REPORT_INNER_ERROR("E19999", "Param op_desc of node is nullptr, check invalid");
  33. GELOGE(PARAM_INVALID, "[Get][OpDesc] OpDesc of param [node] must not be null.");
  34. return PARAM_INVALID;
  35. }
  36. // begin with not trans op, and only has one out data anchor
  37. if (TransOpUtil::IsTransOp(node) || node->GetAllOutDataAnchorsSize() != 1) {
  38. return SUCCESS;
  39. }
  40. std::vector<NodePtr> nodes_to_fuse;
  41. NodePtr end_node = GetTheEndNode(node, nodes_to_fuse);
  42. if (nodes_to_fuse.empty()) {
  43. return SUCCESS;
  44. }
  45. OpDescPtr end_op_desc = end_node->GetOpDesc();
  46. if (end_op_desc == nullptr) {
  47. REPORT_INNER_ERROR("E19999", "op_desc of end_node is nullptr, check invalid");
  48. GELOGE(PARAM_INVALID, "[Get][OpDesc] OpDesc of end node must not be null.");
  49. return PARAM_INVALID;
  50. }
  51. if (!CheckPrecisionLoss(nodes_to_fuse)) {
  52. return SUCCESS;
  53. }
  54. DataType type = DT_UNDEFINED;
  55. if (!HasSameDataType(op_desc, end_op_desc, type)) {
  56. return SUCCESS;
  57. }
  58. if (RemoveCast(type, nodes_to_fuse) != SUCCESS) {
  59. return FAILED;
  60. }
  61. return SUCCESS;
  62. }
  63. bool CastRemovePass::CheckPrecisionLoss(const std::vector<NodePtr> &nodes_to_fuse) {
  64. for (const NodePtr &node : nodes_to_fuse) {
  65. if (!TransOpUtil::CheckPrecisionLoss(node)) {
  66. return false;
  67. }
  68. }
  69. return true;
  70. }
  71. bool CastRemovePass::HasSameDataType(OpDescPtr &begin_op_desc, OpDescPtr &end_op_desc, DataType &type) const {
  72. if (begin_op_desc->GetName() == end_op_desc->GetName()) {
  73. return false;
  74. }
  75. auto end_out_desc = end_op_desc->MutableOutputDesc(0);
  76. DataType end_out_datatype = end_out_desc->GetDataType();
  77. auto begin_out_desc = begin_op_desc->MutableOutputDesc(0);
  78. DataType begin_out_datatype = begin_out_desc->GetDataType();
  79. if (begin_out_datatype == end_out_datatype && (begin_out_datatype == DT_FLOAT16 || begin_out_datatype == DT_FLOAT)) {
  80. type = begin_out_datatype;
  81. return true;
  82. }
  83. return false;
  84. }
  85. // op1->TransData->Cast->TransposeD->Cast->TransData->op2
  86. // change to be
  87. // op1->TransData->TransposeD->TransData->op2
  88. Status CastRemovePass::RemoveCast(DataType &type, std::vector<NodePtr> &nodes_to_fuse) {
  89. string cast_name;
  90. for (NodePtr &node : nodes_to_fuse) {
  91. if (node->GetType() == CAST) {
  92. GELOGI("CastRemovePass, remove Cast %s.", node->GetName().c_str());
  93. cast_name = node->GetName();
  94. if (IsolateAndDeleteNode(node, {0}) != SUCCESS) {
  95. REPORT_CALL_ERROR("E19999", "Isolate and delete node:%s(%s) failed",
  96. node->GetName().c_str(), node->GetType().c_str());
  97. GELOGE(FAILED, "[IsolateAndDelete][Node] %s failed.", node->GetName().c_str());
  98. return FAILED;
  99. }
  100. }
  101. }
  102. if (cast_name.empty()) {
  103. return SUCCESS;
  104. }
  105. for (auto &node : nodes_to_fuse) {
  106. if (node->GetType() == CAST) {
  107. continue;
  108. }
  109. OpDescPtr op_desc = node->GetOpDesc();
  110. if (op_desc == nullptr) {
  111. REPORT_INNER_ERROR("E19999", "Find nullptr op_desc in node, check invalid");
  112. GELOGE(FAILED, "[Get][OpDesc] OpDesc must not be null.");
  113. return FAILED;
  114. }
  115. // change node name for recompile cache, will be abandoned in April
  116. string new_node_name = cast_name + op_desc->GetName();
  117. op_desc->SetName(new_node_name);
  118. // add attr to changed TransData, then will be rebuild
  119. if (!AttrUtils::SetBool(op_desc, ATTR_NEED_COMPILE, true)) {
  120. REPORT_CALL_ERROR("E19999", "Set Attr:%s of op:%s(%s) failed",
  121. ATTR_NEED_COMPILE.c_str(),
  122. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  123. GELOGE(FAILED, "[Set][Attr] %s of op:%s(%s) failed", ATTR_NEED_COMPILE.c_str(),
  124. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  125. return FAILED;
  126. }
  127. auto in_desc = op_desc->MutableInputDesc(0);
  128. auto out_desc = op_desc->MutableOutputDesc(0);
  129. in_desc->SetDataType(type);
  130. out_desc->SetDataType(type);
  131. GELOGI("CastRemovePass, change %s %s datatype to be %s.", node->GetType().c_str(), node->GetName().c_str(),
  132. TypeUtils::DataTypeToSerialString(type).c_str());
  133. }
  134. return SUCCESS;
  135. }
  136. NodePtr CastRemovePass::GetTheEndNode(NodePtr begin_node, std::vector<NodePtr> &nodes_to_fuse) {
  137. while (begin_node->GetOutDataNodes().size() == 1) {
  138. auto out_node = begin_node->GetOutDataNodes().at(0);
  139. if (!TransOpUtil::IsTransOp(out_node)) {
  140. return begin_node; // when seen not trans op
  141. }
  142. begin_node = out_node;
  143. nodes_to_fuse.emplace_back(begin_node);
  144. }
  145. return begin_node; // when seen branch
  146. }
  147. } // namespace ge

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