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