|
| 1 | +from contextlib import contextmanager |
| 2 | +from typing import Any, List, Tuple |
| 3 | +from torch.testing import make_tensor |
| 4 | +import random |
| 5 | +import torch |
| 6 | +import time |
| 7 | + |
| 8 | + |
| 9 | +# TODO - a lot of this was copied from pytorch/jit/scripts/log_extract.py, should we put it somewhere in torch? (and where?) |
| 10 | + |
| 11 | +@contextmanager |
| 12 | +def no_fuser(*args, **kwargs): |
| 13 | + old_cpu_fuse = torch._C._jit_can_fuse_on_cpu() |
| 14 | + old_gpu_fuse = torch._C._jit_can_fuse_on_gpu() |
| 15 | + old_texpr_fuser_state = torch._C._jit_texpr_fuser_enabled() |
| 16 | + old_nvfuser_state = torch._C._jit_nvfuser_enabled() |
| 17 | + |
| 18 | + torch._C._jit_override_can_fuse_on_cpu(False) |
| 19 | + torch._C._jit_override_can_fuse_on_gpu(False) |
| 20 | + torch._C._jit_set_texpr_fuser_enabled(False) |
| 21 | + torch._C._jit_set_nvfuser_enabled(False) |
| 22 | + |
| 23 | + try: |
| 24 | + yield |
| 25 | + finally: |
| 26 | + torch._C._jit_override_can_fuse_on_cpu(old_cpu_fuse) |
| 27 | + torch._C._jit_override_can_fuse_on_gpu(old_gpu_fuse) |
| 28 | + torch._C._jit_set_texpr_fuser_enabled(old_texpr_fuser_state) |
| 29 | + torch._C._jit_set_nvfuser_enabled(old_nvfuser_state) |
| 30 | + |
| 31 | + |
| 32 | +def make_tensor_from_type(inp_type: torch._C.TensorType): |
| 33 | + if inp_type.requires_grad() is not False: |
| 34 | + raise NotImplementedError("Tensors with requires_grad are not implemented") |
| 35 | + return make_tensor( |
| 36 | + inp_type.sizes(), |
| 37 | + dtype=inp_type.dtype(), |
| 38 | + device=inp_type.device()) |
| 39 | + |
| 40 | + |
| 41 | +def load_graph_and_inputs(ir: str) -> Tuple[Any, List[Any]]: |
| 42 | + graph = torch._C.parse_ir(ir) |
| 43 | + graph.makeMultiOutputIntoTuple() |
| 44 | + inputs = [] |
| 45 | + for inp in graph.inputs(): |
| 46 | + if isinstance(inp.type(), torch._C.FloatType): |
| 47 | + inputs.append(random.uniform(.1, 100)) |
| 48 | + elif isinstance(inp.type(), torch._C.IntType): |
| 49 | + inputs.append(random.randint(1, 100)) |
| 50 | + elif isinstance(inp.type(), torch._C.TensorType): |
| 51 | + inputs.append(make_tensor_from_type(inp.type())) |
| 52 | + else: |
| 53 | + raise NotImplementedError(f"A default value is not implemented for type {inp.type()}") |
| 54 | + |
| 55 | + func = torch._C._create_function_from_graph("forward", graph) |
| 56 | + torch._C._jit_pass_erase_shape_information(func.graph) |
| 57 | + return (func, inputs) |
| 58 | + |
| 59 | + |
| 60 | +def time_cuda(fn, inputs, test_runs): |
| 61 | + start_event = torch.cuda.Event(enable_timing=True) |
| 62 | + end_event = torch.cuda.Event(enable_timing=True) |
| 63 | + torch.cuda.synchronize() |
| 64 | + start_event.record() |
| 65 | + torch.cuda.synchronize() |
| 66 | + for i in range(test_runs): |
| 67 | + fn(*inputs) |
| 68 | + torch.cuda.synchronize() |
| 69 | + end_event.record() |
| 70 | + torch.cuda.synchronize() |
| 71 | + return start_event.elapsed_time(end_event) / test_runs |
| 72 | + |
| 73 | +def time_cpu(fn, inputs, test_runs): |
| 74 | + s = time.perf_counter() |
| 75 | + for _ in range(test_runs): |
| 76 | + fn(*inputs) |
| 77 | + e = time.perf_counter() |
| 78 | + return (e - s) / test_runs |
| 79 | + |
| 80 | + |
| 81 | +def run_test(ir, inputs, *, warmup_runs=10, test_runs=20) -> float: |
| 82 | + graph, _ = load_graph_and_inputs(ir) |
| 83 | + for _ in range(warmup_runs): |
| 84 | + graph(*inputs) |
| 85 | + |
| 86 | + is_cpu = None |
| 87 | + for input in inputs: |
| 88 | + if isinstance(input, torch.Tensor): |
| 89 | + is_cpu = input.device.type == "cpu" |
| 90 | + break |
| 91 | + assert is_cpu != None |
| 92 | + |
| 93 | + out = time_cpu(graph, inputs, test_runs) if is_cpu else time_cuda(graph, inputs, test_runs) |
| 94 | + return out |
| 95 | + |
| 96 | + |
| 97 | +class NVFuserBenchmark(): |
| 98 | + def __init__(self, name, ir, warmup_runs = 10, test_runs = 20): |
| 99 | + # TODO - random seed? |
| 100 | + self.name = name |
| 101 | + self.ir = ir |
| 102 | + self.warmup_runs = warmup_runs |
| 103 | + self.test_runs = test_runs |
| 104 | + |
| 105 | + def run_test(self, inputs, fuser_name: str) -> float: |
| 106 | + if fuser_name == "no_fuser": |
| 107 | + with no_fuser(): |
| 108 | + return run_test(self.ir, inputs, warmup_runs=self.warmup_runs, test_runs=self.test_runs) |
| 109 | + with torch.jit.fuser(fuser_name): |
| 110 | + return run_test(self.ir, inputs, warmup_runs=self.warmup_runs, test_runs=self.test_runs) |
| 111 | + |
| 112 | + def get_inputs(self) -> List[Any]: |
| 113 | + _, inputs = load_graph_and_inputs(ir) |
| 114 | + return inputs |
| 115 | + |
| 116 | + |
| 117 | +def get_nvfuser_microbenchmarks(): |
| 118 | + from torchbenchmark.microbenchmarks.nvfuser_ir import ir_list |
| 119 | + benchmarks = [NVFuserBenchmark(name, ir) for name, ir in ir_list] |
| 120 | + return benchmarks |
0 commit comments