"vscode:/vscode.git/clone" did not exist on "107f18438605884f580a656fc980233125382309"
README.md 7.63 KB
Newer Older
luopl's avatar
luopl committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# <div align="center"><strong>LLaMA Factory</strong></div>
## 简介

LLaMA Factory是一个大语言模型训练和推理的框架,支持了魔搭社区(ModelScope)的模型和数据集资源。它允许用户通过内置的Web UI灵活定制100多个LLMs的微调,而无需编写代码。

## 项目特色

- **多种模型**:LLaMA、LLaVA、Mistral、Mixtral-MoE、Qwen、Yi、Gemma、Baichuan、ChatGLM、Phi 等等。
- **集成方法**:(增量)预训练、(多模态)指令监督微调、奖励模型训练、PPO 训练、DPO 训练、KTO 训练、ORPO 训练等等。
- **多种精度**:16 比特全参数微调、冻结微调、LoRA 微调和基于 AQLM/AWQ/GPTQ/LLM.int8/HQQ/EETQ 的 2/3/4/5/6/8 比特 QLoRA 微调。
- **先进算法**:GaLore、BAdam、Adam-mini、DoRA、LongLoRA、LLaMA Pro、Mixture-of-Depths、LoRA+、LoftQ、PiSSA 和 Agent 微调。
- **实用技巧**:FlashAttention-2、Unsloth、RoPE scaling、NEFTune 和 rsLoRA。
- **实验监控**:LlamaBoard、TensorBoard、Wandb、MLflow 等等。
- **极速推理**:基于 vLLM 的 OpenAI 风格 API、浏览器界面和命令行接口。

## 支持模型结构列表

chenych's avatar
chenych committed
18

