(args, graph, inputs, outputs, data)
| 118 | |
| 119 | |
| 120 | def 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 |
no test coverage detected