|
| 1 | +import torch |
| 2 | +from torch.autograd import functional |
| 3 | + |
| 4 | +import time |
| 5 | +from argparse import ArgumentParser |
| 6 | +from collections import defaultdict |
| 7 | +from typing import NamedTuple, Callable, List, Any |
| 8 | + |
| 9 | +import ppl_models |
| 10 | +import vision_models |
| 11 | +import audio_text_models |
| 12 | + |
| 13 | +from utils import to_markdown_table, TimingResultType, InputsType, GetterType, VType |
| 14 | + |
| 15 | +# Listing of the different tasks |
| 16 | +FAST_TASKS_NO_DOUBLE_BACK = [ |
| 17 | + "vjp", |
| 18 | +] |
| 19 | + |
| 20 | +FAST_TASKS = FAST_TASKS_NO_DOUBLE_BACK + [ |
| 21 | + "vhp", |
| 22 | + "jvp", |
| 23 | +] |
| 24 | + |
| 25 | +ALL_TASKS = FAST_TASKS + [ |
| 26 | + "hvp", |
| 27 | + "jacobian", |
| 28 | + "hessian" |
| 29 | +] |
| 30 | + |
| 31 | +DOUBLE_BACKWARD_TASKS = ["jvp", "hvp", "vhp", "hessian"] |
| 32 | + |
| 33 | +# Model definition which contains: |
| 34 | +# - name: a string with the model name. |
| 35 | +# - getter: a function to get the model. It takes as input the device on which the model |
| 36 | +# will run. It should return the forward function and the parameters (Tensors) used as |
| 37 | +# input for the forward function. Note that the forward must *not* have any side effect. |
| 38 | +# - tasks: the list of recommended tasks that can run in a reasonable amount of time with this model. |
| 39 | +# - unsupported: the list of tasks that this model cannot run. |
| 40 | +class ModelDef(NamedTuple): |
| 41 | + name: str |
| 42 | + getter: GetterType |
| 43 | + tasks: List[str] |
| 44 | + unsupported: List[str] |
| 45 | + |
| 46 | +MODELS = [ |
| 47 | + ModelDef("resnet18", vision_models.get_resnet18, FAST_TASKS, []), |
| 48 | + ModelDef("fcn_resnet", vision_models.get_fcn_resnet, FAST_TASKS, []), |
| 49 | + ModelDef("detr", vision_models.get_detr, FAST_TASKS, []), |
| 50 | + ModelDef("ppl_simple_reg", ppl_models.get_simple_regression, ALL_TASKS, []), |
| 51 | + ModelDef("ppl_robust_reg", ppl_models.get_robust_regression, ALL_TASKS, []), |
| 52 | + ModelDef("wav2letter", audio_text_models.get_wav2letter, FAST_TASKS, []), |
| 53 | + ModelDef("deepspeech", audio_text_models.get_deepspeech, FAST_TASKS_NO_DOUBLE_BACK, DOUBLE_BACKWARD_TASKS), |
| 54 | + ModelDef("transformer", audio_text_models.get_transformer, FAST_TASKS, []), |
| 55 | + ModelDef("multiheadattn", audio_text_models.get_multiheadattn, FAST_TASKS, []), |
| 56 | +] |
| 57 | + |
| 58 | +def get_v_for(model: Callable, inp: InputsType, task: str) -> VType: |
| 59 | + v: VType |
| 60 | + |
| 61 | + if task in ["vjp"]: |
| 62 | + out = model(*inp) |
| 63 | + v = torch.rand_like(out) |
| 64 | + elif task in ["jvp", "hvp", "vhp"]: |
| 65 | + if isinstance(inp, tuple): |
| 66 | + v = tuple(torch.rand_like(i) for i in inp) |
| 67 | + else: |
| 68 | + v = torch.rand_like(inp) |
| 69 | + else: |
| 70 | + v = None |
| 71 | + |
| 72 | + return v |
| 73 | + |
| 74 | +def run_once(model: Callable, inp: InputsType, task: str, v: VType) -> None: |
| 75 | + func = getattr(functional, task) |
| 76 | + |
| 77 | + if v is not None: |
| 78 | + res = func(model, inp, v=v, strict=True) |
| 79 | + else: |
| 80 | + res = func(model, inp, strict=True) |
| 81 | + |
| 82 | +def run_model(model_getter: GetterType, args: Any, task: str) -> List[float]: |
| 83 | + if args.gpu == -1: |
| 84 | + device = torch.device("cpu") |
| 85 | + |
| 86 | + def noop(): |
| 87 | + pass |
| 88 | + do_sync = noop |
| 89 | + else: |
| 90 | + device = torch.device("cuda:{}".format(args.gpu)) |
| 91 | + do_sync = torch.cuda.synchronize |
| 92 | + |
| 93 | + model, inp = model_getter(device) |
| 94 | + |
| 95 | + v = get_v_for(model, inp, task) |
| 96 | + # Warmup |
| 97 | + run_once(model, inp, task, v) |
| 98 | + |
| 99 | + elapsed = [] |
| 100 | + for it in range(args.num_iters): |
| 101 | + do_sync() |
| 102 | + start = time.time() |
| 103 | + run_once(model, inp, task, v) |
| 104 | + do_sync() |
| 105 | + elapsed.append(time.time() - start) |
| 106 | + |
| 107 | + return elapsed |
| 108 | + |
| 109 | +def main(): |
| 110 | + parser = ArgumentParser("Main script to benchmark functional API of the autograd.") |
| 111 | + parser.add_argument("--output", type=str, default="", help="Text file where to write the output") |
| 112 | + parser.add_argument("--num-iters", type=int, default=10) |
| 113 | + parser.add_argument("--gpu", type=int, default=-2, help="GPU to use, -1 for CPU and -2 for auto-detect") |
| 114 | + parser.add_argument("--run-slow-tasks", action="store_true", help="Run even the slow tasks") |
| 115 | + parser.add_argument("--model-filter", type=str, default="", help="Only run the models in this filter") |
| 116 | + parser.add_argument("--task-filter", type=str, default="", help="Only run the tasks in this filter") |
| 117 | + parser.add_argument("--num-threads", type=int, default=10, |
| 118 | + help="Number of concurrent threads to use when running on cpu") |
| 119 | + parser.add_argument("--seed", type=int, default=0, help="The random seed to use.") |
| 120 | + args = parser.parse_args() |
| 121 | + |
| 122 | + results: TimingResultType = defaultdict(defaultdict) |
| 123 | + torch.set_num_threads(args.num_threads) |
| 124 | + torch.set_num_interop_threads(args.num_threads) |
| 125 | + |
| 126 | + # This automatically seed cuda if it is available |
| 127 | + torch.manual_seed(args.seed) |
| 128 | + |
| 129 | + if args.gpu == -2: |
| 130 | + args.gpu = 0 if torch.cuda.is_available() else -1 |
| 131 | + |
| 132 | + for name, model_getter, recommended_tasks, unsupported_tasks in MODELS: |
| 133 | + if args.model_filter and name not in args.model_filter: |
| 134 | + continue |
| 135 | + tasks = ALL_TASKS if args.run_slow_tasks else recommended_tasks |
| 136 | + for task in tasks: |
| 137 | + if task in unsupported_tasks: |
| 138 | + continue |
| 139 | + if args.task_filter and task not in args.task_filter: |
| 140 | + continue |
| 141 | + runtimes = run_model(model_getter, args, task) |
| 142 | + |
| 143 | + runtimes = torch.tensor(runtimes) |
| 144 | + mean, var = runtimes.mean(), runtimes.var() |
| 145 | + results[name][task] = (mean.item(), var.item()) |
| 146 | + print("Results for model {} on task {}: {}s (var: {})".format(name, task, mean, var)) |
| 147 | + |
| 148 | + if args.output: |
| 149 | + with open(args.output, "w") as f: |
| 150 | + f.write(to_markdown_table(results)) |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + main() |
0 commit comments