Youda's blog

努力工作 认真生活......

0%

CUDA capability sm_86 is not compatible with the current PyTorch installation

YoloV5按照官网环境配置后执行报错
具体报错问题体现如下:

1
2
3
4
NVIDIA GeForce RTX 3070 with CUDA capability sm_86 is not compatible with the current PyTorch installation.
The current PyTorch install supports CUDA capabilities sm_37 sm_50 sm_60 sm_70.
If you want to use the NVIDIA GeForce RTX 3070 GPU with PyTorch, please check the instructions at https://pytorch.org/get-started/locally/

yolov5 环境配置

按照网上的yolov5官网的说明文档安装配置完成yolov5的环境(pip install -r requirements.txt)以后,验证了一下环境是否配置成功: cuda.is_availabe()=True,说明环境配置符合要求

1
2
3
4
5
6
7
8
9
10
11
(debug)  youda@youda-ubuntu  ~/workspace/yolov5  python
Python 3.6.7 |Anaconda, Inc.| (default, Oct 23 2018, 19:16:44)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.cuda
torch.cuda
>>> torch.cuda.is_available()
True
>>>

执行demo

执行官方demo: python detect.py,出现一下错误,看了下大概意思就是当前安装的pytorch版本和机器的GPU版本的算力不匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
(debug)  youda@youda-ubuntu  ~/workspace/yolov5 
(debug) youda@youda-ubuntu  ~/workspace/yolov5  python detect.py
detect: weights=yolov5s.pt, source=data/images, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False
/home/youda/anaconda3/envs/debug/lib/python3.6/site-packages/torch/cuda/__init__.py:143: UserWarning:
NVIDIA GeForce RTX 3070 with CUDA capability sm_86 is not compatible with the current PyTorch installation.
The current PyTorch install supports CUDA capabilities sm_37 sm_50 sm_60 sm_70.
If you want to use the NVIDIA GeForce RTX 3070 GPU with PyTorch, please check the instructions at https://pytorch.org/get-started/locally/

warnings.warn(incompatible_device_warn.format(device_name, capability, " ".join(arch_list), device_name))
YOLOv5 🚀 2021-11-15 torch 1.10.0+cu102 CUDA:0 (NVIDIA GeForce RTX 3070, 7960MiB)

Traceback (most recent call last):
File "detect.py", line 244, in <module>
main(opt)
File "detect.py", line 239, in main
run(**vars(opt))
File "/home/youda/anaconda3/envs/debug/lib/python3.6/site-packages/torch/autograd/grad_mode.py", line 28, in decorate_context
return func(*args, **kwargs)
File "detect.py", line 79, in run
model = DetectMultiBackend(weights, device=device, dnn=dnn)
File "/home/youda/workspace/yolov5/models/common.py", line 305, in __init__
model = torch.jit.load(w) if 'torchscript' in w else attempt_load(weights, map_location=device)
File "/home/youda/workspace/yolov5/models/experimental.py", line 98, in attempt_load
model.append(ckpt['ema' if ckpt.get('ema') else 'model'].float().fuse().eval()) # FP32 model
File "/home/youda/anaconda3/envs/debug/lib/python3.6/site-packages/torch/nn/modules/module.py", line 735, in float
return self._apply(lambda t: t.float() if t.is_floating_point() else t)
File "/home/youda/workspace/yolov5/models/yolo.py", line 240, in _apply
self = super()._apply(fn)
File "/home/youda/anaconda3/envs/debug/lib/python3.6/site-packages/torch/nn/modules/module.py", line 570, in _apply
module._apply(fn)
File "/home/youda/anaconda3/envs/debug/lib/python3.6/site-packages/torch/nn/modules/module.py", line 570, in _apply
module._apply(fn)
File "/home/youda/anaconda3/envs/debug/lib/python3.6/site-packages/torch/nn/modules/module.py", line 570, in _apply
module._apply(fn)
File "/home/youda/anaconda3/envs/debug/lib/python3.6/site-packages/torch/nn/modules/module.py", line 593, in _apply
param_applied = fn(param)
File "/home/youda/anaconda3/envs/debug/lib/python3.6/site-packages/torch/nn/modules/module.py", line 735, in <lambda>
return self._apply(lambda t: t.float() if t.is_floating_point() else t)
RuntimeError: CUDA error: no kernel image is available for execution on the device
CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1.
(debug) ✘ youda@youda-ubuntu  ~/workspace/yolov5 

问题定位

根据上述报错可以看出,使用默认的命令安装的版本是 torch 1.10.0+cu102,对应的CUDA版本应该是10.2的
使用nvcc –version查看一下本机安装的cuda的实际版本: 11.0.194

1
2
3
4
5
6
7
(debug)  youda@youda-ubuntu  ~/workspace/yolov5  nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Thu_Jun_11_22:26:38_PDT_2020
Cuda compilation tools, release 11.0, V11.0.194
Build cuda_11.0_bu.TC445_37.28540450_0
(debug) youda@youda-ubuntu  ~/workspace/yolov5 

所以问题的根本原因确实是报错原因里体现的版本不匹配

