使用费用优化且高可用的 GPU 预配策略在 GKE 上提供 LLM


本指南介绍了如何在部署大语言模型 (LLM) 时优化工作负载费用。GKE 基础架构结合使用弹性启动型虚拟机、Spot 虚拟机和自定义计算类配置文件,以优化工作负载费用。

本指南以 Mixtral 8x7b 为例,介绍了可部署的 LLM。

本指南适用于机器学习 (ML) 工程师、平台管理员和运维人员,以及对使用 Kubernetes 容器编排功能提供 LLM 感兴趣的数据和 AI 专家。如需详细了解我们在 Google Cloud 内容中提及的常见角色和示例任务,请参阅常见的 GKE Enterprise 用户角色和任务

背景

本部分介绍了可用于根据 AI/机器学习工作负载的要求获取计算资源(包括 GPU 加速器)的各种方法。在 GKE 中,这些方法称为加速器可用性策略

GPU

利用图形处理器 (GPU),您可以加速特定工作负载,例如机器学习和数据处理。GKE 提供配备这些强大 GPU 的节点,以优化机器学习和数据处理任务的性能。GKE 提供了一系列机器类型选项以用于节点配置,包括配备 NVIDIA H100、A100 和 L4 GPU 的机器类型。

如需了解详情,请参阅 GKE 中的 GPU 简介

灵活启动预配模式

弹性启动预配模式是一种 GPU 预留,在这种模式下,GKE 会保留您的 GPU 请求,并在容量可用时自动预配资源。对于需要 GPU 容量一段时间(最长 7 天)且没有固定开始日期的工作负载,不妨考虑使用“灵活开始时间”选项。如需了解详情,请参阅 flex-start

Spot 虚拟机

如果您的工作负载可以容忍频繁的节点中断,您可以将 GPU 用于 Spot 虚拟机。使用 Spot 虚拟机或弹性启动可降低运行 GPU 的费用。将 Spot 虚拟机与 flex-start 搭配使用,可在没有 Spot 虚拟机容量可用时提供回退选项。

如需了解详情,请参阅将 Spot 虚拟机与 GPU 节点池搭配使用

自定义计算类

您可以使用自定义计算类来请求 GPU。借助自定义计算类,您可以定义节点配置的层次结构,以便 GKE 在节点伸缩决策期间确定优先级,从而确保工作负载在所选硬件上运行。如需了解详情,请参阅自定义计算类简介

准备工作

  • Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  • In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  • Make sure that billing is enabled for your Google Cloud project.

  • In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  • Make sure that billing is enabled for your Google Cloud project.

  • Make sure that you have the following role or roles on the project:

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往 IAM
    2. 选择项目。
    3. 点击 授予访问权限
    4. 新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。

    5. 选择角色列表中,选择一个角色。
    6. 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
    7. 点击 Save(保存)。

获取对模型的访问权限

如果您还没有 Hugging Face 令牌,请生成一个新令牌:

  1. 点击您的个人资料 > 设置 > 访问令牌
  2. 选择新建令牌 (New Token)。
  3. 指定您选择的名称和一个至少为 Read 的角色。
  4. 选择生成令牌

创建自定义计算类配置文件

在本部分中,您将创建自定义计算类配置文件。自定义计算类配置文件用于定义工作负载使用的多个计算资源的类型及其之间的关系。

  1. 在 Google Cloud 控制台中,点击 Google Cloud 控制台中的 Cloud Shell 激活图标 激活 Cloud Shell 以启动 Cloud Shell 会话。 Google Cloud 控制台的底部窗格中会打开一个会话。
  2. 创建 dws-flex-start.yaml 清单文件:

    apiVersion: cloud.google.com/v1
    kind: ComputeClass
    metadata:
      name: dws-model-inference-class
    spec:
      priorities:
        - machineType: g2-standard-24
          spot: true
        - machineType: g2-standard-24
          flexStart:
            enabled: true
            nodeRecycling:
              leadTimeSeconds: 3600
      nodePoolAutoCreation:
        enabled: true
    
  3. 应用 dws-flex-start.yaml 清单:

    kubectl apply -f dws-flex-start.yaml
    

GKE 部署了配备 L4 加速器的 g2-standard-24 机器。 GKE 会先使用计算类优先使用 Spot 虚拟机,然后再优先使用弹性启动型虚拟机。

