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.

visit.cc 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
  3. *
  4. * Copyright 2019 Huawei Technologies Co., Ltd
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "backend/optimizer/common/visit.h"
  19. #include <vector>
  20. #include <memory>
  21. #include <algorithm>
  22. #include "backend/optimizer/common/pattern_engine.h"
  23. #include "utils/any.h"
  24. #include "ir/anf.h"
  25. #include "ir/func_graph.h"
  26. #include "utils/log_adapter.h"
  27. /* namespace to support utils definition */
  28. namespace mindspore {
  29. bool CheckIfNeedExpand(const std::vector<BaseRef> &list) {
  30. return std::any_of(list.begin(), list.end(), [](const BaseRef &any) { return utils::isa<Seq>(any); });
  31. }
  32. std::shared_ptr<VectorRef> ExpandList(const std::vector<BaseRef> &list) {
  33. std::shared_ptr<VectorRef> new_list = std::make_shared<VectorRef>();
  34. for (auto &item : list) {
  35. if (utils::isa<Seq>(item)) {
  36. const Seq &seq = utils::cast<Seq>(item);
  37. new_list->insert(new_list->end(), seq.begin(), seq.end());
  38. } else {
  39. new_list->push_back(item);
  40. }
  41. }
  42. return new_list;
  43. }
  44. bool DefaultVisitor::Visit(const VectorRef &v_any, BaseRef *const visit_out) const {
  45. std::vector<BaseRef> out;
  46. (void)std::transform(v_any.begin(), v_any.end(), std::back_inserter(out),
  47. [this](const BaseRef &item) { return fn_(item); });
  48. if (visit_out != nullptr) {
  49. *visit_out = ExpandList(out);
  50. }
  51. return true;
  52. }
  53. bool DefaultVisitor::Visit(const BaseRef &any, BaseRef *const visit_out) const {
  54. if (utils::isa<Seq>(any)) {
  55. return Visit(utils::cast<Seq>(any), visit_out);
  56. } else if (utils::isa<AnfNodePtr>(any)) {
  57. auto nodeptr = utils::cast<AnfNodePtr>(any);
  58. AnfNodePtr output;
  59. AnfNodePtr *p_output = &output;
  60. if (visit_out == nullptr) {
  61. p_output = nullptr;
  62. }
  63. Visit(nodeptr, fn_, p_output);
  64. if (visit_out != nullptr) {
  65. *visit_out = output;
  66. }
  67. return true;
  68. }
  69. MS_LOG(DEBUG) << "VisitError, not support type to Visit: " + any.ToString();
  70. return false;
  71. }
  72. void DefaultVisitor::Visit(const AnfNodePtr &node, const VisitFn &fn, AnfNodePtr *output) const {
  73. if (node->isa<CNode>()) {
  74. Visit(node->cast<CNodePtr>(), fn, output);
  75. return;
  76. }
  77. if (node->isa<ValueNode>()) {
  78. Visit(node->cast<ValueNodePtr>(), fn, output);
  79. return;
  80. }
  81. if (output != nullptr) {
  82. *output = node;
  83. }
  84. }
  85. void DefaultVisitor::Visit(const CNodePtr &cnode, const VisitFn &fn, AnfNodePtr *output) const {
  86. // if output is nullptr, it's not required to make the new CNode node.
  87. if (output == nullptr) {
  88. for (auto &inp : cnode->inputs()) {
  89. (void)fn(inp);
  90. }
  91. if (cnode->func_graph() != nullptr) {
  92. (void)fn(cnode->func_graph());
  93. } else {
  94. (void)fn(cnode->func_graph_as_var());
  95. }
  96. return;
  97. }
  98. std::vector<AnfNodePtr> new_inputs;
  99. std::vector<BaseRef> after_cnode_fn;
  100. std::shared_ptr<VectorRef> out;
  101. (void)std::transform(cnode->inputs().begin(), cnode->inputs().end(), std::back_inserter(after_cnode_fn), fn);
  102. if (CheckIfNeedExpand(after_cnode_fn)) {
  103. out = ExpandList(after_cnode_fn);
  104. }
  105. std::vector<BaseRef> &outs = after_cnode_fn;
  106. if (out != nullptr) {
  107. outs = out->elements();
  108. }
  109. for (auto &any_item : outs) {
  110. if (!utils::isa<AnfNodePtr>(any_item)) {
  111. MS_LOG(EXCEPTION) << "VisitError, fn not return the same type AnfNodePtr";
  112. }
  113. new_inputs.push_back(utils::cast<AnfNodePtr>(any_item));
  114. }
  115. BaseRef any_fg;
  116. AnfNodePtr new_cnode = nullptr;
  117. if (cnode->func_graph() != nullptr) {
  118. any_fg = fn(cnode->func_graph());
  119. if (!utils::isa<FuncGraphPtr>(any_fg)) {
  120. MS_LOG(EXCEPTION) << "VisitError, fn not return the same type FuncGraphPtr";
  121. }
  122. new_cnode = std::make_shared<CNode>(new_inputs, utils::cast<FuncGraphPtr>(any_fg));
  123. } else {
  124. any_fg = fn(cnode->func_graph_as_var());
  125. if (utils::isa<VarPtr>(any_fg)) {
  126. new_cnode = std::make_shared<CNode>(new_inputs, utils::cast<VarPtr>(any_fg));
  127. } else if (utils::isa<FuncGraphPtr>(any_fg)) {
  128. new_cnode = std::make_shared<CNode>(new_inputs, utils::cast<FuncGraphPtr>(any_fg));
  129. } else {
  130. MS_LOG(EXCEPTION) << "VisitError, fn not return VarPtr or FuncGraphPtr";
  131. }
  132. }
  133. new_cnode->set_abstract(cnode->abstract());
  134. *output = new_cnode;
  135. }
  136. void DefaultVisitor::Visit(const ValueNodePtr &vnode, const VisitFn &fn, AnfNodePtr *output) const {
  137. const BaseRef &value = utils::cast<ValuePtr>(fn(vnode->value()));
  138. if (utils::isa<ValuePtr>(value)) {
  139. if (output != nullptr) {
  140. auto ct = NewValueNode(utils::cast<ValuePtr>(value));
  141. ct->set_abstract(vnode->abstract());
  142. *output = ct;
  143. }
  144. return;
  145. }
  146. MS_LOG(EXCEPTION) << "Visit result is not ValuePtr.";
  147. }
  148. } // namespace mindspore