1 /** 2 * Copyright 2020-2023 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_COMMON_GRAPH_UTIL_H_ 18 #define MINDSPORE_LITE_TOOLS_COMMON_GRAPH_UTIL_H_ 19 20 #include <cstdlib> 21 #include <unordered_map> 22 #include <unordered_set> 23 #include <string> 24 #include <utility> 25 #include <memory> 26 #include <vector> 27 #include <map> 28 #include <set> 29 #include <algorithm> 30 #include <numeric> 31 #include <limits> 32 #include <functional> 33 #include "include/errorcode.h" 34 #include "schema/inner/model_generated.h" 35 #include "src/common/graph_util.h" 36 #include "ir/anf.h" 37 #include "ir/func_graph.h" 38 #include "nnacl/op_base.h" 39 #include "tools/common/node_util.h" 40 #include "tools/converter/cxx_api/converter_para.h" 41 42 namespace mindspore { 43 namespace lite { 44 #define MAX_GRAPH_SIZE 1024 45 enum InsertPlace { kBefore, kAfter }; 46 47 using NodeIter = std::vector<std::unique_ptr<schema::CNodeT>>::iterator; 48 49 using OpDefCopyer = std::function<std::unique_ptr<schema::CNodeT>(const schema::CNodeT &)>; 50 51 OpDefCopyer GetSimpleOpCopyer(); 52 53 int SetFuncGraphOutput(const FuncGraphPtr &graph, const std::vector<AnfNodePtr> &outputs); 54 55 STATUS AddTensor2Node(schema::MetaGraphT *graphT, uint32_t nodeIdx, std::unique_ptr<schema::TensorT> tensor, 56 InsertPlace place = kBefore); 57 58 STATUS ReplaceTensorOfNode(schema::MetaGraphT *graphT, uint32_t nodeIdx, uint32_t inTensorIdx, 59 std::unique_ptr<schema::TensorT> tensor); 60 61 NodeIter InsertNode(schema::MetaGraphT *graphT, uint32_t existNodeIdx, InsertPlace place, size_t inoutIndex, 62 std::unique_ptr<schema::CNodeT> toAddNode, STATUS *errorCode, int *insert_num, 63 const OpDefCopyer &opDefCopyer = GetSimpleOpCopyer()); 64 65 NodeIter InsertNode(schema::MetaGraphT *graphT, NodeIter existNodeIter, InsertPlace place, size_t inoutIndexIdx, 66 std::unique_ptr<schema::CNodeT> toAddNode, STATUS *errorCode, int *insert_num, 67 const OpDefCopyer &opDefCopyer = GetSimpleOpCopyer()); 68 69 NodeIter InsertNodeBefore(schema::MetaGraphT *graphT, NodeIter existNodeIter, size_t inputIndexIdx, 70 std::unique_ptr<schema::CNodeT> toAddNode, STATUS *errorCode, int *insert_num, 71 const OpDefCopyer &opDefCopyer); 72 73 NodeIter InsertNodeAfter(schema::MetaGraphT *graphT, NodeIter existNodeIter, size_t outputIndexIdx, 74 std::unique_ptr<schema::CNodeT> toAddNode, STATUS *errorCode, int *insert_num, 75 const OpDefCopyer &opDefCopyery); 76 77 STATUS ValidateFileStr(const std::string &modelFile, const std::string &fileType); 78 79 void SetSubgraphTensorIndices(schema::MetaGraphT *meta_graphT); 80 81 std::string GetModelName(const std::string &modelFile); 82 83 std::vector<int> GetTransposePerm(schema::MetaGraphT *graph, const std::unique_ptr<schema::CNodeT> &cnode); 84 85 TypeId GetAbstractTensorDtype(const abstract::AbstractTensorPtr &tensor); 86 87 TypeId GetParameterDtype(const ParameterPtr ¶m_node); 88 89 STATUS UpdateFuncGraphInputsAndOutputsDtype(const FuncGraphPtr &func_graph); 90 91 STATUS GetFuncGraphOutputsInfo(const FuncGraphPtr &func_graph, std::vector<std::pair<AnfNodePtr, int64_t>> *outputs, 92 std::vector<std::string> *output_names, std::vector<std::vector<int64_t>> *output_dims); 93 94 STATUS UpdateGraphOutputName(schema::MetaGraphT *meta_graph); 95 96 int TransferMetaGraph(const schema::MetaGraphT &graph, void **model_buf, size_t *size); 97 98 STATUS GetShapeVectorAndIdxFromCNode(const CNodePtr &cnode, std::vector<int64_t> *shape_vector, size_t *idx = nullptr); 99 100 STATUS GetShapeVectorFromParameter(const mindspore::ParameterPtr ¶m_node, std::vector<int64_t> *shape_vector); 101 102 STATUS GetCNodeOrParameterShapeVec(const AnfNodePtr &anf_node, std::vector<int> *shape); 103 104 int InitEncryptKey(const std::shared_ptr<ConverterPara> ¶m, unsigned char *encKey, size_t *keyLen); 105 } // namespace lite 106 } // namespace mindspore 107 108 #endif // MINDSPORE_LITE_TOOLS_COMMON_GRAPH_UTIL_H_ 109