เรียกใช้การอนุมานของโมเดล Gemma 4 บน Cloud Run ด้วย RTX 6000 Pro GPU ด้วย vLLM

1. บทนำ

ภาพรวม

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

  • วิธีติดตั้งใช้งานโมเดล Gemma 4 บน GPU ของ Cloud Run RTX 6000 Pro
  • วิธีใช้ vLLM และ Run:ai Model Streamer เพื่อการอนุมานที่เร็วขึ้นและการเริ่มต้นอินสแตนซ์ที่สั้นลง

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

ตั้งค่าตัวแปรสภาพแวดล้อมที่จะใช้ตลอด Codelab นี้

# Model name on HuggingFace Hub
export MODEL_NAME="google/gemma-4-31B-it"

# Cloud Run Service name
export SERVICE_NAME=gemma-rtx-vllm-codelab

# Cloud Project and Region for Cloud Run
export GOOGLE_CLOUD_PROJECT=<YOUR_PROJECT_ID> # Change to your Project Id
export GOOGLE_CLOUD_REGION=us-central1 # or europe-west4

# Optional HuggingFace User Access Token for accessing model weights 
# (https://huggingface.co/docs/hub/en/security-tokens),
# if you are loading a private model.
export HF_TOKEN=""

# Service account for Cloud Run service
export SERVICE_ACCOUNT="vllm-service-sa"
export SERVICE_ACCOUNT_EMAIL="${SERVICE_ACCOUNT}@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com"

# GCS Bucket for the model cache.
export MODEL_CACHE_BUCKET="${GOOGLE_CLOUD_PROJECT}-${GOOGLE_CLOUD_REGION}-hf-model-cache"
# Model cache location in GSC bucket
export GCS_MODEL_LOCATION="gs://${MODEL_CACHE_BUCKET}/model-cache/${MODEL_NAME}"

# VPC Network for Direct VPC Egress
export VPC_NETWORK="vllm-${GOOGLE_CLOUD_REGION}-net"
export VPC_SUBNET="vllm-${GOOGLE_CLOUD_REGION}-subnet"
export SUBNET_RANGE="10.8.0.0/26"

# set the project
gcloud config set project $GOOGLE_CLOUD_PROJECT
gcloud config set run/region $GOOGLE_CLOUD_REGION

เปิดใช้ API ที่จำเป็นสำหรับ Codelab นี้

gcloud services enable --project "${GOOGLE_CLOUD_PROJECT}" \
    run.googleapis.com \
    cloudbuild.googleapis.com \
    artifactregistry.googleapis.com \
    iam.googleapis.com \
    compute.googleapis.com \
    vpcaccess.googleapis.com \
    storage.googleapis.com

3. สร้างบัญชีบริการ

หากคุณไม่ได้ระบุบัญชีบริการเมื่อสร้างบริการหรืองาน Cloud Run ระบบจะใช้บัญชีบริการเริ่มต้นของ Compute Engine เราขอแนะนำให้ใช้บัญชีบริการแยกต่างหากสำหรับบริการ Cloud Run เพื่อหลีกเลี่ยงการเรียกใช้บริการที่มีสิทธิ์มากเกินไป

สร้างบัญชีบริการสำหรับบริการ Cloud Run

gcloud iam service-accounts create ${SERVICE_ACCOUNT} \
  --project "${GOOGLE_CLOUD_PROJECT}" \
  --display-name "vLLM Service Account"

4. ตั้งค่า Cloud Storage

สร้าง Bucket ของ Cloud Storage เพื่อจัดเก็บน้ำหนักของโมเดล ซึ่งจะช่วยให้ใช้ข้อมูลขาออก VPC โดยตรงเพื่อดาวน์โหลดน้ำหนักของโมเดลได้เร็วขึ้นทุกครั้งที่ Cloud Run เริ่มอินสแตนซ์บริการ

เมื่อใช้ร่วมกับฟีเจอร์ Run:ai Model Streamer ใน vLLM จะช่วยลดเวลาในการโหลดโมเดลได้อย่างมาก

