自学内容网 自学内容网

为Jetson Xavier NX安装合适版本的pytorch(解决torch not compiled with cuda enabled)

Jetson Xavier NX 上我的环境是torch2.3.1 cuda 11.4 cuDNN8.4.1

sudo jtop

nvcc -V

都正常显示 有cuda11.4
环境变量路径也配置了
但是我print(“CUDA available:”, torch.cuda.is_available())输出为False

运行torch.cuda.current_device() 报错是torch not compiled with cuda enabled
怀疑pytorch版本和cuda不匹配,导致用不了cuda,所以打算卸载重装pytorch

0.卸载当前pytorch、torchvision

pip uninstall torch
pip uninstall torchvision

1.官网下载pytorch
https://developer.nvidia.com/embedded/downloads#?search=PyTorch
官网现在Jetpack5.0.2(cuda11.4)对应的pytorch是1.13.0,而不是2.3.1,这就是问题所在

2.安装下载的whl包

pip install torch-1.13.0a0+410ce96a.nv22.12-cp38-cp38-linux_aarch64.whl

3.安装完成后,重新验证 PyTorch 的 CUDA 支持

import torch

print("CUDA available:", torch.cuda.is_available())
print("CUDA device count:", torch.cuda.device_count())
if torch.cuda.is_available():
    print("Current CUDA device:", torch.cuda.current_device())
    print("Device name:", torch.cuda.get_device_name(torch.cuda.current_device()))

成功的输出结果是

CUDA available: True
CUDA device count: 1
Current CUDA device: 0
Device name: Xavier

同时确保环境变量正确

echo $PATH
echo $LD_LIBRARY_PATH
echo $CUDA_HOME

核心原因是NX盒子这个aarch64架构的pytorch要去nvidia网站安装,最好Jetpack刚开始刷机就安装,按pytorch官网的指令下载版本2.3.1在aarch64架构下会不匹配

如果还需要torchvision包,也需要保证安装的和pytorch版本匹配,不能直接指令pip install torchvision,因为这样会卸载掉你安装的torch重新安装最新版本的torch和torchvision

应该指定版本, pip 来安装与 torch-1.13.0 兼容的 torchvision v0.14.0

pip install torchvision==0.14.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

原文地址:https://blog.csdn.net/z5z5z5z56/article/details/140629323

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!