pppppM commited on
Commit
d36cdcd
1 Parent(s): 7b0cea6

update README.md

Browse files
Files changed (2) hide show
  1. README.md +115 -0
  2. README_zh-CN.md +90 -0
README.md CHANGED
@@ -1,3 +1,118 @@
1
  ---
2
  license: llama2
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: llama2
3
+ pipeline_tag: text-generation
4
+ tags:
5
+ - text-generation-inference
6
  ---
7
+
8
+ <div align="center">
9
+ <img src="https://cdn-uploads.huggingface.co/production/uploads/64ccdc322e592905f922a06e/VhwQtaklohkUXFWkjA-3M.png" width="450"/>
10
+
11
+ English | [简体中文](README_zh-CN.md)
12
+
13
+ </div>
14
+
15
+ <p align="center">
16
+ 👋 join us on <a href="https://twitter.com/intern_lm" target="_blank">Twitter</a>, <a href="https://discord.gg/xa29JuW87d" target="_blank">Discord</a> and <a href="https://r.vansin.top/?r=internwx" target="_blank">WeChat</a>
17
+ </p>
18
+
19
+
20
+ # W4A16 LLM Model Deployment
21
+
22
+ LMDeploy supports LLM model inference of 4-bit weight, with the minimum requirement for NVIDIA graphics cards being sm80.
23
+
24
+ Before proceeding with the inference, please ensure that lmdeploy(>=v0.0.14) is installed.
25
+
26
+ ```shell
27
+ pip install 'lmdeploy>=0.0.14'
28
+ ```
29
+
30
+ ## 4-bit LLM model Inference
31
+
32
+ You can download the pre-quantized 4-bit weight models from LMDeploy's [model zoo](https://huggingface.co/lmdeploy) and conduct inference using the following command.
33
+
34
+ Alternatively, you can quantize 16-bit weights to 4-bit weights following the ["4-bit Weight Quantization"](#4-bit-weight-quantization) section, and then perform inference as per the below instructions.
35
+
36
+ Take the 4-bit Llama-2-70B model from the model zoo as an example:
37
+
38
+ ```shell
39
+ git-lfs install
40
+ git clone https://huggingface.co/lmdeploy/llama2-chat-70b-4bit
41
+ ```
42
+
43
+ As demonstrated in the command below, first convert the model's layout using `turbomind.deploy`, and then you can interact with the AI assistant in the terminal
44
+
45
+ ```shell
46
+
47
+ ## Convert the model's layout and store it in the default path, ./workspace.
48
+ lmdeploy convert \
49
+ --model-name llama2 \
50
+ --model-path ./llama2-chat-70b-w4 \
51
+ --model-format awq \
52
+ --group-size 128
53
+
54
+ ## inference
55
+ lmdeploy chat ./workspace
56
+ ```
57
+
58
+ ## Serve with gradio
59
+
60
+ If you wish to interact with the model via web ui, please initiate the gradio server as indicated below:
61
+
62
+ ```shell
63
+ lmdeploy serve gradio ./workspace --server_name {ip_addr} --server_port {port}
64
+ ```
65
+
66
+ Subsequently, you can open the website `http://{ip_addr}:{port}` in your browser and interact with the model
67
+
68
+ ## Inference Performance
69
+
70
+ We benchmarked the Llama 2 7B and 13B with 4-bit quantization on NVIDIA GeForce RTX 4090 using [profile_generation.py](https://github.com/InternLM/lmdeploy/blob/main/benchmark/profile_generation.py). And we measure the token generation throughput (tokens/s) by setting a single prompt token and generating 512 tokens. All the results are measured for single batch inference.
71
+
72
+ | model | llm-awq | mlc-llm | turbomind |
73
+ | ----------- | ------- | ------- | --------- |
74
+ | Llama 2 7B | 112.9 | 159.4 | 206.4 |
75
+ | Llama 2 13B | N/A | 90.7 | 115.8 |
76
+
77
+ ```shell
78
+ pip install nvidia-ml-py
79
+ ```
80
+
81
+ ```bash
82
+ python profile_generation.py \
83
+ --model-path /path/to/your/model \
84
+ --concurrency 1 8 --prompt-tokens 0 512 --completion-tokens 2048 512
85
+ ```
86
+
87
+ ## 4-bit Weight Quantization
88
+
89
+ It includes two steps:
90
+
91
+ - generate quantization parameter
92
+ - quantize model according to the parameter
93
+
94
+ ### Step 1: Generate Quantization Parameter
95
+
96
+ ```shell
97
+ lmdeploy lite calibrate \
98
+ --model $HF_MODEL \
99
+ --calib_dataset 'c4' \ # Calibration dataset, supports c4, ptb, wikitext2, pileval
100
+ --calib_samples 128 \ # Number of samples in the calibration set, if memory is insufficient, you can appropriately reduce this
101
+ --calib_seqlen 2048 \ # Length of a single piece of text, if memory is insufficient, you can appropriately reduce this
102
+ --work_dir $WORK_DIR \ # Folder storing Pytorch format quantization statistics parameters and post-quantization weight
103
+ ```
104
+
105
+ ### Step2: Quantize Weights
106
+
107
+ LMDeploy employs AWQ algorithm for model weight quantization.
108
+
109
+ ```shell
110
+ lmdeploy lite auto_awq \
111
+ --model $HF_MODEL \
112
+ --w_bits 4 \ # Bit number for weight quantization
113
+ --w_sym False \ # Whether to use symmetric quantization for weights
114
+ --w_group_size 128 \ # Group size for weight quantization statistics
115
+ --work_dir $WORK_DIR \ # Directory saving quantization parameters from Step 1
116
+ ```
117
+
118
+ After the quantization is complete, the quantized model is saved to `$WORK_DIR`. Then you can proceed with model inference according to the instructions in the ["4-Bit Weight Model Inference"](#4-bit-llm-model-inference) section.
README_zh-CN.md ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # W4A16 LLM 模型部署
2
+
3
+ LMDeploy 支持 4bit 权重模型的推理,**对 NVIDIA 显卡的最低要求是 sm80**。
4
+
5
+ 在推理之前,请确保安装了 lmdeploy,版本 >= v0.0.14
6
+
7
+ ```shell
8
+ pip install 'lmdeploy>=0.0.14'
9
+ ```
10
+
11
+ ## 4bit 权重模型推理
12
+
13
+ 你可以直接从 LMDeploy 的 [model zoo](https://huggingface.co/lmdeploy) 下载已经量化好的 4bit 权重模型,直接使用下面的命令推理。也可以根据["4bit 权重量化"](#4bit-权重量化)章节的内容,把 16bit 权重量化为 4bit 权重,然后再按下述说明推理
14
+
15
+ 以 4bit 的 Llama-2-chat-70B 模型为例,可以从 model zoo 直接下载:
16
+
17
+ ```shell
18
+ git-lfs install
19
+ git clone https://huggingface.co/lmdeploy/llama2-chat-70b-4bit
20
+ ```
21
+
22
+ 执行以下命令,即可在终端与模型对话:
23
+
24
+ ```shell
25
+
26
+ ## 转换模型的layout,存放在默认路径 ./workspace 下
27
+ lmdeploy convert \
28
+ --model-name llama2 \
29
+ --model-path ./llama2-chat-70b-4bit \
30
+ --model-format awq \
31
+ --group-size 128
32
+
33
+ ## 推理
34
+ lmdeploy chat ./workspace
35
+ ```
36
+
37
+ ## 启动 gradio 服务
38
+
39
+ 如果想通过 webui 与模型对话,请执行以下命令启动 gradio 服务
40
+
41
+ ```shell
42
+ lmdeploy serve gradio ./workspace --server_name {ip_addr} --server_port {port}
43
+ ```
44
+
45
+ 然后,在浏览器中打开 http://{ip_addr}:{port},即可在线对话
46
+
47
+ ## 推理速度
48
+
49
+ 我们在 NVIDIA GeForce RTX 4090 上使用 [profile_generation.py](https://github.com/InternLM/lmdeploy/blob/main/benchmark/profile_generation.py),分别测试了 4-bit Llama-2-7B 和 Llama-2-13B 模型的 token 生成速度。测试配置为 batch size = 1,(prompt_tokens, completion_tokens) = (1, 512)
50
+
51
+ | model | llm-awq | mlc-llm | turbomind |
52
+ | ----------- | ------- | ------- | --------- |
53
+ | Llama 2 7B | 112.9 | 159.4 | 206.4 |
54
+ | Llama 2 13B | N/A | 90.7 | 115.8 |
55
+
56
+ ```shell
57
+ python benchmark/profile_generation.py \
58
+ ./workspace \
59
+ --concurrency 1 --input_seqlen 1 --output_seqlen 512
60
+ ```
61
+
62
+ ## 4bit 权重量化
63
+
64
+ 4bit 权重量化包括 2 步:
65
+
66
+ - 生成量化参数
67
+ - 根据量化参数,量化模型权重
68
+
69
+ ### 第一步:生成量化参数
70
+
71
+ ```shell
72
+ lmdeploy lite calibrate \
73
+ --model $HF_MODEL \
74
+ --calib_dataset 'c4' \ # 校准数据集,支持 c4, ptb, wikitext2, pileval
75
+ --calib_samples 128 \ # 校准集的样本数,如果显存不够,可以适当调小
76
+ --calib_seqlen 2048 \ # 单条的文本长度,如果显存不够,可以适当调小
77
+ --work_dir $WORK_DIR \ # 保存 Pytorch 格式量化统计参数和量化后权重的文件夹
78
+ ```
79
+
80
+ ### 第二步:量化权重模型
81
+
82
+ LMDeploy 使用 AWQ 算法对模型权重进行量化。在执行下面的命令时,需要把步骤1的`$WORK_DIR`传入。量化结束后,权重文件也会存放在这个目录中。然后就可以根据 ["4bit权重模型推理"](#4bit-权重模型推理)章节的说明,进行模型推理。
83
+
84
+ ```shell
85
+ lmdeploy lite auto_awq \
86
+ --model $HF_MODEL \
87
+ --w_bits 4 \ # 权重量化的 bit 数
88
+ --w_group_size 128 \ # 权重量化分组统计尺寸
89
+ --work_dir $WORK_DIR \ # 步骤 1 保存量化参数的目录
90
+ ```