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

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