用于多个 GPU 的 theanork

theanorc for multiple GPUs

我有一台带 4 个 GPU 的 aws 机器:

00:03.0 VGA compatible controller: NVIDIA Corporation GK104GL [GRID K520] (rev a1)
00:04.0 VGA compatible controller: NVIDIA Corporation GK104GL [GRID K520] (rev a1)
00:05.0 VGA compatible controller: NVIDIA Corporation GK104GL [GRID K520] (rev a1)
00:06.0 VGA compatible controller: NVIDIA Corporation GK104GL [GRID K520] (rev a1)

我的 theanorc 文件如下所示:

[global]
floatX = float32
device = gpu0

[lib]
cnmem = 1

当我打开一个 jupyter notebook 并导入 theano 时,我得到以下信息(我假设只使用一个 GPU):

Using Theano backend.
Using gpu device 0: GRID K520 (CNMeM is enabled with initial size: 95.0% of memory, cuDNN 5105)
/home/sabeywardana/anaconda3/lib/python3.5/site-packages/theano/sandbox/cuda/__init__.py:600: UserWarning: Your cuDNN version is more recent than the one Theano officially supports. If you see any problems, try updating Theano or downgrading cuDNN to version 5.

但是,如果我同时在同一台机器上打开第二个 jupyter notebook。然后我得到错误:

ERROR (theano.sandbox.cuda): ERROR: Not using GPU. Initialisation of device 0 failed:
initCnmem: cnmemInit call failed! Reason=CNMEM_STATUS_OUT_OF_MEMORY. numdev=1

ERROR (theano.sandbox.cuda): ERROR: Not using GPU. Initialisation of device gpu failed:
initCnmem: cnmemInit call failed! Reason=CNMEM_STATUS_OUT_OF_MEMORY. numdev=1

如果我手动更改我的 .theanorc 以使用 gpu1,那么第二个 jupyter notebook 工作正常。 所以问题是:有没有办法配置 .theanorc 以仅获取可用的 GPU?

导入theano后无法更改gpu设备

也许你可以试试这个-

import os
os.system("THEANO_FLAGS='device=gpu0' python script_1.py")
os.system("THEANO_FLAGS='device=gpu1' python script_2.py")
os.system("THEANO_FLAGS='device=gpu1' python script_3.py")
os.system("THEANO_FLAGS='device=gpu1' python script_4.py")

如果您想从笔记本内部执行此操作(更具编程性),您可以使用以下代码段:-

import theano.sandbox.cuda
theano.sandbox.cuda.use("gpu0")

将此粘贴到每个笔记本并更改 gpu id。它会起作用。

您可以使用 device=gpu,这将 select 第一个可用的 GPU。 但是,在您的情况下,GPU 0 仍将被视为 "available"(它没有多少剩余内存,但仍然可以执行)。您可以使用 nvidia-smi 将您的 GPU 的计算模式设置为 "Exclusive Thread",这样第一个笔记本 "blocks" 第一个 GPU 独占使用,第二个笔记本将使用另一个。

另一种选择是在导入 theano 之前从笔记本内部更改 THEANO_FLAGS 环境变量。类似于:

import os

os.environ['THEANO_FLAGS'] = os.environ.get('THEANO_FLAGS', '') + ',' + 'device=gpu1'

import theano