为什么即使在使用 cuda 安装 pytorch 后,`torch.cuda.is_available()` 也会返回 False? [英] Why `torch.cuda.is_available()` returns False even after installing pytorch with cuda?

查看:48
本文介绍了为什么即使在使用 cuda 安装 pytorch 后,`torch.cuda.is_available()` 也会返回 False?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在装有 NVidia GeForce 820M 的 Windows 10 PC 上我成功安装了 CUDA 9.2 和 cudnn 7.1,然后按照 pytorch.org 上的说明安装 PyTorch.

特别是我使用了命令

pip install torch==1.4.0+cu92 torchvision==0.5.0+cu92 -f https://download.pytorch.org/whl/torch_stable.html

因为我使用 pip 而不是 Anaconda.

但我得到以下内容

<预><代码>>>>进口火炬>>>torch.cuda.is_available()错误的

为什么会这样?

解决方案

您的显卡不支持 CUDA 9.0.

因为我看到了很多涉及此类问题的问题,所以我正在写一个关于如何检查您的系统是否与 CUDA 兼容的广泛答案,特别是针对使用具有 CUDA 支持的 PyTorch.本答案的最后一部分描述了解决问题的各种依赖于环境的选项.


将 PyTorch 与 CUDA 结合使用的系统要求如下:

  • 您的显卡必须支持所需版本的 CUDA
  • 您的显卡驱动程序必须支持所需的 CUDA 版本
  • 构建 PyTorch 二进制文件必须支持显卡的计算能力

注意:如果您安装预构建的二进制文件(使用 pip 或 conda),那么您不需要在安装之前在您的系统上安装 CUDA 工具包或运行时带有 CUDA 支持的 PyTorch.这是因为 PyTorch 除非从源代码编译,否则总是随 CUDA 库的副本一起交付.


1.如何检查您的 GPU/显卡是否支持特定的 CUDA 版本

首先,确定您的显卡型号.

在继续之前,请确保您拥有 NVIDIA 显卡.AMD 和 Intel 显卡不支持 CUDA.

NVIDIA 在单一位置提供 CUDA 兼容性信息方面做得并不好.最好的资源可能是CUDA 维基百科页面上的这一部分.确定支持哪些版本的 CUDA

  1. 在大表中找到您的显卡型号并记下计算能力版本.例如,GeForce 820M 计算能力为 2.1.
  2. 在表格前面的项目符号列表中,检查您的显卡的计算能力是否支持所需的 CUDA 版本.例如,计算兼容性 2.1 不支持 CUDA 9.2.

如果您的卡不支持所需的 CUDA 版本,请参阅本答案第 4 部分中的选项.

注意:计算能力是指您的显卡支持的计算功能.较新版本的 CUDA 库依赖于较新的硬件功能,这就是我们需要确定计算能力以确定支持的 CUDA 版本的原因.


2.如何检查您的 GPU/图形驱动程序是否支持特定版本的 CUDA

图形驱动程序是允许您的操作系统与您的图形卡进行通信的软件.由于 CUDA 依赖于与显卡的低级通信,因此您需要有最新的驱动程序才能使用最新版本的 CUDA.

首先,确保您的系统上安装了 NVIDIA 图形驱动程序.您可以从 NVIDIA 网站 获取适用于您系统的最新驱动程序.

如果您安装了最新的驱动程序版本,那么您的图形驱动程序可能支持与您的图形卡兼容的每个 CUDA 版本(请参阅第 1 部分).要进行验证,您可以查看 CUDA 发行说明.在极少数情况下我听说最新推荐的显卡驱动程序不支持最新的 CUDA 版本.您应该能够通过为所需 CUDA 版本安装 CUDA 工具包并选择安装兼容驱动程序的选项来解决此问题,尽管这通常不是必需的.

如果您不能,或者不想升级显卡驱动,那么您可以检查您当前的驱动是否支持特定的 CUDA 版本,如下所示:

在 Windows 上

  1. 确定您当前的图形驱动程序版本(来源 https://www.nvidia.com/en-gb/drivers/drivers-faq/)

<块引用>

右键单击您的桌面并选择 NVIDIA 控制面板.来自NVIDIA 控制面板菜单,选择帮助 >系统信息.这驱动程序版本列在详细信息"窗口的顶部.更多高级用户,您还可以从Windows 设备管理器.右键单击您的图形设备显示适配器,然后选择属性.选择驱动程序选项卡和阅读驱动程序版本.最后 5 位数字是 NVIDIA 驱动程序版本号.

  1. 访问 CUDA 发行说明并向下滚动到表 2.使用此表来验证您的图形驱动程序是否足够新以支持所需的 CUDA 版本.

在 Linux/OS X 上

在终端窗口中运行以下命令

nvidia-smi

这应该会导致类似以下内容

2020 年 4 月 4 日星期六 15:31:57+------------------------------------------------------------------------------+|NVIDIA-SMI 435.21 驱动程序版本:435.21 CUDA 版本:10.1 ||-----------------------------------------+----------------------+----------------------+|GPU名称持久性-M|Bus-Id Disp.A |挥发性未校正.ECC ||风扇温度性能 Pwr:Usage/Cap|内存使用 |GPU-Util 计算 M.|==========================================================+========================||0 GeForce RTX 206...关闭 |00000000:01:00.0 开 |不适用 ||0% 35C P8 16W/175W |502MiB/7974MiB |1% 默认 |+-------------------------------+----------------------+----------------------++------------------------------------------------------------------------------+|进程:GPU 内存 ||GPU PID 类型进程名称用法||================================================================================||0 1138 G/usr/lib/xorg/Xorg 300MiB ||0 2550 G/usr/bin/compiz 189MiB ||0 5735 G/usr/lib/firefox/firefox 5MiB ||0 7073 G/usr/lib/firefox/firefox 5MiB |+------------------------------------------------------------------------------+

驱动程序版本:###.## 是您的图形驱动程序版本.在上面的示例中,驱动程序版本是 435.21.

CUDA 版本:##.# 是您的图形驱动程序支持的最新版本的 CUDA.在上面的示例中,图形驱动程序支持 CUDA 10.1 以及 10.1 之前的所有兼容 CUDA 版本.

注意:此表中显示的CUDA 版本表示您的系统上实际安装了CUDA 工具包或运行时.这仅表示您的图形驱动程序兼容的最新版本的 CUDA.

为确保您的驱动程序支持所需的 CUDA 版本,您可以访问 CUDA 发行说明页面.


3.如何检查特定版本的 PyTorch 是否与您的 GPU/显卡计算能力兼容

即使您的显卡支持所需的 CUDA 版本,预编译的 PyTorch 二进制文件也可能未编译为支持您的计算能力.例如,在 PyTorch 0.3.1 支持计算能力 <= 5.0 被删除.

首先,确认您的显卡和驱动程序都支持所需的 CUDA 版本(请参阅上面的第 1 和第 2 节),本节中的信息假设是这种情况.

检查 PyTorch 是否支持您的计算能力的最简单方法是安装具有 CUDA 支持的所需版本的 PyTorch,并从 Python 解释器运行以下代码

<预><代码>>>>进口火炬>>>火炬.zeros(1).cuda()

如果您收到一条错误消息

发现 GPU0 XXXXX,它具有 cuda 功能 #.#.PyTorch 不再支持这个 GPU,因为它太旧了.

那么这意味着 PyTorch 没有在编译时支持您的计算能力.如果这运行没有问题,那么你应该很高兴.

更新如果您在具有较新 GPU 的系统上安装旧版本的 PyTorch,则旧的 PyTorch 版本可能未编译为支持您的计算能力.假设您的 GPU 支持 PyTorch 使用的 CUDA 版本,那么您应该能够使用所需的 CUDA 版本从源代码重建 PyTorch,或者升级到支持更新计算功能的更新版本的 PyTorch.


4.结论

如果您的显卡和驱动程序支持所需版本的 CUDA(第 1 和第 2 部分),但 PyTorch 二进制文件不支持您的计算能力(第 3 部分),那么您的选择是

  • 从源代码编译 PyTorch,支持您的计算能力(请参阅此处a>)
  • 在不支持 CUDA 的情况下安装 PyTorch(仅限 CPU)
  • 安装支持您的计算能力的旧版 PyTorch 二进制文件(不推荐,因为此时 PyTorch 0.3.1 已经非常过时).预先构建的二进制文件从未支持 3.X 之前的 AFAIK 计算能力
  • 升级您的显卡

如果您的显卡不支持所需的 CUDA 版本(第 1 部分),那么您的选择是

  • 在不支持 CUDA 的情况下安装 PyTorch(仅限 CPU)
  • 安装支持图形卡支持的 CUDA 版本的旧版 PyTorch(如果二进制文件不支持您的计算能力,仍可能需要从源代码编译)
  • 升级您的显卡

On a Windows 10 PC with an NVidia GeForce 820M I installed CUDA 9.2 and cudnn 7.1 successfully, and then installed PyTorch using the instructions at pytorch.org.

Specifically I used the command

pip install torch==1.4.0+cu92 torchvision==0.5.0+cu92 -f https://download.pytorch.org/whl/torch_stable.html

because i use pip and not Anaconda.

Yet I get the following

>>> import torch
>>> torch.cuda.is_available()
False

Why is this happening?

解决方案

Your graphics card does not support CUDA 9.0.

Since I've seen a lot of questions that refer to issues like this I'm writing a broad answer on how to check if your system is compatible with CUDA, specifically targeted at using PyTorch with CUDA support. Various circumstance-dependent options for resolving issues are described in the last section of this answer.


The system requirements to use PyTorch with CUDA are as follows:

  • Your graphics card must support the required version of CUDA
  • Your graphics card driver must support the required version of CUDA
  • The PyTorch binaries must be built with support for the compute capability of your graphics card

Note: If you install pre-built binaries (using either pip or conda) then you do not need to install the CUDA toolkit or runtime on your system before installing PyTorch with CUDA support. This is because PyTorch, unless compiled from source, is always delivered with a copy of the CUDA library.


1. How to check if your GPU/graphics card supports a particular CUDA version

First, identify the model of your graphics card.

Before moving forward ensure that you've got an NVIDIA graphics card. AMD and Intel graphics cards do not support CUDA.

NVIDIA doesn't do a great job of providing CUDA compatibility information in a single location. The best resource is probably this section on the CUDA Wikipedia page. To determine which versions of CUDA are supported

  1. Locate your graphics card model in the big table and take note of the compute capability version. For example, the GeForce 820M compute capability is 2.1.
  2. In the bullet list preceding the table check to see if the required CUDA version is supported by the compute capability of your graphics card. For example, CUDA 9.2 is not supported for compute compatibility 2.1.

If your card doesn't support the required CUDA version then see the options in section 4 of this answer.

Note: Compute capability refers to the computational features supported by your graphics card. Newer versions of the CUDA library rely on newer hardware features, which is why we need to determine the compute capability in order to determine the supported versions of CUDA.


2. How to check if your GPU/graphics driver supports a particular version CUDA

The graphics driver is the software that allows your operating system to communicate with your graphics card. Since CUDA relies on low-level communication with the graphics card you need to have an up-to-date driver in order use the latest versions of CUDA.

First, make sure you have an NVIDIA graphics driver installed on your system. You can acquire the newest driver for your system from NVIDIA's website.

If you've installed the latest driver version then your graphics driver probably supports every CUDA version compatible with your graphics card (see section 1). To verify, you can check Table 2 in the CUDA release notes. In rare cases I've heard of the latest recommended graphics drivers not supporting the latest CUDA releases. You should be able to get around this by installing the CUDA toolkit for the required CUDA version and selecting the option to install compatible drivers, though this usually isn't required.

If you can't, or don't want to upgrade the graphics driver then you can check to see if your current driver supports the specific CUDA version as follows:

On Windows

  1. Determine your current graphics driver version (Source https://www.nvidia.com/en-gb/drivers/drivers-faq/)

Right-click on your desktop and select NVIDIA Control Panel. From the NVIDIA Control Panel menu, select Help > System Information. The driver version is listed at the top of the Details window. For more advanced users, you can also get the driver version number from the Windows Device Manager. Right-click on your graphics device under display adapters and then select Properties. Select the Driver tab and read the Driver version. The last 5 digits are the NVIDIA driver version number.

  1. Visit the CUDA release notes and scroll down to Table 2. Use this table to verify your graphics driver is new enough to support the required version of CUDA.

On Linux/OS X

Run the following command in a terminal window

nvidia-smi

This should result in something like the following

Sat Apr  4 15:31:57 2020       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 435.21       Driver Version: 435.21       CUDA Version: 10.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce RTX 206...  Off  | 00000000:01:00.0  On |                  N/A |
|  0%   35C    P8    16W / 175W |    502MiB /  7974MiB |      1%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1138      G   /usr/lib/xorg/Xorg                           300MiB |
|    0      2550      G   /usr/bin/compiz                              189MiB |
|    0      5735      G   /usr/lib/firefox/firefox                       5MiB |
|    0      7073      G   /usr/lib/firefox/firefox                       5MiB |
+-----------------------------------------------------------------------------+

Driver Version: ###.## is your graphic driver version. In the example above the driver version is 435.21.

CUDA Version: ##.# is the latest version of CUDA supported by your graphics driver. In the example above the graphics driver supports CUDA 10.1 as well as all compatible CUDA versions before 10.1.

Note: The CUDA Version displayed in this table does not indicate that the CUDA toolkit or runtime are actually installed on your system. This just indicates the latest version of CUDA your graphics driver is compatible with.

To be extra sure that your driver supports the desired CUDA version you can visit Table 2 on the CUDA release notes page.


3. How to check if a particular version of PyTorch is compatible with your GPU/graphics card compute capability

Even if your graphics card supports the required version of CUDA then it's possible that the pre-compiled PyTorch binaries were not compiled with support for your compute capability. For example, in PyTorch 0.3.1 support for compute capability <= 5.0 was dropped.

First, verify that your graphics card and driver both support the required CUDA version (see Sections 1 and 2 above), the information in this section assumes that this is the case.

The easiest way to check if PyTorch supports your compute capability is to install the desired version of PyTorch with CUDA support and run the following from a python interpreter

>>> import torch
>>> torch.zeros(1).cuda()

If you get an error message that reads

Found GPU0 XXXXX which is of cuda capability #.#.
PyTorch no longer supports this GPU because it is too old.

then that means PyTorch was not compiled with support for your compute capability. If this runs without issue then you should be good to go.

Update If you're installing an old version of PyTorch on a system with a newer GPU then it's possible that the old PyTorch release wasn't compiled with support for your compute capability. Assuming your GPU supports the version of CUDA used by PyTorch, then you should be able to rebuild PyTorch from source with the desired CUDA version or upgrade to a more recent version of PyTorch that was compiled with support for the newer compute capabilities.


4. Conclusion

If your graphics card and driver support the required version of CUDA (section 1 and 2) but the PyTorch binaries don't support your compute capability (section 3) then your options are

  • Compile PyTorch from source with support for your compute capability (see here)
  • Install PyTorch without CUDA support (CPU-only)
  • Install an older version of the PyTorch binaries that support your compute capability (not recommended as PyTorch 0.3.1 is very outdated at this point). AFAIK compute capability older than 3.X has never been supported in the pre-built binaries
  • Upgrade your graphics card

If your graphics card doesn't support the required version of CUDA (section 1) then your options are

  • Install PyTorch without CUDA support (CPU-only)
  • Install an older version of PyTorch that supports a CUDA version supported by your graphics card (still may require compiling from source if the binaries don't support your compute capability)
  • Upgrade your graphics card

这篇关于为什么即使在使用 cuda 安装 pytorch 后,`torch.cuda.is_available()` 也会返回 False?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