README_zh.md 6.72 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
基础用法:

```bash
shihm's avatar
uodata  
shihm committed
21
llamafactory-cli train examples/train_lora/qwen3_lora_sft.yaml
chenych's avatar
chenych committed
22
23
24
25
26
```

高级用法:

```bash
shihm's avatar
uodata  
shihm committed
27
CUDA_VISIBLE_DEVICES=0,1 llamafactory-cli train examples/train_lora/qwen3_lora_sft.yaml \
chenych's avatar
chenych committed
28
29
30
31
32
    learning_rate=1e-5 \
    logging_steps=1
```

```bash
shihm's avatar
uodata  
shihm committed
33
bash examples/train_lora/qwen3_lora_sft.sh
chenych's avatar
chenych committed
34
35
```

chenych's avatar
chenych committed
36
37
38
39
40
41
42
## 示例

### LoRA 微调

#### (增量)预训练

```bash
shihm's avatar
uodata  
shihm committed
43
llamafactory-cli train examples/train_lora/qwen3_lora_pretrain.yaml
chenych's avatar
chenych committed
44
45
46
47
48
```

#### 指令监督微调

```bash
shihm's avatar
uodata  
shihm committed
49
llamafactory-cli train examples/train_lora/qwen3_lora_sft.yaml
chenych's avatar
chenych committed
50
51
52
53
54
```

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

```bash
shihm's avatar
uodata  
shihm committed
55
llamafactory-cli train examples/train_lora/qwen3vl_lora_sft.yaml
chenych's avatar
chenych committed
56
57
```

luopl's avatar
luopl committed
58
#### DPO/ORPO/SimPO 训练
chenych's avatar
chenych committed
59
60

```bash
shihm's avatar
uodata  
shihm committed
61
llamafactory-cli train examples/train_lora/qwen3_lora_dpo.yaml
chenych's avatar
chenych committed
62
63
```

luopl's avatar
luopl committed
64
#### 多模态 DPO/ORPO/SimPO 训练
chenych's avatar
chenych committed
65
66

```bash
shihm's avatar
uodata  
shihm committed
67
llamafactory-cli train examples/train_lora/qwen3vl_lora_dpo.yaml
chenych's avatar
chenych committed
68
69
```

luopl's avatar
luopl committed
70
#### 奖励模型训练
chenych's avatar
chenych committed
71
72

```bash
shihm's avatar
uodata  
shihm committed
73
llamafactory-cli train examples/train_lora/qwen3_lora_reward.yaml
chenych's avatar
chenych committed
74
75
76
77
78
```

#### KTO 训练

```bash
shihm's avatar
uodata  
shihm committed
79
llamafactory-cli train examples/train_lora/qwen3_lora_kto.yaml
chenych's avatar
chenych committed
80
81
82
83
84
85
86
```

#### 预处理数据集

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

```bash
shihm's avatar
uodata  
shihm committed
87
llamafactory-cli train examples/train_lora/qwen3_preprocess.yaml
chenych's avatar
chenych committed
88
89
90
91
92
```

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

```bash
shihm's avatar
uodata  
shihm committed
93
94
FORCE_TORCHRUN=1 NNODES=2 NODE_RANK=0 MASTER_ADDR=192.168.0.1 MASTER_PORT=29500 llamafactory-cli train examples/train_lora/qwen3_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/qwen3_lora_sft.yaml
chenych's avatar
chenych committed
95
96
```

chenych's avatar
chenych committed
97
98
99
100
101
### 支持弹性和容错的多机指令监督微调

