win10在anaconda虚拟环境下安装cuda11.0+TensorFlow2.4.0+pytorch1.7.1+cudnn8.0

支持TensorFlow和pytorch在GPU加速下,pycharm中运行。

软件版本

Windows10
NVIDIA GeForce GTX 1660Ti
CUDA11.0
TensorFlow-gpu2.4.0
pytorch1.7.1
cudnn8.0
python3.8.0
anaconda
pycharm community 2020.3

查看适配版本

CUDA

电脑右下角 NVIDIA设置
在这里插入图片描述
左下角系统信息
在这里插入图片描述
可以看到支持的最高CUDA版本
在这里插入图片描述
这里我们选择11.6及以下的版本都是可以的,为了适配tf和torch版本,本次选择11.0。
注意:请确定您的显卡支持CUDA。

cudnn

cudnn查询对应版本

TensorFlow

自编译tensorflow1.python3.5,tensorflow1.12; 2.支持cuda10.0,cudnn7.3.1,TensorRT-5.0.2.6-cuda10.0-cudnn7.3; 3.支持mkl,无MPI; 软硬件硬件环境:Ubuntu16.04,GeForce GTX 1080 配置信息: hp@dla:~/work/ts_compile/tensorflow$ ./configure WARNING: --batch mode is deprecated. Please instead explicitly shut down your Bazel server using the command "bazel shutdown". You have bazel 0.19.1 installed. Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3 Found possible Python library paths: /usr/local/lib/python3.5/dist-packages /usr/lib/python3/dist-packages Please input the desired Python library path to use. Default is [/usr/local/lib/python3.5/dist-packages] Do you wish to build TensorFlow with XLA JIT support? [Y/n]: XLA JIT support will be enabled for TensorFlow. Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: No OpenCL SYCL support will be enabled for TensorFlow. Do you wish to build TensorFlow with ROCm support? [y/N]: No ROCm support will be enabled for TensorFlow. Do you wish to build TensorFlow with CUDA support? [y/N]: y CUDA support will be enabled for TensorFlow. Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 10.0]: Please specify the location where CUDA 10.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: /usr/local/cuda-10.0 Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7]: 7.3.1 Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda-10.0]: Do you wish to build TensorFlow with TensorRT support? [y/N]: y TensorRT support will be enabled for TensorFlow. Please specify the location where TensorRT is installed. [Default is /usr/lib/x86_64-linux-gnu]:/home/hp/bin/TensorRT-5.0.2.6-cuda10.0-cudnn7.3/targets/x86_64-linux-gnu Please specify the locally installed NCCL version you want to use. [Default is to use https://github.com/nvidia/nccl]: Please specify a list of comma-separated Cuda compute capabilities you want to build with. You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus. Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 6.1,6.1,6.1]: Do you want to use clang as CUDA compiler? [y/N]: nvcc will be used as CUDA compiler. Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: Do you wish to build TensorFlow with MPI support? [y/N]: No MPI support will be enabled for TensorFlow. Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native -Wno-sign-compare]: Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: Not configuring the WORKSPACE for Android builds. Preconfigured Bazel build configs. You can use any of the below by adding "--config=" to your build command. See .bazelrc for more details. --config=mkl # Build with MKL support. --config=monolithic # Config for mostly static monolithic build. --config=gdr # Build with GDR support. --config=verbs # Build with libverbs support. --config=ngraph # Build with Intel nGraph support. --config=dynamic_kernels # (Experimental) Build kernels into separate shared objects. Preconfigured Bazel build configs to DISABLE default on features: --config=noaws # Disable AWS S3 filesystem support. --config=nogcp # Disable GCP support. --config=nohdfs # Disable HDFS support. --config=noignite # Disable Apacha Ignite support. --config=nokafka # Disable Apache Kafka support. --config=nonccl # Disable NVIDIA NCCL support. Configuration finished 编译: hp@dla:~/work/ts_compile/tensorflow$ bazel build --config=opt --config=mkl --verbose_failures //tensorflow/tools/pip_package:build_pip_package 卸载已有tensorflow: hp@dla:~/temp$ sudo pip3 uninstall tensorflow 安装自己编译的成果: hp@dla:~/temp$ sudo pip3 install tensorflow-1.12.0-cp35-cp35m-linux_x86_64.whl
<think>我们正在解决PyTorchCUDA工具包、torchvision、torchaudio之间的版本兼容性问题。根据引用内容,我们可以总结以下要点:1.PyTorchCUDA工具包的版本必须兼容(引用[1]展示了不兼容时的错误)。2.安装PyTorch时,可以通过指定`pytorch-cuda`参数来明确CUDA版本(引用[2])。3.使用Conda安装时,可以同时指定PyTorch、torchvision、torchaudio和cudatoolkit的版本(引用[3])。4.使用pip安装时,可以通过特定URL获取预编译的兼容版本(引用[4])。为了解决版本冲突,我们需要查阅官方发布的版本兼容性表,但由于官方文档可能更新,我们可以通过以下步骤来选择和安装兼容的版本:###步骤1:确定PyTorchCUDA版本的兼容性访问PyTorch官方网站(https://pytorch.org/get-started/previous-versions/)查看历史版本的兼容性。但这里我们根据引用内容总结一些已知的兼容组合:-PyTorch1.5.1需要CUDA9.010.1(引用[1]中尝试9.0失败,可能是因为PyTorch1.5.1需要更高版本的CUDA?实际上官方记录1.5.1支持CUDA9.210.1)-PyTorch1.12.1支持CUDA10.211.3、11.6(引用[3]使用了11.6)-PyTorch1.9.0支持CUDA10.211.1(引用[4]使用了11.1)###步骤2:选择组合并安装假设我们想安装PyTorch1.12.1,那么根据引用[3]的命令:```bashcondainstallpytorch==1.12.1torchvision==0.13.1torchaudio==0.12.1cudatoolkit=11.6-cpytorch-cconda-forge```但注意,这里使用了`-cpytorch`和`-cconda-forge`,这可能会从官方频道下载,速度较慢。我们可以尝试使用清华镜像源,但是需要确保镜像源中有这些包。###使用清华镜像源安装为了避免从官方频道下载,我们可以先配置清华镜像源,然后尝试在不指定频道的情况下安装(因为配置镜像后,默认从镜像源搜索)。配置镜像源:```bashcondaconfig--addchannelshttps://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/condaconfig--addchannelshttps://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/condaconfig--addchannelshttps://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/condaconfig--addchannelshttps://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/condaconfig--setshow_channel_urlsyescondaconfig--setchannel_prioritystrict```然后执行安装命令(注意,此时不再使用`-c`参数指定频道,因为镜像源已经包含这些频道):```bashcondainstallpytorch==1.12.1torchvision==0.13.1torchaudio==0.12.1cudatoolkit=11.6```###如果镜像源中没有怎么办?如果清华镜像源没有某个版本的包,我们可以尝试使用pip安装(使用清华的pypi镜像)。例如,安装PyTorch1.9.0(引用[4]):```bashpipinstalltorch==1.9.0+cu111torchvision==0.10.0+cu111torchaudio==0.9.0-fhttps://download.pytorch.org/whl/torch_stable.html-ihttps://pypi.tuna.tsinghua.edu.cn/simple```###处理冲突的通用方法如果遇到不兼容问题,我们可以先尝试创建一个新的环境,然后按照官方推荐的组合安装。例如,根据PyTorch版本选择对应的torchvision和torchaudio(参考https://pytorch.org/get-started/previous-versions/)。以下是一些常见组合:|PyTorch版本|推荐的torchvision版本|推荐的torchaudio版本|支持的CUDA版本||--------------|------------------------|------------------------|------------------||2.0.1|0.15.2|2.0.2|11.7,11.8||1.13.1|0.14.1|0.13.1|11.6,11.7||1.12.1|0.13.1|0.12.1|10.2,11.3,11.6||1.11.0|0.12.0|0.11.0|10.2,11.3,11.5||1.10.2|0.11.3|0.10.2|10.2,11.3||1.9.1|0.10.1|0.9.1|10.2,11.1||1.8.2|0.9.2|0.8.2|10.1,10.2,11.1||...|...|...|...|###示例:安装最新稳定版PyTorch(使用清华镜像)如果我们想安装最新稳定版,并且使用CUDA11.8,可以执行:```bashcondainstallpytorchtorchvisiontorchaudiopytorch-cuda=11.8-chttps://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/win-64/```但是注意,这样直接指定单个URL可能会忽略其他依赖的包。所以更推荐配置镜像源后直接安装。###步骤3:验证安装安装完成后,使用Python验证:```pythonimporttorchprint(torch.__version__)#PyTorch版本print(torch.version.cuda)#所依赖的CUDA版本print(torch.cuda.is_available())#是否可用```###常见问题解决1.**如果安装后`torch.cuda.is_available()`返回False**,可能是系统CUDA驱动版本过低,需要升级驱动(PyTorch预编译的CUDA版本需要匹配的驱动,例如CUDA11.6需要驱动版本>=450.80.02)。2.**如果出现动态链接库错误**,可能是系统安装了多个CUDA版本,可以通过环境变量`CUDA_VISIBLE_DEVICES`或`CUDA_HOME`指定。3.**conda和pip混合使用可能导致问题**,尽量在同一个环境中使用同一种包管理器。###最终建议为了避免冲突,最好的方法是:1.创建新的conda环境:`condacreate-ntorch_envpython=3.9`2.激活环境:`condaactivatetorch_env`3.根据CUDA版本需求,选择PyTorch对应版本,然后根据官方文档的组合安装。§§相关问题§§1.如何升级NVIDIA驱动以支持更高版本的CUDA2.Conda和pip混合使用会有什么后果?如何避免?3.如何在同一台机器上管理多个CUDA版本?4.PyTorch安装CUDA不可用,如何排查?</think> ## 解决PyTorchCUDA工具包、torchvision、torchaudio的版本兼容问题 当安装PyTorch时,必须确保以下组件的版本完全兼容: 1. PyTorch核心库 2. CUDA工具包 3. torchvision 4. torchaudio 5. Python版本 以下是系统化的解决方案: ### 🧩 步骤1:选择官方推荐的兼容组合 根据PyTorch官方的版本兼容性矩阵(访问 [https://pytorch.org/get-started/previous-versions/](https://pytorch.org/get-started/previous-versions/) 获取最新信息),以下推荐组合100%兼容: | PyTorch 版本 | torchvision 版本 | torchaudio 版本 | CUDA 版本 | Python 版本 | |--------------|------------------|-----------------|----------|------------| | 2.1.0 | 0.16.0 | 2.1.0 | 11.8 | ≥3.8 | | 1.13.1 | 0.14.1 | 0.13.1 | 11.6 | ≥3.7 | | 1.12.1 | 0.13.1 | 0.12.1 | 11.6 | ≥3.7 | [^3] | 1.9.0 | 0.10.0 | 0.9.0 | 11.1 | ≥3.6 | [^4] ### 💻 步骤2:使用conda安装兼容组合(清华源加速) ```bash # 创建新环境(避免冲突) conda create -n pytorch_env python=3.9 # 激活环境 conda activate pytorch_env # 安装推荐组合(示例:PyTorch 1.13.1 + CUDA 11.6) conda install pytorch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1 cudatoolkit=11.6 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch ``` ### ⚡ 步骤3:替代方案 - 使用pip安装(指定清华镜像) ```bash # PyTorch 2.1 + CUDA 11.8 pip install torch==2.1.0 torchvision==0.16.0 torchaudio==2.1.0 --extra-index-url https://download.pytorch.org/whl/cu118 -i https://pypi.tuna.tsinghua.edu.cn/simple # PyTorch 1.9 + CUDA 11.1 pip install torch==1.9.0+cu111 torchvision==0.10.0+cu111 torchaudio==0.9.0 -f https://download.pytorch.org/whl/torch_stable.html -i https://pypi.tuna.tsinghua.edu.cn/simple [^4] ``` ### 🔍 步骤4:验证安装兼容性 ```python import torch, torchvision, torchaudio # 显示各组件版本 print(f"PyTorch: {torch.__version__}") print(f"torchvision: {torchvision.__version__}") print(f"torchaudio: {torchaudio.__version__}") print(f"CUDA available: {torch.cuda.is_available()}") print(f"CUDA version: {torch.version.cuda}") ``` ### 🛠️ 冲突解决技巧 1. **版本冲突报错时**: ```bash # 显示所有可用版本 conda search pytorch --channel https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch ``` 2. **强制兼容安装**: ```bash conda install --channel https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch pytorch=1.12.1 torchvision=0.13.1 torchaudio=0.12.1 cudatoolkit=11.6 ``` 3. **清理冲突依赖**: ```bash conda clean --all conda update --all ``` ### 📊 版本兼容性关键点 $$ \text{兼容性} = \begin{cases} \text{PyTorch版本} \leftrightarrow \text{CUDA版本} \\ \text{PyTorch版本} \leftrightarrow \text{torchvision版本} \\ \text{Python版本} \leftrightarrow \text{所有组件} \end{cases} \leq \text{官方兼容矩阵} $$ 所有箭头必须满足官方定义的兼容关系[^1][^2][^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值