1 /** 2 * Copyright 2020 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_INCLUDE_API_SERIALIZATION_H 17 #define MINDSPORE_INCLUDE_API_SERIALIZATION_H 18 19 #include <string> 20 #include <vector> 21 #include <map> 22 #include <memory> 23 #include "include/api/status.h" 24 #include "include/api/types.h" 25 #include "include/api/model.h" 26 #include "include/api/graph.h" 27 #include "include/api/dual_abi_helper.h" 28 29 namespace mindspore { 30 /// \brief The Serialization class is used to summarize methods for reading and writing model files. 31 class MS_API Serialization { 32 public: 33 /// \brief Loads a model file from memory buffer. 34 /// 35 /// \param[in] model_data A buffer filled by model file. 36 /// \param[in] data_size The size of the buffer. 37 /// \param[in] model_type The Type of model file, options are ModelType::kMindIR, ModelType::kOM. 38 /// \param[out] graph The output parameter, an object saves graph data. 39 /// \param[in] dec_key The decryption key, key length is 16, 24, or 32. 40 /// \param[in] dec_mode The decryption mode, optional options are AES-GCM, AES-CBC. 41 /// 42 /// \return Status. 43 inline static Status Load(const void *model_data, size_t data_size, ModelType model_type, Graph *graph, 44 const Key &dec_key = {}, const std::string &dec_mode = kDecModeAesGcm); 45 46 /// \brief Loads a model file from path, is not supported on MindSpore Lite. 47 /// 48 /// \param[in] file The path of model file. 49 /// \param[in] model_type The Type of model file, options are ModelType::kMindIR, ModelType::kOM. 50 /// \param[out] graph The output parameter, an object saves graph data. 51 /// \param[in] dec_key The decryption key, key length is 16, 24, or 32. 52 /// \param[in] dec_mode The decryption mode, optional options are AES-GCM, AES-CBC. 53 /// 54 /// \return Status. 55 inline static Status Load(const std::string &file, ModelType model_type, Graph *graph, const Key &dec_key = {}, 56 const std::string &dec_mode = kDecModeAesGcm); 57 58 /// \brief Load multiple models from multiple files, MindSpore Lite does not provide this feature. 59 /// 60 /// \param[in] files The path of model files. 61 /// \param[in] model_type The Type of model file, options are ModelType::kMindIR, ModelType::kOM. 62 /// \param[out] graph The output parameter, an object saves graph data. 63 /// \param[in] dec_key The decryption key, key length is 16, 24, or 32. 64 /// \param[in] dec_mode The decryption mode, optional options are AES-GCM, AES-CBC. 65 /// 66 /// \return Status. 67 inline static Status Load(const std::vector<std::string> &files, ModelType model_type, std::vector<Graph> *graphs, 68 const Key &dec_key = {}, const std::string &dec_mode = kDecModeAesGcm); 69 static Status SetParameters(const std::map<std::string, Buffer> ¶meters, Model *model); 70 static Status ExportModel(const Model &model, ModelType model_type, Buffer *model_data); 71 static Status ExportModel(const Model &model, ModelType model_type, const std::string &model_file, 72 QuantizationType quantization_type = kNoQuant, bool export_inference_only = true, 73 std::vector<std::string> output_tensor_name = {}); 74 75 private: 76 static Status Load(const void *model_data, size_t data_size, ModelType model_type, Graph *graph, const Key &dec_key, 77 const std::vector<char> &dec_mode); 78 static Status Load(const std::vector<char> &file, ModelType model_type, Graph *graph); 79 static Status Load(const std::vector<char> &file, ModelType model_type, Graph *graph, const Key &dec_key, 80 const std::vector<char> &dec_mode); 81 static Status Load(const std::vector<std::vector<char>> &files, ModelType model_type, std::vector<Graph> *graphs, 82 const Key &dec_key, const std::vector<char> &dec_mode); 83 }; 84 85 Status Serialization::Load(const void *model_data, size_t data_size, ModelType model_type, Graph *graph, 86 const Key &dec_key, const std::string &dec_mode) { 87 return Load(model_data, data_size, model_type, graph, dec_key, StringToChar(dec_mode)); 88 } 89 90 Status Serialization::Load(const std::string &file, ModelType model_type, Graph *graph, const Key &dec_key, 91 const std::string &dec_mode) { 92 return Load(StringToChar(file), model_type, graph, dec_key, StringToChar(dec_mode)); 93 } 94 95 Status Serialization::Load(const std::vector<std::string> &files, ModelType model_type, std::vector<Graph> *graphs, 96 const Key &dec_key, const std::string &dec_mode) { 97 return Load(VectorStringToChar(files), model_type, graphs, dec_key, StringToChar(dec_mode)); 98 } 99 } // namespace mindspore 100 #endif // MINDSPORE_INCLUDE_API_SERIALIZATION_H 101