以下是Ubuntu系统下调试PyTorch的实用技巧,涵盖环境配置、调试工具及优化方法:
基础环境搭建
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
conda create -n pytorch_env python=3.8
conda activate pytorch_env
conda install pytorch torchvision torchaudio pytorch-cuda11.7 -c pytorch -c nvidia
import torch
print(torch.__version__, torch.cuda.is_available()) # 检查版本及GPU可用性
GPU环境检查
nvcc --version # 查看CUDA版本
nvidia-smi # 查看GPU状态及驱动信息
交互式调试
import pdb; pdb.set_trace() # pdb断点
# 或使用ipdb(需安装):import ipdb; ipdb.set_trace()
执行后可通过n(下一步)、s(进入函数)、c(继续)等命令控制流程。IDE集成调试
Debug按钮启动调试会话。launch.json后,在断点处点击Start Debugging。日志与异常检测
import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug(f"Variable x: {x}")
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter('runs/experiment')
writer.add_scalar('Loss/train', loss.item(), epoch)
# 终端运行:tensorboard --logdir=runs
性能分析与优化
torch.autograd.set_detect_anomaly(True) # 检测梯度计算错误
from torch.autograd import profiler
with profiler.profile(record_shapes=True) as prof:
output = model(input)
print(prof.key_averages().table(sort_by="cuda_time_total"))
单元测试与代码审查
unittest或pytest编写测试用例,验证模型各模块功能。pylint或flake8检查代码规范,提前发现潜在问题。batch_size或使用梯度累积。torch.cuda.amp)减少显存占用。torch.nn.DataParallel或DistributedDataParallel时,确保数据正确分配到各GPU。通过以上工具和方法,可高效定位和解决PyTorch代码中的问题,提升开发效率。