Anaconda 读取错误的 CUDA 版本

Anaconda reading wrong CUDA version

我有一个带有 PyTorch 和 Tensorflow 的 conda 环境,它们都需要 CUDA 9.0(来自 conda 的 ~cudatoolkit 9.0)。在用 torchvision 和 cudatoolkit 安装 pytorch 之后(就像他们在他们的网站上提供的那样)我想安装 Tensorflow,这里的问题是我得到这个错误:

Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: / 
Found conflicts! Looking for incompatible packages.
This can take several minutes.  Press CTRL-C to abort.
failed                                                                                                   

UnsatisfiableError: The following specifications were found
to be incompatible with the existing python installation in your environment:

Specifications:

  - tensorflow==1.12.0 -> python[version='2.7.*|3.6.*']
  - tensorflow==1.12.0 -> python[version='>=2.7,<2.8.0a0|>=3.6,<3.7.0a0']

Your python: python=3.5

If python is on the left-most side of the chain, that's the version you've asked for.
When python appears to the right, that indicates that the thing on the left is somehow
not available for the python version you are constrained to. Note that conda will not
change your python version to a different minor version unless you explicitly specify
that.

The following specifications were found to be incompatible with your system:

  - feature:/linux-64::__cuda==10.2=0
  - feature:|@/linux-64::__cuda==10.2=0

Your installed version is: 10.2

如果我 运行 nvccnvidia-smi 在我的主机或激活的 conda 环境上,我知道我已经安装了 CUDA 10.2,即使 conda list 显示我cudatoolkit 9.0 已安装。有什么解决办法吗?

编辑:

当 运行 宁此代码示例时:

# setting device on GPU if available, else CPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('Using device:', device)
print()

#Additional Info when using cuda
if device.type == 'cuda':
    print(torch.cuda.get_device_name(0))
    print('Memory Usage:')
    print('Allocated:', round(torch.cuda.memory_allocated(0)/1024**3,1), 'GB')
    print('Cached:   ', round(torch.cuda.memory_cached(0)/1024**3,1), 'GB')


print(torch.version.cuda)

我得到这个输出:

GeForce GTX 1050
Memory Usage:
Allocated: 0.0 GB
Cached:    0.0 GB
9.0.176

所以 PyTorch 确实获得了正确的 CUDA 版本,我只是无法安装 tensorflow-gpu。

我的经验是,即使conda检测到的cuda版本不正确,重要的是cudatoolkit版本。

我的实际问题是不兼容python版本。 例如。您可能有 Python 3.7/3.8,但此版本的 tensorflow 不支持:

tensorflow==1.12.0 -> python[version='>=2.7,<2.8.0a0|>=3.6,<3.7.0a0']

试试旧的conda环境或者新的tensorflow,2.0之前的最新版本是1.15

如果我没有误解的话,你是使用

安装包的
conda install

来自 https://pytorch.org/get-started/locally/ 我曾经遇到过 PyTorch 和 Cuda 的 conda 安装问题:我通过删除使用 conda 安装的软件包并通过 pip re-installing 解决了它。

如果您害怕使用 pip 弄乱 conda 环境,我建议您创建另一个环境以测试此解决方案。

  1. 使用conda删除pytorch和cuda。请参阅 删除程序包 Conda Managing packages
  2. 安装您需要的cuda toolkit。请注意,pytorch 仅支持 cuda 9.2、10.1 和 10.2,您可以在 Pytorch 下载页面上看到。 如果您的 OS 是 ubuntu 19,请按照 ubuntu 18 的 CUDA 说明进行操作。另请注意,由于驱动程序原因,并非所有 gpus 都支持最新版本的工具包(1050 应该足够新支持所有这些,肯定会达到 10.1,因为我用过它)。
  3. 按照说明安装带有适当 cuda 支持的 pytorch 通过 pipPytorch download page. See Using pip in an enviroment at Conda Managing packages

If I run nvcc or nvidia-smi on my host or the activated conda environment, I get that I have installed CUDA 10.2, even though conda list shows me that cudatoolkit 9.0 is installed. Any solution to this?

cudatoolkit 不附带编译器 (nvcc),因此当您 运行 nvcc 时,您从系统范围安装启动编译器。这就是为什么它打印 10.2 而不是 9.0,而 pytorch 看到本地 cudatoolkit.

anaconda / packages / cudatoolkit :

This CUDA Toolkit includes GPU-accelerated libraries, and the CUDA runtime for the Conda ecosystem. For the full CUDA Toolkit with a compiler and development tools visit https://developer.nvidia.com/cuda-downloads

