forked from pytorch/torchdynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
torchbench.py
executable file
·1103 lines (983 loc) · 35.5 KB
/
torchbench.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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import argparse
import collections
import copy
import csv
import functools
import gc
import importlib
import io
import itertools
import logging
import os
import re
import subprocess
import sys
import time
import warnings
from os.path import abspath
from os.path import exists
import numpy as np
import pandas as pd
import torch
from scipy.stats import gmean
from scipy.stats import ttest_ind
import torchdynamo
import torchdynamo.utils
from torchdynamo.optimizations import backends
from torchdynamo.optimizations.inference import fixed_strategy1
from torchdynamo.optimizations.inference import fixed_strategy2
from torchdynamo.optimizations.inference import offline_autotuner
from torchdynamo.optimizations.inference import online_autotuner
from torchdynamo.optimizations.python_key import python_key
from torchdynamo.optimizations.training import aot_autograd_debug_strategy1
from torchdynamo.optimizations.training import aot_autograd_nnc_strategy
from torchdynamo.optimizations.training import aot_autograd_speedup_strategy
from torchdynamo.profiler import Profiler
from torchdynamo.profiler import fx_insert_profiling
from torchdynamo.testing import collect_results
from torchdynamo.testing import dummy_fx_compile
from torchdynamo.testing import format_speedup
from torchdynamo.testing import reduce_to_scalar_loss
from torchdynamo.testing import same
from torchdynamo.utils import clone_inputs
os.environ["KALDI_ROOT"] = "/tmp" # avoids some spam
torchbench_dir = abspath(
"../torchbench" if exists("../torchbench") else "../torchbenchmark"
)
assert os.path.exists(torchbench_dir)
os.chdir(torchbench_dir)
sys.path.append(torchbench_dir)
log = logging.getLogger(__name__)
SKIP = {
# non-deterministic output / cant check correctness
"pyhpc_turbulent_kinetic_energy",
# https://github.com/facebookresearch/torchdynamo/issues/101
"detectron2_maskrcnn",
# https://github.com/facebookresearch/torchdynamo/issues/145
"fambench_xlmr",
}
# Additional models that are skipped in training
SKIP_TRAIN = {
# not designed for training
"pyhpc_equation_of_state",
"pyhpc_isoneutral_mixing",
# Unusual training setup
"opacus_cifar10",
"maml",
}
# Some models have bad train dataset. We read eval dataset.
# yolov3 - seems to have different number of inputs between eval and train
# timm_efficientdet - loader only exists for eval mode.
ONLY_EVAL_DATASET = {"yolov3", "timm_efficientdet"}
# These models support only train mode. So accuracy checking can't be done in
# eval mode.
ONLY_TRAINING_MODE = {"tts_angular", "tacotron2", "demucs"}
# Need lower tolerance on GPU. GPU kernels have non deterministic kernels for these models.
REQUIRE_HIGHER_TOLERANCE = {
"alexnet",
"attention_is_all_you_need_pytorch",
"densenet121",
"hf_Albert",
"vgg16",
"mobilenet_v3_large",
}
# Some models have large dataset that doesn't fit in memory. Lower the batch
# size to test the accuracy.
USE_SMALL_BATCH_SIZE = {
"demucs": 4,
"densenet121": 4,
"hf_Reformer": 4,
"timm_efficientdet": 1,
}
current_name = ""
current_device = ""
output_filename = None
class NullContext:
def __enter__(self):
pass
def __exit__(self, exc_type, exc_val, exc_tb):
pass
def synchronize():
pass
def iter_models(args):
for model_name in iter_model_names(args):
for device in args.devices:
try:
yield load_model(device, model_name, args.training, args.use_eval_mode)
except NotImplementedError:
continue # bad benchmark implementation
def iter_model_names(args):
from torchbenchmark import _list_model_paths
for model_path in _list_model_paths():
model_name = os.path.basename(model_path)
if (
not re.search("|".join(args.filter), model_name, re.I)
or re.search("|".join(args.exclude), model_name, re.I)
or model_name in SKIP
):
continue
yield model_name
def load_model(device, model_name, is_training, use_eval_mode):
module = importlib.import_module(f"torchbenchmark.models.{model_name}")
benchmark_cls = getattr(module, "Model", None)
if not hasattr(benchmark_cls, "name"):
benchmark_cls.name = model_name
batch_size = None
if is_training and model_name in USE_SMALL_BATCH_SIZE:
batch_size = USE_SMALL_BATCH_SIZE[model_name]
if is_training and model_name not in ONLY_EVAL_DATASET:
benchmark = benchmark_cls(
test="train", device=device, jit=False, batch_size=batch_size
)
else:
benchmark = benchmark_cls(
test="eval", device=device, jit=False, batch_size=batch_size
)
model, example_inputs = benchmark.get_module()
# Models that must be in train mode while training
if is_training and (not use_eval_mode or model_name in ONLY_TRAINING_MODE):
model.train()
else:
model.eval()
gc.collect()
global current_name, current_device
current_device = device
current_name = benchmark.name
return device, current_name, model, example_inputs
def timed(model, model_iter_fn, example_inputs, times=1, return_result=False):
synchronize()
torch.manual_seed(1337)
t0 = time.perf_counter()
# Dont collect outputs to correctly measure timing
for _ in range(times):
result = model_iter_fn(model, example_inputs, collect_outputs=False)
synchronize()
t1 = time.perf_counter()
return (t1 - t0, result) if return_result else t1 - t0
class Stats:
totals = collections.defaultdict(collections.Counter)
@classmethod
def reset_counters(cls):
for k, v in torchdynamo.utils.counters.items():
cls.totals[k].update(v)
ok = torchdynamo.utils.counters["frames"]["ok"]
total = torchdynamo.utils.counters["frames"]["total"]
torchdynamo.utils.counters.clear()
return ok, total
@classmethod
def print_summary(cls):
for k, v in sorted(cls.totals.items()):
lines = "\n ".join(map(str, v.most_common(50)))
print(f"STATS {k}\n {lines}")
@classmethod
def aot_summary(cls):
return [cls.totals["aot_autograd"]["total"], cls.totals["aot_autograd"]["ok"]]
def output_csv(filename, headers, row):
assert filename
existed = os.path.exists(filename)
output = csv.writer(
io.TextIOWrapper(
open(filename, "ab", buffering=0),
"utf-8",
write_through=True,
),
lineterminator="\n",
)
if not existed:
output.writerow(headers)
output.writerow([(f"{x:.4f}" if isinstance(x, float) else x) for x in row])
def coverage_experiment(args, model_iter_fn, model, example_inputs):
"""
Test operator/model coverage of TorchDynamo and record statistics
taken from a profiler. This target is mainly intended to check
correctness.
Writes to ./coverage.csv
"""
profiler = Profiler()
with profiler.prof, torchdynamo.run():
model_iter_fn(model, example_inputs)
coverage_result = profiler.results()
output_csv(
output_filename,
(
"dev",
"name",
"graphs",
"graph_calls",
"captured_ops",
"total_ops",
"pct_ops",
"pct_time",
),
[
current_device,
current_name,
]
+ coverage_result.tocsv(),
)
return coverage_result
def speedup_experiment_fx2trt(args, model_iter_fn, model, example_inputs):
"""
Measure speedups over eager using the trt inference backend. TRT backend is based fx graph
generated by torchdynamo.
Writes to ./speedups_fx2trt.csv
"""
return speedup_experiment(args, model_iter_fn, model, example_inputs)
def randomize_input(inputs):
if isinstance(inputs, (list, tuple)):
return type(inputs)([randomize_input(x) for x in inputs])
elif isinstance(inputs, torch.Tensor):
if inputs.dtype in (torch.float32, torch.float64):
torchdynamo.utils.counters["randomize_input"]["times"] += 1
return torch.randn_like(inputs)
elif inputs.dtype == torch.int64:
# Note: we can not simply tune integer tensors as follows
# `return torch.randint_like(inputs, high=inputs.max().item())`
# This may break some invariants between tensors.
# E.g. in embedding lookup case, one tensor is the length
# and another is an indices tensor.
return inputs
else:
raise RuntimeError(
f"randomize_input need support tensor of type {inputs.dtype}"
)
else:
raise RuntimeError(
f"randomize_input can not handle input of type {type(inputs)}"
)
def speedup_experiment(args, model_iter_fn, model, example_inputs):
"""
Measure speedups over eager using the autotuning inference backend. To use this:
1) First run once to record graphs that need autotuning
2) Next run ./autotune.py to select the right backend for each recorded graph
3) Finally, run this target again to measure speedups
Writes to ./speedups.csv
"""
timings = np.zeros((args.repeat, 2), np.float64)
# if we randomize the input, we should also check the result is correct
should_check_result = should_randomize_input = args.randomize_input
is_correct = True
for rep in range(args.repeat):
inputs = (
randomize_input(copy.deepcopy(example_inputs))
if should_randomize_input
else example_inputs
)
# interleave the runs to handle frequency scaling and load changes
timings[rep, 0], expected_output = timed(
model, model_iter_fn, inputs, return_result=True
)
with torchdynamo.run():
timings[rep, 1], actual_output = timed(
model, model_iter_fn, inputs, return_result=True
)
if should_check_result:
is_correct = is_correct and same(expected_output, actual_output)
pvalue = ttest_ind(timings[:, 0], timings[:, 1]).pvalue
median = np.median(timings, axis=0)
speedup = median[0] / median[1]
output_csv(
output_filename,
("dev", "name", "speedup"),
[current_device, current_name, float(speedup)],
)
return format_speedup(speedup, pvalue, is_correct=is_correct)
def overhead_experiment(*args, model_iter_fn):
"""
Measure overheads of TorchDynamo by running with no backend (only
eager+FX), and reporting speedup/slowdown over eager.
Writes to ./overheads.csv
"""
return speedup_experiment(*args, model_iter_fn)
def baselines(models, model_iter_fn, example_inputs, args):
"""
Common measurement code across all baseline experiments.
"""
models = list(models)
for idx, (name, model) in enumerate(models):
if idx == 0:
result0 = model_iter_fn(model, example_inputs)
elif model is not None:
try:
result = model_iter_fn(model, example_inputs)
if same(result0, result):
continue
print(name, "is INCORRECT")
except Exception:
log.exception("error checking %s", name)
models[idx] = (name, None)
timings = np.zeros((args.repeat, len(models)), np.float64)
timings.fill(1.0e10)
for rep in range(args.repeat):
for idx, (name, model) in enumerate(models):
if model is not None:
timings[rep, idx] = timed(model, model_iter_fn, example_inputs)
pvalue = [
ttest_ind(timings[:, 0], timings[:, i]).pvalue
for i in range(1, timings.shape[1])
]
median = np.median(timings, axis=0)
speedup = median[0] / median[1:]
for idx, (name, model) in enumerate(models[1:]):
if model is None:
speedup[idx] = 0.0
result = " ".join(
[
format_speedup(s, p, m is not None)
for s, p, m in zip(speedup, pvalue, [m for n, m in models[1:]])
]
)
output_csv(
output_filename,
("dev", "name") + tuple(n for n, m in models[1:]),
[current_device, current_name] + [f"{x:.4f}" for x in speedup],
)
return result
def try_script(model, example_inputs):
try:
return torch.jit.script(model)
except Exception:
return None
def speedup_experiment_ts(args, model_iter_fn, model, example_inputs):
"""
Measure baseline performance (without using TorchDynamo) of TorchScript and optimize_for_inference.
Writes to ./baseline_ts.csv
"""
return baselines(
[
("eager", model),
("ts", try_script(model, example_inputs)),
(
"ofi",
backends.ofi(try_script(model, example_inputs), example_inputs),
),
# ("nnc", backends.nnc(try_script(model, example_inputs), example_inputs)),
# ("nvfuser", backends.nvfuser(try_script(model, example_inputs), example_inputs)),
],
model_iter_fn,
example_inputs,
args,
)
def speedup_experiment_sr(args, model_iter_fn, model, example_inputs):
"""
Measure baseline performance (without using TorchDynamo) of static runtime.
Writes to ./baseline_sr.csv
"""
if current_name not in ("opacus_cifar10", "timm_nfnet", "hf_T5"):
sr = backends.static_runtime(try_script(model, example_inputs), example_inputs)
else:
# segfaults on these models
sr = None
return baselines(
[
("eager", model),
(
"sr",
sr,
),
],
model_iter_fn,
example_inputs,
args,
)
def speedup_experiment_onnx(args, model_iter_fn, model, example_inputs):
"""
Measure baseline performance (without using TorchDynamo) of ONNXRT and TensorFlow.
Writes to ./baseline_onnx.csv
"""
if current_device == "cpu":
m_onnxrt = backends.onnxrt_cpu(
try_script(model, example_inputs), example_inputs
)
else:
m_onnxrt = backends.onnxrt_cuda(
try_script(model, example_inputs), example_inputs
)
if current_name != "timm_resnest":
m_onnx2tf = backends.onnx2tf(try_script(model, example_inputs), example_inputs)
else:
# this one takes 8+ hours to finish
m_onnx2tf = None
return baselines(
[
("eager", model),
("onnxrt", m_onnxrt),
("onnx2tf", m_onnx2tf),
],
model_iter_fn,
example_inputs,
args,
)
def speedup_experiment_trt(args, model_iter_fn, model, example_inputs):
"""
Measure baseline performance (without using TorchDynamo) of TensorRT.
Writes to ./baseline_trt.csv
"""
m_onnx2trt = backends.onnx2tensorrt(
try_script(model, example_inputs), example_inputs
)
m_torch2trt = backends.torch2trt(model, example_inputs)
if current_name != "opacus_cifar10":
m_fx2trt = backends.fx2trt(model, example_inputs)
else:
# fx2trt infinite loops on one model
m_fx2trt = None
return baselines(
[
("eager", model),
("onnx2trt", m_onnx2trt),
("torch2trt", m_torch2trt),
("fx2trt", m_fx2trt),
],
model_iter_fn,
example_inputs,
args,
)
def null_experiment(args, model_iter_fn, model, example_inputs):
"""
A no-op experiment useful for making sure TorchBenchark alone works properly.
"""
return []
def pick_grad(name, is_training):
if is_training or name in ("maml",):
return torch.enable_grad()
else:
return torch.no_grad()
def help(fn):
return fn.__doc__
@torchdynamo.skip
def forward_pass(mod, inputs, collect_outputs=True):
return mod(*inputs)
@torchdynamo.skip
def forward_and_backward_pass(mod, inputs, collect_outputs=True):
cloned_inputs = clone_inputs(inputs)
mod.zero_grad(True)
pred = mod(*cloned_inputs)
loss = reduce_to_scalar_loss(pred)
loss.backward()
if collect_outputs:
return collect_results(mod, pred, loss, cloned_inputs)
return None
def cast_to_fp16(model, inputs):
# cast model and inputs to fp16
model = model.half()
from torch.utils._pytree import tree_map
inputs = tuple(
tree_map(
lambda x: x.to(torch.float16)
if getattr(x, "dtype", None) == torch.float32
or getattr(x, "dtype", None) == torch.float64
else x,
inputs,
)
)
# TRT does not support int64. Some model does need it like Super_SloMo
if current_name != "Super_SloMo" and current_name != "fastNLP_Bert":
inputs = tuple(
tree_map(
lambda x: x.to(torch.int32)
if getattr(x, "dtype", None) == torch.int64
else x,
inputs,
)
)
return model, inputs
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--filter", "-k", action="append", help="filter benchmarks with regexp"
)
parser.add_argument(
"--exclude", "-x", action="append", help="filter benchmarks with regexp"
)
parser.add_argument("--devices", "-d", action="append", help="cpu or cuda")
parser.add_argument(
"--repeat", "-n", type=int, default=30, help="number of timing runs"
)
parser.add_argument(
"--randomize-input",
action="store_true",
help="Whether to randomize the input values. Dimensions will be kept the same.",
)
parser.add_argument(
"--threads", "-t", type=int, help="number of threads to use for eager"
)
parser.add_argument(
"--verbose", "-v", action="store_true", help="enable verbose debug printouts"
)
parser.add_argument(
"--nopython", action="store_true", help="Turn graph breaks into errors"
)
parser.add_argument(
"--no-skip",
action="store_true",
help="run models that are in the global SKIP list",
)
parser.add_argument(
"--nvfuser", action="store_true", help="enable nvfuser globally"
)
parser.add_argument(
"--isolate", action="store_true", help="run each model in its own process"
)
parser.add_argument("--only", help="used by --isolate to run just one model")
parser.add_argument(
"--minimum-call-count", type=int, help="filter out graphs with too few ops"
)
parser.add_argument(
"--training",
action="store_true",
help="Performs training",
)
parser.add_argument(
"--use-eval-mode",
action="store_true",
help="sets model.eval() to reduce randomness",
)
parser.add_argument(
"--skip-accuracy-check",
action="store_true",
help="keeps running even when accuracy fails",
)
parser.add_argument(
"--generate-aot-autograd-stats",
action="store_true",
help="Generates AOT Autograd stats like how mnay graphs are sent to AOT",
)
parser.add_argument(
"--disable-functionalization",
action="store_true",
help="Disables functionalization",
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
"--coverage", action="store_true", help="(default) " + help(coverage_experiment)
)
group.add_argument(
"--online-autotune", action="store_true", help=help(speedup_experiment)
)
group.add_argument(
"--offline-autotune", action="store_true", help=help(speedup_experiment)
)
group.add_argument(
"--speedup-fixed1",
action="store_true",
help="speedup using experimental fixed_strategy backend",
)
group.add_argument(
"--speedup-fixed2",
action="store_true",
help="speedup using experimental fixed_strategy backend",
)
group.add_argument(
"--speedup-ltc",
action="store_true",
help="speedup using the ltc backend",
)
group.add_argument(
"--speedup-ltc-trivial",
action="store_true",
help="speedup using the ltc backend without reusing compiled graph",
)
group.add_argument(
"--overhead", action="store_true", help=help(overhead_experiment)
)
group.add_argument(
"--speedup-ts", action="store_true", help=help(speedup_experiment_ts)
)
group.add_argument(
"--speedup-sr", action="store_true", help=help(speedup_experiment_sr)
)
group.add_argument(
"--speedup-onnx", action="store_true", help=help(speedup_experiment_onnx)
)
group.add_argument(
"--speedup-trt", action="store_true", help=help(speedup_experiment_trt)
)
group.add_argument("--python-key", action="store_true")
group.add_argument(
"--speedup-fx2trt", action="store_true", help=help(speedup_experiment_fx2trt)
)
group.add_argument(
"--speedup-fx2trt-fp16",
action="store_true",
help=help(speedup_experiment_fx2trt),
)
group.add_argument(
"--accuracy-aot-nop",
action="store_true",
help="Accuracy testing and speedup for AOT vs Eager",
)
group.add_argument(
"--accuracy-aot-ts",
action="store_true",
help="Accuracy testing and speedup for AOT with Torchscript(NNC/NVFuser) vs Eager",
)
group.add_argument(
"--accuracy-aot-ts-mincut",
action="store_true",
help="Accuracy testing and speedup for AOT with Torchscript(NNC/NVFuser) with mincut vs Eager",
)
group.add_argument(
"--accuracy-ts",
action="store_true",
help="Accuracy testing and speedup using Torchscript (NNC/NVFuser) vs eager",
)
group.add_argument(
"--backend",
choices=torchdynamo.list_backends(),
help="measure speedup with a given backend",
)
group.add_argument("--nothing", action="store_true", help=help(null_experiment))
group.add_argument(
"--nops",
action="store_true",
help="Test that bytecode rewriting works properly.",
)
args = parser.parse_args()
# defaults
args.devices = args.devices or ["cpu"]
args.filter = args.filter or [r"."]
args.exclude = args.exclude or [r"^$"]
if args.devices != ["cpu"] and torch.cuda.is_available():
global synchronize
synchronize = torch.cuda.synchronize
if (
args.devices == ["cuda"]
and torch.cuda.get_device_properties(0).total_memory < 25 * 2**30
):
# OOM errors on an RTX 3090 with 24gb RAM
SKIP.update(
{
"hf_Longformer",
"timm_nfnet",
}
)
if torchdynamo.config.dynamic_shapes:
# TODO(jansel): fix bugs in these
SKIP.update(
{
"demucs",
"timm_nfnet",
}
)
if args.no_skip:
SKIP.clear()
if args.nvfuser:
torch._C._jit_override_can_fuse_on_cpu(False)
torch._C._jit_override_can_fuse_on_gpu(False)
torch._C._jit_set_texpr_fuser_enabled(False)
torch._C._jit_set_nvfuser_enabled(True)
else:
torch._C._jit_override_can_fuse_on_cpu(True)
torch._C._jit_override_can_fuse_on_gpu(True)
torch._C._jit_set_texpr_fuser_enabled(True)
if torch.cuda.is_available():
torch._C._jit_set_nvfuser_enabled(False)
if args.threads:
torch.set_num_threads(args.threads)
if args.verbose:
torchdynamo.config.debug = True
if args.training:
model_iter_fn = forward_and_backward_pass
SKIP.update(SKIP_TRAIN)
else:
model_iter_fn = forward_pass
if args.no_skip:
SKIP.clear()
experiment = null_experiment
optimize_ctx = NullContext()
global output_filename
if args.overhead:
optimize_ctx = torchdynamo.optimize(dummy_fx_compile, nopython=args.nopython)
experiment = speedup_experiment
output_filename = "overheads.csv"
elif args.online_autotune:
optimize_ctx = torchdynamo.optimize(online_autotuner, nopython=args.nopython)
experiment = speedup_experiment
output_filename = "speedups.csv"
args.isolate = True
elif args.offline_autotune:
optimize_ctx = torchdynamo.optimize(offline_autotuner, nopython=args.nopython)
experiment = speedup_experiment
output_filename = "speedups.csv"
args.isolate = True
elif args.python_key:
optimize_ctx = torchdynamo.optimize(python_key, nopython=args.nopython)
experiment = speedup_experiment
output_filename = "pythonkey.csv"
if not args.no_skip:
SKIP.update(
[
# requires training mode
"maml",
# RuntimeError: toIValue() cannot handle converting to type: QScheme
"mobilenet_v2_quantized_qat",
"resnet50_quantized_qat",
# RuntimeError: set_storage_offset is not allowed on a Tensor created from .data or .detach()
"hf_BigBird",
# RuntimeError: DispatchKey PythonTLSSnapshot doesn't correspond to a device
"hf_Reformer",
]
)
elif args.speedup_ltc:
optimize_ctx = torchdynamo.optimize(
backends.ltc_reuse_graph, nopython=args.nopython
)
experiment = speedup_experiment
output_filename = "speedups_ltc.csv"
args.isolate = True
elif args.speedup_ltc_trivial:
optimize_ctx = torchdynamo.optimize(
backends.ltc_trivial, nopython=args.nopython
)
experiment = speedup_experiment
output_filename = "speedups_ltc_trivial.csv"
args.isolate = True
elif args.speedup_fixed1:
optimize_ctx = torchdynamo.optimize(fixed_strategy1, nopython=args.nopython)
experiment = speedup_experiment
output_filename = "speedups_fixed1.csv"
args.isolate = True
elif args.speedup_fixed2:
optimize_ctx = torchdynamo.optimize(fixed_strategy2, nopython=args.nopython)
experiment = speedup_experiment
output_filename = "speedups_fixed2.csv"
args.isolate = True
elif args.speedup_ts:
experiment = speedup_experiment_ts
output_filename = "baseline_ts.csv"
elif args.speedup_sr:
experiment = speedup_experiment_sr
output_filename = "baseline_sr.csv"
elif args.speedup_onnx:
experiment = speedup_experiment_onnx
output_filename = "baseline_onnx.csv"
elif args.speedup_trt:
experiment = speedup_experiment_trt
output_filename = "baseline_trt.csv"
elif args.speedup_fx2trt:
optimize_ctx = torchdynamo.optimize(
backends.fx2trt_compiler, nopython=args.nopython
)
experiment = speedup_experiment_fx2trt
output_filename = "speedups_fx2trt.csv"
elif args.speedup_fx2trt_fp16:
optimize_ctx = torchdynamo.optimize(
backends.fx2trt_compiler_fp16, nopython=args.nopython
)
experiment = speedup_experiment_fx2trt
output_filename = "speedups_fx2trt_fp16.csv"
elif args.accuracy_aot_nop:
optimize_ctx = torchdynamo.optimize(
aot_autograd_debug_strategy1, nopython=args.nopython
)
experiment = speedup_experiment
output_filename = "accuracy_aot_nop.csv"
elif args.accuracy_aot_ts:
optimize_ctx = torchdynamo.optimize(
aot_autograd_nnc_strategy, nopython=args.nopython
)
experiment = speedup_experiment
backend_str = "nvfuser" if args.nvfuser else "nnc"
output_filename = f"accuracy_aot_{backend_str}.csv"
elif args.accuracy_aot_ts_mincut:
optimize_ctx = torchdynamo.optimize(
aot_autograd_speedup_strategy, nopython=args.nopython
)
experiment = speedup_experiment
backend_str = "nvfuser" if args.nvfuser else "nnc"
output_filename = f"accuracy_aot_{backend_str}_mincut.csv"
elif args.accuracy_ts:
optimize_ctx = torchdynamo.optimize(fixed_strategy1, nopython=args.nopython)
experiment = speedup_experiment
backend_str = "nvfuser" if args.nvfuser else "nnc"
output_filename = f"accuracy_{backend_str}.csv"
elif args.nothing:
pass
elif args.nops:
optimize_ctx = torchdynamo.eval_frame._optimize_catch_errors(
torchdynamo.testing.debug_insert_nops, nopython=args.nopython
)
elif args.backend:
optimize_ctx = torchdynamo.optimize(args.backend, nopython=args.nopython)
experiment = speedup_experiment
output_filename = f"speedup_{args.backend}.csv"
else:
optimize_ctx = torchdynamo.optimize(fx_insert_profiling, nopython=args.nopython)
experiment = coverage_experiment
output_filename = "coverage.csv"
experiment = functools.partial(experiment, args, model_iter_fn)
if args.speedup_fx2trt_fp16:
cos_similarity = True
else:
cos_similarity = False
if output_filename:
output_filename = os.path.join(torchdynamo.config.base_dir, output_filename)
if args.disable_functionalization:
torchdynamo.config.normalize_ir = False
if args.minimum_call_count:
torchdynamo.config.minimum_call_count = args.minimum_call_count
if args.only:
for device in args.devices:
try:
device, name, model, example_inputs = load_model(
device, args.only, args.training, args.use_eval_mode
)
# torchbench changed the default precison=fp16 on torchvision net
if args.speedup_fx2trt:
if name in (
"alexnet",
"resnet18",
"resnet50",
"mobilenet_v2",
"mnasnet1_0",
"squeezenet1_1",
"shufflenetv2_x1_0",
"vgg16",
"resnext50_32x4d",
):
print("Do not test vision models in fp32 mode")
continue # We need to cast model and inputs back to fp32 before we can enable it
if args.speedup_fx2trt_fp16:
model, example_inputs = cast_to_fp16(model, example_inputs)
except NotImplementedError:
continue # bad benchmark implementation
run_one_model(
name,
model,
args.training,
model_iter_fn,
example_inputs,
optimize_ctx,
experiment,
cos_similarity,
args.skip_accuracy_check,
)
stats_file = output_filename.split(".csv")[0] + "_stats.csv"
if args.generate_aot_autograd_stats:
output_csv(
stats_file,
("dev", "name", "total_aot_graphs", "ok_aot_graphs"),
[current_device, current_name, *Stats.aot_summary()],
)
elif args.isolate:
if output_filename and os.path.exists(output_filename):
os.unlink(output_filename)
os.chdir(torchdynamo.config.base_dir)
for name in iter_model_names(args):
try:
subprocess.check_call([sys.executable] + sys.argv + [f"--only={name}"])
except subprocess.SubprocessError:
print("ERROR")
for device in args.devices:
output_csv(output_filename, [], [device, name, 0.0])
print_summary(output_filename)
else:
if output_filename and os.path.exists(output_filename):
os.unlink(output_filename)
for device, name, model, example_inputs in iter_models(args):
torchdynamo.reset()
gc.collect()
run_one_model(
name,
model,
args.training,
model_iter_fn,
example_inputs,
optimize_ctx,
experiment,
cos_similarity,
args.skip_accuracy_check,
)
Stats.print_summary()
print_summary(output_filename)