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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 special_op_eliminate_;
  32. SubstitutionPtr zero_like_fill_zero_;
  33. SubstitutionPtr adjust_all_reduce_mul_add_;
  34. // ops eliminate
  35. SubstitutionPtr item_tuple_eliminate_;
  36. SubstitutionPtr tile_eliminate_;
  37. SubstitutionPtr cast_eliminate_;
  38. SubstitutionPtr reshape_eliminate_;
  39. SubstitutionPtr transpose_eliminate_;
  40. SubstitutionPtr reduce_eliminate_;
  41. SubstitutionPtr partial_eliminate_;
  42. SubstitutionPtr same_eliminate_;
  43. SubstitutionPtr check_bprop_eliminate_;
  44. SubstitutionPtr reset_defer_inline_;
  45. // Env Item Eliminate
  46. SubstitutionPtr new_env_get_item_;
  47. SubstitutionPtr add_env_get_item_;
  48. SubstitutionPtr env_get_set_item_;
  49. SubstitutionPtr incorporate_env_getitem_;
  50. SubstitutionPtr incorporate_env_getitem_switch_;
  51. // Ref eliminate
  52. SubstitutionPtr make_ref_eliminate_;
  53. SubstitutionPtr get_make_ref_eliminate_;
  54. SubstitutionPtr replace_refkey_by_param_;
  55. SubstitutionPtr replace_old_param_;
  56. // Branch culling
  57. SubstitutionPtr switch_simplify_;
  58. SubstitutionPtr float_tuple_getitem_switch_;
  59. SubstitutionPtr float_env_getitem_switch_;
  60. SubstitutionPtr convert_switch_replacement_;
  61. // AddN
  62. SubstitutionPtr merge_addn_;
  63. SubstitutionPtr addn_zero_filter_;
  64. // Gradient irpasses
  65. SubstitutionPtr expand_jprim_;
  66. SubstitutionPtr stop_gradient_eliminate_;
  67. SubstitutionPtr minmaximum_grad_;
  68. // inline
  69. SubstitutionPtr inline_;
  70. SubstitutionPtr replace_applicator_;
  71. SubstitutionPtr specialize_transform_;
  72. // Incorporation
  73. SubstitutionPtr incorporate_getitem_;
  74. SubstitutionPtr incorporate_getitem_switch_;
  75. SubstitutionPtr incorporate_call_;
  76. SubstitutionPtr incorporate_call_switch_;
  77. // virtual dataset
  78. SubstitutionPtr virtual_dataset_eliminate_;
  79. // Convert
  80. SubstitutionPtr print_tuple_wrapper_;
  81. };
  82. // the collection of irpass for resolve action
  83. class ResolveIRPassLib {
  84. public:
  85. ResolveIRPassLib();
  86. ~ResolveIRPassLib() = default;
  87. SubstitutionPtr resolver_resolve_;
  88. SubstitutionPtr resolver_getattr_;
  89. };
  90. class InferenceOptPrepareLib {
  91. public:
  92. InferenceOptPrepareLib();
  93. ~InferenceOptPrepareLib() = default;
  94. SubstitutionPtr grad_var_prepare_;
  95. };
  96. // predicate functions
  97. inline bool IsNode(const AnfNodePtr &) { return true; }
  98. inline bool IsCNode(const AnfNodePtr &node) {
  99. if (node != nullptr) {
  100. return node->isa<CNode>();
  101. }
  102. return false;
  103. }
  104. inline bool IsVNode(const AnfNodePtr &node) {
  105. if (node != nullptr) {
  106. return node->isa<ValueNode>();
  107. }
  108. return false;
  109. }
  110. inline bool IsParam(const AnfNodePtr &node) {
  111. if (node != nullptr) {
  112. return node->isa<Parameter>();
  113. }
  114. return false;
  115. }
  116. // Check if CNode Input 0 is Func Graph
  117. inline bool IsCNodeGraph(const AnfNodePtr &node) {
  118. if (node == nullptr || !node->isa<CNode>()) {
  119. return false;
  120. }
  121. auto inp0 = node->cast<CNodePtr>()->input(0);
  122. if (IsValueNode<FuncGraph>(inp0)) {
  123. return true;
  124. }
  125. return false;
  126. }
  127. // Check if CNode Input 0 is CNode
  128. inline bool IsCNodeDup(const AnfNodePtr &node) {
  129. if (node == nullptr || !node->isa<CNode>()) {
  130. return false;
  131. }
  132. auto inp0 = node->cast<CNodePtr>()->input(0);
  133. if (inp0 != nullptr && inp0->isa<CNode>()) {
  134. return true;
  135. }
  136. return false;
  137. }
  138. } // namespace irpass
  139. } // namespace opt
  140. } // namespace mindspore
  141. #endif // MINDSPORE_CCSRC_OPTIMIZER_IRPASS_H_