README_zh-CN.md 6.26 KB
Newer Older
lvhan028's avatar
lvhan028 committed
1
<div align="center">
lvhan028's avatar
lvhan028 committed
2
  <img src="resources/lmdeploy-logo.png" width="450"/>
lvhan028's avatar
lvhan028 committed
3
4
5
6
7
8
9

[English](README.md) | 简体中文

</div>

<div align="center">
  <a href="https://openmmlab.medium.com/" style="text-decoration:none;">
lvhan028's avatar
lvhan028 committed
10
    <img src="https://user-images.githubusercontent.com/25839884/219255827-67c1a27f-f8c5-46a9-811d-5e57448c61d1.png" width="3%" alt="" /></a>
lvhan028's avatar
lvhan028 committed
11
  <img src="https://user-images.githubusercontent.com/25839884/218346358-56cc8e2f-a2b8-487f-9088-32480cceabcf.png" width="3%" alt="" />
lvhan028's avatar
lvhan028 committed
12
  <a href="https://discord.com/channels/1037617289144569886/1046608014234370059" style="text-decoration:none;">
lvhan028's avatar
lvhan028 committed
13
14
15
16
17
18
19
    <img src="https://user-images.githubusercontent.com/25839884/218347213-c080267f-cbb6-443e-8532-8e1ed9a58ea9.png" width="3%" alt="" /></a>
  <img src="https://user-images.githubusercontent.com/25839884/218346358-56cc8e2f-a2b8-487f-9088-32480cceabcf.png" width="3%" alt="" />
  <a href="https://twitter.com/OpenMMLab" style="text-decoration:none;">
    <img src="https://user-images.githubusercontent.com/25839884/218346637-d30c8a0f-3eba-4699-8131-512fb06d46db.png" width="3%" alt="" /></a>
  <img src="https://user-images.githubusercontent.com/25839884/218346358-56cc8e2f-a2b8-487f-9088-32480cceabcf.png" width="3%" alt="" />
  <a href="https://www.youtube.com/openmmlab" style="text-decoration:none;">
    <img src="https://user-images.githubusercontent.com/25839884/218346691-ceb2116a-465a-40af-8424-9f30d2348ca9.png" width="3%" alt="" /></a>
lvhan028's avatar
lvhan028 committed
20
21
22
23
24
25
  <img src="https://user-images.githubusercontent.com/25839884/218346358-56cc8e2f-a2b8-487f-9088-32480cceabcf.png" width="3%" alt="" />
  <a href="https://space.bilibili.com/1293512903" style="text-decoration:none;">
    <img src="https://user-images.githubusercontent.com/25839884/219026751-d7d14cce-a7c9-4e82-9942-8375fca65b99.png" width="3%" alt="" /></a>
  <img src="https://user-images.githubusercontent.com/25839884/218346358-56cc8e2f-a2b8-487f-9088-32480cceabcf.png" width="3%" alt="" />
  <a href="https://www.zhihu.com/people/openmmlab" style="text-decoration:none;">
    <img src="https://user-images.githubusercontent.com/25839884/219026120-ba71e48b-6e94-4bd4-b4e9-b7d175b5e362.png" width="3%" alt="" /></a>
lvhan028's avatar
lvhan028 committed
26
27
28
29
</div>

## 简介

