-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu_logging_utils.py
More file actions
64 lines (57 loc) · 2.27 KB
/
Copy pathgpu_logging_utils.py
File metadata and controls
64 lines (57 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import logging
import subprocess
from datetime import datetime
from typing import Optional
try:
import torch
except Exception:
torch = None
# Configure a module-level logger
logger = logging.getLogger(__name__)
if not logger.handlers:
# handler = logging.StreamHandler()
formatter = logging.Formatter(
fmt='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
# handler.setFormatter(formatter)
# logger.addHandler(handler)
file_handler = logging.FileHandler("logs/gpu.log")
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.setLevel(logging.INFO)
def log_gpu_memory_nvidia_smi(message: str = "") -> None:
"""Log GPU memory usage using nvidia-smi."""
pass
# timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
# try:
# result = subprocess.run(
# ['nvidia-smi', '--query-gpu=memory.used,memory.total', '--format=csv,nounits,noheader'],
# capture_output=True, text=True, check=True
# )
# used, total = result.stdout.strip().split(',')
# logger.info(f"[{timestamp}] GPU Memory {message}: Used={used.strip()}MB, Total={total.strip()}MB")
# except Exception as e:
# logger.warning(f"[{timestamp}] Unable to query GPU memory with nvidia-smi: {e}")
def log_cuda_memory_pytorch(message: str = "") -> None:
"""Log GPU memory usage using PyTorch if available."""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
if torch is None or not torch.cuda.is_available():
logger.info(f"[{timestamp}] CUDA not available. {message}")
return
try:
free, total = torch.cuda.mem_get_info()
used = total - free
used_mb = used / (1024 ** 2)
free_mb = free / (1024 ** 2)
total_mb = total / (1024 ** 2)
logger.info(
f"[{timestamp}] CUDA Memory {message}: Used={used_mb:.2f}MB, Free={free_mb:.2f}MB, Total={total_mb:.2f}MB"
)
except Exception as e:
logger.warning(f"[{timestamp}] Error querying CUDA memory: {e}")
def flush_cuda_cache() -> None:
"""Attempt to free unused GPU memory."""
if torch is not None and torch.cuda.is_available():
torch.cuda.empty_cache()
logger.info("Flushed CUDA cache")