Linux 上选择 PyTorch 版本的关键维度
快速判定与选择步骤
cat /etc/os-release、uname -m(常见为 x86_64/AMD64)。python -V 或 python3 -V。nvidia-smi(右上显示 Supported/Runtime CUDA 版本,表示驱动可支持的最高 CUDA 运行时)nvcc --version(显示已安装的 CUDA Toolkit 版本)--extra-index-url https://download.pytorch.org/whl/cu121 或 /cpu)。pytorch-cuda=xx 或 cudatoolkit=xx 指定 CUDA 版本。import torch; print(torch.__version__, torch.cuda.is_available())。版本匹配与命令示例
nvidia-smi 的 CUDA 是“驱动支持上限”;nvcc --version 是“Toolkit 实际版本”。| 场景 | 判定要点 | pip 安装示例 | conda 安装示例 |
|---|---|---|---|
| CPU-only | 无 NVIDIA GPU 或不启用 GPU | pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu |
conda install pytorch torchvision torchaudio cpuonly -c pytorch |
| GPU + CUDA 12.x | nvidia-smi 显示 Runtime ≥ 12.x |
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu121 |
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia |
| GPU + CUDA 11.8 | nvidia-smi 支持 11.8 且已装 11.8 Toolkit |
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu118 |
conda install pytorch torchvision torchaudio cudatoolkit=11.8 -c pytorch |
python - <<'PY' import torch print("PyTorch:", torch.__version__, "CUDA:", torch.version.cuda) print("CUDA available:", torch.cuda.is_available()) print("Device count:", torch.cuda.device_count()) PY常见问题与建议
torch==x.x.x 与 torchvision/torchaudio 的对应版本。