如何使用 vLLM 在 Cloud Run GPU 上執行 LLM 推論

1. 簡介

總覽

Cloud Run 是 Google Cloud 的容器平台,可讓您輕鬆在容器中執行程式碼,不必管理叢集。

Cloud Run 提供 L4 或 NVIDIA RTX PRO 6000 Blackwell GPU。每個 Cloud Run 執行個體有一個 GPU,且 Cloud Run 自動調整功能仍適用,包括在沒有任何要求時將執行個體數縮減至零。

GPU 的用途之一是執行您自己的開放式大型語言模型 (LLM)。本教學課程會逐步引導您部署執行 LLM 的服務。

本程式碼研究室說明如何使用預建容器和 vLLM 推論程式庫,在 Cloud Run 上部署 Gemma 4 開放式模型

課程內容

  • 如何在 Cloud Run 使用 GPU。
  • 如何使用 vLLM 做為推論引擎,在 Cloud Run 上部署 Google 的 Gemma 4 2B 指令微調模型。

2. 設定和需求條件

必要條件

3. 啟用 API 並設定環境變數

啟用 API

開始進行本程式碼研究室之前,請先啟用數個 API。本程式碼研究室需要使用下列 API。執行下列指令即可啟用這些 API:

gcloud services enable run.googleapis.com \
    cloudbuild.googleapis.com \
    artifactregistry.googleapis.com

設定環境變數

在下方設定專案 ID。

export PROJECT_ID=<YOUR_PROJECT_ID>

export REGION=europe-west4
export SERVICE_NAME=gemma4-cr-codelab
export SERVICE_ACCOUNT_NAME=gemma4-cr-sa
export SERVICE_ACCOUNT_ADDRESS=$SERVICE_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com

4. 建立服務帳戶

這個服務帳戶代表 Cloud Run 服務。

gcloud iam service-accounts create $SERVICE_ACCOUNT_NAME \
  --display-name="Cloud Run gemma 4 SA"

5. 部署服務

如要在 Cloud Run 上部署 Gemma 模型,請使用下列 gcloud CLI 指令,並採用建議設定:

CONTAINER_ARGS=(
    "serve"
    "google/gemma-4-E2B-it"
    "--enable-chunked-prefill"
    "--enable-prefix-caching"
    "--generation-config=auto"
    "--enable-auto-tool-choice"
    "--tool-call-parser=gemma4"
    "--reasoning-parser=gemma4"
    "--dtype=bfloat16"
    "--max-num-seqs=64"
    "--gpu-memory-utilization=0.95"
    "--tensor-parallel-size=1"
    "--port=8080"
    "--host=0.0.0.0"
)
gcloud beta run deploy $SERVICE_NAME \
    --image "us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-vllm-serve:gemma4" \
    --project $PROJECT_ID \
    --region $REGION \
    --execution-environment gen2 \
    --no-allow-unauthenticated \
    --cpu 20 \
    --memory 80Gi \
    --gpu 1 \
    --gpu-type nvidia-rtx-pro-6000 \
    --no-gpu-zonal-redundancy \
    --no-cpu-throttling \
    --max-instances 3 \
    --concurrency 64 \
    --timeout 600 \
    --service-account $SERVICE_ACCOUNT_ADDRESS \
    --startup-probe tcpSocket.port=8080,initialDelaySeconds=240,failureThreshold=1,timeoutSeconds=240,periodSeconds=240 \
    --command "vllm" \
    --args=$(IFS=','; echo "${CONTAINER_ARGS[*]}")

6. 測試服務

部署完成後,您可以使用 Cloud Run 開發人員 Proxy 服務,系統會自動為您新增 ID 權杖,也可以直接使用 curl 服務網址。

使用 Cloud Run 開發 Proxy 服務

首先,啟動 Proxy

gcloud run services proxy $SERVICE_NAME \
  --project $PROJECT \
  --region $REGION \
  --port=9090

執行下列指令,在另一個終端機分頁中傳送要求,讓 Proxy 保持執行狀態。Proxy 會在 localhost:9090 上執行

curl http://localhost:9090/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "google/gemma-4-E2B-it",
    "messages": [{"role": "user", "content": "Why is the sky blue?"}],
    "chat_template_kwargs": {
         "enable_thinking": true
     },
     "skip_special_tokens": false
  }'

輸出結果應該會類似下列內容:

{
 "id": "chatcmpl-9cf1ab1450487047",
 "object": "chat.completion",
 "created": 1774904187,
 "model": "google/gemma-4-E2B-it",
 "choices": [
   {
     "index": 0,
     "message": {
       "role": "assistant",
       "content": "The short answer is a phenomenon called **Rayleigh scattering**...",
       "function_call": null,
       "tool_calls": [],
       "reasoning": "*   Question: \"Why is the sky blue?\"\n..."
     },
     "finish_reason": "stop",
     "stop_reason": 106
   }
 ],
 "usage": {
   "prompt_tokens": 21,
   "total_tokens": 877,
   "completion_tokens": 856
 }
}

直接使用服務網址

首先,請擷取已部署服務的網址。

SERVICE_URL=$(gcloud run services describe $SERVICE_NAME --region $REGION --format 'value(status.url)')

捲曲服務

curl $SERVICE_URL/v1/chat/completions \
  -H "Authorization: bearer $(gcloud auth print-identity-token)" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "google/gemma-4-E2B-it",
    "messages": [{"role": "user", "content": "Why is the sky blue?"}],
    "chat_template_kwargs": {
         "enable_thinking": true
     },
     "skip_special_tokens": false
  }'

7. 恭喜!

恭喜您完成本程式碼研究室!

建議您參閱 Cloud Run 說明文件

涵蓋內容

  • 如何在 Cloud Run 使用 GPU。
  • 如何使用 vLLM 做為推論引擎,在 Cloud Run 上部署 Google 的 Gemma 4 (2B) 模型。

8. 清理

為避免產生意外費用 (例如,Cloud Run 服務意外叫用次數超過免費層級的每月 Cloud Run 叫用次數配額),您可以刪除 Cloud Run,或刪除您在步驟 2 中建立的專案。

如要刪除 Cloud Run 服務,請前往 Cloud Run Cloud 控制台 (https://console.cloud.google.com/run),然後刪除 gemma4-cr-codelab 服務。您也可以刪除 gemma4-cr-codelab-sa 服務帳戶。

如要刪除整個專案,請前往 https://console.cloud.google.com/cloud-resource-manager,選取您在步驟 2 中建立的專案,然後選擇「刪除」。刪除專案後,您必須在 Cloud SDK 中變更專案。如要查看所有可用專案的清單,請執行 gcloud projects list