Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docs for timer_and_memory_stats in benchmarks/targets.py #522

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions thunder/benchmarks/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,40 @@ def is_requires_grad(type: ComputeType):


def timer_and_memory_stats(benchmark) -> float:
def deco(func):
@functools.wraps(func)
def wrapper():
ret = func()
"""
Make a timer that also records the peak allocated memory.

pytest-benchmark has the following benchmarking code structure:

start = timer()
for _ in loops_range:
function_to_benchmark(*args, **kwargs)
end = timer()

So the information about the peak allocated memory should be recorded
after the function_to_benchmark call and we need to reset the peak memory
stats before the function_to_benchmark call.

If reset_peak_memory_stats is called inside the function_to_benchmark call,
the peak memory stats will be reset multiple times and the peak memory
stats may not be accurate.

Args:
benchmark: The pytest-benchmark object

Returns:
The decorator that records the peak allocated memory
"""

def deco(old_timer):
@functools.wraps(old_timer)
def timer():
ret = old_timer()
benchmark.extra_info["max_allocated_memory(MB)"] = torch.cuda.max_memory_allocated() / (1024 * 1024.0)
torch.cuda.reset_peak_memory_stats()
return ret

return wrapper
return timer

return deco

Expand Down
Loading