|
1 |
| -import time |
| 1 | +import os |
2 | 2 |
|
3 | 3 | import numpy as np
|
4 | 4 | import torch
|
5 | 5 | import torch_tensorrt as torch_trt
|
6 | 6 | import torchvision.models as models
|
7 |
| -from torch.fx.experimental.proxy_tensor import maybe_disable_fake_tensor_mode |
8 | 7 |
|
9 | 8 | np.random.seed(0)
|
10 | 9 | torch.manual_seed(0)
|
|
19 | 18 | min_block_size = 0
|
20 | 19 | use_python_runtime = False
|
21 | 20 | torch_executed_ops = {}
|
| 21 | +TIMING_CACHE_PATH = "/tmp/timing_cache.bin" |
22 | 22 |
|
23 | 23 |
|
24 |
| -def dynamo_path(): |
25 |
| - ############### warmup ############### |
26 |
| - inputs = [torch.rand(size).to("cuda")] |
27 |
| - t1 = time.time() |
28 |
| - trt_gm = torch_trt.dynamo.compile( |
29 |
| - exp_program, |
30 |
| - tuple(inputs), |
31 |
| - use_python_runtime=use_python_runtime, |
32 |
| - enabled_precisions=enabled_precisions, |
33 |
| - debug=debug, |
34 |
| - min_block_size=min_block_size, |
35 |
| - torch_executed_ops=torch_executed_ops, |
36 |
| - make_refitable=True, |
37 |
| - ignore_engine_cache=True, |
38 |
| - ) # Output is a torch.fx.GraphModule |
39 |
| - t2 = time.time() |
| 24 | +def remove_timing_cache(path=TIMING_CACHE_PATH): |
| 25 | + if os.path.exists(path): |
| 26 | + os.remove(path) |
40 | 27 |
|
41 |
| - ############### compile for the first time ############### |
42 |
| - inputs = [torch.rand(size).to("cuda")] |
43 |
| - t3 = time.time() |
44 |
| - trt_gm1 = torch_trt.dynamo.compile( |
45 |
| - exp_program, |
46 |
| - tuple(inputs), |
47 |
| - use_python_runtime=use_python_runtime, |
48 |
| - enabled_precisions=enabled_precisions, |
49 |
| - debug=debug, |
50 |
| - min_block_size=min_block_size, |
51 |
| - torch_executed_ops=torch_executed_ops, |
52 |
| - make_refitable=True, |
53 |
| - ignore_engine_cache=False, |
54 |
| - ) # Output is a torch.fx.GraphModule |
55 |
| - t4 = time.time() |
56 |
| - # Check the output |
57 |
| - outputs = trt_gm1(*inputs) |
58 |
| - print("----------> 1st output:", outputs) |
59 | 28 |
|
60 |
| - ############### compile for the second time ############### |
61 |
| - inputs = [torch.rand(size).to("cuda")] |
62 |
| - t5 = time.time() |
63 |
| - trt_gm2 = torch_trt.dynamo.compile( |
64 |
| - exp_program, |
65 |
| - tuple(inputs), |
66 |
| - use_python_runtime=use_python_runtime, |
67 |
| - enabled_precisions=enabled_precisions, |
68 |
| - debug=debug, |
69 |
| - min_block_size=min_block_size, |
70 |
| - torch_executed_ops=torch_executed_ops, |
71 |
| - make_refitable=True, |
72 |
| - ignore_engine_cache=False, |
73 |
| - ) # Output is a torch.fx.GraphModule |
74 |
| - t6 = time.time() |
75 |
| - # Check the output |
76 |
| - outputs = trt_gm2(*inputs) |
77 |
| - print("----------> 2nd output:", outputs) |
| 29 | +def dynamo_path(iterations=3): |
| 30 | + outputs = [] |
| 31 | + times = [] |
| 32 | + start = torch.cuda.Event(enable_timing=True) |
| 33 | + end = torch.cuda.Event(enable_timing=True) |
| 34 | + for i in range(iterations): |
| 35 | + inputs = [torch.rand(size).to("cuda")] |
| 36 | + remove_timing_cache() |
| 37 | + if i == 0: # warmup |
| 38 | + ignore_engine_cache = True |
| 39 | + else: |
| 40 | + ignore_engine_cache = False |
78 | 41 |
|
79 |
| - print("----------> warmup compilation time:", t2 - t1, "seconds") |
80 |
| - print("----------> 1st compilation time:", t4 - t3, "seconds") |
81 |
| - print("----------> 2nd compilation time:", t6 - t5, "seconds") |
| 42 | + start.record() |
| 43 | + trt_gm = torch_trt.dynamo.compile( |
| 44 | + exp_program, |
| 45 | + tuple(inputs), |
| 46 | + use_python_runtime=use_python_runtime, |
| 47 | + enabled_precisions=enabled_precisions, |
| 48 | + debug=debug, |
| 49 | + min_block_size=min_block_size, |
| 50 | + torch_executed_ops=torch_executed_ops, |
| 51 | + make_refitable=True, |
| 52 | + ignore_engine_cache=ignore_engine_cache, |
| 53 | + ) |
| 54 | + end.record() |
| 55 | + torch.cuda.synchronize() |
| 56 | + times.append(start.elapsed_time(end)) |
| 57 | + outputs.append(trt_gm(*inputs)) |
82 | 58 |
|
83 |
| - |
84 |
| -def compile_path(): |
85 |
| - inputs = [torch.rand(size).to("cuda")] |
86 |
| - model = models.resnet18(pretrained=True).eval().to("cuda") |
87 |
| - t1 = time.time() |
88 |
| - model = torch.compile( |
89 |
| - model, |
90 |
| - backend="tensorrt", |
91 |
| - options={ |
92 |
| - "use_python_runtime": use_python_runtime, |
93 |
| - "enabled_precisions": enabled_precisions, |
94 |
| - "debug": debug, |
95 |
| - "min_block_size": min_block_size, |
96 |
| - "torch_executed_ops": torch_executed_ops, |
97 |
| - "make_refitable": True, |
98 |
| - "ignore_engine_cache": True, |
99 |
| - }, |
100 |
| - ) |
101 |
| - t2 = time.time() |
102 |
| - print("---------->", model(*inputs)) |
103 |
| - |
104 |
| - t3 = time.time() |
105 |
| - model1 = torch.compile( |
106 |
| - model, |
107 |
| - backend="tensorrt", |
108 |
| - options={ |
109 |
| - "use_python_runtime": use_python_runtime, |
110 |
| - "enabled_precisions": enabled_precisions, |
111 |
| - "debug": debug, |
112 |
| - "min_block_size": min_block_size, |
113 |
| - "torch_executed_ops": torch_executed_ops, |
114 |
| - "make_refitable": True, |
115 |
| - "ignore_engine_cache": False, |
116 |
| - }, |
117 |
| - ) |
118 |
| - t4 = time.time() |
119 |
| - print("----------> 1st output:", model1(*inputs)) |
120 |
| - |
121 |
| - t5 = time.time() |
122 |
| - model2 = torch.compile( |
123 |
| - model, |
124 |
| - backend="tensorrt", |
125 |
| - options={ |
126 |
| - "use_python_runtime": use_python_runtime, |
127 |
| - "enabled_precisions": enabled_precisions, |
128 |
| - "debug": debug, |
129 |
| - "min_block_size": min_block_size, |
130 |
| - "torch_executed_ops": torch_executed_ops, |
131 |
| - "make_refitable": True, |
132 |
| - "ignore_engine_cache": False, |
133 |
| - }, |
134 |
| - ) |
135 |
| - t6 = time.time() |
136 |
| - print("----------> 2nd output:", model2(*inputs)) |
137 |
| - |
138 |
| - print("----------> warmup compilation time:", t2 - t1, "seconds") |
139 |
| - print("----------> 1st compilation time:", t4 - t3, "seconds") |
140 |
| - print("----------> 2nd compilation time:", t6 - t5, "seconds") |
| 59 | + print("-----dynamo_path-----> output:", outputs) |
| 60 | + print("-----dynamo_path-----> compilation time:", times, "seconds") |
141 | 61 |
|
142 | 62 |
|
143 | 63 | if __name__ == "__main__":
|
144 | 64 | dynamo_path()
|
145 |
| - compile_path() |
0 commit comments