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.

pattern.cc 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/pattern.h"
  17. #include "pybind_api/api_register.h"
  18. namespace mindspore {
  19. namespace opt {
  20. namespace python_pass {
  21. int Pattern::g_id_ = 0;
  22. MatchResultPtr IsPrimTypeOf::match(const AnfNodePtr &node) {
  23. if (!IsValueNode<Primitive>(node)) {
  24. return nullptr;
  25. }
  26. MatchResultPtr res = std::make_shared<MatchResult>();
  27. if (IsValueNode<Primitive>(node)) {
  28. // iterate over all primitives
  29. for (auto &iter : primitives_) {
  30. if (IsPrimitive(node, iter) || iter->name() == "*") {
  31. matched_prim_ = iter;
  32. res->add_entry(shared_from_base<IsPrimTypeOf>(), node);
  33. return res;
  34. }
  35. }
  36. }
  37. return nullptr;
  38. }
  39. MatchResultPtr CallWith::match(const AnfNodePtr &node) {
  40. if (!IsPrimitiveCNode(node)) {
  41. return nullptr;
  42. }
  43. MatchResultPtr res = std::make_shared<MatchResult>();
  44. // IsPrimitiveCNode
  45. auto cnode = node->cast<CNodePtr>();
  46. MS_EXCEPTION_IF_NULL(cnode);
  47. // Check Primitive ValueNode
  48. if (prim_pattern_ != nullptr) {
  49. // Passed in prim_pattern
  50. auto prim_value_res = prim_pattern_->match(cnode->input(0));
  51. if (prim_value_res == nullptr) {
  52. return nullptr;
  53. }
  54. res->merge(prim_value_res);
  55. } else if (prim_ != nullptr) {
  56. // Passed in primitive/primitive str
  57. if (!IsPrimitive(cnode->input(0), prim_)) {
  58. return nullptr;
  59. }
  60. } else {
  61. MS_LOG(EXCEPTION) << "Uninitialized CallWith pattern.";
  62. }
  63. // Check inputs
  64. auto p_inputs_size = inputs_.size();
  65. auto node_inputs_size = cnode->size() - 1;
  66. if (p_inputs_size != 0 && p_inputs_size != node_inputs_size) {
  67. return nullptr;
  68. }
  69. // If inputs is not specified, add node without looking into its inputs
  70. if (p_inputs_size == 0) {
  71. res->add_entry(shared_from_base<CallWith>(), cnode->input(0));
  72. return res;
  73. }
  74. bool failed = false;
  75. for (std::size_t i = 0; i < node_inputs_size; i++) {
  76. auto pattern = inputs_[i];
  77. auto input = cnode->input(i + 1);
  78. auto input_match_result = pattern->match(input);
  79. if (input_match_result == nullptr) {
  80. failed = true;
  81. break;
  82. }
  83. res->merge(input_match_result);
  84. }
  85. if (!failed) {
  86. res->add_entry(shared_from_base<CallWith>(), cnode->input(0));
  87. return res;
  88. }
  89. return nullptr;
  90. }
  91. MatchResultPtr IsIn::match(const AnfNodePtr &node) {
  92. for (auto &iter : patterns_) {
  93. auto res = iter->match(node);
  94. if (res != nullptr) {
  95. res->add_entry(shared_from_base<IsIn>(), node);
  96. return res;
  97. }
  98. }
  99. return nullptr;
  100. }
  101. MatchResultPtr IsNot::match(const AnfNodePtr &node) {
  102. for (auto &iter : patterns_) {
  103. auto res = iter->match(node);
  104. if (res != nullptr) {
  105. return nullptr;
  106. }
  107. }
  108. auto res = std::make_shared<MatchResult>();
  109. res->add_entry(shared_from_base<IsNot>(), node);
  110. return res;
  111. }
  112. MatchResultPtr AnyPattern::match(const AnfNodePtr &node) {
  113. MatchResultPtr res = std::make_shared<MatchResult>();
  114. res->add_entry(shared_from_base<AnyPattern>(), node);
  115. return res;
  116. }
  117. AnfNodePtr MatchResult::get_node(const PatternPtr &pattern) {
  118. auto entry = match_result_.find(pattern);
  119. if (entry == match_result_.end()) {
  120. return nullptr;
  121. }
  122. return entry->second;
  123. }
  124. void MatchResult::merge(const MatchResultPtr &other_result) {
  125. auto other_result_map = other_result->_result();
  126. // add/update entries in other_result
  127. for (auto &iter : other_result_map) {
  128. match_result_[iter.first] = iter.second;
  129. }
  130. }
  131. REGISTER_PYBIND_DEFINE(
  132. Pattern, ([](const py::module *m) {
  133. (void)py::class_<Pattern, std::shared_ptr<Pattern>>(*m, "Pattern").def(py::init<>());
  134. (void)py::class_<IsIn, std::shared_ptr<IsIn>, Pattern>(*m, "IsIn_").def(py::init<vector<PatternPtr>>());
  135. (void)py::class_<IsPrimTypeOf, std::shared_ptr<IsPrimTypeOf>, Pattern>(*m, "IsPrimTypeOf_", py::dynamic_attr())
  136. .def(py::init<vector<PrimitivePyPtr>, string, bool>())
  137. .def(py::init<vector<string>, string, bool>());
  138. (void)py::class_<CallWith, std::shared_ptr<CallWith>, Pattern>(*m, "CallWith_")
  139. .def(py::init<PatternPtr, vector<PatternPtr>, bool>())
  140. .def(py::init<PrimitivePyPtr, vector<PatternPtr>, bool>())
  141. .def(py::init<string, vector<PatternPtr>, bool>());
  142. (void)py::class_<IsNot, std::shared_ptr<IsNot>, Pattern>(*m, "IsNot_").def(py::init<vector<PatternPtr>>());
  143. (void)py::class_<AnyPattern, std::shared_ptr<AnyPattern>, Pattern>(*m, "AnyPattern").def(py::init<>());
  144. (void)py::class_<NewTensor, std::shared_ptr<NewTensor>, Pattern>(*m, "NewTensor_")
  145. .def(py::init<tensor::TensorPtr>());
  146. (void)py::class_<NewParameter, std::shared_ptr<NewParameter>, Pattern>(*m, "NewParameter_")
  147. .def(py::init<string, tensor::TensorPtr, bool, bool, bool>());
  148. (void)py::class_<Imm, std::shared_ptr<Imm>, Pattern>(*m, "Imm").def(py::init<int>());
  149. }));
  150. } // namespace python_pass
  151. } // namespace opt
  152. } // namespace mindspore