其实这个问题在验证pytorch的安装的时候就可以发现的,在验证的时候不能仅仅简单的看看CUDA是否可用,还需要关注具体的运行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(debug)  youda@youda-ubuntu  ~/workspace/yolov5  python
Python 3.6.7 |Anaconda, Inc.| (default, Oct 23 2018, 19:16:44)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.cuda
torch.cuda
>>> torch.cuda.is_available()
True
>>> a=torch.Tensor(5,3)
>>> a.cuda()
/home/youda/anaconda3/envs/debug/lib/python3.6/site-packages/torch/cuda/__init__.py:143: UserWarning:
NVIDIA GeForce RTX 3070 with CUDA capability sm_86 is not compatible with the current PyTorch installation.
The current PyTorch install supports CUDA capabilities sm_37 sm_50 sm_60 sm_70.
If you want to use the NVIDIA GeForce RTX 3070 GPU with PyTorch, please check the instructions at https://pytorch.org/get-started/locally/

warnings.warn(incompatible_device_warn.format(device_name, capability, " ".join(arch_list), device_name))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/youda/anaconda3/envs/debug/lib/python3.6/site-packages/torch/_tensor.py", line 249, in __repr__
return torch._tensor_str._str(self)
File "/home/youda/anaconda3/envs/debug/lib/python3.6/site-packages/torch/_tensor_str.py", line 415, in _str
return _str_intern(self)
File "/home/youda/anaconda3/envs/debug/lib/python3.6/site-packages/torch/_tensor_str.py", line 390, in _str_intern
tensor_str = _tensor_str(self, indent)
File "/home/youda/anaconda3/envs/debug/lib/python3.6/site-packages/torch/_tensor_str.py", line 251, in _tensor_str
formatter = _Formatter(get_summarized_data(self) if summarize else self)
File "/home/youda/anaconda3/envs/debug/lib/python3.6/site-packages/torch/_tensor_str.py", line 90, in __init__
nonzero_finite_vals = torch.masked_select(tensor_view, torch.isfinite(tensor_view) & tensor_view.ne(0))
RuntimeError: CUDA error: no kernel image is available for execution on the device
CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1.
>>>
>>> exit()

解决问题

安装匹配版本

去官网重新下载和当前cuda匹配的pytorch版本: 官网地址
本地Nvidia驱动兼容的版本是11.4及以下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
(base)  youda@youda-ubuntu  ~  nvidia-smi
Tue Nov 15 21:02:24 2021
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 470.82.01 Driver Version: 470.82.01 CUDA Version: 11.4 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 NVIDIA GeForce ... Off | 00000000:01:00.0 Off | N/A |
| 0% 40C P8 20W / 240W | 329MiB / 7959MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| 0 N/A N/A 1180 G /usr/lib/xorg/Xorg 53MiB |
| 0 N/A N/A 1656 G /usr/lib/xorg/Xorg 222MiB |
| 0 N/A N/A 1791 G /usr/bin/gnome-shell 11MiB |
| 0 N/A N/A 929292 G ...AAAAAAAAA= --shared-files 11MiB |
| 0 N/A N/A 929500 G ...wares/firefox/firefox-bin 11MiB |
+-----------------------------------------------------------------------------+
(base) youda@youda-ubuntu  ~ 

自己安装的版本是11.0

1
2
3
4
5
6
(debug)  youda@youda-ubuntu  ~/workspace/yolov5  nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Thu_Jun_11_22:26:38_PDT_2020
Cuda compilation tools, release 11.0, V11.0.194
Build cuda_11.0_bu.TC445_37.28540450_0

所以在此选择了 cuda 11.3版本的torch

1
2
3
4
(yolov5-py36)  youda@youda-ubuntu  ~/workspace/yolov5  pip list | grep torch
torch 1.10.0+cu113
torchaudio 0.10.0+cu113
torchvision 0.11.1+cu113

验证

torch.Tensor(5,3).cuda()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(yolov5-py36)  youda@youda-ubuntu  ~/workspace/yolov5 
(yolov5-py36) youda@youda-ubuntu  ~/workspace/yolov5  python
Python 3.6.7 |Anaconda, Inc.| (default, Oct 23 2018, 19:16:44)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.cuda.is_available()
True
>>> a=torch.Tensor(5,3)
>>> a.cuda()
tensor([[3.0881e+29, 4.5840e+30, 4.2039e-45],
[1.5947e-42, 7.7551e-39, 1.0250e-35],
[2.9396e+29, 7.4797e-39, 4.5081e-23],
[1.7589e+22, 3.2180e+21, 5.2170e-11],
[3.0881e+29, 8.2382e+23, 4.5444e+30]], device='cuda:0')
>>>
>>> exit()
(yolov5-py36) youda@youda-ubuntu  ~/workspace/yolov5 

python detect.py

1
2
3
4
5
6
7
8
9
10
11
(yolov5-py36)  youda@youda-ubuntu  ~/workspace/yolov5  python detect.py
detect: weights=yolov5s.pt, source=data/images, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False
YOLOv5 🚀 2021-11-15 torch 1.10.0+cu113 CUDA:0 (NVIDIA GeForce RTX 3070, 7960MiB)

Fusing layers...
Model Summary: 213 layers, 7225885 parameters, 0 gradients
image 1/2 /home/youda/workspace/yolov5/data/images/bus.jpg: 640x480 4 persons, 1 bus, Done. (0.008s)
image 2/2 /home/youda/workspace/yolov5/data/images/zidane.jpg: 384x640 2 persons, 1 tie, Done. (0.008s)
Speed: 0.2ms pre-process, 7.9ms inference, 0.7ms NMS per image at shape (1, 3, 640, 640)
Results saved to runs/detect/exp9
(yolov5-py36) youda@youda-ubuntu  ~/workspace/yolov5 