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.

item_tuple_eliminate.h 6.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #ifndef MINDSPORE_CCSRC_OPTIMIZER_IRPASS_ITEM_TUPLE_ELIMINATE_H_
  17. #define MINDSPORE_CCSRC_OPTIMIZER_IRPASS_ITEM_TUPLE_ELIMINATE_H_
  18. #include <vector>
  19. #include <algorithm>
  20. #include "optimizer/irpass.h"
  21. #include "optimizer/optimizer.h"
  22. #include "ir/visitor.h"
  23. #include "operator/ops.h"
  24. namespace mindspore {
  25. namespace opt {
  26. namespace irpass {
  27. // (a, b, c, ...)[0] => a
  28. // (a, b, c, ...)[1] => b
  29. // {prim::kPrimTupleGetItem, {prim::kPrimMakeTuple, Xs}, C}
  30. class GetitemEliminater : public AnfVisitor {
  31. public:
  32. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
  33. Reset();
  34. AnfVisitor::Match(prim::kPrimTupleGetItem, {IsCNode, IsVNode})(node);
  35. if (is_match_) {
  36. return tuple_->input(id_);
  37. }
  38. return nullptr;
  39. }
  40. void Visit(const CNodePtr &cnode) override {
  41. if (IsPrimitiveCNode(cnode, prim::kPrimMakeTuple)) {
  42. tuple_ = cnode;
  43. }
  44. }
  45. void Visit(const ValueNodePtr &vnode) override {
  46. if (tuple_ != nullptr && IsValueNode<Int32Imm>(vnode)) {
  47. id_ = IntToSize(GetValue<int>(vnode->value()) + 1);
  48. if (tuple_->size() > id_) {
  49. is_match_ = true;
  50. }
  51. }
  52. }
  53. void Reset() {
  54. id_ = 0;
  55. tuple_ = nullptr;
  56. is_match_ = false;
  57. }
  58. private:
  59. bool is_match_{false};
  60. size_t id_{0};
  61. CNodePtr tuple_{nullptr};
  62. };
  63. // setitem((a, b, c, ...), 0, z) => (z, b, c, ...)
  64. // setitem((a, b, c, ...), 1, z) => (a, z, c, ...)
  65. // {prim::kPrimTupleSetItem, {prim::kPrimMakeTuple, Xs}, C, Z}
  66. class SetitemEliminater : public AnfVisitor {
  67. public:
  68. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
  69. Reset();
  70. AnfVisitor::Match(prim::kPrimTupleSetItem, {IsCNode, IsVNode, IsNode})(node);
  71. auto fg = node->func_graph();
  72. if (fg != nullptr && z_ != nullptr) {
  73. args_[id_] = z_;
  74. return fg->NewCNode(args_);
  75. }
  76. return nullptr;
  77. }
  78. void Visit(const AnfNodePtr &node) override {
  79. if (is_match_) {
  80. z_ = node;
  81. return;
  82. }
  83. AnfVisitor::Visit(node);
  84. }
  85. void Visit(const CNodePtr &cnode) override {
  86. if (IsPrimitiveCNode(cnode, prim::kPrimMakeTuple)) {
  87. auto &inputs = cnode->inputs();
  88. (void)std::copy(inputs.begin(), inputs.end(), std::back_inserter(args_));
  89. }
  90. }
  91. void Visit(const ValueNodePtr &vnode) override {
  92. if (args_.size() > 0 && IsValueNode<Int32Imm>(vnode)) {
  93. id_ = IntToSize(GetValue<int>(vnode->value()) + 1);
  94. if (id_ < args_.size()) {
  95. is_match_ = true;
  96. }
  97. }
  98. }
  99. void Reset() {
  100. id_ = 0;
  101. z_ = nullptr;
  102. is_match_ = false;
  103. args_.clear();
  104. }
  105. private:
  106. bool is_match_{false};
  107. size_t id_{0};
  108. AnfNodePtr z_{nullptr};
  109. std::vector<AnfNodePtr> args_{};
  110. };
  111. // {prim::kPrimTupleGetItem, {prim::kPrimTupleSetItem, Y, C1, X}, C2}
  112. class GetSetitemEliminater : public AnfVisitor {
  113. public:
  114. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
  115. Reset();
  116. AnfVisitor::Match(prim::kPrimTupleGetItem, {IsCNode, IsVNode})(node);
  117. auto fg = node->func_graph();
  118. if (fg != nullptr && key1_ >= 0 && key2_ >= 0) {
  119. if (key1_ == key2_) {
  120. return last_;
  121. }
  122. return fg->NewCNode({op_, tuple_, c2_});
  123. }
  124. return nullptr;
  125. }
  126. void Visit(const CNodePtr &cnode) override {
  127. if (IsPrimitiveCNode(cnode, prim::kPrimTupleSetItem)) {
  128. if (cnode->size() < 4) {
  129. return;
  130. }
  131. op_ = cnode->input(0);
  132. tuple_ = cnode->input(1);
  133. last_ = cnode->input(3);
  134. // key of setitem
  135. is_in_set_ = true;
  136. AnfVisitor::Visit(cnode->input(2));
  137. is_in_set_ = false;
  138. }
  139. }
  140. void Visit(const ValueNodePtr &vnode) override {
  141. if (IsValueNode<Int32Imm>(vnode)) {
  142. auto key = GetValue<int>(vnode->value());
  143. if (is_in_set_) {
  144. key1_ = key;
  145. } else {
  146. c2_ = vnode;
  147. key2_ = key;
  148. }
  149. }
  150. }
  151. void Reset() {
  152. key1_ = -1;
  153. key2_ = -1;
  154. op_ = nullptr;
  155. c2_ = nullptr;
  156. last_ = nullptr;
  157. tuple_ = nullptr;
  158. is_in_set_ = false;
  159. }
  160. private:
  161. bool is_in_set_{false};
  162. int key1_{-1}, key2_{-1};
  163. AnfNodePtr op_{nullptr}, tuple_{nullptr}, last_{nullptr}, c2_{nullptr};
  164. };
  165. // {prim::kPrimTupleGetItem, {prim::kPrimDepend, X, Y}, C} ->
  166. // {prim::kPrimDepend, {prim::kPrimTupleGetItem, X, C}, Y}
  167. class GetitemDependReorder : public AnfVisitor {
  168. public:
  169. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
  170. Reset();
  171. AnfVisitor::Match(prim::kPrimTupleGetItem, {IsCNode, IsValueNode<Int32Imm>})(node);
  172. if (x_ == nullptr) {
  173. return nullptr;
  174. }
  175. auto fg = node->func_graph();
  176. auto item_node = NewCNode({NewValueNode(prim::kPrimTupleGetItem), x_, c_}, fg);
  177. return NewCNode({NewValueNode(prim::kPrimDepend), item_node, y_}, fg);
  178. }
  179. void Visit(const CNodePtr &cnode) override {
  180. // {prim::kPrimDepend, X, Y}
  181. if (IsPrimitiveCNode(cnode, prim::kPrimDepend) && cnode->size() == 3) {
  182. x_ = cnode->input(1);
  183. y_ = cnode->input(2);
  184. }
  185. }
  186. void Visit(const ValueNodePtr &vnode) override { c_ = vnode; }
  187. void Reset() {
  188. x_ = nullptr;
  189. y_ = nullptr;
  190. c_ = nullptr;
  191. }
  192. private:
  193. AnfNodePtr x_{nullptr}, y_{nullptr}, c_{nullptr};
  194. };
  195. class ItemTupleEliminater {
  196. public:
  197. ItemTupleEliminater()
  198. : get_item_eliminater_(), set_item_eliminater_(), get_set_item_eliminater_(), get_item_depend_reorder_() {
  199. eliminaters_.emplace_back(get_item_eliminater_);
  200. eliminaters_.emplace_back(set_item_eliminater_);
  201. eliminaters_.emplace_back(get_set_item_eliminater_);
  202. eliminaters_.emplace_back(get_item_depend_reorder_);
  203. }
  204. ~ItemTupleEliminater() = default;
  205. AnfNodePtr operator()(const OptimizerPtr &optimizer, const AnfNodePtr &node) {
  206. AnfNodePtr new_node;
  207. for (auto &eliminater : eliminaters_) {
  208. new_node = eliminater(optimizer, node);
  209. if (new_node != nullptr) {
  210. return new_node;
  211. }
  212. }
  213. return nullptr;
  214. }
  215. private:
  216. GetitemEliminater get_item_eliminater_;
  217. SetitemEliminater set_item_eliminater_;
  218. GetSetitemEliminater get_set_item_eliminater_;
  219. GetitemDependReorder get_item_depend_reorder_;
  220. std::vector<TransformFuncType> eliminaters_{};
  221. };
  222. } // namespace irpass
  223. } // namespace opt
  224. } // namespace mindspore
  225. #endif // MINDSPORE_CCSRC_OPTIMIZER_IRPASS_ITEM_TUPLE_ELIMINATE_H_