mirror of
https://github.com/QwenLM/Qwen.git
synced 2026-05-20 16:35:47 +08:00
update new version of quantization and inference efficiency profiling result
This commit is contained in:
106
README_CN.md
106
README_CN.md
@@ -6,7 +6,7 @@
|
||||
<br>
|
||||
|
||||
<p align="center">
|
||||
Qwen-7B <a href="https://modelscope.cn/models/qwen/Qwen-7B/summary">🤖 <a> | <a href="https://huggingface.co/Qwen/Qwen-7B">🤗</a>  | Qwen-7B-Chat <a href="https://modelscope.cn/models/qwen/Qwen-7B-Chat/summary">🤖 <a>| <a href="https://huggingface.co/Qwen/Qwen-7B-Chat">🤗</a>  |  <a href="https://modelscope.cn/studios/qwen/Qwen-7B-Chat-Demo/summary">Demo</a>  |  <a href="https://github.com/QwenLM/Qwen-7B/blob/main/tech_memo.md">Report</a>   |   <a href="https://discord.gg/9bjvspyu">Discord</a>
|
||||
Qwen-7B <a href="https://modelscope.cn/models/qwen/Qwen-7B/summary">🤖 <a> | <a href="https://huggingface.co/Qwen/Qwen-7B">🤗</a>  | Qwen-7B-Chat <a href="https://modelscope.cn/models/qwen/Qwen-7B-Chat/summary">🤖 <a>| <a href="https://huggingface.co/Qwen/Qwen-7B-Chat">🤗</a>  | Qwen-7B-Chat-Int4 <a href="https://huggingface.co/Qwen/Qwen-7B-Chat-Int4">🤗</a>  |  <a href="https://modelscope.cn/studios/qwen/Qwen-7B-Chat-Demo/summary">Demo</a>  |  <a href="https://github.com/QwenLM/Qwen-7B/blob/main/tech_memo.md">Report</a>   |   <a href="https://discord.gg/9bjvspyu">Discord</a>
|
||||
</p>
|
||||
<br>
|
||||
|
||||
@@ -29,6 +29,8 @@
|
||||
|
||||
## 新闻
|
||||
|
||||
* 2023年8月21日 发布Qwen-7B-Chat的Int4量化模型,Qwen-7B-Chat-Int4。该模型显存占用低,推理速度相比半精度模型显著提升,在基准评测上效果损失较小。
|
||||
|
||||
* 2023年8月3日 在魔搭社区(ModelScope)和Hugging Face同步推出Qwen-7B和Qwen-7B-Chat模型。同时,我们发布了技术备忘录,介绍了相关的训练细节和模型表现。
|
||||
|
||||
## 评测表现
|
||||
@@ -198,89 +200,62 @@ print(f'Response: {response}')
|
||||
|
||||
## 量化
|
||||
|
||||
如希望使用更低精度的量化模型,如4比特和8比特的模型,我们提供了简单的示例来说明如何快速使用量化模型。在开始前,确保你已经安装了`bitsandbytes`。请注意,`bitsandbytes`的安装要求是:
|
||||
### 用法
|
||||
|
||||
```
|
||||
**Requirements** Python >=3.8. Linux distribution (Ubuntu, MacOS, etc.) + CUDA > 10.0.
|
||||
**请注意:我们更新量化方案为基于[AutoGPTQ](https://github.com/PanQiWei/AutoGPTQ)的量化,提供Qwen-7B-Chat的Int4量化模型[点击这里](https://huggingface.co/Qwen/Qwen-7B-Chat-Int4)。相比此前方案,该方案在模型评测效果几乎无损,且存储需求更低,推理速度更优。**
|
||||
|
||||
以下我们提供示例说明如何使用Int4量化模型。在开始使用前,请先保证满足AutoGPTQ的要求,并使用源代码安装(由于最新支持Qwen的代码未发布到PyPI):
|
||||
|
||||
```bash
|
||||
git clone https://github.com/PanQiWei/AutoGPTQ.git && cd AutoGPTQ
|
||||
pip install .
|
||||
```
|
||||
|
||||
随后运行如下命令安装`bitsandbytes`:
|
||||
|
||||
```
|
||||
pip install bitsandbytes
|
||||
```
|
||||
|
||||
Windows用户需安装特定版本的`bitsandbytes`,可选项包括[bitsandbytes-windows-webui](https://github.com/jllllll/bitsandbytes-windows-webui/releases/tag/wheels)。
|
||||
|
||||
你只需要在`AutoModelForCausalLM.from_pretrained`中添加你的量化配置,即可使用量化模型。如下所示:
|
||||
随后便能轻松读取量化模型:
|
||||
|
||||
```python
|
||||
from transformers import AutoModelForCausalLM, BitsAndBytesConfig
|
||||
|
||||
# quantization configuration for NF4 (4 bits)
|
||||
quantization_config = BitsAndBytesConfig(
|
||||
load_in_4bit=True,
|
||||
bnb_4bit_quant_type='nf4',
|
||||
bnb_4bit_compute_dtype=torch.bfloat16
|
||||
)
|
||||
|
||||
# quantization configuration for Int8 (8 bits)
|
||||
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
|
||||
|
||||
model = AutoModelForCausalLM.from_pretrained(
|
||||
args.checkpoint_path,
|
||||
device_map="cuda:0",
|
||||
quantization_config=quantization_config,
|
||||
max_memory=max_memory,
|
||||
trust_remote_code=True,
|
||||
).eval()
|
||||
from auto_gptq import AutoGPTQForCausalLM
|
||||
model = AutoGPTQForCausalLM.from_quantized("Qwen/Qwen-7B-Chat-Int4", device_map="auto", trust_remote_code=True, use_safetensors=True).eval()
|
||||
```
|
||||
|
||||
上述方法可以让我们将模型量化成`NF4`和`Int8`精度的模型进行读取,帮助我们节省显存开销。我们也提供了相关性能数据。我们发现尽管模型在效果上存在损失,但模型的显存开销大幅降低。
|
||||
推理方法和基础用法类似,但注意需要从外部传入generation config:
|
||||
|
||||
| Precision | MMLU | GPU Memory for Loading Model |
|
||||
| ----------- | :------: | :---------------------------: |
|
||||
| BF16 | 56.7 | 16.38G |
|
||||
| Int8 | 52.8 | 10.44G |
|
||||
| NF4 | 48.9 | 7.79G |
|
||||
```python
|
||||
from transformers import GenerationConfig
|
||||
config = GenerationConfig.from_pretrained("Qwen/Qwen-7B-Chat-Int4", trust_remote_code=True)
|
||||
response, history = model.chat(tokenizer, "Hi", history=None, generation_config=config)
|
||||
```
|
||||
|
||||
注:表中显存占用的测试环境为A100-SXM4-80G单卡,PyTorch 2.0.1,CUDA 11.8,开启flash attention
|
||||
### 效果评测
|
||||
|
||||
## 推理性能
|
||||
我们对BF16和Int4模型在基准评测上做了测试,发现量化模型效果损失较小,结果如下所示:
|
||||
|
||||
| Quantization | MMLU | CEval (val) | GSM8K | Humaneval |
|
||||
| ------------- | :--------: | :----------: | :----: | :--------: |
|
||||
| BF16 | 53.9 | 54.2 | 41.1 | 24.4 |
|
||||
| Int4 | 52.6 | 52.9 | 38.1 | 23.8 |
|
||||
|
||||
### 推理速度
|
||||
|
||||
我们分别测试了BF16和量化条件下,模型生成2K tokens的平均推理速度,结果如下
|
||||
我们测算了BF16和Int4模型生成2048和8192个token的平均推理速度(tokens/s)。如图所示:
|
||||
|
||||
| 量化等级 | 开flash_attn的推理速度 (字符/秒) | 关flash_attn的推理速度 (字符/秒) |
|
||||
| ------ | :---------------------------: | :---------------------------: |
|
||||
| BF16 (无量化) | 30.06 | 27.55 |
|
||||
| Int8 (bnb) | 7.94 | 7.86 |
|
||||
| NF4 (bnb) | 21.43 | 20.37 |
|
||||
| Quantization | Speed (2048 tokens) | Speed (8192 tokens) |
|
||||
| ------------- | :------------------:| :------------------:|
|
||||
| BF16 | 30.53 | 28.51 |
|
||||
| Int4 | 45.60 | 33.83 |
|
||||
|
||||
具体的评测方式为:指定输入context长度为1,生成长度为2048;测试硬件为A100-SXM4-80G单卡,软件环境为PyTorch 2.0.1,CUDA版本11.8,计算生成该2048序列的平均速度
|
||||
具体而言,我们记录在长度为1的上下文的条件下生成8192个token的性能。评测运行于单张A100-SXM4-80G GPU,使用PyTorch 2.0.1和CUDA 11.4。推理速度是生成8192个token的速度均值。
|
||||
|
||||
### 显存占用
|
||||
### 显存使用
|
||||
|
||||
在BF16和不同量化条件下,我们分别测算了模型编码2048长度序列(并生成1个token),和生成8192长度序列(编码1个token作为context)的峰值显存占用。结果如下
|
||||
我们还测算了BF16和Int4模型编码2048个token及生成8192个token的峰值显存占用情况。结果如下所示:
|
||||
|
||||
打开flash attention时
|
||||
| Quantization Level | Peak Usage for Encoding 2048 Tokens | Peak Usage for Generating 8192 Tokens |
|
||||
| ------------------ | :---------------------------------: | :-----------------------------------: |
|
||||
| BF16 | 18.99GB | 24.40GB |
|
||||
| In4 | 10.20GB | 15.61GB |
|
||||
|
||||
| 量化等级 | 编码 2048 长度的峰值显存 | 生成 8192 长度的峰值显存 |
|
||||
| --- | :---: | :---: |
|
||||
| BF16 | 18.11GB | 23.52GB |
|
||||
| Int8 | 12.17GB | 17.60GB |
|
||||
| NF4 | 9.52GB | 14.93GB |
|
||||
|
||||
关闭flash attention时
|
||||
|
||||
| 量化等级 | 编码 2048 长度的峰值显存 | 生成 8192 长度的峰值显存 |
|
||||
| --- | :---: | :---: |
|
||||
| BF16 | 18.11GB | 24.40GB |
|
||||
| Int8 | 12.18GB | 18.47GB |
|
||||
| NF4 | 9.52GB | 15.81GB |
|
||||
|
||||
以上测速和显存占用情况,均可通过该[评测脚本](https://qianwen-res.oss-cn-beijing.aliyuncs.com/profile.py)测算得到。
|
||||
上述性能测算使用[此脚本](https://qianwen-res.oss-cn-beijing.aliyuncs.com/profile.py)完成。
|
||||
|
||||
## Demo
|
||||
|
||||
@@ -304,7 +279,6 @@ python web_demo.py
|
||||
<br>
|
||||
<p>
|
||||
|
||||
|
||||
### 交互式Demo
|
||||
|
||||
我们提供了一个简单的交互式Demo示例,请查看`cli_demo.py`。当前模型已经支持流式输出,用户可通过输入文字的方式和Qwen-7B-Chat交互,模型将流式输出返回结果。运行如下命令:
|
||||
|
||||
Reference in New Issue
Block a user