สร้างที่เก็บข้อมูล

ตรวจสอบว่า Bucket เป็นแบบภูมิภาคเดียวที่ตั้งอยู่ร่วมกับบริการ Cloud Run

gcloud storage buckets create "gs://${MODEL_CACHE_BUCKET}" \
    --uniform-bucket-level-access --public-access-prevention \
    --project "${GOOGLE_CLOUD_PROJECT}" --location "${GOOGLE_CLOUD_REGION}"

5. เรียกข้อมูลและแคชน้ำหนักของโมเดล

จากนั้นดาวน์โหลดโมเดล Gemma 4 ไปยัง Bucket ของ Cloud Storage

น้ำหนักของโมเดลมีขนาดหลายสิบกิกะไบต์ และอาจดาวน์โหลดไปยังเครื่องในพื้นที่หรือ Cloud Shell ไม่ได้

แต่เราใช้ Cloud Build ที่มีพื้นที่เก็บข้อมูลเพียงพอสำหรับจัดเก็บน้ำหนักของโมเดลแทน

gcloud builds submit --project="${GOOGLE_CLOUD_PROJECT}" --region="${GOOGLE_CLOUD_REGION}" --no-source \
    --substitutions="_MODEL_NAME=${MODEL_NAME},_HF_TOKEN=${HF_TOKEN},_GCS_MODEL_LOCATION=${GCS_MODEL_LOCATION}" \
    --config=/dev/stdin <<'EOF'
steps:
- name: 'gcr.io/google.com/cloudsdktool/google-cloud-cli:slim'
  entrypoint: 'bash'
  args:
  - '-c'
  - |
    set -e
    pip3 install --root-user-action=ignore --break-system-packages huggingface_hub[cli]
    echo "Downloading the model..."
    if [[ "$_HF_TOKEN" != "" ]]; then
      hf download "$_MODEL_NAME" --token $_HF_TOKEN --local-dir "./model-cache/$_MODEL_NAME"
    else
      hf download "$_MODEL_NAME" --local-dir "./model-cache/$_MODEL_NAME"
    fi
    echo "Uploading the model..."
    gcloud storage cp -r "./model-cache/$_MODEL_NAME" "$_GCS_MODEL_LOCATION"
options:
  machineType: 'E2_HIGHCPU_32'
  diskSizeGb: 500
EOF

6. กำหนดค่าเครือข่ายสำหรับข้อมูลขาออก VPC โดยตรง

การกำหนดค่าขาออก VPC โดยตรงต้องสร้างเครือข่ายและซับเน็ตที่เปิดใช้การเข้าถึง Google แบบส่วนตัว

ซึ่งจะช่วยให้บริการ Cloud Run เชื่อมต่อกับชุดที่อยู่ IP ภายนอกที่ใช้โดย Google API และบริการต่างๆ รวมถึง Cloud Storage ได้

สร้างเครือข่าย

gcloud compute networks create "$VPC_NETWORK" \
        --subnet-mode=custom \
        --bgp-routing-mode=regional \
        --project "$GOOGLE_CLOUD_PROJECT"

สร้างซับเน็ต

gcloud compute networks subnets create "$VPC_SUBNET" \
        --network="$VPC_NETWORK" \
        --region="$GOOGLE_CLOUD_REGION" \
        --range="$SUBNET_RANGE" \
        --enable-private-ip-google-access \
        --project "$GOOGLE_CLOUD_PROJECT"

7. กำหนดค่านโยบายการเข้าถึงบัญชีบริการ

บัญชีบริการ Cloud Run ต้องมีสิทธิ์เข้าถึงน้ำหนักของโมเดลในที่เก็บข้อมูลที่คุณสร้างขึ้น

gcloud storage buckets add-iam-policy-binding "gs://${MODEL_CACHE_BUCKET}" \
    --member "serviceAccount:${SERVICE_ACCOUNT_EMAIL}" \
    --role "roles/storage.admin" \
    --project "${GOOGLE_CLOUD_PROJECT}"

