Cuda 12 + tf-nightly 2.12:在您的计算机上找不到cuda驱动程序,因此GPU将无法使用,尽管每个检查都正常,在torch中它可以工作。

37
  • tf-nightly版本 = 2.12.0-dev2023203
  • Python版本 = 3.10.6
  • CUDA驱动程序版本 = 525.85.12
  • CUDA版本 = 12.0
  • Cudnn版本 = 8.5.0
  • 我正在使用Linux(x86_64,Ubuntu 22.04)
  • 我在venv虚拟环境中使用Visual Studio Code进行编码

我正在尝试在GPU(NVIDIA GeForce RTX 3050)上运行一些模型,使用tensorflow nightly 2.12(以便能够使用Cuda 12.0)。我的问题是,显然我所做的每个检查似乎都是正确的,但最终脚本无法检测到GPU。我已经花了很多时间来尝试看看发生了什么事情,但似乎没有任何作用,因此任何建议或解决方案都将不胜感激。正如您在问题的最后可以看到的那样,GPU似乎对torch有效。

我将展示一些我在CUDA方面进行的最常见检查(Visual Studio Code终端),希望您会发现它们有用:

  1. 检查CUDA版本:

    $ nvcc --version

    nvcc: NVIDIA (R) Cuda compiler driver
    Copyright (c) 2005-2023 NVIDIA Corporation
    Built on Fri_Jan__6_16:45:21_PST_2023
    Cuda compilation tools, release 12.0, V12.0.140
    Build cuda_12.0.r12.0/compiler.32267302_0
    
  2. 检查与CUDA库的连接是否正确:

    $ echo $LD_LIBRARY_PATH

    /usr/cuda/lib
    
  3. 检查GPU的nvidia驱动程序并检查GPU是否可读取venv:

    $ nvidia-smi

    +-----------------------------------------------------------------------------+
    | NVIDIA-SMI 525.85.12    Driver Version: 525.85.12    CUDA Version: 12.0     |
    |-------------------------------+----------------------+----------------------+
    | 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 ...  On   | 00000000:01:00.0  On |                  N/A |
    | N/A   40C    P5     6W /  20W |     46MiB /  4096MiB |     22%      Default |
    |                               |                      |                  N/A |
    +-------------------------------+----------------------+----------------------+
    
    +-----------------------------------------------------------------------------+
    | Processes:                                                                  |
    |  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
    |        ID   ID                                                   Usage      |
    |=============================================================================|
    |    0   N/A  N/A      1356      G   /usr/lib/xorg/Xorg                 45MiB |
    +-----------------------------------------------------------------------------+
    
  4. 添加cuda/bin路径并检查:

    $ export PATH="/usr/local/cuda/bin:$PATH"

    $ echo $PATH

    /usr/local/cuda-12.0/bin:/home/victus-linux/Escritorio/MasterThesis_CODE/to_share/venv_master/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
    
  5. 自定义函数以检查CUDA是否正确安装:[function by Sherlock]

    function lib_installed() { /sbin/ldconfig -N -v $(sed 's/:/ /' <<< $LD_LIBRARY_PATH) 2>/dev/null | grep $1; }
    function check() { lib_installed $1 && echo "$1已安装" || echo "错误:$1未安装"; }
    check libcuda
    check libcudart
    
    libcudart.so.12 -> libcudart.so.12.0.146
            libcuda.so.1 -> libcuda.so.525.85.12
            libcuda.so.1 -> libcuda.so.525.85.12
            libcudadebugger.so.1 -> libcudadebugger.so.525.85.12
    libcuda已安装
            libcudart.so.12 -> libcudart.so.12.0.146
    libcudart已安装
    
  6. 自定义函数以检查Cudnn是否正确安装:[function by Sherlock]

    function lib_installed() { /sbin/ldconfig -N -v $(sed 's/:/ /' <<< $LD_LIBRARY_PATH) 2>/dev/null | grep $1; }
    function check() { lib_installed $1 &&
    
    所以,一旦我完成了这些先前的检查,我使用脚本来评估是否一切都最终正常,然后出现了以下错误:
    
    <pre class="lang-py prettyprint-override"><code>import tensorflow as tf
    
    print(f'\nTensorflow version = {tf.__version__}\n')
    print(f'\n{tf.config.list_physical_devices("GPU")}\n')
    

    2023-03-02 12:05:09.463343: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
    2023-03-02 12:05:09.489911: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
    2023-03-02 12:05:09.490522: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
    To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
    2023-03-02 12:05:10.066759: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
    
    Tensorflow version = 2.12.0-dev20230203
    
    2023-03-02 12:05:10.748675: I tensorflow/compiler/xla/stream_executor/cuda/cuda_gpu_executor.cc:996] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
    2023-03-02 12:05:10.771263: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1956] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
    Skipping registering GPU devices...
    
    []
    

    额外检查:我尝试在torch上运行一个检查脚本,在这里它可以工作,所以我猜问题与tensorflow/tf-nightly有关。

    import torch
    
    print(f'\nAvailable cuda = {torch.cuda.is_available()}')
    
    print(f'\nGPUs availables = {torch.cuda.device_count()}')
    
    print(f'\nCurrent device = {torch.cuda.current_device()}')
    
    print(f'\nCurrent Device location = {torch.cuda.device(0)}')
    
    print(f'\nName of the device = {torch.cuda.get_device_name(0)}')
    

    Available cuda = True
    
    GPUs availables = 1
    
    Current device = 0
    
    Current Device location = <torch.cuda.device object at 0x7fbe26fd2ec0>
    
    Name of the device = NVIDIA GeForce RTX 3050 Laptop GPU
    

    如果您知道任何可以帮助解决这个问题的信息,请不要犹豫告诉我。


