使用 SkyPilot 部署和扩展#

vLLM

vLLM 可以使用 SkyPilot 在云和 Kubernetes 上 运行并扩展到多个服务副本,这是一个用于在任何云上运行 LLM 的开源框架。更多关于各种开源模型(如 Llama-3、Mixtral 等)的示例可以在 SkyPilot AI 库 中找到。

先决条件#

  • 访问 HuggingFace 模型页面 并请求访问模型 meta-llama/Meta-Llama-3-8B-Instruct。

  • 检查你是否已安装 SkyPilot(文档)。

  • 检查 sky check 是否显示云或 Kubernetes 已启用。

pip install skypilot-nightly
sky check

在单个实例上运行#

查看用于服务的 vLLM SkyPilot YAML,serving.yaml。

resources:
  accelerators: {L4, A10g, A10, L40, A40, A100, A100-80GB} # We can use cheaper accelerators for 8B model.
  use_spot: True
  disk_size: 512  # Ensure model checkpoints can fit.
  disk_tier: best
  ports: 8081  # Expose to internet traffic.

envs:
  MODEL_NAME: meta-llama/Meta-Llama-3-8B-Instruct
  HF_TOKEN: <your-huggingface-token>  # Change to your own huggingface token, or use --env to pass.

setup: |
  conda create -n vllm python=3.10 -y
  conda activate vllm

  pip install vllm==0.4.0.post1
  # Install Gradio for web UI.
  pip install gradio openai
  pip install flash-attn==2.5.7

run: |
  conda activate vllm
  echo 'Starting vllm api server...'
  python -u -m vllm.entrypoints.openai.api_server \
    --port 8081 \
    --model $MODEL_NAME \
    --trust-remote-code \
    --tensor-parallel-size $SKYPILOT_NUM_GPUS_PER_NODE \
    2>&1 | tee api_server.log &

  echo 'Waiting for vllm api server to start...'
  while ! `cat api_server.log | grep -q 'Uvicorn running on'`; do sleep 1; done

  echo 'Starting gradio server...'
  git clone https://github.com/vllm-project/vllm.git || true
  python vllm/examples/gradio_openai_chatbot_webserver.py \
    -m $MODEL_NAME \
    --port 8811 \
    --model-url http://localhost:8081/v1 \
    --stop-token-ids 128009,128001

在任何列出的候选 GPU(L4、A10g 等)上开始服务 Llama-3 8B 模型:

HF_TOKEN="your-huggingface-token" sky launch serving.yaml --env HF_TOKEN

检查命令的输出。将会有一个可共享的 gradio 链接(如以下的最后一行)。在你的浏览器中打开它,使用 LLaMA 模型进行文本补全。

(task, pid=7431) Running on public URL: https://<gradio-hash>.gradio.live

可选: 服务 70B 模型而不是默认的 8B 模型,并使用更多 GPU:

HF_TOKEN="your-huggingface-token" sky launch serving.yaml --gpus A100:8 --env HF_TOKEN --env MODEL_NAME=meta-llama/Meta-Llama-3-70B-Instruct

扩展到多个副本#

SkyPilot 可以通过内置的自动扩展、负载均衡和容错功能将服务扩展到多个服务副本。你可以在 YAML 文件中添加一个 services 部分来实现。

service:
  replicas: 2
  # An actual request for readiness probe.
  readiness_probe:
    path: /v1/chat/completions
    post_data:
    model: $MODEL_NAME
    messages:
      - role: user
        content: Hello! What is your name?
  max_tokens: 1
Click to see the full recipe YAML
service:
  replicas: 2
  # An actual request for readiness probe.
  readiness_probe:
    path: /v1/chat/completions
    post_data:
      model: $MODEL_NAME
      messages:
        - role