Files
airllm/README.md
2023-06-12 15:35:17 -05:00

134 lines
7.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Anima
![Anima Logo](https://github.com/lyogavin/Anima/blob/main/anima_logo.png?raw=true)
第一个开源的基于QLoRA的33B中文大语言模型 the First QLoRA based 33B fully open-source Chinese LLM
*Read this in [English](README_en.md).*
## Huggingface模型开源地址
[lyogavin/Anima33B](https://huggingface.co/lyogavin/Anima33B)
## 模型训练
#### Backbone模型选择
Anima模型基于QLoRA开源的[33B guanaco](https://huggingface.co/timdettmers/guanaco-33b)训练了10000 steps。训练使用一个H100 GPU。
* **思考逻辑**本工作主要为了验证QLoRA训练方法的有效性因此选择了基于QLoRA的Guanaco 33B finetune训练这个训练更多的是增强模型的中文能力。Assume模型的基础logical reasoning和Knowledge能力已经足够。
#### 训练数据选择
使用[Chinese-Vicuna](https://github.com/Facico/Chinese-Vicuna)项目开放的训练数据集[guanaco_belle_merge_v1.0](https://huggingface.co/datasets/Chinese-Vicuna/guanaco_belle_merge_v1.0)进行finetune训练。
* **思考逻辑**:按照[QLoRA](https://arxiv.org/abs/2305.14314) Appendix B.4和Table 9中的Grid Search的结论对于QLoRA finetunetraining sample量不一定越大越好。10000个steps是一个ROI比较优的size。因此我们希望选择一个不小于10000个steps的数据集。[Belle 10M](https://github.com/LianjiaTech/BELLE/blob/main/data/10M)数据集似乎太大了不确定数据质量如何。时间有限先选择guanaco_belle_merge_v1.0。后边会进一步更系统性的测试更多的数据集和数据质量筛选的效果。
* **感谢**Chinese-Vicuna项目、Belle项目、GuanacoDataset的贡献。
#### 超参选择
基于成本ROI平衡的考虑没有做太多的grid search基本的思路是follow [QLoRA paper](https://arxiv.org/abs/2305.14314) 的结论因为QLoRA做了相对比较详尽的超参Grid Search实验
* Batch size: 16 ([QLoRA](https://arxiv.org/abs/2305.14314) Appendix B.4和Table 9)
* Max steps: 10000 ([QLoRA](https://arxiv.org/abs/2305.14314) Appendix B.4和Table 9)更多的steps和更大的数据集的训练在进一步实验中后续会持续更新。
* Learning rate: 1e-4 ([QLoRA](https://arxiv.org/abs/2305.14314) Appendix B.4和Table 9)
* LoRA r=64, alpha=16 ([QLoRA](https://arxiv.org/abs/2305.14314) Appendix B.2)
* source_max_len=512, target_max_len=512需要保证大部分的training sample没有truncate能完整的把信息训练到模型中根据[脚本](https://github.com/lyogavin/Anima/blob/main/scripts/test_cn_dataset_lenghts.py)中的估计512大概可以覆盖大部分的样本长度。
#### 如何训练
使用以下步骤可以重现Anima 33B模型
# 1. install dependencies
pip install -r requirements.txt
# 2.
cd training
./run_Amina_training.sh
## 验证评估
#### Elo rating tournament结论
| Model | Elo | Rank |
|-------------------|---------|------|
| ChatGPT-3.5 turbo | 1341.98 | 1 |
| **Anima 33B** | **1096.69** | **2** |
| Belle | 937.71 | 3 |
| Chinese Vicuna | 623.62 | 4 |
#### 评估方法论
* **数据集的选择**:如[Belle Paper](https://github.com/LianjiaTech/BELLE/blob/main/docs/Towards%20Better%20Instruction%20Following%20Language%20Models%20for%20Chinese.pdf)中论述评估集的不同类型分布对于评估结论影响巨大。如田忌赛马以己之长攻人之短很容易占优势。因此我们选择了英文chatbot模型研究工作中比较普遍公认的[Vicuna benchmark](https://lmsys.org/blog/2023-03-30-vicuna/)。为了评测中文我们使用GPT4对于问题做了翻译。翻译代码和数据集如下
* **评估方法**: 为了平衡成本我们主要采用GPT4进行评估。如[QLoRA](https://arxiv.org/abs/2305.14314) 论证单纯GPT4打分进行模型的对比随机波动性较大。这与我们的观察一致。因此采用了[QLoRA](https://arxiv.org/abs/2305.14314) 推荐的现在比较普遍采用的Elo Rating tournament评测方法。
* **超参选择**出于成本考虑我们选择300轮随机评估随机选择模型PK的先后顺序以抵消先后顺序的影响随机种子为42。Elo rating的实现代码和其他超参参照[Vicuna的Elo代码](https://raw.githubusercontent.com/lm-sys/FastChat/833d65032a715240a3978f4a8f08e7a496c83cb1/fastchat/serve/monitor/elo_analysis.py): K=32, init rating=1000。
#### 结论
LLM模型最重要的还是logical reasoning的能力和encode knowledge的能力。因此模型的规模还是最重要的因素。通过QLoRA的方式可以让我们以足够低的成本finetune优化给定硬件条件下最大的模型。从而达到最优的效果。
Anima模型只通过10000 steps的训练并没有深度优化训练数据的质量就已经达到了最优的中文模型的效果。
# 如何Inferrence
首先保证依赖都已经安装:
pip install -r https://github.com/lyogavin/Anima/blob/main/requirements.txt?raw=true
可以参考:[inferrence.ipynb]
或者使用如下代码:
# imports
from peft import PeftModel
from transformers import GenerationConfig, LlamaForCausalLM, LlamaTokenizer
import torch
# create tokenizer
base_model = "timdettmers/guanaco-33b-merged"
tokenizer = LlamaTokenizer.from_pretrained(base_model)
# base model
model = LlamaForCausalLM.from_pretrained(
base_model,
torch_dtype=torch.float16,
device_map="auto",
)
# LORA PEFT adapters
adapter_model ="lyogavin/Anima33B"
model = PeftModel.from_pretrained(
model,
adapter_model,
#torch_dtype=torch.float16,
)
model.eval()
# prompt
prompt = "中国的首都是哪里?"
inputs = tokenizer(prompt, return_tensors="pt")
# Generate
generate_ids = model.generate(**inputs, max_new_tokens=30)
print(tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0])
# output: '中国的首都是哪里?\n中国的首都是北京。\n北京位于中国北部是中国历史悠'
## Who We Are?
此工作来自于[艾写科技](https://aicompose.cn/about)。我们团队来自于硅谷有多年中、美大厂的一线AI工作经验。
我们致力于通过最新的AGILLM技术为内容创作提供下一代的内容创作工具。
**我们相信**生成式AI的年代“写”不是变得更容易而是更难了。因为AI拉平了玩家之间的差距。每个人都可以很容易的让ChatGPT帮你写一段文案。
单纯的为内容创作提供“写”文案的工具已经远远不够。内容创作者需要的不是“写”而是“写爆款”是要结合“爆款”的趋势结合对于用户内容兴趣和口味变化的敏锐洞察为内容创作提供能高效产出爆款的AI。
我们坚持积累大量的中文全网社交媒体数据积累了大量实时的对于爆款趋势的变化数据。通过结合爆款数据和最近的LLM AI技术为内容创作者提供算法分发时代真正有效的竞争优势。