当前位置:   article > 正文

使用 Docker Client 和 Go SDK 为容器分配 GPU 资源

docker api分配gpu

91f92142ddc18f6477ce6ca11dfbf2a5.gif

本文转自博客园,原文:https://www.cnblogs.com/joexu01/p/16539619.html,版权归原作者所有。欢迎投稿,投稿请添加微信好友:cloud-native-yang

背景

深度学习的环境配置通常是一项比较麻烦的工作,尤其是在多个用户共享的服务器上。虽然 conda 集成了 virtualenv 这样的工具用来隔离不同的依赖环境,但这种解决方案仍然没办法统一地分配计算资源。现在,我们可以通过容器技术为每个用户创建一个属于他们自己的容器,并为容器分配相应的计算资源。目前市面上基于容器的深度学习平台产品已经有很多了,比如超益集伦的 AiMax。这款产品本身集成了非常多的功能,但如果你只是需要在容器内调用一下 GPU,可以参考下面的步骤。

使用 Docker Client 调用 GPU

依赖安装

docker run --gpu 命令依赖于 nvidia Linux 驱动和 nvidia container toolkit,如果你想查看安装文档请点击这里[1],本节的下文只是安装文档的翻译和提示。

在 Linux 服务器上安装 nvidia 驱动非常简单,如果你安装了图形化界面的话直接在 Ubuntu 的“附加驱动”应用中安装即可,在 nvidia 官网上也可以下载驱动。

接下来就是安装 nvidia container toolkit,我们的服务器需要满足一些先决条件:

  • GNU/Linux x86_64 内核版本 > 3.10

  • Docker >= 19.03 (注意不是 Docker Desktop,如果你想在自己的台式机上使用 toolkit,请安装 Docker Engine 而不是 Docker Desktop,因为 Desktop 版本都是运行在虚拟机之上的)

  • NVIDIA GPU 架构 >= Kepler (目前 RTX20 系显卡是图灵架构,RTX30 系显卡是安培架构)

  • NVIDIA Linux drivers >= 418.81.07

然后就可以正式地在 Ubuntu 或者 Debian 上安装 NVIDIA Container Toolkit,**如果你想在 CentOS 上或者其他 Linux 发行版上安装,请参考官方的安装文档[2]**。

安装 Docker
  1. $ curl https://get.docker.com | sh \
  2.   && sudo systemctl --now enable docker

当然,这里安装完成后请参考官方的安装后需要执行的一系列操作[3]。如果安装遇到问题,请参照官方的安装文档[4]

安装 NVIDIA Container Toolkit¶

设置 Package Repository 和 GPG Key

  1. $ distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
  2.       && curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
  3.       && curl -s -L https://nvidia.github.io/libnvidia-container/$distribution/libnvidia-container.list | \
  4.             sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
  5.             sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list

请注意:如果你想安装 NVIDIA Container Toolkit 1.6.0 之前的版本,你应该使用 nvidia-docker repository 而不是上方的 libnvidia-container repositories。如果遇到问题请直接参考 安装手册[5]安装 nvidia-docker2 应该会自动安装 libnvidia-container-tools libnvidia-container1 等依赖包,如果没有安装可以 手动安装[6]

完成前面步骤后安装 nvidia-docker2

  1. $ sudo apt update 
  2. $ sudo apt install -y nvidia-docker2

重启 Docker Daemon

$ sudo systemctl restart docker

接下来你就可以通过运行一个 CUDA 容器测试下安装是否正确。

docker run --rm --gpus all nvidia/cuda:11.0.3-base-ubuntu20.04 nvidia-smi

Shell 中显示的应该类似于下面的输出:

  1. +-----------------------------------------------------------------------------+
  2. | NVIDIA-SMI 450.51.06    Driver Version: 450.51.06    CUDA Version: 11.0     |
  3. |-------------------------------+----------------------+----------------------+
  4. | GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
  5. | Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
  6. |                               |                      |               MIG M. |
  7. |===============================+======================+======================|
  8. |   0  Tesla T4            On   | 00000000:00:1E.0 Off |                    0 |
  9. | N/A   34C    P8     9W /  70W |      0MiB / 15109MiB |      0%      Default |
  10. |                               |                      |                  N/A |
  11. +-------------------------------+----------------------+----------------------+
  12. +-----------------------------------------------------------------------------+
  13. | Processes:                                                                  |
  14. |  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
  15. |        ID   ID                                                   Usage      |
  16. |=============================================================================|
  17. |  No running processes found                                                 |
  18. +-----------------------------------------------------------------------------+

--gpus 用法

注意,如果你安装的是 nvidia-docker2 的话,它在安装时就已经在 Docker 中注册了 NVIDIA Runtime。如果你安装的是 nvidia-docker ,请根据 官方文档[7] 向 Docker 注册运行时。如果你有任何疑问,请移步 本节参考的文档[8]

