File size: 10,511 Bytes
8e629b7
 
 
95fbfe7
47ae276
c94d951
47ae276
c94d951
47ae276
c94d951
 
 
47ae276
 
c94d951
 
 
47ae276
 
 
c94d951
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30f0f5a
c94d951
 
 
 
 
 
 
 
 
 
 
 
 
 
30f0f5a
c94d951
 
 
 
 
30f0f5a
c94d951
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30f0f5a
c94d951
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4877bfc
c94d951
 
 
 
 
4877bfc
c94d951
 
 
 
 
 
 
 
4877bfc
c94d951
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
---
license: mit
---

# Note: To comply with the LLaMA model license, we release Lion weights as _delta weights_.

# Lion: Adversarial Distillation of Closed-Source Large Language Model
<p align="center" width="100%">
  <a ><img src="https://github.com/YJiangcm/Lion/raw/master/pics/Lion.jpg" alt="Lion" style="width: 20%; min-width: 200px; display: block; margin: auto;"></a>
</p>
<p align="center">
  <a href="https://arxiv.org/abs/2305.12870">[📄 Paper]</a> | 
  <a href="https://github.com/YJiangcm/Lion">[⌨ Github]</a> |
  <a href="https://84bc5e1fdfbb976d51.gradio.live/">[💻 Demo]</a>
</p>

### Tuned on 70k instruction-following data, Lion (7B) can achieve 95% capability of ChatGPT!
<p align="center">
  <img width="500" height="250" src="https://github.com/YJiangcm/Lion/raw/master/pics/relative_quality_overall.jpg">
</p>

