【问题标题】:How to conda install CUDA enabled PyTorch in a Docker container?如何在 Docker 容器中 conda 安装启用 CUDA 的 PyTorch?
【发布时间】:2021-04-06 02:28:27
【问题描述】:

我正在尝试在其中构建 conda 环境的服务器上构建 Docker 容器。除了启用 CUDA 的 PyTorch 之外,所有其他要求都得到了满足(我可以在没有 CUDA 的情况下让 PyTorch 工作,但是没问题)。如何确保 PyTorch 正在使用 CUDA?

这是Dockerfile

# Use nvidia/cuda image
FROM nvidia/cuda:10.2-cudnn7-devel-ubuntu18.04

# set bash as current shell
RUN chsh -s /bin/bash

# install anaconda
RUN apt-get update
RUN apt-get install -y wget bzip2 ca-certificates libglib2.0-0 libxext6 libsm6 libxrender1 git mercurial subversion && \
        apt-get clean
RUN wget --quiet https://repo.anaconda.com/archive/Anaconda3-2020.02-Linux-x86_64.sh -O ~/anaconda.sh && \
        /bin/bash ~/anaconda.sh -b -p /opt/conda && \
        rm ~/anaconda.sh && \
        ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh && \
        echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc && \
        find /opt/conda/ -follow -type f -name '*.a' -delete && \
        find /opt/conda/ -follow -type f -name '*.js.map' -delete && \
        /opt/conda/bin/conda clean -afy

# set path to conda
ENV PATH /opt/conda/bin:$PATH


# setup conda virtual environment
COPY ./requirements.yaml /tmp/requirements.yaml
RUN conda update conda \
    && conda env create --name camera-seg -f /tmp/requirements.yaml \
    && conda install -y -c conda-forge -n camera-seg flake8

# From the pythonspeed tutorial; Make RUN commands use the new environment
SHELL ["conda", "run", "-n", "camera-seg", "/bin/bash", "-c"]

# PyTorch with CUDA 10.2
RUN conda activate camera-seg && conda install pytorch torchvision cudatoolkit=10.2 -c pytorch

RUN echo "conda activate camera-seg" > ~/.bashrc
ENV PATH /opt/conda/envs/camera-seg/bin:$PATH

当我尝试构建此容器 (docker build -t camera-seg .) 时,这给了我以下错误:

.....

Step 10/12 : RUN conda activate camera-seg && conda install pytorch torchvision cudatoolkit=10.2 -c pytorch
 ---> Running in e0dd3e648f7b
ERROR conda.cli.main_run:execute(34): Subprocess for 'conda run ['/bin/bash', '-c', 'conda activate camera-seg && conda install pytorch torchvision cudatoolkit=10.2 -c pytorch']' command failed.  (See above for error)

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.



The command 'conda run -n camera-seg /bin/bash -c conda activate camera-seg && conda install pytorch torchvision cudatoolkit=10.2 -c pytorch' returned a non-zero code: 1

这是requirements.yaml

name: camera-seg
channels:
  - defaults
  - conda-forge
dependencies:
  - python=3.6
  - numpy
  - pillow
  - yaml
  - pyyaml
  - matplotlib
  - jupyter
  - notebook
  - tensorboardx
  - tensorboard
  - protobuf
  - tqdm

当我将pytorchtorchvisioncudatoolkit=10.2 放在requirements.yaml 中时,PyTorch 安装成功,但它无法识别 CUDA(torch.cuda.is_available() 返回False)。

我尝试了各种解决方案,例如thisthisthis 以及它们的一些不同组合,但都无济于事。

非常感谢任何帮助。谢谢。

【问题讨论】:

  • 我也有同样的问题。关于如何解决这个问题的任何想法没有使用pip安装torch?
  • @rubencart 您可以在需求文件中使用 conda 安装所有依赖项,对于 torch 和 torchvision,请在 Dockerfile 中明确输入。例如,在 Dockerfile 中安装 requirements.yaml 依赖项之后添加这一行:RUN conda install -y -n ${CONDA_ENV_NAME} -c pytorch pytorch torchvision cudatoolkit=10.2 ipython

标签: python-3.x docker anaconda pytorch


【解决方案1】:

经过多次尝试,我得到了它。在这里发布答案以防对任何人有帮助。

基本上,我像往常一样通过pip(从conda 环境中)安装了pytorchtorchvision,其余的依赖项通过conda 安装。

这就是最终的Dockerfile 的样子:

# Use nvidia/cuda image
FROM nvidia/cuda:10.2-cudnn7-devel-ubuntu18.04

# set bash as current shell
RUN chsh -s /bin/bash
SHELL ["/bin/bash", "-c"]

# install anaconda
RUN apt-get update
RUN apt-get install -y wget bzip2 ca-certificates libglib2.0-0 libxext6 libsm6 libxrender1 git mercurial subversion && \
        apt-get clean
RUN wget --quiet https://repo.anaconda.com/archive/Anaconda3-2020.02-Linux-x86_64.sh -O ~/anaconda.sh && \
        /bin/bash ~/anaconda.sh -b -p /opt/conda && \
        rm ~/anaconda.sh && \
        ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh && \
        echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc && \
        find /opt/conda/ -follow -type f -name '*.a' -delete && \
        find /opt/conda/ -follow -type f -name '*.js.map' -delete && \
        /opt/conda/bin/conda clean -afy

# set path to conda
ENV PATH /opt/conda/bin:$PATH


# setup conda virtual environment
COPY ./requirements.yaml /tmp/requirements.yaml
RUN conda update conda \
    && conda env create --name camera-seg -f /tmp/requirements.yaml

RUN echo "conda activate camera-seg" >> ~/.bashrc
ENV PATH /opt/conda/envs/camera-seg/bin:$PATH
ENV CONDA_DEFAULT_ENV $camera-seg

这就是requirements.yaml 的样子:

name: camera-seg
channels:
  - defaults
  - conda-forge
dependencies:
  - python=3.6
  - pip
  - numpy
  - pillow
  - yaml
  - pyyaml
  - matplotlib
  - jupyter
  - notebook
  - tensorboardx
  - tensorboard
  - protobuf
  - tqdm
  - pip:
    - torch
    - torchvision

然后我使用命令docker build -t camera-seg . 构建容器,PyTorch 现在能够识别 CUDA。

【讨论】:

  • 对 Cuda 11.0 做这件事有什么想法吗?我很想尝试为 11.0 调整您的解决方案,但不确定我是否想进入兔子洞 :)
  • 我没有意识到的一件事是 conda 安装了自己的 cuda,因此无需担心版本控制。这个解决方案对我来说很好。
  • @JohnCurry 你设法让一切正常吗?您也不需要使用 pip 安装 torch 和 torchvision。它可以通过在 Dockerfile 中的语句使用 conda 安装。请参阅我在问题中的评论。对于不同的 cuda 版本,我假设只是更改 cudatoolkit=11.0 应该与 Dockerfile 中第一行的更改一起工作:FROM nvidia/cuda:11.0-cudnn.....。不过我自己没试过。
  • 是的,一切正常。我最终没有更改为 11.0,因为我使用的代码无论如何都需要 10.2。非常有用的图片,谢谢!
猜你喜欢
  • 2020-04-21
  • 1970-01-01
  • 2018-09-29
  • 2017-11-11
  • 2021-06-18
  • 2019-02-17
  • 2017-06-04
  • 2022-01-18
  • 2019-08-02
相关资源
最近更新 更多