要启动一个支持弹性节点和容错的多机指令微调,在每个节点上执行以下命令。弹性节点数量范围为 `MIN_NNODES:MAX_NNODES`,每个节点最多允许因为错误重启 `MAX_RESTARTS` 次。`RDZV_ID` 应设置为一个唯一的作业 ID(由参与该作业的所有节点共享)。更多新可以参考官方文档 [torchrun](https://docs.pytorch.org/docs/stable/elastic/run.html)

```bash
shihm's avatar
uodata  
shihm committed
102
FORCE_TORCHRUN=1 MIN_NNODES=1 MAX_NNODES=3 MAX_RESTARTS=3 RDZV_ID=llamafactory MASTER_ADDR=192.168.0.1 MASTER_PORT=29500 llamafactory-cli train examples/train_full/qwen3_full_sft.yaml
chenych's avatar
chenych committed
103
104
```

chenych's avatar
chenych committed
105
106
107
#### 使用 DeepSpeed ZeRO-3 平均分配显存

```bash
shihm's avatar
uodata  
shihm committed
108
FORCE_TORCHRUN=1 llamafactory-cli train examples/train_lora/qwen3_lora_sft_ds3.yaml
chenych's avatar
chenych committed
109
110
```

luopl's avatar
luopl committed
111
112
113
#### 使用 Ray 在 4 张 GPU 上微调

```bash
shihm's avatar
uodata  
shihm committed
114
USE_RAY=1 llamafactory-cli train examples/train_lora/qwen3_lora_sft_ray.yaml
luopl's avatar
luopl committed
115
116
```

chenych's avatar
chenych committed
117
118
119
120
121
### QLoRA 微调

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

```bash
shihm's avatar
uodata  
shihm committed
122
llamafactory-cli train examples/train_qlora/qwen3_lora_sft_otfq.yaml
chenych's avatar
chenych committed
123
124
```

luopl's avatar
luopl committed
125
126
127
#### 在 NPU 上基于 4 比特 Bitsandbytes 量化进行指令监督微调

```bash
shihm's avatar
uodata  
shihm committed
128
llamafactory-cli train examples/train_qlora/qwen3_lora_sft_bnb_npu.yaml
luopl's avatar
luopl committed
129
130
```

chenych's avatar
chenych committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#### 基于 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
shihm's avatar
uodata  
shihm committed
154
FORCE_TORCHRUN=1 llamafactory-cli train examples/train_full/qwen3_full_sft.yaml
chenych's avatar
chenych committed
155
156
157
158
159
```

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

```bash
shihm's avatar
uodata  
shihm committed
160
161
FORCE_TORCHRUN=1 NNODES=2 NODE_RANK=0 MASTER_ADDR=192.168.0.1 MASTER_PORT=29500 llamafactory-cli train examples/train_full/qwen3_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/qwen3_full_sft.yaml
chenych's avatar
chenych committed
162
163
```

luopl's avatar
luopl committed
164
165
166
#### 多模态指令监督微调

```bash
shihm's avatar
uodata  
shihm committed
167
FORCE_TORCHRUN=1 llamafactory-cli train examples/train_full/qwen3vl_full_sft.yaml
luopl's avatar
luopl committed
168
169
```

chenych's avatar
chenych committed
170
171
172
173
174
175
176
### 合并 LoRA 适配器与模型量化

#### 合并 LoRA 适配器

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

```bash
shihm's avatar
uodata  
shihm committed
177
llamafactory-cli export examples/merge_lora/qwen3_lora_sft.yaml
chenych's avatar
chenych committed
178
179
180
181
182
```

#### 使用 AutoGPTQ 量化模型

```bash
shihm's avatar
uodata  
shihm committed
183
llamafactory-cli export examples/merge_lora/qwen3_gptq.yaml
chenych's avatar
chenych committed
184
185
```

chenych's avatar
chenych committed
186
187
188
### 保存 Ollama 配置文件

```bash
shihm's avatar
uodata  
shihm committed
189
llamafactory-cli export examples/merge_lora/qwen3_full_sft.yaml
chenych's avatar
chenych committed
190
191
```

chenych's avatar
chenych committed
192
193
### 推理 LoRA 模型

chenych's avatar
chenych committed
194
#### 使用 vLLM 多卡推理评估
luopl's avatar
luopl committed
195
196

```
shihm's avatar
uodata  
shihm committed
197
python scripts/vllm_infer.py --model_name_or_path Qwen/Qwen3-4B-Instruct-2507 --template qwen3_nothink --dataset alpaca_en_demo
chenych's avatar
chenych committed
198
python scripts/eval_bleu_rouge.py generated_predictions.jsonl
luopl's avatar
luopl committed
199
200
201
```

#### 使用命令行对话框
chenych's avatar
chenych committed
202
203

```bash
shihm's avatar
uodata  
shihm committed
204
llamafactory-cli chat examples/inference/qwen3_lora_sft.yaml
chenych's avatar
chenych committed
205
206
```

luopl's avatar
luopl committed
207
#### 使用浏览器对话框
chenych's avatar
chenych committed
208
209

```bash
shihm's avatar
uodata  
shihm committed
210
llamafactory-cli webchat examples/inference/qwen3_lora_sft.yaml
chenych's avatar
chenych committed
211
212
213
214
215
```

#### 启动 OpenAI 风格 API

```bash
shihm's avatar
uodata  
shihm committed
216
llamafactory-cli api examples/inference/qwen3_lora_sft.yaml
chenych's avatar
chenych committed
217
218
219
220
221
222
223
224
225
226
```

### 杂项

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

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

luopl's avatar
luopl committed
227
228
229
230
231
232
#### 使用 APOLLO 进行全参数训练

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

chenych's avatar
chenych committed
233
234
235
236
237
238
239
240
241
242
243
244
#### 使用 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
```

chenych's avatar
chenych committed
245
246
247
248
249
250
#### 使用 Muon 进行全参数训练

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

chenych's avatar
chenych committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#### 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
280
```
shihm's avatar
uodata  
shihm committed
281
282
283
284
285
286
287
288
289
290
291
292

#### OFT 微调

```bash
llamafactory-cli train examples/extras/oft/llama3_oft_sft.yaml
```

#### QOFT 微调

```bash
llamafactory-cli train examples/extras/qoft/llama3_oft_sft_bnb_npu.yaml
```