luopl's avatar
luopl committed
19
20
21
| 模型名                                                       | 模型大小                          | Template  |
| ------------------------------------------------------------ | -------------------------------- | --------- |
| [Baichuan 2](https://huggingface.co/baichuan-inc)            | 7B/13B                           | baichuan2 |
jianq's avatar
jianq committed
22
23
| [ChatGLM3](https://huggingface.co/THUDM) | 6B | chatglm3 |
| [GLM-4](https://huggingface.co/THUDM) | 9B | glm4 |
luopl's avatar
luopl committed
24
25
26
| [Gemma 2](https://huggingface.co/google)                     | 2B/9B                            | gemma     |
| [Llama 2](https://huggingface.co/meta-llama)                 | 7B/13B/70B                       | llama2    |
| [Llama 3/Llama 3.1](https://huggingface.co/meta-llama)       | 8B/70B                           | llama3    |
chenych's avatar
chenych committed
27
| [Llama 4](https://huggingface.co/meta-llama)                 | 109B/402B                        | llama4    |
chenych's avatar
chenych committed
28
| [Qwen1.5 (Code/MoE)/Qwen2/Qwen2.5/QwQ](https://huggingface.co/Qwen)            | 0.5B/1.8B/4B/7B/14B/32B/72B      | qwen      |
luopl's avatar
luopl committed
29
30
| [XVERSE](https://hf-mirror.com/xverse)                       | 7B/13B                           | xverse    |
| [OLMo](https://hf-mirror.com/allenai)                        | 1B/7B                            | olmo      |
chenych's avatar
chenych committed
31
| [GLM-4/GLM-4-0414/GLM-Z1](https://huggingface.co/THUDM)      | 9B/32B                           | glm4      |
luopl's avatar
luopl committed
32
33
34

持续更新中...

chenych's avatar
chenych committed
35
36
37
38
> **[!NOTE]**
>
> 注意:本版本仅支持deepseek蒸馏模型的监督微调(SFT),可参考[deepseek-r1-distill_vllm](https://developer.sourcefind.cn/codes/modelzoo/deepseek-r1-distill_vllm)
>
luopl's avatar
luopl committed
39
40
41
> 对于所有“基座”(Base)模型,`template` 参数可以是 `default`, `alpaca`, `vicuna` 等任意值。但“对话”(Instruct/Chat)模型请务必使用**对应的模板**。
>
> 请务必在训练和推理时采用**完全一致**的模板。
chenych's avatar
chenych committed
42
> 您也可以在 [template.py](src/llamafactory/data/template.py) 中添加自己的对话模板。
luopl's avatar
luopl committed
43
>
chenych's avatar
chenych committed
44
> **已知问题及解决方案**
luopl's avatar
luopl committed
45
46
47
48
49
> 1. `Baichuan 2` 需要卸载掉环境中的xformers库,当前仅支持Lora方式训练。
>
> 2. `XVERSE`在`tokenizer > 0.19`的版本下有兼容性问题报错`Exception: data did not match any variant of untagged enum PyPreTokenizerTypeWrappe`,需要使用[XVERSE-13B-256K-hf](https://huggingface.co/xverse/XVERSE-13B-256K/tree/main)中的`tokenizer_config.json.update`/`tokenizer.json.update`替换原有模型文件中的对应tokenizer文件,具体解决方法参考[xverse-ai/XVERSE-7B issues](https://github.com/xverse-ai/XVERSE-7B/issues/1)
>
> 3. `Qwen2`训练仅支持bf16格式,**fp16会出现loss为0,lr为0的问题**,参考[issues](https://github.com/hiyouga/LLaMA-Factory/issues/4848)
50
51
52
>
> 4. `deepspeed-cpu-offload-stage3`出现`RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!`错误,是deepspeed本身bug,解决办法参考官方[issuse](https://github.com/microsoft/DeepSpeed/issues/5634)

luopl's avatar
luopl committed
53
54
55
## 使用源码编译方式安装
### 环境准备

chenych's avatar
chenych committed
56
`-v 路径``docker_name``imageID`根据实际情况修改
luopl's avatar
luopl committed
57
58
59

####  Docker(方法一)

chenych's avatar
chenych committed
60
基于光源pytorch2.4.1基础镜像环境:镜像下载地址:[https://sourcefind.cn/#/image/dcu/pytorch](https://sourcefind.cn/#/image/dcu/pytorch),根据pytorch2.4.1、python、dtk及系统下载对应的镜像版本。
luopl's avatar
luopl committed
61
62

```bash
chenych's avatar
chenych committed
63
64
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.4.1-ubuntu22.04-dtk25.04-py3.10
docker run -it --shm-size 200g --network=host --name {docker_name} --privileged --device=/dev/kfd --device=/dev/dri --device=/dev/mkfd --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -u root -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro {imageID} bash
luopl's avatar
luopl committed
65

chenych's avatar
chenych committed
66
67
cd /your_code_path/llama_factory
pip install -e ".[torch,metrics]"
chenych's avatar
chenych committed
68
69
## llama4 需要单独安装以下包
pip install git+https://github.com/hiyouga/transformers.git@llama4_train
luopl's avatar
luopl committed
70
71
72
73
74
75
76
```

#### Dockerfile(方法二)

```bash
cd docker
docker build --no-cache -t llama-factory:latest .
chenych's avatar
chenych committed
77
docker run -it --shm-size 200g --network=host --name {docker_name} --privileged --device=/dev/kfd --device=/dev/dri --device=/dev/mkfd --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -u root -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro {imageID} bash
luopl's avatar
luopl committed
78

chenych's avatar
chenych committed
79
80
cd /your_code_path/llama_factory
pip install -e ".[torch,metrics]"
chenych's avatar
chenych committed
81
82
## llama4 需要单独安装以下包
pip install git+https://github.com/hiyouga/transformers.git@llama4_train
luopl's avatar
luopl committed
83
84
85
86
87
88
```

#### Anaconda(方法三)

关于本项目DCU显卡所需的特殊深度学习库可从[光合](https://developer.hpccube.com/tool/)开发者社区下载安装。
```bash
chenych's avatar
chenych committed
89
DTK: 25.04
luopl's avatar
luopl committed
90
python: 3.10
chenych's avatar
chenych committed
91
92
torch: 2.4.1
deepspeed: 0.14.2+das.opt2.dtk2504
luopl's avatar
luopl committed
93
94
95
96
97
98
99
100
101
102
103
104
```
`Tips:以上dtk驱动、python、torch等DCU相关工具版本需要严格一一对应`

### 源码编译安装

> [!TIP]
> 遇到包冲突时,可使用 `pip install --no-deps -e .` 解决。

```bash
git clone http://developer.hpccube.com/codes/OpenDAS/llama-factory.git
cd /your_code_path/llama_factory
pip install -e ".[torch,metrics]"
chenych's avatar
chenych committed
105
106
## llama4 需要单独安装以下包
pip install git+https://github.com/hiyouga/transformers.git@llama4_train
luopl's avatar
luopl committed
107

chenych's avatar
chenych committed
108
109
# (可选)deepspeed多机训练
# pdsh安装,若已安装,可忽略。
luopl's avatar
luopl committed
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
# 安装需要root权限
cd ../
#下载解压
wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/pdsh/pdsh-2.29.tar.bz2 && tar -xf pdsh-2.29.tar.bz2
#编译安装
cd pdsh-2.29 && ./configure --with-ssh --enable-static-modules --prefix=/usr/local && make && make install
#测试
pdsh -V
```

## 如何使用
### 数据准备

关于数据集文件的格式,请参考 [data/README_zh.md](data/README_zh.md) 的内容。你可以使用 HuggingFace / ModelScope 上的数据集或加载本地数据集。

> [!NOTE]
> 使用自定义数据集时,请更新 `data/dataset_info.json` 文件。

### 快速开始

下面三行命令分别对 Llama3-8B-Instruct 模型进行 LoRA **微调****推理****合并**。根据实际情况修改参数,如`model_name_or_path`/`dataset`/`template`等。

```bash
llamafactory-cli train examples/train_lora/llama3_lora_sft.yaml
llamafactory-cli chat examples/inference/llama3_lora_sft.yaml
llamafactory-cli export examples/merge_lora/llama3_lora_sft.yaml
```

高级用法请参考 [examples/README_zh.md](examples/README_zh.md)(包括多 GPU 微调)。

> [!TIP]
> 使用 `llamafactory-cli help` 显示帮助信息。

## 参考资料

- [README_zh](README_zh.md)
- [LLaMA-Factory](https://github.com/hiyouga/LLaMA-Factory)