作者|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,
原文鏈接:https://www.analyticsvidhya.com/blog/2020/08/docker-based-python-development-with-cuda-support-on-pycharm-and-or-visual-studio-code/
歡迎關注磐創AI博客站:
http://panchuang.net/
sklearn機器學習中文官方檔案:
http://sklearn123.com/
歡迎關注磐創博客資源匯總站:
http://docs.panchuang.net/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/165405.html
標籤:其他
上一篇:基于K近鄰的電影推薦與分級預測
下一篇:并查集
