Skip to content

Commit d4bb787

Browse files
authored
Update code (#962)
1 parent f3c141b commit d4bb787

23 files changed

+394
-214
lines changed

docs/source/LLM/LLM推理文档.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,26 +183,47 @@ model, tokenizer = get_model_tokenizer(model_type, model_kwargs={'device_map': '
183183

184184
template = get_template(template_type, tokenizer)
185185
seed_everything(42)
186+
186187
query = '浙江的省会在哪里?'
187188
gen = inference_stream(model, template, query)
188189
print(f'query: {query}')
189190
for response, history in gen:
190-
print(f'response: {response}')
191+
pass
192+
print(f'response: {response}')
193+
194+
# 方式1
191195
query = '这有什么好吃的?'
192-
gen = inference_stream(model, template, query, history)
196+
old_history = history
197+
gen = inference_stream(model, template, query, old_history)
193198
print(f'query: {query}')
194199
for response, history in gen:
195200
print(f'response: {response}')
196201
print(f'history: {history}')
197202

203+
# 方式2
204+
query = '这有什么好吃的?'
205+
gen = inference_stream(model, template, query, old_history)
206+
print_idx = 0
207+
print(f'query: {query}\nresponse: ', end='')
208+
for response, history in gen:
209+
delta = response[print_idx:]
210+
print(delta, end='', flush=True)
211+
print_idx = len(response)
212+
print(f'\nhistory: {history}')
213+
198214
"""Out[0]
199215
query: 浙江的省会在哪里?
200-
...
201216
response: 浙江省的省会是杭州。
202217
query: 这有什么好吃的?
218+
response: 杭
219+
response: 杭州
220+
response: 杭州市有
203221
...
204-
response: 杭州市有很多著名的美食,例如西湖醋鱼、龙井虾仁、糖醋排骨、毛血旺等。此外,还有杭州特色的点心,如桂花糕、荷花酥、艾窝窝等。
205-
history: [('浙江的省会在哪里?', '浙江省的省会是杭州。'), ('这有什么好吃的?', '杭州市有很多著名的美食,例如西湖醋鱼、龙井虾仁、糖醋排骨、毛血旺等。此外,还有杭州特色的点心,如桂花糕、荷花酥、艾窝窝等。')]
222+
response: 杭州市有很多著名的美食,例如西湖醋鱼、龙井虾仁、糖醋排骨、毛血旺等。此外,还有杭州特色的点心,如桂花酥饼、抹茶糕点等。
223+
history: [['浙江的省会在哪里?', '浙江省的省会是杭州。'], ['这有什么好吃的?', '杭州市有很多著名的美食,例如西湖醋鱼、龙井虾仁、糖醋排骨、毛血旺等。此外,还有杭州特色的点心,如桂花酥饼、抹茶糕点等。']]
224+
query: 这有什么好吃的?
225+
response: 杭州有许多美食,比如西湖醋鱼、龙井虾仁、酱鸭等。此外,还有许多小吃,如烧麦、春卷、油条等,都是浙江特色美食。
226+
history: [['浙江的省会在哪里?', '浙江省的省会是杭州。'], ['这有什么好吃的?', '杭州有许多美食,比如西湖醋鱼、龙井虾仁、酱鸭等。此外,还有许多小吃,如烧麦、春卷、油条等,都是浙江特色美食。']]
206227
"""
207228
```
208229

docs/source/LLM/LLM量化文档.md

Lines changed: 87 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
# LLM量化文档
2-
swift支持使用awq, gptq, bnb, hqq, eetq技术对模型进行量化. 其中awq, gptq量化技术支持vllm进行推理加速, 且量化后的模型支持qlora微调.
2+
swift支持使用awqgptqbnbhqqeetq技术对模型进行量化其中awqgptq量化技术支持vllm进行推理加速,需要使用校准数据集,量化性能更好,但量化速度较慢。而bnb、hqq、eetq无需校准数据,量化速度较快。这五种量化方法都支持qlora微调。
33

4-
**注意** 量化在不同指令下的作用不同
5-
- sft lora训练中指定量化用于`qlora`,用于降低训练所需显存
6-
- export中指定量化用于量化模型并保存。
7-
- infer中指定量化用于量化模型并推理。
4+
awq、gptq需要使用`swift export`进行量化。而bnb、hqq、eetq可以直接在sft和infer时进行快速量化。
85

9-
其中bnb,hqq,eetq无需校准数据,量化速度较快,在 sft lora 训练 和 infer 中使用,指定`--quant_method bnb/hqq/eetq`
106

11-
awq,gptq需要校准数据,在 export 中使用,`--quant_method awq/gptq`
7+
从vllm推理加速支持的角度来看,更推荐使用awq和gptq进行量化。从量化效果的角度来看,更推荐使用awq、hqq和gptq进行量化。而从量化速度的角度来看,更推荐使用hqq进行量化。
8+
129

1310
## 目录
1411
- [环境准备](#环境准备)
15-
- [量化微调(qlora)](#量化微调(qlora))
1612
- [原始模型](#原始模型)
1713
- [微调后模型](#微调后模型)
14+
- [QLoRA微调](#QLoRA微调)
1815
- [推送模型](#推送模型)
1916

2017
## 环境准备
@@ -35,6 +32,9 @@ pip install autoawq -U
3532
# auto_gptq和cuda版本有对应关系,请按照`https://github.com/PanQiWei/AutoGPTQ#quick-installation`选择版本
3633
pip install auto_gptq -U
3734

35+
# 使用bnb量化:
36+
pip install bitsandbytes -U
37+
3838
# 使用hqq量化:
3939
# 需要transformers版本>4.40,从源码安装
4040
pip install git+https://github.com/huggingface/transformers
@@ -58,54 +58,9 @@ pip install -r requirements/framework.txt -U
5858
pip install -r requirements/llm.txt -U
5959
```
6060

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-
9161
## 原始模型
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
9862

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
10964

11065
这里展示对qwen1half-7b-chat进行awq, gptq量化.
11166
```bash
@@ -234,6 +189,25 @@ CUDA_VISIBLE_DEVICES=0 swift infer --model_type qwen1half-7b-chat
234189
```
235190

236191

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+
237211
## 微调后模型
238212

239213
假设你使用lora微调了qwen1half-4b-chat, 模型权重目录为: `output/qwen1half-4b-chat/vx-xxx/checkpoint-xxx`.
@@ -281,6 +255,65 @@ curl http://localhost:8000/v1/chat/completions \
281255
}'
282256
```
283257

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+
284317

285318
## 推送模型
286319
假设你使用lora微调了qwen1half-4b-chat, 模型权重目录为: `output/qwen1half-4b-chat/vx-xxx/checkpoint-xxx`.

docs/source/LLM/Qwen1.5全流程最佳实践.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,9 @@ for query in ['78654+657=?', '晚上睡不着觉怎么办']:
413413

414414
print(f'query: {query}')
415415
print('response: ', end='')
416+
response = ''
416417
for chunk in stream_resp:
418+
response += chunk.choices[0].delta.content
417419
print(chunk.choices[0].delta.content, end='', flush=True)
418420
print()
419421
messages.append({'role': 'assistant', 'content': response})
@@ -574,7 +576,9 @@ for query in ['78654+657=?', '晚上睡不着觉怎么办']:
574576

575577
print(f'query: {query}')
576578
print('response: ', end='')
579+
response = ''
577580
for chunk in stream_resp:
581+
response += chunk.choices[0].delta.content
578582
print(chunk.choices[0].delta.content, end='', flush=True)
579583
print()
580584
messages.append({'role': 'assistant', 'content': response})

docs/source/LLM/支持的模型和数据集.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@
121121
|llama-3-chinese-8b-instruct|[ChineseAlpacaGroup/llama-3-chinese-8b-instruct](https://modelscope.cn/models/ChineseAlpacaGroup/llama-3-chinese-8b-instruct/summary)|q_proj, k_proj, v_proj|llama3|✔|✔||-|[hfl/llama-3-chinese-8b-instruct](https://huggingface.co/hfl/llama-3-chinese-8b-instruct)|
122122
|atom-7b|[FlagAlpha/Atom-7B](https://modelscope.cn/models/FlagAlpha/Atom-7B/summary)|q_proj, k_proj, v_proj|default-generation|✔|✔||-|[FlagAlpha/Atom-7B](https://huggingface.co/FlagAlpha/Atom-7B)|
123123
|atom-7b-chat|[FlagAlpha/Atom-7B-Chat](https://modelscope.cn/models/FlagAlpha/Atom-7B-Chat/summary)|q_proj, k_proj, v_proj|atom|✔|✔||-|[FlagAlpha/Atom-7B-Chat](https://huggingface.co/FlagAlpha/Atom-7B-Chat)|
124-
|llava1d6-mistral-7b-instruct|[AI-ModelScope/llava-v1.6-mistral-7b](https://modelscope.cn/models/AI-ModelScope/llava-v1.6-mistral-7b/summary)|q_proj, k_proj, v_proj|llava-mistral-instruct|✔|✘|transformers>=4.34|multi-modal, vision|[liuhaotian/llava-v1.6-mistral-7b](https://huggingface.co/liuhaotian/llava-v1.6-mistral-7b)|
125-
|llava1d6-yi-34b-instruct|[AI-ModelScope/llava-v1.6-34b](https://modelscope.cn/models/AI-ModelScope/llava-v1.6-34b/summary)|q_proj, k_proj, v_proj|llava-yi-instruct|✔|✘||multi-modal, vision|[liuhaotian/llava-v1.6-34b](https://huggingface.co/liuhaotian/llava-v1.6-34b)|
124+
|llava1_6-mistral-7b-instruct|[AI-ModelScope/llava-v1.6-mistral-7b](https://modelscope.cn/models/AI-ModelScope/llava-v1.6-mistral-7b/summary)|q_proj, k_proj, v_proj|llava-mistral-instruct|✔|✘|transformers>=4.34|multi-modal, vision|[liuhaotian/llava-v1.6-mistral-7b](https://huggingface.co/liuhaotian/llava-v1.6-mistral-7b)|
125+
|llava1_6-yi-34b-instruct|[AI-ModelScope/llava-v1.6-34b](https://modelscope.cn/models/AI-ModelScope/llava-v1.6-34b/summary)|q_proj, k_proj, v_proj|llava-yi-instruct|✔|✘||multi-modal, vision|[liuhaotian/llava-v1.6-34b](https://huggingface.co/liuhaotian/llava-v1.6-34b)|
126126
|llama3-llava-next-8b|[AI-Modelscope/llama3-llava-next-8b](https://modelscope.cn/models/AI-Modelscope/llama3-llava-next-8b/summary)|q_proj, k_proj, v_proj|llama-llava-next|✔|✘||multi-modal, vision|[lmms-lab/llama3-llava-next-8b](https://huggingface.co/lmms-lab/llama3-llava-next-8b)|
127127
|llava-next-72b|[AI-Modelscope/llava-next-72b](https://modelscope.cn/models/AI-Modelscope/llava-next-72b/summary)|q_proj, k_proj, v_proj|llava-qwen-instruct|✔|✘||multi-modal, vision|[lmms-lab/llava-next-72b](https://huggingface.co/lmms-lab/llava-next-72b)|
128128
|llava-next-110b|[AI-Modelscope/llava-next-110b](https://modelscope.cn/models/AI-Modelscope/llava-next-110b/summary)|q_proj, k_proj, v_proj|llava-qwen-instruct|✔|✘||multi-modal, vision|[lmms-lab/llava-next-110b](https://huggingface.co/lmms-lab/llava-next-110b)|
@@ -236,7 +236,7 @@
236236
|baichuan2-13b-chat|[baichuan-inc/Baichuan2-13B-Chat](https://modelscope.cn/models/baichuan-inc/Baichuan2-13B-Chat/summary)|W_pack|baichuan|✘|✔||-|[baichuan-inc/Baichuan2-13B-Chat](https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat)|
237237
|baichuan2-13b-chat-int4|[baichuan-inc/Baichuan2-13B-Chat-4bits](https://modelscope.cn/models/baichuan-inc/Baichuan2-13B-Chat-4bits/summary)|W_pack|baichuan|&#x2718;|&#x2718;|bitsandbytes<0.41.2, accelerate<0.26|-|[baichuan-inc/Baichuan2-13B-Chat-4bits](https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat-4bits)|
238238
|mplug-owl2-chat|[iic/mPLUG-Owl2](https://modelscope.cn/models/iic/mPLUG-Owl2/summary)|q_proj, k_proj.multiway.0, k_proj.multiway.1, v_proj.multiway.0, v_proj.multiway.1|mplug-owl2|&#x2714;|&#x2718;|transformers<4.35, icecream|-|[MAGAer13/mplug-owl2-llama2-7b](https://huggingface.co/MAGAer13/mplug-owl2-llama2-7b)|
239-
|mplug-owl2d1-chat|[iic/mPLUG-Owl2.1](https://modelscope.cn/models/iic/mPLUG-Owl2.1/summary)|c_attn.multiway.0, c_attn.multiway.1|mplug-owl2|&#x2714;|&#x2718;|transformers<4.35, icecream|-|[Mizukiluke/mplug_owl_2_1](https://huggingface.co/Mizukiluke/mplug_owl_2_1)|
239+
|mplug-owl2_1-chat|[iic/mPLUG-Owl2.1](https://modelscope.cn/models/iic/mPLUG-Owl2.1/summary)|c_attn.multiway.0, c_attn.multiway.1|mplug-owl2|&#x2714;|&#x2718;|transformers<4.35, icecream|-|[Mizukiluke/mplug_owl_2_1](https://huggingface.co/Mizukiluke/mplug_owl_2_1)|
240240
|yuan2-2b-instruct|[YuanLLM/Yuan2.0-2B-hf](https://modelscope.cn/models/YuanLLM/Yuan2.0-2B-hf/summary)|q_proj, k_proj, v_proj|yuan|&#x2714;|&#x2718;||-|[IEITYuan/Yuan2-2B-hf](https://huggingface.co/IEITYuan/Yuan2-2B-hf)|
241241
|yuan2-2b-janus-instruct|[YuanLLM/Yuan2-2B-Janus-hf](https://modelscope.cn/models/YuanLLM/Yuan2-2B-Janus-hf/summary)|q_proj, k_proj, v_proj|yuan|&#x2714;|&#x2718;||-|[IEITYuan/Yuan2-2B-Janus-hf](https://huggingface.co/IEITYuan/Yuan2-2B-Janus-hf)|
242242
|yuan2-51b-instruct|[YuanLLM/Yuan2.0-51B-hf](https://modelscope.cn/models/YuanLLM/Yuan2.0-51B-hf/summary)|q_proj, k_proj, v_proj|yuan|&#x2714;|&#x2718;||-|[IEITYuan/Yuan2-51B-hf](https://huggingface.co/IEITYuan/Yuan2-51B-hf)|

docs/source/LLM/自定义与拓展.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
我们支持三种**自定义数据集**的方法.
99

1010
1. 【推荐】**命令行参数**的形式: **更加方便支持自定义数据集**, 支持四种数据集格式(即使用`SmartPreprocessor`), 支持`dataset_id``dataset_path`.
11-
2. 添加数据集到`dataset_info.json`中, 比第一种方式更灵活, 支持对数据集使用两种预处理器并指定其参数: `RenameColumnsPreprocessor`, `ConversationsPreprocessor`(默认使用`SmartPreprocessor`). 支持直接修改swift内置的`dataset_info.json`, 或者通过`--dataset_info_path xxx.json`的方式传入外置的json文件(方便pip install而非git clone的用户拓展数据集).
12-
3. **注册数据集**的方式: 比第1、2种方式更加灵活, 支持使用函数对数据集进行预处理. 方法1、2在实现上借助了方法3. 可以直接修改源码进行拓展, 或者通过`--custom_register_path xxx.py`的方式传入, 脚本会对py文件进行解析(方便pip install的用户).
11+
2. 添加数据集到`dataset_info.json`中, 比第一种方式更灵活但繁琐, 支持对数据集使用两种预处理器并指定其参数: `RenameColumnsPreprocessor`, `ConversationsPreprocessor`(默认使用`SmartPreprocessor`). 支持直接修改swift内置的`dataset_info.json`, 或者通过`--dataset_info_path xxx.json`的方式传入外置的json文件(方便pip install而非git clone的用户拓展数据集).
12+
3. **注册数据集**的方式: 比第1、2种方式更加灵活但繁琐, 支持使用函数对数据集进行预处理. 方法1、2在实现上借助了方法3. 可以直接修改源码进行拓展, 或者通过`--custom_register_path xxx.py`的方式传入, 脚本会对py文件进行解析(方便pip install的用户).
1313

1414
### 📌 【推荐】命令行参数的形式
1515
支持直接传入行自定义的**dataset_id**(兼容MS和HF)和**dataset_path**, 以及同时传入多个自定义数据集以及对应采样数, 脚本会进行自动的预处理和拼接. 如果传入的是`dataset_id`, 默认会使用dataset\_id中的'default'子数据集, 并设置split为'train'. 如果该dataset\_id已经注册, 则会使用注册时传入的subsets、split以及预处理函数. 如果传入的是`dataset_path`, 则可以指定为相对路径和绝对路径, 其中相对路径为相对于当前运行目录.

0 commit comments

Comments
 (0)