Meta Llama3 in torchtune¶
下载 Llama3-8B-Instruct 的权重和分词器
使用 LoRA 和 QLoRA 微调 Llama3-8B-Instruct
评估您微调后的 Llama3-8B-Instruct 模型
使用您微调后的模型生成文本
量化您的模型以加速生成
熟悉 torchtune
确保已 安装 torchtune
Llama3-8B¶
Meta Llama 3 是 Meta AI 发布的新一代模型系列,相较于 Llama2 系列模型,在多个不同基准测试中提升了性能表现。 目前 Meta Llama 3 共有两种不同规模的版本:8B 和 70B。本教程将重点介绍 8B 规模的模型。 Llama2-7B 和 Llama3-8B 模型之间主要有以下几项主要改进:
Llama3-8B 使用了 分组查询注意力,而不是 Llama2-7B 中的标准多头注意力。
Llama3-8B 拥有更大的词表规模(128,256,而 Llama2 模型为 32,000)
Llama3-8B 使用了与 Llama2 模型不同的分词器(tiktoken 而不是 sentencepiece)
Llama3-8B 在其 MLP 层中使用了比 Llama2-7B 更大的中间维度。
Llama3-8B 使用更高的基础值来计算其旋转位置嵌入(rotary positional embeddings)中的 theta
获取对 Llama3-8B-Instruct 的访问权限¶
对于本教程,我们将使用Llama3-8B的指令调优版本。首先,让我们从Hugging Face下载模型。你需要按照 Meta官方页面上的说明来获取对模型的访问权限。 接下来,请确保从 这里 获取你的Hugging Face令牌。
tune download meta-llama/Meta-Llama-3-8B-Instruct \
--output-dir <checkpoint_dir> \
--hf-token <ACCESS TOKEN>
在torchtune中微调Llama3-8B-Instruct¶
torchtune 提供了用于在一台或多台 GPU 上微调 Llama3-8B 的 LoRA、QLoRA 和全微调配方。有关 torchtune 中 LoRA 的更多信息,请参阅我们的 LoRA 教程。 有关 torchtune 中 QLoRA 的更多信息,请参阅我们的 QLoRA 教程。
让我们看看如何使用 torchtune 在单个设备上通过 LoRA 对 Llama3-8B-Instruct 进行微调。在本例中,为便于说明,我们将在一个常见的指令数据集上训练一个 epoch。单设备 LoRA 微调的基本命令是
tune run lora_finetune_single_device --config llama3/8B_lora_single_device
注意
要查看所有配方及其对应的配置列表,只需在命令行中运行 tune ls。
我们还可以根据需要添加 命令行覆盖,例如
tune run lora_finetune_single_device --config llama3/8B_lora_single_device \
checkpointer.checkpoint_dir=<checkpoint_dir> \
tokenizer.path=<checkpoint_dir>/tokenizer.model \
checkpointer.output_dir=<checkpoint_dir>
这将从 <checkpoint_dir> 加载 Llama3-8B-Instruct 检查点和分词器,该值用于上述的 tune download 命令,
然后按照原始格式在相同目录中保存最终的检查点。有关 torchtune 支持的检查点格式的更多详细信息,请参阅我们的 检查点深入解析。
注意
要查看此(以及其他)配置的完整可配置参数集,我们可以使用 tune cp 来复制(并修改)默认配置。tune cp 也可以与配方脚本一起使用,以便在无法直接修改现有可配置参数的情况下进行更自定义的更改。有关 tune cp 的更多信息,请参阅我们在“修改配置”部分中的“微调你的第一个大型语言模型”教程。
训练完成后,模型检查点将被保存,其位置将记录在日志中。对于 LoRA 微调,最终检查点将包含合并后的权重,同时会单独保存一份仅包含(小得多的)LoRA 权重的副本。
在我们的实验中,我们观察到峰值内存使用量为 18.5 GB。默认配置可在配备 24 GB 显存的消费级 GPU 上进行训练。
如果你有多个可用的GPU,你可以运行该配方的分布式版本。 torchtune 利用了 PyTorch Distributed 的 FSDP API 来拆分模型、优化器状态和梯度。这应该能够让你增加批量大小,从而实现更快的整体训练速度。 例如,在两个设备上:
tune run --nproc_per_node 2 lora_finetune_distributed --config llama3/8B_lora
最后,如果我们希望进一步减少内存占用,可以通过以下方式利用 torchtune 的 QLoRA 配方:
tune run lora_finetune_single_device --config llama3/8B_qlora_single_device
由于我们的默认配置启用了完整的 bfloat16 训练,上述所有命令均可在显存至少为 24 GB 的设备上运行;事实上,QLoRA 方案的峰值分配内存应低于 10 GB。您也可以尝试不同的 LoRA 和 QLoRA 配置,甚至进行全量微调。立即体验吧!
评估使用EleutherAI的Eval Harness微调的Llama3-8B模型¶
既然我们已经完成了模型的微调,接下来该做什么呢?让我们使用上一节中经过 LoRA 微调的模型,探讨几种评估其在关键任务上性能的方法。
首先,torchtune 提供了与 EleutherAI 的评估工具包 的集成,用于在常见基准任务上对模型进行评估。
注意
请确保您已通过 pip install "lm_eval==0.4.*" 安装了评估工具包。
在这个教程中,我们将使用来自测试套件的 truthfulqa_mc2 任务。 该任务衡量模型在回答问题时保持诚实的倾向,并测量模型在问题后跟随一个或多个正确答案和一个或多个错误答案的情况下进行零样本准确性的能力。首先,让我们复制配置,以便我们可以将 YAML 文件指向我们的微调检查点文件。
tune cp eleuther_evaluation ./custom_eval_config.yaml
接下来,我们将 custom_eval_config.yaml 修改为包含微调后的检查点。
model:
_component_: torchtune.models.llama3.llama3_8b
checkpointer:
_component_: torchtune.training.FullModelMetaCheckpointer
# directory with the checkpoint files
# this should match the output_dir specified during
# fine-tuning
checkpoint_dir: <checkpoint_dir>
# checkpoint files for the fine-tuned model. These will be logged
# at the end of your fine-tune
checkpoint_files: [
meta_model_0.pt
]
output_dir: <checkpoint_dir>
model_type: LLAMA3
# Make sure to update the tokenizer path to the right
# checkpoint directory as well
tokenizer:
_component_: torchtune.models.llama3.llama3_tokenizer
path: <checkpoint_dir>/tokenizer.model
最后,我们可以使用修改后的配置文件运行评估。
tune run eleuther_eval --config ./custom_eval_config.yaml
亲自试一试,看看你的模型能达到怎样的准确率!
使用我们微调的Llama3模型生成文本¶
接下来,让我们看看另一种评估模型的方法:生成文本!torchtune 提供了一个 生成的配方。
与我们所做的类似,让我们复制并修改默认生成配置。
tune cp generation ./custom_generation_config.yaml
现在我们将 custom_generation_config.yaml 修改为指向我们的检查点和分词器。
model:
_component_: torchtune.models.llama3.llama3_8b
checkpointer:
_component_: torchtune.training.FullModelMetaCheckpointer
# directory with the checkpoint files
# this should match the output_dir specified during
# fine-tuning
checkpoint_dir: <checkpoint_dir>
# checkpoint files for the fine-tuned model. These will be logged
# at the end of your fine-tune
checkpoint_files: [
meta_model_0.pt
]
output_dir: <checkpoint_dir>
model_type: LLAMA3
# Make sure to update the tokenizer path to the right
# checkpoint directory as well
tokenizer:
_component_: torchtune.models.llama3.llama3_tokenizer
path: <checkpoint_dir>/tokenizer.model
使用我们经过 LoRA 微调的模型运行生成,可以看到以下输出:
tune run generate --config ./custom_generation_config.yaml \
prompt="Hello, my name is"
[generate.py:122] Hello, my name is Sarah and I am a busy working mum of two young children, living in the North East of England.
...
[generate.py:135] Time for inference: 10.88 sec total, 18.94 tokens/sec
[generate.py:138] Bandwidth achieved: 346.09 GB/s
[generate.py:139] Memory used: 18.31 GB
通过量化实现更快的生成¶
我们依赖 torchao 进行 训练后量化。 在安装 torchao 后,我们可以运行以下命令来量化微调后的模型:
# we also support `int8_weight_only()` and `int8_dynamic_activation_int8_weight()`, see
# https://github.com/pytorch/ao/tree/main/torchao/quantization#other-available-quantization-techniques
# for a full list of techniques that we support
from torchao.quantization.quant_api import quantize_, int4_weight_only
quantize_(model, int4_weight_only())
量化后,我们依赖 torch.compile 来实现性能提升。详情请参阅 此示例用法。
torchao 还提供了 此表格,列出了 llama2 和 llama3 的性能和准确率结果。
对于Llama模型,您可以直接在量化模型上使用他们的generate.py脚本通过torchao进行生成,如此readme中所述。这样您就可以将您的结果与之前链接的表格中的结果进行比较。
这仅仅是使用 torchtune 及更广泛的生态系统,借助 Meta Llama3 所能实现功能的开端。 我们期待看到您的创作成果!