30
31
LMDeploy 由 [MMDeploy](https://github.com/open-mmlab/mmdeploy)[MMRazor](https://github.com/open-mmlab/mmrazor) 团队联合开发,是涵盖了 LLM 任务的全套轻量化、部署和服务解决方案。
这个强大的工具箱提供以下核心功能:
lvhan028's avatar
lvhan028 committed
32

lvhan028's avatar
lvhan028 committed
33
- **高效推理引擎 TurboMind**:基于 [FasterTransformer](https://github.com/NVIDIA/FasterTransformer),我们实现了高效推理引擎 TurboMind,支持 InternLM、LLaMA、vicuna等模型在 NVIDIA GPU 上的推理。
lvhan028's avatar
lvhan028 committed
34

35
- **交互推理方式**:通过缓存多轮对话过程中 attention 的 k/v,记住对话历史,从而避免重复处理历史会话。
lvhan028's avatar
lvhan028 committed
36

tpoisonooo's avatar
tpoisonooo committed
37
- **多 GPU 部署和量化**:我们提供了全面的模型部署和量化支持,已在不同规模上完成验证。
lvhan028's avatar
lvhan028 committed
38

39
40
- **persistent batch 推理**:进一步优化模型执行效率。

pppppM's avatar
pppppM committed
41
  ![PersistentBatchInference](https://github.com/InternLM/lmdeploy/assets/67539920/e3876167-0671-44fc-ac52-5a0f9382493e)
lvhan028's avatar
lvhan028 committed
42

lvhan028's avatar
lvhan028 committed
43
## 性能
lvhan028's avatar
lvhan028 committed
44

lvhan028's avatar
lvhan028 committed
45
如下图所示,我们对比了 facebookresearch/llama、HuggingFace Transformers、DeepSpeed 在 7B 模型上的token生成的速度。
lvhan028's avatar
lvhan028 committed
46

lvhan028's avatar
lvhan028 committed
47
测试设备:NVIDIA A100(80G)
lvhan028's avatar
lvhan028 committed
48

lvhan028's avatar
lvhan028 committed
49
测试指标:吞吐量(token/s)
lvhan028's avatar
lvhan028 committed
50

lvhan028's avatar
lvhan028 committed
51
测试数据:输入token数为1,生成token数为2048
lvhan028's avatar
lvhan028 committed
52

lvhan028's avatar
lvhan028 committed
53
TurboMind 的吞吐量超过 2000 token/s, 整体比 DeepSpeed 提升约 5% - 15%,比 huggingface transformers 提升 2.3 倍
lvhan028's avatar
lvhan028 committed
54

pppppM's avatar
pppppM committed
55
![benchmark](https://github.com/InternLM/lmdeploy/assets/67539920/bb9fdf35-8dc5-41f5-ad5e-33df786665e3)
lvhan028's avatar
lvhan028 committed
56

lvhan028's avatar
lvhan028 committed
57
## 快速上手
lvhan028's avatar
lvhan028 committed
58

lvhan028's avatar
lvhan028 committed
59
### 安装
lvhan028's avatar
lvhan028 committed
60
61

```shell
lvhan028's avatar
lvhan028 committed
62
63
64
65
66
conda create -n lmdeploy python=3.10
conda activate lmdeploy
git clone https://github.com/InternLM/lmdeploy.git
cd lmdeploy
pip install -e .
lvhan028's avatar
lvhan028 committed
67
68
```

lvhan028's avatar
lvhan028 committed
69
### 部署 InternLM
lvhan028's avatar
lvhan028 committed
70

lvhan028's avatar
lvhan028 committed
71
#### 获取 InternLM 模型
lvhan028's avatar
lvhan028 committed
72
73

```shell
lvhan028's avatar
lvhan028 committed
74
# 1. 下载 InternLM 模型
lvhan028's avatar
lvhan028 committed
75

pppppM's avatar
pppppM committed
76
77
78
79
80
81
82
83
# Make sure you have git-lfs installed (https://git-lfs.com)
git lfs install
git clone https://huggingface.co/internlm/internlm-7b /path/to/internlm-7b

# if you want to clone without large files – just their pointers
# prepend your git clone with the following env var:
GIT_LFS_SKIP_SMUDGE=1

lvhan028's avatar
lvhan028 committed
84
85
# 2. 转换为 trubomind 要求的格式。默认存放路径为 ./workspace
python3 -m lmdeploy.serve.turbomind.deploy internlm-7b /path/to/internlm-7b hf
lvhan028's avatar
lvhan028 committed
86

lvhan028's avatar
lvhan028 committed
87
```
lvhan028's avatar
lvhan028 committed
88

lvhan028's avatar
lvhan028 committed
89
#### 使用 turbomind 推理
lvhan028's avatar
lvhan028 committed
90
91

```shell
tpoisonooo's avatar
tpoisonooo committed
92
docker run --gpus all --rm -v $(pwd)/workspace:/workspace -it openmmlab/lmdeploy:latest \
lvhan028's avatar
lvhan028 committed
93
    python3 -m lmdeploy.turbomind.chat internlm /workspace
lvhan028's avatar
lvhan028 committed
94
95
```

lvhan028's avatar
lvhan028 committed
96
97
98
```{note}
turbomind 在使用 FP16 精度推理 InternLM-7B 模型时,显存开销至少需要 22.7G。建议使用 3090, V100,A100等型号的显卡
```
lvhan028's avatar
lvhan028 committed
99

lvhan028's avatar
lvhan028 committed
100
101
102
#### 部署推理服务

使用下面的命令启动推理服务:
lvhan028's avatar
lvhan028 committed
103
104

```shell
lvhan028's avatar
lvhan028 committed
105
bash workspace/service_docker_up.sh
lvhan028's avatar
lvhan028 committed
106
107
```

lvhan028's avatar
lvhan028 committed
108
你可以通过命令行方式与推理服务进行对话:
lvhan028's avatar
lvhan028 committed
109
110

```shell
lvhan028's avatar
lvhan028 committed
111
python3 lmdeploy.serve.client {server_ip_addresss}:33337 internlm
lvhan028's avatar
lvhan028 committed
112
113
```

lvhan028's avatar
lvhan028 committed
114
也可以通过 WebUI 方式来对话:
AllentDan's avatar
AllentDan committed
115

lvhan028's avatar
lvhan028 committed
116
117
```
python3 lmdeploy.app {server_ip_addresss}:33337 internlm
AllentDan's avatar
AllentDan committed
118
```
lvhan028's avatar
lvhan028 committed
119

pppppM's avatar
pppppM committed
120
![](https://github.com/InternLM/lmdeploy/assets/67539920/08d1e6f2-3767-44d5-8654-c85767cec2ab)
AllentDan's avatar
AllentDan committed
121

lvhan028's avatar
lvhan028 committed
122
123
其他模型的部署方式,比如 LLaMA,vicuna,请参考[这里](docs/zh_cn/serving.md)

124
## 量化部署
lvhan028's avatar
lvhan028 committed
125

126
127
在 fp16 模式下,可以开启 kv_cache int8 量化,单卡可服务更多用户。
首先执行量化脚本,量化参数存放到 `deploy.py` 转换的 weight 目录下。
128
129
130
131
132
133
134
135
136
137

```
python3 -m lmdeploy.lite.apis.kv_qparams \
  --model $HF_MODEL \
  --output_dir $DEPLOY_WEIGHT_DIR \
  --symmetry True \ # 对称量化或非对称量化,默认为 True
  --offload  False \ # 将模型放在 CPU,只在推理时加载部分模块到 GPU,默认为 False
  --num_tp 1  \  # Tensor 并行使用的 GPU 数,和 deploy.py 保持一致
```

138
然后调整 `config.ini`
lvhan028's avatar
lvhan028 committed
139
140
141

- `use_context_fmha` 改为 0,表示关闭
- `quant_policy` 设置为 4。此参数默认为 0,表示不开启
142

143
144
这里是[量化测试结果](./docs/zh_cn/quantization.md)

lvhan028's avatar
lvhan028 committed
145
146
## 贡献指南

lvhan028's avatar
lvhan028 committed
147
我们感谢所有的贡献者为改进和提升 LMDeploy 所作出的努力。请参考[贡献指南](.github/CONTRIBUTING.md)来了解参与项目贡献的相关指引。
lvhan028's avatar
lvhan028 committed
148
149
150
151
152
153
154
155

## 致谢

- [FasterTransformer](https://github.com/NVIDIA/FasterTransformer)

## License

该项目采用 [Apache 2.0 开源许可证](LICENSE)