作者|GUEST
編譯|VK
來源|Analytics Vidhya
在PyCharm和Visual Studio代碼上支持CUDA
介紹
如果你沒有經驗,建立一個開發環境是不容易的,特別是如果你想學習的技術很多。
本教程旨在向你展示如何在PyCharm或Visual Studio代碼中設置一個基於Docker的Python開發環境,並支持CUDA。
免責聲明
-
在寫這篇文章的時候,我無法在Windows10 家庭版中使用Docker內部的CUDA(即使是內部版本),所以本教程是在考慮Linux的情況下實現的,盡管基本上沒有什么是特定於平台的。
-
只有在專業版上才能使用Docker作為PyCharm的遠程Python解釋器。
-
我假設你已經在你的機器上安裝了Docker。
-
我假設你已經在你的機器上安裝了CUDA。如果你仍在設置你的Linux機器,並且你不願意研究太多,我通常推薦Pop操作系統(https://pop.system76.com/)。在該文中(https://support.system76.com/articles/cuda/),你可以找到如何在他們的平台上非常容易地設置CUDA和cuDNN。文章還提供了在Ubuntu上使用他們的包的說明。
項目結構
在本教程中,我使用了一個只有3個文件的玩具項目:
生成容器的Dockerfile
。
requirements.txt
包含項目依賴項的文件。
run.py
包含一些要運行的代碼的文件。顯然,你的個人項目很可能更復雜,你可以使用不同的方法來管理依賴關系,你也可以使用docker-compose.yaml
但為了實現我的例子這會毫無意義引入復雜性。
Dockerfile文件
對於一篇更關注Docker和Dockerfiles的文章,我推薦Docker初學者指南:https://medium.com/codingthesmartway-com-blog/docker-beginners-guide-part-1-images-containers-6f3507fffc98
下面是我們的Dockerfile和一個簡短的注釋
FROM nvidia/cuda:10.2-devel
# 地址: https://github.com/ContinuumIO/docker-images/blob/master/miniconda3/debian/Dockerfile
ENV PATH /opt/conda/bin:$PATH
RUN apt-get update --fix-missing && \
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/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh && \
/bin/bash ~/miniconda.sh -b -p /opt/conda && \
rm ~/miniconda.sh && \
/opt/conda/bin/conda clean -tipsy && \
ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh && \
echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc && \
echo "conda activate base" >> ~/.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
# 項目設置
WORKDIR /code
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "./run.py"]
通俗地說,Dockerfile描述了生成Docker鏡像的過程,該鏡像隨后用於創建Docker容器。這個Dockerfile文件建立在nvidia/cuda:10.2-devel
,鏡像由NVIDIA直接在DockerHub中提供:https://hub.docker.com/r/nvidia/cuda。
nvidia/cuda:10.2-devel
是已經安裝了CUDA10.2工具包的開發鏡像
現在你只需要安裝Python開發所需的東西並設置我們的項目。
在Dockerfile的中間部分有一個Miniconda3安裝。我決定使用Miniconda而不是僅僅使用Python,因為它是我大多數項目的首選平台。
我們沒有利用Miniconda的任何功能,所以這有點過頭了。將Miniconda替換成Dockerfile中的Python,這是留給讀者的一個練習(不要驚慌,只需使用與新的Ubuntu設備相同的命令)。
最后一節是關於項目設置的,我們只是安裝依賴項,復制鏡像工作目錄中的所有文件,並選擇在沒有指定命令的情況下調用docker run時啟動的命令。
要構建Docker鏡像,只需使用你選擇的shell導航到包含Dockerfile的路徑並運行:
docker build -t <image_name> .
這將生成配置描述的Docker鏡像,並將其命名為image_name。如果在名稱中沒有指定標記,則使用最新的作為默認值。要指定標記,只需在冒號后寫入。
在本教程的其余部分中,我將使用pytorch-development-box
這個名稱。
requirements.txt
我只使用Pytorch和Torchvision作為這個項目的依賴項。我經常使用這些包,我會使用他們的CUDA可用性方法來檢查是否一切正常。所以我的requirements.txt是:
torch
torchvision
run.py
我的Python文件非常簡單,我只是檢查CUDA是否可用。
import torch.cuda
if torch.cuda.is_available():
print("CUDA is available :D")
else:
print("CUDA isn't available :(")
設置PyCharm
只有在PyCharm Professional上才能使用Docker的遠程Python解釋器。那么,讓我們看看如何設置它。
構建好Docker鏡像並在PyCharm中打開項目文件夾后,導航到File > Settings > Project > Python Interpreter。
你應該看到這樣的畫面:
現在單擊右上角附近的小齒輪並添加一個新的Python解釋器。
在這里,你需要選擇Docker並在名為image name的下拉菜單中選擇之前選擇的鏡像名稱,如下所示:
確認此配置后,請等待索引完成,然后嘗試運行run.py
.
CUDA isn't available :(
在這一點上,我們沒有配置讓Docker使用GPU,但我們可以快速修復它。
打開自動生成的運行/調試配置,並在Docker容器設置的末尾添加--gpus all
。
你應該得到這樣的結果:
確認此配置並運行它。CUDA結果現在可用!
設置Visual Studio代碼
我將依靠新的Visual Studio代碼的遠程開發擴展來設置通過Docker的開發。
第一步是安裝遠程開發擴展包並打開項目文件夾:https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack
使用VisualStudio palette中的“Add Development Container Configuration Files”命令。
選擇使用自己的Dockerfile。
此時devcontainer.json
文件將被創建到一個.devcontainer
目錄中,如下所示:
// For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.128.0/containers/docker-existing-dockerfile
{
"name": "Existing Dockerfile",
// Sets the run context to one level up instead of the .devcontainer folder.
"context": "..",
// Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
"dockerFile": "../Dockerfile",
// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.shell.linux": null
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": []
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Uncomment the next line to run commands after the container is created - for example installing curl.
// "postCreateCommand": "apt-get update && apt-get install -y curl",
// Uncomment when using a ptrace-based debugger like C++, Go, and Rust
// "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
// Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker.
// "mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ],
// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "vscode"
}
將彈出一個提示,要求在容器中重新打開該文件夾。
在此之前,我們只需要選擇一些在容器中開發時使用的擴展。
轉到Extensions選項卡,瀏覽你需要的擴展,你可以右鍵單擊並選擇Add to devcontainer。將它們添加到配置中。
現在我們只需要添加一個runArgs鍵來啟用GPU,我們就可以開始開發了。
減去注釋,你應該得到這樣的結果:
{
"name": "Existing Dockerfile",
"context": "..",
"dockerFile": "../Dockerfile",
"settings": {
"terminal.integrated.shell.linux": null
},
"extensions": [
"ms-python.python"
],
// This was added!
"runArgs": [
"--gpus=all"
]
}
現在從命令面板,我們可以重建和重新打開容器
結論
現在你已經在IDE中配置了一個非常基本的開發環境,它基於你自己的Docker鏡像,所有這些都支持GPU。
歡迎關注磐創AI博客站:
http://panchuang.net/
sklearn機器學習中文官方文檔:
http://sklearn123.com/
歡迎關注磐創博客資源匯總站:
http://docs.panchuang.net/