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