嗯,注意pip3安装torch会带来很多cuda 11的包。 - arivero
4
tf.sysconfig.get_build_info() 显示 cuda 11,是吗?我猜测目前还没有支持 cuda12 的版本。 - arivero
1
@arivero 这是tf.sysconfig.get_build_info()的输出结果:_OrderedDict([('cpu_compiler', '/dt9/usr/bin/gcc'), ('cuda_compute_capabilities', ['sm_35', 'sm_50', 'sm_60', 'sm_70', 'sm_75', 'compute_80']), ('cuda_version', '11.8'), ('cudnn_version', '8'), ('is_cuda_build', True), ('is_rocm_build', False), ('is_tensorrt_build', True)])_。正如您所提到的,Cuda_version为11.8。我不明白的是为什么会出现这样的情况,考虑到tf夜间版应该与Cuda 12兼容。 - JaimeCorton
1
是的,我看到了问题,因此我已经为这个问题设置了赏金,希望有知识的人能告诉我们tf nightly是否可以自动选择11和12之间的版本。 - arivero
1
刚刚看到这个commit,tf-nightly已经迁移到了CUDA 12。 - undefined
5个回答

25

我认为截至2023年3月,唯一支持CUDA 12的TensorFlow发行版是NVIDIA的Docker包。

适用于CUDA 12的TensorFlow软件包应显示以下信息:

>>> tf.sysconfig.get_build_info() 
OrderedDict([('cpu_compiler', '/usr/bin/x86_64-linux-gnu-gcc-11'), 
('cuda_compute_capabilities', ['compute_86']), 
('cuda_version', '12.0'), ('cudnn_version', '8'), 
('is_cuda_build', True), ('is_rocm_build', False), ('is_tensorrt_build', True)])

但是,如果我们在通过pip安装的任何tensorflow包上运行tf.sysconfig.get_build_info(),它仍然会告诉我们cuda_version为11.x

因此,您的选择是:

  • 按照nvidia云指南安装docker,并运行最近的容器之一
  • 从源代码编译tensorflow,无论是夜间版还是最新版本。注意,这需要大量RAM和一些时间,就像所有好的编译一样,并且偶尔需要在运行时纠正错误。在我的情况下,要定义kFP8,即新的8位浮点数。
  • 等待

请注意,我不太确定兼容性前向如何工作。例如,我有一个构建版本声称支持cuda 11.2,它可以在安装了11.8和12.0的混合环境中运行。 - arivero
4
一个月后,我现在可以通过首先卸载pip tensorflow软件包,然后运行sudo pacman -S tensorflow-cuda python-tensorflow-cuda来从pip切换到arch存储库版本。这使我获得了“cuda_version”,“12.1”并修复了“libcudart.so.11.0”的加载错误。 - Joel Sjögren
另一个选择是从链接 https://developer.nvidia.com/cuda-11-8-0-download-archive安装cuda-11-8(例如)。如果您使用WSL2,Windows上的CUDA安装需要与您在WSL2下运行的Linux发行版相匹配。对我来说,这比编译TensorFlow或使用docker要简单得多。 - George

5

我也遇到了同样的问题,可以通过安装TensorFlowRT来解决。

  1. pip3 install nvidia-tensorrt
  2. 再次检查libnvinfer.*文件链接,并确保LD_LIBRARY_PATH指向安装目录。
  3. 参考:无法加载动态库'libnvinfer.so.7'

在修复所有库之后,GPU输出将可见。

GPU visible


1
Tensor RT 在我这种情况下不是问题。看起来 @arivero 的答案指向了正确的方向。 - JaimeCorton
我认为也是如此。不过,仍然授予50分,因为它可能对其他人有用。 - arivero
不太有用,它并没有解决问题。您能否详细说明一下您是如何“绕过”需要不同 cuda 版本的要求的? - luisgrisolia
这可能适用于在Ubuntu Linux上使用旧的显卡。我有一张GTX 1050 Ti。安装命令延迟了一分钟,然后完成了。我重新启动了终端,并能够列出我的GPU设备。 - Joachim Rives

3

只是想再提供另一种选择,官方的pip3 install tensorflow在CUDA 12上也无法工作,所以我的解决方案是回到CUDA 11:

sudo apt install cuda-11-8

Tensorflow现在可用。


1
抱歉,Ken。我投反对票是因为我在最初的问题中已经非常清楚地表达了你在这里所说的内容。从一开始我就使用了tf-nightly版本,以便我可以使用CUDA 12(因为当时tensorflow不支持CUDA 11)。也许你的回答适用于另一个问题。祝你有美好的一天。 - JaimeCorton

0
一个更简单和更实时的解决方案 - 只需使用以下方式进行安装:
pip3 install tensorflow[and-cuda]

安装cuda-11库和tensorflow对我来说没有任何问题(ubuntu 22.04,RTX-4090)。

-1
我通过按照这个要点解决了我的TF GPU问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接