1 /** 2 * Copyright 2020-2022 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_LITE_TOOLS_CONVERTER_PARSER_TF_FUNCTIONALIZE_COND_H_ 17 #define MINDSPORE_LITE_TOOLS_CONVERTER_PARSER_TF_FUNCTIONALIZE_COND_H_ 18 #define USE_DEPRECATED_API 19 20 #include <string> 21 #include <set> 22 #include <vector> 23 #include <map> 24 #include "include/backend/optimizer/pass.h" 25 #include "tools/optimizer/common/gllo_utils.h" 26 #include "tools/converter/parser/tf/functionalize_control_op_pass.h" 27 28 namespace mindspore::opt { 29 30 typedef enum { kThenBranch = 0, kElseBranch = 1 } BranchType; 31 32 // Functionalize all the switch-merge nodes of a loop-free graph into single switch node. 33 // Precondition: While loops must have been functionalized. 34 class FunctionalizeCond { 35 public: FunctionalizeCond(FuncGraphPtr fg,CNodePtr merge_node)36 FunctionalizeCond(FuncGraphPtr fg, CNodePtr merge_node) : fg_(fg), merge_node_(merge_node) {} 37 38 virtual ~FunctionalizeCond() = default; 39 40 STATUS Process(); 41 42 private: 43 STATUS GetSwitchBranchType(const CNodePtr &switch_cnode, BranchType *branch_type); 44 STATUS BranchSubGraphAddNodes(const FuncGraphPtr &graph, const AnfNodePtr &root_node, BranchType branch_type); 45 FuncGraphPtr CreateBranchGraph(const AnfNodePtr &node, std::string name, BranchType branch_type); 46 STATUS DegenerateNonControlFlow(const FuncGraphPtr &else_graph, const FuncGraphPtr &then_graph); 47 int PosInInputNodes(const CNodePtr &node); 48 STATUS IdentifySubgraphInput(const FuncGraphPtr &graph, std::string graph_name); 49 CNodePtr CreateNewIf(const FuncGraphPtr &else_branch, const FuncGraphPtr &then_branch); 50 STATUS VerifyPredictNode(); 51 void CheckBranchIsEffective(const CNodePtr &switch_cnode, BranchType branch_type); 52 53 bool then_is_effective_ = true; 54 bool else_is_effective_ = true; 55 FuncGraphPtr fg_ = nullptr; 56 CNodePtr merge_node_ = nullptr; 57 AnfNodePtr pred_node_ = nullptr; 58 CNodePtr then_switch_ = nullptr; 59 CNodePtr else_switch_ = nullptr; 60 std::vector<CNodePtr> input_nodes_{}; 61 std::vector<AnfNodePtr> pred_nodes_{}; 62 }; 63 } // namespace mindspore::opt 64 65 #endif // MINDSPORE_LITE_TOOLS_CONVERTER_PARSER_TF_FUNCTIONALIZE_COND_H_ 66