• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_LITE_TOOLS_OPTIMIZER_GRAPH_REDUNDANT_OP_REMOVE_PASS_H_
18 #define MINDSPORE_LITE_TOOLS_OPTIMIZER_GRAPH_REDUNDANT_OP_REMOVE_PASS_H_
19 #include <string>
20 #include <set>
21 #include "include/backend/optimizer/pass.h"
22 #include "tools/optimizer/common/gllo_utils.h"
23 #include "tools/lite_exporter/fetch_content.h"
24 
25 using mindspore::converter::FmkType;
26 namespace mindspore::opt {
27 class RemoveRedundantOpPass : public Pass {
28  public:
RemoveRedundantOpPass(bool is_train_model)29   explicit RemoveRedundantOpPass(bool is_train_model)
30       : Pass("remove_redundant_op_pass"), is_train_model_(is_train_model) {}
RemoveRedundantOpPass(bool is_train_model,bool remove_side_effect)31   explicit RemoveRedundantOpPass(bool is_train_model, bool remove_side_effect)
32       : Pass("remove_redundant_op_pass"), is_train_model_(is_train_model), remove_side_effect_(remove_side_effect) {}
RemoveRedundantOpPass(bool is_train_model,bool remove_side_effect,bool keep_update_state)33   RemoveRedundantOpPass(bool is_train_model, bool remove_side_effect, bool keep_update_state)
34       : Pass("remove_redundant_op_pass"),
35         is_train_model_(is_train_model),
36         remove_side_effect_(remove_side_effect),
37         keep_update_state_(keep_update_state) {}
38   ~RemoveRedundantOpPass() override = default;
39   int RemoveUmonad(const FuncGraphPtr &graph, const FuncGraphManagerPtr &manager);
40   int ReplaceOp(const AnfNodePtr &anf_node, const FuncGraphManagerPtr &manager);
41   int ReplaceUpdateStateOp(const FuncGraphPtr &func_graph, const AnfNodePtr &anf_node);
42   int ReplaceTupleGetItem(const AnfNodePtr &anf_node, const FuncGraphManagerPtr &manager);
43   int RemoveDropoutOp(const AnfNodePtr &anf_node, const FuncGraphManagerPtr &manager);
44   int RemoveInvalidPadOp(const AnfNodePtr &anf_node, const FuncGraphManagerPtr &manager);
45   int RemoveInvalidTransposeOp(const AnfNodePtr &anf_node, const FuncGraphManagerPtr &manager);
46   int FlattenMakeTuple(const FuncGraphPtr &graph, const FuncGraphManagerPtr &manager);
47   bool Run(const FuncGraphPtr &graph) override;
48 
49  private:
50   int GetConstDataFromInputNode(const CNodePtr &cnode, lite::DataInfo *data_info);
51   int RemoveRedundantOp(const FuncGraphPtr &func_graph, const FuncGraphManagerPtr &manager, const AnfNodePtr &node);
52   bool is_train_model_ = false;
53   bool remove_side_effect_ = false;
54   bool keep_update_state_ = false;
55   std::set<AnfNodePtr> remove_cnode_;
56 };
57 }  // namespace mindspore::opt
58 #endif  // MINDSPORE_LITE_TOOLS_OPTIMIZER_GRAPH_REDUNDANT_OP_REMOVE_PASS_H_
59