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_CONVERTER_LEGACY_OPTIMIZER_GRAPH_INFERSHAPE_PASS_H_ 18 #define MINDSPORE_LITE_TOOLS_CONVERTER_LEGACY_OPTIMIZER_GRAPH_INFERSHAPE_PASS_H_ 19 20 #include <unordered_map> 21 #include <memory> 22 #include <vector> 23 #include <string> 24 #include <utility> 25 #include <set> 26 #include "tools/common/graph_util.h" 27 #include "tools/converter/optimizer.h" 28 #include "include/registry/converter_context.h" 29 30 namespace mindspore { 31 namespace lite { 32 struct InferTensor { 33 std::vector<uint32_t> next_nodes_; 34 std::vector<uint32_t> prev_nodes_; 35 bool is_inferred_; 36 }; 37 38 class InferShapePass : public GraphPass { 39 public: InferShapePass(converter::FmkType fmk_type)40 explicit InferShapePass(converter::FmkType fmk_type) : fmk_type_(fmk_type) {} 41 ~InferShapePass() override = default; 42 STATUS Run(MetaGraphT *graph) override; 43 44 private: 45 int InitSearchTensor(const int64_t &subgraph_index, MetaGraphT *graph, std::vector<uint32_t> *infer_node_indexes); 46 void AddNextInferShapeNode(MetaGraphT *graph, std::vector<uint32_t> *infer_node_indexes, 47 std::vector<uint32_t> next_nodes_indexes, size_t index); 48 void AddOutputNodes(MetaGraphT *graph, std::vector<uint32_t> *infer_node_indexes, uint32_t infer_node_index); 49 void ResetIncorrectTensorShape(MetaGraphT *graph); 50 int InferPartialNode(const bool &is_tail_call, const std::unique_ptr<CNodeT> &call_node, const CNodeT *partial_node, 51 MetaGraphT *graph); 52 int InferSwitchOrSwitchLayerNode(const bool &is_tail_call, const std::unique_ptr<CNodeT> &call_node, 53 const std::unique_ptr<CNodeT> &switch_node, MetaGraphT *graph); 54 int InferCallNode(const std::unique_ptr<CNodeT> &call_node, MetaGraphT *graph); 55 int CopyPartialShapeToSubGraph(const CNodeT *partial_node, MetaGraphT *graph); 56 int SetNonTailCallOutputShape(const std::unique_ptr<CNodeT> &call_node, const CNodeT *partial_node, 57 MetaGraphT *graph); 58 void RestoreSubGraphInput(const CNodeT *partial_node, MetaGraphT *graph); 59 void InitInferTensor(MetaGraphT *graph); 60 int InferSubgraph(const int64_t &subgraph_index, MetaGraphT *graph); 61 62 converter::FmkType fmk_type_ = converter::kFmkTypeTf; 63 std::vector<InferTensor> tensors_ = {}; 64 std::set<CNodeT *> partial_cnode_inferred_{}; 65 }; 66 } // namespace lite 67 } // namespace mindspore 68 #endif // MINDSPORE_LITE_TOOLS_CONVERTER_LEGACY_OPTIMIZER_GRAPH_INFERSHAPE_PASS_H_ 69