README.md 6.92 KB
Newer Older
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1
2
We provide diverse examples about fine-tuning LLMs.

chenych's avatar
chenych committed
3
4
5
6
7
8
9
10
11
12
13
14
15
Make sure to execute these commands in the `LLaMA-Factory` directory.

## Table of Contents

- [LoRA Fine-Tuning](#lora-fine-tuning)
- [QLoRA Fine-Tuning](#qlora-fine-tuning)
- [Full-Parameter Fine-Tuning](#full-parameter-fine-tuning)
- [Merging LoRA Adapters and Quantization](#merging-lora-adapters-and-quantization)
- [Inferring LoRA Fine-Tuned Models](#inferring-lora-fine-tuned-models)
- [Extras](#extras)

Use `CUDA_VISIBLE_DEVICES` (GPU) or `ASCEND_RT_VISIBLE_DEVICES` (NPU) to choose computing devices.

luopl's avatar
luopl committed
16
17
By default, LLaMA-Factory uses all visible computing devices.

chenych's avatar
chenych committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Basic usage:

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

Advanced usage:

```bash
CUDA_VISIBLE_DEVICES=0,1 llamafactory-cli train examples/train_lora/llama3_lora_sft.yaml \
    learning_rate=1e-5 \
    logging_steps=1
```

```bash
bash examples/train_lora/llama3_lora_sft.sh
```

chenych's avatar
chenych committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
## Examples

### LoRA Fine-Tuning

#### (Continuous) Pre-Training

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

#### Supervised Fine-Tuning

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

#### Multimodal Supervised Fine-Tuning

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

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

```bash
luopl's avatar
luopl committed
61
llamafactory-cli train examples/train_lora/llama3_lora_dpo.yaml
chenych's avatar
chenych committed
62
63
```

luopl's avatar
luopl committed
64
#### Multimodal DPO/ORPO/SimPO Training
chenych's avatar
chenych committed
65
66

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

luopl's avatar
luopl committed
70
#### Reward Modeling
chenych's avatar
chenych committed
71
72

```bash
luopl's avatar
luopl committed
73
74
75
76
77
78
79
llamafactory-cli train examples/train_lora/llama3_lora_reward.yaml
```

#### PPO Training

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

#### KTO Training

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

#### Preprocess Dataset

It is useful for large dataset, use `tokenized_path` in config to load the preprocessed dataset.

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

#### Evaluating on MMLU/CMMLU/C-Eval Benchmarks

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

#### Supervised Fine-Tuning on Multiple Nodes

```bash
luopl's avatar
luopl committed
105
106
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
107
108
109
110
111
112
113
114
```

#### Supervised Fine-Tuning with DeepSpeed ZeRO-3 (Weight Sharding)

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

luopl's avatar
luopl committed
115
116
117
#### Supervised Fine-Tuning with Ray on 4 GPUs

```bash
chenych's avatar
chenych committed
118
USE_RAY=1 llamafactory-cli train examples/train_lora/llama3_lora_sft_ray.yaml
luopl's avatar
luopl committed
119
120
```

chenych's avatar
chenych committed
121
122
123
124
125
126
127
128
### QLoRA Fine-Tuning

#### Supervised Fine-Tuning with 4/8-bit Bitsandbytes/HQQ/EETQ Quantization (Recommended)

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

luopl's avatar
luopl committed
129
130
131
132
133
134
#### Supervised Fine-Tuning with 4-bit Bitsandbytes Quantization on Ascend NPU

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

chenych's avatar
chenych committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#### Supervised Fine-Tuning with 4/8-bit GPTQ Quantization

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

#### Supervised Fine-Tuning with 4-bit AWQ Quantization

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

#### Supervised Fine-Tuning with 2-bit AQLM Quantization

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

### Full-Parameter Fine-Tuning

#### Supervised Fine-Tuning on Single Node

```bash
luopl's avatar
luopl committed
158
FORCE_TORCHRUN=1 llamafactory-cli train examples/train_full/llama3_full_sft.yaml
chenych's avatar
chenych committed
159
160
161
162
163
```

#### Supervised Fine-Tuning on Multiple Nodes

```bash
luopl's avatar
luopl committed
164
165
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
166
167
```

chenych's avatar
chenych committed
168
169
170
171
172
173
174
175
### Elastic and Fault-Tolerant Supervised Fine-Tuning on Multiple Nodes

To launch an elastic job with `MAX_RESTARTS` failures retries, run the following on at least `MIN_NNODES` nodes and at most `MAX_NNODES` nodes. `RDZV_ID` should be set as a unique job id (shared by all nodes participating in the job). See also [torchrun](https://docs.pytorch.org/docs/stable/elastic/run.html).

```bash
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/llama3_full_sft.yaml
```

luopl's avatar
luopl committed
176
177
178
#### Multimodal Supervised Fine-Tuning

```bash
chenych's avatar
chenych committed
179
FORCE_TORCHRUN=1 llamafactory-cli train examples/train_full/qwen2_5vl_full_sft.yaml
luopl's avatar
luopl committed
180
181
```

chenych's avatar
chenych committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
### Merging LoRA Adapters and Quantization

#### Merge LoRA Adapters

Note: DO NOT use quantized model or `quantization_bit` when merging LoRA adapters.

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

#### Quantizing Model using AutoGPTQ

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

chenych's avatar
chenych committed
198
199
200
201
202
203
### Save Ollama modelfile

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

chenych's avatar
chenych committed
204
205
### Inferring LoRA Fine-Tuned Models

chenych's avatar
chenych committed
206
#### Evaluation using vLLM's Multi-GPU Inference
luopl's avatar
luopl committed
207
208

```
chenych's avatar
chenych committed
209
210
python scripts/vllm_infer.py --model_name_or_path meta-llama/Meta-Llama-3-8B-Instruct --template llama3 --dataset alpaca_en_demo
python scripts/eval_bleu_rouge.py generated_predictions.jsonl
luopl's avatar
luopl committed
211
212
213
```

#### Use CLI ChatBox
chenych's avatar
chenych committed
214
215
216
217
218

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

luopl's avatar
luopl committed
219
#### Use Web UI ChatBox
chenych's avatar
chenych committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238

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

#### Launch OpenAI-style API

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

### Extras

#### Full-Parameter Fine-Tuning using GaLore

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

luopl's avatar
luopl committed
239
240
241
242
243
244
#### Full-Parameter Fine-Tuning using APOLLO

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

chenych's avatar
chenych committed
245
246
247
248
249
250
251
252
253
254
255
256
#### Full-Parameter Fine-Tuning using BAdam

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

#### Full-Parameter Fine-Tuning using Adam-mini

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

chenych's avatar
chenych committed
257
258
259
260
261
262
#### Full-Parameter Fine-Tuning using Muon

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

chenych's avatar
chenych committed
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#### LoRA+ Fine-Tuning

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

#### PiSSA Fine-Tuning

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

#### Mixture-of-Depths Fine-Tuning

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

#### LLaMA-Pro Fine-Tuning

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

#### FSDP+QLoRA Fine-Tuning

```bash
bash examples/extras/fsdp_qlora/train.sh
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
292
```