在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 cudatoolkit=11.8 -c pytorch
import torch
print(torch.__version__, torch.cuda.is_available()) # 檢查版本和CUDA是否可用
import pdb; pdb.set_trace()
,程序暫停后可使用n
(下一步)、s
(進入函數)、c
(繼續)等命令。logging
模塊記錄關鍵信息。print()
輸出變量值(簡單場景適用)。from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter('runs/experiment')
writer.add_scalar('Loss/train', loss.item(), epoch)
torch.autograd.set_detect_anomaly(True)
:檢測梯度異常。torch.autograd.profiler
:分析計算性能。unittest
或pytest
編寫測試用例,驗證模塊功能。torch.cuda.amp
減少內存占用,加速計算。torch.distributed
模塊時需檢查進程同步和通信。nvidia-smi
查看驅動狀態。torch.utils.checkpoint
進行梯度檢查點優化。參考資料: