README.md 6.49 KB
Newer Older
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1
2
# <div align="center"><strong>LLaMA Factory</strong></div>
## 简介
chenych's avatar
chenych committed
3

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

chenych's avatar
chenych committed
6
## 项目特色
Rayyyyy's avatar
Rayyyyy committed
7

chenych's avatar
chenych committed
8
9
10
11
12
13
14
- **多种模型**: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、浏览器界面和命令行接口。
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
15
16

## 支持模型结构列表
chenych's avatar
chenych committed
17

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

持续更新中...

> 对于所有“基座”(Base)模型,`template` 参数可以是 `default`, `alpaca`, `vicuna` 等任意值。但“对话”(Instruct/Chat)模型请务必使用**对应的模板**。
>
> 请务必在训练和推理时采用**完全一致**的模板。

您也可以在 [template.py](src/llamafactory/data/template.py) 中添加自己的对话模板。

chenych's avatar
chenych committed
36
37
38
39
> **[!NOTE]**
>
> 1. `Baichuan 2` 需要卸载掉环境中的xformers库,当前仅支持Lora方式训练。
>
chenych's avatar
chenych committed
40
> 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)
41
42
>
> 3. `Qwen2`训练仅支持bf16格式,**fp16会出现loss为0,lr为0的问题**,参考[issuse](https://github.com/hiyouga/LLaMA-Factory/issues/4848)
chenych's avatar
chenych committed
43

Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
44
45
## 使用源码编译方式安装
### 环境准备
chenych's avatar
chenych committed
46

Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
47
-v 路径、docker_name和imageID根据实际情况修改
chenych's avatar
chenych committed
48

Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
49
####  Docker(方法一)
chenych's avatar
chenych committed
50

Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
51
52
53
基于光源pytorch2.1.0基础镜像环境:镜像下载地址:[https://sourcefind.cn/#/image/dcu/pytorch](https://sourcefind.cn/#/image/dcu/pytorch),根据pytorch2.1.0、python、dtk及系统下载对应的镜像版本。

```bash
Rayyyyy's avatar
Rayyyyy committed
54
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-ubuntu20.04-dtk24.04.1-py3.10
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
55
docker run -it -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro --shm-size=80G --privileged=true --device=/dev/kfd --device=/dev/dri/ --group-add video --name docker_name imageID bash
chenych's avatar
chenych committed
56
57
58

pip install vllm-0.5.0+das.opt1.3e2c63a.dtk2404.torch2.1.0-cp310-cp310-linux_x86_64.whl
pip install lmslim-0.0.1+das.dtk24041.torch2.1.0-cp310-cp310-linux_x86_64.whl
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
59
60
61
```

#### Dockerfile(方法二)
chenych's avatar
chenych committed
62

Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
63
```bash
chenych's avatar
chenych committed
64
cd docker
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
65
66
docker build --no-cache -t llama-factory:latest .
docker run -it -v /path/your_code_data/:/path/your_code_data/ -v /opt/hyhal/:/opt/hyhal/:ro --shm-size=80G --privileged=true --device=/dev/kfd --device=/dev/dri/ --group-add video --name docker_name imageID bash
chenych's avatar
chenych committed
67
68
69

pip install vllm-0.5.0+das.opt1.3e2c63a.dtk2404.torch2.1.0-cp310-cp310-linux_x86_64.whl
pip install lmslim-0.0.1+das.dtk24041.torch2.1.0-cp310-cp310-linux_x86_64.whl
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
70
71
72
```

#### Anaconda(方法三)
chenych's avatar
chenych committed
73

Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
74
75
关于本项目DCU显卡所需的特殊深度学习库可从[光合](https://developer.hpccube.com/tool/)开发者社区下载安装。
```bash
chenych's avatar
chenych committed
76
DTK驱动: dtk24.04.1
chenych's avatar
chenych committed
77
python: 3.10
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
78
torch: 2.1.0
Rayyyyy's avatar
Rayyyyy committed
79
bitsandbytes: 0.42.0
chenych's avatar
chenych committed
80
81
vllm: 0.5.0
lmslim: 0.0.1
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
82
83
84
85
```
`Tips:以上dtk驱动、python、torch等DCU相关工具版本需要严格一一对应`

### 源码编译安装
chenych's avatar
chenych committed
86

Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
87
88
89
90
> [!TIP]
> 遇到包冲突时,可使用 `pip install --no-deps -e .` 解决。

```bash
chenych's avatar
chenych committed
91
git clone http://developer.hpccube.com/codes/OpenDAS/llama-factory.git
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
92
cd /your_code_path/llama_factory
chenych's avatar
chenych committed
93
pip install -e ".[torch,metrics]"
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
94

chenych's avatar
chenych committed
95
96
# pdsh安装,deepspeed多机需要,若已安装,可忽略。
# 安装需要root权限
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
97
98
99
100
101
102
103
104
105
106
107
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
```

## 如何使用
### 数据准备
chenych's avatar
chenych committed
108

Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
109
110
111
112
113
关于数据集文件的格式,请参考 [data/README_zh.md](data/README_zh.md) 的内容。你可以使用 HuggingFace / ModelScope 上的数据集或加载本地数据集。

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

chenych's avatar
chenych committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
### 快速开始

下面三行命令分别对 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` 显示帮助信息。
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
128
129

## 参考资料
chenych's avatar
chenych committed
130

chenych's avatar
chenych committed
131
- [README_zh](README_zh.md)
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
132
- [LLaMA-Factory](https://github.com/hiyouga/LLaMA-Factory)