介绍
新一代大语言模型 InternLM2(书生·浦语)在推理、对话体验及工具调用能力上均有显著提升,支持 20 万字超长上下文,能够处理长文档摘要及复杂的多轮对话任务。
InternLM2 包含 InternLM2-7B 和 InternLM2-20B 两种规格。根据应用场景不同,分为 InternLM2-Base、InternLM2、InternLM2-Chat-SFT 和 InternLM2-Chat。其中 InternLM2 为基础模型,InternLM2-Chat 为官方推荐的对话模型。本文以 InternLM2-Chat-7B 为例,详细介绍其本地化部署与使用流程。
| 模型 | HuggingFace | ModelScope |
|---|---|---|
| InternLM2-Chat-7B | internlm/internlm2-chat-7b | Shanghai_AI_Laboratory/internlm2-chat-7b |
| InternLM2-Chat-20B | internlm/internlm2-chat-20b | Shanghai_AI_Laboratory/internlm2-chat-20b |
环境准备
硬件要求
InternLM2-Chat-7B 模型在量化或半精度模式下运行,通常消耗约 20GB 显存。建议使用配备 RTX 3090 或 RTX 4090 等高性能 GPU 的服务器。若显存不足,可考虑使用量化版本或降低 batch size。
软件依赖
- Python 环境:建议 Python 3.8 及以上版本。
- CUDA 驱动:确保显卡驱动已安装且版本兼容。
- 核心库:
transformers:用于加载模型。lmdeploy:用于高性能推理服务。gradio/streamlit:用于构建 Web 交互界面。
模型部署 & 使用
页面交互方式
Gradio 部署
LMDeploy 封装了 Gradio 接口,适合快速搭建演示服务。
启动参数说明:
tp(tensor_parallel_size):张量并行度,表示使用几张 GPU 运行一个模型。max_batch_size:批处理大小,值越大吞吐量越高,但显存占用增加。cache_max_entry_count:K/V 缓存大小设置。小数表示显存百分比,整数表示 block 数量。server_name/server_port:服务监听地址和端口。
部署脚本示例:
cd ~
# 安装运行环境
echo "Installing Python dependencies"
pip install lmdeploy socksio gradio==3.50.2
# 安装 Git LFS 扩展包
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bash
apt-get install -y git-lfs
git lfs install
git https://huggingface.co/internlm/internlm2-chat-7b
python3 -m lmdeploy.serve.gradio.app \
--tp=1 \
--max_batch_size=64 \
--cache_max_entry_count=0.1 \
--server_name=0.0.0.0 \
--server_port=8888 \
./internlm2-chat-7b


