วิธีเรียกใช้การอนุมาน LLM บน GPU ของ Cloud Run ด้วย vLLM

1. บทนำ

ภาพรวม

Cloud Run เป็นแพลตฟอร์มคอนเทนเนอร์ใน Google Cloud ที่ช่วยให้คุณเรียกใช้โค้ดในคอนเทนเนอร์ได้อย่างตรงไปตรงมาโดยไม่ต้องจัดการคลัสเตอร์

Cloud Run มี GPU รุ่น L4 หรือ NVIDIA RTX PRO 6000 Blackwell มี GPU 1 รายการต่ออินสแตนซ์ Cloud Run และการปรับขนาดอัตโนมัติของ Cloud Run ยังคงมีผล รวมถึงการลดขนาดลงเหลือ 0 อินสแตนซ์เมื่อไม่มีคำขอ

กรณีการใช้งานหนึ่งสำหรับ GPU คือการเรียกใช้โมเดลภาษาขนาดใหญ่ (LLM) แบบเปิดของคุณเอง บทแนะนำนี้จะแนะนำขั้นตอนการติดตั้งใช้งานบริการที่เรียกใช้ LLM

Codelab นี้อธิบายวิธีติดตั้งใช้งานโมเดลแบบเปิด Gemma 4 ใน Cloud Run โดยใช้คอนเทนเนอร์ที่สร้างไว้ล่วงหน้าพร้อมไลบรารีการอนุมาน vLLM

สิ่งที่คุณจะได้เรียนรู้

  • วิธีใช้ GPU ใน Cloud Run
  • วิธีทำให้ใช้งานได้โมเดล Gemma 4 2B ของ Google ที่ได้รับการปรับแต่งคำสั่งบน Cloud Run โดยใช้ vLLM เป็นเครื่องมืออนุมาน

2. การตั้งค่าและข้อกำหนด

ข้อกำหนดเบื้องต้น

3. เปิดใช้ API และตั้งค่าตัวแปรสภาพแวดล้อม

เปิดใช้ API

ก่อนที่จะเริ่มใช้ Codelab นี้ได้ คุณจะต้องเปิดใช้ API หลายรายการ Codelab นี้กำหนดให้ใช้ API ต่อไปนี้ คุณเปิดใช้ API เหล่านั้นได้โดยการเรียกใช้คำสั่งต่อไปนี้

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

ตั้งค่าตัวแปรสภาพแวดล้อม

กำหนดค่ารหัสโปรเจ็กต์ด้านล่าง

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. ติดตั้งใช้งานบริการ

หากต้องการติดตั้งใช้งานโมเดล Gemma บน Cloud Run ให้ใช้คำสั่ง 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 ซึ่งจะเพิ่มโทเค็นรหัสให้โดยอัตโนมัติ หรือจะใช้ URL ของบริการโดยตรงก็ได้

การใช้บริการพร็อกซีสำหรับนักพัฒนาแอปของ Cloud Run

ก่อนอื่น ให้เริ่มพร็อกซี

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

เรียกใช้คำสั่งต่อไปนี้เพื่อส่งคำขอในแท็บเทอร์มินัลแยกต่างหาก โดยปล่อยให้พร็อกซีทำงานต่อไป พร็อกซีทำงานบน 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
 }
}

การใช้ URL ของบริการโดยตรง

ก่อนอื่น ให้ดึง URL ของบริการที่ใช้งาน

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

Curl บริการ

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. ยินดีด้วย

ขอแสดงความยินดีที่ทำ Codelab เสร็จสมบูรณ์

เราขอแนะนำให้อ่านเอกสารประกอบ Cloud Run

สิ่งที่เราได้พูดถึง

  • วิธีใช้ GPU ใน Cloud Run
  • วิธีติดตั้งใช้งานโมเดล Gemma 4 (2B) ของ Google บน Cloud Run โดยใช้ vLLM เป็นเครื่องมืออนุมาน

8. ล้างข้อมูล

หากต้องการหลีกเลี่ยงการเรียกเก็บเงินโดยไม่ตั้งใจ (เช่น หากมีการเรียกใช้บริการ Cloud Run โดยไม่ตั้งใจมากกว่าการจัดสรรการเรียกใช้ Cloud Run รายเดือนในระดับฟรี) คุณสามารถลบ Cloud Run หรือลบโปรเจ็กต์ที่สร้างในขั้นตอนที่ 2 ได้

หากต้องการลบบริการ Cloud Run ให้ไปที่ Cloud Console ของ Cloud Run ที่ 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