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 #include "include/api/serialization.h"
17 #include <fstream>
18 #include <sstream>
19 #include "cxx_api/graph/graph_data.h"
20 #include "utils/log_adapter.h"
21 #include "mindspore/core/load_mindir/load_model.h"
22 #if !defined(_WIN32) && !defined(_WIN64)
23 #include "cxx_api/dlutils.h"
24 #include "minddata/dataset/engine/serdes.h"
25 #include "minddata/dataset/include/dataset/execute.h"
26 #endif
27 #include "utils/crypto.h"
28
29 namespace mindspore {
RealPath(const std::string & file,std::string * realpath_str)30 static Status RealPath(const std::string &file, std::string *realpath_str) {
31 MS_EXCEPTION_IF_NULL(realpath_str);
32 char real_path_mem[PATH_MAX] = {0};
33 char *real_path_ret = nullptr;
34 #if defined(_WIN32) || defined(_WIN64)
35 real_path_ret = _fullpath(real_path_mem, common::SafeCStr(file), PATH_MAX);
36 #else
37 real_path_ret = realpath(common::SafeCStr(file), real_path_mem);
38 #endif
39 if (real_path_ret == nullptr) {
40 return Status(kMEInvalidInput, "File: " + file + " does not exist.");
41 }
42 *realpath_str = real_path_mem;
43 return kSuccess;
44 }
45
ReadFile(const std::string & file)46 static Buffer ReadFile(const std::string &file) {
47 Buffer buffer;
48 if (file.empty()) {
49 MS_LOG(ERROR) << "Pointer file is nullptr";
50 return buffer;
51 }
52
53 std::string real_path;
54 auto status = RealPath(file, &real_path);
55 if (status != kSuccess) {
56 MS_LOG(ERROR) << status.GetErrDescription();
57 return buffer;
58 }
59
60 std::ifstream ifs(real_path);
61 if (!ifs.good()) {
62 MS_LOG(ERROR) << "File: " << real_path << " does not exist";
63 return buffer;
64 }
65
66 if (!ifs.is_open()) {
67 MS_LOG(ERROR) << "File: " << real_path << " open failed";
68 return buffer;
69 }
70
71 (void)ifs.seekg(0, std::ios::end);
72 size_t size = static_cast<size_t>(ifs.tellg());
73 buffer.ResizeData(size);
74 if (buffer.DataSize() != size) {
75 MS_LOG(ERROR) << "Malloc buf failed, file: " << real_path;
76 ifs.close();
77 return buffer;
78 }
79
80 (void)ifs.seekg(0, std::ios::beg);
81 (void)ifs.read(reinterpret_cast<char *>(buffer.MutableData()), static_cast<std::streamsize>(size));
82 ifs.close();
83
84 return buffer;
85 }
86
Key(const char * dec_key,size_t key_len)87 Key::Key(const char *dec_key, size_t key_len) {
88 len = 0;
89 if (key_len >= max_key_len) {
90 MS_LOG(ERROR) << "Invalid key len " << key_len << " is more than max key len " << max_key_len;
91 return;
92 }
93
94 auto sec_ret = memcpy_s(key, max_key_len, dec_key, key_len);
95 if (sec_ret != EOK) {
96 MS_LOG(ERROR) << "memcpy_s failed, src_len = " << key_len << ", dst_len = " << max_key_len << ", ret = " << sec_ret;
97 return;
98 }
99
100 len = key_len;
101 }
102
Load(const void * model_data,size_t data_size,ModelType model_type,Graph * graph,const Key & dec_key,const std::vector<char> & dec_mode)103 Status Serialization::Load(const void *model_data, size_t data_size, ModelType model_type, Graph *graph,
104 const Key &dec_key, const std::vector<char> &dec_mode) {
105 std::stringstream err_msg;
106 if (graph == nullptr) {
107 err_msg << "Output args graph is nullptr.";
108 MS_LOG(ERROR) << err_msg.str();
109 return Status(kMEInvalidInput, err_msg.str());
110 }
111
112 if (model_type == kMindIR) {
113 FuncGraphPtr anf_graph = nullptr;
114 try {
115 if (dec_key.len > dec_key.max_key_len) {
116 err_msg << "The key length exceeds maximum length: " << dec_key.max_key_len;
117 MS_LOG(ERROR) << err_msg.str();
118 return Status(kMEInvalidInput, err_msg.str());
119 } else if (dec_key.len == 0) {
120 if (IsCipherFile(reinterpret_cast<const unsigned char *>(model_data))) {
121 err_msg << "Load model failed. The model_data may be encrypted, please pass in correct key.";
122 MS_LOG(ERROR) << err_msg.str();
123 return Status(kMEInvalidInput, err_msg.str());
124 } else {
125 anf_graph = ConvertStreamToFuncGraph(reinterpret_cast<const char *>(model_data), data_size);
126 }
127 } else {
128 size_t plain_data_size;
129 auto plain_data = mindspore::Decrypt(&plain_data_size, reinterpret_cast<const unsigned char *>(model_data),
130 data_size, dec_key.key, dec_key.len, CharToString(dec_mode));
131 if (plain_data == nullptr) {
132 err_msg << "Load model failed. Please check the valid of dec_key and dec_mode.";
133 MS_LOG(ERROR) << err_msg.str();
134 return Status(kMEInvalidInput, err_msg.str());
135 }
136 anf_graph = ConvertStreamToFuncGraph(reinterpret_cast<const char *>(plain_data.get()), plain_data_size);
137 }
138 } catch (const std::exception &) {
139 err_msg << "Load model failed. Please check the valid of dec_key and dec_mode.";
140 MS_LOG(ERROR) << err_msg.str();
141 return Status(kMEInvalidInput, err_msg.str());
142 }
143
144 *graph = Graph(std::make_shared<Graph::GraphData>(anf_graph, kMindIR));
145 return kSuccess;
146 } else if (model_type == kOM) {
147 *graph = Graph(std::make_shared<Graph::GraphData>(Buffer(model_data, data_size), kOM));
148 return kSuccess;
149 }
150
151 err_msg << "Unsupported ModelType " << model_type;
152 MS_LOG(ERROR) << err_msg.str();
153 return Status(kMEInvalidInput, err_msg.str());
154 }
155
Load(const std::vector<char> & file,ModelType model_type,Graph * graph)156 Status Serialization::Load(const std::vector<char> &file, ModelType model_type, Graph *graph) {
157 return Load(file, model_type, graph, Key{}, StringToChar(kDecModeAesGcm));
158 }
159
Load(const std::vector<char> & file,ModelType model_type,Graph * graph,const Key & dec_key,const std::vector<char> & dec_mode)160 Status Serialization::Load(const std::vector<char> &file, ModelType model_type, Graph *graph, const Key &dec_key,
161 const std::vector<char> &dec_mode) {
162 std::stringstream err_msg;
163 if (graph == nullptr) {
164 MS_LOG(ERROR) << "Output args graph is nullptr.";
165 return Status(kMEInvalidInput, "Output args graph is nullptr.");
166 }
167
168 std::string file_path;
169 auto status = RealPath(CharToString(file), &file_path);
170 if (status != kSuccess) {
171 MS_LOG(ERROR) << status.GetErrDescription();
172 return status;
173 }
174
175 if (model_type == kMindIR) {
176 FuncGraphPtr anf_graph;
177 if (dec_key.len > dec_key.max_key_len) {
178 err_msg << "The key length exceeds maximum length: " << dec_key.max_key_len;
179 MS_LOG(ERROR) << err_msg.str();
180 return Status(kMEInvalidInput, err_msg.str());
181 } else if (dec_key.len == 0 && IsCipherFile(file_path)) {
182 err_msg << "Load model failed. The file may be encrypted, please pass in correct key.";
183 MS_LOG(ERROR) << err_msg.str();
184 return Status(kMEInvalidInput, err_msg.str());
185 } else {
186 anf_graph =
187 LoadMindIR(file_path, false, dec_key.len == 0 ? nullptr : dec_key.key, dec_key.len, CharToString(dec_mode));
188 }
189 if (anf_graph == nullptr) {
190 err_msg << "Load model failed. Please check the valid of dec_key and dec_mode";
191 MS_LOG(ERROR) << err_msg.str();
192 return Status(kMEInvalidInput, err_msg.str());
193 }
194 auto graph_data = std::make_shared<Graph::GraphData>(anf_graph, kMindIR);
195 #if !defined(_WIN32) && !defined(_WIN64)
196 // Config preprocessor, temporary way to let mindspore.so depends on _c_dataengine
197 std::string preprocessor = LoadPreprocess(file_path);
198 if (!preprocessor.empty()) {
199 std::string dataengine_so_path;
200 Status dlret = DLSoPath(&dataengine_so_path);
201 CHECK_FAIL_AND_RELEASE(dlret, nullptr, "Parse dataengine_so failed: " + dlret.GetErrDescription());
202
203 void *handle = nullptr;
204 void *function = nullptr;
205 dlret = DLSoOpen(dataengine_so_path, "ParseMindIRPreprocess_C", &handle, &function);
206 CHECK_FAIL_AND_RELEASE(dlret, handle, "Parse ParseMindIRPreprocess_C failed: " + dlret.GetErrDescription());
207
208 auto ParseMindIRPreprocessFun =
209 (void (*)(const std::string &, const std::string &, std::vector<std::shared_ptr<mindspore::dataset::Execute>> *,
210 Status *))(function);
211
212 std::vector<std::shared_ptr<dataset::Execute>> data_graph;
213 ParseMindIRPreprocessFun(preprocessor, "image", &data_graph, &dlret);
214 CHECK_FAIL_AND_RELEASE(dlret, handle, "Load preprocess failed: " + dlret.GetErrDescription());
215 DLSoClose(handle);
216 if (!data_graph.empty()) {
217 graph_data->SetPreprocess(data_graph);
218 }
219 }
220 #endif
221 *graph = Graph(graph_data);
222 return kSuccess;
223 } else if (model_type == kOM) {
224 Buffer data = ReadFile(file_path);
225 if (data.Data() == nullptr) {
226 err_msg << "Read file " << file_path << " failed.";
227 MS_LOG(ERROR) << err_msg.str();
228 return Status(kMEInvalidInput, err_msg.str());
229 }
230 *graph = Graph(std::make_shared<Graph::GraphData>(data, kOM));
231 return kSuccess;
232 }
233
234 err_msg << "Unsupported ModelType " << model_type;
235 MS_LOG(ERROR) << err_msg.str();
236 return Status(kMEInvalidInput, err_msg.str());
237 }
238
Load(const std::vector<std::vector<char>> & files,ModelType model_type,std::vector<Graph> * graphs,const Key & dec_key,const std::vector<char> & dec_mode)239 Status Serialization::Load(const std::vector<std::vector<char>> &files, ModelType model_type,
240 std::vector<Graph> *graphs, const Key &dec_key, const std::vector<char> &dec_mode) {
241 std::stringstream err_msg;
242 if (graphs == nullptr) {
243 MS_LOG(ERROR) << "Output args graph is nullptr.";
244 return Status(kMEInvalidInput, "Output args graph is nullptr.");
245 }
246
247 if (files.size() == 1) {
248 std::vector<Graph> result(files.size());
249 auto ret = Load(files[0], model_type, &result[0], dec_key, dec_mode);
250 *graphs = std::move(result);
251 return ret;
252 }
253
254 std::vector<std::string> files_path;
255 for (const auto &file : files) {
256 std::string file_path;
257 auto status = RealPath(CharToString(file), &file_path);
258 if (status != kSuccess) {
259 MS_LOG(ERROR) << status.GetErrDescription();
260 return status;
261 }
262 files_path.emplace_back(std::move(file_path));
263 }
264
265 if (model_type == kMindIR) {
266 if (dec_key.len > dec_key.max_key_len) {
267 err_msg << "The key length exceeds maximum length: " << dec_key.max_key_len;
268 MS_LOG(ERROR) << err_msg.str();
269 return Status(kMEInvalidInput, err_msg.str());
270 }
271 auto anf_graphs =
272 LoadMindIRs(files_path, false, dec_key.len == 0 ? nullptr : dec_key.key, dec_key.len, CharToString(dec_mode));
273 if (anf_graphs.size() != files_path.size()) {
274 err_msg << "Load model failed, " << files_path.size() << " files got " << anf_graphs.size() << " graphs.";
275 MS_LOG(ERROR) << err_msg.str();
276 return Status(kMEInvalidInput, err_msg.str());
277 }
278 #if !defined(_WIN32) && !defined(_WIN64)
279 // Dataset so loading
280 std::string dataengine_so_path;
281 Status dlret = DLSoPath(&dataengine_so_path);
282 CHECK_FAIL_AND_RELEASE(dlret, nullptr, "Parse dataengine_so failed: " + dlret.GetErrDescription());
283
284 void *handle = nullptr;
285 void *function = nullptr;
286 dlret = DLSoOpen(dataengine_so_path, "ParseMindIRPreprocess_C", &handle, &function);
287 CHECK_FAIL_AND_RELEASE(dlret, handle, "Parse ParseMindIRPreprocess_C failed: " + dlret.GetErrDescription());
288
289 auto ParseMindIRPreprocessFun =
290 (void (*)(const std::string &, const std::string &, std::vector<std::shared_ptr<mindspore::dataset::Execute>> *,
291 Status *))(function);
292 #endif
293 std::vector<Graph> results;
294 for (size_t i = 0; i < anf_graphs.size(); ++i) {
295 if (anf_graphs[i] == nullptr) {
296 if (dec_key.len == 0 && IsCipherFile(files_path[i])) {
297 err_msg << "Load model failed. The file " << files_path[i] << " be encrypted, please pass in correct key.";
298 } else {
299 err_msg << "Load model " << files_path[i] << " failed.";
300 }
301 MS_LOG(ERROR) << err_msg.str();
302 #if !defined(_WIN32) && !defined(_WIN64)
303 DLSoClose(handle);
304 #endif
305 return Status(kMEInvalidInput, err_msg.str());
306 }
307 auto graph_data = std::make_shared<Graph::GraphData>(anf_graphs[i], kMindIR);
308
309 #if !defined(_WIN32) && !defined(_WIN64)
310 // Config preprocessor, temporary way to let mindspore.so depends on _c_dataengine
311 std::string preprocessor = LoadPreprocess(files_path[i]);
312 if (!preprocessor.empty()) {
313 std::vector<std::shared_ptr<dataset::Execute>> data_graph;
314 ParseMindIRPreprocessFun(preprocessor, "image", &data_graph, &dlret);
315 CHECK_FAIL_AND_RELEASE(dlret, handle, "Load preprocess failed: " + dlret.GetErrDescription());
316 if (!data_graph.empty()) {
317 graph_data->SetPreprocess(data_graph);
318 }
319 }
320 #endif
321 results.emplace_back(graph_data);
322 }
323 #if !defined(_WIN32) && !defined(_WIN64)
324 // Dataset so release
325 DLSoClose(handle);
326 #endif
327 *graphs = std::move(results);
328 return kSuccess;
329 }
330
331 err_msg << "Unsupported ModelType " << model_type;
332 MS_LOG(ERROR) << err_msg.str();
333 return Status(kMEInvalidInput, err_msg.str());
334 }
335
SetParameters(const std::map<std::string,Buffer> &,Model *)336 Status Serialization::SetParameters(const std::map<std::string, Buffer> &, Model *) {
337 MS_LOG(ERROR) << "Unsupported feature.";
338 return kMEFailed;
339 }
340
ExportModel(const Model &,ModelType,Buffer *)341 Status Serialization::ExportModel(const Model &, ModelType, Buffer *) {
342 MS_LOG(ERROR) << "Unsupported feature.";
343 return kMEFailed;
344 }
345
ExportModel(const Model &,ModelType,const std::string &,QuantizationType,bool,std::vector<std::string> output_tensor_name)346 Status Serialization::ExportModel(const Model &, ModelType, const std::string &, QuantizationType, bool,
347 std::vector<std::string> output_tensor_name) {
348 MS_LOG(ERROR) << "Unsupported feature.";
349 return kMEFailed;
350 }
351 } // namespace mindspore
352