|
| 1 | +"""This file contains a few functions to profile the memory usage of the model. |
| 2 | +
|
| 3 | +It is not meant to be used in production, but rather to help us debug the memory usage of the model. |
| 4 | +
|
| 5 | +The codes are borrowed from https://github.com/huggingface/accelerate/blob/main/benchmarks/measures_util.py |
| 6 | +""" |
| 7 | + |
| 8 | +import gc |
| 9 | +import threading |
| 10 | +import time |
| 11 | + |
| 12 | +import psutil |
| 13 | +import torch |
| 14 | +from accelerate.utils import compute_module_sizes |
| 15 | + |
| 16 | + |
| 17 | +class PeakCPUMemory: |
| 18 | + def __init__(self): |
| 19 | + self.process = psutil.Process() |
| 20 | + self.peak_monitoring = False |
| 21 | + |
| 22 | + def peak_monitor(self): |
| 23 | + self.cpu_memory_peak = -1 |
| 24 | + |
| 25 | + while True: |
| 26 | + self.cpu_memory_peak = max( |
| 27 | + self.process.memory_info().rss, self.cpu_memory_peak |
| 28 | + ) |
| 29 | + |
| 30 | + # can't sleep or will not catch the peak right (this comment is here on purpose) |
| 31 | + if not self.peak_monitoring: |
| 32 | + break |
| 33 | + |
| 34 | + def start(self): |
| 35 | + self.peak_monitoring = True |
| 36 | + self.thread = threading.Thread(target=self.peak_monitor) |
| 37 | + self.thread.daemon = True |
| 38 | + self.thread.start() |
| 39 | + |
| 40 | + def stop(self): |
| 41 | + self.peak_monitoring = False |
| 42 | + self.thread.join() |
| 43 | + return self.cpu_memory_peak |
| 44 | + |
| 45 | + |
| 46 | +cpu_peak_tracker = PeakCPUMemory() |
| 47 | + |
| 48 | + |
| 49 | +def start_measure(): |
| 50 | + # Time |
| 51 | + measures = {"time": time.time()} |
| 52 | + |
| 53 | + gc.collect() |
| 54 | + torch.cuda.empty_cache() |
| 55 | + |
| 56 | + # CPU mem |
| 57 | + measures["cpu"] = psutil.Process().memory_info().rss |
| 58 | + cpu_peak_tracker.start() |
| 59 | + |
| 60 | + # GPU mem |
| 61 | + for i in range(torch.cuda.device_count()): |
| 62 | + measures[str(i)] = torch.cuda.memory_allocated(i) |
| 63 | + torch.cuda.reset_peak_memory_stats() |
| 64 | + |
| 65 | + return measures |
| 66 | + |
| 67 | + |
| 68 | +def end_measure(start_measures): |
| 69 | + # Time |
| 70 | + measures = {"time": time.time() - start_measures["time"]} |
| 71 | + |
| 72 | + gc.collect() |
| 73 | + torch.cuda.empty_cache() |
| 74 | + |
| 75 | + # CPU mem |
| 76 | + measures["cpu"] = ( |
| 77 | + psutil.Process().memory_info().rss - start_measures["cpu"] |
| 78 | + ) / 2**20 |
| 79 | + measures["cpu-peak"] = (cpu_peak_tracker.stop() - start_measures["cpu"]) / 2**20 |
| 80 | + |
| 81 | + # GPU mem |
| 82 | + for i in range(torch.cuda.device_count()): |
| 83 | + measures[str(i)] = ( |
| 84 | + torch.cuda.memory_allocated(i) - start_measures[str(i)] |
| 85 | + ) / 2**20 |
| 86 | + measures[f"{i}-peak"] = ( |
| 87 | + torch.cuda.max_memory_allocated(i) - start_measures[str(i)] |
| 88 | + ) / 2**20 |
| 89 | + |
| 90 | + return measures |
| 91 | + |
| 92 | + |
| 93 | +def log_measures(measures, description): |
| 94 | + print(f"{description}:") |
| 95 | + print(f"- Time: {measures['time']:.2f}s") |
| 96 | + for i in range(torch.cuda.device_count()): |
| 97 | + print(f"- GPU {i} allocated: {measures[str(i)]:.2f}MiB") |
| 98 | + peak = measures[f"{i}-peak"] |
| 99 | + print(f"- GPU {i} peak: {peak:.2f}MiB") |
| 100 | + print(f"- CPU RAM allocated: {measures['cpu']:.2f}MiB") |
| 101 | + print(f"- CPU RAM peak: {measures['cpu-peak']:.2f}MiB") |
0 commit comments