Task:
- 使用Cli Demo实现InternLM2-Chat-1.8B模型部署,并生成300字小故事。
- 使用LMDeploy完成InternLM-XComposer2-VL-1.8B部署,完成一次图文理解对话。
- 使用LMDeploy完成InternVL2-2B的部署,完成一次图文理解对话。
1 Task1
使用Cli Demo实现InternLM2-Chat-1.8B模型部署,并生成300字小故事。
创建torch环境,在jupyter notebook中执行命令安装环境:
!pip install transformers==4.38 -i https://pypi.tuna.tsinghua.edu.cn/simple
!pip install sentencepiece==0.1.99 -i https://pypi.tuna.tsinghua.edu.cn/simple
!pip install einops==0.8.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
!pip install protobuf==5.27.2 -i https://pypi.tuna.tsinghua.edu.cn/simple
!pip install accelerate==0.33.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
!pip install streamlit==1.37.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
!pip install modelscope -i https://pypi.tuna.tsinghua.edu.cn/simple
下载模型:
from modelscope import snapshot_download
model_dir = snapshot_download('jayhust/internlm2-chat-1_8b')
model_dir
导入预训练模型,加载模型:
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
model_path = '/data/coding/demo/internlm2-chat-1_8b'
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True, device_map='cuda:0')
model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True, device_map='cuda:0', torch_dtype= torch.bfloat16)
model = model.eval()
system_prompt = """You are an AI assistant whose name is InternLM (书生·浦语).
- InternLM (书生·浦语) is a conversational language model that is developed by Shanghai AI Laboratory (上海人工智能实验室). It is designed to be helpful, honest, and harmless.
- InternLM (书生·浦语) can understand and communicate fluently in the language chosen by the user such as English and 中文.
"""
messages = [(system_prompt, '')]
print("=============Welcome to InternLM chatbot, type 'exit' to exit.=============")
while True:
input_text = input("\nUser >>> ")
input_text = input_text.replace(' ', '')
if input_text == "exit":
break
length = 0
for response, _ in model.stream_chat(tokenizer, input_text, messages):
if response is not None:
print(response[length:], flush=True, end="")
length = len(response)
执行截图:
2 Task2
使用LMDeploy完成InternLM-XComposer2-VL-1.8B部署,完成一次图文理解对话。
LMDeploy 是一个用于压缩、部署和服务 LLM 的工具包,具有以下核心功能:
- 高效的推理:LMDeploy 通过引入持久化批处理、块 KV 缓存、动态分割与融合、张量并行、高性能 CUDA 内核等关键技术,提供了比 vLLM 高 1.8 倍的推理性能。
- 有效的量化:LMDeploy 支持仅权重量化和 k/v 量化,4bit 推理性能是 FP16 的 2.4 倍。量化后模型质量已通过 OpenCompass 评估确认。
- 轻松的分发:利用请求分发服务,LMDeploy 可以在多台机器和设备上轻松高效地部署多模型服务。
- 交互式推理模式:通过缓存多轮对话过程中注意力的 k/v,推理引擎记住对话历史,从而避免重复处理历史会话。
- 优秀的兼容性:LMDeploy支持 KV Cache Quant,AWQ 和自动前缀缓存同时使用。
安装环境:
!pip install lmdeploy[all]==0.5.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
!pip install timm==1.0.7 -i https://pypi.tuna.tsinghua.edu.cn/simple
下载模型:
from modelscope import snapshot_download
model_dir = snapshot_download('Shanghai_AI_Laboratory/internlm-xcomposer2-vl-1_8b',cache_dir='/data/coding/demo')
model_dir
解决huggingface.co无法访问的问题:
import os
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
部署模型:
!lmdeploy serve gradio /data/coding/demo/Shanghai_AI_Laboratory/internlm-xcomposer2-vl-1_8b --cache-max-entry-count 0.1
执行结果截图: ![[书生大模型/基础岛/pic/Pasted image 20240731095903.png]]
3 Task3
使用LMDeploy完成InternVL2-2B的部署,完成一次图文理解对话。
模型需经过申请才可下载,申请链接:[[https://www.modelscope.cn/models/OpenGVLab/InternVL2-2B/files]]
导入自己的token,认证:
from modelscope.hub.api import HubApi
api = HubApi()
api.login('XXXXXX')
下载模型:
# 下载模型
from modelscope import snapshot_download
model_dir = snapshot_download('OpenGVLab/InternVL2-2B',cache_dir='/data/coding/demo')
model_dir
部署,执行模型:
!lmdeploy serve gradio /data/coding/demo/OpenGVLab/InternVL2-2B --cache-max-entry-count 0.1
执行截图: