Windows 装 torch cu128 + pip 卡死排错
一、装 cu128(RTX 50 系 / Blackwell sm_120 必须)
RTX 50 系是 sm_120,cu126/cu124 只编到 sm_90,装错会报
no kernel image is available for execution on the device。只要 cu128。
先试国内镜像(实测可用):
<PY> -m pip install torch torchvision ^
--index-url https://mirrors.aliyun.com/pytorch-wheels/cu128
- ✅ 阿里云
https://mirrors.aliyun.com/pytorch-wheels/cu128/→ HTTP 200,
文件可直接取(Server: Tengine) - ❌ 清华
mirrors.tuna.tsinghua.edu.cn/pytorch-wheels/cu128/→ 404,别走这条路 - 官方源
https://download.pytorch.org/whl/cu128国内直连可能超时,但 pip 有
断点续传(日志出现Attempting to resume incomplete download (x/y MB, attempt N)),
2.7 GB 也能自己跑完
版本对应(Windows + cp313 实测):torch 2.11.0+cu128 + torchvision 0.26.0+cu128。
注意与 CPU 版的 2.13.0+cpu 版本号不同,属正常,不是装错。
大文件下载中断的处置:
1. pip 只在进程启动时读取网络配置 → 中途改了网络环境必须重启 pip 进程
2. 2.7 GB 单文件长连接容易中断,反而不如「直连 + 断点续传」稳
二、验证(铁律:不能只看 cuda.is_available())
sm_120 上 torch.cuda.is_available() 会假 True,必须真跑一次算子:
<PY> -c "import torch;print(torch.__version__,torch.cuda.is_available(),torch.cuda.get_device_capability(0));a=torch.rand(2048,2048,device='cuda');print('GPU OK' if float((a@a).sum())!=0 else 'FAIL')"
- 期望:
capability == (12, 0)、torch.version.cuda == '12.8'、matmul 出非零值 - 再加一个 fp16 Conv2d(SAM/CNN 实际负载):
torch.nn.Conv2d(3,16,3,padding=1).cuda().half()前向不报错即通过 - 驱动需 ≥ 572.x
三、pip 卡死诊断与处置(Windows 专属坑)
判定:是"慢"还是"真卡死"
看进程 IO 计数,不要看日志(pip 输出在非 tty 下被缓冲,日志会假装停在原地):
Get-CimInstance Win32_Process -Filter "ProcessId=<pid>" |
Select ReadOperationCount,WriteOperationCount,OtherOperationCount
间隔 10~15 秒采两次。计数零变化 = 挂死,不是慢。
真因与证据:~xxx 残骸
pip 删不掉的目录会被改名成 ~<name> 后挂起。去 site-packages 里找:
dir /b "<env>\Lib\site-packages\~*"
看到 ~orch / ~orchgen / ~unctorch / ~orch-*.dist-info 就是卸载被文件占用卡死的直接证据。
Python 不会 import 它们,但会占空间,并让 pip 后续报警
Ignoring invalid distribution ~orch。
处置
- 先看命令行再动手(机器上可能有别人的任务在跑,误杀代价大):
powershell Get-CimInstance Win32_Process -Filter "Name='python.exe'" | ForEach-Object { "$($_.ProcessId) $($_.CommandLine)" } - 按 PID 精准终止(Git Bash 下不要用
//F,改用-F或MSYS_NO_PATHCONV=1):
bash MSYS_NO_PATHCONV=1 taskkill /F /T /PID <子进程PID>
⚠️ 终止类命令不要重定向 stderr,报错信息才是最该看的。 - 意外收益:若原命令是「先 uninstall 再 install」的顺序脚本,
杀掉卡死的 uninstall 后会自动进入 install,不必重跑。 - 清残骸(前台会超时,放后台):
python import glob, os, shutil for t in glob.glob(os.path.join(sp, '~*')): shutil.rmtree(t)
实测~orch单个要删 627 秒(10.5 分钟)——因为 Windows Defender 逐个扫描几千个文件。
所以"卡死"往往只是在慢慢删。判断依据仍然是 IO 计数是否变化。
四、踩坑速查
| 症状 | 真因 | 对策 |
|---|---|---|
no kernel image is available |
装的不是 cu128 | 用阿里云 cu128 源重装 |
cuda.is_available() True 但跑算子报错 |
假 True(sm_120) | 必须真跑 matmul 验证 |
| pip 日志停住不动 | 输出缓冲,不是卡死 | 看 IO 计数 |
| 卸载卡 10~20 分钟 | 与 ~xxx 残骸较劲(Defender 扫文件) |
等,或按 PID 杀后放后台清 |
pip 报警 invalid distribution ~orch |
上次卸载留下残骸 | 手删 ~* |