README.md 5.76 KB
Newer Older
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1
2
3
4
# <div align="center"><strong>LLaMA Factory</strong></div>
## 简介
LLaMA Factory是一个大语言模型训练和推理的框架,支持了魔搭社区(ModelScope)的模型和数据集资源。它允许用户通过内置的Web UI灵活定制100多个LLMs的微调,而无需编写代码。

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

chenych's avatar
chenych committed
7
8
9
10
11
12
13
- **多种模型**: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
14
15

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

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

持续更新中...

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

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

chenych's avatar
chenych committed
33
34
> **[!NOTE]** `Baichuan 2` 需要卸载掉环境中的xformers库,当前仅支持Lora方式训练。

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

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

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

Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
42
43
44
基于光源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
45
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
46
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
47
48
49

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
50
51
52
53
```

#### Dockerfile(方法二)
```bash
chenych's avatar
chenych committed
54
cd docker
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
55
56
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
57
58
59

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
60
61
62
63
64
```

#### Anaconda(方法三)
关于本项目DCU显卡所需的特殊深度学习库可从[光合](https://developer.hpccube.com/tool/)开发者社区下载安装。
```bash
chenych's avatar
chenych committed
65
DTK驱动: dtk24.04.1
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
66
67
python: python3.10
torch: 2.1.0
Rayyyyy's avatar
Rayyyyy committed
68
bitsandbytes: 0.42.0
chenych's avatar
chenych committed
69
70
vllm: vllm-0.5.0+das.opt1.3e2c63a.dtk2404.torch2.1.0-cp310-cp310-linux_x86_64
pip: lmslim-0.0.1+das.dtk24041.torch2.1.0-cp310-cp310-linux_x86_64
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
71
72
73
74
75
76
77
78
```
`Tips:以上dtk驱动、python、torch等DCU相关工具版本需要严格一一对应`

### 源码编译安装
> [!TIP]
> 遇到包冲突时,可使用 `pip install --no-deps -e .` 解决。

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

chenych's avatar
chenych committed
83
84
# pdsh安装,deepspeed多机需要,若已安装,可忽略。
# 安装需要root权限
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
85
86
87
88
89
90
91
92
93
94
95
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
96

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

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

chenych's avatar
chenych committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
### 快速开始

下面三行命令分别对 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
116
117

## 参考资料
chenych's avatar
chenych committed
118
- [README_zh](README_zh.md)
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
119
- [LLaMA-Factory](https://github.com/hiyouga/LLaMA-Factory)