模型简介
模型特点
模型能力
使用案例
🚀 FuseLLM-7B:大语言模型知识融合
FuseLLM-7B致力于探索大语言模型(LLM)的知识融合领域,旨在创建一个统一模型,融合多个结构不同的LLM的能力和独特优势。通过引入FuseLLM方法,将源LLM的生成分布进行利用,将集体知识和个体优势转移到目标LLM中,实现了不同架构LLM的有效融合。
🚀 快速开始
环境设置
本项目使用 python 3.9
,需要安装 requirements.txt
中列出的所有库,执行以下命令:
pip install -r requirements.txt
使用示例
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("Wanfq/FuseLLM-7B", use_fast=False)
model = AutoModelForCausalLM.from_pretrained("Wanfq/FuseLLM-7B", torch_dtype="auto")
model.cuda()
inputs = tokenizer("<your text here>", return_tensors="pt").to(model.device)
tokens = model.generate(
**inputs,
max_new_tokens=512,
temperature=0.6,
top_p=0.9,
do_sample=True,
)
print(tokenizer.decode(tokens[0], skip_special_tokens=True))
此外,还可以在 FuseLLM-7B-exl2 找到 Exllama v2 Quantizations
版本,它使用 ExLlamaV2 v0.0.11 进行量化。
✨ 主要特性
- 知识融合创新:探索LLM知识融合领域,创建统一模型,结合多个结构不同LLM的能力和优势。
- 架构兼容性强:与模型集成方法和权重合并技术不同,FuseLLM支持将多个不同架构的LLM融合为更强大的LLM。
- 性能表现优异:在多个基准测试中表现出色,如BBH、ARC-easy、ARC-challenge等。
📦 安装指南
本项目使用 python 3.9
,安装 requirements.txt
中列出的所有库:
pip install -r requirements.txt
💻 使用示例
基础用法
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("Wanfq/FuseLLM-7B", use_fast=False)
model = AutoModelForCausalLM.from_pretrained("Wanfq/FuseLLM-7B", torch_dtype="auto")
model.cuda()
inputs = tokenizer("<your text here>", return_tensors="pt").to(model.device)
tokens = model.generate(
**inputs,
max_new_tokens=512,
temperature=0.6,
top_p=0.9,
do_sample=True,
)
print(tokenizer.decode(tokens[0], skip_special_tokens=True))
高级用法
可使用 Exllama v2 Quantizations
版本,在 FuseLLM-7B-exl2 中找到,它使用 ExLlamaV2 v0.0.11 进行量化。
📚 详细文档
数据构建
使用 MiniPile 数据集进行持续训练,以下是获取多个LLM表示以进行模型融合的脚本:
- 分割长文本
python ./src/utils/split_long_text.py \
--base_model_name_or_path "<path_to_llama_2_7b>" \
--blending_model_name_or_path "<path_to_open_llama_7b_v2>" \
--another_blending_model_name_or_path "<path_to_mpt_7b>" \
--dataset "<path_to_minipile>" \
--dataset_save_dir "<path_to_minipile_split>" \
--cache_dir "<path_to_cache_dir>" \
--block_size 2048 \
--preprocessing_num_workers 80
- 获取每个LLM的表示
# 我们将数据集分成8个分割,然后在GPU上处理每个分割。
# 请为llama_2_7b、open_llama_7b_v2和mpt_7b运行此脚本。
for i in {0..7}; do
export CUDA_VISIBLE_DEVICES=${i}
python ./src/utils/forward_for_logits.py \
--model_name_or_path "<path_to_each_model>" \
--dataset "<path_to_minipile_split>" \
--dataset_save_dir "${i}_8_<path_to_minipile_split_each_model_representation>" \
--dataset_split_num 8 \
--dataset_index ${i} \
--cache_dir "<path_to_cache_dir>" \
--model_max_length 2048 \
--training_mode full \
--load_in_half bf16 \
--batch_size 8 \
--preprocessing_num_workers 80 \
--top_k_logits 10 \
--save_per_token_metric 2>&1 > "${i}_8_<path_to_log_file>" 2>&1 &
unset CUDA_VISIBLE_DEVICES
sleep 30
done
wait
- 对齐不同LLM的表示
# 获取不同LLM的词汇映射。
# llama_2_7b <-> open_llama_7b_v2
python ./src/utils/vocab_mapping.py \
--base_model_name_or_path "<path_to_llama_2_7b>" \
--blending_model_name_or_path "<path_to_open_llama_7b_v2>" \
--dataset_dir "<path_to_minipile_split>" \
--vocab_mapping_save_dir "<path_to_llama_2_7b_open_llama_7b_v2_vocab_mapping>" \
--cache_dir "<path_to_cache_dir>" \
--model_max_length 2048 \
--vocab_mapping_type "default" \
--num_process 1
# llama_2_7b <-> mpt_7b
python ./src/utils/vocab_mapping.py \
--base_model_name_or_path "<path_to_llama_2_7b>" \
--blending_model_name_or_path "<path_to_mpt_7b>" \
--dataset_dir "<path_to_minipile_split>" \
--vocab_mapping_save_dir "<path_to_llama_2_7b_mpt_7b_vocab_mapping>" \
--cache_dir "<path_to_cache_dir>" \
--model_max_length 2048 \
--vocab_mapping_type "default" \
--num_process 1
# 对齐不同LLM的表示。
# llama_2_7b <-> open_llama_7b_v2
for i in {0..7}; do
python ./src/utils/token_alignment.py \
--base_model_name_or_path "<path_to_llama_2_7b>" \
--blending_model_name_or_path "<path_to_open_llama_7b_v2>" \
--base_dataset_dir "${i}_8_<path_to_minipile_split_llama_2_7b_representation>" \
--blending_dataset_dir "${i}_8_<path_to_minipile_split_open_llama_7b_v2_representation>" \
--dataset_save_dir "${i}_8_<path_to_minipile_split_llama_2_7b_open_llama_7b_v2_aligned_representation>" \
--cache_dir "<path_to_cache_dir>" \
--model_max_length 2048 \
--preprocessing_num_workers 80 \
--batch_size 100 \
--blending_model_index 0 \
--vocab_align_type "soft" \
--vocab_mapping_save_dir "<path_to_llama_2_7b_open_llama_7b_v2_vocab_mapping>" \
--metric_level "sequence"
done
# llama_2_7b <-> mpt_7b
for i in {0..7}; do
python ./src/utils/token_alignment.py \
--base_model_name_or_path "<path_to_llama_2_7b>" \
--blending_model_name_or_path "<path_to_mpt_7b>" \
--base_dataset_dir "${i}_8_<path_to_minipile_split_llama_2_7b_open_llama_7b_v2_aligned_representation>" \
--blending_dataset_dir "${i}_8_<path_to_minipile_split_mpt_7b_representation>" \
--dataset_save_dir "${i}_8_<path_to_minipile_split_llama_2_7b_open_llama_7b_v2_mpt_7b_aligned_representation>" \
--cache_dir "<path_to_cache_dir>" \
--model_max_length 2048 \
--preprocessing_num_workers 80 \
--batch_size 100 \
--blending_model_index 1 \
--vocab_align_type "soft" \
--vocab_mapping_save_dir "<path_to_llama_2_7b_mpt_7b_vocab_mapping>" \
--metric_level "sequence"
done
- 打包所有特征以加速训练
for i in {0..7}; do
python3 ./src/utils/packing.py \
--dataset_dir "${i}_8_<path_to_minipile_split_llama_2_7b_open_llama_7b_v2_mpt_7b_aligned_representation>" \
--dataset_save_dir "${i}_8_<path_to_miniplie_fusellm_processed>" \
--cache_dir "<path_to_cache_dir>" \
--model_max_length 2048 \
--preprocessing_num_workers 80 \
--batch_size 1000 \
--metric_level "sequence"
最终处理后的数据位于 ${i}_8_<path_to_miniplie_fusellm_processed>
,其中 i in {0..7}
。
训练
以下是FuseLLM训练的脚本:
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
deepspeed --master_port=20001 ./src/train.py \
--training_mode full \
--deepspeed ./config/zero_stage2_config.json \
--model_name_or_path "<path_to_llama_2_7b>" \
--output_dir "<path_to_save_fusellm_7b>" \
--model_max_length 2048 \
--logging_steps 1 \
--save_strategy steps \
--save_steps 500 \
--save_total_limit 1 \
--evaluation_strategy steps \
--per_device_eval_batch_size 1 \
--logging_strategy steps \
--do_train \
--do_eval \
--bf16 True \
--tf32 True \
--warmup_ratio 0.008 \
--lr_scheduler_type cosine \
--dataset_name "0_8_<path_to_miniplie_fusellm_processed>,1_8_<path_to_miniplie_fusellm_processed>,2_8_<path_to_miniplie_fusellm_processed>,3_8_<path_to_miniplie_fusellm_processed>,4_8_<path_to_miniplie_fusellm_processed>,5_8_<path_to_miniplie_fusellm_processed>,6_8_<path_to_miniplie_fusellm_processed>,7_8_<path_to_miniplie_fusellm_processed>" \
--per_device_train_batch_size 1 \
--gradient_accumulation_steps 16 \
--num_train_epochs 1 \
--eval_steps 500 \
--optim adamw_torch \
--adam_beta1 0.9 \
--adam_beta2 0.95 \
--learning_rate 1e-5 \
--weight_decay 0.1 \
--max_grad_norm 1.0 \
--seed 42 \
--gradient_checkpointing True \
--use_flash_attn True \
--report_to tensorboard \
--do_distill \
--distill_with_ref_model True \
--distill_with_aligned_model_0 True \
--distill_with_aligned_model_1 True \
--distill_loss_type "ce" \
--distill_teacher_temperature 1.0 \
--lm_loss_weight 0.9 \
--distill_greater_as_gt True \
--distill_greater_as_gt_type "hard" \
--dataloader_num_workers 10 \
--remove_unused_columns False 2>&1 | tee "<path_to_log_file>"
评估
使用的评估代码如下:
- Big-Bench Hard
- CommonSense: ARC-easy, ARC-challenge, BoolQ, HellaSwag, OpenBookQA
- MultiPL-E
- Text Generation: TrivialQA, DROP, LAMBADA, IWSLT2017, SciBench
- Vicuna Bench
🔧 技术细节
在本研究中,探索了大语言模型(LLM)的知识融合领域,旨在创建一个统一模型,结合多个结构不同的LLM的能力和独特优势。为实现这一目标,引入了FuseLLM方法。该方法首先利用这些源LLM的生成分布,将它们的集体知识和个体优势外部化,然后通过轻量级的持续训练将其转移到目标LLM中。
与需要并行部署多个LLM的模型集成方法,或通常限于相同架构LLM的权重合并技术不同,FuseLLM旨在支持将多个不同架构的LLM融合为更强大的LLM。通过将它们的知识和能力明确转移到单个目标LLM中,FuseLLM为LLM的知识融合提供了强大而灵活的解决方案。
📄 许可证
本项目采用 apache-2.0
许可证。
引用
如果您发现这项工作与您的研究或应用相关,请随时引用我们的工作!
@inproceedings{wan2024knowledge,
title={Knowledge Fusion of Large Language Models},
author={Fanqi Wan and Xinting Huang and Deng Cai and Xiaojun Quan and Wei Bi and Shuming Shi},
booktitle={The Twelfth International Conference on Learning Representations},
year={2024},
url={https://openreview.net/pdf?id=jiDsk12qcz}
}
Open LLM Leaderboard评估结果
详细结果可在此处找到。
指标 | 值 |
---|---|
平均值 | 51.07 |
AI2推理挑战(25-shot) | 53.24 |
HellaSwag(10-shot) | 78.72 |
MMLU(5-shot) | 47.93 |
TruthfulQA(0-shot) | 38.17 |
Winogrande(5-shot) | 74.03 |
GSM8k(5-shot) | 14.33 |



