|
| 1 | +import tvm |
| 2 | +import numpy as np |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import time |
| 6 | + |
| 7 | +def run_model(model_dir, input_fp, output_fp, warmup_trials, run_trials, cuda, try_randin): |
| 8 | + # import compiled graph |
| 9 | + print("=> [TVM on TX2] using model files in {}".format(model_dir)) |
| 10 | + assert(os.path.isdir(model_dir)) |
| 11 | + |
| 12 | + print("=> [TVM on TX2] loading model lib and ptx") |
| 13 | + loaded_lib = tvm.module.load(os.path.join(model_dir, "deploy_lib.o")) |
| 14 | + if cuda: |
| 15 | + dev_lib = tvm.module.load(os.path.join(model_dir, "deploy_cuda.ptx")) |
| 16 | + loaded_lib.import_module(dev_lib) |
| 17 | + |
| 18 | + print("=> [TVM on TX2] loading model graph and params") |
| 19 | + loaded_graph = open(os.path.join(model_dir,"deploy_graph.json")).read() |
| 20 | + loaded_params = bytearray(open(os.path.join(model_dir, "deploy_param.params"), "rb").read()) |
| 21 | + |
| 22 | + print("=> [TVM on TX2] creating TVM runtime module") |
| 23 | + fcreate = tvm.get_global_func("tvm.graph_runtime.create") |
| 24 | + ctx = tvm.gpu(0) if cuda else tvm.cpu(0) |
| 25 | + gmodule = fcreate(loaded_graph, loaded_lib, ctx.device_type, ctx.device_id) |
| 26 | + set_input, get_output, run = gmodule["set_input"], gmodule["get_output"], gmodule["run"] |
| 27 | + |
| 28 | + print("=> [TVM on TX2] feeding inputs and params into TVM module") |
| 29 | + rgb_np = np.load(input_fp) # HWC |
| 30 | + x = np.zeros([1,3,224,224]) # NCHW |
| 31 | + x[0,:,:,:] = np.transpose(rgb_np, (2,0,1)) |
| 32 | + set_input('0', tvm.nd.array(x.astype('float32'))) |
| 33 | + gmodule["load_params"](loaded_params) |
| 34 | + |
| 35 | + print("=> [TVM on TX2] running TVM module, saving output") |
| 36 | + run() # not gmodule.run() |
| 37 | + out_shape = (1, 1, 224, 224) |
| 38 | + out = tvm.nd.empty(out_shape, "float32") |
| 39 | + get_output(0, out) |
| 40 | + np.save(output_fp, out.asnumpy()) |
| 41 | + |
| 42 | + print("=> [TVM on TX2] benchmarking: {} warmup, {} run trials".format(warmup_trials, run_trials)) |
| 43 | + # run model several times as a warmup |
| 44 | + for i in range(warmup_trials): |
| 45 | + run() |
| 46 | + ctx.sync() |
| 47 | + |
| 48 | + # profile runtime using TVM time evaluator |
| 49 | + ftimer = gmodule.time_evaluator("run", ctx, number=1, repeat=run_trials) |
| 50 | + profile_result = ftimer() |
| 51 | + profiled_runtime = profile_result[0] |
| 52 | + |
| 53 | + print("=> [TVM on TX2] profiled runtime (in ms): {:.5f}".format(1000*profiled_runtime)) |
| 54 | + |
| 55 | + # try randomizing input |
| 56 | + if try_randin: |
| 57 | + randin_runtime = 0 |
| 58 | + for i in range(run_trials): |
| 59 | + x = np.random.randn(1, 3, 224, 224) |
| 60 | + set_input('0', tvm.nd.array(x.astype('float32'))) |
| 61 | + randin_ftimer = gmodule.time_evaluator("run", ctx, number=1, repeat=1) |
| 62 | + randin_profile_result = randin_ftimer() |
| 63 | + randin_runtime += randin_profile_result[0] |
| 64 | + randomized_input_runtime = randin_runtime/run_trials |
| 65 | + print("=> [TVM on TX2] with randomized input on every run, profiled runtime (in ms): {:.5f}".format(1000*randomized_input_runtime)) |
| 66 | + |
| 67 | +def main(): |
| 68 | + parser = argparse.ArgumentParser() |
| 69 | + parser.add_argument('--model-dir', type=str, required=True, |
| 70 | + help='path to folder with TVM-compiled model files (required)') |
| 71 | + parser.add_argument('--input-fp', type=str, default='data/rgb.npy', |
| 72 | + help='numpy file containing input rgb data (default: data/rgb.npy') |
| 73 | + parser.add_argument('--output-fp', type=str, default='data/pred.npy', |
| 74 | + help='numpy file to store output prediction data (default: data/pred.npy') |
| 75 | + |
| 76 | + |
| 77 | + parser.add_argument('--warmup', type=int, default=10, |
| 78 | + help='number of inference warmup trials (default: 10)') |
| 79 | + parser.add_argument('--run', type=int, default=100, |
| 80 | + help='number of inference run trials (default: 100)') |
| 81 | + parser.add_argument('--cuda', type=bool, default=False, |
| 82 | + help='run with CUDA (default: False)') |
| 83 | + |
| 84 | + parser.add_argument('--randin', type=bool, default=False, |
| 85 | + help='profile runtime while randomizing input on every run (default: False)') |
| 86 | + |
| 87 | + args = parser.parse_args() |
| 88 | + run_model(args.model_dir, args.input_fp, args.output_fp, args.warmup, args.run, args.cuda, try_randin=args.randin) |
| 89 | + |
| 90 | +if __name__ == '__main__': |
| 91 | + main() |
| 92 | + |
0 commit comments