@@ -8,6 +8,15 @@ pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
88git clone https://github.com/modelscope/swift.git
99cd swift
1010pip 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
79126import os
@@ -131,8 +178,7 @@ model_type = ModelType.qwen_7b_chat
131178template_type = get_default_template_type(model_type)
132179print (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
137183template = get_template(template_type, tokenizer)
138184seed_everything(42 )
@@ -159,7 +205,7 @@ history: [('浙江的省会在哪里?', '浙江省的省会是杭州。'), ('
159205"""
160206```
161207
162- ### 量化
208+ ### qwen-vl-chat
163209``` python
164210import os
165211os.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)
170216from swift.utils import seed_everything
171- from modelscope import BitsAndBytesConfig
172217import torch
173218
174- model_type = ModelType.qwen_7b_chat
219+ model_type = ModelType.qwen_vl_chat
175220template_type = get_default_template_type(model_type)
176221print (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
187225template = get_template(template_type, tokenizer)
188226seed_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+ ])
190231response, history = inference(model, template, query)
191232print (f ' query: { query} ' )
192233print (f ' response: { response} ' )
193- query = ' 这有什么好吃的? '
234+ query = ' 输出击掌的检测框 '
194235response, history = inference(model, template, query, history)
195236print (f ' query: { query} ' )
196237print (f ' response: { response} ' )
197238print (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
220305template_type = get_default_template_type(model_type)
221306print (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' })
225309model.generation_config.max_new_tokens = 128
226310
227311template = 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\n 1. 杭州小笼包:这是杭州著名的传统小吃,外皮薄而有韧性,内馅鲜美多汁。\n\n 2. 浙江粽子:浙江粽子有多种口味,如咸蛋黄肉粽、豆沙粽等,其中以杭州粽子最为著名。\n\n 3. 油爆虾:这是浙江海鲜中的代表之一,用热油爆炒虾仁,口感鲜嫩。\n\n 4. 椒盐土豆丝:这是浙江传统的素菜之一,用土豆丝和椒盐一起炒制,口感清爽。\n ')]
389+ """
390+ ```
253391## Web-UI
254392### qwen-7b-chat
255393使用CLI
0 commit comments