本笔记为昇思25天打卡营第三天,MindNLP体验ChatGLM-6B,打卡截屏和心得体会在笔记末尾。
文章首发自我的个人博客 www.jiewong.com
环境信息:
规格:96G Ascend910
镜像:py39-ms2.2.14-cann7.0.0
特性:预装mindspore(2.2.14)、numpy、pandas、download
代码
依赖安装
pip install mindnlp
pip install mdtex2html
配置网络线路
!export HF_ENDPOINT=https://hf-mirror.com
下载权重
from mindnlp.transformers import AutoModelForSeq2SeqLM, AutoTokenizer
import gradio as gr
import mdtex2html
model = AutoModelForSeq2SeqLM.from_pretrained('ZhipuAI/ChatGLM-6B', mirror="modelscope").half()
model.set_train(False)
tokenizer = AutoTokenizer.from_pretrained('ZhipuAI/ChatGLM-6B', mirror="modelscope")
模型推理
# 训练营打卡
print("打卡人:汪朝杰")
from datetime import datetime
import pytz
# 设置时区为北京时区
beijing_tz = pytz.timezone('Asia/shanghai')
# 获取当前时间,并转为北京时间
current_beijing_time = datetime.now(beijing_tz)
# 格式化时间输出
formatted_time = current_beijing_time.strftime('%Y-%m-%d %H:%M:%S')
print("当前北京时间:",formatted_time)
prompt = '讲个笑话'
history = []
response, _ = model.chat(tokenizer, prompt, history=history, max_length=200)
response
心得体会
今天了解到了 mindnlp 这个库,他支持支持类似 huggingface transformer 的 api 调用
from mindnlp.transformers import AutoModel
model = AutoModel.from_pretrained('bert-base-cased')
很方便,兼容性强。有一点让我困惑,即使我用的是96G的Ascend910,他的推理速度却不尽人意,训练营的群友说是昇腾上部分模型动态图优化的问题。 另外 ,示例代码中的 model 是用 AutoModelForSeq2SeqLM 导入的,我按照 mindnlp 仓库给的示例,换成了AutoModel,但是却报错,没有 chat 方法:
AttributeError: The 'ChatGLMModel' object has no attribute 'chat'.
在mindspore开发者群里问了才知道, transformers 没有chat方法,chat是 chatglm 自己加的。
我真的对 transformers 了解很少啊😂
继续加油💪⛽️