AssertionError: Torch not compiled with CUDA enabled solution

Article directory

Encounter problems

(base) C:\Users\m1521>python
Python 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> a = torch.ones((3, 1))
>>> a  = a.cuda(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\cuda\__init__.py", line 221, in _lazy_init
    raise AssertionError("Torch not compiled with CUDA enabled")
AssertionError: Torch not compiled with CUDA enabled

Solution

Uninstall torch

(base) C:\Users\m1521>pip uninstall torch
Found existing installation: torch 1.13.1
Uninstalling torch-1.13.1:
  Would remove:
    c:\programdata\anaconda3\lib\site-packages\functorch\*
    c:\programdata\anaconda3\lib\site-packages\torch-1.13.1.dist-info\*
    c:\programdata\anaconda3\lib\site-packages\torch\*
    c:\programdata\anaconda3\lib\site-packages\torchgen\*
    c:\programdata\anaconda3\scripts\convert-caffe2-to-onnx.exe
    c:\programdata\anaconda3\scripts\convert-onnx-to-caffe2.exe
    c:\programdata\anaconda3\scripts\torchrun.exe
Proceed (Y/n)? y
  Successfully uninstalled torch-1.13.1

Re-select the new PyTorch version to solve this problem: from 11.7 to 11.6

Insert image description here
Still use pip to download

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

Test Results

(base) C:\Users\m1521>python
Python 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.cuda.is_available()
True
>>> a = torch.ones((3, 1))
>>> a = a.cuda(0)
>>> b = torch.ones((3,1))
>>> a + b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!
>>>
>>> b = torch.ones((3,1)).cuda(0)
>>> a + b
tensor([[2.],
        [2.],
        [2.]], device='cuda:0')
>>>

reference

  1. https://github.com/pytorch/pytorch/issues/30664

Guess you like

Origin blog.csdn.net/shizheng_Li/article/details/128506735