Skip to content

Commit 9efe7dd

Browse files
authored
support qwem-72b qwen-1_8b qwen-audio (#180)
1 parent b4a0f37 commit 9efe7dd

File tree

64 files changed

+2038
-527
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2038
-527
lines changed

README.md

Lines changed: 30 additions & 23 deletions
Large diffs are not rendered by default.

README_CN.md

Lines changed: 30 additions & 23 deletions
Large diffs are not rendered by default.

docs/source/LLM/LLM微调文档.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@ pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
88
git clone https://github.com/modelscope/swift.git
99
cd swift
1010
pip install -e .[llm]
11-
# 下面的脚本需要在此目录下执行
12-
cd examples/pytorch/llm
1311

1412
# 如果你想要使用deepspeed.
1513
pip install deepspeed -U
1614

1715
# 如果你想要使用基于auto_gptq的qlora训练. (推荐, 效果优于bnb)
18-
# 支持auto_gptq的模型: https://github.com/modelscope/swift/blob/main/docs/source/LLM/支持的模型和数据集.md#-模型
19-
# auto_gptq和cuda版本有对应关系,请按照https://github.com/PanQiWei/AutoGPTQ#quick-installation选择版本
16+
# 支持auto_gptq的模型: `https://github.com/modelscope/swift/blob/main/docs/source/LLM/支持的模型和数据集.md#模型`
17+
# auto_gptq和cuda版本有对应关系,请按照`https://github.com/PanQiWei/AutoGPTQ#quick-installation`选择版本
2018
pip install auto_gptq
2119

2220
# 如果你想要使用基于bnb的qlora训练.
2321
pip install bitsandbytes -U
22+
23+
# 环境对齐 (如果你运行错误, 可以跑下面的代码, 仓库使用最新环境测试)
24+
pip install -r requirements/framework.txt -U
25+
pip install -r requirements/llm.txt -U
2426
```
2527

2628
## 微调
@@ -94,16 +96,21 @@ swift sft \
9496

9597
训练显存: qlora(低,3090) < lora < full(高,2*A100)
9698

99+
```bash
100+
# 下面的脚本需要在此目录下执行
101+
cd examples/pytorch/llm
102+
```
103+
97104
**提示**:
98105

99106
- 我们默认在训练时设置`--gradient_checkpointing true`**节约显存**, 这会略微降低训练速度.
100-
- 如果你想要使用量化参数`--quantization_bit 4`, 你需要先安装[bnb](https://github.com/TimDettmers/bitsandbytes): `pip install bitsandbytes -U`.
107+
- 如果你想要使用量化参数`--quantization_bit 4`, 你需要先安装[bnb](https://github.com/TimDettmers/bitsandbytes): `pip install bitsandbytes -U`. 这会减少显存消耗, 但通常会降低训练速度.
101108
- 如果你想要使用基于**auto_gptq**的量化, 你需要先安装对应cuda版本的[auto_gptq](https://github.com/PanQiWei/AutoGPTQ): `pip install auto_gptq -U`.
102-
使用auto_gptq的模型可以查看[LLM支持的模型](https://github.com/modelscope/swift/blob/main/docs/source/LLM/支持的模型和数据集.md#-模型). 建议使用auto_gptq, 而不是bnb.
109+
> 使用auto_gptq的模型可以查看[LLM支持的模型](https://github.com/modelscope/swift/blob/main/docs/source/LLM/支持的模型和数据集.md#模型). 建议使用auto_gptq, 而不是bnb.
103110
- 如果你想要使用deepspeed, 你需要`pip install deepspeed -U`. 使用deepspeed可以**节约显存**, 但可能会略微降低训练速度.
104111
- 如果你使用的是**V100**等较老的GPU, 你需要设置`--dtype AUTO`或者`--dtype fp16`, 因为其不支持bf16.
105-
- 如果你的机器是A100等高性能显卡, 且使用的是qwen系列模型, 推荐你安装[**flash-attn**](https://github.com/Dao-AILab/flash-attention), 这将会加快训练和推理的速度以及显存占用(A10, 3090, V100等显卡不支持flash-attn进行训练). 支持flash-attn的模型可以查看[LLM支持的模型](https://github.com/modelscope/swift/blob/main/docs/source/LLM/支持的模型和数据集.md#-模型)
106-
- 如果你要进行**二次预训练**, **多轮对话**, 你可以参考[自定义与拓展](https://github.com/modelscope/swift/blob/main/docs/source/LLM/自定义与拓展.md#-注册数据集的方式)
112+
- 如果你的机器是A100等高性能显卡, 且使用的是qwen系列模型, 推荐你安装[**flash-attn**](https://github.com/Dao-AILab/flash-attention), 这将会加快训练和推理的速度以及显存占用(A10, 3090, V100等显卡不支持flash-attn进行训练). 支持flash-attn的模型可以查看[LLM支持的模型](https://github.com/modelscope/swift/blob/main/docs/source/LLM/支持的模型和数据集.md#模型)
113+
- 如果你要进行**二次预训练**, **多轮对话**, 你可以参考[自定义与拓展](https://github.com/modelscope/swift/blob/main/docs/source/LLM/自定义与拓展.md#注册数据集的方式)
107114
- 如果你需要断网进行训练, 请使用`--model_cache_dir`和设置`--check_model_is_latest false`. 具体参数含义请查看[命令行超参数](https://github.com/modelscope/swift/blob/main/docs/source/LLM/命令行超参数.md).
108115
- 如果你想在训练时, 将权重push到ModelScope Hub中, 你需要设置`--push_to_hub true`.
109116
- 如何你想要在推理时, 合并LoRA权重并保存,你需要设置`--merge_lora_and_save true`. **不推荐对qlora训练的模型进行merge**, 这会存在精度损失.

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

Lines changed: 161 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
88
git clone https://github.com/modelscope/swift.git
99
cd swift
1010
pip install -e .[llm]
11+
12+
# 如果你想要使用基于auto_gptq的模型进行推理.
13+
# 使用auto_gptq的模型: `https://github.com/modelscope/swift/blob/main/docs/source/LLM/支持的模型和数据集.md#模型`
14+
# auto_gptq和cuda版本有对应关系,请按照`https://github.com/PanQiWei/AutoGPTQ#quick-installation`选择版本
15+
pip install auto_gptq
16+
17+
# 环境对齐 (如果你运行错误, 可以跑下面的代码, 仓库使用最新环境测试)
18+
pip install -r requirements/framework.txt -U
19+
pip install -r requirements/llm.txt -U
1120
```
1221

1322
## 推理
@@ -74,6 +83,44 @@ you are a helpful assistant!<|im_end|>
7483
"""
7584
```
7685

86+
### qwen-7b-chat-int4
87+
```python
88+
import os
89+
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
90+
91+
from swift.llm import (
92+
get_model_tokenizer, get_template, inference, ModelType, get_default_template_type,
93+
)
94+
from swift.utils import seed_everything
95+
import torch
96+
97+
model_type = ModelType.qwen_7b_chat_int4
98+
template_type = get_default_template_type(model_type)
99+
print(f'template_type: {template_type}') # template_type: chatml
100+
101+
model, tokenizer = get_model_tokenizer(model_type, model_kwargs={'device_map': 'auto'})
102+
103+
template = get_template(template_type, tokenizer)
104+
seed_everything(42)
105+
query = '浙江的省会在哪里?'
106+
response, history = inference(model, template, query)
107+
print(f'query: {query}')
108+
print(f'response: {response}')
109+
query = '这有什么好吃的?'
110+
response, history = inference(model, template, query, history)
111+
print(f'query: {query}')
112+
print(f'response: {response}')
113+
print(f'history: {history}')
114+
115+
"""Out[0]
116+
query: 浙江的省会在哪里?
117+
response: 浙江省的省会是杭州。
118+
query: 这有什么好吃的?
119+
response: 杭州有很多著名的美食,例如西湖醋鱼、东坡肉、宋嫂鱼羹、叫化鸡等。此外,还有杭州特色的点心,如桂花糖藕、酒酿圆子、麻婆豆腐等等。
120+
history: [('浙江的省会在哪里?', '浙江省的省会是杭州。'), ('这有什么好吃的?', '杭州有很多著名的美食,例如西湖醋鱼、东坡肉、宋嫂鱼羹、叫化鸡等。此外,还有杭州特色的点心,如桂花糖藕、酒酿圆子、麻婆豆腐等等。')]
121+
"""
122+
```
123+
77124
### qwen-7b
78125
```python
79126
import os
@@ -131,8 +178,7 @@ model_type = ModelType.qwen_7b_chat
131178
template_type = get_default_template_type(model_type)
132179
print(f'template_type: {template_type}') # template_type: chatml
133180

134-
model, tokenizer = get_model_tokenizer(model_type, torch.bfloat16, {'device_map': 'auto'})
135-
model.generation_config.max_new_tokens = 128
181+
model, tokenizer = get_model_tokenizer(model_type, model_kwargs={'device_map': 'auto'})
136182

137183
template = get_template(template_type, tokenizer)
138184
seed_everything(42)
@@ -159,7 +205,7 @@ history: [('浙江的省会在哪里?', '浙江省的省会是杭州。'), ('
159205
"""
160206
```
161207

162-
### 量化
208+
### qwen-vl-chat
163209
```python
164210
import os
165211
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
@@ -168,40 +214,79 @@ from swift.llm import (
168214
get_model_tokenizer, get_template, inference, ModelType, get_default_template_type,
169215
)
170216
from swift.utils import seed_everything
171-
from modelscope import BitsAndBytesConfig
172217
import torch
173218

174-
model_type = ModelType.qwen_7b_chat
219+
model_type = ModelType.qwen_vl_chat
175220
template_type = get_default_template_type(model_type)
176221
print(f'template_type: {template_type}') # template_type: chatml
177222

178-
torch_dtype = torch.bfloat16
179-
quantization_config = BitsAndBytesConfig(load_in_4bit=True,
180-
bnb_4bit_compute_dtype=torch_dtype,
181-
bnb_4bit_quant_type='nf4',
182-
bnb_4bit_use_double_quant=True)
183-
model, tokenizer = get_model_tokenizer(model_type, torch_dtype, {'device_map': 'auto',
184-
'quantization_config': quantization_config})
185-
model.generation_config.max_new_tokens = 128
223+
model, tokenizer = get_model_tokenizer(model_type, model_kwargs={'device_map': 'auto'})
186224

187225
template = get_template(template_type, tokenizer)
188226
seed_everything(42)
189-
query = '浙江的省会在哪里?'
227+
query = tokenizer.from_list_format([
228+
{'image': 'https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg'},
229+
{'text': '这是什么'},
230+
])
190231
response, history = inference(model, template, query)
191232
print(f'query: {query}')
192233
print(f'response: {response}')
193-
query = '这有什么好吃的?'
234+
query = '输出击掌的检测框'
194235
response, history = inference(model, template, query, history)
195236
print(f'query: {query}')
196237
print(f'response: {response}')
197238
print(f'history: {history}')
239+
image = tokenizer.draw_bbox_on_latest_picture(response, history)
240+
image.save('output_chat.jpg')
241+
"""
242+
query: Picture 1:<img>https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg</img>
243+
这是什么
244+
response: 图中是一名女子在沙滩上和狗玩耍,旁边的狗是一只拉布拉多犬,它们处于沙滩上。
245+
query: 输出击掌的检测框
246+
response: <ref>击掌</ref><box>(523,513),(584,605)</box>
247+
history: [('Picture 1:<img>https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg</img>\n这是什么', '图中是一名女子在沙滩上和狗玩耍,旁边的狗是一只拉布拉多犬,它们处于沙滩上。'), ('输出击掌的检测框', '<ref>击掌</ref><box>(523,513),(584,605)</box>')]
248+
"""
249+
```
198250

199-
"""Out[0]
200-
query: 浙江的省会在哪里?
201-
response: 浙江省会是杭州。
202-
query: 这有什么好吃的?
203-
response: 浙江有许多美食,比如西湖醋鱼、龙井虾仁、东坡肉、梅干菜烧肉等,这些都是浙江地区非常有名的食物。此外,浙江还盛产海鲜,如螃蟹、海螺、贝壳类和各种鱼类。
204-
history: [('浙江的省会在哪里?', '浙江省会是杭州。'), ('这有什么好吃的?', '浙江有许多美食,比如西湖醋鱼、龙井虾仁、东坡肉、梅干菜烧肉等,这些都是浙江地区非常有名的食物。此外,浙江还盛产海鲜,如螃蟹、海螺、贝壳类和各种鱼类。')]
251+
### qwen-audio-chat
252+
```python
253+
import os
254+
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
255+
256+
from swift.llm import (
257+
get_model_tokenizer, get_template, inference, ModelType, get_default_template_type,
258+
)
259+
from swift.utils import seed_everything
260+
import torch
261+
262+
model_type = ModelType.qwen_audio_chat
263+
template_type = get_default_template_type(model_type)
264+
print(f'template_type: {template_type}') # template_type: chatml
265+
266+
model, tokenizer = get_model_tokenizer(model_type, model_kwargs={'device_map': 'auto'})
267+
268+
template = get_template(template_type, tokenizer)
269+
270+
seed_everything(42)
271+
query = tokenizer.from_list_format([
272+
{'audio': 'demo.wav'},
273+
{'text': '请将语音转成文本'},
274+
])
275+
response, history = inference(model, template, query)
276+
print(f'query: {query}')
277+
print(f'response: {response}')
278+
query = '这句话一般在什么语境下使用'
279+
response, history = inference(model, template, query, history)
280+
print(f'query: {query}')
281+
print(f'response: {response}')
282+
print(f'history: {history}')
283+
"""
284+
query: Audio 1:<audio>demo.wav</audio>
285+
请将语音转成文本
286+
response: 好的,这是转成的文本:"每一天都要快乐哦"。
287+
query: 这句话一般在什么语境下使用
288+
response: 这句话一般在表达祝福或者鼓励的时候使用,比如在朋友或者亲人过生日的时候说"每一天都要快乐哦",表达祝福的意思。
289+
history: [('Audio 1:<audio>demo.wav</audio>\n请将语音转成文本', '好的,这是转成的文本:"每一天都要快乐哦"。'), ('这句话一般在什么语境下使用', '这句话一般在表达祝福或者鼓励的时候使用,比如在朋友或者亲人过生日的时候说"每一天都要快乐哦",表达祝福的意思。')]
205290
"""
206291
```
207292

@@ -220,8 +305,7 @@ model_type = ModelType.chatglm3_6b
220305
template_type = get_default_template_type(model_type)
221306
print(f'template_type: {template_type}') # template_type: chatglm3
222307

223-
model, tokenizer = get_model_tokenizer(model_type, torch.bfloat16, {'device_map': 'auto'})
224-
# 修改max_new_tokens
308+
model, tokenizer = get_model_tokenizer(model_type, model_kwargs={'device_map': 'auto'})
225309
model.generation_config.max_new_tokens = 128
226310

227311
template = get_template(template_type, tokenizer)
@@ -250,6 +334,60 @@ history: [('浙江的省会在哪里?', '浙江的省会是杭州。'), ('这
250334
"""
251335
```
252336

337+
338+
### 量化
339+
```python
340+
import os
341+
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
342+
343+
from swift.llm import (
344+
get_model_tokenizer, get_template, inference, ModelType, get_default_template_type,
345+
)
346+
from swift.utils import seed_everything
347+
from modelscope import BitsAndBytesConfig
348+
import torch
349+
350+
model_type = ModelType.chatglm3_6b
351+
template_type = get_default_template_type(model_type)
352+
print(f'template_type: {template_type}') # template_type: chatglm3
353+
354+
torch_dtype = torch.bfloat16
355+
quantization_config = BitsAndBytesConfig(load_in_4bit=True,
356+
bnb_4bit_compute_dtype=torch_dtype,
357+
bnb_4bit_quant_type='nf4',
358+
bnb_4bit_use_double_quant=True)
359+
model, tokenizer = get_model_tokenizer(model_type, torch_dtype, {'device_map': 'auto',
360+
'quantization_config': quantization_config})
361+
model.generation_config.max_new_tokens = 128
362+
template = get_template(template_type, tokenizer)
363+
seed_everything(42)
364+
query = '浙江的省会在哪里?'
365+
response, history = inference(model, template, query)
366+
print(f'query: {query}')
367+
print(f'response: {response}')
368+
query = '这有什么好吃的?'
369+
response, history = inference(model, template, query, history)
370+
print(f'query: {query}')
371+
print(f'response: {response}')
372+
print(f'history: {history}')
373+
374+
"""Out[0]
375+
query: 浙江的省会在哪里?
376+
response: 浙江的省会是杭州。
377+
query: 这有什么好吃的?
378+
response: 浙江有很多美食,以下是一些著名的:
379+
380+
1. 杭州小笼包:这是杭州著名的传统小吃,外皮薄而有韧性,内馅鲜美多汁。
381+
382+
2. 浙江粽子:浙江粽子有多种口味,如咸蛋黄肉粽、豆沙粽等,其中以杭州粽子最为著名。
383+
384+
3. 油爆虾:这是浙江海鲜中的代表之一,用热油爆炒虾仁,口感鲜嫩。
385+
386+
4. 椒盐土豆丝:这是浙江传统的素菜之一,用土豆丝和椒盐一起炒制,口感清爽。
387+
388+
history: [('浙江的省会在哪里?', '浙江的省会是杭州。'), ('这有什么好吃的?', '浙江有很多美食,以下是一些著名的:\n\n1. 杭州小笼包:这是杭州著名的传统小吃,外皮薄而有韧性,内馅鲜美多汁。\n\n2. 浙江粽子:浙江粽子有多种口味,如咸蛋黄肉粽、豆沙粽等,其中以杭州粽子最为著名。\n\n3. 油爆虾:这是浙江海鲜中的代表之一,用热油爆炒虾仁,口感鲜嫩。\n\n4. 椒盐土豆丝:这是浙江传统的素菜之一,用土豆丝和椒盐一起炒制,口感清爽。\n')]
389+
"""
390+
```
253391
## Web-UI
254392
### qwen-7b-chat
255393
使用CLI

0 commit comments

Comments
 (0)