forked from pytorch/TensorRT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoxfile.py
344 lines (299 loc) · 13.1 KB
/
noxfile.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
from distutils.command.clean import clean
import nox
import os
import sys
# Use system installed Python packages
PYT_PATH='/opt/conda/lib/python3.8/site-packages' if not 'PYT_PATH' in os.environ else os.environ["PYT_PATH"]
# Set the root directory to the directory of the noxfile unless the user wants to
# TOP_DIR
TOP_DIR=os.path.dirname(os.path.realpath(__file__)) if not 'TOP_DIR' in os.environ else os.environ["TOP_DIR"]
# Set the USE_CXX11=1 to use cxx11_abi
USE_CXX11=0 if not 'USE_CXX11' in os.environ else os.environ["USE_CXX11"]
SUPPORTED_PYTHON_VERSIONS=["3.7", "3.8", "3.9", "3.10"]
nox.options.sessions = ["l0_api_tests-" + "{}.{}".format(sys.version_info.major, sys.version_info.minor)]
def install_deps(session):
print("Installing deps")
session.install("-r", os.path.join(TOP_DIR, "py", "requirements.txt"))
session.install("-r", os.path.join(TOP_DIR, "tests", "py", "requirements.txt"))
def download_models(session, use_host_env=False):
print("Downloading test models")
session.install("-r", os.path.join(TOP_DIR, "tests", "modules", "requirements.txt"))
print(TOP_DIR)
session.chdir(os.path.join(TOP_DIR, "tests", "modules"))
if use_host_env:
session.run_always('python', 'hub.py', env={'PYTHONPATH': PYT_PATH})
else:
session.install("-r", os.path.join(TOP_DIR, "py", "requirements.txt"))
session.run_always('python', 'hub.py')
def install_torch_trt(session):
print("Installing latest torch-tensorrt build")
session.chdir(os.path.join(TOP_DIR, "py"))
if USE_CXX11:
session.run('python', 'setup.py', 'develop', '--use-cxx11-abi')
else:
session.run("python", "setup.py", "develop")
def download_datasets(session):
print("Downloading dataset to path", os.path.join(TOP_DIR, 'examples/int8/training/vgg16'))
session.chdir(os.path.join(TOP_DIR, 'examples/int8/training/vgg16'))
session.run_always('wget', 'https://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz', external=True)
session.run_always('tar', '-xvzf', 'cifar-10-binary.tar.gz', external=True)
session.run_always('mkdir', '-p',
os.path.join(TOP_DIR, 'tests/accuracy/datasets/data'),
external=True)
session.run_always('cp', '-rpf',
os.path.join(TOP_DIR, 'examples/int8/training/vgg16/cifar-10-batches-bin'),
os.path.join(TOP_DIR, 'tests/accuracy/datasets/data/cidar-10-batches-bin'),
external=True)
def train_model(session, use_host_env=False):
session.chdir(os.path.join(TOP_DIR, 'examples/int8/training/vgg16'))
if use_host_env:
session.run_always('python',
'main.py',
'--lr', '0.01',
'--batch-size', '128',
'--drop-ratio', '0.15',
'--ckpt-dir', 'vgg16_ckpts',
'--epochs', '25',
env={'PYTHONPATH': PYT_PATH})
session.run_always('python',
'export_ckpt.py',
'vgg16_ckpts/ckpt_epoch25.pth',
env={'PYTHONPATH': PYT_PATH})
else:
session.run_always('python',
'main.py',
'--lr', '0.01',
'--batch-size', '128',
'--drop-ratio', '0.15',
'--ckpt-dir', 'vgg16_ckpts',
'--epochs', '25')
session.run_always('python',
'export_ckpt.py',
'vgg16_ckpts/ckpt_epoch25.pth')
def finetune_model(session, use_host_env=False):
# Install pytorch-quantization dependency
session.install('pytorch-quantization', '--extra-index-url', 'https://pypi.ngc.nvidia.com')
session.chdir(os.path.join(TOP_DIR, 'examples/int8/training/vgg16'))
if use_host_env:
session.run_always('python',
'finetune_qat.py',
'--lr', '0.01',
'--batch-size', '128',
'--drop-ratio', '0.15',
'--ckpt-dir', 'vgg16_ckpts',
'--start-from', '25',
'--epochs', '26',
env={'PYTHONPATH': PYT_PATH})
# Export model
session.run_always('python',
'export_qat.py',
'vgg16_ckpts/ckpt_epoch26.pth',
env={'PYTHONPATH': PYT_PATH})
else:
session.run_always('python',
'finetune_qat.py',
'--lr', '0.01',
'--batch-size', '128',
'--drop-ratio', '0.15',
'--ckpt-dir', 'vgg16_ckpts',
'--start-from', '25',
'--epochs', '26')
# Export model
session.run_always('python',
'export_qat.py',
'vgg16_ckpts/ckpt_epoch26.pth')
def cleanup(session):
target = [
'examples/int8/training/vgg16/*.jit.pt',
'examples/int8/training/vgg16/vgg16_ckpts',
'examples/int8/training/vgg16/cifar-10-*',
'examples/int8/training/vgg16/data',
'tests/modules/*.jit.pt',
'tests/py/*.jit.pt'
]
target = ' '.join(x for x in [os.path.join(TOP_DIR, i) for i in target])
session.run_always('bash', '-c',
str('rm -rf ') + target,
external=True)
def run_base_tests(session, use_host_env=False):
print("Running basic tests")
session.chdir(os.path.join(TOP_DIR, 'tests/py'))
tests = [
"test_api.py",
"test_to_backend_api.py",
]
for test in tests:
if use_host_env:
session.run_always('python', test, env={'PYTHONPATH': PYT_PATH})
else:
session.run_always("python", test)
def run_accuracy_tests(session, use_host_env=False):
print("Running accuracy tests")
session.chdir(os.path.join(TOP_DIR, 'tests/py'))
tests = []
for test in tests:
if use_host_env:
session.run_always('python', test, env={'PYTHONPATH': PYT_PATH})
else:
session.run_always("python", test)
def copy_model(session):
model_files = [ 'trained_vgg16.jit.pt',
'trained_vgg16_qat.jit.pt']
for file_name in model_files:
src_file = os.path.join(TOP_DIR, str('examples/int8/training/vgg16/') + file_name)
if os.path.exists(src_file):
session.run_always('cp',
'-rpf',
os.path.join(TOP_DIR, src_file),
os.path.join(TOP_DIR, str('tests/py/') + file_name),
external=True)
def run_int8_accuracy_tests(session, use_host_env=False):
print("Running accuracy tests")
copy_model(session)
session.chdir(os.path.join(TOP_DIR, 'tests/py'))
tests = [
"test_ptq_dataloader_calibrator.py",
"test_ptq_to_backend.py",
"test_qat_trt_accuracy.py",
]
for test in tests:
if use_host_env:
session.run_always('python', test, env={'PYTHONPATH': PYT_PATH})
else:
session.run_always("python", test)
def run_trt_compatibility_tests(session, use_host_env=False):
print("Running TensorRT compatibility tests")
copy_model(session)
session.chdir(os.path.join(TOP_DIR, 'tests/py'))
tests = [
"test_trt_intercompatibility.py",
"test_ptq_trt_calibrator.py",
]
for test in tests:
if use_host_env:
session.run_always('python', test, env={'PYTHONPATH': PYT_PATH})
else:
session.run_always("python", test)
def run_dla_tests(session, use_host_env=False):
print("Running DLA tests")
session.chdir(os.path.join(TOP_DIR, 'tests/py'))
tests = [
"test_api_dla.py",
]
for test in tests:
if use_host_env:
session.run_always('python', test, env={'PYTHONPATH': PYT_PATH})
else:
session.run_always("python", test)
def run_multi_gpu_tests(session, use_host_env=False):
print("Running multi GPU tests")
session.chdir(os.path.join(TOP_DIR, 'tests/py'))
tests = [
"test_multi_gpu.py",
]
for test in tests:
if use_host_env:
session.run_always('python', test, env={'PYTHONPATH': PYT_PATH})
else:
session.run_always("python", test)
def run_l0_api_tests(session, use_host_env=False):
if not use_host_env:
install_deps(session)
install_torch_trt(session)
download_models(session, use_host_env)
run_base_tests(session, use_host_env)
cleanup(session)
def run_l0_dla_tests(session, use_host_env=False):
if not use_host_env:
install_deps(session)
install_torch_trt(session)
download_models(session, use_host_env)
run_base_tests(session, use_host_env)
cleanup(session)
def run_l1_accuracy_tests(session, use_host_env=False):
if not use_host_env:
install_deps(session)
install_torch_trt(session)
download_models(session, use_host_env)
download_datasets(session)
train_model(session, use_host_env)
run_accuracy_tests(session, use_host_env)
cleanup(session)
def run_l1_int8_accuracy_tests(session, use_host_env=False):
if not use_host_env:
install_deps(session)
install_torch_trt(session)
download_models(session, use_host_env)
download_datasets(session)
train_model(session, use_host_env)
finetune_model(session, use_host_env)
run_int8_accuracy_tests(session, use_host_env)
cleanup(session)
def run_l2_trt_compatibility_tests(session, use_host_env=False):
if not use_host_env:
install_deps(session)
install_torch_trt(session)
download_models(session, use_host_env)
download_datasets(session)
train_model(session, use_host_env)
run_trt_compatibility_tests(session, use_host_env)
cleanup(session)
def run_l2_multi_gpu_tests(session, use_host_env=False):
if not use_host_env:
install_deps(session)
install_torch_trt(session)
download_models(session, use_host_env)
run_multi_gpu_tests(session, use_host_env)
cleanup(session)
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l0_api_tests(session):
"""When a developer needs to check correctness for a PR or something"""
run_l0_api_tests(session, use_host_env=False)
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l0_api_tests_host_deps(session):
"""When a developer needs to check basic api functionality using host dependencies"""
run_l0_api_tests(session, use_host_env=True)
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l0_dla_tests_host_deps(session):
"""When a developer needs to check basic api functionality using host dependencies"""
run_l0_dla_tests(session, use_host_env=True)
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l1_accuracy_tests(session):
"""Checking accuracy performance on various usecases"""
run_l1_accuracy_tests(session, use_host_env=False)
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l1_accuracy_tests_host_deps(session):
"""Checking accuracy performance on various usecases using host dependencies"""
run_l1_accuracy_tests(session, use_host_env=True)
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l1_int8_accuracy_tests(session):
"""Checking accuracy performance on various usecases"""
run_l1_int8_accuracy_tests(session, use_host_env=False)
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l1_int8_accuracy_tests_host_deps(session):
"""Checking accuracy performance on various usecases using host dependencies"""
run_l1_int8_accuracy_tests(session, use_host_env=True)
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l2_trt_compatibility_tests(session):
"""Makes sure that TensorRT Python and Torch-TensorRT can work together"""
run_l2_trt_compatibility_tests(session, use_host_env=False)
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l2_trt_compatibility_tests_host_deps(session):
"""Makes sure that TensorRT Python and Torch-TensorRT can work together using host dependencies"""
run_l2_trt_compatibility_tests(session, use_host_env=True)
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l2_multi_gpu_tests(session):
"""Makes sure that Torch-TensorRT can operate on multi-gpu systems"""
run_l2_multi_gpu_tests(session, use_host_env=False)
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l2_multi_gpu_tests_host_deps(session):
"""Makes sure that Torch-TensorRT can operate on multi-gpu systems using host dependencies"""
run_l2_multi_gpu_tests(session, use_host_env=True)
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def download_test_models(session):
"""Grab all the models needed for testing"""
download_models(session, use_host_env=False)
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def download_test_models_host_deps(session):
"""Grab all the models needed for testing using host dependencies"""
download_models(session, use_host_env=True)