Torch CUDA 智能检测与安装
问题
- 后端 Issure 中有用户提到卡顿情况。
- GUI 首次启动通过
pip install autowsgr 安装依赖,easyocr 会从 PyPI 拉取 CPU-only 的 torch(~120MB)。用户若想用 GPU 加速,需手动修改 usersettings.yaml 的 ocr.gpu: true,且前提是已安装了 CUDA 版 torch——但 GUI 的内嵌 Python 环境不可能预装 CUDA torch。
目标
- 首次安装时检测用户硬件(是否有 NVIDIA GPU + CUDA 版本)
- 让用户选择安装 CPU 还是 CUDA 版 torch
- 安装后自动配置
ocr.gpu 开关
关键背景
PyTorch CUDA variant 选择
| Variant |
PyTorch 版本 |
GPU 架构覆盖 |
| cu126 |
2.6.0~2.12.0(最新) |
SM 5.0 ~ 12.0(GTX 900 ~ RTX 5000) |
| cu128 |
2.7.0~2.11.0 |
SM 5.0+ |
| cu124 |
2.4.0~2.7.1 |
SM 5.0+ |
结论:选择 cu126 单一 variant,因为 torch 2.12.0(2026-05-13 发布)仅提供 cu126,且向下兼容全部 SM 5.0+ GPU,包括 RTX 50 系列(Blackwell SM 12.0,需 PyTorch 2.11.0+)。
CUDA < 12.6 的用户回退 CPU torch,提示更新驱动。
改造方案:从全自动到用户选择
当前流程
StartupController.run()
└─ checkAndPrepareEnv()
├─ 检测缺 Python → installPortablePython()
└─ 检测缺依赖 → installDependencies() ← 自动拉 CPU torch,无选择
新流程
StartupController.run()
├─ 环境检查(仅检测,不安装)
├─ 若缺依赖 → 弹出选择面板
│ ├─ 自动运行 detectCuda() 检测硬件
│ ├─ 展示检测结果(GPU 型号 / CUDA 版本)
│ └─ 用户选择「CPU 模式」或「GPU 模式」
├─ 按选择安装
└─ 启动后端
选择面板示意
有 GPU + CUDA ≥ 12.6 时:
┌─────────────────────────────────────────┐
│ 🛠️ 安装后端依赖 │
│ │
│ ● NVIDIA GPU: RTX 3060 │
│ ● CUDA 12.6 → 推荐 GPU 模式 │
│ │
│ ○ CPU 模式 (~120MB) │
│ ● GPU 模式 (~1GB) │
│ │
│ [开始安装] [取消] │
└─────────────────────────────────────────┘
无 GPU 时直接显示 CPU 模式,只有一个按钮。
CUDA 检测脚本
import subprocess, re, sys, json
result = {'has_nvidia_gpu': False, 'cuda_version': None,
'cuda_major': None, 'should_use_gpu': False}
try:
out = subprocess.check_output(
['nvidia-smi', '--query-gpu=name', '--format=csv,noheader'],
timeout=10, stderr=subprocess.DEVNULL
).decode().strip()
if not out:
print(json.dumps(result)); sys.exit(0)
result['has_nvidia_gpu'] = True
except Exception:
print(json.dumps(result)); sys.exit(0)
try:
out = subprocess.check_output(
['nvidia-smi'], timeout=10, stderr=subprocess.DEVNULL
).decode()
m = re.search(r'CUDA Version:\s*(\d+)\.(\d+)', out)
if m:
result['cuda_version'] = f'{m.group(1)}.{m.group(2)}'
result['cuda_major'] = int(m.group(1))
result['should_use_gpu'] = result['cuda_major'] >= 12
except Exception:
pass
print(json.dumps(result))
GPU 安装命令(三步,防止 torch 被降级)
# 步骤 1: 预装 CUDA torch(覆盖 easyocr 后续拉取的 CPU torch)
pip install --target <site-packages> torch==2.12.0+cu126 torchvision==0.22.0+cu126 \
--index-url https://download.pytorch.org/whl/cu126
# 步骤 2: 安装 autowsgr(--no-deps 防止 torch 被降级回 CPU)
pip install --target <site-packages> --no-deps setuptools autowsgr
# 步骤 3: 安装 easyocr 非 torch 依赖
pip install --target <site-packages> --no-deps easyocr>=1.7.1
任一失败则回退 CPU 模式。
需修改的文件
P0 — 核心流程
P1 — 配置自动写入
P2 — 健壮性
| 文件 |
修改 |
ocr.py |
EasyOCREngine.__init__() 增加 torch.cuda.is_available() fallback,GPU 不可用时自动降级 CPU |
边界情况
| 场景 |
处理 |
| 无 NVIDIA GPU |
面板仅显示 CPU 选项 |
| 有 GPU 但驱动太旧(CUDA < 12.6) |
面板显示警告,仅 CPU 选项,提示更新驱动至 R535+ |
| CUDA ≥ 12.6 |
面板推荐 GPU 模式,提供 CPU/GPU 两个选项 |
| 用户取消安装 |
启动中断,日志提示通过设置页手动安装 |
| 网络失败 |
面板显示失败信息 + 重试按钮 |
| CUDA torch 安装失败 |
自动回退 CPU 模式并提示 |
Torch CUDA 智能检测与安装
问题
pip install autowsgr安装依赖,easyocr会从 PyPI 拉取 CPU-only 的 torch(~120MB)。用户若想用 GPU 加速,需手动修改usersettings.yaml的ocr.gpu: true,且前提是已安装了 CUDA 版 torch——但 GUI 的内嵌 Python 环境不可能预装 CUDA torch。目标
ocr.gpu开关关键背景
PyTorch CUDA variant 选择
结论:选择 cu126 单一 variant,因为 torch 2.12.0(2026-05-13 发布)仅提供 cu126,且向下兼容全部 SM 5.0+ GPU,包括 RTX 50 系列(Blackwell SM 12.0,需 PyTorch 2.11.0+)。
CUDA < 12.6 的用户回退 CPU torch,提示更新驱动。
改造方案:从全自动到用户选择
当前流程
新流程
选择面板示意
有 GPU + CUDA ≥ 12.6 时:
无 GPU 时直接显示 CPU 模式,只有一个按钮。
CUDA 检测脚本
GPU 安装命令(三步,防止 torch 被降级)
任一失败则回退 CPU 模式。
需修改的文件
P0 — 核心流程
installer.tsdetectCuda()、installWithCudaTorch()、installDependenciesWithGpu()main.tsdetect-cuda和install-deps-with-gpu两个 IPC handlerelectronBridge.tsdetectCuda()和installDepsWithGpu()接口preload.tsStartupController.tsrun()流程改为:检查 → 缺依赖时弹选择面板 → 按选择安装envAndUpdates.tscheckAndPrepareEnv()拆分为「仅检查」和「带选择的安装」两个阶段index.htmlP1 — 配置自动写入
installer.tsocr.gpu到usersettings.yamlP2 — 健壮性
ocr.pyEasyOCREngine.__init__()增加torch.cuda.is_available()fallback,GPU 不可用时自动降级 CPU边界情况