可以使用以 Docker 开头的选项或使用环境变量将 GPU 指定给 Docker CLI。此变量控制在容器内可访问哪些 GPU。

  • --gpus

  • NVIDIA_VISIBLE_DEVICES

可能的值描述
0,1,2 或者 GPU-fef8089b逗号分割的 GPU UUID(s) 或者 GPU 索引
all所有 GPU 都可被容器访问,默认值
none不可访问 GPU,但可以使用驱动提供的功能
void 或者 empty 或者 unsetnvidia-container-runtime will have the same behavior as (i.e. neither GPUs nor capabilities are exposed)runc

使用该选项指定 GPU 时,应使用该参数。参数的格式应封装在单引号中,后跟要枚举到容器的设备的双引号。例如:将 GPU 2 和 3 枚举到容器。--gpus '"device=2,3"'

使用 NVIDIA_VISIBLE_DEVICES 变量时,可能需要设置--runtime nvidia 除非已设置为默认值。

1、设置一个启用 CUDA 支持的容器

$ docker run --rm --gpus all nvidia/cuda nvidia-smi

2、指定 nvidia 作为运行时,并指定变量 NVIDIA_VISIBLE_DEVICES

  1. $ docker run --rm --runtime=nvidia \
  2.     -e NVIDIA_VISIBLE_DEVICES=all nvidia/cuda nvidia-smi

3、为启动的容器分配 2 个 GPU

$ docker run --rm --gpus 2 nvidia/cuda nvidia-smi

4、为容器指定使用索引为 1 和 2 的 GPU

  1. $ docker run --gpus '"device=1,2"' \
  2.  nvidia/cuda nvidia-smi --query-gpu=uuid --format=csv
  3. uuid
  4. GPU-ad2367dd-a40e-6b86-6fc3-c44a2cc92c7e
  5. GPU-16a23983-e73e-0945-2095-cdeb50696982

5、也可以使用 NVIDIA_VISIBLE_DEVICES

  1. $ docker run --rm --runtime=nvidia \
  2.  -e NVIDIA_VISIBLE_DEVICES=1,2 \
  3.  nvidia/cuda nvidia-smi --query-gpu=uuid --format=csv
  4. uuid
  5. GPU-ad2367dd-a40e-6b86-6fc3-c44a2cc92c7e
  6. GPU-16a23983-e73e-0945-2095-cdeb50696982

6、使用 nvidia-smi 查询 GPU UUID 然后将其指定给容器

  1. $ nvidia-smi -i 3 --query-gpu=uuid --format=csv
  2. uuid
  3. GPU-18a3e86f-4c0e-cd9f-59c3-55488c4b0c24
  4. docker run --gpus device=GPU-18a3e86f-4c0e-cd9f-59c3-55488c4b0c24 \ 
  5.  nvidia/cuda nvidia-smi

关于在容器内使用驱动程序的功能的设置,以及其他设置请参阅这里[9]

使用 Docker Go SDK 为容器分配 GPU

使用 NVIDIA/go-nvml 获取 GPU 信息

NVIDIA/go-nvml 提供 NVIDIA Management Library API (NVML) 的 Go 语言绑定。目前仅支持 Linux,仓库地址[10]

