PyTorch GPU 版安装记录(RTX 5060 + Python 3.14)
整理日期:2026-09-04
适用环境:Windows 11 / RTX 5060 Laptop GPU (sm_120, Blackwell) /
E:\PyNumpyLearning\python.exe(Python 3.14.7)
一、问题现象
环境中已安装 torch,但 torch.cuda.is_available() 返回 False,无法使用 GPU:
python
import torch
torch.__version__ # '2.14.0+cpu' ← 注意 +cpu 后缀
torch.cuda.is_available() # False
torch.version.cuda # None
二、原因分析
1. 根本原因:Windows 上 pip install torch 默认装的是 CPU 版
在 Windows 平台,PyPI 默认源提供的 torch 轮子不含 CUDA (版本号带 +cpu 后缀)。GPU 版必须指定 PyTorch 官方的 CUDA 索引安装,版本号形如 2.14.0+cu130。
2. 硬件本身没有问题
plain
NVIDIA-SMI 592.19 Driver Version: 592.19 CUDA Version: 13.1
GPU 0: NVIDIA GeForce RTX 5060 Laptop GPU, 8151MiB
驱动正常,支持到 CUDA 13.1。注意 :RTX 5060 是 Blackwell 架构(算力 sm_120),必须用 cu128 或更高的构建,旧的 cu121/cu126 轮子即使装上也无法识别这块显卡。
3. 本机额外限制:官方源极慢
直连 download.pytorch.org 实测仅约 0.1 MB/s(torch cu130 轮子约 1.9 GB,需要 8 小时以上),因此改用阿里云镜像。
三、环境信息
| 环境 | Python | 原 torch 状态 |
|---|---|---|
E:\PyNumpyLearning\python.exe(PATH 第一位,本文目标) |
3.14.7 | 2.14.0+cpu(已替换为 GPU 版) |
D:\Python313\python.exe |
3.13.13 | 2.13.0+cpu(未处理) |
安装前先用 where python、python -c "import sys; print(sys.executable)" 确认操作的是哪个解释器。
四、安装步骤
第 1 步:卸载 CPU 版
bash
"E:\PyNumpyLearning\python.exe" -m pip uninstall -y torch torchvision torchaudio
第 2 步(推荐路径):从阿里云镜像安装
阿里云同步了 PyTorch 官方 CUDA 轮子,直接用它作为索引:
bash
"E:\PyNumpyLearning\python.exe" -m pip install torch torchvision torchaudio \
--index-url https://mirrors.aliyun.com/pytorch-wheels/cu130/
镜像目录:https://mirrors.aliyun.com/pytorch-wheels/cu130/(另有 cu121/cu124/cu126/cu128/cu129/cu132 等目录)。
第 2 步(备选路径):镜像单线程也慢时,curl 分段并行下载再本地安装
本机实测镜像单线程约 0.8 MB/s,16 段并行可达 14+ MB/s(1.9 GB 约 2 分钟)。安装好的三个轮子文件名:
plain
torch-2.14.0+cu130-cp314-cp314-win_amd64.whl (1,998,865,090 字节)
torchvision-0.29.0+cu130-cp314-cp314-win_amd64.whl
torchaudio-2.11.0+cu130-cp314-cp314-win_amd64.whl
下载脚本(Git Bash 运行,核心是 curl -r start-end 分段 + 后台并发 + cat 拼接 + 大小校验):
bash
#!/bin/bash
set -u
DL="$LOCALAPPDATA/Temp/torch_gpu_dl"; mkdir -p "$DL"; cd "$DL"
BASE="https://mirrors.aliyun.com/pytorch-wheels/cu130"
TORCH="torch-2.14.0+cu130-cp314-cp314-win_amd64.whl"
TORCH_SIZE=1998865090 # 先 curl -sI 查 Content-Length
N=16
CHUNK=$(( (TORCH_SIZE + N - 1) / N ))
dl_part() { # 下载第 i 段,已完成则跳过(可断点续传)
local i10=$((10#$1)) start=$((i10*CHUNK)) end=$((start+CHUNK-1))
[ $end -ge $TORCH_SIZE ] && end=$((TORCH_SIZE-1))
[ -f "part_$1" ] && [ "$(stat -c%s "part_$1")" -eq $((end-start+1)) ] && return 0
curl -s --retry 8 -r "${start}-${end}" -o "part_$1" "${BASE}/${TORCH//+/%2B}"
}
for i in $(seq -w 0 $((N-1))); do dl_part "$i" & done; wait
cat part_* > "$TORCH"
[ "$(stat -c%s "$TORCH")" -eq "$TORCH_SIZE" ] || { echo "SIZE MISMATCH"; exit 2; }
然后本地安装(URL 中的 + 需编码为 %2B):
bash
"E:\PyNumpyLearning\python.exe" -m pip install \
"./torch-2.14.0+cu130-cp314-cp314-win_amd64.whl" \
"./torchvision-0.29.0+cu130-cp314-cp314-win_amd64.whl" \
"./torchaudio-2.11.0+cu130-cp314-cp314-win_amd64.whl"
五、验证
python
import torch, time
print(torch.__version__) # 2.14.0+cu130
print(torch.cuda.is_available()) # True
print(torch.version.cuda) # 13.0
print(torch.cuda.get_device_name(0)) # NVIDIA GeForce RTX 5060 Laptop GPU
print(torch.cuda.get_device_capability(0)) # (12, 0) → sm_120
# 真实 GPU 运算测试
a = torch.randn(2048, 2048, device='cuda')
b = torch.randn(2048, 2048, device='cuda')
c = a @ b # 结果张量位于 cuda:0
本次实测输出:GPU matmul 50x(2048^3): 989.9ms, 结果张量在 cuda:0。
六、注意事项
- 版本号后缀是判断依据 :
+cpu= CPU 版,+cu130= CUDA 13.0 构建。装完先看后缀。 - RTX 50 系列(Blackwell, sm_120)最低要求 cu128 构建,cu126 及以下装上也无法用 GPU。
- Windows 的 PyPI 默认源没有 CUDA 版 torch ;升级/重装时务必带
--index-url(官方源或阿里云镜像),不要裸跑pip install torch。 - torchvision / torchaudio 的版本要与 torch 配对(本文组合:2.14.0 / 0.29.0 / 2.11.0),混装可能报 ABI 不兼容。
- CUDA 运行时随轮子自带(本例 CUDA 13.0),无需单独安装 CUDA Toolkit,只需 NVIDIA 驱动(592.19,支持 CUDA ≤13.1)满足要求。
D:\Python313环境中的 torch 2.13.0+cpu 尚未替换,如需使用参照同一路径执行即可。