1
1
# LLM量化文档
2
- swift支持使用awq, gptq, bnb, hqq, eetq技术对模型进行量化. 其中awq, gptq量化技术支持vllm进行推理加速, 且量化后的模型支持qlora微调.
2
+ swift支持使用awq、 gptq、 bnb、 hqq、 eetq技术对模型进行量化。 其中awq、 gptq量化技术支持vllm进行推理加速,需要使用校准数据集,量化性能更好,但量化速度较慢。而bnb、hqq、eetq无需校准数据,量化速度较快。这五种量化方法都支持qlora微调。
3
3
4
- ** 注意** 量化在不同指令下的作用不同
5
- - sft lora训练中指定量化用于` qlora ` ,用于降低训练所需显存
6
- - export中指定量化用于量化模型并保存。
7
- - infer中指定量化用于量化模型并推理。
4
+ awq、gptq需要使用` swift export ` 进行量化。而bnb、hqq、eetq可以直接在sft和infer时进行快速量化。
8
5
9
- 其中bnb,hqq,eetq无需校准数据,量化速度较快,在 sft lora 训练 和 infer 中使用,指定` --quant_method bnb/hqq/eetq `
10
6
11
- awq,gptq需要校准数据,在 export 中使用,` --quant_method awq/gptq `
7
+ 从vllm推理加速支持的角度来看,更推荐使用awq和gptq进行量化。从量化效果的角度来看,更推荐使用awq、hqq和gptq进行量化。而从量化速度的角度来看,更推荐使用hqq进行量化。
8
+
12
9
13
10
## 目录
14
11
- [ 环境准备] ( #环境准备 )
15
- - [ 量化微调(qlora)] ( #量化微调(qlora) )
16
12
- [ 原始模型] ( #原始模型 )
17
13
- [ 微调后模型] ( #微调后模型 )
14
+ - [ QLoRA微调] ( #QLoRA微调 )
18
15
- [ 推送模型] ( #推送模型 )
19
16
20
17
## 环境准备
@@ -35,6 +32,9 @@ pip install autoawq -U
35
32
# auto_gptq和cuda版本有对应关系,请按照`https://github.com/PanQiWei/AutoGPTQ#quick-installation`选择版本
36
33
pip install auto_gptq -U
37
34
35
+ # 使用bnb量化:
36
+ pip install bitsandbytes -U
37
+
38
38
# 使用hqq量化:
39
39
# 需要transformers版本>4.40,从源码安装
40
40
pip install git+https://github.com/huggingface/transformers
@@ -58,54 +58,9 @@ pip install -r requirements/framework.txt -U
58
58
pip install -r requirements/llm.txt -U
59
59
```
60
60
61
- ## 量化微调(qlora)
62
- 在sft lora训练中指定` --quant_method ` 和` --quantization_bit ` 来执行qlora,显著减少训练所需显存
63
- ``` bash
64
- CUDA_VISIBLE_DEVICES=0 swift sft \
65
- --model_type qwen1half-7b-chat \
66
- --sft_type lora \
67
- --dataset alpaca-zh#5000 \
68
- --quant_method hqq \
69
- --quantization_bit 4 \
70
-
71
- CUDA_VISIBLE_DEVICES=0 swift sft \
72
- --model_type qwen1half-7b-chat \
73
- --sft_type lora \
74
- --dataset alpaca-zh#5000 \
75
- --quant_method eetq \
76
- --dtype fp16 \
77
-
78
- CUDA_VISIBLE_DEVICES=0 swift sft \
79
- --model_type qwen1half-7b-chat \
80
- --sft_type lora \
81
- --dataset alpaca-zh#5000 \
82
- --quant_method bnb \
83
- --quantization_bit 4 \
84
- --dtype fp16 \
85
- ```
86
- ** 注意**
87
- - hqq支持更多自定义参数,比如为不同网络层指定不同量化配置,具体请见[ 命令行参数] ( https://github.com/modelscope/swift/blob/main/docs/source/LLM/命令行参数.md )
88
- - eetq量化为8bit量化,无需指定quantization_bit。目前不支持bf16,需要指定dtype为fp16
89
- - eetq目前qlora速度比较慢,推荐使用hqq。参考[ issue] ( https://github.com/NetEase-FuXi/EETQ/issues/17 )
90
-
91
61
## 原始模型
92
- 使用bnb,hqq,eetq量化模型并推理
93
- ``` bash
94
- CUDA_VISIBLE_DEVICES=0 swift infer \
95
- --model_type qwen1half-7b-chat \
96
- --quant_method bnb \
97
- --quantization_bit 4
98
62
99
- CUDA_VISIBLE_DEVICES=0 swift infer \
100
- --model_type qwen1half-7b-chat \
101
- --quant_method hqq \
102
- --quantization_bit 4
103
-
104
- CUDA_VISIBLE_DEVICES=0 swift infer \
105
- --model_type qwen1half-7b-chat \
106
- --quant_method eetq \
107
- --dtype fp16
108
- ```
63
+ ### awq、gptq
109
64
110
65
这里展示对qwen1half-7b-chat进行awq, gptq量化.
111
66
``` bash
@@ -234,6 +189,25 @@ CUDA_VISIBLE_DEVICES=0 swift infer --model_type qwen1half-7b-chat
234
189
```
235
190
236
191
192
+ ### bnb、hqq、eetq
193
+ 对于bnb、hqq、eetq,我们只需要使用swift infer来进行快速量化并推理。
194
+ ``` bash
195
+ CUDA_VISIBLE_DEVICES=0 swift infer \
196
+ --model_type qwen1half-7b-chat \
197
+ --quant_method bnb \
198
+ --quantization_bit 4
199
+
200
+ CUDA_VISIBLE_DEVICES=0 swift infer \
201
+ --model_type qwen1half-7b-chat \
202
+ --quant_method hqq \
203
+ --quantization_bit 4
204
+
205
+ CUDA_VISIBLE_DEVICES=0 swift infer \
206
+ --model_type qwen1half-7b-chat \
207
+ --quant_method eetq \
208
+ --dtype fp16
209
+ ```
210
+
237
211
## 微调后模型
238
212
239
213
假设你使用lora微调了qwen1half-4b-chat, 模型权重目录为: ` output/qwen1half-4b-chat/vx-xxx/checkpoint-xxx ` .
@@ -281,6 +255,65 @@ curl http://localhost:8000/v1/chat/completions \
281
255
}'
282
256
```
283
257
258
+ ## QLoRA微调
259
+
260
+ ### awq、gptq
261
+ 如果想要对awq、gptq量化的模型进行qlora微调,你需要进行提前量化。例如可以对原始模型使用` swift export ` 进行量化。然后使用以下命令进行微调,你需要指定` --quant_method ` 来指定对应量化的方式:
262
+
263
+ ``` bash
264
+ # awq
265
+ CUDA_VISIBLE_DEVICES=0 swift sft \
266
+ --model_type qwen1half-7b-chat \
267
+ --model_id_or_path qwen1half-7b-chat-awq-int4 \
268
+ --quant_method awq \
269
+ --sft_type lora \
270
+ --dataset alpaca-zh#5000 \
271
+
272
+ # gptq
273
+ CUDA_VISIBLE_DEVICES=0 swift sft \
274
+ --model_type qwen1half-7b-chat \
275
+ --model_id_or_path qwen1half-7b-chat-gptq-int4 \
276
+ --quant_method gptq \
277
+ --sft_type lora \
278
+ --dataset alpaca-zh#5000 \
279
+ ```
280
+
281
+
282
+ ### bnb、hqq、eetq
283
+ 如果想要使用bnb、hqq、eetq进行qlora微调,你需要在训练中指定` --quant_method ` 和` --quantization_bit ` :
284
+
285
+ ``` bash
286
+ # bnb
287
+ CUDA_VISIBLE_DEVICES=0 swift sft \
288
+ --model_type qwen1half-7b-chat \
289
+ --sft_type lora \
290
+ --dataset alpaca-zh#5000 \
291
+ --quant_method bnb \
292
+ --quantization_bit 4 \
293
+ --dtype fp16 \
294
+
295
+ # hqq
296
+ CUDA_VISIBLE_DEVICES=0 swift sft \
297
+ --model_type qwen1half-7b-chat \
298
+ --sft_type lora \
299
+ --dataset alpaca-zh#5000 \
300
+ --quant_method hqq \
301
+ --quantization_bit 4 \
302
+
303
+ # eetq
304
+ CUDA_VISIBLE_DEVICES=0 swift sft \
305
+ --model_type qwen1half-7b-chat \
306
+ --sft_type lora \
307
+ --dataset alpaca-zh#5000 \
308
+ --quant_method eetq \
309
+ --dtype fp16 \
310
+ ```
311
+
312
+ ** 注意**
313
+ - hqq支持更多自定义参数,比如为不同网络层指定不同量化配置,具体请见[ 命令行参数] ( https://github.com/modelscope/swift/blob/main/docs/source/LLM/命令行参数.md )
314
+ - eetq量化为8bit量化,无需指定quantization_bit。目前不支持bf16,需要指定dtype为fp16
315
+ - eetq目前qlora速度比较慢,推荐使用hqq。参考[ issue] ( https://github.com/NetEase-FuXi/EETQ/issues/17 )
316
+
284
317
285
318
## 推送模型
286
319
假设你使用lora微调了qwen1half-4b-chat, 模型权重目录为: ` output/qwen1half-4b-chat/vx-xxx/checkpoint-xxx ` .
0 commit comments