Feb 25, 2024: 🔥 We release FuseChat-Mixture, which is a comprehensive training dataset covers different styles and capabilities, featuring both human-written and model-generated, and spanning general instruction-following and specific skills.
In this work, we propose an extended framework of FuseLLM to integrate the collective knowledge and individual strengths of multiple structure and scale-varied chat LLMs into a more powerful chat LLM, resulting in FuseChat. FuseChat adopts a fuse-then-merge strategy with two main stages. Firstly, it undertakes pairwise knowledge fusion for source LLMs to derive multiple target LLMs of identical structure and size via lightweight fine-tuning. Then, these target LLMs are merged within the parameter space, wherein we propose a novel method VaRM for determining the merging weights based on the variation ratio of parameter matrices before and after fine-tuning.
Moreover, we argue that the concept of knowledge fusion adopted by both FuseChat and FuseLLM shares a fundamentally similar purpose with other related topics, such as the recently popular topic of mixture of experts (MoEs), because they all aim to leverage the strengths of multiple models (experts). However, while MoEs require loading multiple experts during inference, which has higher memory requirements, knowledge fusion supports the integration of multiple LLMs with diverse architectures into a single LLM without any additional memory requirement, making it more memory-efficient.
The GPT4 template is also available as the integrated tokenizer.chat_template, which can be used instead of manually specifying the template:
python
1messages =[2{"role":"user","content":"Hello"},3{"role":"assistant","content":"Hi"},4{"role":"user","content":"How are you today?"}5]6tokens = tokenizer.apply_chat_template(messages, add_generation_prompt=True)7assert tokens ==[1,420,6316,28781,3198,3123,1247,28747,22557,32000,420,6316,28781,3198,3123,21631,28747,15359,32000,420,6316,28781,3198,3123,1247,28747,1602,460,368,3154,28804,32000,420,6316,28781,3198,3123,21631,28747]
To support a new model in FuseChat, you'll need to follow these steps:
Implement a conversation template for the new model at conversation.py. You can follow existing examples and use register_conv_template to add a new one.
Implement a model adapter for the new model at model/model_adapter.py. You can follow existing examples and use register_model_adapter to add a new one.
We curated a comprehensive training dataset, FuseChat-Mixture, from various sources. This dataset covers different styles and capabilities, featuring both human-written and model-generated, and spanning general instruction-following and specific skills. You should download the dataset and place it into data/fusechat_v1_clean_split_2048_filter_wrong.json before experiments.
Data Construction
Here we show the scripts to obtain representations from multiple source LLMs for model fusion with the following three steps.
1. Get Representations
Here we show the scripts to obtain representations from multiple source LLMs.
bash
1# We split the dataset into 4 splits, then process each split on one or multiple GPUs.2# OpenChat-3.5-7B Starling-LM-7B-alpha Nous-Hermes-2-SOLAR-10.7B internlm2-chat-20b Mixtral-8x7B-Instruct-v0.1 Qwen1.5-72B-Chat3exportCUDA_VISIBLE_DEVICES=0# specify one or multiple GPUs4PROJ_PATH=FuseChat # specify your own project path5DATA_NAME="fusechat_v1_clean_split_2048_filter_wrong"6MODEL_NAME=openchat_3.5 # model to get representation7CONV_TEMP=openchat_3.5 # conversation template, should be the same for all models, see more template names in train/conversation.py89foriin{0..3};do10python ${PROJ_PATH}/train/get_data_representation.py \11 --model_name_or_path ${PROJ_PATH}/models/${MODEL_NAME}\12 --data_path ${PROJ_PATH}/data/${DATA_NAME}.json \13 --dataset_save_dir ${PROJ_PATH}/representations/${MODEL_NAME}_representation_split${i}\14 --tknz_dataset_path ${PROJ_PATH}/representations/${MODEL_NAME}_representation_tknz_split${i}\15 --cache_dir ${PROJ_PATH}/.cache/huggingface/datasets \16 --model_max_length 2048\17 --load_in_half bf16 \18 --batch_size 32\19 --top_k_logits 10\20 --save_per_token_metric \21 --no_assert \22 --conv_temp ${CONV_TEMP}\23 --mask_instruction \24 --dataset_split_num 4\25 --dataset_index ${i}\26 --get_representation \27 --device_map "auto"28done
2. Align Representations
Here we show the scripts to align representations from different source LLMs to pivot LLM.
For source LLMs share the same vocab as pivot LLM, we only merge their representations into a single dataset.
bash
1# Pivot LLM:OpenChat-3.5-7B <-> Source LLMs: Starling-LM-7B-alpha Nous-Hermes-2-SOLAR-10.7B Mixtral-8x7B-Instruct-v0.12PROJ_PATH=FuseChat # specify your own project path3PIVOT_NAME=openchat_3.5 # Pivot LLM4SOURCE_NAME=Starling-LM-7B-alpha # Source LLMs with the same vocab as Pivot56foriin{0..3};do7python ${PROJ_PATH}/train/replace_model.py \8 --dataset_dir ${PROJ_PATH}/representations/${PIVOT_NAME}_representation_split${i}\9 --replace_dataset_dir ${PROJ_PATH}/representations/${SOURCE_NAME}_representation_split${i}\10 --dataset_save_dir ${PROJ_PATH}/representations/${PIVOT_NAME}_${SOURCE_NAME}_representation_split${i}\11 --preprocessing_num_workers 32\12 --batch_size 100013done
For source LLMs have different vocabs with pivot LLM, we need to do token alignment and distribution alignment.
We show the scripts on how to get FuseChat from target LLMs using different merging methods.
Before merging, please install our modified "mergekit".
bash
1cd mergekit
2pip install -e .
Our SCE method
The implementation of our method is in mergekit/mergekit/merge_methods/sce_merging.py.
bash
1model_save_dir=xx # specify your path to save the merged models2mergekit-yaml mergekit/fusechat_configs/fusechat-sce.yml ${model_save_dir}/FuseChat-7B-SCE
Other merging methods
bash
1model_save_dir=xx # your path to save the merged models2mergekit-yaml mergekit/fusechat_configs/fusechat-linear.yml ${model_save_dir}/FuseChat-7B-LINEAR
34mergekit-yaml mergekit/fusechat_configs/fusechat-ta.yml ${model_save_dir}/FuseChat-7B-TA
56mergekit-yaml mergekit/fusechat_configs/fusechat-ties.yml ${model_save_dir}/FuseChat-7B-TIES
78mergekit-yaml mergekit/fusechat_configs/fusechat-dare.yml ${model_save_dir}/FuseChat-7B-DARE
Evaluation
We conduct experiments on two representative benchmarks named AlpacaEval 2.0 and MT-Bench to evaluate the instruction-following and multi-turn conversation capabilities.
MT-Bench
MT-Bench comprises 80 multi-turn dialogues spanning writing, roleplay, reasoning, math, coding, stem, and humanities domains.The original benchmark uses GPT-4-0613 as the evaluator to provide a scalar score ranging from 1 (lowest) to 10 (highest) for the generated responses. However, due to inaccuracies in the reference responses generated by the old GPT-4-0613, we follow the latest works to adopt an updated GPT-4-0125-Preview to correct these errors and evaluate the generated responses.
Please download the official code and follow the guidelines for evaluation. To use GPT-4-0125-Preview as judge model, you should download gpt-4-0125-preview.jsonl, and place it in llm_judge/data/mt_bench/reference_answer. Then, add "gpt-4-0125-preview" as a valid judge model in common.py.
bash
1# Step 1. Generate model answers to MT-bench questions2exportCUDA_VISIBLE_DEVICES=0,13python gen_model_answer.py \4 --model-path "FuseChat-7B-v2.0"\5 --model-id "openchat_3.5_fusechat_7b_sce"\6 --num-gpus-per-model 1\7 --num-gpus-total 289# Step 2. Generate GPT-4-0125-Preview judgments10exportOPENAI_API_KEY=XXXXXX # set the OpenAI API key11python gen_judgment.py \12 --model-list "openchat_3.5_fusechat_7b_sce"\13 --judge-model "gpt-4-0125-preview"\14 --parallel 81516# Step 3. Show MT-bench scores17python show_result.py --model-list "openchat_3.5_fusechat_7b_sce"
AlpacaEval 2.0
AlpacaEval 2.0, contains 805 instructions from five test subsets. This benchmark compares the Win Rate and Length-Controlled Win Rate (LC Win Rate) against GPT-4.
We follow the default settings to employ GPT-4-1106-Preview to assess the quality of generated responses.
Please download the official code and follow the guidelines. We use the default alpaca_eval_gpt4_turbo_fn for evaluation. The prompt for generation is:
If you find this work is relevant with your research or applications, please feel free to cite our work!
@article{wan2024fusechat,
title={FuseChat: Knowledge Fusion of Chat Models},
author={Fanqi Wan and Longguang Zhong and Ziyi Yang and Ruijun Chen and Xiaojun Quan},
journal={arXiv preprint arXiv:2408.07990},
year={2024}
}