先分清四个容易混淆的版本

GPU环境不是一个“CUDA版本”决定的。宿主机NVIDIA驱动负责内核与设备;CUDA Toolkit提供`nvcc`和开发头文件;PyTorch安装包通常自带CUDA用户态运行库;cuDNN、NCCL等库又有各自版本。`nvidia-smi`右上角的CUDA数字表示驱动最高兼容能力,不等于`nvcc --version`,也不等于`torch.version.cuda`。

保存系统基线

cat /etc/os-release
uname -r
nvidia-smi
nvidia-smi -L
command -v nvcc && nvcc --version || echo 'nvcc未安装'

如果只运行官方PyTorch wheel或容器,宿主机通常不需要完整Toolkit;只有编译自定义CUDA扩展时才需要匹配的编译工具链。宿主机`nvidia-smi`失败时不要继续重装Python包,应先检查驱动模块、DKMS和Xid日志。

使用独立环境安装

python3 -m venv /opt/venvs/torch-check
source /opt/venvs/torch-check/bin/activate
python -m pip install -U pip
# 从PyTorch官方安装选择器复制与当前系统相符的命令,不要照抄旧博客
pip install torch torchvision torchaudio
pip freeze > /opt/venvs/torch-check.freeze.txt

官方选择器会根据Linux/Windows、pip/conda及CUDA平台生成命令。安装后先打印完整信息:

python - <<'PY'
import torch
print('torch:', torch.__version__)
print('built CUDA:', torch.version.cuda)
print('cuDNN:', torch.backends.cudnn.version())
print('CUDA available:', torch.cuda.is_available())
print('GPU count:', torch.cuda.device_count())
for i in range(torch.cuda.device_count()):
    p=torch.cuda.get_device_properties(i)
    print(i,p.name,p.total_memory,p.major,p.minor)
PY

不要停在cuda.is_available

设备可见只说明初始化成功。还要真正执行计算、同步并检查结果:

python - <<'PY'
import torch,time
assert torch.cuda.is_available()
device='cuda:0'
a=torch.randn((4096,4096),device=device,dtype=torch.float16)
b=torch.randn_like(a)
for _ in range(3): c=a@b
torch.cuda.synchronize();t=time.time()
for _ in range(20): c=a@b
torch.cuda.synchronize()
print('20 GEMM seconds:',time.time()-t)
print('finite:',bool(torch.isfinite(c).all()),'memory MB:',torch.cuda.max_memory_allocated()/1024**2)
PY

`torch.cuda.synchronize()`不可省略,否则测到的是异步提交时间。这里用于健康检查,不应作为GPU型号的正式性能排名。

验证BF16、FP16与自动混合精度

python - <<'PY'
import torch
print('bf16 supported:',torch.cuda.is_bf16_supported())
for dt in [torch.float32,torch.float16,torch.bfloat16]:
 try:
  x=torch.randn(2048,2048,device='cuda',dtype=dt); y=x@x
  torch.cuda.synchronize();print(dt,'ok',float(y.float().mean()))
 except Exception as e: print(dt,'failed',repr(e))
PY

硬件支持某精度不代表目标模型全部算子都有高效实现。正式训练还要观察损失、梯度溢出和数值稳定性;推理则需对比固定任务质量。

多卡逐卡与点对点检查

每张卡单独创建张量,防止0号卡正常掩盖其他卡故障:

python - <<'PY'
import torch
for i in range(torch.cuda.device_count()):
 with torch.cuda.device(i):
  x=torch.ones(1024,1024,device=f'cuda:{i}');print(i,float((x@x).mean()))
print('peer matrix')
for i in range(torch.cuda.device_count()):
 print([torch.cuda.can_device_access_peer(i,j) for j in range(torch.cuda.device_count())])
PY

再用`nvidia-smi topo -m`读取PCIe/NVLink路径。PyTorch能看到两张卡不代表NCCL正常,多进程训练前还需单独运行nccl-tests。

高频故障

  • `cuda.is_available=False`:确认安装的是CUDA构建而非CPU包,并查看驱动是否正常。
  • `no kernel image`:GPU计算能力不在当前PyTorch构建支持范围。
  • 编译扩展报CUDA版本不匹配:区分wheel自带运行时与本机Toolkit编译器。
  • 首次正常、重启后失败:检查驱动模块、容器设备映射和服务启动顺序。
  • 随机非法内存访问:用`CUDA_LAUNCH_BLOCKING=1`缩小报错位置,但不要长期保留该变量。

最终验收资料应包含OS、内核、驱动、PyTorch、CUDA构建、cuDNN、GPU UUID、安装命令和测试输出。只有这些信息齐全,环境才能被他人复现。

来源、翻译与版权说明

来源:网昱原创。第三方内容版权归原作者或发布机构所有;本站仅在许可证或明确授权允许时提供本地原文。

原文语言
ZH-CN
原文更新时间
未提供
许可证
未登记