部署使用 GPU 资源的容器实例

Important

该产品自 2025 年 7 月 14 日起停用。

若要在 Azure 容器实例上运行某些计算密集型工作负荷,请使用图形处理单元 (GPU) 资源部署容器组。 组中的容器实例可以在运行计算统一设备体系结构(CUDA)和深度学习应用程序等容器工作负载时访问一个或多个 NVIDIA Tesla GPU。

本文介绍如何使用 YAML 文件Azure 资源管理器模板(ARM 模板)部署容器组时添加 GPU 资源。 使用 Azure 门户部署容器实例时,还可以指定 GPU 资源。

Prerequisites

由于一些当前限制,所有限制增加请求都可能无法获得批准。

如果要将此版本用于生产容器部署,请创建 一个 Azure 支持请求 来增加限制。

预览限制

在预览版中,在容器组中使用 GPU 资源时,以下限制适用。

区域可用性

Regions OS 可用 GPU SKU
中国东部 2、中国北部 3 Linux V100

随着时间的推移,将为更多区域添加支持。

支持的 OS 类型:仅限 Linux。

其他限制:将容器组部署到 虚拟网络时,无法使用 GPU 资源。

关于 GPU 资源

计数和版本

若要在容器实例中使用 GPU,请使用以下信息指定 GPU 资源

  • 计数:GPU 数为 1、2 或 4。

  • 版本:GPU 版本为 V100。 每个版本映射到以下已启用 Azure GPU 的 VM 系列之一中的 NVIDIA Tesla GPU:

    版本 VM 系列
    V100 NCv3

每个 SKU 的最大资源数

OS GPU 型号 GPU 数量 最大 CPU 最大内存(GB) 存储器 (GB)
Linux V100 1 6 112 50
Linux V100 2 12 224 50
Linux V100 4 24 448 50

部署 GPU 资源时,请设置适用于工作负荷的 CPU 和内存资源,最高为上表中所示的最大值。 这些值当前大于容器组中可用的 CPU 和内存资源(不含 GPU 资源)。

Important

GPU 资源的默认 订阅限制 (配额)因版本而异。 V100 版本的默认 CPU 限制最初设置为 0。 若要请求增加可用区域,请提交 Azure 支持请求

使用须知

  • 部署时间:创建包含 GPU 资源的容器组最多需要 8 到 10 分钟。 在 Azure 中预配和配置 GPU 虚拟机(VM)需要更多时间。

  • 定价:与不使用 GPU 资源的容器组类似,Azure 会根据使用 GPU 资源的容器组 期间 使用的资源计费。 持续时间自容器开始拉取第一个容器的映像起开始计算,至容器组终止为止。 它不包括部署容器组的时间。

    有关详细信息,请参阅定价详细信息

  • CUDA 驱动程序:使用 NVIDIA CUDA 驱动程序和容器运行时预先预配具有 GPU 资源的容器实例,以便可以使用为 CUDA 工作负荷开发的容器映像。

    在此阶段,我们支持 CUDA 11。 例如,可以将以下基本映像用于 Docker 文件:

    若要提高从 Docker 中心使用公共容器映像时的可靠性,请导入和管理专用 Azure 容器注册表中的映像。 然后更新 Docker 文件以使用专用管理的基础映像。 了解有关使用公共映像的详细信息

YAML 示例

添加 GPU 资源的一种方式就是使用 YAML 文件部署容器组。 将以下 YAML 复制到名为 gpu-deploy-aci.yaml 的新文件中,然后保存该文件。 此 YAML 创建了一个名为 gpucontainergroup 的容器组,该组包含具有 V100 GPU 的容器实例。 该实例运行示例 CUDA 矢量添加应用程序。 请求的资源足以运行工作负荷。

Note

以下示例使用公共容器映像。 若要提高可靠性,请导入和管理专用 Azure 容器注册表中的映像。 然后更新您的 YAML 以使用您私有管理的基础映像。 了解有关使用公共映像的详细信息

additional_properties: {}
apiVersion: '2021-09-01'
name: gpucontainergroup
properties:
  containers:
  - name: gpucontainer
    properties:
      image: k8s-gcrio.azureedge.net/cuda-vector-add:v0.1
      resources:
        requests:
          cpu: 1.0
          memoryInGB: 1.5
          gpu:
            count: 1
            sku: V100
  osType: Linux
  restartPolicy: OnFailure

