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.

switch_dead_branch_elimination.cc 6.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/switch_dead_branch_elimination.h"
  17. #include <string>
  18. #include <vector>
  19. #include "framework/common/debug/ge_log.h"
  20. #include "graph/common/omg_util.h"
  21. #include "graph/passes/pass_utils.h"
  22. #include "graph/utils/graph_utils.h"
  23. namespace ge {
  24. namespace {
  25. const std::vector<int>::size_type kDataInputIndex = 0;
  26. const std::vector<int>::size_type kPredInputIndex = 1;
  27. const int kDefaultInputIndex = -1;
  28. bool ParsePred(const ConstGeTensorPtr &tensor) {
  29. if (tensor == nullptr) {
  30. GELOGE(FAILED, "parameter is null.");
  31. return false;
  32. }
  33. const uint8_t *data_ptr = tensor->GetData().data();
  34. auto type = tensor->GetTensorDesc().GetDataType();
  35. switch (type) {
  36. case DT_BOOL:
  37. return *reinterpret_cast<const bool *>(data_ptr);
  38. case DT_FLOAT:
  39. return static_cast<bool>(*reinterpret_cast<const float *>(data_ptr));
  40. case DT_DOUBLE:
  41. return static_cast<bool>(*reinterpret_cast<const double *>(data_ptr));
  42. case DT_INT8:
  43. case DT_UINT8:
  44. return static_cast<bool>(*data_ptr);
  45. case DT_FLOAT16:
  46. case DT_INT16:
  47. case DT_UINT16:
  48. return static_cast<bool>(*reinterpret_cast<const int16_t *>(data_ptr));
  49. case DT_INT32:
  50. case DT_UINT32:
  51. return static_cast<bool>(*reinterpret_cast<const int32_t *>(data_ptr));
  52. case DT_INT64:
  53. case DT_UINT64:
  54. return static_cast<bool>(*reinterpret_cast<const int64_t *>(data_ptr));
  55. default:
  56. return static_cast<bool>(*data_ptr);
  57. }
  58. }
  59. bool ParseOutDataAnchors(const NodePtr &node, const NodePtr &pred_node, OutDataAnchorPtr &active_out_data_anchor,
  60. OutDataAnchorPtr &inactive_out_data_anchor) {
  61. auto tensors = OpDescUtils::MutableWeights(pred_node);
  62. if (tensors.empty()) {
  63. return false;
  64. }
  65. bool pred_value = ParsePred(tensors[0]);
  66. int inactive_output_index = pred_value ? 0 : 1;
  67. if (node == nullptr) {
  68. GELOGE(FAILED, "parameter is null.");
  69. return false;
  70. }
  71. GELOGI("[%s] Inactive output index = %d", node->GetName().c_str(), inactive_output_index);
  72. for (const auto &out_anchor : node->GetAllOutDataAnchors()) {
  73. if (out_anchor->GetIdx() == inactive_output_index) {
  74. inactive_out_data_anchor = out_anchor;
  75. } else {
  76. active_out_data_anchor = out_anchor;
  77. }
  78. }
  79. return true;
  80. }
  81. } // namespace
  82. Status SwitchDeadBranchElimination::DeleteSwitchNode(NodePtr &node, NodePtr &pred_node,
  83. const OutDataAnchorPtr &active_out_data_anchor) {
  84. if (node == nullptr || active_out_data_anchor == nullptr) {
  85. GELOGE(FAILED, "parameter is null.");
  86. return FAILED;
  87. }
  88. // If two nodes aren't in same graph, get node's direct in_node instead of pred_node.
  89. if (node->GetOwnerComputeGraph() != pred_node->GetOwnerComputeGraph()) {
  90. pred_node = PassUtils::GetInDataNode(node, kPredInputIndex);
  91. }
  92. // link pred's in control nodes to switch
  93. if (GraphUtils::CopyInCtrlEdges(pred_node, node) != GRAPH_SUCCESS) {
  94. return FAILED;
  95. }
  96. // Remove link between pred and switch
  97. auto in_pred_anchor = node->GetInDataAnchor(kPredInputIndex);
  98. GE_CHECK_NOTNULL(in_pred_anchor);
  99. in_pred_anchor->UnlinkAll();
  100. /// If condition Const is isolate, it will be delete with pruning
  101. /// Isolate Switch and delete it
  102. std::vector<int> switch_io_map = {kDefaultInputIndex, kDefaultInputIndex};
  103. size_t out_index = static_cast<size_t>(active_out_data_anchor->GetIdx());
  104. if (out_index >= switch_io_map.size()) {
  105. GELOGE(FAILED, "[%s] out index check failed, out_index:%zu.", node->GetName().c_str(), out_index);
  106. return FAILED;
  107. }
  108. switch_io_map[out_index] = kDataInputIndex;
  109. return IsolateAndDeleteNode(node, switch_io_map);
  110. }
  111. Status SwitchDeadBranchElimination::Run(NodePtr &node) {
  112. if (node == nullptr) {
  113. GELOGE(PARAM_INVALID, "Param [node] must not be null.");
  114. return PARAM_INVALID;
  115. }
  116. std::string op_type;
  117. GE_CHK_STATUS_RET(GetOriginalType(node, op_type), "Get original type failed");
  118. if ((op_type != SWITCH) && (op_type != REFSWITCH)) {
  119. return SUCCESS;
  120. }
  121. if (node->GetOutAllNodes().empty()) {
  122. return SUCCESS;
  123. }
  124. auto pred_node = PassUtils::GetInNodeCrossSubgraphByIndex(node, kPredInputIndex);
  125. if (pred_node == nullptr) {
  126. GELOGD("[%s] Pred input is null.", node->GetName().c_str());
  127. return SUCCESS;
  128. }
  129. // Can be optimized when pred is constant
  130. if (!PassUtils::IsConstant(pred_node)) {
  131. GELOGD("[%s] Pred is not constant.", node->GetName().c_str());
  132. return SUCCESS;
  133. }
  134. auto input_node = PassUtils::GetInNodeCrossSubgraphByIndex(node, kDataInputIndex);
  135. if (input_node == nullptr) {
  136. GELOGD("[%s] Data input is null.", node->GetName().c_str());
  137. return SUCCESS;
  138. }
  139. // Get active & inactive output anchors by the value of pred
  140. OutDataAnchorPtr active_out_data_anchor = nullptr;
  141. OutDataAnchorPtr inactive_out_data_anchor = nullptr;
  142. if (!ParseOutDataAnchors(node, pred_node, active_out_data_anchor, inactive_out_data_anchor)) {
  143. return PARAM_INVALID;
  144. }
  145. if (inactive_out_data_anchor != nullptr) {
  146. GELOGI("[%s] To unlink inactive output %d", node->GetName().c_str(), inactive_out_data_anchor->GetIdx());
  147. std::vector<NodePtr> del_nodes;
  148. std::vector<NodePtr> end_nodes;
  149. Status ret = PassUtils::RemoveInactiveBranchToMerge(inactive_out_data_anchor, del_nodes, end_nodes);
  150. if (ret != SUCCESS) {
  151. return ret;
  152. }
  153. for (auto &end_node : end_nodes) {
  154. AddRePassNode(end_node);
  155. }
  156. for (const auto &delete_node : del_nodes) {
  157. AddNodeDeleted(delete_node);
  158. }
  159. }
  160. return DeleteSwitchNode(node, pred_node, active_out_data_anchor);
  161. }
  162. } // namespace ge

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