本教學課程會使用 vLLM TPU 服務架構,在 v6e TPU VM 上提供 Qwen/Qwen2-7B-Instruct 模型。
目標
- 設定環境。
- 使用 Qwen2-7B-Instruct 執行 vLLM。
- 傳送推論要求。
- 執行基準工作負載。
- 清除所用資源。
費用
本教學課程使用的 Google Cloud 計費元件包括:
您可以使用 Pricing Calculator 根據預測用量估算費用。
事前準備
在進行本教學課程前,請先按照「設定 Cloud TPU 環境」頁面的操作說明進行操作。按照指示建立 Google Cloud專案,並設定專案以使用 Cloud TPU。您也可以使用現有Google Cloud 專案。如果選擇這麼做,您可以略過建立Google Cloud 專案的步驟,直接從「設定環境以使用 Cloud TPU」開始。
如要使用本教學課程,您需要 Hugging Face 存取權杖。你可以在 Hugging Face 申請免費帳戶。建立帳戶後,請產生存取權杖:
設定環境
使用佇列資源 API 建立 Cloud TPU v6e VM。對於 qwen2-7b-instruct,建議使用 v6e-1 TPU。
export PROJECT_ID=YOUR_PROJECT_ID export TPU_NAME=qwen2-7b-instruct-tutorial export ZONE=us-east5-a export QR_ID=qwen2-7b-instruct-qr gcloud alpha compute tpus queued-resources create $QR_ID \ --node-id $TPU_NAME \ --project $PROJECT_ID \ --zone $ZONE \ --accelerator-type v6e-1 \ --runtime-version v2-alpha-tpuv6e確認 TPU VM 已準備就緒。
gcloud compute tpus queued-resources describe $QR_ID \ --project $PROJECT_ID \ --zone $ZONE建立 TPU VM 後,排入佇列的資源要求狀態會設為
ACTIVE。例如:name: projects/your-project-id/locations/your-zone/queuedResources/your-queued-resource-id state: state: ACTIVE tpu: nodeSpec: - node: acceleratorType: v6e-1 bootDisk: {} networkConfig: enableExternalIps: true queuedResource: projects/your-project-number/locations/your-zone/queuedResources/your-queued-resource-id runtimeVersion: v2-alpha-tpuv6e schedulingConfig: {} serviceAccount: {} shieldedInstanceConfig: {} useTpuVm: true nodeId: your-node-id parent: projects/your-project-number/locations/your-zone連線至 TPU VM。
gcloud compute tpus tpu-vm ssh $TPU_NAME \ --project $PROJECT_ID \ --zone $ZONE
使用 Qwen2-7B-instruct 執行 vLLM
在 TPU VM 內執行 vLLM Docker 容器。這項指令會使用 10 GB 的共用記憶體大小。
export DOCKER_URI=vllm/vllm-tpu:latest sudo docker run -it --rm --name $USER-vllm --privileged --net=host \ -v /dev/shm:/dev/shm \ --shm-size 10gb \ -p 8000:8000 \ --entrypoint /bin/bash ${DOCKER_URI}在容器內設定 Hugging Face 權杖。將
YOUR_HF_TOKEN替換成您的 Hugging Face 權杖。export HF_HOME=/dev/shm export HF_TOKEN=YOUR_HF_TOKEN使用
vllm serve指令啟動 vLLM 伺服器。export MAX_MODEL_LEN=4096 export TP=1 # number of chips vllm serve Qwen/Qwen2-7B-Instruct \ --seed 42 \ --disable-log-requests \ --gpu-memory-utilization 0.98 \ --max-num-batched-tokens 1024 \ --max-num-seqs 128 \ --tensor-parallel-size $TP \ --max-model-len $MAX_MODEL_LENvLLM 伺服器執行時,您會看到如下所示的輸出:
(APIServer pid=7) INFO: Started server process [7] (APIServer pid=7) INFO: Waiting for application startup. (APIServer pid=7) INFO: Application startup complete.
傳送推論要求
vLLM 伺服器執行後,您就可以從新的殼層傳送要求。
開啟新殼層並連線至 TPU VM。
export PROJECT_ID=YOUR_PROJECT_ID export TPU_NAME=qwen2-7b-instruct-tutorial export ZONE=us-east5-a gcloud compute tpus tpu-vm ssh $TPU_NAME \ --project $PROJECT_ID \ --zone=$ZONE開啟執行中 Docker 容器的殼層。
sudo docker exec -it $USER-vllm /bin/bash使用
curl將測試要求傳送至伺服器。curl http://localhost:8000/v1/completions \ -H "Content-Type: application/json" \ -d '{ "model": "Qwen/Qwen2-7B-Instruct", "prompt": "The future of AI is", "max_tokens": 200, "temperature": 0 }'
回應會以 JSON 格式傳回。
執行基準工作負載
您可以從第二個終端機針對執行中的伺服器執行基準測試。
在容器內安裝
datasets程式庫。pip install datasets請執行
vllm bench serve指令。export HF_HOME=/dev/shm cd /workspace/vllm vllm bench serve \ --backend vllm \ --model "Qwen/Qwen2-7B-Instruct" \ --dataset-name random \ --num-prompts 1000 \ --seed 100
效能評定結果如下所示:
============ Serving Benchmark Result ============
Successful requests: 1000
Benchmark duration (s): 45.35
Total input tokens: 1024000
Total generated tokens: 126848
Request throughput (req/s): 22.05
Output token throughput (tok/s): 2797.15
Peak output token throughput (tok/s): 4258.00
Peak concurrent requests: 1000.00
Total Token throughput (tok/s): 25377.57
---------------Time to First Token----------------
Mean TTFT (ms): 21332.46
Median TTFT (ms): 21330.37
P99 TTFT (ms): 42436.47
-----Time per Output Token (excl. 1st token)------
Mean TPOT (ms): 37.36
Median TPOT (ms): 38.56
P99 TPOT (ms): 38.69
---------------Inter-token Latency----------------
Mean ITL (ms): 37.35
Median ITL (ms): 38.55
P99 ITL (ms): 39.43
==================================================
清除所用資源
為避免因為本教學課程所用資源,導致系統向 Google Cloud 帳戶收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
- 在第二個殼層中輸入 exit,退出 vLLM 容器。
- 在第二個殼層中,輸入 exit 指令關閉終端機。
- 在第一個殼層中,輸入 Ctrl+C 停止 vLLM 伺服器。
- 在第一個殼層中輸入 exit,退出 vLLM 容器。
- 在第一個殼層中輸入 exit,中斷與 TPU VM 的連線。
刪除資源
您可以刪除專案 (這會一併刪除所有資源),也可以保留專案並刪除資源。
刪除專案
如要刪除 Google Cloud 專案和所有相關聯的資源,請執行下列指令:
gcloud projects delete $PROJECT_ID
刪除 TPU 資源
刪除 Cloud TPU 資源。下列指令會使用 --force 參數,同時刪除佇列資源要求和 TPU VM。
gcloud alpha compute tpus queued-resources delete $QR_ID \
--project=$PROJECT_ID \
--zone=$ZONE \
--force
後續步驟
- 進一步瞭解 Cloud TPU 上的 vLLM。
- 進一步瞭解 Cloud TPU。