下面的演示代码获取了 GPU 的各种信息,其他功能请参考 NVML 和 go-nvml 的官方文档。

  1. package main
  2. import (
  3.  "fmt"
  4.  "github.com/NVIDIA/go-nvml/pkg/nvml"
  5.  "log"
  6. )
  7. func main() {
  8.  ret := nvml.Init()
  9.  if ret != nvml.SUCCESS {
  10.   log.Fatalf("Unable to initialize NVML: %v", nvml.ErrorString(ret))
  11.  }
  12.  defer func() {
  13.   ret := nvml.Shutdown()
  14.   if ret != nvml.SUCCESS {
  15.    log.Fatalf("Unable to shutdown NVML: %v", nvml.ErrorString(ret))
  16.   }
  17.  }()
  18.  count, ret := nvml.DeviceGetCount()
  19.  if ret != nvml.SUCCESS {
  20.   log.Fatalf("Unable to get device count: %v", nvml.ErrorString(ret))
  21.  }
  22.  for i := 0; i < count; i++ {
  23.   device, ret := nvml.DeviceGetHandleByIndex(i)
  24.   if ret != nvml.SUCCESS {
  25.    log.Fatalf("Unable to get device at index %d: %v", i, nvml.ErrorString(ret))
  26.   }
  27.   
  28.   // 获取 UUID 
  29.   uuid, ret := device.GetUUID()
  30.   if ret != nvml.SUCCESS {
  31.    log.Fatalf("Unable to get uuid of device at index %d: %v", i, nvml.ErrorString(ret))
  32.   }
  33.   fmt.Printf("GPU UUID: %v\n", uuid)
  34.   name, ret := device.GetName()
  35.   if ret != nvml.SUCCESS {
  36.    log.Fatalf("Unable to get name of device at index %d: %v", i, nvml.ErrorString(ret))
  37.   }
  38.   fmt.Printf("GPU Name: %+v\n", name)
  39.   memoryInfo, _ := device.GetMemoryInfo()
  40.   fmt.Printf("Memory Info: %+v\n", memoryInfo)
  41.   powerUsage, _ := device.GetPowerUsage()
  42.   fmt.Printf("Power Usage: %+v\n", powerUsage)
  43.   powerState, _ := device.GetPowerState()
  44.   fmt.Printf("Power State: %+v\n", powerState)
  45.   managementDefaultLimit, _ := device.GetPowerManagementDefaultLimit()
  46.   fmt.Printf("Power Managment Default Limit: %+v\n", managementDefaultLimit)
  47.   version, _ := device.GetInforomImageVersion()
  48.   fmt.Printf("Info Image Version: %+v\n", version)
  49.   driverVersion, _ := nvml.SystemGetDriverVersion()
  50.   fmt.Printf("Driver Version: %+v\n", driverVersion)
  51.   cudaDriverVersion, _ := nvml.SystemGetCudaDriverVersion()
  52.   fmt.Printf("CUDA Driver Version: %+v\n", cudaDriverVersion)
  53.   computeRunningProcesses, _ := device.GetGraphicsRunningProcesses()
  54.   for _, proc := range computeRunningProcesses {
  55.    fmt.Printf("Proc: %+v\n", proc)
  56.   }
  57.  }
  58.  fmt.Println()
  59. }

使用 Docker Go SDK 为容器分配 GPU

首先需要用的的是 ContainerCreate API

  1. // ContainerCreate creates a new container based in the given configuration.
  2. // It can be associated with a name, but it's not mandatory.
  3. func (cli *Client) ContainerCreate(
  4.  ctx context.Context, 
  5.  config *container.Config,
  6.  hostConfig *container.HostConfig,
  7.  networkingConfig *network.NetworkingConfig, 
  8.  platform *specs.Platform, 
  9.  containerName string) (container.ContainerCreateCreatedBody, error)

这个 API 中需要很多用来指定配置的 struct, 其中用来请求 GPU 设备的是 container.HostConfig 这个 struct 中的 Resources ,它的类型是 container.Resources ,而在它的里面保存的是 container.DeviceRequest 这个结构体的切片,这个变量会被 GPU 设备的驱动使用。

  1. cli.ContainerCreate API  需要 ---------> container.HostConfig{
  2.       Resources: container.Resources{
  3.        DeviceRequests: []container.DeviceRequest {
  4.         {
  5.          Driver:       "nvidia",
  6.          Count:        0,
  7.          DeviceIDs:    []string{"0"},
  8.          Capabilities: [][]string{{"gpu"}},
  9.          Options:      nil,
  10.         }
  11.        }
  12.       }
  13.      }

下面是 container.DeviceRequest 结构体的定义

  1. // DeviceRequest represents a request for devices from a device driver.
  2. // Used by GPU device drivers.
  3. type DeviceRequest struct {
  4.  Driver       string            // 设备驱动名称 这里就填写 "nvidia" 即可
  5.  Count        int               // 请求设备的数量 (-1 = All)
  6.  DeviceIDs    []string          // 可被设备驱动识别的设备ID列表,可以是索引也可以是UUID
  7.  Capabilities [][]string        // An OR list of AND lists of device capabilities (e.g. "gpu")
  8.  Options      map[string]string // Options to pass onto the device driver
  9. }

注意:如果指定了 Count 字段,就无法通过 DeviceIDs 指定 GPU,它们是互斥的。

接下来我们尝试使用 Docker Go SDK 启动一个 pytorch 容器。

首先我们编写一个 test.py 文件,让它在容器内运行,检查 CUDA 是否可用。

  1. # test.py
  2. import torch
  3. print("cuda.is_available:", torch.cuda.is_available())

