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

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