MCPcopy Create free account
hub / github.com/OpenSparseLLMs/MoM / benchmark_forward

Function benchmark_forward

benchmarks/ops/benchmark.py:8–27  ·  view source on GitHub ↗

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
)

Source from the content-addressed store, hash-verified

6
7
8def 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
30def benchmark_backward(

Callers 3

time_fwdFunction · 0.90
benchmark_fwd_bwdFunction · 0.85
benchmark_allFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected