-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
250 lines (229 loc) · 7.42 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# pylint: disable = (missing-module-docstring)
import argparse
import os
import torch
# to install version 1.3.0 follow
# https://github.com/pytorch/TensorRT/issues/1371#issuecomment-1256035010
import torch_tensorrt
from src.benchmark import (
BenchmarkCPU,
BenchmarkCUDA,
BenchmarkTensorDynamicQuantization,
BenchmarkTensorPruning,
BenchmarkTensorPTQ,
BenchmarkTensorRT,
)
from src.dataset_utils import (
DatasetFactory,
DatasetImagenetMiniFactory,
DatasetIMDBFactory,
)
from src.model_utils import save_torchscript
torch_tensorrt.logging.set_reportable_log_level(
torch_tensorrt.logging.Level(torch_tensorrt.logging.Level.Error)
)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser("Benchmark model optimization techniques")
parser.add_argument(
"--type",
choices=[
"cpu",
"cuda",
"tensorrt",
"quantization",
"dynamic_quantization",
"pruning",
],
required=True,
help="Model's operation type.",
)
parser.add_argument(
"--model_name",
choices=[
"swin_t",
"vit",
"resnet",
"mobilenet",
"fcn",
"cnn",
"rnn",
"bert",
"t5",
"gptneo",
],
required=True,
help="Model's name.",
)
parser.add_argument(
"--use_fp16", action="store_true", help="Use half precision model."
)
parser.add_argument("--use_jit", action="store_true", help="Use JIT model.")
parser.add_argument(
"--batch_size", type=int, default=1, help="Size of processed batch."
)
parser.add_argument(
"--n_runs",
type=int,
default=1,
help="Number of runs to compute mean of inference times.",
)
parser.add_argument(
"--model_dir",
type=str,
default="saved_models",
help="Directory with saved JIT models.",
)
parser.add_argument(
"--model_filename",
type=str,
default="model_jit.pth",
help="JIT model file name.",
)
parser.add_argument(
"--pruning_ratio",
type=float,
default=0.2,
help="Ratio of model's pruned weights.",
)
parser.add_argument(
"--pretrained_model_name",
type=str,
help="Name of a model to load from huggingface.",
)
parser.add_argument(
"--structural_pruning", action="store_true", help="Use structural pruning."
)
parser.add_argument(
"--max_length",
type=int,
default=100,
help="Max processed text in number of tokens.",
)
parser.add_argument(
"--data_dir", type=str, default="data/", help="ImageNet-Mini dataset root dir."
)
parser.add_argument(
"--subset_name",
type=str,
default="val",
help="Subset of ImageNet-Mini dataset: val or train.",
)
parser.add_argument(
"--dataset_size",
type=int,
default=150,
help="Number of samples from IMDB dataset to use.",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
# has influence on performance on CNNs:
# https://pytorch.org/tutorials/recipes/recipes/tuning_guide.html#enable-cudnn-auto-tuner
torch.backends.cudnn.benchmark = True
# https://pytorch.org/docs/stable/amp.html
# For now, we suggest to disable the Jit Autocast Pass,
# As the issue: https://github.com/pytorch/pytorch/issues/75956
if args.use_jit:
torch._C._jit_set_autocast_mode( # pylint: disable = (protected-access,c-extension-no-member)
False
)
if not torch.cuda.is_available():
raise RuntimeError("No CUDA device detected. Exiting...")
cuda_device = torch.device("cuda:0") # pylint: disable = (no-member)
cpu_device = torch.device("cpu:0") # pylint: disable = (no-member)
dataset_factory: DatasetFactory
if args.model_name in ["bert", "t5", "gptneo"]:
dataset_factory = DatasetIMDBFactory(
pretrained_model_name=args.pretrained_model_name,
dataset_size=args.dataset_size,
max_length=args.max_length,
batch_size=args.batch_size,
)
else:
dataset_factory = DatasetImagenetMiniFactory(
data_dir=args.data_dir,
subset_name=args.subset_name,
)
example_inputs = dataset_factory.get_example_inputs()
# save model's torchscript .pth file
model_torchscript_path: str = os.path.join(args.model_dir, args.model_filename)
if args.use_jit:
save_torchscript(
model_name=args.model_name,
device=cpu_device,
batch_size=args.batch_size,
model_torchscript_path=model_torchscript_path,
example_inputs=example_inputs,
)
# compute inference time, CUDA memory usage and F1 score
if args.type == "cpu":
BenchmarkCPU().benchmark(
model_name=args.model_name,
device=cpu_device,
batch_size=args.batch_size,
dataset_factory=dataset_factory,
model_torchscript_path=model_torchscript_path,
use_jit=args.use_jit,
use_fp16=args.use_fp16,
n_runs=args.n_runs,
)
elif args.type == "cuda":
BenchmarkCUDA().benchmark(
model_name=args.model_name,
device=cuda_device,
batch_size=args.batch_size,
dataset_factory=dataset_factory,
model_torchscript_path=model_torchscript_path,
use_jit=args.use_jit,
use_fp16=args.use_fp16,
n_runs=args.n_runs,
)
elif args.type == "tensorrt":
BenchmarkTensorRT().benchmark(
model_name=args.model_name,
device=cuda_device,
batch_size=args.batch_size,
dataset_factory=dataset_factory,
model_torchscript_path=model_torchscript_path,
use_jit=args.use_jit,
use_fp16=args.use_fp16,
n_runs=args.n_runs,
)
elif args.type == "quantization":
BenchmarkTensorPTQ().benchmark(
model_name=args.model_name,
device=cuda_device,
batch_size=args.batch_size,
dataset_factory=dataset_factory,
use_jit=args.use_jit,
use_fp16=args.use_fp16,
n_runs=args.n_runs,
model_torchscript_path=model_torchscript_path,
)
elif args.type == "dynamic_quantization":
BenchmarkTensorDynamicQuantization().benchmark(
model_name=args.model_name,
device=cpu_device,
dataset_factory=dataset_factory,
batch_size=args.batch_size,
model_torchscript_path=model_torchscript_path,
use_jit=args.use_jit,
use_fp16=args.use_fp16,
n_runs=args.n_runs,
)
elif args.type == "pruning":
BenchmarkTensorPruning().benchmark(
model_name=args.model_name,
device=cpu_device,
batch_size=args.batch_size,
model_torchscript_path=model_torchscript_path,
dataset_factory=dataset_factory,
use_jit=args.use_jit,
use_fp16=args.use_fp16,
n_runs=args.n_runs,
name="weight",
amount=args.pruning_ratio,
structural_pruning=args.structural_pruning,
)
if __name__ == "__main__":
main()