| 1 |
# Copyright 2023 The HuggingFace Team. All rights reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
import gc |
| 15 |
import threading |
| 16 |
import time |
| 17 |
|
| 18 |
import psutil |
| 19 |
import torch |
| 20 |
|
| 21 |
from accelerate.test_utils.testing import get_backend |
| 22 |
|
| 23 |
|
| 24 |
torch_device_type, _, _ = get_backend() |
| 25 |
torch_accelerator_module = getattr(torch, torch_device_type, torch.cuda) |
| 26 |
|
| 27 |
|
| 28 |
class PeakCPUMemory: |
| 29 |
def __init__(self): |
| 30 |
self.process = psutil.Process() |
| 31 |
self.peak_monitoring = False |
| 32 |
|
| 33 |
def peak_monitor(self): |
| 34 |
self.cpu_memory_peak = -1 |
| 35 |
|
| 36 |
while True: |
| 37 |
self.cpu_memory_peak = max(self.process.memory_info().rss, self.cpu_memory_peak) |
| 38 |
|
| 39 |
# can't sleep or will not catch the peak right (this comment is here on purpose) |
| 40 |
if not self.peak_monitoring: |
| 41 |
break |
| 42 |
|
| 43 |
def start(self): |
| 44 |
self.peak_monitoring = True |
| 45 |
self.thread = threading.Thread(target=self.peak_monitor) |
| 46 |
self.thread.daemon = True |
| 47 |
self.thread.start() |
| 48 |
|
| 49 |
def stop(self): |
| 50 |
self.peak_monitoring = False |
| 51 |
self.thread.join() |
| 52 |
return self.cpu_memory_peak |
| 53 |
|
| 54 |
|
| 55 |
cpu_peak_tracker = PeakCPUMemory() |
| 56 |
|
| 57 |
|
| 58 |
def start_measure(): |
| 59 |
# Time |
| 60 |
measures = {"time": time.time()} |
| 61 |
|
| 62 |
gc.collect() |
| 63 |
torch_accelerator_module.empty_cache() |
| 64 |
|
| 65 |
# CPU mem |
| 66 |
measures["cpu"] = psutil.Process().memory_info().rss |
| 67 |
cpu_peak_tracker.start() |
| 68 |
|
| 69 |
# GPU mem |
| 70 |
for i in range(torch_accelerator_module.device_count()): |
| 71 |
measures[str(i)] = torch_accelerator_module.memory_allocated(i) |
| 72 |
torch_accelerator_module.reset_peak_memory_stats() |
| 73 |
|
| 74 |
return measures |
| 75 |
|
| 76 |
|
| 77 |
def end_measure(start_measures): |
| 78 |
# Time |
| 79 |
measures = {"time": time.time() - start_measures["time"]} |
| 80 |
|
| 81 |
gc.collect() |
| 82 |
torch_accelerator_module.empty_cache() |
| 83 |
|
| 84 |
# CPU mem |
| 85 |
measures["cpu"] = (psutil.Process().memory_info().rss - start_measures["cpu"]) / 2**20 |
| 86 |
measures["cpu-peak"] = (cpu_peak_tracker.stop() - start_measures["cpu"]) / 2**20 |
| 87 |
|
| 88 |
# GPU mem |
| 89 |
for i in range(torch_accelerator_module.device_count()): |
| 90 |
measures[str(i)] = (torch_accelerator_module.memory_allocated(i) - start_measures[str(i)]) / 2**20 |
| 91 |
measures[f"{i}-peak"] = (torch_accelerator_module.max_memory_allocated(i) - start_measures[str(i)]) / 2**20 |
| 92 |
|
| 93 |
return measures |
| 94 |
|
| 95 |
|
| 96 |
def log_measures(measures, description): |
| 97 |
print(f"{description}:") |
| 98 |
print(f"- Time: {measures['time']:.2f}s") |
| 99 |
for i in range(torch_accelerator_module.device_count()): |
| 100 |
print(f"- {torch_device_type} {i} allocated: {measures[str(i)]:.2f}MiB") |
| 101 |
peak = measures[f"{i}-peak"] |
| 102 |
print(f"- {torch_device_type} {i} peak: {peak:.2f}MiB") |
| 103 |
print(f"- CPU RAM allocated: {measures['cpu']:.2f}MiB") |
| 104 |
print(f"- CPU RAM peak: {measures['cpu-peak']:.2f}MiB") |