8. เริ่มต้นตัวแปรการกำหนดค่า

กำหนดตัวแปรสำหรับทั้งเครื่องมืออนุมาน vLLM และบริการ Cloud Run

# vLLM variables
export MAX_MODEL_LEN=32767      # 32767 to improve concurrency. Keep it empty to use model's maximim context length (256K)
export QUANTIZATION_TYPE="fp8"  # Model quantization for faster performance and lower memory usage.
export KV_CACHE_DTYPE="fp8"     # KV-cache quantization to save GPU memory.
export GPU_MEM_UTIL="0.95"      # Fraction of GPU memory to be used by the vLLM engine.
export TENSOR_PARALLEL_SIZE="1" # Partitioning model across GPUs (1 here as we have only 1 GPU).
export MAX_NUM_SEQS=8           # Max concurrent requests vLLM processes in one batch.

# Cloud Run variables
export CLOUD_RUN_CPU_NUM=20
export CLOUD_RUN_MEMORY_GB=80
export CLOUD_RUN_MAX_INSTANCES=3
export CLOUD_RUN_CONCURRENCY=16

9. ทำให้ใช้งานได้กับ Cloud Run

เตรียมบรรทัดคำสั่งของคอนเทนเนอร์ vLLM

vLLM ต้องใช้พารามิเตอร์จำนวนมากเพื่อเรียกใช้โมเดลขนาดใหญ่ได้อย่างรวดเร็วและมีประสิทธิภาพ

CONTAINER_ARGS=(
    "vllm"
    "serve"
    "${GCS_MODEL_LOCATION}"
    "--served-model-name" "${MODEL_NAME}"
    "--enable-log-requests"
    "--enable-chunked-prefill"
    "--enable-prefix-caching"
    "--generation-config" "auto"
    "--enable-auto-tool-choice"
    "--tool-call-parser" "gemma4"
    "--reasoning-parser" "gemma4"
    "--dtype" "bfloat16"
    "--quantization" "${QUANTIZATION_TYPE}"
    "--kv-cache-dtype" "${KV_CACHE_DTYPE}"
    "--max-num-seqs" "${MAX_NUM_SEQS}"
    "--limit-mm-per-prompt" '{"image":4,"video":2}'
    "--gpu-memory-utilization" "${GPU_MEM_UTIL}"
    "--tensor-parallel-size" "${TENSOR_PARALLEL_SIZE}"
    "--load-format" "runai_streamer"
    "--port" "8080"
    "--host" "0.0.0.0"
)

if [[ "${MAX_MODEL_LEN}" != "" ]]; then
    CONTAINER_ARGS+=("--max-model-len" "${MAX_MODEL_LEN}")
fi

export CONTAINER_ARGS_STR="${CONTAINER_ARGS[*]}"

ติดตั้งใช้งานบริการ Cloud Run

เรียกใช้คำสั่งต่อไปนี้เพื่อทำให้บริการ Cloud Run ใช้งานได้

gcloud beta run deploy "${SERVICE_NAME}" \
    --image="us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-vllm-serve:gemma4" \
    --project "${GOOGLE_CLOUD_PROJECT}" \
    --region "${GOOGLE_CLOUD_REGION}" \
    --service-account "${SERVICE_ACCOUNT_EMAIL}" \
    --execution-environment gen2 \
    --no-allow-unauthenticated \
    --cpu="${CLOUD_RUN_CPU_NUM}" \
    --memory="${CLOUD_RUN_MEMORY_GB}Gi" \
    --gpu=1 \
    --gpu-type=nvidia-rtx-pro-6000 \
    --no-gpu-zonal-redundancy \
    --no-cpu-throttling \
    --max-instances ${CLOUD_RUN_MAX_INSTANCES} \
    --concurrency ${CLOUD_RUN_CONCURRENCY} \
    --network ${VPC_NETWORK} \
    --subnet ${VPC_SUBNET} \
    --vpc-egress all-traffic \
    --set-env-vars "MODEL_NAME=${MODEL_NAME}" \
    --set-env-vars "GOOGLE_CLOUD_PROJECT=${GOOGLE_CLOUD_PROJECT}" \
    --set-env-vars "GOOGLE_CLOUD_REGION=${GOOGLE_CLOUD_REGION}" \
    --startup-probe tcpSocket.port=8080,initialDelaySeconds=240,failureThreshold=1,timeoutSeconds=240,periodSeconds=240 \
    --command "bash" \
    --args="^;^-c;${CONTAINER_ARGS_STR}"

