• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 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_COMMON_META_GRAPH_SERIALIZER_H_
18 #define MINDSPORE_LITE_TOOLS_COMMON_META_GRAPH_SERIALIZER_H_
19 
20 #include <fstream>
21 #include <string>
22 #include "flatbuffers/flatbuffers.h"
23 #include "schema/inner/model_generated.h"
24 #include "utils/crypto.h"
25 
26 namespace mindspore::lite {
27 class MetaGraphSerializer {
28  public:
29   // get metagraph packed buffer
30   static uint8_t *GetMetaGraphPackedBuff(flatbuffers::FlatBufferBuilder *builder, const schema::MetaGraphT &graph,
31                                          size_t *data_size);
32 
33   // save serialized fb model
34   static int Save(const schema::MetaGraphT &graph, const std::string &output_path, const Byte *key = {},
35                   const size_t key_len = 0, const std::string &enc_mode = "");
36   static int Save(const schema::MetaGraphT &graph, const std::string &output_path, size_t *size, const Byte *key = {},
37                   const size_t key_len = 0, const std::string &enc_mode = "");
38 
39  private:
40   MetaGraphSerializer() = default;
41 
42   virtual ~MetaGraphSerializer();
43 
44   bool InitPath(const std::string &real_output_path);
45 
46   bool Init(const schema::MetaGraphT &graph, bool save_together = true);
47 
48   schema::ExternalDataT *AddExternalData(const char *data, size_t size);
49 
50   bool ExtraAndSerializeModelWeight(const schema::MetaGraphT &graph);
51 
52   bool SerializeModelAndUpdateWeight(const schema::MetaGraphT &meta_graphT, const Byte *key, const size_t key_len,
53                                      const std::string &enc_mode, size_t *size = 0);
54 
55   bool SerializeModel(const void *content, size_t size, const Byte *key, const size_t key_len,
56                       const std::string &enc_mode);
57 
58   int64_t cur_offset_ = 0;
59   std::string save_path_;
60   std::string model_name_;
61   std::string save_model_path_;
62   std::string save_data_path_;
63   std::fstream *model_fs_ = nullptr;
64   std::fstream *data_fs_ = nullptr;
65 };
66 }  // namespace mindspore::lite
67 
68 #endif  // MINDSPORE_LITE_TOOLS_COMMON_META_GRAPH_SERIALIZER_H_
69