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

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

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