根据您上面的评论,我了解到您正在使用 python=3.5.6。所以,首先你应该搜索可用的 tensorflow py35 构建使用:

conda search tensorflow | grep py35

我有以下输出:

tensorflow                     1.9.0 eigen_py35h8c89287_1  pkgs/main           
tensorflow                     1.9.0 gpu_py35h42d5ad8_1  pkgs/main           
tensorflow                     1.9.0 gpu_py35h60c0932_1  pkgs/main           
tensorflow                     1.9.0 gpu_py35hb39db67_1  pkgs/main           
tensorflow                     1.9.0 mkl_py35h5be851a_1  pkgs/main           
tensorflow                    1.10.0 eigen_py35h5ed898b_0  pkgs/main           
tensorflow                    1.10.0 gpu_py35h566a776_0  pkgs/main           
tensorflow                    1.10.0 gpu_py35ha6119f3_0  pkgs/main           
tensorflow                    1.10.0 gpu_py35hd9c640d_0  pkgs/main           
tensorflow                    1.10.0 mkl_py35heddcb22_0  pkgs/main

如您所见,py35 没有 tensorflow 1.12.0 构建,这就是您收到该错误的原因。您可以尝试检查其他 conda 个频道,例如 conda-forge:

conda search tensorflow -c conda-forge | grep py35

但这没有帮助:

tensorflow                     0.9.0          py35_0  conda-forge         
tensorflow                    0.10.0          py35_0  conda-forge         
tensorflow                 0.11.0rc0          py35_0  conda-forge         
tensorflow                 0.11.0rc2          py35_0  conda-forge         
tensorflow                    0.11.0          py35_0  conda-forge         
tensorflow                    0.12.1          py35_0  conda-forge         
tensorflow                    0.12.1          py35_1  conda-forge         
tensorflow                    0.12.1          py35_2  conda-forge         
tensorflow                     1.0.0          py35_0  conda-forge         
tensorflow                     1.1.0          py35_0  conda-forge         
tensorflow                     1.2.0          py35_0  conda-forge         
tensorflow                     1.2.1          py35_0  conda-forge         
tensorflow                     1.3.0          py35_0  conda-forge         
tensorflow                     1.4.0          py35_0  conda-forge         
tensorflow                     1.5.0          py35_0  conda-forge         
tensorflow                     1.5.1          py35_0  conda-forge         
tensorflow                     1.6.0          py35_0  conda-forge         
tensorflow                     1.8.0          py35_0  conda-forge         
tensorflow                     1.8.0          py35_1  conda-forge         
tensorflow                     1.9.0 eigen_py35h8c89287_1  pkgs/main           
tensorflow                     1.9.0 gpu_py35h42d5ad8_1  pkgs/main           
tensorflow                     1.9.0 gpu_py35h60c0932_1  pkgs/main           
tensorflow                     1.9.0 gpu_py35hb39db67_1  pkgs/main           
tensorflow                     1.9.0 mkl_py35h5be851a_1  pkgs/main           
tensorflow                     1.9.0          py35_0  conda-forge         
tensorflow                    1.10.0 eigen_py35h5ed898b_0  pkgs/main           
tensorflow                    1.10.0 gpu_py35h566a776_0  pkgs/main           
tensorflow                    1.10.0 gpu_py35ha6119f3_0  pkgs/main           
tensorflow                    1.10.0 gpu_py35hd9c640d_0  pkgs/main           
tensorflow                    1.10.0 mkl_py35heddcb22_0  pkgs/main           
tensorflow                    1.10.0          py35_0  conda-forge

因此,可能的解决方案是:

  1. 安装一个较旧的 tensorflow 1.10.0 gpu_py35 版本。
  2. 切换到 python 3.6
conda search tensorflow | grep py36

...
tensorflow                    1.11.0 gpu_py36h4459f94_0  pkgs/main           
tensorflow                    1.11.0 gpu_py36h9c9050a_0  pkgs/main           
...        
tensorflow                    1.12.0 gpu_py36he68c306_0  pkgs/main           
tensorflow                    1.12.0 gpu_py36he74679b_0  pkgs/main
...         

注意 >=1.13.1 版本不支持 CUDA 9.

  1. conda env 中使用 pip install 来安装缺少的 tensorflow 构建,因为 pip 拥有更多构建组合:Tested build configurations

以下是 Anaconda 的一些最佳实践,如何使用 pip w/ condaUsing Pip in a Conda Environment

  1. 最后一个选项是使用 conda-build
  2. 构建您自己缺少的 conda