• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 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_INFERSHAPE_PASS_H_
18 #define MINDSPORE_LITE_TOOLS_OPTIMIZER_GRAPH_INFERSHAPE_PASS_H_
19 
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 #include "include/backend/optimizer/pass.h"
25 #include "tools/optimizer/graph/node_infershape.h"
26 
27 namespace mindspore {
28 namespace opt {
29 class InferShapePass : public Pass {
30  public:
31   explicit InferShapePass(FmkType fmk_type = converter::kFmkTypeMs, bool train_flag = false,
32                           bool take_infer_invalid_as_failure = false, const std::string &name = "InferShapePass")
Pass(name)33       : Pass(name), fmk_type_(fmk_type), train_flag_(train_flag) {}
34   ~InferShapePass() override = default;
35   bool Run(const FuncGraphPtr &func_graph) override;
36 
37  protected:
PostProcess(const FuncGraphPtr & func_graph,const CNodePtr & cnode)38   virtual STATUS PostProcess(const FuncGraphPtr &func_graph, const CNodePtr &cnode) { return lite::RET_OK; }
39 
40  private:
41   bool JudgeAllOpsCanInfer(const FuncGraphPtr &func_graph);
42   STATUS InferProcess(const FuncGraphPtr &func_graph);
43   STATUS InferProcessSubGraph(const FuncGraphPtr &func_graph, const CNodePtr &cnode);
44   STATUS SetSubGraphInput(const CNodePtr &cnode, const FuncGraphPtr &sub_graph);
45   STATUS SetSubGraphOutput(const FuncGraphPtr &sub_graph);
46   STATUS SetSubGraphAbstract(const CNodePtr &cnode, const FuncGraphPtr &sub_graph);
47   int ResetSubGraphInput();
48 
49  protected:
50   FmkType fmk_type_{converter::kFmkTypeMs};
51   bool train_flag_{false};
52   std::shared_ptr<NodeInferShape> node_infer_shape_{nullptr};
53   std::map<FuncGraphPtr, std::vector<AnfNodePtr>> sub_inputs_map_{};
54   FuncGraphManagerPtr manager_{nullptr};
55 };
56 }  // namespace opt
57 }  // namespace mindspore
58 #endif  // MINDSPORE_LITE_TOOLS_OPTIMIZER_GRAPH_INFERSHAPE_PASS_H_
59