使用 az container create 命令部署容器组,并为参数指定 YAML 文件名 --file 。 需要为支持 GPU 资源的容器组(如 chinaeast2)提供资源组的名称和位置。

az container create --resource-group myResourceGroup --file gpu-deploy-aci.yaml --location chinaeast2

部署需要数分钟才能完成。 然后,容器启动并运行 CUDA 矢量添加操作。 运行 az container logs 命令,查看日志输出:

az container logs --resource-group myResourceGroup --name gpucontainergroup --container-name gpucontainer

Output:

[Vector addition of 50000 elements]
Copy input data from the host memory to the CUDA device
CUDA kernel launch with 196 blocks of 256 threads
Copy output data from the CUDA device to the host memory
Test PASSED
Done

资源管理器模板示例

使用 GPU 资源部署容器组的另一种方法是使用 ARM 模板。 首先创建一个名为gpudeploy.json的文件。 然后将以下 JSON 复制到其中。 此示例使用 V100 GPU 部署容器实例,该 GPU 针对 MNIST 数据集运行 TensorFlow 培训作业。 请求的资源足以运行工作负荷。

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
      "containerGroupName": {
        "type": "string",
        "defaultValue": "gpucontainergrouprm",
        "metadata": {
          "description": "Container Group name."
        }
      }
    },
    "variables": {
      "containername": "gpucontainer",
      "containerimage": "mcr.microsoft.com/azuredocs/samples-tf-mnist-demo:gpu"
    },
    "resources": [
      {
        "name": "[parameters('containerGroupName')]",
        "type": "Microsoft.ContainerInstance/containerGroups",
        "apiVersion": "2021-09-01",
        "location": "[resourceGroup().location]",
        "properties": {
            "containers": [
            {
              "name": "[variables('containername')]",
              "properties": {
                "image": "[variables('containerimage')]",
                "resources": {
                  "requests": {
                    "cpu": 4.0,
                    "memoryInGb": 12.0,
                    "gpu": {
                        "count": 1,
                        "sku": "V100"
                  }
                }
              }
            }
          }
        ],
        "osType": "Linux",
        "restartPolicy": "OnFailure"
        }
      }
    ]
}

使用 az deployment group create 命令部署模板。 需要提供支持 GPU 资源的在区域(例如 chinaeast2)中创建的资源组的名称

az deployment group create --resource-group myResourceGroup --template-file gpudeploy.json

部署需要数分钟才能完成。 然后,容器启动并运行 TensorFlow 作业。 运行 az container logs 命令,查看日志输出:

az container logs --resource-group myResourceGroup --name gpucontainergrouprm --container-name gpucontainer

Output:

2018-10-25 18:31:10.155010: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2018-10-25 18:31:10.305937: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties:
name: Tesla V100 major: 3 minor: 7 memoryClockRate(GHz): 0.8235
pciBusID: ccb6:00:00.0
totalMemory: 11.92GiB freeMemory: 11.85GiB
2018-10-25 18:31:10.305981: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: Tesla V100, pci bus id: ccb6:00:00.0, compute capability: 3.7)
2018-10-25 18:31:14.941723: I tensorflow/stream_executor/dso_loader.cc:139] successfully opened CUDA library libcupti.so.8.0 locally
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting /tmp/tensorflow/input_data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting /tmp/tensorflow/input_data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting /tmp/tensorflow/input_data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting /tmp/tensorflow/input_data/t10k-labels-idx1-ubyte.gz
Accuracy at step 0: 0.097
Accuracy at step 10: 0.6993
Accuracy at step 20: 0.8208
Accuracy at step 30: 0.8594
...
Accuracy at step 990: 0.969
Adding run metadata for 999

清理资源

由于使用 GPU 资源可能很昂贵,因此请确保容器长时间不会意外运行。 在 Azure 门户中监视容器。 还可以使用 az container show 命令检查容器组的状态。 例如:

az container show --resource-group myResourceGroup --name gpucontainergroup --output table

使用完创建的容器实例后,请使用以下命令将其删除:

az container delete --resource-group myResourceGroup --name gpucontainergroup -y
az container delete --resource-group myResourceGroup --name gpucontainergrouprm -y