在Linux環境下監控PyTorch性能,可從系統資源監控、GPU監控、訓練過程監控三方面入手,常用工具及方法如下:
htop支持交互式排序(如按CPU/內存使用率)。watch -n 1 nvidia-smi可每秒刷新。from torch.profiler import profile
with profile(activities=[ProfilerActivity.CPU, ProfilerActivity.MEMORY]) as prof:
# 訓練代碼
print(prof.key_averages().table(sort_by="cuda_time_total"))
cuda-memcheck檢測內存錯誤,nvprof分析GPU內核性能(需安裝CUDA Toolkit)。SummaryWriter使用。from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter('runs/experiment')
writer.add_scalar('Loss/train', loss.item(), epoch)
writer.close()
logging模塊記錄關鍵指標(如每輪損失、學習率),保存到文件便于分析。torch.distributed和Dynolog,支持多GPU訓練的分布式追蹤。perf分析CPU緩存命中率、磁盤IO延遲等底層性能。選擇建議:
nvidia-smi + htop + TensorBoard(覆蓋GPU、系統資源及訓練指標)。perf(定位性能瓶頸)。torch.distributed(追蹤跨節點性能)。