尽管升级到 CUDA 版本,但“断言错误:Torch 未在启用 CUDA 的情况下编译”

Tin*_*a J 12 python conda torch pytorch

我发现这是一个受欢迎的问题,但我仍然找不到解决方案。

我正在尝试运行一个简单的 repo Here,它使用PyTorch. 虽然我刚刚从 pytorch.org ( 1.2.0)将我的 Pytorch 升级到最新的 CUDA 版本,但它仍然抛出相同的错误。我在 Windows 10 上使用 conda 和 python 3.7。

    raise AssertionError("Torch not compiled with CUDA enabled")
AssertionError: Torch not compiled with CUDA enabled
Run Code Online (Sandbox Code Playgroud)

如何解决问题?

这是我的conda list

# Name                    Version                   Build  Channel
_ipyw_jlab_nb_ext_conf    0.1.0                    py37_0    anaconda
_pytorch_select           1.1.0                       cpu    anaconda
_tflow_select             2.3.0                       mkl    anaconda
absl-py                   0.7.1                    pypi_0    pypi
alabaster                 0.7.12                   py37_0    anaconda
anaconda                  2019.07                  py37_0    anaconda
anaconda-client           1.7.2                    py37_0    anaconda
anaconda-navigator        1.9.7                    py37_0    anaconda
anaconda-project          0.8.3                      py_0    anaconda
argparse                  1.4.0                    pypi_0    pypi
asn1crypto                0.24.0                   py37_0    anaconda
astor                     0.8.0                    pypi_0    pypi
astroid                   2.2.5                    py37_0    anaconda
astropy                   3.2.1            py37he774522_0    anaconda
atomicwrites              1.3.0                    py37_1    anaconda
attrs                     19.1.0                   py37_1    anaconda
babel                     2.7.0                      py_0    anaconda
backcall                  0.1.0                    py37_0    anaconda
backports                 1.0                        py_2    anaconda
backports-csv             1.0.7                    pypi_0    pypi
backports-functools-lru-cache 1.5                      pypi_0    pypi
backports.functools_lru_cache 1.5                        py_2    anaconda
backports.os              0.1.1                    py37_0    anaconda
backports.shutil_get_terminal_size 1.0.0                    py37_2    anaconda
backports.tempfile        1.0                        py_1    anaconda
backports.weakref         1.0.post1                  py_1    anaconda
beautifulsoup4            4.7.1                    py37_1    anaconda
bitarray                  0.9.3            py37he774522_0    anaconda
bkcharts                  0.2                      py37_0    anaconda
blas                      1.0                         mkl    anaconda
bleach                    3.1.0                    py37_0    anaconda
blosc                     1.16.3               h7bd577a_0    anaconda
bokeh                     1.2.0                    py37_0    anaconda
boto                      2.49.0                   py37_0    anaconda
bottleneck                1.2.1            py37h452e1ab_1    anaconda
bzip2                     1.0.8                he774522_0    anaconda
ca-certificates           2019.5.15                     0    anaconda
certifi                   2019.6.16                py37_0    anaconda
cffi                      1.12.3           py37h7a1dbc1_0    anaconda
chainer                   6.2.0                    pypi_0    pypi
chardet                   3.0.4                    py37_1    anaconda
cheroot                   6.5.5                    pypi_0    pypi
cherrypy                  18.1.2                   pypi_0    pypi
click                     7.0                      py37_0    anaconda
cloudpickle               1.2.1                      py_0    anaconda
clyent                    1.2.2                    py37_1    anaconda
colorama                  0.4.1                    py37_0    anaconda
comtypes                  1.1.7                    py37_0    anaconda
conda                     4.7.11                   py37_0    anaconda
conda-build               3.18.9                   py37_3    anaconda
conda-env                 2.6.0                         1    anaconda
conda-package-handling    1.3.11                   py37_0    anaconda
conda-verify              3.4.2                      py_1    anaconda
console_shortcut          0.1.1                         3    anaconda
constants                 0.6.0                    pypi_0    pypi
contextlib2               0.5.5                    py37_0    anaconda
cpuonly                   1.0                           0    pytorch
cryptography              2.7              py37h7a1dbc1_0    anaconda
cudatoolkit               10.0.130                      0    anaconda
curl                      7.65.2               h2a8f88b_0    anaconda
cycler                    0.10.0                   py37_0    anaconda
cython                    0.29.12          py37ha925a31_0    anaconda
cytoolz                   0.10.0           py37he774522_0    anaconda
dask                      2.1.0                      py_0    anaconda
dask-core                 2.1.0                      py_0    anaconda
decorator                 4.4.0                    py37_1    anaconda
defusedxml                0.6.0                      py_0    anaconda
distributed               2.1.0                      py_0    anaconda
docutils                  0.14                     py37_0    anaconda
entrypoints               0.3                      py37_0    anaconda
et_xmlfile                1.0.1                    py37_0    anaconda
ez-setup                  0.9                      pypi_0    pypi
fastcache                 1.1.0            py37he774522_0    anaconda
fasttext                  0.9.1                    pypi_0    pypi
feedparser                5.2.1                    pypi_0    pypi
ffmpeg                    4.1.3                h6538335_0    conda-forge
filelock                  3.0.12                     py_0    anaconda
first                     2.0.2                    pypi_0    pypi
flask                     1.1.1                      py_0    anaconda
freetype                  2.9.1                ha9979f8_1    anaconda
future                    0.17.1                   py37_0    anaconda
gast                      0.2.2                    py37_0    anaconda
get                       2019.4.13                pypi_0    pypi
get_terminal_size         1.0.0                h38e98db_0    anaconda
gevent                    1.4.0            py37he774522_0    anaconda
glob2                     0.7                        py_0    anaconda
google-pasta              0.1.7                    pypi_0    pypi
graphviz                  2.38.0                        4    anaconda
greenlet                  0.4.15           py37hfa6e2cd_0    anaconda
grpcio                    1.22.0                   pypi_0    pypi
h5py                      2.9.0            py37h5e291fa_0    anaconda
hdf5                      1.10.4               h7ebc959_0    anaconda
heapdict                  1.0.0                    py37_2    anaconda
html5lib                  1.0.1                    py37_0    anaconda
http-client               0.1.22                   pypi_0    pypi
hypothesis                4.34.0                   pypi_0    pypi
icc_rt                    2019.0.0             h0cc432a_1    anaconda
icu                       58.2                 ha66f8fd_1    anaconda
idna                      2.8                      py37_0    anaconda
imageio                   2.4.1                    pypi_0    pypi
imageio-ffmpeg            0.3.0                    pypi_0    pypi
imagesize                 1.1.0                    py37_0    anaconda
importlib_metadata        0.17                     py37_1    anaconda
imutils                   0.5.2                    pypi_0    pypi
intel-openmp              2019.0                   pypi_0    pypi
ipykernel                 5.1.1            py37h39e3cac_0    anaconda
ipython                   7.6.1            py37h39e3cac_0    anaconda
ipython_genutils          0.2.0                    py37_0    anaconda
ipywidgets                7.5.0                      py_0    anaconda
isort                     4.3.21                   py37_0    anaconda
itsdangerous              1.1.0                    py37_0    anaconda
jaraco-functools          2.0                      pypi_0    pypi
jdcal                     1.4.1                      py_0    anaconda
jedi                      0.13.3                   py37_0    anaconda
jinja2                    2.10.1                   py37_0    anaconda
joblib                    0.13.2                   py37_0    anaconda
jpeg                      9b                   hb83a4c4_2    anaconda
json5                     0.8.4                      py_0    anaconda
jsonschema                3.0.1                    py37_0    anaconda
jupyter                   1.0.0                    py37_7    anaconda
jupyter_client            5.3.1                      py_0    anaconda
jupyter_console           6.0.0                    py37_0    anaconda
jupyter_core              4.5.0                      py_0    anaconda
jupyterlab                1.0.2            py37hf63ae98_0    anaconda
jupyterlab_server         1.0.0                      py_0    anaconda
keras                     2.2.4                         0    anaconda
keras-applications        1.0.8                      py_0    anaconda
keras-base                2.2.4                    py37_0    anaconda
keras-preprocessing       1.1.0                      py_1    anaconda
keyring                   18.0.0                   py37_0    anaconda
kiwisolver                1.1.0            py37ha925a31_0    anaconda
krb5                      1.16.1               hc04afaa_7
lazy-object-proxy         1.4.1            py37he774522_0    anaconda
libarchive                3.3.3                h0643e63_5    anaconda
libcurl                   7.65.2               h2a8f88b_0    anaconda
libiconv                  1.15                 h1df5818_7    anaconda
liblief                   0.9.0                ha925a31_2    anaconda
libmklml                  2019.0.5                      0    anaconda
libpng                    1.6.37               h2a8f88b_0    anaconda
libprotobuf               3.8.0                h7bd577a_0    anaconda
libsodium                 1.0.16               h9d3ae62_0    anaconda
libssh2                   1.8.2                h7a1dbc1_0    anaconda
libtiff                   4.0.10               hb898794_2    anaconda
libxml2                   2.9.9                h464c3ec_0    anaconda
libxslt                   1.1.33               h579f668_0    anaconda
llvmlite                  0.29.0           py37ha925a31_0    anaconda
locket                    0.2.0                    py37_1    anaconda
lxml                      4.3.4            py37h1350720_0    anaconda
lz4-c                     1.8.1.2              h2fa13f4_0    anaconda
lzo                       2.10                 h6df0209_2    anaconda
m2w64-gcc-libgfortran     5.3.0                         6
m2w64-gcc-libs            5.3.0                         7
m2w64-gcc-libs-core       5.3.0                         7
m2w64-gmp                 6.1.0                         2
m2w64-libwinpthread-git   5.0.0.4634.697f757               2
make-dataset              1.0                      pypi_0    pypi
markdown                  3.1.1                    py37_0    anaconda
markupsafe                1.1.1            py37he774522_0    anaconda
matplotlib                3.1.0            py37hc8f65d3_0    anaconda
mccabe                    0.6.1                    py37_1    anaconda
menuinst                  1.4.16           py37he774522_0    anaconda
mistune                   0.8.4            py37he774522_0    anaconda
mkl                       2019.0                   pypi_0    pypi
mkl-service               2.0.2            py37he774522_0    anaconda
mkl_fft                   1.0.12           py37h14836fe_0    anaconda
mkl_random                1.0.2            py37h343c172_0    anaconda
mock                      3.0.5                    py37_0    anaconda
more-itertools            7.0.0                    py37_0    anaconda
moviepy                   1.0.0                    pypi_0    pypi
mpmath                    1.1.0                    py37_0    anaconda
msgpack-python            0.6.1            py37h74a9793_1    anaconda
msys2-conda-epoch         20160418                      1
multipledispatch          0.6.0                    py37_0    anaconda
mysqlclient               1.4.2.post1              pypi_0    pypi
navigator-updater         0.2.1                    py37_0    anaconda
nbconvert                 5.5.0                      py_0    anaconda
nbformat                  4.4.0                    py37_0    anaconda
networkx                  2.3                        py_0    anaconda
ninja                     1.9.0            py37h74a9793_0    anaconda
nltk                      3.4.4                    py37_0    anaconda
nose                      1.3.7                    py37_2    anaconda
notebook                  6.0.0                    py37_0    anaconda
numba                     0.44.1           py37hf9181ef_0    anaconda
numexpr                   2.6.9            py37hdce8814_0    anaconda
numpy                     1.16.4                   pypi_0    pypi
numpy-base                1.16.4           py37hc3f5095_0    anaconda
numpydoc                  0.9.1                      py_0    anaconda
olefile                   0.46                     py37_0    anaconda
opencv-contrib-python     4.1.0.25                 pypi_0    pypi
opencv-python             4.1.0.25                 pypi_0    pypi
openpyxl                  2.6.2                      py_0    anaconda
openssl                   1.1.1c               he774522_1    anaconda
packaging                 19.0                     py37_0    anaconda
pandas                    0.24.2           py37ha925a31_0    anaconda
pandoc                    2.2.3.2                       0    anaconda
pandocfilters             1.4.2                    py37_1    anaconda
parso                     0.5.0                      py_0    anaconda
partd                     1.0.0                      py_0    anaconda
path.py                   12.0.1                     py_0    anaconda
pathlib2                  2.3.4                    py37_0    anaconda
patsy                     0.5.1                    py37_0    anaconda
pattern                   3.6                      pypi_0    pypi
pdfminer-six              20181108                 pypi_0    pypi
pep8                      1.7.1                    py37_0    anaconda
pickleshare               0.7.5                    py37_0    anaconda
pillow                    6.1.0            py37hdc69c19_0    anaconda
pip                       19.1.1                   py37_0    anaconda
pkginfo                   1.5.0.1                  py37_0    anaconda
pluggy                    0.12.0                     py_0    anaconda
ply                       3.11                     py37_0    anaconda
portend                   2.5                      pypi_0    pypi
post                      2019.4.13                pypi_0    pypi
powershell_shortcut       0.0.1                         2    anaconda
proglog                   0.1.9                    pypi_0    pypi
prometheus_client         0.7.1                      py_0    anaconda
prompt_toolkit            2.0.9                    py37_0    anaconda
protobuf                  3.7.1                    pypi_0    pypi
psutil                    5.6.3            py37he774522_0    anaconda
public                    2019.4.13                pypi_0    pypi
py                        1.8.0                    py37_0    anaconda
py-lief                   0.9.0            py37ha925a31_2    anaconda
pybind11                  2.3.0                    pypi_0    pypi
pycodestyle               2.5.0                    py37_0    anaconda
pycosat                   0.6.3            py37hfa6e2cd_0    anaconda
pycparser                 2.19                     py37_0    anaconda
pycrypto                  2.6.1            py37hfa6e2cd_9    anaconda
pycryptodome              3.8.2                    pypi_0    pypi
pycurl                    7.43.0.3         py37h7a1dbc1_0    anaconda
pydot                     1.4.1                    pypi_0    pypi
pyflakes                  2.1.1                    py37_0    anaconda
pygments                  2.4.2                      py_0    anaconda
pylint                    2.3.1                    py37_0    anaconda
pyodbc                    4.0.26           py37ha925a31_0    anaconda
pyopenssl                 19.0.0                   py37_0    anaconda
pyparsing                 2.4.0                      py_0    anaconda
pyqt                      5.9.2            py37h6538335_2    anaconda
pyreadline                2.1                      py37_1    anaconda
pyrsistent                0.14.11          py37he774522_0    anaconda
pysocks                   1.7.0                    py37_0    anaconda
pytables                  3.5.2            py37h1da0976_1    anaconda
pytest                    5.0.1                    py37_0    anaconda
pytest-arraydiff          0.3              py37h39e3cac_0    anaconda
pytest-astropy            0.5.0                    py37_0    anaconda
pytest-doctestplus        0.3.0                    py37_0    anaconda
pytest-openfiles          0.3.2                    py37_0    anaconda
pytest-remotedata         0.3.1                    py37_0    anaconda
python                    3.7.3                h8c8aaf0_1    anaconda
python-dateutil           2.8.0                    py37_0    anaconda
python-docx               0.8.10                   pypi_0    pypi
python-graphviz           0.11.1                   pypi_0    pypi
python-libarchive-c       2.8                     py37_11    anaconda
pytorch                   1.2.0               py3.7_cpu_1  [cpuonly]  pytorch
pytube                    9.5.1                    pypi_0    pypi
pytz                      2019.1                     py_0    anaconda
pywavelets                1.0.3            py37h8c2d366_1    anaconda
pywin32                   223              py37hfa6e2cd_1    anaconda
pywinpty                  0.5.5                 py37_1000    anaconda
pyyaml                    5.1.1            py37he774522_0    anaconda
pyzmq                     18.0.0           py37ha925a31_0    anaconda
qt                        5.9.7            vc14h73c81de_0  [vc14]  anaconda
qtawesome                 0.5.7                    py37_1    anaconda
qtconsole                 4.5.1                      py_0    anaconda
qtpy                      1.8.0                      py_0    anaconda
query-string              2019.4.13                pypi_0    pypi
request                   2019.4.13                pypi_0    pypi
requests                  2.22.0                   py37_0    anaconda
rope                      0.14.0                     py_0    anaconda
ruamel_yaml               0.15.46          py37hfa6e2cd_0    anaconda
scikit-image              0.15.0           py37ha925a31_0    anaconda
scikit-learn              0.21.2           py37h6288b17_0    anaconda
scipy                     1.3.0                    pypi_0    pypi
scipy-stack               0.0.5                    pypi_0    pypi
seaborn                   0.9.0                    py37_0    anaconda
send2trash                1.5.0                    py37_0    anaconda
setuptools                41.1.0                   pypi_0    pypi
simplegeneric             0.8.1                    py37_2    anaconda
singledispatch            3.4.0.3                  py37_0    anaconda
sip                       4.19.8           py37h6538335_0    anaconda
six                       1.12.0                   py37_0    anaconda
snappy                    1.1.7                h777316e_3    anaconda
snowballstemmer           1.9.0                      py_0    anac

