Last active
September 23, 2024 16:35
-
-
Save ovuruska/55c6c50c9f60eb14c1136025f0b0c75e to your computer and use it in GitHub Desktop.
Measure Max GPU Usage: Python Decorator for CUDA Memory Tracking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def measure_max_gpu_usage(func): | |
from torch.cuda import max_memory_allocated, reset_peak_memory_stats | |
def wrapper(*args, **kwargs): | |
reset_peak_memory_stats() | |
result = func(*args, **kwargs) | |
max_memory = max_memory_allocated() / 1024 / 1024 / 1024 | |
print(f"Maximum GPU Usage: {max_memory:.3f} GB") | |
return result | |
return wrapper | |
############ Example usage ############ | |
@measure_max_gpu_usage | |
def example_func(): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment