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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "pre_activate/common/visit.h"
  19. #include <vector>
  20. #include <memory>
  21. #include <algorithm>
  22. #include <utility>
  23. #include "pre_activate/common/pattern_engine.h"
  24. #include "utils/any.h"
  25. #include "ir/anf.h"
  26. #include "ir/func_graph.h"
  27. #include "utils/log_adapter.h"
  28. /* namespace to support utils definition */
  29. namespace mindspore {
  30. bool CheckIfNeedExpand(const std::vector<BaseRef> &list) {
  31. return std::any_of(list.begin(), list.end(), [](const BaseRef &any) { return utils::isa<Seq>(any); });
  32. }
  33. std::shared_ptr<VectorRef> ExpandList(const std::vector<BaseRef> &list) {
  34. std::shared_ptr<VectorRef> new_list = std::make_shared<VectorRef>();
  35. for (auto &item : list) {
  36. if (utils::isa<Seq>(item)) {
  37. const Seq &seq = utils::cast<Seq>(item);
  38. new_list->insert(new_list->end(), seq.begin(), seq.end());
  39. } else {
  40. new_list->push_back(item);
  41. }
  42. }
  43. return new_list;
  44. }
  45. bool DefaultVisitor::Visit(const VectorRef &v_any, BaseRef *const visit_out) const {
  46. std::vector<BaseRef> out;
  47. (void)std::transform(v_any.begin(), v_any.end(), std::back_inserter(out),
  48. [this](const BaseRef &item) { return fn_(item); });
  49. if (visit_out != nullptr) {
  50. *visit_out = ExpandList(out);
  51. }
  52. return true;
  53. }
  54. bool DefaultVisitor::Visit(const BaseRef &any, BaseRef *const visit_out) const {
  55. if (utils::isa<Seq>(any)) {
  56. return Visit(utils::cast<Seq>(any), visit_out);
  57. } else if (utils::isa<AnfNodePtr>(any)) {
  58. auto nodeptr = utils::cast<AnfNodePtr>(any);
  59. AnfNodePtr output;
  60. AnfNodePtr *p_output = &output;
  61. if (visit_out == nullptr) {
  62. p_output = nullptr;
  63. }
  64. Visit(nodeptr, fn_, p_output);
  65. if (visit_out != nullptr) {
  66. *visit_out = output;
  67. }
  68. return true;
  69. }
  70. MS_LOG(DEBUG) << "VisitError, not support type to Visit: " + any.ToString();
  71. return false;
  72. }
  73. void DefaultVisitor::Visit(const AnfNodePtr &node, const VisitFn &fn, AnfNodePtr *output) const {
  74. if (node->isa<CNode>()) {
  75. Visit(node->cast<CNodePtr>(), fn, output);
  76. return;
  77. }
  78. if (node->isa<ValueNode>()) {
  79. Visit(node->cast<ValueNodePtr>(), fn, output);
  80. return;
  81. }
  82. if (output != nullptr) {
  83. *output = node;
  84. }
  85. }
  86. void DefaultVisitor::Visit(const CNodePtr &cnode, const VisitFn &fn, AnfNodePtr *output) const {
  87. // if output is nullptr, it's not required to make the new CNode node.
  88. if (output == nullptr) {
  89. for (auto &inp : cnode->inputs()) {
  90. (void)fn(inp);
  91. }
  92. if (cnode->func_graph() != nullptr) {
  93. (void)fn(cnode->func_graph());
  94. } else {
  95. (void)fn(cnode->func_graph_as_var());
  96. }
  97. return;
  98. }
  99. std::vector<AnfNodePtr> new_inputs;
  100. std::vector<BaseRef> after_cnode_fn;
  101. std::shared_ptr<VectorRef> out;
  102. (void)std::transform(cnode->inputs().begin(), cnode->inputs().end(), std::back_inserter(after_cnode_fn), fn);
  103. if (CheckIfNeedExpand(after_cnode_fn)) {
  104. out = ExpandList(after_cnode_fn);
  105. }
  106. std::vector<BaseRef> &outs = after_cnode_fn;
  107. if (out != nullptr) {
  108. outs = out->elements();
  109. }
  110. for (auto &any_item : outs) {
  111. if (!utils::isa<AnfNodePtr>(any_item)) {
  112. MS_LOG(EXCEPTION) << "VisitError, fn not return the same type AnfNodePtr";
  113. }
  114. new_inputs.push_back(utils::cast<AnfNodePtr>(any_item));
  115. }
  116. BaseRef any_fg;
  117. AnfNodePtr new_cnode = nullptr;
  118. if (cnode->func_graph() != nullptr) {
  119. any_fg = fn(cnode->func_graph());
  120. if (!utils::isa<FuncGraphPtr>(any_fg)) {
  121. MS_LOG(EXCEPTION) << "VisitError, fn not return the same type FuncGraphPtr";
  122. }
  123. new_cnode = std::make_shared<CNode>(new_inputs, utils::cast<FuncGraphPtr>(any_fg));
  124. } else {
  125. any_fg = fn(cnode->func_graph_as_var());
  126. if (utils::isa<VarPtr>(any_fg)) {
  127. new_cnode = std::make_shared<CNode>(new_inputs, utils::cast<VarPtr>(any_fg));
  128. } else if (utils::isa<FuncGraphPtr>(any_fg)) {
  129. new_cnode = std::make_shared<CNode>(new_inputs, utils::cast<FuncGraphPtr>(any_fg));
  130. } else {
  131. MS_LOG(EXCEPTION) << "VisitError, fn not return VarPtr or FuncGraphPtr";
  132. }
  133. }
  134. new_cnode->set_abstract(cnode->abstract());
  135. *output = new_cnode;
  136. }
  137. void DefaultVisitor::Visit(const ValueNodePtr &vnode, const VisitFn &fn, AnfNodePtr *output) const {
  138. const BaseRef &value = utils::cast<ValuePtr>(fn(vnode->value()));
  139. if (utils::isa<ValuePtr>(value)) {
  140. if (output != nullptr) {
  141. auto ct = NewValueNode(utils::cast<ValuePtr>(value));
  142. ct->set_abstract(vnode->abstract());
  143. *output = ct;
  144. }
  145. return;
  146. }
  147. MS_LOG(EXCEPTION) << "Visit result is not ValuePtr.";
  148. }
  149. } // namespace mindspore