torchtune 中的 Meta Llama3¶
下载 Llama3-8B-Instruct 权重和分词器
使用 LoRA 和 QLoRA 微调 Llama3-8B-Instruct
评估微调的 Llama3-8B-Instruct 模型
使用微调的模型生成文本
量化模型以加快生成速度
熟悉 torchtune
美洲驼3-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 使用更高的基值来计算其旋转位置嵌入中的 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 提供 LoRA、QLoRA 和完全微调 在一个或多个 GPU 上微调 Llama3-8B 的配方。有关 torchtune 中 LoRA 的更多信息,请参阅我们的 LoRA 教程。 有关 torchtune 中 QLoRA 的更多信息,请参阅我们的 QLoRA 教程。
让我们来看看如何使用 torchtune 在单个设备上使用 LoRA 微调 Llama3-8B-Instruct。在此示例中,我们将微调 用于说明目的的通用 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>
这将加载上面 tune download 命令中使用的 Llama3-8B-Instruct 检查点和分词器。
然后,按照原始格式在同一目录中保存最终检查点。有关
Torchtune 支持的检查点格式,请参阅我们的检查点深入探讨。<checkpoint_dir>
注意
要查看此(和其他)配置的完整可配置参数集,我们可以使用 tune cp 来复制(和修改) 默认配置。Tune CP 也可以与配方脚本一起使用,以防您想要进行更多自定义更改 这无法通过直接修改现有的可配置参数来实现。有关 Tune cp 的更多信息,请参阅我们的 “Fine-Tune Your First LLM” 教程中有关修改配置的部分。
训练完成后,将保存模型检查点并记录其位置。为 LoRA 微调时,最终检查点将包含合并的权重,以及仅(小得多的)LoRA 权重的副本 将单独保存。
在我们的实验中,我们观察到峰值内存使用量为 18.5 GB。默认配置可以在具有 24 GB VRAM 的消费类 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 的 VRAM,实际上 QLoRA 配方应该具有峰值分配的内存 低于 10 GB。您还可以尝试 LoRA 和 QLoRA 的不同配置,甚至可以运行完整的微调。 试试看!
使用 EleutherAI 的 Eval Harness 评估微调的 Llama3-8B 模型¶
现在我们已经微调了我们的模型,下一步是什么?让我们从 上一节,看看我们可以通过几种不同的方式来评估它在我们关心的任务上的表现。
首先, torchtune 提供了与 EleutherAI 的评估工具的集成,用于对常见基准任务进行模型评估。
注意
确保您首先通过 安装了评估工具。pip install "lm_eval==0.4.*"
在本教程中,我们将使用线束中的 truthfulqa_mc2 任务。 此任务衡量模型在回答问题时保持真实的倾向,并且 测量模型对问题后跟一个或多个 true 的零镜头准确率 responses 和一个或多个 false 响应。首先,让我们复制配置,以便我们可以将 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
最后,我们可以使用修改后的配置运行 evaluation。
tune run eleuther_eval --config ./custom_eval_config.yaml
亲自尝试一下,看看您的模型的准确性如何!
使用我们微调的 Llama3 模型生成文本¶
接下来,让我们看看评估模型的另一种方法:生成文本!torchtune 也提供了生成方法。
与我们所做的类似,让我们复制并修改默认的生成配置。
tune cp generation ./custom_generation_config.yaml
现在我们修改以指向我们的 checkpoint 和 tokenizer。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 微调模型运行 generation,我们看到以下输出:
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 模型,您可以使用其脚本直接在 torchao 中的量化模型上运行生成,例如
在本自述文件中进行了讨论。这样您就可以比较自己的结果
添加到先前链接的表中的那些。generate.py
这只是您可以使用 torchtune 和更广泛的生态系统对 Meta Llama3 执行的操作的开始。 我们期待看到您构建的内容!