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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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_H_
  17. #define MINDSPORE_CCSRC_OPTIMIZER_IRPASS_H_
  18. #include <memory>
  19. #include "optimizer/optimizer.h"
  20. #include "optimizer/opt.h"
  21. #include "ir/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. // Env Item Eliminate
  48. SubstitutionPtr env_get_item_eliminate_;
  49. SubstitutionPtr new_env_get_item_;
  50. SubstitutionPtr incorporate_env_getitem_;
  51. SubstitutionPtr incorporate_env_getitem_switch_;
  52. // Ref eliminate
  53. SubstitutionPtr make_ref_eliminate_;
  54. SubstitutionPtr get_ref_param_eliminate_;
  55. SubstitutionPtr get_make_ref_eliminate_;
  56. SubstitutionPtr replace_refkey_by_param_;
  57. SubstitutionPtr replace_old_param_;
  58. // Branch culling
  59. SubstitutionPtr switch_simplify_;
  60. SubstitutionPtr float_tuple_getitem_switch_;
  61. SubstitutionPtr float_env_getitem_switch_;
  62. SubstitutionPtr convert_switch_replacement_;
  63. // AddN
  64. SubstitutionPtr merge_addn_;
  65. SubstitutionPtr addn_zero_filter_;
  66. // Gradient irpasses
  67. SubstitutionPtr expand_jprim_;
  68. SubstitutionPtr minmaximum_grad_;
  69. // inline
  70. SubstitutionPtr inline_;
  71. SubstitutionPtr replace_applicator_;
  72. SubstitutionPtr specialize_transform_;
  73. // Incorporation
  74. SubstitutionPtr incorporate_getitem_set_;
  75. SubstitutionPtr incorporate_getitem_from_param_;
  76. SubstitutionPtr incorporate_call_;
  77. SubstitutionPtr incorporate_call_switch_;
  78. // virtual dataset
  79. SubstitutionPtr virtual_dataset_eliminate_;
  80. // Convert
  81. SubstitutionPtr print_tuple_wrapper_;
  82. // Unused parameter eliminate
  83. SubstitutionPtr unused_parameter_eliminate_;
  84. SubstitutionPtr unused_output_eliminate_;
  85. // AddN eliminate
  86. SubstitutionPtr addn_eliminate_;
  87. // Fusion
  88. SubstitutionPtr mark_interface_fusion_;
  89. // IndexedSlices Eliminate
  90. SubstitutionPtr indexed_slices_eliminate_;
  91. };
  92. // the collection of irpass for resolve action
  93. class ResolveIRPassLib {
  94. public:
  95. ResolveIRPassLib();
  96. ~ResolveIRPassLib() = default;
  97. SubstitutionPtr resolver_resolve_;
  98. SubstitutionPtr resolver_getattr_;
  99. };
  100. class InferenceOptPrepareLib {
  101. public:
  102. InferenceOptPrepareLib();
  103. ~InferenceOptPrepareLib() = default;
  104. SubstitutionPtr grad_var_prepare_;
  105. };
  106. // predicate functions
  107. inline bool IsNode(const AnfNodePtr &) { return true; }
  108. inline bool IsCNode(const AnfNodePtr &node) {
  109. if (node != nullptr) {
  110. return node->isa<CNode>();
  111. }
  112. return false;
  113. }
  114. inline bool IsVNode(const AnfNodePtr &node) {
  115. if (node != nullptr) {
  116. return node->isa<ValueNode>();
  117. }
  118. return false;
  119. }
  120. inline bool IsParam(const AnfNodePtr &node) {
  121. if (node != nullptr) {
  122. return node->isa<Parameter>();
  123. }
  124. return false;
  125. }
  126. // Check if CNode Input 0 is Func Graph
  127. inline bool IsCNodeGraph(const AnfNodePtr &node) {
  128. if (node == nullptr || !node->isa<CNode>()) {
  129. return false;
  130. }
  131. auto inp0 = node->cast<CNodePtr>()->input(0);
  132. return IsValueNode<FuncGraph>(inp0);
  133. }
  134. // Check if CNode Input 0 is Func Graph of graph kernel.
  135. inline bool IsCNodeGraphKernel(const AnfNodePtr &node) {
  136. if (node == nullptr || !node->isa<CNode>()) {
  137. return false;
  138. }
  139. auto inp0 = node->cast<CNodePtr>()->input(0);
  140. if (IsValueNode<FuncGraph>(inp0)) {
  141. auto fg = GetValueNode<FuncGraphPtr>(inp0);
  142. if (fg == nullptr) {
  143. return false;
  144. }
  145. return fg->has_attr(FUNC_GRAPH_ATTR_GRAPH_KERNEL);
  146. }
  147. return false;
  148. }
  149. // Check if CNode Input 0 is CNode
  150. inline bool IsCNodeDup(const AnfNodePtr &node) {
  151. if (node == nullptr || !node->isa<CNode>()) {
  152. return false;
  153. }
  154. auto inp0 = node->cast<CNodePtr>()->input(0);
  155. return (inp0 != nullptr) && inp0->isa<CNode>();
  156. }
  157. } // namespace irpass
  158. } // namespace opt
  159. } // namespace mindspore
  160. #endif // MINDSPORE_CCSRC_OPTIMIZER_IRPASS_H_