本指南說明如何在 GKE 提升 LLM 服務工作負載的成本效益。本教學課程會結合使用彈性啟動型 VM、Spot VM 和 ComputeClass,以經濟實惠的方式進行推論。
本指南以 Mixtral 8x7b 為例,說明如何部署 LLM。
本指南適用於機器學習 (ML) 工程師、平台管理員和營運人員,以及有興趣使用 Kubernetes 容器協調功能提供 LLM 服務的資料和 AI 專家。如要進一步瞭解 Google Cloud 內容中提及的常見角色和工作範例,請參閱「常見的 GKE 使用者角色和工作」。
彈性啟動定價
如果工作負載需要視需求動態佈建資源,最多可使用短期預留項目七天,無需複雜的配額管理,且能以符合成本效益的方式存取,建議使用彈性啟動。彈性啟動功能採用 Dynamic Workload Scheduler,並按照 Dynamic Workload Scheduler 定價計費:
- vCPU、GPU、TPU 和任何附加的本機 SSD 磁碟可享折扣 (最高 53%)。
- 您會採用即付即用 (PAYG) 模式。
背景
本節說明可用的功能,您可根據 AI/機器學習工作負載的需求,使用這些功能取得運算資源,包括 GPU 或 TPU。這些功能可協助您實作相關策略,盡可能提高運算資源的彈性、效率和可用性。
GPU
圖形處理器 (GPU) 可加速處理特定工作負載,例如機器學習和資料處理。GKE 提供搭載這些強大 GPU 的節點,可提升機器學習和資料處理工作的效能。GKE 提供多種節點設定的機型選項,包括搭載 NVIDIA H100、A100 和 L4 GPU 的機型。
詳情請參閱「關於 GKE 中的 GPU」。
彈性啟動
彈性啟動模式採用 Dynamic Workload Scheduler,是一種 GPU 消耗選項。GKE 會保留您的 GPU 要求,並在容量可用時自動佈建彈性啟動型 VM。如果工作負載需要 GPU 資源的時間有限 (最多七天),且沒有固定開始日期,建議使用彈性啟動。詳情請參閱「flex-start」。
Spot VM
如果工作負載可容許節點頻繁中斷,您可以使用 GPU 和 Spot VM。使用 Spot VM 或彈性啟動 VM 可降低 GPU 執行價格。結合使用 Spot VM 和彈性啟動功能,可在 Spot VM 容量不足時提供備援選項。
詳情請參閱「搭配使用 Spot VM 和 GPU 節點集區」。
ComputeClasses
您可以使用 ComputeClass 要求 GPU。您可以透過 ComputeClass 定義節點設定的階層,供 GKE 在決定節點調度時優先採用,確保工作負載在您選取的硬體上執行。詳情請參閱「關於自訂 ComputeClass」。
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自體驗產品的實際應用成效。新客戶還能獲得價值 $300 美元的免費抵免額,能用於執行、測試及部署工作負載。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify 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.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
- 請確認您有執行 1.32.2-gke.1652000 以上版本的 GKE Autopilot 或 Standard 叢集。叢集必須啟用自動佈建節點功能並設定 GPU 限制。
- 如果還沒有帳戶,請建立 Hugging Face 帳戶。
- 請確認專案有足夠的 NVIDIA L4 GPU 搶占型配額。詳情請參閱「先占配額」。
取得模型存取權
如果沒有,請產生新的 Hugging Face 權杖:
- 依序點選「你的個人資料」>「設定」>「存取權杖」。
- 選取「New Token」。
- 指定想要的名稱和角色,角色至少需具有
Read權限。 - 選取「產生權杖」。
建立 ComputeClass
在本節中,您將建立自訂 ComputeClass。ComputeClass 會定義工作負載使用的多個運算資源類型和關係。
- 在 Google Cloud 控制台中,按一下
Google Cloud 控制台中的「啟用 Cloud Shell」,啟動 Cloud Shell 工作階段。工作階段會在 Google 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 會部署搭載 L4 加速器的g2-standard-24機器。GKE 會使用 ComputeClasses,優先使用 Spot VM,其次是彈性啟動型 VM。
部署 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: replicas: 1 selector: matchLabels: app: llm template: metadata: labels: app: llm spec: nodeSelector: cloud.google.com/compute-class: dws-model-inference-class 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.yamlGKE 會排定部署新 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。等待容器下載並開始提供模型:
watch "kubectl logs $(kubectl get pods -l app=llm -o custom-columns=:metadata.name --no-headers) | tail"如要退出手錶模式,請按
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 VM。
公開模型:
kubectl expose deployment/inference-mixtral-ccc --port 8080 --name=llm-service
使用 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.
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
刪除專案
- 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。
- 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
- 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。
刪除個別資源
刪除您在本指南中建立的 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。