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.8 kB

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

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