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.

irpass.h 6.1 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_FRONTEND_OPTIMIZER_IRPASS_H_
  17. #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_H_
  18. #include <memory>
  19. #include "frontend/optimizer/optimizer.h"
  20. #include "frontend/optimizer/opt.h"
  21. #include "frontend/optimizer/anf_visitor.h"
  22. namespace mindspore {
  23. namespace opt {
  24. namespace irpass {
  25. // the collection of irpass for optimie action
  26. class OptimizeIRPassLib {
  27. public:
  28. OptimizeIRPassLib();
  29. ~OptimizeIRPassLib() = default;
  30. SubstitutionPtr arithmetic_simplify_;
  31. SubstitutionPtr arithmetic_simplify2_;
  32. SubstitutionPtr special_op_eliminate_;
  33. SubstitutionPtr zero_like_fill_zero_;
  34. SubstitutionPtr adjust_all_reduce_mul_add_;
  35. // ops eliminate
  36. SubstitutionPtr item_tuple_eliminate_;
  37. SubstitutionPtr tile_eliminate_;
  38. SubstitutionPtr cast_eliminate_;
  39. SubstitutionPtr reshape_eliminate_;
  40. SubstitutionPtr transpose_eliminate_;
  41. SubstitutionPtr reduce_eliminate_;
  42. SubstitutionPtr partial_eliminate_;
  43. SubstitutionPtr same_eliminate_;
  44. SubstitutionPtr check_bprop_eliminate_;
  45. SubstitutionPtr reset_defer_inline_;
  46. SubstitutionPtr depend_value_elim_;
  47. SubstitutionPtr all_reduce_const_elim_;
  48. // Env Item Eliminate
  49. SubstitutionPtr env_get_item_eliminate_;
  50. SubstitutionPtr new_env_get_item_;
  51. SubstitutionPtr incorporate_env_getitem_;
  52. SubstitutionPtr incorporate_env_getitem_bypass_recursive_;
  53. SubstitutionPtr incorporate_env_getitem_switch_;
  54. SubstitutionPtr incorporate_env_getitem_switch_layer_;
  55. // Ref eliminate
  56. SubstitutionPtr make_ref_eliminate_;
  57. SubstitutionPtr get_ref_param_eliminate_;
  58. SubstitutionPtr get_make_ref_eliminate_;
  59. SubstitutionPtr replace_refkey_by_param_;
  60. SubstitutionPtr replace_old_param_;
  61. // Branch culling
  62. SubstitutionPtr switch_simplify_;
  63. SubstitutionPtr float_tuple_getitem_switch_;
  64. SubstitutionPtr float_env_getitem_switch_;
  65. SubstitutionPtr convert_switch_replacement_;
  66. // AddN
  67. SubstitutionPtr merge_addn_;
  68. SubstitutionPtr addn_zero_filter_;
  69. // AccumulateNV2
  70. SubstitutionPtr accumulaten_eliminater_;
  71. // Gradient irpasses
  72. SubstitutionPtr expand_jprim_;
  73. SubstitutionPtr minmaximum_grad_;
  74. // inline
  75. SubstitutionPtr inline_;
  76. SubstitutionPtr inline_without_move_;
  77. SubstitutionPtr replace_applicator_;
  78. SubstitutionPtr specialize_transform_;
  79. // Incorporation
  80. SubstitutionPtr incorporate_getitem_set_;
  81. SubstitutionPtr incorporate_getitem_from_param_;
  82. SubstitutionPtr incorporate_call_;
  83. SubstitutionPtr incorporate_call_switch_;
  84. // virtual dataset
  85. SubstitutionPtr virtual_dataset_eliminate_;
  86. // Convert
  87. SubstitutionPtr print_tuple_wrapper_;
  88. // Unused parameter eliminate
  89. SubstitutionPtr unused_parameter_eliminate_;
  90. SubstitutionPtr unused_output_eliminate_;
  91. // tuple parameter graph transform
  92. SubstitutionPtr call_graph_tuple_transform_;
  93. // AddN eliminate
  94. SubstitutionPtr addn_eliminate_;
  95. // Fusion
  96. SubstitutionPtr mark_interface_fusion_;
  97. // RowTensor Eliminate
  98. SubstitutionPtr row_tensor_eliminate_;
  99. // SparseTensor Eliminate
  100. SubstitutionPtr sparse_tensor_eliminate_;
  101. // Value_Based Eliminate
  102. SubstitutionPtr value_based_eliminate_;
  103. // SwitchLayer defer inline
  104. SubstitutionPtr switch_layer_defer_inline_;
  105. // Pynative Eliminate
  106. SubstitutionPtr pynative_eliminate_;
  107. };
  108. // the collection of irpass for resolve action
  109. class ResolveIRPassLib {
  110. public:
  111. ResolveIRPassLib();
  112. ~ResolveIRPassLib() = default;
  113. SubstitutionPtr resolver_resolve_attr_;
  114. SubstitutionPtr resolver_resolve_;
  115. SubstitutionPtr resolver_getattr_;
  116. };
  117. class InferenceOptPrepareLib {
  118. public:
  119. InferenceOptPrepareLib();
  120. ~InferenceOptPrepareLib() = default;
  121. SubstitutionPtr grad_var_prepare_;
  122. };
  123. // predicate functions
  124. inline bool IsNode(const AnfNodePtr &) { return true; }
  125. inline bool IsCNode(const AnfNodePtr &node) {
  126. if (node != nullptr) {
  127. return node->isa<CNode>();
  128. }
  129. return false;
  130. }
  131. inline bool IsVNode(const AnfNodePtr &node) {
  132. if (node != nullptr) {
  133. return node->isa<ValueNode>();
  134. }
  135. return false;
  136. }
  137. inline bool IsParam(const AnfNodePtr &node) {
  138. if (node != nullptr) {
  139. return node->isa<Parameter>();
  140. }
  141. return false;
  142. }
  143. // Check if CNode Input 0 is Func Graph
  144. inline bool IsCNodeGraph(const AnfNodePtr &node) {
  145. if (node == nullptr || !node->isa<CNode>()) {
  146. return false;
  147. }
  148. auto inp0 = node->cast<CNodePtr>()->input(0);
  149. return IsValueNode<FuncGraph>(inp0);
  150. }
  151. // Check if CNode Input 0 is Func Graph of graph kernel.
  152. inline bool IsCNodeGraphKernel(const AnfNodePtr &node) {
  153. if (node == nullptr || !node->isa<CNode>()) {
  154. return false;
  155. }
  156. auto inp0 = node->cast<CNodePtr>()->input(0);
  157. if (IsValueNode<FuncGraph>(inp0)) {
  158. auto fg = GetValueNode<FuncGraphPtr>(inp0);
  159. if (fg == nullptr) {
  160. return false;
  161. }
  162. return fg->has_attr(FUNC_GRAPH_ATTR_GRAPH_KERNEL);
  163. }
  164. return false;
  165. }
  166. // Check if CNode Input 0 is CNode
  167. inline bool IsCNodeDup(const AnfNodePtr &node) {
  168. if (node == nullptr || !node->isa<CNode>()) {
  169. return false;
  170. }
  171. auto inp0 = node->cast<CNodePtr>()->input(0);
  172. return (inp0 != nullptr) && inp0->isa<CNode>();
  173. }
  174. // check if the cnode is a switch cnode
  175. inline bool IsCNodeSwitch(const AnfNodePtr &node) {
  176. if (node != nullptr) {
  177. if (node->isa<CNode>()) {
  178. return IsPrimitiveCNode(node, prim::kPrimSwitch);
  179. }
  180. }
  181. return false;
  182. }
  183. } // namespace irpass
  184. } // namespace opt
  185. } // namespace mindspore
  186. #endif // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_H_