MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / load_model

Method load_model

lite/load_and_run/src/models/model_mdl.cpp:15–69  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13}
14
15void ModelMdl::load_model() {
16 //! read dump file
17 if (share_model_mem) {
18 mgb_log("enable share model memory");
19 FILE* fin = fopen(model_path.c_str(), "rb");
20 mgb_assert(fin, "failed to open %s: %s", model_path.c_str(), strerror(errno));
21 fseek(fin, 0, SEEK_END);
22 size_t size = ftell(fin);
23 fseek(fin, 0, SEEK_SET);
24
25 void* ptr = malloc(size);
26 std::shared_ptr<void> buf{ptr, free};
27 auto nr = fread(buf.get(), 1, size, fin);
28 mgb_assert(nr == size, "read model file failed");
29 fclose(fin);
30
31 m_model_file = mgb::serialization::InputFile::make_mem_proxy(buf, size);
32 } else {
33 m_model_file = mgb::serialization::InputFile::make_fs(model_path.c_str());
34 }
35
36 //! get dump_with_testcase model testcase number
37 char magic[8];
38 m_model_file->read(magic, sizeof(magic));
39 if (strncmp(magic, "mgbtest0", 8)) {
40 m_model_file->rewind();
41 } else {
42 m_model_file->read(&testcase_num, sizeof(testcase_num));
43 }
44
45 m_format =
46 mgb::serialization::GraphLoader::identify_graph_dump_format(*m_model_file);
47 mgb_assert(
48 m_format.valid(),
49 "invalid format, please make sure model is dumped by GraphDumper");
50
51 //! load computing graph of model
52 m_loader = mgb::serialization::GraphLoader::make(
53 std::move(m_model_file), m_format.val());
54 m_load_result = m_loader->load(m_load_config, false);
55 m_load_config.comp_graph.reset();
56
57 // get testcase input generated by dump_with_testcase.py
58 if (testcase_num) {
59 for (auto&& i : m_load_result.tensor_map) {
60 test_input_tensors.emplace_back(i.first, i.second.get());
61 }
62 std::sort(test_input_tensors.begin(), test_input_tensors.end());
63 }
64 // initialize output callback
65 for (size_t i = 0; i < m_load_result.output_var_list.size(); i++) {
66 mgb::ComputingGraph::Callback cb;
67 m_callbacks.push_back(cb);
68 }
69}
70
71void ModelMdl::make_output_spec() {
72 for (size_t i = 0; i < m_load_result.output_var_list.size(); i++) {

Callers 6

run_sublineMethod · 0.45
dump_modelMethod · 0.45
LITE_load_model_from_memFunction · 0.45

Calls 13

sortFunction · 0.85
emplace_backMethod · 0.80
makeFunction · 0.50
getMethod · 0.45
readMethod · 0.45
rewindMethod · 0.45
validMethod · 0.45
loadMethod · 0.45
resetMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected