本指南介绍了如何优化 GKE 上 LLM 服务工作负载的费用。本教程结合使用了灵活启动、Spot 虚拟机和自定义计算类配置文件,以实现经济高效的推理。
本指南以 Mixtral 8x7b 为例,说明您可以部署哪些 LLM。
本指南适用于机器学习 (ML) 工程师、平台管理员和运维人员,以及对使用 Kubernetes 容器编排功能提供 LLM 感兴趣的数据和 AI 专家。如需详细了解我们在 Trusted Cloud by S3NS 内容中提及的常见角色和示例任务,请参阅常见的 GKE Enterprise 用户角色和任务。
灵活启动价格
如果您的工作负载需要根据需要动态配置资源(最多 7 天,短期预留)、无需复杂的配额管理且可经济高效地访问,建议使用灵活启动。 灵活启动由动态工作负载调度器提供支持,并按照动态工作负载调度器价格计费:
- vCPU、GPU 和 TPU 可享受折扣(最高 53%)。
- 随用随付。
背景
本部分介绍了可用于根据 AI/ML 工作负载的要求获取计算资源(包括 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 虚拟机与灵活启动相结合,可在 Spot 虚拟机容量不可用时提供回退选项。
如需了解详情,请参阅将 Spot 虚拟机与 GPU 节点池搭配使用。
自定义计算类
您可以使用自定义计算类来请求 GPU。借助自定义计算类,您可以定义节点配置的层次结构,供 GKE 在做出节点扩缩决策时优先使用,以便工作负载在您选择的硬件上运行。如需了解详情,请参阅自定义计算类简介。
准备工作
-
In the Trusted Cloud console, on the project selector page, select or create a Trusted Cloud project.
-
Make sure that billing is enabled for your Trusted Cloud project.
-
Make sure that you have the following role or roles on the project:
Check for the roles
-
In the Trusted Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
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.
- 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
-
In the Trusted Cloud console, go to the IAM page.
前往 IAM - 选择项目。
- 点击 授予访问权限。
-
在新的主账号字段中,输入您的用户标识符。 这通常是员工身份池中的用户的标识符。如需了解详情,请参阅在 IAM 政策中表示员工池用户,或与您的管理员联系。
- 在选择角色列表中,选择一个角色。
- 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
- 点击 Save(保存)。
-
- 确保您拥有运行 1.32.2-gke.1652000 版或更高版本的 GKE Autopilot 或 Standard 集群。您的集群必须启用节点自动预配并配置 GPU 限制。
- 如果您还没有 Hugging Face 账号,请创建一个。
- 确保您的项目具有足够的 NVIDIA L4 GPU 配额。如需了解详情,请参阅 GPU 简介和分配配额。
获取对模型的访问权限
如果您还没有 Hugging Face 令牌,请生成一个新令牌:
- 点击您的个人资料 > 设置 > 访问令牌。
- 选择新建令牌 (New Token)。
- 指定您选择的名称和一个至少为
Read
的角色。 - 选择生成令牌。
创建自定义计算类配置文件
在本部分中,您将创建自定义计算类配置文件。自定义计算类配置文件定义了工作负载使用的多种计算资源之间的类型和关系。
- 在 Trusted Cloud 控制台中,点击 Trusted Cloud 控制台中的
激活 Cloud Shell 以启动 Cloud Shell 会话。会话会在 Trusted Cloud 控制台的底部窗格中打开。
创建
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
应用
dws-flex-start.yaml
清单:kubectl apply -f dws-flex-start.yaml
GKE 部署了 g2-standard-24
台配备 L4 加速器的机器。
GKE 使用计算类来优先使用 Spot 虚拟机,其次使用弹性启动虚拟机。
部署 LLM 工作负载
使用以下命令创建包含 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 访问令牌。创建一个名为
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
路径。下载的模型将存储在此目录中。部署模型:
kubectl apply -f mixtral-deployment.yaml
GKE 会调度一个新 Pod 进行部署,这会触发节点池自动扩缩器在部署模型的第二个副本之前添加第二个节点。
验证模型的状态:
watch kubectl get deploy inference-mixtral-ccc
如果模型已成功部署,则输出类似于以下内容:
NAME READY UP-TO-DATE AVAILABLE AGE inference-mixtral-ccc 1/1 1 1 10m
如需退出监控,请按
CTRL + C
。查看 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
与模型互动
本部分介绍如何执行基本推理测试以验证所部署的模型。
设置到模型的端口转发:
kubectl port-forward service/llm-service 8080:8080
输出类似于以下内容:
Forwarding from 127.0.0.1:8080 -> 8080
在新的终端会话中,使用
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.
清理
为避免因本页面中使用的资源导致您的 Trusted Cloud by S3NS 账号产生费用,请删除包含这些资源的项目,或者保留该项目但删除各个资源。
删除项目
- In the Trusted Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
逐个删除资源
删除您根据本指南创建的 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
删除集群:
gcloud container clusters delete CLUSTER_NAME
后续步骤
- 详细了解如何使用灵活启动训练小型工作负载。
- 详细了解 GKE 中的 GPU。