通义千问开源模型部署使用

首先可以参考modelScope社区给出的使用文档,已经足够全面

modelscope.cn/models/qwen...

但在按照文档中步骤部署时,还是有些错误问题发生,可以搜索参考的解决方式不多,所以记录下来

个人电脑部署

这里不太建议使用自己的笔记本部署通义千问模型,因为实在是太耗资源,我使用的M2芯片的MacBook Pro即使运行起来了,但模型回答一个问题都需要四五分钟的时间,内存全部占满,其他应用程序也都强制退出了。所以还是使用社区提供的免费资源,或者有更高配置的服务器来部署模型。而且期间还有各种问题,搜了很多github上的问答才解决,耗时耗力,这里就不记录了,很不推荐这种方式。

免费算力服务器

打开modelScope社区后,点击登录注册可以看到免费赠送算力的活动

注册完成后在对应模型里可以看到,随时都能启用的服务器

这里CPU环境的服务器勉强可以跑起来模型,但运行效果感人,而且配置过程中有各种问题需要修改,而GPU环境启动模型可以说是非常流畅,体验效果也很好

CPU环境启动

社区提供的服务器配置已经很高了,8核32G,但因为是纯CPU环境,启动过程中还是有些问题

安装依赖包

第一行命令不需要运行,服务器已经自带了modelscope包

只需要新建一个Terminal窗口来执行第二条命令

启动代码

直接运行文档提供的代码会报错,这里是因为纯CPU环境导致的

错误 1

RuntimeError: "addmm_impl_cpu_" not implemented for 'Half'Hide Error Details

ini 复制代码
RuntimeError: "addmm_impl_cpu_" not implemented for 'Half'
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[1], line 8
      5 model = AutoModelForCausalLM.from_pretrained("qwen/Qwen-7B-Chat", revision = 'v1.0.5',device_map="auto", trust_remote_code=True,fp16 = True).eval()
      6 model.generation_config = GenerationConfig.from_pretrained("Qwen/Qwen-7B-Chat",revision = 'v1.0.5', trust_remote_code=True) # 可指定不同的生成长度、top_p等相关超参
----> 8 response, history = model.chat(tokenizer, "你好", history=None)
      9 print(response)
     10 response, history = model.chat(tokenizer, "浙江的省会在哪里?", history=history) 

File ~/.cache/huggingface/modules/transformers_modules/Qwen-7B-Chat/modeling_qwen.py:1010, in QWenLMHeadModel.chat(self, tokenizer, query, history, system, append_history, stream, stop_words_ids, **kwargs)
   1006 stop_words_ids.extend(get_stop_words_ids(
   1007     self.generation_config.chat_format, tokenizer
   1008 ))
   1009 input_ids = torch.tensor([context_tokens]).to(self.device)
-> 1010 outputs = self.generate(
   1011             input_ids,
   1012             stop_words_ids = stop_words_ids,
   1013             return_dict_in_generate = False,
   1014             **kwargs,
   1015         )
   1017 response = decode_tokens(
   1018     outputs[0],
   1019     tokenizer,
   (...)
   1024     errors='replace'
   1025 )
   1027 if append_history:

错误 2

ValueError: The current device_map had weights offloaded to the disk. Please provide an offload_folder for them. Alternatively, make sure you have safetensors installed if the model you are using offers the weights in this format.Hide Error Details

ini 复制代码
ValueError: The current `device_map` had weights offloaded to the disk. Please provide an `offload_folder` for them. Alternatively, make sure you have `safetensors` installed if the model you are using offers the weights in this format.
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[2], line 5
      2 from modelscope import GenerationConfig
      4 tokenizer = AutoTokenizer.from_pretrained("qwen/Qwen-7B-Chat", revision = 'v1.0.5',trust_remote_code=True)
----> 5 model = AutoModelForCausalLM.from_pretrained("qwen/Qwen-7B-Chat", revision = 'v1.0.5',device_map="auto", trust_remote_code=True,fp16 = True).eval()
      6 model.generation_config = GenerationConfig.from_pretrained("Qwen/Qwen-7B-Chat",revision = 'v1.0.5', trust_remote_code=True) # 可指定不同的生成长度、top_p等相关超参
      7 model.float()

