--Prepare the execution environment of Deep Learning by PyTorch with Docker container --Docker version 19.03 or later, if you have the GPU driver of the host OS and nvidia-container-runtime, the others will be confined in the container ――In most cases, you can reproduce the environment with a single command.
Roughly the following preparations are required.
--docker version 19.03 or later -See Official Documentation (this link is for Ubuntu)
CUDA and CuDNN are stored in the container, and the GPU Driver on the host side is used.
In this Dockerfile, Python 3.7.1 and the Python package are installed based on the image containing CUDA: 10.1 and CuDNN: 7 in ʻUbuntu: 18.04`.
FROM nvidia/cuda:10.1-cudnn7-devel-ubuntu18.04
WORKDIR /code
ENV PYTHON_VERSION 3.7.1
ENV HOME /root
ENV PYTHON_ROOT $HOME/local/python-$PYTHON_VERSION
ENV PATH $PYTHON_ROOT/bin:$PATH
ENV PYENV_ROOT $HOME/.pyenv
ENV TZ=Asia/Tokyo
ADD requirements.txt /code
RUN export DEBIAN_FRONTEND=noninteractive && \
apt-get -y update && \
apt-get -y upgrade && \
apt-get -y install tzdata
RUN apt-get -y install git make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev && \
apt-get -y install wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev && \
git clone https://github.com/pyenv/pyenv.git $PYENV_ROOT && \
$PYENV_ROOT/plugins/python-build/install.sh && \
/usr/local/bin/python-build -v $PYTHON_VERSION $PYTHON_ROOT && \
rm -rf $PYENV_ROOT && \
pip install -r requirements.txt
An example of the contents of requirements.txt
requirements.txt
torch
torchvision
First, build the container. Execute the following command in the directory where the Dockerfile is created.
docker build . -t torch3.7
Enter the container and check if the GPU can be recognized. If the following display appears with the nvidia-smi command, it is ok.
$ docker run -it --gpus all torch3.7 bash
$ nvidia-smi
Tue Nov 19 15:01:12 2019
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 430.50 Driver Version: 430.50 CUDA Version: 10.1 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 166... Off | 00000000:1F:00.0 Off | N/A |
| 45% 36C P0 1W / 120W | 0MiB / 5944MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+
Finally, confirm that PyTorch's torch.Tensor can be loaded in the memory on the GPU. I was able to use torch.Tensor on the GPU.
$ python
>>> import torch
>>> torch.cuda.is_available()
True
>>> x = torch.Tensor([0, 1])
>>> x
tensor([0., 1.])
>>> x.cuda()
tensor([0., 1.], device='cuda:0')
It seems that volume is often used synchronously between the host and the container. In that case, change the following command as appropriate and use it.
docker run -it -v /path/to/your_project_directory:/code --gpus all torch3.7 bash
Recommended Posts