部署 LLM 工作负载

  1. 使用以下命令创建包含 Hugging Face 令牌的 Kubernetes Secret:

    kubectl create secret generic model-inference-secret \
        --from-literal=HUGGING_FACE_TOKEN=HUGGING_FACE_TOKEN \
        --dry-run=client -o yaml | kubectl apply -f -
    

    HUGGING_FACE_TOKEN 替换为您的 Hugging Face 访问令牌。

  2. 创建一个名为 mixtral-deployment.yaml 的文件:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: inference-mixtral-ccc
    spec:
      nodeSelector:
        cloud.google.com/compute-class: dws-model-inference-class
      replicas: 1
      selector:
        matchLabels:
          app: llm
      template:
        metadata:
          labels:
            app: llm
        spec:
          containers:
          - name: llm
            image: us-docker.pkg.dev/deeplearning-platform-release/gcr.io/huggingface-text-generation-inference-cu124.2-3.ubuntu2204.py311
            resources:
              requests:
                cpu: "5"
                memory: "40Gi"
                nvidia.com/gpu: "2"
              limits:
                cpu: "5"
                memory: "40Gi"
                nvidia.com/gpu: "2"
            env:
            - name: MODEL_ID
              value: mistralai/Mixtral-8x7B-Instruct-v0.1
            - name: NUM_SHARD
              value: "2"
            - name: PORT
              value: "8080"
            - name: QUANTIZE
              value: bitsandbytes-nf4
            - name: HUGGING_FACE_HUB_TOKEN
              valueFrom:
                secretKeyRef:
                  name: model-inference-secret
                  key: HUGGING_FACE_TOKEN
            volumeMounts:
              - mountPath: /dev/shm
                name: dshm
              - mountPath: /tmp
                name: ephemeral-volume
          volumes:
            - name: dshm
              emptyDir:
                  medium: Memory
            - name: ephemeral-volume
              ephemeral:
                volumeClaimTemplate:
                  metadata:
                    labels:
                      type: ephemeral
                  spec:
                    accessModes: ["ReadWriteOnce"]
                    storageClassName: "premium-rwo"
                    resources:
                      requests:
                        storage: 100Gi
    

    在此清单中,mountPath 字段设置为 /tmp,因为它是用于设置用于文本生成推理 (TGI) 的深度学习容器 (DLC) 中的 HF_HOME 环境变量的路径,而不是在 TGI 默认映像中设置的默认 /data 路径。下载的模型将存储在此目录中。

  3. 部署模型:

    kubectl apply -f  mixtral-deployment.yaml
    

    GKE 会调度一个新的 Pod 进行部署,这会触发节点池自动扩缩器在部署模型的第二个副本之前添加第二个节点。

  4. 验证模型的状态:

    watch kubectl get deploy inference-mixtral-ccc
    

    如果模型成功部署,则输出类似于以下内容:

    NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
    inference-mixtral-ccc  1/1     1            1           10m
    

    如需退出监控,请按 CTRL + C

  5. 查看 GKE 预配的节点池:

    kubectl get nodes -L cloud.google.com/gke-nodepool
    

    输出类似于以下内容:

      NAME                                                  STATUS   ROLES    AGE   VERSION               GKE-NODEPOOL
      gke-flex-na-nap-g2-standard--0723b782-fg7v   Ready    <none>   10m   v1.32.3-gke.1152000   nap-g2-standard-24-spot-gpu2-1gbdlbxz
      gke-flex-nap-zo-default-pool-09f6fe53-fzm8   Ready    <none>   32m   v1.32.3-gke.1152000   default-pool
      gke-flex-nap-zo-default-pool-09f6fe53-lv2v   Ready    <none>   32m   v1.32.3-gke.1152000   default-pool
      gke-flex-nap-zo-default-pool-09f6fe53-pq6m   Ready    <none>   32m   v1.32.3-gke.1152000   default-pool
    

    创建的节点池的名称表示机器类型。在本例中,GKE 预配了 Spot 虚拟机。

使用 curl 与模型交互

本部分介绍如何执行基本推理测试来验证所部署的模型。

  1. 设置到模型的端口转发:

    kubectl port-forward service/llm-service 8080:8080
    

    输出类似于以下内容:

    Forwarding from 127.0.0.1:8080 -> 8080
    
  2. 在新的终端会话中,使用 curl 与模型聊天:

    curl http://localhost:8080/v1/completions \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{
        "model": "mixtral-8x7b-instruct-gptq",
        "prompt": "<s>[INST]Who was the first president of the United States?[/INST]",
        "max_tokens": 40}'
    

    输出类似于以下内容:

    George Washington was a Founding Father and the first president of the United States, serving from 1789 to 1797.
    

清理

为避免因本页面中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留该项目但删除各个资源。

删除项目

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

逐个删除资源

  1. 删除您在本指南中创建的 Kubernetes 资源:

    kubectl delete deployment inference-mixtral-ccc
    kubectl delete service llm-service
    kubectl delete computeclass dws-model-inference-class
    kubectl delete secret model-inference-secret
    
  2. 删除集群:

    gcloud container clusters delete CLUSTER_NAME
    

后续步骤