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

Function run_model

imperative/python/megengine/tools/load_network_and_run.py:120–228  ·  view source on GitHub ↗
(args, graph, inputs, outputs, data)

Source from the content-addressed store, hash-verified

118
119
120def run_model(args, graph, inputs, outputs, data):
121 # must use level0 to avoid unintended opr modification
122 graph.options.graph_opt_level = 0
123
124 if args.weight_preprocess:
125 graph.enable_weight_preprocess()
126
127 logger.info("input tensors: ")
128 for k, v in data.items():
129 logger.info(" {}: {}".format(k, v.shape))
130
131 G.modify_opr_algo_strategy_inplace(outputs, get_execution_strategy(args))
132
133 if args.optimize_for_inference:
134 opt_kwargs = get_opt_kwargs(args)
135 outputs = G.optimize_for_inference(outputs, **opt_kwargs)
136
137 # embed inputs must be on the last, to avoid const fold
138 if args.embed_input:
139 outputs, inp_dict = tools.embed_inputs(outputs, data.values(), inputs=inputs)
140 else:
141 outputs, inp_dict = tools.convert_inputs(outputs, inputs=inputs)
142
143 if args.dump_cpp_model:
144 dump_content, _ = G.dump_graph(outputs, keep_var_name=2)
145 with open(args.dump_cpp_model, "wb") as file:
146 file.write(dump_content)
147 logger.info("C++ model written to {}".format(args.dump_cpp_model))
148
149 outputs, output_dict = tools.convert_outputs(outputs)
150
151 if args.profile:
152 profiler = tools.GraphProfiler(graph)
153
154 func = graph.compile(outputs)
155
156 if args.get_static_mem_info:
157 func.get_static_memory_alloc_info(args.get_static_mem_info)
158
159 def run():
160 if not args.embed_input:
161 for key in inp_dict:
162 inp_dict[key].set_value(mge.Tensor(data[key])._dev_tensor())
163 func.execute()
164 func.wait()
165 return [oup_node.get_value().numpy() for oup_node in output_dict.values()]
166
167 for i in range(args.warm_up):
168 logger.info("warming up {}".format(i))
169 run()
170
171 total_time = 0
172
173 for i in range(args.iter):
174 logger.info("iter {}".format(i))
175 start_time = time.time()
176 retval = run()
177 cur_time = time.time() - start_time

Callers 1

mainFunction · 0.85

Calls 15

get_device_countFunction · 0.90
get_opt_kwargsFunction · 0.85
printFunction · 0.85
itemsMethod · 0.80
valuesMethod · 0.80
GraphProfilerMethod · 0.80
get_prev_exec_timeMethod · 0.80
joinMethod · 0.80
get_execution_strategyFunction · 0.70
runFunction · 0.70
infoMethod · 0.45

Tested by

no test coverage detected