当前位置: 首页 > news >正文

使用 modelscope产生的问题和解决方案

使用 modelscope产生的问题和解决方案

flyfish

代码地址https://github.com/QwenLM/Qwen2

这里将原代码做了更改
例如

模型容易下载
from transformers import AutoModelForCausalLM, AutoTokenizer更改为from modelscope import AutoModelForCausalLM, AutoTokenizer

使用小的模型快速测试
"Qwen/Qwen2-7B-Instruct"更改为"qwen/Qwen2-0.5B-Instruct"

完整代码如下

from modelscope import AutoModelForCausalLM, AutoTokenizermodel_name = "qwen/Qwen2-0.5B-Instruct"
device = "cuda" # the device to load the model ontomodel = AutoModelForCausalLM.from_pretrained(model_name,torch_dtype="auto",device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(model_name)prompt = "Give me a short introduction to large language model."
messages = [{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(messages,tokenize=False,add_generation_prompt=True
)
model_inputs = tokenizer([text], return_tensors="pt").to(device)generated_ids = model.generate(**model_inputs,max_new_tokens=512
)
generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(response)

输出
A large language model is an artificial intelligence system that can generate human-like text from natural language input. These models are designed to mimic the way humans process and understand language, and they can generate sentences based on patterns and relationships in the input text. Large language models have been used for a variety of applications, including chatbots, virtual assistants, and automated decision-making systems. They are becoming increasingly important as we move towards more complex and nuanced interactions with machines, and their development has attracted significant attention from researchers and industry leaders alike.

错误1

from modelscope import AutoModelForCausalLM, AutoTokenizer
ImportError: cannot import name 'AutoModelForCausalLM' from 'modelscope'

modelscope版本低,modelscope1.4.3版本解决问题

pip install modelscope==1.4.3 -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html  -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -U modelscope -i https://pypi.tuna.tsinghua.edu.cn/simple

错误2
transformers 版本低,升级到了transformers-4.44.2解决

Traceback (most recent call last):model = AutoModelForCausalLM.from_pretrained(File "/site-packages/modelscope/utils/hf_util.py", line 65, in from_pretrainedmodule_obj = module_class.from_pretrained(model_dir, *model_args,File "/site-packages/transformers/models/auto/auto_factory.py", line 441, in from_pretrainedconfig, kwargs = AutoConfig.from_pretrained(File "/site-packages/transformers/models/auto/configuration_auto.py", line 917, in from_pretrainedconfig_class = CONFIG_MAPPING[config_dict["model_type"]]File "/site-packages/transformers/models/auto/configuration_auto.py", line 623, in __getitem__raise KeyError(key)
KeyError: 'qwen2'
 pip install -U transformers -i https://pypi.tuna.tsinghua.edu.cn/simple

http://www.mrgr.cn/news/24543.html

相关文章:

  • java 中线程的等待和唤醒
  • 【hot100-java】【接雨水】
  • 使用WordCloud报错‘ImageDraw‘ object has no attribute ‘textbbox‘
  • [leetcode-python]最长回文子串
  • Spring Boot 集成 MongoDB - 入门指南
  • 鸿蒙开发(NEXT/API 12)【WebSocket连接】 网络篇
  • 76-mysql的聚集索引和非聚集索引区别
  • 【专题】2024年8月数字化、数智化行业报告合集汇总PDF分享(附原数据表)
  • 卷积神经网络(一)
  • MongoDB根据字段内容长度查询语句
  • Win10 9月更新补丁KB5043064发布:21H2/22H2用户不容错过!
  • 鸿蒙应用开发者基础认证题库
  • vue3页面组件中怎么获取上一个页面的路由地址
  • 时序预测 | MATLAB实现BKA-XGBoost(黑翅鸢优化算法优化极限梯度提升树)时间序列预测
  • OJ在线评测系统 前端开发设计优化通用菜单组件 初始化JS全局项目入口
  • 安泰功率放大器在压电马达中的应用实例
  • c语言是干嘛的
  • C#Join()和GroupJoin的区别
  • 搭子小程序开发搭建,数字化下的“找搭子”文化
  • 高防IP是如何防御攻击