10. ทดสอบบริการ

เมื่อติดตั้งใช้งานแล้ว คุณจะโต้ตอบกับโมเดล Gemma 4 ได้โดยใช้ vLLM API ที่เข้ากันได้กับ OpenAI

รับ URL ของบริการ

ดึงข้อมูล URL ของบริการ Cloud Run ที่ทำให้ใช้งานได้

SERVICE_URL=$(gcloud run services describe $SERVICE_NAME --project "${GOOGLE_CLOUD_PROJECT}" --region "${GOOGLE_CLOUD_REGION}" --format 'value(status.url)')
echo "Service URL: $SERVICE_URL"

เรียกใช้การอนุมาน

ส่งพรอมต์ไปยังโมเดลโดยใช้ curl

curl -s "$SERVICE_URL/v1/chat/completions" \
  -H "Authorization: Bearer $(gcloud auth print-identity-token)" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "'"${MODEL_NAME}"'",
  "messages": [
    {"role": "user", "content": "Why is the sky blue?"}
  ],
  "chat_template_kwargs": {
    "enable_thinking": true
  },
  "skip_special_tokens": false
}' | jq -r '.choices[0].message.content'

11. ยินดีด้วย

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

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

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

  • วิธีติดตั้งใช้งานโมเดล Gemma 4 บน GPU ของ Cloud Run RTX 6000 Pro
  • วิธีกำหนดค่าข้อมูลขาออก VPC โดยตรงและการสตรีมโมเดล vLLM ด้วย Cloud Storage เพื่อให้บริการเริ่มต้นได้เร็วขึ้น

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

คุณสามารถลบโปรเจ็กต์หรือลบทรัพยากรแต่ละรายการเพื่อหลีกเลี่ยงการเรียกเก็บเงินจากบัญชี Google Cloud สำหรับทรัพยากรที่ใช้ในบทแนะนำนี้

ตัวเลือกที่ 1: ลบทรัพยากร

ลบบริการ Cloud Run

gcloud run services delete $SERVICE_NAME \
      --project "${GOOGLE_CLOUD_PROJECT}" \
      --region "${GOOGLE_CLOUD_REGION}"
      --quiet

ลบบัญชีบริการ

gcloud iam service-accounts delete \
      ${SERVICE_ACCOUNT_EMAIL} \
      --project "${GOOGLE_CLOUD_PROJECT}" \
      --quiet

ลบที่เก็บข้อมูล Cloud Storage

gcloud storage rm --recursive gs://$MODEL_CACHE_BUCKET

ลบเครือข่าย VPC และซับเน็ต

gcloud compute networks subnets delete $VPC_SUBNET \
    --region "${GOOGLE_CLOUD_REGION}" \
    --project "${GOOGLE_CLOUD_PROJECT}" \
    --quiet

gcloud compute networks delete $VPC_NETWORK \
    --project "${GOOGLE_CLOUD_PROJECT}" \
    --quiet

ตัวเลือกที่ 2: ลบโปรเจ็กต์

หากต้องการลบทั้งโปรเจ็กต์ ให้ไปที่จัดการทรัพยากร เลือกโปรเจ็กต์ที่คุณสร้างในขั้นตอนที่ 2 แล้วเลือก "ลบ" หากลบโปรเจ็กต์ คุณจะต้องเปลี่ยนโปรเจ็กต์ใน Cloud SDK คุณดูรายการโปรเจ็กต์ทั้งหมดที่พร้อมใช้งานได้โดยเรียกใช้ gcloud projects list