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 #ifndef MINDSPORE_LITE_INCLUDE_MODEL_H_ 17 #define MINDSPORE_LITE_INCLUDE_MODEL_H_ 18 19 #include <map> 20 #include <memory> 21 #include <string> 22 #include <vector> 23 #include "include/api/visible.h" 24 25 namespace mindspore { 26 namespace schema { 27 struct Tensor; 28 } // namespace schema 29 30 namespace lite { 31 typedef enum { ModelType_MSLite, ModelType_MindIR } LiteModelType; 32 33 // LiteGraph can be considered as a light weight and subset of FuncGraph, it can not support the advanced expression of 34 // FuncGraph, e.g., non-tail recursive. 35 struct MS_API LiteGraph { 36 struct Node { 37 std::string name_; 38 std::string op_type_; 39 int node_type_; 40 const void *primitive_ = nullptr; 41 std::shared_ptr<void> base_operator_ = nullptr; 42 std::vector<uint32_t> input_indices_; 43 std::vector<uint32_t> output_indices_; 44 int quant_type_; 45 int device_type_ = -1; 46 }; 47 struct SubGraph { 48 std::string name_; 49 std::vector<uint32_t> input_indices_; 50 std::vector<uint32_t> output_indices_; 51 std::vector<uint32_t> node_indices_; 52 std::vector<uint32_t> tensor_indices_; 53 }; 54 std::string name_; 55 std::string version_; 56 std::vector<uint32_t> input_indices_; 57 std::vector<uint32_t> output_indices_; 58 std::vector<mindspore::schema::Tensor *> all_tensors_; 59 std::vector<Node *> all_nodes_; 60 std::vector<SubGraph *> sub_graphs_; 61 62 std::string ToString() const; 63 }; 64 65 struct MS_API Model { 66 LiteGraph graph_; 67 char *buf = nullptr; 68 size_t buf_size_ = 0; 69 LiteModelType model_type_ = mindspore::lite::ModelType_MSLite; 70 void *deobf = nullptr; 71 72 /// \brief Static method to create a Model pointer. 73 static Model *Import(const char *model_buf, size_t size); 74 75 /// \brief Static method to create a Model pointer. 76 static Model *Import(const char *filename); 77 78 /// \brief method to export model to file. 79 static int Export(Model *model, const char *filename); 80 81 /// \brief method to export model to buffer. 82 static int Export(Model *model, char *buf, size_t *size); 83 84 /// \brief Free meta graph temporary buffer 85 virtual void Free() = 0; 86 87 /// \brief Free all temporary buffer.EG: nodes in the model. 88 virtual void Destroy() = 0; 89 90 /// \brief Model destruct, free all memory 91 virtual ~Model() = default; 92 }; 93 } // namespace lite 94 } // namespace mindspore 95 96 #endif // MINDSPORE_LITE_INCLUDE_MODEL_H_ 97