python查看显卡是否支持cuda、torch的cuda是否可用
文章目录
调用cuda之前都会先验证下cuda是否可用。
如果不可用,可以从如下几个方面排查:
1、是否装了nvidia显卡,驱动是否正常等。
2、torch版本必须是cuda版本,如果不指定,默认装的是cpu版本。(重)
1、是否装了nvidia显卡,驱动是否正常、nvidia-smi命令
nvidia-smi(nvidia system management interface)(英文达系统管理接口)。
查看显卡版本命令:
nvidia-smi
输出结果:
Wed Mar 25 17:08:11 2026
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 582.28 Driver Version: 582.28 CUDA Version: 13.0 |
+-----------------------------------------+------------------------+----------------------+
| GPU Name Driver-Model | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 NVIDIA GeForce GTX 850M WDDM | 00000000:01:00.0 Off | N/A |
| N/A 0C P8 N/A / 26W | 56MiB / 2048MiB | 0% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------+
主要功能就是这个,如果想看其他参数,可以通过:
nvidia -h
2、torch版本必须是cuda版本,如果不指定,默认装的是cpu版本。(重)
大多数是因为这个问题。
安装命令:
正确命令:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
错误命令,也不是说这个命令是错的,而是对于想用cuda能力的场景来说,这样安装是错的:
pip install torch
pip命令查看torch版本
命令:
pip show torch
输出结果:
这种是cpu版本:
Version: 2.11.0
这种是cuda版本:
Version: 2.7.1+cu118
校验cuda代码
代码:
import torch
print(torch.__version__)
print(torch.version.cuda) # 如果是 None,说明装的是 CPU 版!
print(torch.cuda.is_available())
指定固定显卡
例如有两张卡,集显(核显)和独显,用下标0可能拿不到cuda那张卡。
1、加这行代码:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
2、然后替换device_map的值为device,如:
base_model = AutoModelForCausalLM.from_pretrained(
MODEL_NAME,
device_map=device,
torch_dtype=torch.float16 # 建议加上这个,节省显存并加快速度
)
注:这种写法主要在单机调试时用,因为它不支持多显卡。
生产还是建议用auto。
上一秒cuda还可用,突然就不可用了
1、可能被覆盖了,先判断下版本,如果不对重新装下:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 --force-reinstall
2、有可能是跑某个任务显卡僵死了,重启后居然好了。。。
3、如果不想重启,可以试下ctrl+shift+win+b,重置显卡驱动程序。
为什么明明有显卡,打游戏等也正常,但是cuda是false
按之前步骤排查,大多数是torch版本问题。
有显卡一定支持cuda吗?
不一定,目前主要是nvidia的显卡才可能支持cuda,而且一些老版本的nvidia显卡是不支持cuda的。
也就是:
非nvidia显卡,基本不支持。
nvidia显卡-老版本,如GT系列显卡(如GT210)不支持。
nvidia显卡-高版本,大多数支持。
cuda可用的情况下,查看显卡型号和内存
代码:
not_available_tip = """
cuda 不可用。请检查:
1. 是否安装了 NVIDIA 驱动
2. 是否安装了 cuda 版本的 pytorch
"""
if torch.cuda.is_available():
print(f"cuda 可用!显卡型号: {torch.cuda.get_device_name(0)}")
print(f"显存总量: {torch.cuda.get_device_properties(0).total_memory / 1024**2:.2f} MB")
else:
print(not_available_tip)
检测是否支持gpu
代码:
import torch
# 检查 CUDA 是否可用
if torch.cuda.is_available():
print(f"GPU 可用!显卡型号: {torch.cuda.get_device_name(0)}")
print(f"显存总量: {torch.cuda.get_device_properties(0).total_memory / 1024**2:.2f} MB")
else:
print("GPU 不可用,将回退到 CPU。请检查:\n1. 是否安装了 NVIDIA 驱动\n2. 是否安装了 cuda 版本的 pytorch (pip install torch --index-url https://download.pytorch.org/whl/cu118)")
gpu和cpu切换使用
1、检查 torch.cuda.is_available()
2、定义 device = torch.device(“cuda” if … else “cpu”)
3、移动模型 model.to(device)
4、移动数据 data = data.to(device) (在每一个 training step 循环里都要做)
5、计算 (自动在指定设备上运行)
更多推荐



所有评论(0)