书生浦语第五次课

LMDeploy环境部署

InternStudio开发机创建conda环境

由于环境依赖项存在torch,下载过程可能比较缓慢。InternStudio上提供了快速创建conda环境的方法。打开命令行终端,创建一个名为lmdeploy的环境:

studio-conda -t lmdeploy -o pytorch-2.1.2

环境创建成功后,提示如下:
在这里插入图片描述

安装LMDeploy

执行下面命令

conda activate lmdeploy

pip install lmdeploy[all]==0.3.0

下载模型

由于在InternStudio开发机上,这次直接从/root/share文件中cp到/root/models/Shanghai_AI_Laboratory中

cp -r /root/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-1_8b /root/models/Shanghai_AI_Laboratory

执行后结果如下:
在这里插入图片描述

使用Transformer库运行模型

在InternStudio开发机进行vscode平台,然后创建一个pipeline_transformer.py文件,复制下面的代码,需要将modelpath 地址修改为自己的目录下地址
在这里插入图片描述

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

modelpath = "/root/models/Shanghai_AI_Laboratory/internlm2-chat-1_8b"

tokenizer = AutoTokenizer.from_pretrained(modelpath, trust_remote_code=True)

# Set `torch_dtype=torch.float16` to load model in float16, otherwise it will be loaded as float32 and cause OOM Error.
model = AutoModelForCausalLM.from_pretrained(modelpath, torch_dtype=torch.float16, trust_remote_code=True).cuda()
model = model.eval()

inp = "hello"
print("[INPUT]", inp)
response, history = model.chat(tokenizer, inp, history=[])
print("[OUTPUT]", response)

inp = "please provide three suggestions about time management"
print("[INPUT]", inp)
response, history = model.chat(tokenizer, inp, history=history)
print("[OUTPUT]", response)

初步执行结果如下
在这里插入图片描述

LMDeploy与模型对话

首先激活创建好的conda环境:

conda activate lmdeploy

使用LMDeploy与模型进行对话的通用命令格式为:

lmdeploy chat [HF格式模型路径/TurboMind格式模型路径]

在我自己的机器上执行如下

lmdeploy chat /root/models/Shanghai_AI_Laboratory/internlm2-chat-1_8b

在这里插入图片描述

下面我们就可以与InternLM2-Chat-1.8B大模型对话了。比如输入“请给我讲一个孙悟空的故事吧“
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值