## News
- **[May 26, 2023]** We released the model weights. Check out the [7B](https://huggingface.co/YuxinJiang/Lion) model!
- **[May 25, 2023]** We released an [online demo](https://84bc5e1fdfbb976d51.gradio.live/), try our model here!
- **[May 23, 2023]** We released the code for training and inference.

<!-- :pray: Since our team members are perparing for the PhD Qualifying Exam, we apologize for any possible delay in responding to your questions. We warmly welcome all inquiries and appreciate your constructive feedback :) -->

## Contents 

1. [Overview](#overview) 

2. [Online Demo](#online-demo) 

3. [Recovering Lion weights](#recovering-lion-weights) 

4. [Inference](#inference)

5. [Training Process](#training-process) 

6. [Evaluation](#evaluation)

7. [Citation](#citation)

8. [Disclaimer](#disclaimer)


## Overview
<p align="center">
  <img width="700" height="320" src="https://github.com/YJiangcm/Lion/raw/master/pics/overview.jpg">
</p>

The high-level overview of our adversarial distillation framework, where we craft a compact Student LLM based on a superior closed-source LLM that serves three roles: the **Teacher**, the **Referee**, and the **Generator**. From left to right, there are three stages in an iteration:  
1) an _imitation_ stage to align the student’s response with the teacher’s response;  
2) a _discrimination_ stage to identify hard samples;  
3) a _generation_ stage to produce new hard samples for escalating the challenges presented to the student model.


## Online Demo
We will provide our latest models for you to try for as long as possible. You may ask some questions to Lion and we are happy to hear your feedback!

[**Demo Link**](https://84bc5e1fdfbb976d51.gradio.live/) (the UI interface is shown below)

<p align="center">
  <img width="800" height="350" src="https://github.com/YJiangcm/Lion/raw/master/pics/english_case2.png">
</p>

Since the training data are English instruction-following examples, You'd better ask questions in English. However, we found Lion can also understand instructions in other languages to some extent. See the following case:

<p align="center">
  <img width="800" height="350" src="https://github.com/YJiangcm/Lion/raw/master/pics/chinese_case.png">
</p>


## Recovering Lion weights
We release Lion weights as delta weights to comply with the LLaMA model license.

- [Lion-7B (delta weights)](https://huggingface.co/YuxinJiang/Lion)

You can add our delta to the original LLaMA weights to obtain the Lion weights. Instructions:
1. Get the original LLaMA weights in the huggingface format by following the instructions [here](https://huggingface.co/docs/transformers/main/model_doc/llama)
2. Please download our delta model from [Hugging Face](https://huggingface.co/YuxinJiang/Lion)  
3. Use the following scripts to get Lion weights by applying our delta:
```bash
python src/weight_diff.py recover --path_raw huggyllama/llama-7b --path_diff YuxinJiang/Lion --path_tuned <path_to_store_recovered_weights>
```

## Inference
For inference and training of Lion, please first install the requirements:
```bash
pip install -r requirements.txt
```

We provide the decoding script for Lion, which reads a input file and generates corresponding responses for each sample, and finally consolidates them into an output file.
```bash
python src/lion_inference.py \
    --model_dir <path_to_hf_converted_lion_ckpt_and_tokenizer> \
    --data_dir <path_to_input_json_file> \
    --output_dir <path_to_output_json_file> \
    --num_gpus 8
```


## Training Process
Below shows one iteration of our adversarial distillation framework.
### 1. Imitation Stage
#### 1.1 Acquire the teacher's response on the Train Pool

```bash
python src/chatgpt_inference.py \
    -q <path_to_json_file_for_the_Train_Pool> \
    -o <path_to_chatgpt_inference_for_the_Train_Pool> \
    --api_key <your_openai_api_key>
```

#### 1.2 Instruction-tuning the student based on the teacher’s response on the Train Pool

Fine-tuning was conducted on on a machine with 8 A100 80G GPUs.

```bash
torchrun --nproc_per_node=8 --master_port=<your_random_port> src/train.py \
    --model_name_or_path <path_to_hf_converted_ckpt_and_tokenizer> \
    --data_path <path_to_chatgpt_inference_for_the_Train_Pool> \
    --bf16 True \
    --output_dir result \
    --num_train_epochs 3 \
    --model_max_length 1024 \
    --per_device_train_batch_size 1 \
    --per_device_eval_batch_size 1 \
    --gradient_accumulation_steps 8 \
    --evaluation_strategy "no" \
    --save_strategy "steps" \
    --save_steps 500 \
    --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
```

### 2. Discrimination Stage
#### 2.1 Acquire the teacher's response on the Cache Pool

```bash
python src/chatgpt_inference.py \
    -q <path_to_json_file_for_the_Cache_Pool> \
    -o <path_to_chatgpt_inference_for_the_Cache_Pool> \
    --api_key <your_openai_api_key>
```

#### 2.2 Acquire the student's response on the Cache Pool

```bash
python src/lion_inference.py \
    --model_dir <path_to_hf_converted_lion_ckpt_and_tokenizer> \
    --data_dir <path_to_json_file_for_the_Cache_Pool> \
    --output_dir <path_to_lion_inference_for_the_Cache_Pool> \
    --num_gpus 8
```

#### 2.3 Ask the referee to output two scores according to the respose quality of the teacher and the student

```bash
python src/chatgpt_referee.py \
    -a <path_to_chatgpt_inference_for_the_Cache_Pool> <path_to_lion_inference_for_the_Cache_Pool> \
    -o <path_to_output_review_file> \
    --api_key <your_openai_api_key>
```

#### 2.4 Discriminate hard instructions and easy instructions

```bash
python src/discrimination.py \
    --review_path <path_to_output_review_file> \
    --chatgpt_inference_path <path_to_chatgpt_inference_for_the_Cache_Pool> \
    --lion_inference_path path_to_lion_inference_for_the_Cache_Pool \
    --hard_save_path <path_to_identified_hard_instructions> \
    --easy_save_path <path_to_identified_easy_instructions>
```

### 3. Generation Stage
Fill the `openai.api_key = "<you_openai_api_key>"` in [src/utils.py](https://github.com/YJiangcm/Lion/raw/master/src/utils.py).
#### 3.1 Generate new hard instructions

```bash
python -m src/generate_hard_instruction generate_instruction_following_data \
    --seed_tasks_path <path_to_identified_hard_instructions> \
    --output_dir <path_to_generated_hard_instructions> \
    --num_instructions_to_generate 3000
```
#### 3.2 Generate new easy instructions
```bash
python -m src/generate_easy_instruction generate_instruction_following_data \
    --seed_tasks_path <path_to_identified_easy_instructions> \
    --output_dir <path_to_generated_easy_instructions> \
    --num_instructions_to_generate 3000
```

## Evaluation

### Automatic Evaluation with GPT-4
we leverage GPT-4 to automatically rate the response quality (with scores from 1 to 10) between two models on 80 unseen [Vicuna-Instructions](https://github.com/lm-sys/FastChat/blob/main/fastchat/eval/table/question.jsonl).
ChatGPT has been chosen as the reference model to estimate the relative capability of diverse LLMs against it. The relative score is reported in percentage, computed as the ratio of the sum of scores.

**Relative Overall Response Quality**:

<p align="center">
  <img width="500" height="250" src="https://github.com/YJiangcm/Lion/raw/master/pics/relative_quality_overall.jpg">
</p>

**Relative Response Quality of Diverse Task Categories**:

<p align="center">
  <img width="700" height="330" src="https://github.com/YJiangcm/Lion/raw/master/pics/relative_quality_category.jpg">
</p>

### Human Evaluation with Alignment Criteria
We employ the alignment criteria proposed by Askell et al. (2021), which define that an assistant is considered aligned if it is characterized by being helpful, honest, and
harmless (HHH). We performed a human evaluation on 252 [UserOriented-Instructions](https://github.com/yizhongw/self-instruct/blob/main/human_eval/user_oriented_instructions.jsonl). To estimate the won rate, we compare the frequency of won, tie, and lost between each pair
of models below.

<p align="center">
  <img width="500" height="300" src="https://github.com/YJiangcm/Lion/raw/master/pics/252task_win.jpg">
</p>


## Citation
Please cite our paper if you use the code in this repo.

```
@article{DBLP:journals/corr/abs-2305-12870,
  author       = {Yuxin Jiang and
                  Chunkit Chan and
                  Mingyang Chen and
                  Wei Wang},
  title        = {Lion: Adversarial Distillation of Closed-Source Large Language Model},
  journal      = {CoRR},
  volume       = {abs/2305.12870},
  year         = {2023},
  url          = {https://doi.org/10.48550/arXiv.2305.12870},
  doi          = {10.48550/arXiv.2305.12870},
  eprinttype   = {arXiv},
  eprint       = {2305.12870},
  biburl       = {https://dblp.org/rec/journals/corr/abs-2305-12870.bib},
  bibsource    = {dblp computer science bibliography, https://dblp.org}
}
```




## Disclaimer
⚠️ Lion is intended and licensed for **research use ONLY**. Commercial use is **strictly prohibited**.
The content produced by any version of Lion is influenced by uncontrollable variables such as randomness, and therefore, the accuracy of the output cannot be guaranteed by this project. 
This project does not accept any legal liability for the content of the model output, nor does it assume responsibility for any losses incurred due to the use of associated resources and output results.