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.

graph_transform.cc 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "frontend/optimizer/graph_transform.h"
  17. #include <vector>
  18. #include <algorithm>
  19. #include <string>
  20. #include "ir/graph_utils.h"
  21. namespace mindspore {
  22. /* namespace to support opt */
  23. namespace opt {
  24. // check cnode input values, whether it is tuple input
  25. bool CNodeHasTupleInput(const CNodePtr &cnode) {
  26. auto &inputs = cnode->inputs();
  27. for (size_t i = 1; i < inputs.size(); i++) {
  28. if (IsValueNode<FuncGraph>(inputs[i])) {
  29. continue;
  30. }
  31. if (IsValueNode<Primitive>(inputs[i])) {
  32. // unexpected high order primitvie as cnode input when transform graph
  33. MS_LOG(WARNING) << "CheckTupleInput, got unexpected primitve as input" << cnode->DebugString();
  34. return false;
  35. }
  36. auto abs = inputs[i]->abstract();
  37. if (abs == nullptr) {
  38. MS_LOG(WARNING) << "CheckTupleInput, got abstract nullptr for node:" << cnode->DebugString();
  39. return false;
  40. }
  41. if (abs->isa<abstract::AbstractTuple>()) {
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. bool FuncGraphHasTupleInput(const FuncGraphPtr &fg) {
  48. auto &params = fg->parameters();
  49. for (auto &param : params) {
  50. if (param->abstract()->isa<abstract::AbstractTuple>()) {
  51. return true;
  52. }
  53. }
  54. return false;
  55. }
  56. std::vector<AnfNodePtr> TransformTupleArgument(const FuncGraphPtr &fg, const AnfNodePtr &node,
  57. const abstract::AbstractTuplePtr &abs) {
  58. auto &elements = abs->elements();
  59. std::vector<AnfNodePtr> tuple_node_expanded;
  60. for (size_t i = 0; i < elements.size(); i++) {
  61. auto elem_node = fg->NewCNode({NewValueNode(prim::kPrimTupleGetItem), node, NewValueNode(SizeToInt(i))});
  62. elem_node->set_abstract(elements[i]);
  63. if (elements[i]->isa<abstract::AbstractTuple>()) {
  64. auto nodes = TransformTupleArgument(fg, elem_node, elements[i]->cast<abstract::AbstractTuplePtr>());
  65. tuple_node_expanded.insert(tuple_node_expanded.end(), nodes.begin(), nodes.end());
  66. } else {
  67. tuple_node_expanded.push_back(elem_node);
  68. }
  69. }
  70. return tuple_node_expanded;
  71. }
  72. AnfNodePtr TransformCallGraph(const FuncGraphPtr &trans_fg, const CNodePtr &cnode) {
  73. auto &cinputs = cnode->inputs();
  74. auto fg = cnode->func_graph();
  75. std::vector<AnfNodePtr> inputs;
  76. inputs.push_back(NewValueNode(trans_fg));
  77. for (size_t i = 1; i < cinputs.size(); i++) {
  78. auto abs = cinputs[i]->abstract();
  79. if (abs == nullptr) {
  80. MS_LOG(EXCEPTION) << "TransformCallGraph:Node abstract should not be nullptr" << cinputs[i]->DebugString();
  81. }
  82. if (abs->isa<abstract::AbstractTuple>()) {
  83. auto nodes = TransformTupleArgument(fg, cinputs[i], abs->cast<abstract::AbstractTuplePtr>());
  84. inputs.insert(inputs.end(), nodes.begin(), nodes.end());
  85. } else {
  86. inputs.push_back(cinputs[i]);
  87. }
  88. }
  89. auto new_node = fg->NewCNode(inputs);
  90. new_node->set_abstract(cnode->abstract());
  91. return new_node;
  92. }
  93. AnfNodePtr TransformPartial(const FuncGraphPtr &trans_fg, const CNodePtr &cnode) {
  94. auto &cinputs = cnode->inputs();
  95. auto fg = cnode->func_graph();
  96. std::vector<AnfNodePtr> inputs;
  97. inputs.push_back(NewValueNode(prim::kPrimPartial));
  98. inputs.push_back(NewValueNode(trans_fg));
  99. for (size_t i = 2; i < cinputs.size(); i++) {
  100. auto abs = cinputs[i]->abstract();
  101. if (abs == nullptr) {
  102. MS_LOG(EXCEPTION) << "TransformPartial:Node abstract should not be nullptr" << cinputs[i]->DebugString();
  103. }
  104. if (abs->isa<abstract::AbstractTuple>()) {
  105. auto nodes = TransformTupleArgument(fg, cinputs[i], abs->cast<abstract::AbstractTuplePtr>());
  106. inputs.insert(inputs.end(), nodes.begin(), nodes.end());
  107. } else {
  108. inputs.push_back(cinputs[i]);
  109. }
  110. }
  111. auto new_node = fg->NewCNode(inputs);
  112. new_node->set_abstract(cnode->abstract());
  113. return new_node;
  114. }
  115. AnfNodePtr TransformSwitchCall(const AnfNodePtr &swtich_node, const CNodePtr &cnode) {
  116. auto &cinputs = cnode->inputs();
  117. auto fg = cnode->func_graph();
  118. std::vector<AnfNodePtr> inputs;
  119. inputs.push_back(swtich_node);
  120. for (size_t i = 1; i < cinputs.size(); i++) {
  121. auto abs = cinputs[i]->abstract();
  122. if (abs == nullptr) {
  123. MS_LOG(EXCEPTION) << "TransformSwitchCall:Node abstract should not be nullptr" << cinputs[i]->DebugString();
  124. }
  125. if (abs->isa<abstract::AbstractTuple>()) {
  126. auto nodes = TransformTupleArgument(fg, cinputs[i], abs->cast<abstract::AbstractTuplePtr>());
  127. inputs.insert(inputs.end(), nodes.begin(), nodes.end());
  128. } else {
  129. inputs.push_back(cinputs[i]);
  130. }
  131. }
  132. auto new_node = fg->NewCNode(inputs);
  133. new_node->set_abstract(cnode->abstract());
  134. return new_node;
  135. }
  136. } // namespace opt
  137. } // namespace mindspore