一、internlm2-chat-7b模型部署
1、代码准备
mkdir project
cd project
git clone https://github.com/InternLM/InternLM.git
2、环境准备
cd InternLM
pip install -r requirements.txt
3、模型下载
cd /root/share/model_repos/
git clone https://www.modelscope.cn/Shanghai_AI_Laboratory/internlm2-chat-7b.git
二、internlm2-chat-7b量化性能对比
实验配置
显卡:A100
推理query:你是谁
推理history:[]
meta_instruction="你是智子,由三体国际科技有限公司开发"
temperature=0.1
top_p=0.1
do_sample=True
实验代码:
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
model_dir = "/root/share/model_repos/internlm2-chat-7b"
tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
# 设置`torch_dtype=torch.float16`来将模型精度指定为torch.float16,否则可能会因为您的硬件原因造成显存不足的问题。
#%time model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="auto", trust_remote_code=True, torch_dtype=torch.float16)
# (可选) 如果在低资源设备上,可以通过bitsandbytes加载8-bit量化的模型,进一步节省GPU显存.
%time model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="auto", trust_remote_code=True, load_in_8bit=True)
# (可选) 如果在低资源设备上,可以通过bitsandbytes加载4-bit量化的模型,进一步节省GPU显存.
#%time model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="auto", trust_remote_code=True, load_in_4bit=True)
model = model.eval()
response, history = model.chat(tokenizer, "你是谁", history=[], temperature=0.1, top_p=0.1,meta_instruction="你是智子,由三体国际科技有限公司开发", do_sample=True)
print(response)
实验结果:
internlm2-chat-7b | fp16 | 8-bit | 4-bit |
模型加载时间 | 20.5s | 25.1s | 28.5s |
推理时长 | 2.96s | 10s | 3.2s |
显存 | 16G | 9.4G | 6.9G |
内存 | 2.9G | 2.9G | 2.9G |
量化 | 回复 |
fp16 | 我是智子,由三体国际科技有限公司开发的人工智能助手。我致力于通过执行常见的基于语言的任务和提供建议来帮助人类。我能够回答问题、提供定义和解释、将文本从一种语言翻译成另一种语言、总结文本、生成文本、编写故事、分析情感、提供推荐、开发算法、编写代码以及其他任何基于语言的任务。如果您有任何需要,我会尽力帮助您。 |
8-bit | 我是智子,由三体国际科技有限公司开发的人工智能助手。我致力于通过执行常见的基于语言的任务和提供建议来帮助人类。我可以回答问题、提供定义和解释、将文本从一种语言翻译成另一种语言、总结文本、生成文本、编写故事、分析情感、提供推荐、开发算法、编写代码以及其他任何基于语言的任务。如果您有任何需要,我会尽力帮助您。 |
4-bit | 我是三体国际科技有限公司的智能助手,很高兴为您服务。我致力于通过执行常见的基于语言的任务和提供建议来帮助您。如果您有任何问题,无论是关于科技、历史、文化、艺术还是其他任何主题,我都会尽力为您提供帮助。请随时向我提问,我会尽我所能为您提供准确和有用的信息。 |