Paramiko SSH 连接无法使用 tensorflow 访问 GPU

Paramiko SSH connection does not have access to GPU with tensorflow

使用 Python 的 paramiko,我尝试通过 SSH 连接远程访问我拥有的服务器。该服务器有一个我通过 SSH 连接执行的 python 脚本(我实际上执行了一个调用此 python 脚本的 .sh 文件)。此 python 脚本的第一行是:

print("Devices:", tf.config.experimental.list_physical_devices())

这里我只看到:

设备:[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]

如果我在使用 PuTTY 打开 SSH 连接后执行相同的 .sh 文件,则同一行代码会生成以下结果:

设备:[PhysicalDevice(名称='/physical_device:CPU:0', device_type='CPU'), PhysicalDevice(名称='/physical_device:GPU:0', device_type='GPU')]

此处显示 GPU 可用。我想这与我打开 paramiko SSH 连接的方式有关。这是代码:

ssh = paramiko.SSHClient()  
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect(ip, port=port, username=u, password=pass)

cmd_to_execute = "./" + shFileName + ".sh"

ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd_to_execute)
    
for line in ssh_stdout:
    print(line.strip('\n'))
for line in ssh_stderr:
    print('ERR: ' + line.strip('\n'))
    
ssh.close()

为什么在使用 paramiko 建立 SSH 连接时我无法访问 GPU?

编辑 1

如果我允许显示 tensorflow 日志,我会收到此错误: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] 无法加载动态库 'libcudart.so.11.0'; dlerror:libcudart.so.11.0:无法打开共享对象文件:没有这样的文件或目录

和其他看起来或多或少像这个但改变了 .11.0

另一方面,如果我使用 PuTTY,它会正确加载库: I tensorflow/stream_executor/platform/default/dso_loader.cc:53]成功打开动态库libcudart.so.11.0

根据 Martin Prikryl 和 DR 的评论。 Snoopy I 能够通过修改启动脚本为所有会话设置相同的 PATH 来解决问题。我通过修改两个文件来做到这一点。

$HOME/.profile文件原代码有这部分

if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

我将其更改为:

# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi

$HOME/.bashrc 原代码有这部分:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

我将其更改为:

# If not running interactively, don't do anything
#case $- in
#    *i*) ;;
#      *) return;;
#esac

现在以这种方式加载相同的库,而无需考虑我建立连接的方式。