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 7.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * Copyright 2020-2021 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. SubstitutionPtr float_depend_g_call_;
  36. // ops eliminate
  37. SubstitutionPtr tuple_list_get_item_eliminator_;
  38. SubstitutionPtr tuple_list_get_item_const_eliminator_;
  39. SubstitutionPtr tuple_list_set_item_eliminator_;
  40. SubstitutionPtr tuple_list_get_set_item_eliminator_;
  41. SubstitutionPtr tuple_list_get_item_depend_reorder_;
  42. SubstitutionPtr tuple_list_convert_item_index_to_positive_;
  43. SubstitutionPtr make_slice_get_slice_eliminator_;
  44. SubstitutionPtr tile_eliminate_;
  45. SubstitutionPtr cast_eliminate_;
  46. SubstitutionPtr reshape_eliminate_;
  47. SubstitutionPtr transpose_eliminate_;
  48. SubstitutionPtr reduce_eliminate_;
  49. SubstitutionPtr partial_eliminate_;
  50. SubstitutionPtr same_eliminate_;
  51. SubstitutionPtr check_bprop_eliminate_;
  52. SubstitutionPtr reset_defer_inline_;
  53. SubstitutionPtr depend_value_elim_;
  54. SubstitutionPtr all_reduce_const_elim_;
  55. SubstitutionPtr mirror_mini_step_elim_;
  56. SubstitutionPtr virtual_add_elim_;
  57. SubstitutionPtr mini_step_allgather_replace_;
  58. SubstitutionPtr micro_step_allgather_replace_;
  59. SubstitutionPtr real_op_eliminate_;
  60. // Env Item Eliminate
  61. SubstitutionPtr env_get_item_eliminate_;
  62. SubstitutionPtr env_get_item_add_eliminate_;
  63. SubstitutionPtr env_get_set_item_eliminate_;
  64. SubstitutionPtr env_get_item_depend_swap_;
  65. SubstitutionPtr incorporate_env_getitem_;
  66. SubstitutionPtr incorporate_env_getitem_bypass_recursive_;
  67. SubstitutionPtr incorporate_env_getitem_switch_;
  68. SubstitutionPtr incorporate_env_getitem_switch_layer_;
  69. // Ref eliminate
  70. SubstitutionPtr make_ref_eliminate_;
  71. SubstitutionPtr get_ref_param_eliminate_;
  72. SubstitutionPtr get_make_ref_eliminate_;
  73. SubstitutionPtr replace_refkey_by_param_;
  74. SubstitutionPtr replace_old_param_;
  75. // Branch culling
  76. SubstitutionPtr switch_simplify_;
  77. SubstitutionPtr float_tuple_getitem_switch_;
  78. SubstitutionPtr float_env_getitem_switch_;
  79. SubstitutionPtr exchange_switch_depend_value_;
  80. SubstitutionPtr switch_partial_eliminater_;
  81. SubstitutionPtr switch_layer_partial_eliminater_;
  82. // AddN
  83. SubstitutionPtr merge_addn_;
  84. SubstitutionPtr addn_zero_filter_;
  85. // AccumulateNV2
  86. SubstitutionPtr accumulaten_eliminater_;
  87. // Accelerated Algorithm
  88. SubstitutionPtr less_batch_normalization_;
  89. // Gradient irpasses
  90. SubstitutionPtr minmaximum_grad_;
  91. // inline
  92. SubstitutionPtr inline_;
  93. SubstitutionPtr inline_without_move_;
  94. SubstitutionPtr replace_applicator_;
  95. SubstitutionPtr specialize_transform_;
  96. // Auto-monad related eliminaters.
  97. SubstitutionPtr updatestate_useless_node_eliminater_;
  98. SubstitutionPtr updatestate_pure_node_eliminater_;
  99. SubstitutionPtr switch_call_monad_eliminater_;
  100. SubstitutionPtr stopgrad_eliminater_;
  101. SubstitutionPtr load_eliminater_;
  102. // Incorporation
  103. SubstitutionPtr incorporate_getitem_set_;
  104. SubstitutionPtr incorporate_call_;
  105. SubstitutionPtr incorporate_call_switch_;
  106. // virtual dataset
  107. SubstitutionPtr virtual_dataset_eliminate_;
  108. // virtual output
  109. SubstitutionPtr virtual_output_eliminate_;
  110. // PipelineSplit
  111. SubstitutionPtr receive_eliminate_;
  112. SubstitutionPtr virtual_accu_grad_;
  113. SubstitutionPtr virtual_assign_add_;
  114. SubstitutionPtr mirror_micro_step_;
  115. // Convert
  116. SubstitutionPtr print_tuple_wrapper_;
  117. // tuple parameter graph transform
  118. SubstitutionPtr call_graph_tuple_transform_;
  119. // RowTensor Eliminate
  120. SubstitutionPtr row_tensor_eliminate_;
  121. // RowTensorAddZerosLike Eliminate
  122. SubstitutionPtr row_tensor_add_zeros_like_;
  123. // SparseTensor Eliminate
  124. SubstitutionPtr sparse_tensor_eliminate_;
  125. // Value_Based Eliminate
  126. SubstitutionPtr value_based_eliminate_;
  127. // Switch defer inline
  128. SubstitutionPtr switch_defer_inline_;
  129. // SwitchLayer defer inline
  130. SubstitutionPtr switch_layer_defer_inline_;
  131. // Pynative Eliminate
  132. SubstitutionPtr pynative_eliminate_;
  133. // Recompute
  134. SubstitutionPtr set_cell_output_no_recompute_;
  135. };
  136. // the collection of irpass for resolve action
  137. class ResolveIRPassLib {
  138. public:
  139. ResolveIRPassLib();
  140. ~ResolveIRPassLib() = default;
  141. SubstitutionPtr resolver_getattr_resolve_;
  142. };
  143. class InferenceOptPrepareLib {
  144. public:
  145. InferenceOptPrepareLib();
  146. ~InferenceOptPrepareLib() = default;
  147. SubstitutionPtr grad_var_prepare_;
  148. };
  149. // predicate functions
  150. inline bool IsNode(const AnfNodePtr &) { return true; }
  151. inline bool IsCNode(const AnfNodePtr &node) {
  152. if (node != nullptr) {
  153. return node->isa<CNode>();
  154. }
  155. return false;
  156. }
  157. inline bool IsVNode(const AnfNodePtr &node) {
  158. if (node != nullptr) {
  159. return node->isa<ValueNode>();
  160. }
  161. return false;
  162. }
  163. inline bool IsParam(const AnfNodePtr &node) {
  164. if (node != nullptr) {
  165. return node->isa<Parameter>();
  166. }
  167. return false;
  168. }
  169. inline bool IsLoad(const AnfNodePtr &node) {
  170. if (node == nullptr || !node->isa<CNode>()) {
  171. return false;
  172. }
  173. return IsPrimitiveCNode(node, prim::kPrimLoad);
  174. }
  175. // Check if CNode Input 0 is Func Graph
  176. inline bool IsCNodeGraph(const AnfNodePtr &node) {
  177. if (node == nullptr || !node->isa<CNode>()) {
  178. return false;
  179. }
  180. auto inp0 = node->cast<CNodePtr>()->input(0);
  181. return IsValueNode<FuncGraph>(inp0);
  182. }
  183. // Check if CNode Input 0 is CNode
  184. inline bool IsCNodeDup(const AnfNodePtr &node) {
  185. if (node == nullptr || !node->isa<CNode>()) {
  186. return false;
  187. }
  188. auto inp0 = node->cast<CNodePtr>()->input(0);
  189. return (inp0 != nullptr) && inp0->isa<CNode>();
  190. }
  191. // check if the cnode is a switch cnode
  192. inline bool IsCNodeSwitch(const AnfNodePtr &node) {
  193. if (node != nullptr) {
  194. if (node->isa<CNode>()) {
  195. return IsPrimitiveCNode(node, prim::kPrimSwitch);
  196. }
  197. }
  198. return false;
  199. }
  200. } // namespace irpass
  201. } // namespace opt
  202. } // namespace mindspore
  203. #endif // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_H_