README_zh.md 5.66 KB
Newer Older
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1
2
我们提供了多样化的大模型微调示例脚本。

luopl's avatar
luopl committed
3
请确保在 `LLaMA-Factory` 目录下执行下述命令。
chenych's avatar
chenych committed
4
5
6
7
8
9
10
11
12
13

## 目录

- [LoRA 微调](#lora-微调)
- [QLoRA 微调](#qlora-微调)
- [全参数微调](#全参数微调)
- [合并 LoRA 适配器与模型量化](#合并-lora-适配器与模型量化)
- [推理 LoRA 模型](#推理-lora-模型)
- [杂项](#杂项)

luopl's avatar
luopl committed
14
使用 `CUDA_VISIBLE_DEVICES`(GPU)或 `ASCEND_RT_VISIBLE_DEVICES`(NPU)选择计算设备。
chenych's avatar
chenych committed
15

luopl's avatar
luopl committed
16
17
LLaMA-Factory 默认使用所有可见的计算设备。

chenych's avatar
chenych committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
## 示例

### LoRA 微调

#### (增量)预训练

```bash
llamafactory-cli train examples/train_lora/llama3_lora_pretrain.yaml
```

#### 指令监督微调

```bash
llamafactory-cli train examples/train_lora/llama3_lora_sft.yaml
```

#### 多模态指令监督微调

```bash
llamafactory-cli train examples/train_lora/llava1_5_lora_sft.yaml
luopl's avatar
luopl committed
38
llamafactory-cli train examples/train_lora/qwen2vl_lora_sft.yaml
chenych's avatar
chenych committed
39
40
```

luopl's avatar
luopl committed
41
#### DPO/ORPO/SimPO 训练
chenych's avatar
chenych committed
42
43

```bash
luopl's avatar
luopl committed
44
llamafactory-cli train examples/train_lora/llama3_lora_dpo.yaml
chenych's avatar
chenych committed
45
46
```

luopl's avatar
luopl committed
47
#### 多模态 DPO/ORPO/SimPO 训练
chenych's avatar
chenych committed
48
49

```bash
luopl's avatar
luopl committed
50
llamafactory-cli train examples/train_lora/qwen2vl_lora_dpo.yaml
chenych's avatar
chenych committed
51
52
```

luopl's avatar
luopl committed
53
#### 奖励模型训练
chenych's avatar
chenych committed
54
55

```bash
luopl's avatar
luopl committed
56
57
58
59
60
61
62
llamafactory-cli train examples/train_lora/llama3_lora_reward.yaml
```

#### PPO 训练

```bash
llamafactory-cli train examples/train_lora/llama3_lora_ppo.yaml
chenych's avatar
chenych committed
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
```

#### KTO 训练

```bash
llamafactory-cli train examples/train_lora/llama3_lora_kto.yaml
```

#### 预处理数据集

对于大数据集有帮助,在配置中使用 `tokenized_path` 以加载预处理后的数据集。

```bash
llamafactory-cli train examples/train_lora/llama3_preprocess.yaml
```

#### 在 MMLU/CMMLU/C-Eval 上评估

```bash
llamafactory-cli eval examples/train_lora/llama3_lora_eval.yaml
```

#### 多机指令监督微调

```bash
luopl's avatar
luopl committed
88
89
FORCE_TORCHRUN=1 NNODES=2 NODE_RANK=0 MASTER_ADDR=192.168.0.1 MASTER_PORT=29500 llamafactory-cli train examples/train_lora/llama3_lora_sft.yaml
FORCE_TORCHRUN=1 NNODES=2 NODE_RANK=1 MASTER_ADDR=192.168.0.1 MASTER_PORT=29500 llamafactory-cli train examples/train_lora/llama3_lora_sft.yaml
chenych's avatar
chenych committed
90
91
92
93
94
95
96
97
```

#### 使用 DeepSpeed ZeRO-3 平均分配显存

```bash
FORCE_TORCHRUN=1 llamafactory-cli train examples/train_lora/llama3_lora_sft_ds3.yaml
```

luopl's avatar
luopl committed
98
99
100
101
102
103
#### 使用 Ray 在 4 张 GPU 上微调

```bash
USE_RAY=1 llamafactory-cli train examples/train_full/llama3_lora_sft_ray.yaml
```

chenych's avatar
chenych committed
104
105
106
107
108
109
110
111
### QLoRA 微调

#### 基于 4/8 比特 Bitsandbytes/HQQ/EETQ 量化进行指令监督微调(推荐)

```bash
llamafactory-cli train examples/train_qlora/llama3_lora_sft_otfq.yaml
```

luopl's avatar
luopl committed
112
113
114
115
116
117
#### 在 NPU 上基于 4 比特 Bitsandbytes 量化进行指令监督微调

```bash
llamafactory-cli train examples/train_qlora/llama3_lora_sft_bnb_npu.yaml
```

chenych's avatar
chenych committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#### 基于 4/8 比特 GPTQ 量化进行指令监督微调

```bash
llamafactory-cli train examples/train_qlora/llama3_lora_sft_gptq.yaml
```

#### 基于 4 比特 AWQ 量化进行指令监督微调

```bash
llamafactory-cli train examples/train_qlora/llama3_lora_sft_awq.yaml
```

#### 基于 2 比特 AQLM 量化进行指令监督微调

```bash
llamafactory-cli train examples/train_qlora/llama3_lora_sft_aqlm.yaml
```

### 全参数微调

#### 在单机上进行指令监督微调

```bash
luopl's avatar
luopl committed
141
FORCE_TORCHRUN=1 llamafactory-cli train examples/train_full/llama3_full_sft.yaml
chenych's avatar
chenych committed
142
143
144
145
146
```

#### 在多机上进行指令监督微调

```bash
luopl's avatar
luopl committed
147
148
FORCE_TORCHRUN=1 NNODES=2 NODE_RANK=0 MASTER_ADDR=192.168.0.1 MASTER_PORT=29500 llamafactory-cli train examples/train_full/llama3_full_sft.yaml
FORCE_TORCHRUN=1 NNODES=2 NODE_RANK=1 MASTER_ADDR=192.168.0.1 MASTER_PORT=29500 llamafactory-cli train examples/train_full/llama3_full_sft.yaml
chenych's avatar
chenych committed
149
150
```

luopl's avatar
luopl committed
151
152
153
154
155
156
#### 多模态指令监督微调

```bash
FORCE_TORCHRUN=1 llamafactory-cli train examples/train_full/qwen2vl_full_sft.yaml
```

chenych's avatar
chenych committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
### 合并 LoRA 适配器与模型量化

#### 合并 LoRA 适配器

注:请勿使用量化后的模型或 `quantization_bit` 参数来合并 LoRA 适配器。

```bash
llamafactory-cli export examples/merge_lora/llama3_lora_sft.yaml
```

#### 使用 AutoGPTQ 量化模型

```bash
llamafactory-cli export examples/merge_lora/llama3_gptq.yaml
```

### 推理 LoRA 模型

luopl's avatar
luopl committed
175
176
177
178
179
180
181
#### 使用 vLLM+TP 批量推理

```
python scripts/vllm_infer.py --model_name_or_path path_to_merged_model --dataset alpaca_en_demo
```

#### 使用命令行对话框
chenych's avatar
chenych committed
182
183
184
185
186

```bash
llamafactory-cli chat examples/inference/llama3_lora_sft.yaml
```

luopl's avatar
luopl committed
187
#### 使用浏览器对话框
chenych's avatar
chenych committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

```bash
llamafactory-cli webchat examples/inference/llama3_lora_sft.yaml
```

#### 启动 OpenAI 风格 API

```bash
llamafactory-cli api examples/inference/llama3_lora_sft.yaml
```

### 杂项

#### 使用 GaLore 进行全参数训练

```bash
llamafactory-cli train examples/extras/galore/llama3_full_sft.yaml
```

luopl's avatar
luopl committed
207
208
209
210
211
212
#### 使用 APOLLO 进行全参数训练

```bash
llamafactory-cli train examples/extras/apollo/llama3_full_sft.yaml
```

chenych's avatar
chenych committed
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
#### 使用 BAdam 进行全参数训练

```bash
llamafactory-cli train examples/extras/badam/llama3_full_sft.yaml
```

#### 使用 Adam-mini 进行全参数训练

```bash
llamafactory-cli train examples/extras/adam_mini/qwen2_full_sft.yaml
```

#### LoRA+ 微调

```bash
llamafactory-cli train examples/extras/loraplus/llama3_lora_sft.yaml
```

#### PiSSA 微调

```bash
llamafactory-cli train examples/extras/pissa/llama3_lora_sft.yaml
```

#### 深度混合微调

```bash
llamafactory-cli train examples/extras/mod/llama3_full_sft.yaml
```

#### LLaMA-Pro 微调

```bash
bash examples/extras/llama_pro/expand.sh
llamafactory-cli train examples/extras/llama_pro/llama3_freeze_sft.yaml
```

#### FSDP+QLoRA 微调

```bash
bash examples/extras/fsdp_qlora/train.sh
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
254
```
luopl's avatar
luopl committed
255
256
257
258
259
260

#### 计算 BLEU 和 ROUGE 分数

```bash
llamafactory-cli train examples/extras/nlg_eval/llama3_lora_predict.yaml
```