Skip to content

Commit

Permalink
Include the torchao utils
Browse files Browse the repository at this point in the history
  • Loading branch information
lancerts committed May 18, 2024
1 parent 8348c0b commit 58b64ae
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions torchao/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import torch


def benchmark_model(model, num_runs, input_tensor):
torch.cuda.synchronize()
start_event = torch.cuda.Event(enable_timing=True)
end_event = torch.cuda.Event(enable_timing=True)
start_event.record()

# benchmark
for _ in range(num_runs):
with torch.autograd.profiler.record_function("timed region"):
model(input_tensor)

end_event.record()
torch.cuda.synchronize()
return start_event.elapsed_time(end_event) / num_runs

def profiler_runner(path, fn, *args, **kwargs):
with torch.profiler.profile(
activities=[torch.profiler.ProfilerActivity.CPU,
torch.profiler.ProfilerActivity.CUDA],
record_shapes=True) as prof:
result = fn(*args, **kwargs)
prof.export_chrome_trace(path)
return result

0 comments on commit 58b64ae

Please sign in to comment.