Wanfq commited on
Commit
1b3fb2f
1 Parent(s): 6c8efcf

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +443 -0
README.md ADDED
@@ -0,0 +1,443 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ language:
4
+ - en
5
+ base_model: openchat/openchat_3.5
6
+ datasets:
7
+ - FuseAI/FuseChat-Mixture
8
+ pipeline_tag: text-generation
9
+ tags:
10
+ - model-fusion
11
+ - fusechat
12
+ library_name: transformers
13
+ model-index:
14
+ - name: FuseChat-7B-v2.0
15
+ results:
16
+ - task:
17
+ type: text-generation
18
+ name: Text Generation
19
+ dataset:
20
+ name: MT-Bench (GPT-4-0125-Preview)
21
+ type: unknown
22
+ metrics:
23
+ - type: unknown
24
+ value: 7.23
25
+ name: score
26
+ source:
27
+ url: https://github.com/lm-sys/FastChat/pull/3158
28
+ - task:
29
+ type: text-generation
30
+ name: Text Generation
31
+ dataset:
32
+ name: AlpacaEval 2.0 (GPT-4-1106-Preview)
33
+ type: unknown
34
+ metrics:
35
+ - type: unknown
36
+ value: 14.98
37
+ name: lc win rate
38
+ source:
39
+ url: https://github.com/tatsu-lab/alpaca_eval
40
+ ---
41
+ <p align="center" width="100%">
42
+ </p>
43
+
44
+ <div id="top" align="center">
45
+
46
+ <p style="font-size: 30px; font-weight: bold;">FuseChat: Knowledge Fusion of Chat Models</p>
47
+
48
+ <p style="font-size: 24px; font-weight: bold;">[SOTA 7B LLM on MT-Bench]</p>
49
+
50
+ <h4> |<a href="https://arxiv.org/abs/2408.07990"> 📑 Paper </a> |
51
+ <a href="https://huggingface.co/FuseAI"> 🤗 HuggingFace Repo </a> |
52
+ <a href="https://github.com/fanqiwan/FuseLLM"> 🐱 GitHub Repo </a> |
53
+ </h4>
54
+
55
+ <!-- **Authors:** -->
56
+
57
+ _**Fanqi Wan, Longguang Zhong, Ziyi Yang, Ruijun Chen, Xiaojun Quan**_
58
+
59
+
60
+ <!-- **Affiliations:** -->
61
+
62
+
63
+ _Sun Yat-sen University_
64
+
65
+ <p align="center">
66
+ <img src="./assets/fig1.png" width="60%"> <br>
67
+ </p>
68
+
69
+
70
+ </div>
71
+
72
+
73
+ ## News
74
+ - **Aug 16, 2024:** 🔥🔥🔥 We update the [FuseChat tech report](https://arxiv.org/abs/2408.07990) and release [FuseChat-7B-v2.0](https://huggingface.co/FuseAI/FuseChat-7B-v2.0), which is the fusion of six prominent chat LLMs with diverse architectures and scales, namely [OpenChat-3.5-7B](https://huggingface.co/openchat/openchat_3.5), [Starling-LM-7B-alpha](https://huggingface.co/berkeley-nest/Starling-LM-7B-alpha), [NH2-Solar-10.7B](https://huggingface.co/NousResearch/Nous-Hermes-2-SOLAR-10.7B), [InternLM2-Chat-20B](https://huggingface.co/internlm/internlm2-chat-20b), [Mixtral-8x7B-Instruct](https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1), and [Qwen1.5-Chat-72B](https://huggingface.co/Qwen/Qwen1.5-72B-Chat). FuseChat-7B-v2.0 achieves an average performance of **7.38** on MT-Bench (GPT-4-0125-Preview as judge LLM), which is comparable to [Mixtral-8x7B-Instruct](https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1) and approaches [GPT-3.5-Turbo-1106](https://platform.openai.com/docs/models/gpt-3-5-turbo).
75
+
76
+ - **Feb 26, 2024:** 🔥🔥 We release [FuseChat-7B-VaRM](https://huggingface.co/FuseAI/FuseChat-7B-VaRM), which is the fusion of three prominent chat LLMs with diverse architectures and scales, namely [NH2-Mixtral-8x7B](https://huggingface.co/NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO), [NH2-Solar-10.7B](https://huggingface.co/NousResearch/Nous-Hermes-2-SOLAR-10.7B), and [OpenChat-3.5-7B](https://huggingface.co/openchat/openchat_3.5). FuseChat-7B-VaRM achieves an average performance of **8.22** on MT-Bench (GPT-4-1106-Preview as judge LLM), outperforming various powerful chat LLMs at 7B and 34B scales like [Starling-7B](https://huggingface.co/berkeley-nest/Starling-LM-7B-alpha) and [Yi-34B-Chat](https://huggingface.co/01-ai/Yi-34B-Chat), even surpassing [GPT-3.5 (March)](https://platform.openai.com/docs/models/gpt-3-5-turbo), [Claude-2.1](https://www.anthropic.com/news/claude-2-1), and approaching [Mixtral-8x7B-Instruct](https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1).
77
+
78
+ - **Feb 25, 2024:** 🔥 We release [FuseChat-Mixture](https://huggingface.co/datasets/FuseAI/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.
79
+
80
+ - **Aug 16, 2024:** 🔥 We release [FuseChat-7B-v2.0](https://huggingface.co/FuseAI/FuseChat-7B-v2.0), which is the fusion of six prominent chat LLMs with diverse architectures and scales, namely [OpenChat-3.5-7B](https://huggingface.co/openchat/openchat_3.5), [Starling-LM-7B-alpha](https://huggingface.co/berkeley-nest/Starling-LM-7B-alpha), [NH2-Solar-10.7B](https://huggingface.co/NousResearch/Nous-Hermes-2-SOLAR-10.7B), [InternLM2-Chat-20B](https://huggingface.co/internlm/internlm2-chat-20b), [Mixtral-8x7B-Instruct](https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1), and [Qwen1.5-Chat-72B](https://huggingface.co/Qwen/Qwen1.5-72B-Chat). FuseChat-7B-v2.0 achieves an average performance of **7.38** on MT-Bench(GPT4-0125-preview), which is comparable to [Mixtral-8x7B-Instruct](https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1) and approaches [GPT-3.5-Turbo-1106](https://platform.openai.com/docs/models/gpt-3-5-turbo).
81
+
82
+ ## Contents
83
+
84
+ - [Overview](#overview)
85
+ - [Model Release](#model-release)
86
+ - [Quick Start](#quick-start)
87
+ - [Supported Models](#supported-models)
88
+ - [Experiment Preparation](#experiment-preparation)
89
+ - [Data Construction](#data-construction)
90
+ - [Pairwise Knowledge Fusion](#pairwise-knowledge-fusion)
91
+ - [Model Merging](#model-merging)
92
+ - [Evaluation](#evaluation)
93
+ - [Citation](#citation)
94
+
95
+ ## Overview
96
+
97
+ 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.
98
+
99
+
100
+ 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.
101
+
102
+ <p align="center">
103
+ <img src="./assets/fig2.png" width="95%"> <br>
104
+ </p>
105
+
106
+
107
+ ## Model Release
108
+
109
+ We release [FuseChat-7B-v2.0](https://huggingface.co/FuseAI/FuseChat-7B-v2.0), which is the fusion of six prominent chat LLMs with diverse architectures and scales, namely [OpenChat-3.5-7B](https://huggingface.co/openchat/openchat_3.5), [Starling-LM-7B-alpha](https://huggingface.co/berkeley-nest/Starling-LM-7B-alpha), [NH2-Solar-10.7B](https://huggingface.co/NousResearch/Nous-Hermes-2-SOLAR-10.7B), [InternLM2-Chat-20B](https://huggingface.co/internlm/internlm2-chat-20b), [Mixtral-8x7B-Instruct](https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1), and [Qwen1.5-Chat-72B](https://huggingface.co/Qwen/Qwen1.5-72B-Chat). FuseChat-7B-v2.0 achieves an average performance of **7.38** on MT-Bench (GPT-4-0125-Preview as judge LLM), which is comparable to [Mixtral-8x7B-Instruct](https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1) and approaches [GPT-3.5-Turbo-1106](https://platform.openai.com/docs/models/gpt-3-5-turbo).
110
+
111
+ To support a plug-and-play fusion of new source LLM, we release our target LLMs: [OpenChat-3.5-7B-Starling-v2.0](https://huggingface.co/FuseAI/OpenChat-3.5-7B-Starling-v2.0), [OpenChat-3.5-7B-SOLAR-v2.0](https://huggingface.co/FuseAI/OpenChat-3.5-7B-SOLAR-v2.0), [OpenChat-3.5-7B-InternLM-v2.0](https://huggingface.co/FuseAI/OpenChat-3.5-7B-InternLM-v2.0), [OpenChat-3.5-7B-Mixtral-v2.0](https://huggingface.co/FuseAI/OpenChat-3.5-7B-Mixtral-v2.0), and [OpenChat-3.5-7B-Qwen-v2.0](https://huggingface.co/FuseAI/OpenChat-3.5-7B-Qwen-v2.0), which are obtained from pair-wise knowledge fusion. Integrating a new source LLM at any scale requires only obtaining a target LLM from the new source LLM and merging it with the existing target LLMs.
112
+
113
+ Here are the evaluation results.
114
+
115
+ <p align="center">
116
+ <img src="./assets/tab1.png" width="95%"> <br>
117
+ </p>
118
+
119
+ ## Quick Start
120
+
121
+ ### Setup
122
+
123
+ We use `python 3.11` in this project.
124
+
125
+ Then, we have to install all the libraries listed in `requirements.txt`.
126
+
127
+ ```bash
128
+ pip install -r requirements.txt
129
+ ```
130
+
131
+ ### Usage
132
+
133
+ Here's how you can run the model using the 🤗 Transformers:
134
+
135
+ ```python
136
+ import transformers
137
+ tokenizer = transformers.AutoTokenizer.from_pretrained("FuseAI/FuseChat-7B-v2.0")
138
+ # Single-turn
139
+ tokens = tokenizer("GPT4 Correct User: Hello<|end_of_turn|>GPT4 Correct Assistant:").input_ids
140
+ assert tokens == [1, 420, 6316, 28781, 3198, 3123, 1247, 28747, 22557, 32000, 420, 6316, 28781, 3198, 3123, 21631, 28747]
141
+ # Multi-turn
142
+ tokens = tokenizer("GPT4 Correct User: Hello<|end_of_turn|>GPT4 Correct Assistant: Hi<|end_of_turn|>GPT4 Correct User: How are you today?<|end_of_turn|>GPT4 Correct Assistant:").input_ids
143
+ assert 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]
144
+ ```
145
+
146
+ The GPT4 template is also available as the integrated `tokenizer.chat_template`, which can be used instead of manually specifying the template:
147
+
148
+ ```python
149
+ messages = [
150
+ {"role": "user", "content": "Hello"},
151
+ {"role": "assistant", "content": "Hi"},
152
+ {"role": "user", "content": "How are you today?"}
153
+ ]
154
+ tokens = tokenizer.apply_chat_template(messages, add_generation_prompt=True)
155
+ assert 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]
156
+ ```
157
+ ## Supported Models
158
+
159
+ | Model | Model size | Chat Template |
160
+ |-----------------------------------------------------------------------------------------------------------------------------|----------------------------------|---------------|
161
+ | [openchat_3.5](https://huggingface.co/openchat)/[Starling-LM-7B-alpha](https://huggingface.co/berkeley-nest/Starling-LM-7B-alpha) | 7B | openchat_3.5 |
162
+ | [Mistral/Mixtral](https://huggingface.co/mistralai) | 7B/8x7B/8x22B | mistral |
163
+ | [Llama 3/Llama 3.1](https://huggingface.co/meta-llama) | 8B/70B | llama-3 |
164
+ | [Gemma/Gemma 2/CodeGemma](https://huggingface.co/google) | 2B/7B/9B/27B | gemma |
165
+ | [Phi-3](https://huggingface.co/microsoft) | 4B/7B/14B | phi-3 |
166
+ | [Qwen1.5/Qwen2](https://huggingface.co/Qwen) | 0.5B/1.5B/4B/7B/14B/32B/72B/110B | qwen |
167
+ | [InternLM2/InternLM2.5](https://huggingface.co/internlm) | 7B/20B | internlm2 |
168
+ | [Yi/Yi-1.5](https://huggingface.co/01-ai) | 6B/9B/34B | yi |
169
+
170
+
171
+ To support a new model in FuseChat, you'll need to follow these steps:
172
+
173
+ 1. Implement a conversation template for the new model at [conversation.py](FuseChat/train/conversation.py). You can follow existing examples and use `register_conv_template` to add a new one.
174
+ 2. Implement a model adapter for the new model at [model/model_adapter.py](FuseChat/train/model/model_adapter.py). You can follow existing examples and use `register_model_adapter` to add a new one.
175
+ 3. Modify the `preprocess()` Function in [train.py](FuseChat/train/train.py).
176
+
177
+
178
+ ## Experiment Preparation
179
+
180
+ ### Source LLMs
181
+
182
+ We conduct experiments using six representative chat LLMs as the source LLMs, including [OpenChat-3.5-7B](https://huggingface.co/openchat/openchat_3.5), [Starling-LM-7B-alpha](https://huggingface.co/berkeley-nest/Starling-LM-7B-alpha), [NH2-SOLAR-10.7B](https://huggingface.co/NousResearch/Nous-Hermes-2-SOLAR-10.7B), [InternLM2-Chat-20B](https://huggingface.co/bartowski/internlm2-chat-20b-llama-old), [Mixtral-8x7B-Instruct](https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1), [Qwen-1.5-Chat-72B](https://huggingface.co/Qwen/Qwen1.5-72B-Chat). As for the pivot LLM, which also serves as the starting point for the target LLMs, we opt for [OpenChat-3.5-7B](https://huggingface.co/openchat/openchat_3.5) due to its balanced scale and performance. You should download all these models and place them into `/models` before experiments.
183
+
184
+ ### Training Dataset
185
+
186
+ We curated a comprehensive training dataset, [FuseChat-Mixture](https://huggingface.co/datasets/FuseAI/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.
187
+
188
+ ## Data Construction
189
+
190
+ Here we show the scripts to obtain representations from multiple source LLMs for model fusion with the following three steps.
191
+
192
+ ### 1. Get Representations
193
+
194
+ Here we show the scripts to obtain representations from multiple source LLMs.
195
+
196
+ ```bash
197
+ # We split the dataset into 4 splits, then process each split on one or multiple GPUs.
198
+ # 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-Chat
199
+ export CUDA_VISIBLE_DEVICES=0 # specify one or multiple GPUs
200
+ PROJ_PATH=FuseChat # specify your own project path
201
+ DATA_NAME="fusechat_v1_clean_split_2048_filter_wrong"
202
+ MODEL_NAME=openchat_3.5 # model to get representation
203
+ CONV_TEMP=openchat_3.5 # conversation template, should be the same for all models, see more template names in train/conversation.py
204
+
205
+ for i in {0..3}; do
206
+ python ${PROJ_PATH}/train/get_data_representation.py \
207
+ --model_name_or_path ${PROJ_PATH}/models/${MODEL_NAME} \
208
+ --data_path ${PROJ_PATH}/data/${DATA_NAME}.json \
209
+ --dataset_save_dir ${PROJ_PATH}/representations/${MODEL_NAME}_representation_split${i} \
210
+ --tknz_dataset_path ${PROJ_PATH}/representations/${MODEL_NAME}_representation_tknz_split${i} \
211
+ --cache_dir ${PROJ_PATH}/.cache/huggingface/datasets \
212
+ --model_max_length 2048 \
213
+ --load_in_half bf16 \
214
+ --batch_size 32 \
215
+ --top_k_logits 10 \
216
+ --save_per_token_metric \
217
+ --no_assert \
218
+ --conv_temp ${CONV_TEMP} \
219
+ --mask_instruction \
220
+ --dataset_split_num 4 \
221
+ --dataset_index ${i} \
222
+ --get_representation \
223
+ --device_map "auto"
224
+ done
225
+ ```
226
+
227
+ ### 2. Align Representations
228
+
229
+ Here we show the scripts to align representations from different source LLMs to pivot LLM.
230
+
231
+ For source LLMs share the **same vocab** as pivot LLM, we only merge their representations into a single dataset.
232
+
233
+ ```bash
234
+ # Pivot LLM:OpenChat-3.5-7B <-> Source LLMs: Starling-LM-7B-alpha Nous-Hermes-2-SOLAR-10.7B Mixtral-8x7B-Instruct-v0.1
235
+ PROJ_PATH=FuseChat # specify your own project path
236
+ PIVOT_NAME=openchat_3.5 # Pivot LLM
237
+ SOURCE_NAME=Starling-LM-7B-alpha # Source LLMs with the same vocab as Pivot
238
+
239
+ for i in {0..3}; do
240
+ python ${PROJ_PATH}/train/replace_model.py \
241
+ --dataset_dir ${PROJ_PATH}/representations/${PIVOT_NAME}_representation_split${i} \
242
+ --replace_dataset_dir ${PROJ_PATH}/representations/${SOURCE_NAME}_representation_split${i} \
243
+ --dataset_save_dir ${PROJ_PATH}/representations/${PIVOT_NAME}_${SOURCE_NAME}_representation_split${i} \
244
+ --preprocessing_num_workers 32 \
245
+ --batch_size 1000
246
+ done
247
+ ```
248
+
249
+ For source LLMs have **different vocabs** with pivot LLM, we need to do token alignment and distribution alignment.
250
+
251
+ ```bash
252
+ # Pivot LLM:OpenChat-3.5-7B <->Source LLMs: internlm2-chat-20b Qwen1.5-72B-Chat
253
+ PROJ_PATH=FuseChat # specify your own project path
254
+ PIVOT_NAME=openchat_3.5 # Pivot LLM
255
+ SOURCE_NAME=internlm2-chat-20b # Source LLMs have different vocab with Pivot
256
+ align_type="default" # different alignment method hard -> EM, soft -> MinED, default -> MS
257
+ token_alignment_matrix_file=${PROJ_PATH}/aligned_representations/${PIVOT_NAME}_${SOURCE_NAME}_token_sparse_matrix_${align_type}.npz
258
+ blending_to_base_file=${PROJ_PATH}/aligned_representations/${PIVOT_NAME}_${SOURCE_NAME}_token_mapping_${align_type}.json
259
+
260
+ # token alignment
261
+ python ${PROJ_PATH}/train/align_token_and_vocab.py \
262
+ --align_type ${align_type} \
263
+ --base_model_name_or_path ${PROJ_PATH}/models/${PIVOT_NAME} \
264
+ --blending_model_name_or_path ${PROJ_PATH}/models/${SOURCE_NAME} \
265
+ --base_dataset_dir "${PROJ_PATH}/representations/${PIVOT_NAME}_representation_tknz_split0,${PROJ_PATH}/representations/${PIVOT_NAME}_representation_tknz_split1,${PROJ_PATH}/representations/${PIVOT_NAME}_representation_tknz_split2,${PROJ_PATH}/representations/${PIVOT_NAME}_representation_tknz_split3" \
266
+ --blending_dataset_dir "${PROJ_PATH}/representations/${SOURCE_NAME}_representation_tknz_split0,${PROJ_PATH}/representations/${SOURCE_NAME}_representation_tknz_split1,${PROJ_PATH}/representations/${SOURCE_NAME}_representation_tknz_split2,${PROJ_PATH}/representations/${SOURCE_NAME}_representation_tknz_split3" \
267
+ --aligned_dataset_tknz_save_dir ${PROJ_PATH}/aligned_representations/${PIVOT_NAME}_${SOURCE_NAME}_representation_tknz \
268
+ --model_max_length 2048 \
269
+ --preprocessing_num_workers 32 \
270
+ --batch_size 16 \
271
+ --token_alignment_matrix_file ${token_alignment_matrix_file} \
272
+ --blending_to_base_file ${blending_to_base_file} \
273
+ --do_token_alignment \
274
+ --metric_level "sequence" \
275
+ --use_token_alignment_matrix
276
+
277
+ # distribution alignment
278
+ for i in {0..3}; do
279
+ python ${PROJ_PATH}/train/align_token_and_vocab.py \
280
+ --align_type ${align_type} \
281
+ --base_model_name_or_path ${PROJ_PATH}/models/${PIVOT_NAME} \
282
+ --blending_model_name_or_path ${PROJ_PATH}/models/${SOURCE_NAME} \
283
+ --base_dataset_dir ${PROJ_PATH}/representations/${PIVOT_NAME}_representation_split${i} \
284
+ --blending_dataset_dir ${PROJ_PATH}/representations/${SOURCE_NAME}_representation_split${i} \
285
+ --aligned_dataset_save_dir ${PROJ_PATH}/aligned_representations/${PIVOT_NAME}_${SOURCE_NAME}_representation_split${i} \
286
+ --model_max_length 2048 \
287
+ --preprocessing_num_workers 32 \
288
+ --batch_size 16 \
289
+ --temperature 0.5 \
290
+ --token_alignment_matrix_file ${token_alignment_matrix_file} \
291
+ --blending_to_base_file ${blending_to_base_file} \
292
+ --do_distribution_alignment \
293
+ --metric_level "sequence" \
294
+ --use_token_alignment_matrix
295
+ done
296
+ ```
297
+
298
+ ### 3. Filter NaN
299
+
300
+ Here we show the scripts to filter instance with NaN "metric_ce" in the dataset.
301
+
302
+ ```bash
303
+ for i in {0..3}; do
304
+ python ${PROJ_PATH}/train/filter_nan.py \
305
+ --input_data_dir ${PROJ_PATH}/aligned_representations/${PIVOT_NAME}_${SOURCE_NAME}_representation_split${i} \
306
+ --output_data_dir ${PROJ_PATH}/aligned_representations/${PIVOT_NAME}_${SOURCE_NAME}_representation_split${i}_fnan \
307
+ done
308
+ ```
309
+
310
+ The processed representations data are in the following path:
311
+
312
+ `${PROJ_PATH}/aligned_representations/${PIVOT_NAME}_${SOURCE_NAME}_representation_split${i}_fnan`
313
+
314
+ ## Pairwise Knowledge Fusion
315
+
316
+ We show the scripts for pairwise knowledge fusion with the processed representations.
317
+
318
+ ```bash
319
+ # 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-Chat
320
+ export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
321
+ torchrun --nproc_per_node=8 --master_port=20001 ${PROJ_PATH}/train/train.py \
322
+ --model_name_or_path "openchat/openchat_3.5" \
323
+ --data_path "${PROJ_PATH}/aligned_representations/${PIVOT_NAME}_${SOURCE_NAME}_representation_split0_fnan,${PROJ_PATH}/aligned_representations/${PIVOT_NAME}_${SOURCE_NAME}_representation_split1_fnan,${PROJ_PATH}/aligned_representations/${PIVOT_NAME}_${SOURCE_NAME}_representation_split2_fnan,${PROJ_PATH}/aligned_representations/${PIVOT_NAME}_${SOURCE_NAME}_representation_split3_fnan" \
324
+ --bf16 True \
325
+ --output_dir "${PROJ_PATH}/checkpoints/${PIVOT_NAME}_${SOURCE_NAME}_pairwise_fusion_ckpt" \
326
+ --num_train_epochs 3 \
327
+ --per_device_train_batch_size 4 \
328
+ --per_device_eval_batch_size 4 \
329
+ --gradient_accumulation_steps 4 \
330
+ --evaluation_strategy "no" \
331
+ --save_strategy "epoch" \
332
+ --save_steps 10000 \
333
+ --save_total_limit 5 \
334
+ --learning_rate 5e-6 \
335
+ --weight_decay 0. \
336
+ --warmup_ratio 0.03 \
337
+ --lr_scheduler_type "cosine" \
338
+ --logging_steps 1 \
339
+ --fsdp "full_shard auto_wrap" \
340
+ --fsdp_transformer_layer_cls_to_wrap 'MistralDecoderLayer' \
341
+ --tf32 True \
342
+ --model_max_length 2048 \
343
+ --gradient_checkpointing True \
344
+ --conv_temp "openchat_3.5" \
345
+ --lazy_preprocess True \
346
+ --flash_attn_transformers True \
347
+ --do_train \
348
+ --do_fuse \
349
+ --fuse_with_ref_model True \
350
+ --fuse_loss_type "ce" \
351
+ --fuse_temperature 1.0 \
352
+ --lm_loss_weight 0.9 \
353
+ --dataloader_num_workers 8 \
354
+ --remove_unused_columns False
355
+ ```
356
+
357
+ ## Model Merging
358
+
359
+ We show the scripts on how to get FuseChat from target LLMs using different merging methods.
360
+
361
+ Before merging, please install our modified ["mergekit"](https://github.com/arcee-ai/mergekit).
362
+
363
+ ```bash
364
+ cd mergekit
365
+ pip install -e .
366
+ ```
367
+
368
+ ### Our SCE method
369
+
370
+ The implementation of our method is in `mergekit/mergekit/merge_methods/sce_merging.py`.
371
+
372
+ ```bash
373
+ model_save_dir=xx # specify your path to save the merged models
374
+ mergekit-yaml mergekit/fusechat_configs/fusechat-sce.yml ${model_save_dir}/FuseChat-7B-SCE
375
+ ```
376
+
377
+ ### Other merging methods
378
+
379
+ ```bash
380
+ model_save_dir=xx # your path to save the merged models
381
+ mergekit-yaml mergekit/fusechat_configs/fusechat-linear.yml ${model_save_dir}/FuseChat-7B-LINEAR
382
+
383
+ mergekit-yaml mergekit/fusechat_configs/fusechat-ta.yml ${model_save_dir}/FuseChat-7B-TA
384
+
385
+ mergekit-yaml mergekit/fusechat_configs/fusechat-ties.yml ${model_save_dir}/FuseChat-7B-TIES
386
+
387
+ mergekit-yaml mergekit/fusechat_configs/fusechat-dare.yml ${model_save_dir}/FuseChat-7B-DARE
388
+ ```
389
+
390
+
391
+
392
+ ## Evaluation
393
+
394
+ We conduct experiments on two representative benchmarks named AlpacaEval 2.0 and MT-Bench to evaluate the instruction-following and multi-turn conversation capabilities.
395
+
396
+ ### MT-Bench
397
+
398
+ 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](https://github.com/lm-sys/FastChat/pull/3158) to adopt an updated GPT-4-0125-Preview to correct these errors and evaluate the generated responses.
399
+
400
+ Please download the [official code](https://github.com/lm-sys/FastChat/tree/main/fastchat/llm_judge) and follow the guidelines for evaluation. To use GPT-4-0125-Preview as judge model, you should download [gpt-4-0125-preview.jsonl](https://github.com/lm-sys/FastChat/pull/3158/files), 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`.
401
+
402
+ ```bash
403
+ # Step 1. Generate model answers to MT-bench questions
404
+ export CUDA_VISIBLE_DEVICES=0,1
405
+ python gen_model_answer.py \
406
+ --model-path "FuseChat-7B-v2.0" \
407
+ --model-id "openchat_3.5_fusechat_7b_sce" \
408
+ --num-gpus-per-model 1 \
409
+ --num-gpus-total 2
410
+
411
+ # Step 2. Generate GPT-4-0125-Preview judgments
412
+ export OPENAI_API_KEY=XXXXXX # set the OpenAI API key
413
+ python gen_judgment.py \
414
+ --model-list "openchat_3.5_fusechat_7b_sce" \
415
+ --judge-model "gpt-4-0125-preview" \
416
+ --parallel 8
417
+
418
+ # Step 3. Show MT-bench scores
419
+ python show_result.py --model-list "openchat_3.5_fusechat_7b_sce"
420
+ ```
421
+
422
+ ### AlpacaEval 2.0
423
+
424
+ 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.
425
+ We follow the default settings to employ GPT-4-1106-Preview to assess the quality of generated responses.
426
+
427
+ Please download the [official code](https://github.com/tatsu-lab/alpaca_eval) and follow the guidelines. We use the default `alpaca_eval_gpt4_turbo_fn` for evaluation. The prompt for generation is:
428
+
429
+ ```
430
+ GPT4 Correct User: {instruction}<|end_of_turn|>GPT4 Correct Assistant:
431
+ ```
432
+
433
+ ## Citation
434
+
435
+ If you find this work is relevant with your research or applications, please feel free to cite our work!
436
+ ```
437
+ @article{wan2024fusechat,
438
+ title={FuseChat: Knowledge Fusion of Chat Models},
439
+ author={Fanqi Wan and Longguang Zhong and Ziyi Yang and Ruijun Chen and Xiaojun Quan},
440
+ journal={arXiv preprint arXiv:2408.07990},
441
+ year={2024}
442
+ }
443
+ ```