
这是斯坦福羊驼项目的回购,该项目旨在建立和分享关注指导型美洲驼的模型。存储库包含:
注意:我们感谢社区对斯坦福 - 阿尔帕卡的反馈并支持我们的研究。我们的现场演示被暂停,直到另行通知。
用法和许可声明:羊驼仅用于研究用途和许可。该数据集由NC 4.0(仅允许非商业用途)为CC,并且不应在研究目的之外使用使用数据集进行培训的模型。重量差异也是NC 4.0的CC(仅允许非商业用途)。
当前的羊驼模型是从7B Llama模型[1]微调的,该模型[1]在自我教学[2]论文中的技术生成的52K指令遵循数据上进行了微调,并在下一部分中讨论了一些修改。在初步的人类评估中,我们发现羊驼7B模型的行为与自我实施指令评估套件上的text-davinci-003模型相似[2]。
羊驼仍在开发中,必须解决许多局限性。重要的是,我们尚未对羊驼模型进行微调安全和无害。因此,我们鼓励用户在与羊驼互动时保持谨慎,并报告有关行为的任何行为,以帮助改善模型的安全性和道德考虑。
我们的初始版本包含数据生成过程,数据集和培训配方。如果我们获得了Llama的创建者的许可,我们打算释放模型权重。目前,我们选择举办现场演示,以帮助读者更好地了解羊驼的功能和限制,以及一种帮助我们更好地评估羊驼在广泛受众中的表现的方法。
请阅读我们的发行博客文章,以了解有关该模型的更多详细信息,我们对羊驼模型的潜在危害和局限性的讨论以及我们发布可再现模型的思维过程。
[1]:美洲驼:开放有效的基础语言模型。 Hugo Touvron, Thibaut Lavril, Gautier Izacard, Xavier Martinet, Marie-Anne Lachaux, Timothée Lacroix, Baptiste Rozière, Naman Goyal, Eric Hambro, Faisal Azhar, Aurelien Rodriguez, Armand Joulin, Edouard Grave, Guillaume Lample. https://arxiv.org/abs/2302.13971v1
[2]:自我建造:将语言模型与自生成指令保持一致。 Yizhong Wang,Yeganeh Kordi,Swaroop Mishra,Alisa Liu,Noah A. Smith,Daniel Khashabi,Hannaneh Hajishirzi。 https://arxiv.org/abs/2212.10560
alpaca_data.json包含我们用于微调羊驼模型的52K指令遵循数据。该JSON文件是字典列表,每个字典包含以下字段:
instruction : str ,描述模型应执行的任务。 52k说明中的每一个都是唯一的。input : str ,可选上下文或任务输入。例如,当指令是“总结以下文章”时,输入就是文章。大约40%的示例具有输入。output : str , text-davinci-003生成的指令的答案。我们使用以下提示来微调羊驼模型:
Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction:
{instruction}
### Input:
{input}
### Response:
Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
{instruction}
### Response:
在推理期间(例如,用于Web演示),我们将用户指令与空输入字段(第二个选项)一起使用。
OPENAI_API_KEY设置为OpenAI API键。pip install -r requirements.txt安装依赖项。python -m generate_instruction generate_instruction_following_data生成数据。我们建立在自我指导的数据生成管道上,并进行了以下修改:
text-davinci-003来生成指令数据,而不是davinci 。prompt.txt ),该提示明确地将指令生成的要求发送给了text-davinci-003 。注意:我们使用的提示中有一个轻微的错误,未来的用户应将编辑合并到#24中这产生了一个以52k示例获得的指令跟随数据集,其成本要低得多(低于500美元)。在一项初步研究中,我们还发现我们的52K生成的数据比自我指示发布的数据多得多。我们绘制下图(以自我教学纸中图2的样式以图2的方式展示了我们数据的多样性。绘图的内圆表示指令的根动词,而外圈表示直接对象。

