
2024年7月23日,Meta 发布了Llama 3.1 模型系列,包括一个 405B 参数的模型,提供基础模型和指令微调版本。Llama 3.1 405B 成为了第一个能与 GPT-4o 和 Claude 3.5 Sonnet 等顶级专有模型紧密竞争的开源 LLM。
本指南展示了如何使用SkyPilot和torchtune,在您自己的数据和基础设施上微调 Llama 3.1。所有内容都打包在一个简单的SkyPilot YAML中,您只需一个命令即可在您的基础设施上启动
- 本地 GPU 工作站
- Kubernetes 集群
- 云账户(支持 12 个云平台)

让我们微调 Llama 3.1
我们将使用torchtune来微调 Llama 3.1。下面的示例使用了yahma/alpaca-cleaned
数据集,您可以稍后将其替换为您自己的数据集。
要设置启动微调作业的环境,请先完成附录:准备工作部分。
微调作业被打包在一个 SkyPilot YAML 中。它可以在您自己的任何基础设施上启动,例如 Kubernetes 或任何云平台,使用相同的接口
用于微调 Llama 3.1 的 SkyPilot YAML:lora.yaml
# LoRA finetuning Meta Llama 3.1 on any of your own infra.
#
# Usage:
#
# HF_TOKEN=xxx sky launch lora.yaml -c llama31 --env HF_TOKEN
#
# To finetune a 70B model:
#
# HF_TOKEN=xxx sky launch lora.yaml -c llama31-70 --env HF_TOKEN --env MODEL_SIZE=70B
envs:
MODEL_SIZE: 8B
HF_TOKEN:
DATASET: "yahma/alpaca-cleaned"
# Change this to your own checkpoint bucket
CHECKPOINT_BUCKET_NAME: sky-llama-31-checkpoints
resources:
accelerators: A100:8
disk_tier: best
use_spot: true
file_mounts:
/configs: ./configs
/output:
name: $CHECKPOINT_BUCKET_NAME
mode: MOUNT
# Optionally, specify the store to enforce to use one of the stores below:
# r2/azure/gcs/s3/cos
# store: r2
setup: |
pip install torch torchvision
# Install torch tune from source for the latest Llama 3.1 model
pip install git+https://github.com/pytorch/torchtune.git@58255001bd0b1e3a81a6302201024e472af05379
# pip install torchtune
tune download meta-llama/Meta-Llama-3.1-${MODEL_SIZE}-Instruct \
--hf-token $HF_TOKEN \
--output-dir /tmp/Meta-Llama-3.1-${MODEL_SIZE}-Instruct \
--ignore-patterns "original/consolidated*"
run: |
tune run --nproc_per_node $SKYPILOT_NUM_GPUS_PER_NODE \
lora_finetune_distributed \
--config /configs/${MODEL_SIZE}-lora.yaml \
dataset.source=$DATASET
# Remove the checkpoint files to save space, LoRA serving only needs the
# adapter files.
rm /tmp/Meta-Llama-3.1-${MODEL_SIZE}-Instruct/*.pt
rm /tmp/Meta-Llama-3.1-${MODEL_SIZE}-Instruct/*.safetensors
mkdir -p /output/$MODEL_SIZE-lora
rsync -Pavz /tmp/Meta-Llama-3.1-${MODEL_SIZE}-Instruct /output/$MODEL_SIZE-lora
cp -r /tmp/lora_finetune_output /output/$MODEL_SIZE-lora/
在您的本地机器上运行以下命令
# Download the files for Llama 3.1 finetuning
git clone https://github.com/skypilot-org/skypilot
cd skypilot/llm/llama-3.1
export HF_TOKEN=xxxx
# It takes about 40 mins on 8 A100 GPUs to finetune a 8B
# Llama3.1 model with LoRA on Alpaca dataset.
sky launch -c llama31 lora.yaml \
--env HF_TOKEN --env MODEL_SIZE=8B \
--env CHECKPOINT_BUCKET_NAME="your-own-bucket-name"
要微调一个包含 70B 参数的更大模型,您只需按如下所示更改参数即可
sky launch -c llama31-70 lora.yaml \
--env HF_TOKEN --env MODEL_SIZE=70B \
--env CHECKPOINT_BUCKET_NAME="your-own-bucket-name"
微调 Llama 3.1 405B:正在进行中!如果您想关注这项工作,请加入 SkyPilot 社区 Slack 进行讨论。
使用您的自定义数据
上面的示例在 Alpaca 数据集(yahma/alpaca-cleaned
)上微调 Llama 3.1,但对于实际用例,您可能希望在您自己的数据集上进行微调。
您可以通过指定您的数据集的 huggingface 路径来实现,如下所示(我们在下面使用gbharti/finance-alpaca
作为示例)
# It takes about 1 hour on 8 A100 GPUs to finetune a 8B
# Llama3.1 model with LoRA on finance dataset.
sky launch -c llama31 lora.yaml \
--env HF_TOKEN --env MODEL_SIZE=8B \
--env CHECKPOINT_BUCKET_NAME="your-own-bucket-name" \
--env DATASET="gbharti/finance-alpaca"

部署微调后的模型
在您自己的数据集上微调训练好的 Llama 3.1 后,您现在可以使用一个简单的命令来部署微调后的模型
注意:
CHECKPOINT_BUCKET_NAME
应该是您在之前的微调步骤中用于存储检查点的存储桶名称。
sky launch -c serve-llama31 serve.yaml \
--env LORA_NAME="my-finance-lora" \
--env CHECKPOINT_BUCEKT_NAME="your-own-bucket-name"
您可以在终端中与模型交互
ENDPOINT=$(sky status --endpoint 8081 serve-llama31)
curl http://$ENDPOINT/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "my-finance-lora",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "For a car, what scams can be plotted with 0% financing vs rebate?"
}
]
}' | jq .
🎉 恭喜!您现在拥有一个在金融主题方面非常精通的微调版 Llama 3.1 8B 模型。总结一下,所有模型检查点和副本都保存在您自己的私有基础设施中。
用于部署微调模型的 SkyPilot YAML serve.yaml
# Serve a LoRA finetuned Meta Llama 3.1.
#
# Usage:
#
# HF_TOKEN=xxx sky launch serve.yaml -c llama31-serve --env HF_TOKEN
envs:
MODEL_SIZE: 8B
HF_TOKEN:
# Change this to your checkpoint bucket created in lora.yaml
CHECKPOINT_BUCKET_NAME: your-checkpoint-bucket
LORA_NAME: my-finance-lora
resources:
accelerators: L4
ports: 8081
cpus: 32+
file_mounts:
/checkpoints:
name: $CHECKPOINT_BUCKET_NAME
mode: MOUNT
setup: |
pip install vllm==0.5.3post1
pip install vllm-flash-attn==2.5.9.post1
pip install openai
run: |
vllm serve meta-llama/Meta-Llama-3.1-${MODEL_SIZE}-Instruct \
--tensor-parallel-size $SKYPILOT_NUM_GPUS_PER_NODE --enable-lora \
--lora-modules $LORA_NAME=/checkpoints/${MODEL_SIZE}-lora/Meta-Llama-3.1-${MODEL_SIZE}-Instruct/ \
--max-model-len=2048 --port 8081
附录:准备工作
申请访问 huggingface 上的 Llama 3.1 权重(点击蓝色框并按照步骤操作):
获取您的 huggingface 访问令牌:
将 huggingface 令牌添加到您的环境变量中
export HF_TOKEN="xxxx"
- 安装 SkyPilot 以启动微调
pip install skypilot-nightly[aws,gcp,kubernetes]
# or other clouds (12 clouds + kubernetes supported) you have setup
# See: https://docs.skypilot.org.cn/en/latest/getting-started/installation.html
- 检查您的基础设施设置
sky check
🎉 Enabled clouds 🎉
✔ AWS
✔ GCP
✔ Azure
✔ OCI
✔ Lambda
✔ RunPod
✔ Paperspace
✔ Fluidstack
✔ Cudo
✔ IBM
✔ SCP
✔ vSphere
✔ Cloudflare (for R2 object store)
✔ Kubernetes