if multiple GPUs are in one server, it is sometimes tricky to select the best GPU. Especially, since the GPU ordering in nvidia-smi and the one used by the driver do not match.
This module can be imported before theano, and will select the GPU with minimal utilization.
pip install --process-dependency-links git+https://github.com/Maluuba/gpuselect@master#egg=gpuselect
For Python 3, ensure that your libboost_python
is compiled for Python 3.
If you are using conda, you can ensure this by doing conda install boost
in
your Python 3 environment.
import gpuselect
import theano
- ensure that
THEANO_FLAGS
or.theanorc
hasdevice=gpu
- if necessary, set
GPUSELECT_GPU_WEIGHT
and/orGPUSELECT_MEM_WEIGHT
environment variables. Default is 2 and 1, respectively.
The NVidia driver uses an ordering which seems to place faster GPUs first, while nvidia-smi and NVML use an ordering based on bus-ID. GPU-Utilization is best acquired using the NVML API.
Thus, the strategy employed by this script is:
- Get the busID of every device in driver-ordering using nvml
- Get gpu/memory utilization of device via nvml for given bus id
- Weight gpu/memory utilization
- Select the least utilized GPU,
X
- Append
device=gpuX
toTHEANO_FLAGS
. - You load theano and everything is good.