在分布式大模型训练中,GPU利用率监控是性能调优的关键环节。本文分享一套可复现的监控方案。
核心监控指标
- GPU利用率(Utilization)
- 显存利用率(Memory Utilization)
- CUDA核心利用率
- 通信带宽利用率
实用监控脚本
#!/bin/bash
# gpu_monitor.sh
while true; do
nvidia-smi --query-gpu=utilization.gpu,utilization.memory,memory.used,memory.total\
--format=csv,nounits,noheader -l 1 | \
awk '{print strftime("%Y-%m-%d %H:%M:%S"), $0}'
done
进阶监控方法 使用pytorch的torch.cuda.amp进行混合精度训练时,结合以下代码获取实时GPU状态:
import torch
import time
def monitor_gpu():
while True:
# 获取当前GPU信息
gpu_stats = torch.cuda.memory_stats()
print(f"Used: {gpu_stats['allocated_bytes.all.current']/1024**2:.2f} MB")
time.sleep(1)
调优建议
- 利用率低于30%时考虑增加batch size
- 显存利用率持续90%以上需优化内存管理
- 配合分布式训练框架(如DeepSpeed、FSDP)的监控插件使用效果更佳
通过这套方案,可以实现对分布式训练GPU资源的精细化管控。

讨论