Use Pytorch Benchmark on the forward pass of an arbitrary function.
(
fn, *inputs, repeats=10, desc="", verbose=True, amp=False, amp_dtype=torch.float16, **kwinputs
)
| 6 | |
| 7 | |
| 8 | def benchmark_forward( |
| 9 | fn, *inputs, repeats=10, desc="", verbose=True, amp=False, amp_dtype=torch.float16, **kwinputs |
| 10 | ): |
| 11 | """Use Pytorch Benchmark on the forward pass of an arbitrary function.""" |
| 12 | if verbose: |
| 13 | print(desc, "- Forward pass") |
| 14 | |
| 15 | def amp_wrapper(*inputs, **kwinputs): |
| 16 | with torch.autocast(device_type="cuda", dtype=amp_dtype, enabled=amp): |
| 17 | fn(*inputs, **kwinputs) |
| 18 | |
| 19 | t = benchmark.Timer( |
| 20 | stmt="fn_amp(*inputs, **kwinputs)", |
| 21 | globals={"fn_amp": amp_wrapper, "inputs": inputs, "kwinputs": kwinputs}, |
| 22 | num_threads=torch.get_num_threads(), |
| 23 | ) |
| 24 | m = t.timeit(repeats) |
| 25 | if verbose: |
| 26 | print(m) |
| 27 | return t, m |
| 28 | |
| 29 | |
| 30 | def benchmark_backward( |
no outgoing calls
no test coverage detected