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

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

通义千问-7B-Chat

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

个人电脑部署

这里不太建议使用自己的笔记本部署通义千问模型,因为实在是太耗资源,我使用的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

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

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",

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重新执行就好了,可能是服务器负载太高

### 通义开源模型部署方法教程 #### 准备工作环境 为了成功部署通义(Qwen)开源模型,首先需准备合适的工作环境。这涉及到操作系统的选择以及必要的软件包安装。对于PyTorch的安装,具体命令取决于用户设备是否配备有GPU支持[^3]。 #### 获取Qwen模型及相关资源 创建项目目录并切换至该路径下执行Git克隆操作来获取最新的Qwen仓库文件。此过程涉及使用特定URL地址完成对GitHub上的官方存储库复制动作。随后进入`Qwen`文件夹内继续下一步骤的操作[^4]: ```bash mkdir /workspace/ cd /workspace/ git clone https://github.com/QwenLM/Qwen ``` #### 安装依赖项 在获得所需代码之后,紧接着要做的就是满足所有外部库的需求。这部分可以通过读取两个不同需求列表(`requirements.txt`, `requirements_web_demo.txt`)来进行批量安装,并额外加入几个指定版本号的关键Python模块,如`bitsandbytes`、`accelerate`和可视化工具Streamlit等。另外还需更新APT包索引并安装Git LFS用于处理大型文件传输题: ```bash pip install -r requirements.txt pip install -r requirements_web_demo.txt pip install bitsandbytes==0.41.1 pip install accelerate==0.25.0 pip install streamlit sudo apt update && sudo apt install git-lfs ``` #### 下载预训练权重 接着便是从ModelScope平台拉取实际的预训练参数集。这里同样采用Git LFS协议下的子命令实现远程同步功能,确保能够顺利取得完整的模型结构及其对应的初始化数值集合: ```bash mkdir Qwen cd Qwen git lfs clone https://www.modelscope.cn/ccyh123/Qwen-7B-Chat.git ``` #### 启动交互界面 最后一步则是运行所提供的命令行演示程序(cli_demo.py),以此开启与已加载完毕的语言模型之间的对话交流体验环节。此时应该可以看到终端窗口中显示出由AI生成的回答内容了: ```bash python cli_demo.py ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值