Eli*_*ski 50

你不必通过 anaconda 安装它,你可以从他们的网站安装 cuda 。安装结束后打开一个新终端并使用以下命令检查您的 cuda 版本:

>>> nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Thu_Nov_18_09:52:33_Pacific_Standard_Time_2021
Cuda compilation tools, release 11.5, V11.5.119
Build cuda_11.5.r11.5/compiler.30672275_0
Run Code Online (Sandbox Code Playgroud)

我的是V11.5

然后到这里选择你的操作系统和首选的包管理器(pip或anaconda),以及你安装的cuda版本,并复制生成的安装命令,我得到:

pip3 install torch==1.10.1+cu113 torchvision==0.11.2+cu113 torchaudio===0.10.1+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html
Run Code Online (Sandbox Code Playgroud)

请注意,对我来说,我安装了 python 3.10,但我的项目运行超过 3.9,因此要么使用虚拟环境,要么显式运行您想要的基本解释器的 pip(例如C:\Software\Python\Python39\python.exe -m pip install .....),否则您将陷入Could not find a version that satisfies the requirement torch错误

然后打开 python 控制台并检查 cuda 可用性

>>> nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Thu_Nov_18_09:52:33_Pacific_Standard_Time_2021
Cuda compilation tools, release 11.5, V11.5.119
Build cuda_11.5.r11.5/compiler.30672275_0
Run Code Online (Sandbox Code Playgroud)

  • 该网站给了我“pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116”,但“cuda.is_available()”仍然返回“False”,并且我的脚本不断引发`断言错误:Torch 未在启用 CUDA 的情况下编译`。 (8认同)
  • 如果您仍然得到“False”,您可能必须先通过“pip3 uninstall torch torchvision torchaudio”安装没有 Cuda 支持的先前版本。 (3认同)

小智 12

你是如何安装pytorch的?听起来你安装的 pytorch 没有 CUDA 支持。https://pytorch.org/有关于如何安装支持 cuda 的 pytorch 的说明。

在这种情况下,我们有以下命令:

conda install pytorch torchvision cudatoolkit=10.1 -c pytorch

或者使用最新 cudatoolkit 版本的命令。

  • 请仅在评论中提出问题。仅在确定时才回答问题! (7认同)

小智 11

卸载软件包并使用 pip 重新安装它为我解决了这个问题。

1.conda remove pytorch torchvision torchaudio cudatoolkit

2.pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116