来源:llm/vicuna-llama-2

在 Llama-2 上训练您自己的 Vicuna#

Vicuna-Llama-2

Meta 两周前发布了 Llama 2,这在 AI 社区引起了巨大反响。在我们看来,其最大的影响是该模型现在在 宽松的许可 下发布,该许可允许模型权重用于商业用途[1]。这与 Llama 1 不同,Llama 1 不能用于商业用途。

Vicuna 是首批基于 Llama 1 微调的高质量大型语言模型 (LLM) 之一。我们,Vicuna 的共同创建者,更新了我们用于训练 Vicuna 的确切配方,使其基于 Llama 2,从而产生了本微调指南。

在本配方中,我们将展示如何使用 SkyPilot 在 Llama 2 上训练您自己的 Vicuna,轻松找到云上可用的 GPU,同时将成本降低到仅约 300 美元。

先决条件#

  1. 申请 Llama-2 模型访问权限

前往 申请页面 并申请模型权重的访问权限。

  1. 从 HuggingFace 获取访问令牌

在 HuggingFace 此处 生成一个只读访问令牌。前往 HuggingFace 的 Llama-2 模型页面 此处 并申请访问权限。确保您的 HuggingFace 邮箱与 Meta 申请上的邮箱一致。批准可能需要 1-2 天。

  1. 下载配方

git clone https://github.com/skypilot-org/skypilot.git
cd skypilot/llm/vicuna-llama-2

将访问令牌粘贴到 train.yaml

envs:
  HF_TOKEN: # TODO: Fill with your own huggingface token, or use --env to pass.

在 Llama-2 上训练您自己的 Vicuna#

训练数据和模型身份#

默认情况下,我们使用 ShareGPT 数据hardcoded_questions.py 中的身份问题。

可选:要使用自定义数据,您可以修改 train.yaml 中的以下行

setup: |
  ...
  wget https://hugging-face.cn/datasets/anon8231489123/ShareGPT_Vicuna_unfiltered/resolve/main/ShareGPT_V3_unfiltered_cleaned_split.json  -O $HOME/data/sharegpt.json
  ...

上面的 json 文件是一个数组,其每个元素的格式如下所示(对话可以有多轮,在 humangpt 之间)

{
  "id": "i6IyJda_0",
  "conversations": [
    {
      "from": "human",
      "value": "How to tell if a customer segment is well segmented? In 3 bullet points."
    },
    {
      "from": "gpt",
      "value": "1. Homogeneity: The segment should consist of customers who share similar characteristics and behaviors.\n2. Distinctiveness: The segment should be different from other segments in terms of their characteristics and behaviors.\n3. Stability: The segment should remain relatively stable over time and not change drastically. The characteristics and behaviors of customers within the segment should not change significantly."
    }
  ]
},

可选:为了让模型了解其身份,您可以修改硬编码问题文件 hardcoded_questions.py

注意:在 ShareGPT 数据上训练的模型可能存在商业使用限制。若用于商业用途,请替换为您自己的数据。

在任何云上启动训练#

使用单个命令启动训练

sky launch --down -c vicuna train.yaml \
  --env ARTIFACT_BUCKET_NAME=<your-bucket-name> \
  --env WANDB_API_KEY=<your-wandb-api-key>

这将在拥有可用 8x A100-80GB Spot GPU 的最便宜云上启动训练作业。

提示:您可以在 https://wandb.ai/settings 获取 WANDB_API_KEY。要禁用 Weights & Biases,只需省略该 --env 标志即可。

提示:您可以将 ARTIFACT_BUCKET_NAME 设置为一个新的存储桶名称,例如 <whoami>-tmp-bucket,SkyPilot 将为您创建该存储桶。

改用 On-demand 以解锁更多云服务提供商:在 train.yaml 中,我们请求使用 Spot 实例

resources:
  accelerators: A100-80GB:8
  disk_size: 1000
  use_spot: true

然而,目前 Spot A100-80GB:8 仅在 GCP 上支持。On-demand 版本在 AWS、Azure、GCP、Lambda 等提供商上都支持。(提示:查看 sky show-gpus A100-80GB:8 的有用输出!)

要使用这些云,请添加 --no-use-spot 标志以请求 On-demand 实例

sky launch --no-use-spot ...

Optimizer

可选:尝试训练 13B 模型

sky launch -c vicuna train.yaml \
  --env ARTIFACT_BUCKET_NAME=<your-bucket-name> \
  --env WANDB_API_KEY=<your-wandb-api-key> \
  --env MODEL_SIZE=13

使用 Spot 实例将成本降低 3 倍#

SkyPilot Managed Jobs 是一个构建在 SkyPilot 之上的库,它帮助用户在 Spot 实例上运行作业,而无需担心中断。LMSYS 组织正是使用此工具训练了第一个版本的 Vicuna(更多详细信息可在其 发布博客文章示例 中找到)。借此,训练成本可以从 1000 美元降低到 300 美元

要使用 SkyPilot Managed Spot Jobs,您只需将上述命令中的 sky launch 替换为 sky jobs launch

sky jobs launch -n vicuna train.yaml \
  --env ARTIFACT_BUCKET_NAME=<your-bucket-name> \
  --env WANDB_API_KEY=<your-wandb-api-key>

服务您的模型#

训练完成后,您可以使用单个命令在您自己的云环境中服务您的模型

sky launch -c serve serve.yaml --env MODEL_CKPT=<your-model-checkpoint>/chatbot/7b

serve.yaml 中,我们指定启动一个 Gradio 服务器,该服务器在 <your-model-checkpoint>/chatbot/7b 提供模型检查点服务。

Vicuna-Llama-2

提示:您也可以通过向上述命令添加 --gpus L4 来切换到更便宜的加速器(例如 L4),以节省成本。

包含的文件#