File /opt/conda/lib/python3.8/site-packages/modelscope/utils/hf_util.py:98, in get_wrapped_class.<locals>.ClassWrapper.from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
     95 else:
     96     model_dir = pretrained_model_name_or_path
---> 98 model = module_class.from_pretrained(model_dir, *model_args,
     99                                      **kwargs)
    100 model.model_dir = model_dir
    101 return model

解决方式

首先确保torch 2.0.1版本,然后在代码中添加这两行,即可运行

model.float()

offload_folder="offload_folder",

python 复制代码
from modelscope import AutoModelForCausalLM, AutoTokenizer
from modelscope import GenerationConfig
import datetime
print("启动时间:" + str(datetime.datetime.now()))
tokenizer = AutoTokenizer.from_pretrained("qwen/Qwen-7B-Chat", revision = 'v1.0.5',trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("qwen/Qwen-7B-Chat", revision = 'v1.0.5',device_map="auto",offload_folder="offload_folder", trust_remote_code=True,fp16 = True).eval()
model.generation_config = GenerationConfig.from_pretrained("Qwen/Qwen-7B-Chat",revision = 'v1.0.5', trust_remote_code=True) # 可指定不同的生成长度、top_p等相关超参
model.float()

print("开始执行:" + str(datetime.datetime.now()))
response, history = model.chat(tokenizer, "你好", history=None)
print(response)
print("第一个问题处理完毕:" + str(datetime.datetime.now()))
response, history = model.chat(tokenizer, "浙江的省会在哪里?", history=history) 
print(response)
print("第二个问题处理完毕:" + str(datetime.datetime.now()))
response, history = model.chat(tokenizer, "它有什么好玩的景点", history=history)
print(response)
print("第三个问题处理完毕:" + str(datetime.datetime.now()))

运行起来之后速度实在感人,没回答一个问题都需要 5 分钟左右,还有一定概率直接启动失败

启动模型过程中会出现这种报错,点击OK重新执行就好了,可能是服务器负载太高

相关推荐
龙的爹23332 小时前
论文 | Model-tuning Via Prompts Makes NLP Models Adversarially Robust
人工智能·gpt·深度学习·语言模型·自然语言处理·prompt
龙的爹23336 小时前
论文翻译 | Generated Knowledge Prompting for Commonsense Reasoning
人工智能·gpt·机器学习·语言模型·自然语言处理·nlp·prompt
龙的爹23336 小时前
论文翻译 | Model-tuning Via Prompts Makes NLP Models Adversarially Robust
人工智能·gpt·语言模型·自然语言处理·nlp·prompt
AAI机器之心7 小时前
LLM大模型:开源RAG框架汇总
人工智能·chatgpt·开源·大模型·llm·大语言模型·rag
杨荧8 小时前
【JAVA开源】基于Vue和SpringBoot的洗衣店订单管理系统
java·开发语言·vue.js·spring boot·spring cloud·开源
玄奕子10 小时前
GPT对话知识库——在STM32的平台下,通过SPI读取和写入Flash的步骤。
stm32·单片机·gpt·嵌入式·嵌入式驱动
FIT2CLOUD飞致云13 小时前
测试管理新增视图与高级搜索功能,测试计划支持一键生成缺陷详情,MeterSphere开源持续测试工具v3.3版本发布
开源·接口测试·metersphere·团队协作·持续测试·测试管理
杨荧15 小时前
【JAVA开源】基于Vue和SpringBoot的旅游管理系统
java·vue.js·spring boot·spring cloud·开源·旅游
杨荧1 天前
【JAVA开源】基于Vue和SpringBoot的水果购物网站
java·开发语言·vue.js·spring boot·spring cloud·开源
x-cmd1 天前
[241005] 14 款最佳免费开源图像处理库 | PostgreSQL 17 正式发布
数据库·图像处理·sql·安全·postgresql·开源·json