我们使用标准的拥抱面部训练代码微调模型。我们将Llama-7b和Llama-13b微调以下超参数:
| 超参数 | Llama-7b | Llama-13b |
|---|---|---|
| 批量大小 | 128 | 128 |
| 学习率 | 2E-5 | 1E-5 |
| 时代 | 3 | 5 |
| 最大长度 | 512 | 512 |
| 重量衰减 | 0 | 0 |
要重现我们为美洲驼的微调运行,请首先安装要求
pip install -r requirements.txt以下是在FSDP full_shard模式下使用4 A100 80G GPU的机器上使用我们的数据集微调Llama-7b的命令。我们能够使用Python 3.10复制与演示中托管的模型相似的模型。 <your_random_port>用自己的端口替换, <your_path_to_hf_converted_llama_ckpt_and_tokenizer>带有转换后的检查点和tokenizer的路径(PR中的以下说明),以及<your_output_dir> ,以及想要存储输出的位置。
torchrun --nproc_per_node=4 --master_port= < your_random_port > train.py
--model_name_or_path < your_path_to_hf_converted_llama_ckpt_and_tokenizer >
--data_path ./alpaca_data.json
--bf16 True
--output_dir < your_output_dir >
--num_train_epochs 3
--per_device_train_batch_size 4
--per_device_eval_batch_size 4
--gradient_accumulation_steps 8
--evaluation_strategy " no "
--save_strategy " steps "
--save_steps 2000
--save_total_limit 1
--learning_rate 2e-5
--weight_decay 0.
--warmup_ratio 0.03
--lr_scheduler_type " cosine "
--logging_steps 1
--fsdp " full_shard auto_wrap "
--fsdp_transformer_layer_cls_to_wrap ' LlamaDecoderLayer '
--tf32 True同一脚本也适用于选择微调。这是微调opt-6.7b的示例
torchrun --nproc_per_node=4 --master_port= < your_random_port > train.py
--model_name_or_path " facebook/opt-6.7b "
--data_path ./alpaca_data.json
--bf16 True
--output_dir < your_output_dir >
--num_train_epochs 3
--per_device_train_batch_size 4
--per_device_eval_batch_size 4
--gradient_accumulation_steps 8
--evaluation_strategy " no "
--save_strategy " steps "
--save_steps 2000
--save_total_limit 1
--learning_rate 2e-5
--weight_decay 0.
--warmup_ratio 0.03
--lr_scheduler_type " cosine "
--logging_steps 1
--fsdp " full_shard auto_wrap "
--fsdp_transformer_layer_cls_to_wrap ' OPTDecoderLayer '
--tf32 True请注意,给定的培训脚本旨在简单易用,并且不是特别优化。要在更多的GPU上运行,您可能更喜欢键入gradient_accumulation_steps ,以保持全局批次大小为128。尚未对全局批次大小进行最佳测试。
天真,微调7B模型需要约7 x 4 x 4 = 112 GB的VRAM。上面给出的命令启用参数碎片,因此在任何GPU上均未存储冗余模型副本。如果您想进一步减少内存足迹,这里有一些选择:
--fsdp "full_shard auto_wrap offload"打开FSDP的CPU卸载。这样可以节省VRAM,而运行时间更长。pip install deepspeed
torchrun --nproc_per_node=4 --master_port= < your_random_port > train.py
--model_name_or_path < your_path_to_hf_converted_llama_ckpt_and_tokenizer >
--data_path ./alpaca_data.json
--bf16 True
--output_dir < your_output_dir >
--num_train_epochs 3
--per_device_train_batch_size 4
--per_device_eval_batch_size 4
--gradient_accumulation_steps 8
--evaluation_strategy " no "
--save_strategy " steps "
--save_steps 2000
--save_total_limit 1
--learning_rate 2e-5
--weight_decay 0.
--warmup_ratio 0.03
--deepspeed " ./configs/default_offload_opt_param.json "
--tf32 True羊驼-7B和骆驼-7B之间的重量差异在这里。要恢复原始的羊驼-7b重量,请按照以下步骤:
1. Convert Meta's released weights into huggingface format. Follow this guide:
https://huggingface.co/docs/transformers/main/model_doc/llama
2. Make sure you cloned the released weight diff into your local machine. The weight diff is located at:
https://huggingface.co/tatsu-lab/alpaca-7b/tree/main
3. Run this function with the correct paths. E.g.,
python weight_diff.py recover --path_raw <path_to_step_1_dir> --path_diff <path_to_step_2_dir> --path_tuned <path_to_store_recovered_weights>
步骤3完成后,您应该有一个带有恢复权重的目录,您可以从中加载模型
import transformers
alpaca_model = transformers . AutoModelForCausalLM . from_pretrained ( "<path_to_store_recovered_weights>" )
alpaca_tokenizer = transformers . AutoTokenizer . from_pretrained ( "<path_to_store_recovered_weights>" )下面的所有研究生均同等贡献,订单均由随机抽签确定。
所有这些都由Tatsunori B. Hashimoto建议。珀西·梁(Percy Liang)也建议Yann,Carlos Guestrin也建议Xuechen。
如果您在此存储库中使用数据或代码,请引用回购。
@misc{alpaca,
author = {Rohan Taori and Ishaan Gulrajani and Tianyi Zhang and Yann Dubois and Xuechen Li and Carlos Guestrin and Percy Liang and Tatsunori B. Hashimoto },
title = {Stanford Alpaca: An Instruction-following LLaMA model},
year = {2023},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {url{https://github.com/tatsu-lab/stanford_alpaca}},
}
自然,您还应该引用原始的骆驼纸[1]和自我教学论文[2]。
我们感谢Yizhong Wang在解释自我教学中的数据生成管道的帮助,并为解析分析图提供了代码。我们感谢Yifan Mai的有益支持,Stanford NLP集团以及基础模型研究中心(CRFM)的成员提供了有益的反馈。