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大模型对话了。比如输入“请给我讲一个孙悟空的故事吧“