在 CentOS 上监控 PyTorch 训练/推理任务,通常从“系统资源”“GPU 资源”“PyTorch 内部状态”三个层面来做。下面按实用程度给你一套常见方案。
top
htop # 推荐,需安装:yum install htop -y
free -h
查看某个 Python / PyTorch 进程:
ps -ef | grep python
nvidia-smi
实时监控(每 1 秒刷新):
watch -n 1 nvidia-smi
查看更详细 GPU 使用率:
nvidia-smi dmon
关注指标:
nvidia-smi -L
确认 PyTorch 是否真的用到多张卡:
import torch
print(torch.cuda.device_count())
import torch
print(torch.cuda.memory_allocated() / 1024**2, "MB")
print(torch.cuda.memory_reserved() / 1024**2, "MB")
清空缓存:
torch.cuda.empty_cache()
最常见方式:
printTensorBoardwandbpip install tensorboard
tensorboard --logdir=logs --host=0.0.0.0 --port=6006
在 PyTorch 中:
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter("logs")
writer.add_scalar("loss", loss.item(), step)
nohup python train.py > train.log 2>&1 &
或:
screen -S pytorch
python train.py
# Ctrl+A D 退出
查看日志:
tail -f train.log
node_exporter:CPU / 内存dcgm-exporter:NVIDIA GPU适合:
nvtop(GPU 版 htop)yum install nvtop -y
DataLoader num_workers)loss.backward() 没调用batch_size 太大with torch.no_grad()CentOS 上监控 PyTorch = nvidia-smi + htop + TensorBoard + 日志
如果你愿意,可以告诉我:
我可以直接给你一套可复制的监控脚本。