下面是实验代码,启动一个名为 torch_test_1 的容器,并运行 python3 /workspace/test.py 命令,然后从 stdoutstderr 获取输出。

  1. package main
  2. import (
  3.  "context"
  4.  "fmt"
  5.  "github.com/docker/docker/api/types"
  6.  "github.com/docker/docker/api/types/container"
  7.  "github.com/docker/docker/client"
  8.  "github.com/docker/docker/pkg/stdcopy"
  9.  "os"
  10. )
  11. var (
  12.  defaultHost = "unix:///var/run/docker.sock"
  13. )
  14. func main() {
  15.  ctx := context.Background()
  16.  cli, err := client.NewClientWithOpts(client.WithHost(defaultHost), client.WithAPIVersionNegotiation())
  17.  if err != nil {
  18.   panic(err)
  19.  }
  20.  resp, err := cli.ContainerCreate(ctx,
  21.   &container.Config{
  22.    Image:     "pytorch/pytorch",
  23.    Cmd:       []string{},
  24.    OpenStdin: true,
  25.    Volumes:   map[string]struct{}{},
  26.    Tty:       true,
  27.   }, &container.HostConfig{
  28.    Binds: []string{`/home/joseph/workspace:/workspace`},
  29.    Resources: container.Resources{DeviceRequests: []container.DeviceRequest{{
  30.     Driver:       "nvidia",
  31.     Count:        0,
  32.     DeviceIDs:    []string{"0"},  // 这里填写GPU index 或者 GPU UUID 都可以
  33.     Capabilities: [][]string{{"gpu"}},
  34.     Options:      nil,
  35.    }}},
  36.   }, nilnil"torch_test_1")
  37.  if err != nil {
  38.   panic(err)
  39.  }
  40.  if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
  41.   panic(err)
  42.  }
  43.  fmt.Println(resp.ID)
  44.  execConf := types.ExecConfig{
  45.   User:         "",
  46.   Privileged:   false,
  47.   Tty:          false,
  48.   AttachStdin:  false,
  49.   AttachStderr: true,
  50.   AttachStdout: true,
  51.   Detach:       true,
  52.   DetachKeys:   "ctrl-p,q",
  53.   Env:          nil,
  54.   WorkingDir:   "/",
  55.   Cmd:          []string{"python3""/workspace/test.py"},
  56.  }
  57.  execCreate, err := cli.ContainerExecCreate(ctx, resp.ID, execConf)
  58.  if err != nil {
  59.   panic(err)
  60.  }
  61.  response, err := cli.ContainerExecAttach(ctx, execCreate.ID, types.ExecStartCheck{})
  62.  defer response.Close()
  63.  if err != nil {
  64.   fmt.Println(err)
  65.  }
  66.  // read the output
  67.  _, _ = stdcopy.StdCopy(os.Stdout, os.Stderr, response.Reader)
  68. }

可以看到,程序输出了创建的容器的 Contrainer ID 和 执行命令的输出。

  1. go build main.go 
  2. $ sudo ./main 
  3. 264535c7086391eab1d74ea48094f149ecda6d25709ac0c6c55c7693c349967b
  4. cuda.is_available: True

接下来使用 docker ps 查看容器状态。

  1. $ docker ps 
  2. CONTAINER ID   IMAGE             COMMAND   CREATED         STATUS             PORTS     NAMES
  3. 264535c70863   pytorch/pytorch   "bash"    2 minutes ago   Up 2 minutes                 torch_test_1

没问题,Container ID 对得上。

扩展阅读:NVIDIA Multi-Instance GPU

多实例 GPU (MIG) 功能允许将基于 NVIDIA Ampere 架构的 GPU(如 NVIDIA  A100)安全地分区为多达七个单独的 GPU 实例,用于 CUDA 应用程序,为多个用户提供单独的 GPU 资源,以实现最佳的 GPU  利用率。此功能对于未使 GPU 的计算容量完全饱和的工作负载特别有用,因此用户可能希望并行运行不同的工作负载以最大限度地提高利用率。

MIG 文档[11]

引用链接

[1]

这里: https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#installation-guide

[2]

安装文档: https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#installation-guide

[3]

安装后需要执行的一系列操作: https://docs.docker.com/engine/install/linux-postinstall/

[4]

安装文档: https://docs.docker.com/engine/install/ubuntu/

[5]

安装手册: https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#installation-guide

[6]

手动安装: https://github.com/NVIDIA/libnvidia-container

[7]

官方文档: https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/user-guide.html

[8]

本节参考的文档: https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/user-guide.html#environment-variables-oci-spec

[9]

这里: https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/user-guide.html#driver-capabilities

[10]

仓库地址: https://github.com/NVIDIA/go-nvml

[11]

MIG 文档: https://docs.nvidia.com/datacenter/tesla/mig-user-guide/index.html

1d230a2409472a7c473d5651fdfc8996.gif

a83cbe94a7de83381c10c01bf044b366.png

你可能还喜欢

点击下方图片即可阅读

使用 Golang 玩转 Bridge 与 NetNamespace 互联

2022-10-10

d2179c4bb8ae0c222bc924c09b83b4ee.jpeg

使用 Grafana Mimir 实现云原生监控报警可视化

2022-10-09

a9c98d47119dedf69ba821a596ce96a5.jpeg

突破运营商 QoS 封锁,WireGuard 真有“一套”!

2022-10-07

fd8d64ded5ce7c5f109b1fb467ee023b.jpeg

b94a8895fbcc29fbed573850988970d6.gif

云原生是一种信仰 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/707898
推荐阅读
相关标签