在容器中使用 GPU 一直是使用 Compose 的一個痛點!
在面向 AI 開發的大趨勢下,容器化可以將環境無縫遷移,將配置環境的成本無限降低。但是,在容器中配置 CUDA 並運行 TensorFlow 一段時間內確實是個比較麻煩的時候,所以我們這裡就介紹和使用它。
在 Compose 中使用 GPU 資源
如果我們部署 Docker 服務的的主機上正確安裝並設置了其對應配置,且該主機上恰恰也有對應的 GPU 顯卡,那麼就可以在 Compose 中來定義和設置這些 GPU 顯卡了。# 需要安裝的配置
$ apt-get install nvidia-container-runtime# runtime
$ docker run --runtime=nvidia --rm nvidia/cuda nvidia-smi# with --gpus
$ docker run -it --rm --gpus all ubuntu nvidia-smi
# use device
$ docker run -it --rm --gpus \
device=GPU-3a23c669-1f69-c64e-cf85-44e9b07e7a2a \
ubuntu nvidia-smi
# specific gpu
$ docker run -it --rm --gpus '"device=0,2"' ubuntu nvidia-smi
# set nvidia capabilities
$ docker run --gpus 'all,capabilities=utility' --rm ubuntu nvidia-smi
對應 Compose 工具的老版本(v2.3)配置文件來說的話,想要在部署的服務當中使用 GPU 顯卡資源的話,就必須使用 runtime 參數來進行配置才可以。雖然可以作為運行時為容器提供 GPU 的訪問和使用,但是在該模式下並不允許對 GPU 設備的特定屬性進行控制。services:
test:
image: nvidia/cuda:10.2-base
command: nvidia-smi
runtime: nvidia
environment:
- NVIDIA_VISIBLE_DEVICES=all在 Compose v1.28.0+ 的版本中,使用 Compose Specification 的配置文件寫法,並提供了一些可以更細粒度的控制 GPU 資源的配置屬性可被使用,因此可以在啟動的時候來精確表達我們的需求。咳咳咳,那這裡我們就一起看看吧!
指定需要支持的功能;可以配置多個不同功能;必須配置的欄位deploy:
resources:
reservations:
devices:
- capabilities: ["gpu"]
指定需要使用的GPU數量;值為int類型;與device_ids欄位二選一deploy:
resources:
reservations:
devices:
- capabilities: ["tpu"]
count: 2deploy:
resources:
reservations:
devices:
- capabilities: ["gpu"]
device_ids: ["0", "3"]deploy:
resources:
reservations:
devices:
- capabilities: ["gpu"]
device_ids: ["GPU-f123d1c9-26bb-df9b-1c23-4a731f61d8c7"]deploy:
resources:
reservations:
devices:
- capabilities: ["nvidia-compute"]
driver: nvidiadeploy:
resources:
reservations:
devices:
- capabilities: ["gpu"]
driver: gpuvendor
options:
virtualization: false咳咳咳,看也看了,說也說了,那我們就簡單的編寫一個示例文件,讓啟動的 cuda 容器服務來使用一個 GPU 設備資源,並運行得到如下輸出。
services:
test:
image: nvidia/cuda:10.2-base
command: nvidia-smi
deploy:
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
window: 120s
resources:
limits:
cpus: "0.50"
memory: 50M
reservations:
cpus: "0.25"
memory: 20M
devices:
- driver: nvidia
count: 1
capabilities: [gpu, utility]
update_config:
parallelism: 2
delay: 10s
order: stop-first
注意這裡,如果設置 count: 2 的話,就會下面的輸出中看到兩塊顯卡設置的信息。如果,我們這裡均未設置 count 或 device_ids 欄位的話,則默認情況下將主機上所有 GPU 一同使用。# 前臺直接運行
$ docker-compose up
Creating network "gpu_default" with the default driver
Creating gpu_test_1 ... done
Attaching to gpu_test_1
test_1 | +--+
test_1 | | NVIDIA-SMI 450.80.02 Driver Version: 450.80.02 CUDA Version: 11.1 |
test_1 | |-+--+--+
test_1 | | GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
test_1 | | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
test_1 | | | | MIG M. |
test_1 | |===============================+======================+======================|
test_1 | | 0 Tesla T4 On | 00000000:00:1E.0 Off | 0 |
test_1 | | N/A 23C P8 9W / 70W | 0MiB / 15109MiB | 0% Default |
test_1 | | | | N/A |
test_1 | +-+--+--+
test_1 |
test_1 | +--+
test_1 | | Processes: |
test_1 | | GPU GI CI PID Type Process name GPU Memory |
test_1 | | ID ID Usage |
test_1 | |=============================================================================|
test_1 | | No running processes found |
test_1 | +--+
gpu_test_1 exited with code 0
當然,如果設置了 count 或 device_ids 欄位的話,就可以在容器裡面的程序中使用多塊顯卡資源了。可以通過以下部署配置文件來進行驗證和使用。services:
test:
image: tensorflow/tensorflow:latest-gpu
command: python -c "import tensorflow as tf;tf.test.gpu_device_name()"
deploy:
resources:
reservations:
devices:
- driver: nvidia
device_ids: ["0", "3"]
capabilities: [gpu]
運行結果,如下所示,我們可以看到兩塊顯卡均可以被使用到。# 前臺直接運行
$ docker-compose up
...
Created TensorFlow device (/device:GPU:0 with 13970 MB memory -> physical GPU (device: 0, name: Tesla T4, pci bus id: 0000:00:1b.0, compute capability: 7.5)
...
Created TensorFlow device (/device:GPU:1 with 13970 MB memory) -> physical GPU (device: 1, name: Tesla T4, pci bus id: 0000:00:1e.0, compute capability: 7.5)
...
gpu_test_1 exited with code 0本文轉載自:「 Ecsape 的博客 」,原文:http://t.cn/A6c6d4l1 ,版權歸原作者所有。歡迎投稿,投稿郵箱: editor@hi-linux.com。