-
Notifications
You must be signed in to change notification settings - Fork 350
/
noxfile.py
571 lines (466 loc) · 16.2 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
import os
import sys
from distutils.command.clean import clean
import nox
# Use system installed Python packages
PYT_PATH = (
"/usr/local/lib/python3.10/dist-packages"
if not "PYT_PATH" in os.environ
else os.environ["PYT_PATH"]
)
print(f"Using python path {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"]
)
print(f"Test root directory {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"]
if USE_CXX11:
print("Using cxx11 abi")
# Set the USE_HOST_DEPS=1 to use host dependencies for tests
USE_HOST_DEPS = 0 if not "USE_HOST_DEPS" in os.environ else os.environ["USE_HOST_DEPS"]
if USE_HOST_DEPS:
print("Using dependencies from host python")
# Set epochs to train VGG model for accuracy tests
EPOCHS = 25
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):
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_DEPS:
session.run_always("python", "hub.py", env={"PYTHONPATH": PYT_PATH})
else:
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 train_model(session):
session.chdir(os.path.join(TOP_DIR, "examples/int8/training/vgg16"))
session.install("-r", "requirements.txt")
if os.path.exists("vgg16_ckpts/ckpt_epoch25.pth"):
session.run_always("python", "export_ckpt.py", "vgg16_ckpts/ckpt_epoch25.pth")
return
if USE_HOST_DEPS:
session.run_always(
"python",
"main.py",
"--lr",
"0.01",
"--batch-size",
"128",
"--drop-ratio",
"0.15",
"--ckpt-dir",
"vgg16_ckpts",
"--epochs",
str(EPOCHS),
env={"PYTHONPATH": PYT_PATH},
)
session.run_always(
"python",
"export_ckpt.py",
"vgg16_ckpts/ckpt_epoch" + str(EPOCHS) + ".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",
str(EPOCHS),
)
session.run_always(
"python", "export_ckpt.py", "vgg16_ckpts/ckpt_epoch" + str(EPOCHS) + ".pth"
)
def finetune_model(session):
# 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_DEPS:
session.run_always(
"python",
"finetune_qat.py",
"--lr",
"0.01",
"--batch-size",
"128",
"--drop-ratio",
"0.15",
"--ckpt-dir",
"vgg16_ckpts",
"--start-from",
str(EPOCHS),
"--epochs",
str(EPOCHS + 1),
env={"PYTHONPATH": PYT_PATH},
)
# Export model
session.run_always(
"python",
"export_qat.py",
"vgg16_ckpts/ckpt_epoch" + str(EPOCHS + 1) + ".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",
str(EPOCHS),
"--epochs",
str(EPOCHS + 1),
)
# Export model
session.run_always(
"python",
"export_qat.py",
"vgg16_ckpts/ckpt_epoch" + str(EPOCHS + 1) + ".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):
print("Running basic tests")
session.chdir(os.path.join(TOP_DIR, "tests/py/ts"))
tests = [
"api",
"integrations/test_to_backend_api.py",
]
for test in tests:
if USE_HOST_DEPS:
session.run_always("pytest", test, env={"PYTHONPATH": PYT_PATH})
else:
session.run_always("pytest", test)
def run_dynamo_backend_tests(session):
print("Running Dynamo core tests")
session.chdir(os.path.join(TOP_DIR, "tests/py/dynamo/"))
tests = [
"backend",
]
for test in tests:
if USE_HOST_DEPS:
session.run_always("pytest", test, env={"PYTHONPATH": PYT_PATH})
else:
session.run_always("pytest", test)
def run_dynamo_converter_tests(session):
print("Running Dynamo converter tests")
session.chdir(os.path.join(TOP_DIR, "tests/py/dynamo/"))
tests = [
"conversion",
]
for test in tests:
if USE_HOST_DEPS:
session.run_always("pytest", test, env={"PYTHONPATH": PYT_PATH})
else:
session.run_always("pytest", test)
def run_dynamo_lower_tests(session):
print("Running Dynamo lowering passes")
session.chdir(os.path.join(TOP_DIR, "tests/py/dynamo/"))
tests = ["lowering"]
for test in tests:
if USE_HOST_DEPS:
session.run_always("pytest", test, env={"PYTHONPATH": PYT_PATH})
else:
session.run_always("pytest", test)
def run_dynamo_partitioning_tests(session):
print("Running Dynamo Partitioning tests")
session.chdir(os.path.join(TOP_DIR, "tests/py/dynamo/"))
tests = ["partitioning"]
for test in tests:
if USE_HOST_DEPS:
session.run_always("pytest", test, env={"PYTHONPATH": PYT_PATH})
else:
session.run_always("pytest", test)
def run_dynamo_runtime_tests(session):
print("Running Dynamo Runtime tests")
session.chdir(os.path.join(TOP_DIR, "tests/py/dynamo/"))
tests = [
"runtime",
]
for test in tests:
if USE_HOST_DEPS:
session.run_always("pytest", test, env={"PYTHONPATH": PYT_PATH})
else:
session.run_always("pytest", test)
def run_dynamo_model_compile_tests(session):
print("Running model torch-compile tests")
session.chdir(os.path.join(TOP_DIR, "tests/py/dynamo/models"))
tests = [
"test_models.py",
]
for test in tests:
if USE_HOST_DEPS:
session.run_always(
"python",
test,
"--ir",
str("torch_compile"),
env={"PYTHONPATH": PYT_PATH},
)
else:
session.run_always("python", test, "--ir", str("torch_compile"))
def run_dynamo_model_export_tests(session):
print("Running model torch-export tests")
session.chdir(os.path.join(TOP_DIR, "tests/py/dynamo/models"))
tests = ["test_models_export.py", "test_export_serde.py"]
for test in tests:
if USE_HOST_DEPS:
session.run_always(
"python", test, "--ir", str("dynamo"), env={"PYTHONPATH": PYT_PATH}
)
else:
session.run_always("python", test, "--ir", str("dynamo"))
def run_accuracy_tests(session):
print("Running accuracy tests")
session.chdir(os.path.join(TOP_DIR, "tests/py/ts"))
tests = []
for test in tests:
if USE_HOST_DEPS:
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/modules/") + file_name),
external=True,
)
def run_int8_accuracy_tests(session):
print("Running accuracy tests")
copy_model(session)
session.chdir(os.path.join(TOP_DIR, "tests/py/ts"))
tests = [
"ptq/test_ptq_to_backend.py",
"ptq/test_ptq_dataloader_calibrator.py",
"qat/",
]
for test in tests:
if USE_HOST_DEPS:
session.run_always("pytest", test, env={"PYTHONPATH": PYT_PATH})
else:
session.run_always("pytest", test)
def run_trt_compatibility_tests(session):
print("Running TensorRT compatibility tests")
copy_model(session)
session.chdir(os.path.join(TOP_DIR, "tests/py/ts"))
tests = [
"integrations/test_trt_intercompatibility.py",
# "ptq/test_ptq_trt_calibrator.py",
]
for test in tests:
if USE_HOST_DEPS:
session.run_always("pytest", test, env={"PYTHONPATH": PYT_PATH})
else:
session.run_always("pytest", test)
def run_dla_tests(session):
print("Running DLA tests")
session.chdir(os.path.join(TOP_DIR, "tests/py/ts"))
tests = [
"hw/test_api_dla.py",
]
for test in tests:
if USE_HOST_DEPS:
session.run_always("pytest", test, env={"PYTHONPATH": PYT_PATH})
else:
session.run_always("pytest", test)
def run_multi_gpu_tests(session):
print("Running multi GPU tests")
session.chdir(os.path.join(TOP_DIR, "tests/py/ts"))
tests = [
"hw/test_multi_gpu.py",
]
for test in tests:
if USE_HOST_DEPS:
session.run_always("pytest", test, env={"PYTHONPATH": PYT_PATH})
else:
session.run_always("pytest", test)
def run_l0_api_tests(session):
if not USE_HOST_DEPS:
install_deps(session)
install_torch_trt(session)
download_models(session)
run_base_tests(session)
cleanup(session)
def run_l0_dynamo_tests(session):
if not USE_HOST_DEPS:
install_deps(session)
install_torch_trt(session)
run_dynamo_backend_tests(session)
run_dynamo_converter_tests(session)
run_dynamo_lower_tests(session)
cleanup(session)
def run_l0_dynamo_backend_tests(session):
if not USE_HOST_DEPS:
install_deps(session)
install_torch_trt(session)
run_dynamo_backend_tests(session)
cleanup(session)
def run_l0_dynamo_converter_tests(session):
if not USE_HOST_DEPS:
install_deps(session)
install_torch_trt(session)
run_dynamo_converter_tests(session)
cleanup(session)
def run_l0_dynamo_lower_tests(session):
if not USE_HOST_DEPS:
install_deps(session)
install_torch_trt(session)
run_dynamo_lower_tests(session)
cleanup(session)
def run_l0_dynamo_model_tests(session):
if not USE_HOST_DEPS:
install_deps(session)
install_torch_trt(session)
run_dynamo_model_tests(session)
cleanup(session)
def run_l0_dynamo_partitioning_tests(session):
if not USE_HOST_DEPS:
install_deps(session)
install_torch_trt(session)
run_dynamo_partitioning_tests(session)
cleanup(session)
def run_l0_dynamo_runtime_tests(session):
if not USE_HOST_DEPS:
install_deps(session)
install_torch_trt(session)
run_dynamo_runtime_tests(session)
cleanup(session)
def run_l0_dla_tests(session):
if not USE_HOST_DEPS:
install_deps(session)
install_torch_trt(session)
download_models(session)
run_base_tests(session)
cleanup(session)
def run_dynamo_model_tests(session):
if not USE_HOST_DEPS:
install_deps(session)
install_torch_trt(session)
download_models(session)
run_dynamo_model_compile_tests(session)
run_dynamo_model_export_tests(session)
cleanup(session)
def run_l1_int8_accuracy_tests(session):
if not USE_HOST_DEPS:
install_deps(session)
install_torch_trt(session)
train_model(session)
finetune_model(session)
run_int8_accuracy_tests(session)
cleanup(session)
def run_l1_dynamo_tests(session):
if not USE_HOST_DEPS:
install_deps(session)
install_torch_trt(session)
run_dynamo_model_tests(session)
run_dynamo_partitioning_tests(session)
run_dynamo_runtime_tests(session)
cleanup(session)
def run_l2_trt_compatibility_tests(session):
if not USE_HOST_DEPS:
install_deps(session)
install_torch_trt(session)
run_trt_compatibility_tests(session)
cleanup(session)
def run_l2_multi_gpu_tests(session):
if not USE_HOST_DEPS:
install_deps(session)
install_torch_trt(session)
download_models(session)
run_multi_gpu_tests(session)
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)
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l0_dynamo_tests(session):
"""When a developer needs to check correctness for a PR or something"""
run_l0_dynamo_tests(session)
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l0_dynamo_backend_tests(session):
"""When a developer needs to check correctness for a PR or something"""
run_l0_dynamo_backend_tests(session)
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l0_dynamo_converter_tests(session):
"""When a developer needs to check correctness for a PR or something"""
run_l0_dynamo_converter_tests(session)
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l0_dynamo_lower_tests(session):
"""When a developer needs to check correctness for a PR or something"""
run_l0_dynamo_lower_tests(session)
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l0_dla_tests(session):
"""When a developer needs to check basic api functionality using host dependencies"""
run_l0_dla_tests(session)
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l1_model_tests(session):
"""When a user needs to test the functionality of standard models compilation and results"""
run_dynamo_model_tests(session)
@nox.session(python=SUPPORTED_PYTHON_VERSIONS, reuse_venv=True)
def l1_dynamo_tests(session):
"""When a user needs to test the functionality of standard models compilation and results"""
run_l1_dynamo_tests(session)
@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)
@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)
@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)